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

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