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

Last change on this file since 2075 was 2075, checked in by Sam Hocevar, 13 years ago
  • Fix memory leaks that slipped in here and there. We still have 100,000 fewer than Firefox, lol.
  • 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 2075 2007-11-26 01:04:34Z 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->resize_callback = callback;
132    cv->resize_data = p;
133    cv->refcount = 1;
134
135    return 0;
136}
137
138/** \brief Unmanage a canvas.
139 *
140 *  Unlock a canvas previously locked by cucul_manage_canvas(). For safety
141 *  reasons, the callback and callback data arguments must be the same as for
142 *  the cucul_manage_canvas() call.
143 *
144 *  This function is only useful for display drivers such as the \e libcaca
145 *  library.
146 *
147 *  If an error occurs, -1 is returned and \b errno is set accordingly:
148 *  - \c EINVAL The canvas is not managed, or the callback arguments do
149 *              not match.
150 *
151 *  \param cv A libcucul canvas.
152 *  \param callback The \e callback argument previously passed to
153                    cucul_manage_canvas().
154 *  \param p The \e p argument previously passed to cucul_manage_canvas().
155 *  \return 0 in case of success, -1 if an error occurred.
156 */
157int cucul_unmanage_canvas(cucul_canvas_t *cv, int (*callback)(void *), void *p)
158{
159    if(!cv->refcount
160        || cv->resize_callback != callback || cv->resize_data != p)
161    {
162        seterrno(EINVAL);
163        return -1;
164    }
165
166    cv->refcount = 0;
167
168    return 0;
169}
170
171/** \brief Resize a canvas.
172 *
173 *  Set the canvas' width and height, in character cells.
174 *
175 *  The contents of the canvas are preserved to the extent of the new
176 *  canvas size. Newly allocated character cells at the right and/or at
177 *  the bottom of the canvas are filled with spaces.
178 *
179 *  If as a result of the resize the cursor coordinates fall outside the
180 *  new canvas boundaries, they are readjusted. For instance, if the
181 *  current X cursor coordinate is 11 and the requested width is 10, the
182 *  new X cursor coordinate will be 10.
183 *
184 *  It is an error to try to resize the canvas if an output driver has
185 *  been attached to the canvas using caca_create_display(). You need to
186 *  remove the output driver using caca_free_display() before you can change
187 *  the canvas size again. However, the caca output driver can cause a
188 *  canvas resize through user interaction. See the caca_event() documentation
189 *  for more about this.
190 *
191 *  If an error occurs, -1 is returned and \b errno is set accordingly:
192 *  - \c EBUSY The canvas is in use by a display driver and cannot be resized.
193 *  - \c ENOMEM Not enough memory for the requested canvas size. If this
194 *    happens, the canvas handle becomes invalid and should not be used.
195 *
196 *  \param cv A libcucul canvas.
197 *  \param width The desired canvas width.
198 *  \param height The desired canvas height.
199 *  \return 0 in case of success, -1 if an error occurred.
200 */
201int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
202                                              unsigned int height)
203{
204    if(cv->refcount && cv->resize_callback
205        && !cv->resize_callback(cv->resize_data))
206    {
207        seterrno(EBUSY);
208        return -1;
209    }
210
211    return cucul_resize(cv, width, height);
212}
213
214/** \brief Get the canvas width.
215 *
216 *  Return the current canvas' width, in character cells.
217 *
218 *  This function never fails.
219 *
220 *  \param cv A libcucul canvas.
221 *  \return The canvas width.
222 */
223unsigned int cucul_get_canvas_width(cucul_canvas_t const *cv)
224{
225    return cv->width;
226}
227
228/** \brief Get the canvas height.
229 *
230 *  Returns the current canvas' height, in character cells.
231 *
232 *  This function never fails.
233 *
234 *  \param cv A libcucul canvas.
235 *  \return The canvas height.
236 */
237unsigned int cucul_get_canvas_height(cucul_canvas_t const *cv)
238{
239    return cv->height;
240}
241
242/** \brief Get the canvas character array.
243 *
244 *  Return the current canvas' internal character array. The array elements
245 *  consist in native endian 32-bit Unicode values as returned by
246 *  cucul_get_char().
247 *
248 *  This function is only useful for display drivers such as the \e libcaca
249 *  library.
250 *
251 *  This function never fails.
252 *
253 *  \param cv A libcucul canvas.
254 *  \return The canvas character array.
255 */
256unsigned char const * cucul_get_canvas_chars(cucul_canvas_t const *cv)
257{
258    return (unsigned char const *)cv->chars;
259}
260
261/** \brief Get the canvas attribute array.
262 *
263 *  Returns the current canvas' internal attribute array. The array elements
264 *  consist in native endian 32-bit attribute values as returned by
265 *  cucul_get_attr().
266 *
267 *  This function is only useful for display drivers such as the \e libcaca
268 *  library.
269 *
270 *  This function never fails.
271 *
272 *  \param cv A libcucul canvas.
273 *  \return The canvas attribute array.
274 */
275unsigned char const * cucul_get_canvas_attrs(cucul_canvas_t const *cv)
276{
277    return (unsigned char const *)cv->attrs;
278}
279
280/** \brief Uninitialise \e libcucul.
281 *
282 *  Free all resources allocated by cucul_create_canvas(). After
283 *  this function has been called, no other \e libcucul functions may be
284 *  used unless a new call to cucul_create_canvas() is done.
285 *
286 *  If an error occurs, -1 is returned and \b errno is set accordingly:
287 *  - \c EBUSY The canvas is in use by a display driver and cannot be freed.
288 *
289 *  \param cv A libcucul canvas.
290 *  \return 0 in case of success, -1 if an error occurred.
291 */
292int cucul_free_canvas(cucul_canvas_t *cv)
293{
294    unsigned int f;
295
296    if(cv->refcount)
297    {
298        seterrno(EBUSY);
299        return -1;
300    }
301
302    for(f = 0; f < cv->framecount; f++)
303    {
304        free(cv->frames[f].chars);
305        free(cv->frames[f].attrs);
306        free(cv->frames[f].name);
307    }
308
309    free(cv->frames);
310    free(cv);
311
312    return 0;
313}
314
315/** \brief Generate a random integer within a range.
316 *
317 *  Generate a random integer within the given range.
318 *
319 *  This function never fails.
320 *
321 *  \param min The lower bound of the integer range.
322 *  \param max The upper bound of the integer range.
323 *  \return A random integer comprised between \p min  and \p max - 1
324 *  (inclusive).
325 */
326int cucul_rand(int min, int max)
327{
328    static int need_init = 1;
329
330    if(need_init)
331    {
332        srand(getpid() + time(NULL));
333        need_init = 0;
334    }
335
336    return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
337}
338
339/** \brief Return the \e libcucul version.
340 *
341 *  Return a read-only string with the \e libcucul version information.
342 *
343 *  This function never fails.
344 *
345 *  \return The \e libcucul version information.
346 */
347char const * cucul_get_version(void)
348{
349    return VERSION;
350}
351
352/*
353 * XXX: The following functions are local.
354 */
355
356int cucul_resize(cucul_canvas_t *cv, unsigned int width, unsigned int height)
357{
358    unsigned int x, y, f, old_width, old_height, new_size, old_size;
359
360    old_width = cv->width;
361    old_height = cv->height;
362    old_size = old_width * old_height;
363
364    _cucul_save_frame_info(cv);
365
366    cv->width = width;
367    cv->height = height;
368    new_size = width * height;
369
370    /* Step 1: if new area is bigger, resize the memory area now. */
371    if(new_size > old_size)
372    {
373        for(f = 0; f < cv->framecount; f++)
374        {
375            cv->frames[f].chars = realloc(cv->frames[f].chars,
376                                          new_size * sizeof(uint32_t));
377            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
378                                          new_size * sizeof(uint32_t));
379            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
380            {
381                seterrno(ENOMEM);
382                return -1;
383            }
384        }
385    }
386
387    /* Step 2: move line data if necessary. */
388    if(width == old_width)
389    {
390        /* Width did not change, which means we do not need to move data. */
391        ;
392    }
393    else if(width > old_width)
394    {
395        /* New width is bigger than old width, which means we need to
396         * copy lines starting from the bottom of the screen otherwise
397         * we will overwrite information. */
398        for(f = 0; f < cv->framecount; f++)
399        {
400            uint32_t *chars = cv->frames[f].chars;
401            uint32_t *attrs = cv->frames[f].attrs;
402
403            for(y = height < old_height ? height : old_height; y--; )
404            {
405                uint32_t attr = cv->frames[f].curattr;
406
407                for(x = old_width; x--; )
408                {
409                    chars[y * width + x] = chars[y * old_width + x];
410                    attrs[y * width + x] = attrs[y * old_width + x];
411                }
412
413                /* Zero the end of the line */
414                for(x = width - old_width; x--; )
415                {
416                    chars[y * width + old_width + x] = (uint32_t)' ';
417                    attrs[y * width + old_width + x] = attr;
418                }
419            }
420        }
421    }
422    else
423    {
424        /* New width is smaller. Copy as many lines as possible. Ignore
425         * the first line, it is already in place. */
426        unsigned int lines = height < old_height ? height : old_height;
427
428        for(f = 0; f < cv->framecount; f++)
429        {
430            uint32_t *chars = cv->frames[f].chars;
431            uint32_t *attrs = cv->frames[f].attrs;
432
433            for(y = 1; y < lines; y++)
434            {
435                for(x = 0; x < width; x++)
436                {
437                    chars[y * width + x] = chars[y * old_width + x];
438                    attrs[y * width + x] = attrs[y * old_width + x];
439                }
440            }
441        }
442    }
443
444    /* Step 3: fill the bottom of the new screen if necessary. */
445    if(height > old_height)
446    {
447        for(f = 0; f < cv->framecount; f++)
448        {
449            uint32_t *chars = cv->frames[f].chars;
450            uint32_t *attrs = cv->frames[f].attrs;
451            uint32_t attr = cv->frames[f].curattr;
452
453            /* Zero the bottom of the screen */
454            for(x = (height - old_height) * width; x--; )
455            {
456                chars[old_height * width + x] = (uint32_t)' ';
457                attrs[old_height * width + x] = attr;
458            }
459        }
460    }
461
462    /* Step 4: if new area is smaller, resize memory area now. */
463    if(new_size < old_size)
464    {
465        for(f = 0; f < cv->framecount; f++)
466        {
467            cv->frames[f].chars = realloc(cv->frames[f].chars,
468                                          new_size * sizeof(uint32_t));
469            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
470                                          new_size * sizeof(uint32_t));
471            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
472            {
473                seterrno(ENOMEM);
474                return -1;
475            }
476        }
477    }
478
479    /* Set new size */
480    for(f = 0; f < cv->framecount; f++)
481    {
482        if(cv->frames[f].x > (int)width)
483            cv->frames[f].x = width;
484        if(cv->frames[f].y > (int)height)
485            cv->frames[f].y = height;
486
487        cv->frames[f].width = width;
488        cv->frames[f].height = height;
489    }
490
491    /* Reset the current frame shortcuts */
492    _cucul_load_frame_info(cv);
493
494    return 0;
495}
496
Note: See TracBrowser for help on using the repository browser.