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

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