source: libcaca/trunk/cucul/cucul.c @ 2074

Last change on this file since 2074 was 2074, checked in by Sam Hocevar, 13 years ago
  • Add cucul_get_version() and caca_get_version() and updated C++ bindings accordingly.
  • The C++ test example no longer needs "config.h".
  • Property svn:keywords set to Id
File size: 14.1 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: cucul.c 2074 2007-11-26 01:04:32Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains the main functions used by \e libcucul applications
17 *  to initialise a drawing context.
18 */
19
20#include "config.h"
21#include "common.h"
22
23#if !defined(__KERNEL__)
24#   include <stdio.h>
25#   include <stdlib.h>
26#   include <string.h>
27#   include <time.h>
28#   include <sys/types.h>
29#   if defined(HAVE_UNISTD_H)
30#       include <unistd.h>
31#   endif
32#endif
33
34#include "cucul.h"
35#include "cucul_internals.h"
36
37static int cucul_resize(cucul_canvas_t *, unsigned int, unsigned int);
38
39/** \brief Initialise a \e libcucul canvas.
40 *
41 *  Initialise internal \e libcucul structures and the backend that will
42 *  be used for subsequent graphical operations. It must be the first
43 *  \e libcucul function to be called in a function. cucul_free_canvas()
44 *  should be called at the end of the program to free all allocated resources.
45 *
46 *  Both the cursor and the canvas' handle are initialised at the top-left
47 *  corner.
48 *
49 *  If an error occurs, NULL is returned and \b errno is set accordingly:
50 *  - \c ENOMEM Not enough memory for the requested canvas size.
51 *
52 *  \param width The desired canvas width
53 *  \param height The desired canvas height
54 *  \return A libcucul canvas handle upon success, NULL if an error occurred.
55 */
56cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height)
57{
58    cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
59
60    if(!cv)
61        goto nomem;
62
63    cv->refcount = 0;
64    cv->autoinc = 0;
65    cv->resize_callback = NULL;
66    cv->resize_data = NULL;
67
68    cv->frame = 0;
69    cv->framecount = 1;
70    cv->frames = malloc(sizeof(struct cucul_frame));
71    if(!cv->frames)
72    {
73        free(cv);
74        goto nomem;
75    }
76
77    cv->frames[0].width = cv->frames[0].height = 0;
78    cv->frames[0].chars = NULL;
79    cv->frames[0].attrs = NULL;
80    cv->frames[0].x = cv->frames[0].y = 0;
81    cv->frames[0].handlex = cv->frames[0].handley = 0;
82    cv->frames[0].curattr = 0;
83    cv->frames[0].name = strdup("frame#00000000");
84
85    _cucul_load_frame_info(cv);
86    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
87
88    if(cucul_resize(cv, width, height) < 0)
89    {
90        int saved_errno = geterrno();
91        free(cv->frames[0].name);
92        free(cv->frames);
93        free(cv);
94        seterrno(saved_errno);
95        return NULL;
96    }
97
98    return cv;
99
100nomem:
101    seterrno(ENOMEM);
102    return NULL;
103}
104
105/** \brief Manage a canvas.
106 *
107 *  Lock a canvas to prevent it from being resized. If non-NULL,
108 *  the \e callback function pointer will be called upon each
109 *  \e cucul_set_canvas_size call and if the returned value is zero, the
110 *  canvas resize request will be denied.
111 *
112 *  This function is only useful for display drivers such as the \e libcaca
113 *  library.
114 *
115 *  If an error occurs, -1 is returned and \b errno is set accordingly:
116 *  - \c EBUSY The canvas is already being managed.
117 *
118 *  \param cv A libcucul canvas.
119 *  \param callback An optional callback function pointer.
120 *  \param p The argument to be passed to \e callback.
121 *  \return 0 in case of success, -1 if an error occurred.
122 */
123int cucul_manage_canvas(cucul_canvas_t *cv, int (*callback)(void *), void *p)
124{
125    if(cv->refcount)
126    {
127        seterrno(EBUSY);
128        return -1;
129    }
130
131    cv->refcount = 1;
132
133    return 0;
134}
135
136/** \brief Unmanage a canvas.
137 *
138 *  Unlock a canvas previously locked by cucul_manage_canvas(). For safety
139 *  reasons, the callback and callback data arguments must be the same as for
140 *  the cucul_manage_canvas() call.
141 *
142 *  This function is only useful for display drivers such as the \e libcaca
143 *  library.
144 *
145 *  If an error occurs, -1 is returned and \b errno is set accordingly:
146 *  - \c EINVAL The canvas is not managed, or the callback arguments do
147 *              not match.
148 *
149 *  \param cv A libcucul canvas.
150 *  \param callback The \e callback argument previously passed to
151                    cucul_manage_canvas().
152 *  \param p The \e p argument previously passed to cucul_manage_canvas().
153 *  \return 0 in case of success, -1 if an error occurred.
154 */
155int cucul_unmanage_canvas(cucul_canvas_t *cv, int (*callback)(void *), void *p)
156{
157    if(!cv->refcount
158        || cv->resize_callback != callback || cv->resize_data != p)
159    {
160        seterrno(EINVAL);
161        return -1;
162    }
163
164    cv->refcount = 0;
165
166    return 0;
167}
168
169/** \brief Resize a canvas.
170 *
171 *  Set the canvas' width and height, in character cells.
172 *
173 *  The contents of the canvas are preserved to the extent of the new
174 *  canvas size. Newly allocated character cells at the right and/or at
175 *  the bottom of the canvas are filled with spaces.
176 *
177 *  If as a result of the resize the cursor coordinates fall outside the
178 *  new canvas boundaries, they are readjusted. For instance, if the
179 *  current X cursor coordinate is 11 and the requested width is 10, the
180 *  new X cursor coordinate will be 10.
181 *
182 *  It is an error to try to resize the canvas if an output driver has
183 *  been attached to the canvas using caca_create_display(). You need to
184 *  remove the output driver using caca_free_display() before you can change
185 *  the canvas size again. However, the caca output driver can cause a
186 *  canvas resize through user interaction. See the caca_event() documentation
187 *  for more about this.
188 *
189 *  If an error occurs, -1 is returned and \b errno is set accordingly:
190 *  - \c EBUSY The canvas is in use by a display driver and cannot be resized.
191 *  - \c ENOMEM Not enough memory for the requested canvas size. If this
192 *    happens, the canvas handle becomes invalid and should not be used.
193 *
194 *  \param cv A libcucul canvas.
195 *  \param width The desired canvas width.
196 *  \param height The desired canvas height.
197 *  \return 0 in case of success, -1 if an error occurred.
198 */
199int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
200                                              unsigned int height)
201{
202    if(cv->refcount && cv->resize_callback
203        && !cv->resize_callback(cv->resize_data))
204    {
205        seterrno(EBUSY);
206        return -1;
207    }
208
209    return cucul_resize(cv, width, height);
210}
211
212/** \brief Get the canvas width.
213 *
214 *  Return the current canvas' width, in character cells.
215 *
216 *  This function never fails.
217 *
218 *  \param cv A libcucul canvas.
219 *  \return The canvas width.
220 */
221unsigned int cucul_get_canvas_width(cucul_canvas_t const *cv)
222{
223    return cv->width;
224}
225
226/** \brief Get the canvas height.
227 *
228 *  Returns the current canvas' height, in character cells.
229 *
230 *  This function never fails.
231 *
232 *  \param cv A libcucul canvas.
233 *  \return The canvas height.
234 */
235unsigned int cucul_get_canvas_height(cucul_canvas_t const *cv)
236{
237    return cv->height;
238}
239
240/** \brief Get the canvas character array.
241 *
242 *  Return the current canvas' internal character array. The array elements
243 *  consist in native endian 32-bit Unicode values as returned by
244 *  cucul_get_char().
245 *
246 *  This function is only useful for display drivers such as the \e libcaca
247 *  library.
248 *
249 *  This function never fails.
250 *
251 *  \param cv A libcucul canvas.
252 *  \return The canvas character array.
253 */
254unsigned char const * cucul_get_canvas_chars(cucul_canvas_t const *cv)
255{
256    return (unsigned char const *)cv->chars;
257}
258
259/** \brief Get the canvas attribute array.
260 *
261 *  Returns the current canvas' internal attribute array. The array elements
262 *  consist in native endian 32-bit attribute values as returned by
263 *  cucul_get_attr().
264 *
265 *  This function is only useful for display drivers such as the \e libcaca
266 *  library.
267 *
268 *  This function never fails.
269 *
270 *  \param cv A libcucul canvas.
271 *  \return The canvas attribute array.
272 */
273unsigned char const * cucul_get_canvas_attrs(cucul_canvas_t const *cv)
274{
275    return (unsigned char const *)cv->attrs;
276}
277
278/** \brief Uninitialise \e libcucul.
279 *
280 *  Free all resources allocated by cucul_create_canvas(). After
281 *  this function has been called, no other \e libcucul functions may be
282 *  used unless a new call to cucul_create_canvas() is done.
283 *
284 *  If an error occurs, -1 is returned and \b errno is set accordingly:
285 *  - \c EBUSY The canvas is in use by a display driver and cannot be freed.
286 *
287 *  \param cv A libcucul canvas.
288 *  \return 0 in case of success, -1 if an error occurred.
289 */
290int cucul_free_canvas(cucul_canvas_t *cv)
291{
292    unsigned int f;
293
294    if(cv->refcount)
295    {
296        seterrno(EBUSY);
297        return -1;
298    }
299
300    for(f = 0; f < cv->framecount; f++)
301    {
302        free(cv->frames[f].chars);
303        free(cv->frames[f].attrs);
304        free(cv->frames[f].name);
305    }
306
307    free(cv->frames);
308    free(cv);
309
310    return 0;
311}
312
313/** \brief Generate a random integer within a range.
314 *
315 *  Generate a random integer within the given range.
316 *
317 *  This function never fails.
318 *
319 *  \param min The lower bound of the integer range.
320 *  \param max The upper bound of the integer range.
321 *  \return A random integer comprised between \p min  and \p max - 1
322 *  (inclusive).
323 */
324int cucul_rand(int min, int max)
325{
326    static int need_init = 1;
327
328    if(need_init)
329    {
330        srand(getpid() + time(NULL));
331        need_init = 0;
332    }
333
334    return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
335}
336
337/** \brief Return the \e libcucul version.
338 *
339 *  Return a read-only string with the \e libcucul version information.
340 *
341 *  This function never fails.
342 *
343 *  \return The \e libcucul version information.
344 */
345char const * cucul_get_version(void)
346{
347    return VERSION;
348}
349
350/*
351 * XXX: The following functions are local.
352 */
353
354int cucul_resize(cucul_canvas_t *cv, unsigned int width, unsigned int height)
355{
356    unsigned int x, y, f, old_width, old_height, new_size, old_size;
357
358    old_width = cv->width;
359    old_height = cv->height;
360    old_size = old_width * old_height;
361
362    _cucul_save_frame_info(cv);
363
364    cv->width = width;
365    cv->height = height;
366    new_size = width * height;
367
368    /* Step 1: if new area is bigger, resize the memory area now. */
369    if(new_size > old_size)
370    {
371        for(f = 0; f < cv->framecount; f++)
372        {
373            cv->frames[f].chars = realloc(cv->frames[f].chars,
374                                          new_size * sizeof(uint32_t));
375            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
376                                          new_size * sizeof(uint32_t));
377            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
378            {
379                seterrno(ENOMEM);
380                return -1;
381            }
382        }
383    }
384
385    /* Step 2: move line data if necessary. */
386    if(width == old_width)
387    {
388        /* Width did not change, which means we do not need to move data. */
389        ;
390    }
391    else if(width > old_width)
392    {
393        /* New width is bigger than old width, which means we need to
394         * copy lines starting from the bottom of the screen otherwise
395         * we will overwrite information. */
396        for(f = 0; f < cv->framecount; f++)
397        {
398            uint32_t *chars = cv->frames[f].chars;
399            uint32_t *attrs = cv->frames[f].attrs;
400
401            for(y = height < old_height ? height : old_height; y--; )
402            {
403                uint32_t attr = cv->frames[f].curattr;
404
405                for(x = old_width; x--; )
406                {
407                    chars[y * width + x] = chars[y * old_width + x];
408                    attrs[y * width + x] = attrs[y * old_width + x];
409                }
410
411                /* Zero the end of the line */
412                for(x = width - old_width; x--; )
413                {
414                    chars[y * width + old_width + x] = (uint32_t)' ';
415                    attrs[y * width + old_width + x] = attr;
416                }
417            }
418        }
419    }
420    else
421    {
422        /* New width is smaller. Copy as many lines as possible. Ignore
423         * the first line, it is already in place. */
424        unsigned int lines = height < old_height ? height : old_height;
425
426        for(f = 0; f < cv->framecount; f++)
427        {
428            uint32_t *chars = cv->frames[f].chars;
429            uint32_t *attrs = cv->frames[f].attrs;
430
431            for(y = 1; y < lines; y++)
432            {
433                for(x = 0; x < width; x++)
434                {
435                    chars[y * width + x] = chars[y * old_width + x];
436                    attrs[y * width + x] = attrs[y * old_width + x];
437                }
438            }
439        }
440    }
441
442    /* Step 3: fill the bottom of the new screen if necessary. */
443    if(height > old_height)
444    {
445        for(f = 0; f < cv->framecount; f++)
446        {
447            uint32_t *chars = cv->frames[f].chars;
448            uint32_t *attrs = cv->frames[f].attrs;
449            uint32_t attr = cv->frames[f].curattr;
450
451            /* Zero the bottom of the screen */
452            for(x = (height - old_height) * width; x--; )
453            {
454                chars[old_height * width + x] = (uint32_t)' ';
455                attrs[old_height * width + x] = attr;
456            }
457        }
458    }
459
460    /* Step 4: if new area is smaller, resize memory area now. */
461    if(new_size < old_size)
462    {
463        for(f = 0; f < cv->framecount; f++)
464        {
465            cv->frames[f].chars = realloc(cv->frames[f].chars,
466                                          new_size * sizeof(uint32_t));
467            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
468                                          new_size * sizeof(uint32_t));
469            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
470            {
471                seterrno(ENOMEM);
472                return -1;
473            }
474        }
475    }
476
477    /* Set new size */
478    for(f = 0; f < cv->framecount; f++)
479    {
480        if(cv->frames[f].x > (int)width)
481            cv->frames[f].x = width;
482        if(cv->frames[f].y > (int)height)
483            cv->frames[f].y = height;
484
485        cv->frames[f].width = width;
486        cv->frames[f].height = height;
487    }
488
489    /* Reset the current frame shortcuts */
490    _cucul_load_frame_info(cv);
491
492    return 0;
493}
494
Note: See TracBrowser for help on using the repository browser.