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
RevLine 
[524]1/*
[672]2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
[524]3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
[769]6 *  $Id: cucul.c 2299 2008-04-19 12:42:50Z sam $
7 *
[1462]8 *  This library is free software. It comes without any warranty, to
[1452]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
[524]12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
[769]15/*
[524]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
[568]22#if !defined(__KERNEL__)
[724]23#   include <stdio.h>
[568]24#   include <stdlib.h>
25#   include <string.h>
[1022]26#   include <time.h>
27#   include <sys/types.h>
28#   if defined(HAVE_UNISTD_H)
29#       include <unistd.h>
30#   endif
[568]31#endif
[524]32
33#include "cucul.h"
34#include "cucul_internals.h"
35
[2055]36static int cucul_resize(cucul_canvas_t *, unsigned int, unsigned int);
37
[663]38/** \brief Initialise a \e libcucul canvas.
[524]39 *
[1231]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()
[813]43 *  should be called at the end of the program to free all allocated resources.
[524]44 *
[1342]45 *  Both the cursor and the canvas' handle are initialised at the top-left
46 *  corner.
47 *
[1005]48 *  If an error occurs, NULL is returned and \b errno is set accordingly:
[874]49 *  - \c ENOMEM Not enough memory for the requested canvas size.
50 *
[665]51 *  \param width The desired canvas width
52 *  \param height The desired canvas height
[773]53 *  \return A libcucul canvas handle upon success, NULL if an error occurred.
[524]54 */
[813]55cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height)
[524]56{
[811]57    cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
[524]58
[874]59    if(!cv)
60        goto nomem;
61
[811]62    cv->refcount = 0;
[1390]63    cv->autoinc = 0;
[2055]64    cv->resize_callback = NULL;
65    cv->resize_data = NULL;
[669]66
[842]67    cv->frame = 0;
68    cv->framecount = 1;
[1338]69    cv->frames = malloc(sizeof(struct cucul_frame));
70    if(!cv->frames)
[874]71    {
72        free(cv);
73        goto nomem;
74    }
[842]75
[1338]76    cv->frames[0].width = cv->frames[0].height = 0;
77    cv->frames[0].chars = NULL;
78    cv->frames[0].attrs = NULL;
[1342]79    cv->frames[0].x = cv->frames[0].y = 0;
80    cv->frames[0].handlex = cv->frames[0].handley = 0;
[1381]81    cv->frames[0].curattr = 0;
[1390]82    cv->frames[0].name = strdup("frame#00000000");
[1338]83
[1381]84    _cucul_load_frame_info(cv);
85    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
86
[2110]87    cv->ff = NULL;
88
[2055]89    if(cucul_resize(cv, width, height) < 0)
[524]90    {
[1362]91        int saved_errno = geterrno();
[1390]92        free(cv->frames[0].name);
[1338]93        free(cv->frames);
[811]94        free(cv);
[1362]95        seterrno(saved_errno);
[524]96        return NULL;
97    }
98
[811]99    return cv;
[874]100
101nomem:
[1362]102    seterrno(ENOMEM);
[874]103    return NULL;
[524]104}
105
[2055]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
[2075]132    cv->resize_callback = callback;
133    cv->resize_data = p;
[2055]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
[701]172/** \brief Resize a canvas.
[524]173 *
[1231]174 *  Set the canvas' width and height, in character cells.
[524]175 *
[665]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 *
[1813]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 *
[552]185 *  It is an error to try to resize the canvas if an output driver has
[819]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
[1813]188 *  the canvas size again. However, the caca output driver can cause a
[819]189 *  canvas resize through user interaction. See the caca_event() documentation
[665]190 *  for more about this.
[552]191 *
[874]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 *
[2055]197 *  \param cv A libcucul canvas.
198 *  \param width The desired canvas width.
199 *  \param height The desired canvas height.
[874]200 *  \return 0 in case of success, -1 if an error occurred.
[524]201 */
[874]202int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
203                                              unsigned int height)
[524]204{
[2055]205    if(cv->refcount && cv->resize_callback
206        && !cv->resize_callback(cv->resize_data))
[874]207    {
[1362]208        seterrno(EBUSY);
[874]209        return -1;
210    }
[527]211
[2055]212    return cucul_resize(cv, width, height);
[524]213}
214
[552]215/** \brief Get the canvas width.
[524]216 *
[1231]217 *  Return the current canvas' width, in character cells.
[524]218 *
[874]219 *  This function never fails.
220 *
[2055]221 *  \param cv A libcucul canvas.
[552]222 *  \return The canvas width.
[524]223 */
[2043]224unsigned int cucul_get_canvas_width(cucul_canvas_t const *cv)
[524]225{
[811]226    return cv->width;
[524]227}
228
[552]229/** \brief Get the canvas height.
[524]230 *
[1231]231 *  Returns the current canvas' height, in character cells.
[524]232 *
[874]233 *  This function never fails.
234 *
[2055]235 *  \param cv A libcucul canvas.
[552]236 *  \return The canvas height.
[524]237 */
[2043]238unsigned int cucul_get_canvas_height(cucul_canvas_t const *cv)
[524]239{
[811]240    return cv->height;
[524]241}
242
[2056]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
[524]281/** \brief Uninitialise \e libcucul.
282 *
[1231]283 *  Free all resources allocated by cucul_create_canvas(). After
284 *  this function has been called, no other \e libcucul functions may be
[813]285 *  used unless a new call to cucul_create_canvas() is done.
[773]286 *
[874]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 *
[2055]290 *  \param cv A libcucul canvas.
[874]291 *  \return 0 in case of success, -1 if an error occurred.
[524]292 */
[874]293int cucul_free_canvas(cucul_canvas_t *cv)
[524]294{
[842]295    unsigned int f;
296
[874]297    if(cv->refcount)
298    {
[1362]299        seterrno(EBUSY);
[874]300        return -1;
301    }
302
[842]303    for(f = 0; f < cv->framecount; f++)
304    {
[1338]305        free(cv->frames[f].chars);
306        free(cv->frames[f].attrs);
[1390]307        free(cv->frames[f].name);
[842]308    }
[524]309
[2111]310    cucul_canvas_set_figfont(cv, NULL);
[2110]311
[1338]312    free(cv->frames);
[811]313    free(cv);
[874]314
315    return 0;
[609]316}
[560]317
[795]318/** \brief Generate a random integer within a range.
319 *
[1231]320 *  Generate a random integer within the given range.
321 *
[874]322 *  This function never fails.
323 *
[795]324 *  \param min The lower bound of the integer range.
325 *  \param max The upper bound of the integer range.
[815]326 *  \return A random integer comprised between \p min  and \p max - 1
327 *  (inclusive).
[795]328 */
329int cucul_rand(int min, int max)
330{
[1022]331    static int need_init = 1;
332
333    if(need_init)
334    {
335        srand(getpid() + time(NULL));
336        need_init = 0;
337    }
338
[815]339    return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
[795]340}
341
[2074]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
[524]355/*
356 * XXX: The following functions are local.
357 */
358
[2055]359int cucul_resize(cucul_canvas_t *cv, unsigned int width, unsigned int height)
[552]360{
[842]361    unsigned int x, y, f, old_width, old_height, new_size, old_size;
[552]362
[811]363    old_width = cv->width;
364    old_height = cv->height;
[552]365    old_size = old_width * old_height;
366
[1381]367    _cucul_save_frame_info(cv);
368
[811]369    cv->width = width;
370    cv->height = height;
[552]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    {
[842]376        for(f = 0; f < cv->framecount; f++)
377        {
[1338]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))
[874]383            {
[1362]384                seterrno(ENOMEM);
[874]385                return -1;
386            }
[842]387        }
[552]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. */
[842]401        for(f = 0; f < cv->framecount; f++)
[552]402        {
[1338]403            uint32_t *chars = cv->frames[f].chars;
404            uint32_t *attrs = cv->frames[f].attrs;
[842]405
406            for(y = height < old_height ? height : old_height; y--; )
[552]407            {
[1338]408                uint32_t attr = cv->frames[f].curattr;
[1088]409
[842]410                for(x = old_width; x--; )
411                {
412                    chars[y * width + x] = chars[y * old_width + x];
[1254]413                    attrs[y * width + x] = attrs[y * old_width + x];
[842]414                }
415
416                /* Zero the end of the line */
417                for(x = width - old_width; x--; )
[1088]418                {
[842]419                    chars[y * width + old_width + x] = (uint32_t)' ';
[1254]420                    attrs[y * width + old_width + x] = attr;
[1088]421                }
[552]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
[842]431        for(f = 0; f < cv->framecount; f++)
[552]432        {
[1338]433            uint32_t *chars = cv->frames[f].chars;
434            uint32_t *attrs = cv->frames[f].attrs;
[842]435
436            for(y = 1; y < lines; y++)
[552]437            {
[842]438                for(x = 0; x < width; x++)
439                {
440                    chars[y * width + x] = chars[y * old_width + x];
[1254]441                    attrs[y * width + x] = attrs[y * old_width + x];
[842]442                }
[552]443            }
444        }
445    }
446
447    /* Step 3: fill the bottom of the new screen if necessary. */
448    if(height > old_height)
449    {
[842]450        for(f = 0; f < cv->framecount; f++)
451        {
[1338]452            uint32_t *chars = cv->frames[f].chars;
453            uint32_t *attrs = cv->frames[f].attrs;
454            uint32_t attr = cv->frames[f].curattr;
[842]455
456            /* Zero the bottom of the screen */
457            for(x = (height - old_height) * width; x--; )
[1088]458            {
[842]459                chars[old_height * width + x] = (uint32_t)' ';
[1254]460                attrs[old_height * width + x] = attr;
[1088]461            }
[842]462        }
[552]463    }
464
465    /* Step 4: if new area is smaller, resize memory area now. */
[1338]466    if(new_size < old_size)
[552]467    {
[842]468        for(f = 0; f < cv->framecount; f++)
469        {
[1338]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))
[874]475            {
[1362]476                seterrno(ENOMEM);
[874]477                return -1;
478            }
[842]479        }
[552]480    }
[842]481
[1338]482    /* Set new size */
483    for(f = 0; f < cv->framecount; f++)
484    {
[1813]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
[1338]490        cv->frames[f].width = width;
491        cv->frames[f].height = height;
492    }
[874]493
[1338]494    /* Reset the current frame shortcuts */
[1381]495    _cucul_load_frame_info(cv);
[1338]496
[874]497    return 0;
[552]498}
499
Note: See TracBrowser for help on using the repository browser.