source: libcaca/trunk/caca/canvas.c @ 2826

Last change on this file since 2826 was 2826, checked in by Sam Hocevar, 12 years ago

Continue the libcaca/libcucul merge. Source and binary compatibility
are almost there.

  • Property svn:keywords set to Id
File size: 14.9 KB
RevLine 
[524]1/*
[2821]2 *  libcaca       Colour ASCII-Art library
[524]3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
[769]6 *  $Id: canvas.c 2826 2008-09-27 15:43:01Z 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/*
[2821]16 *  This file contains the main functions used by \e libcaca applications
[524]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
[2821]33#include "caca.h"
34#include "caca_internals.h"
[524]35
[2821]36static int caca_resize(caca_canvas_t *, int, int);
[2055]37
[2821]38/** \brief Initialise a \e libcaca canvas.
[524]39 *
[2821]40 *  Initialise internal \e libcaca structures and the backend that will
[1231]41 *  be used for subsequent graphical operations. It must be the first
[2821]42 *  \e libcaca function to be called in a function. caca_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:
[2305]49 *  - \c EINVAL Specified width or height is invalid.
[874]50 *  - \c ENOMEM Not enough memory for the requested canvas size.
51 *
[665]52 *  \param width The desired canvas width
53 *  \param height The desired canvas height
[2821]54 *  \return A libcaca canvas handle upon success, NULL if an error occurred.
[524]55 */
[2821]56caca_canvas_t * caca_create_canvas(int width, int height)
[524]57{
[2821]58    caca_canvas_t *cv;
[524]59
[2305]60    if(width < 0 || height < 0)
61    {
62        seterrno(EINVAL);
63        return NULL;
64    }
65
[2821]66    cv = malloc(sizeof(caca_canvas_t));
[2305]67
[874]68    if(!cv)
69        goto nomem;
70
[811]71    cv->refcount = 0;
[1390]72    cv->autoinc = 0;
[2055]73    cv->resize_callback = NULL;
74    cv->resize_data = NULL;
[669]75
[842]76    cv->frame = 0;
77    cv->framecount = 1;
[2821]78    cv->frames = malloc(sizeof(struct caca_frame));
[1338]79    if(!cv->frames)
[874]80    {
81        free(cv);
82        goto nomem;
83    }
[842]84
[1338]85    cv->frames[0].width = cv->frames[0].height = 0;
86    cv->frames[0].chars = NULL;
87    cv->frames[0].attrs = NULL;
[1342]88    cv->frames[0].x = cv->frames[0].y = 0;
89    cv->frames[0].handlex = cv->frames[0].handley = 0;
[1381]90    cv->frames[0].curattr = 0;
[1390]91    cv->frames[0].name = strdup("frame#00000000");
[1338]92
[2821]93    _caca_load_frame_info(cv);
94    caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
[1381]95
[2110]96    cv->ff = NULL;
97
[2821]98    if(caca_resize(cv, width, height) < 0)
[524]99    {
[1362]100        int saved_errno = geterrno();
[1390]101        free(cv->frames[0].name);
[1338]102        free(cv->frames);
[811]103        free(cv);
[1362]104        seterrno(saved_errno);
[524]105        return NULL;
106    }
107
[811]108    return cv;
[874]109
110nomem:
[1362]111    seterrno(ENOMEM);
[874]112    return NULL;
[524]113}
114
[2055]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
[2821]119 *  \e caca_set_canvas_size call and if the returned value is zero, the
[2055]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 *
[2821]128 *  \param cv A libcaca canvas.
[2055]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 */
[2821]133int caca_manage_canvas(caca_canvas_t *cv, int (*callback)(void *), void *p)
[2055]134{
135    if(cv->refcount)
136    {
137        seterrno(EBUSY);
138        return -1;
139    }
140
[2075]141    cv->resize_callback = callback;
142    cv->resize_data = p;
[2055]143    cv->refcount = 1;
144
145    return 0;
146}
147
148/** \brief Unmanage a canvas.
149 *
[2821]150 *  Unlock a canvas previously locked by caca_manage_canvas(). For safety
[2055]151 *  reasons, the callback and callback data arguments must be the same as for
[2821]152 *  the caca_manage_canvas() call.
[2055]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 *
[2821]161 *  \param cv A libcaca canvas.
[2055]162 *  \param callback The \e callback argument previously passed to
[2821]163                    caca_manage_canvas().
164 *  \param p The \e p argument previously passed to caca_manage_canvas().
[2055]165 *  \return 0 in case of success, -1 if an error occurred.
166 */
[2821]167int caca_unmanage_canvas(caca_canvas_t *cv, int (*callback)(void *), void *p)
[2055]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
[701]181/** \brief Resize a canvas.
[524]182 *
[1231]183 *  Set the canvas' width and height, in character cells.
[524]184 *
[665]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 *
[1813]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 *
[552]194 *  It is an error to try to resize the canvas if an output driver has
[819]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
[1813]197 *  the canvas size again. However, the caca output driver can cause a
[819]198 *  canvas resize through user interaction. See the caca_event() documentation
[665]199 *  for more about this.
[552]200 *
[874]201 *  If an error occurs, -1 is returned and \b errno is set accordingly:
[2305]202 *  - \c EINVAL Specified width or height is invalid.
[874]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 *
[2821]207 *  \param cv A libcaca canvas.
[2055]208 *  \param width The desired canvas width.
209 *  \param height The desired canvas height.
[874]210 *  \return 0 in case of success, -1 if an error occurred.
[524]211 */
[2821]212int caca_set_canvas_size(caca_canvas_t *cv, int width, int height)
[524]213{
[2305]214    if(width < 0 || height < 0)
215    {
216        seterrno(EINVAL);
217        return -1;
218    }
219
[2055]220    if(cv->refcount && cv->resize_callback
221        && !cv->resize_callback(cv->resize_data))
[874]222    {
[1362]223        seterrno(EBUSY);
[874]224        return -1;
225    }
[527]226
[2821]227    return caca_resize(cv, width, height);
[524]228}
229
[552]230/** \brief Get the canvas width.
[524]231 *
[1231]232 *  Return the current canvas' width, in character cells.
[524]233 *
[874]234 *  This function never fails.
235 *
[2821]236 *  \param cv A libcaca canvas.
[552]237 *  \return The canvas width.
[524]238 */
[2821]239int caca_get_canvas_width(caca_canvas_t const *cv)
[524]240{
[811]241    return cv->width;
[524]242}
243
[552]244/** \brief Get the canvas height.
[524]245 *
[1231]246 *  Returns the current canvas' height, in character cells.
[524]247 *
[874]248 *  This function never fails.
249 *
[2821]250 *  \param cv A libcaca canvas.
[552]251 *  \return The canvas height.
[524]252 */
[2821]253int caca_get_canvas_height(caca_canvas_t const *cv)
[524]254{
[811]255    return cv->height;
[524]256}
257
[2056]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
[2821]262 *  caca_get_char().
[2056]263 *
264 *  This function is only useful for display drivers such as the \e libcaca
265 *  library.
266 *
267 *  This function never fails.
268 *
[2821]269 *  \param cv A libcaca canvas.
[2056]270 *  \return The canvas character array.
271 */
[2821]272uint8_t const * caca_get_canvas_chars(caca_canvas_t const *cv)
[2056]273{
[2300]274    return (uint8_t const *)cv->chars;
[2056]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
[2821]281 *  caca_get_attr().
[2056]282 *
283 *  This function is only useful for display drivers such as the \e libcaca
284 *  library.
285 *
286 *  This function never fails.
287 *
[2821]288 *  \param cv A libcaca canvas.
[2056]289 *  \return The canvas attribute array.
290 */
[2821]291uint8_t const * caca_get_canvas_attrs(caca_canvas_t const *cv)
[2056]292{
[2300]293    return (uint8_t const *)cv->attrs;
[2056]294}
295
[2821]296/** \brief Uninitialise \e libcaca.
[524]297 *
[2821]298 *  Free all resources allocated by caca_create_canvas(). After
299 *  this function has been called, no other \e libcaca functions may be
300 *  used unless a new call to caca_create_canvas() is done.
[773]301 *
[874]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 *
[2821]305 *  \param cv A libcaca canvas.
[874]306 *  \return 0 in case of success, -1 if an error occurred.
[524]307 */
[2821]308int caca_free_canvas(caca_canvas_t *cv)
[524]309{
[2305]310    int f;
[842]311
[874]312    if(cv->refcount)
313    {
[1362]314        seterrno(EBUSY);
[874]315        return -1;
316    }
317
[842]318    for(f = 0; f < cv->framecount; f++)
319    {
[1338]320        free(cv->frames[f].chars);
321        free(cv->frames[f].attrs);
[1390]322        free(cv->frames[f].name);
[842]323    }
[524]324
[2821]325    caca_canvas_set_figfont(cv, NULL);
[2110]326
[1338]327    free(cv->frames);
[811]328    free(cv);
[874]329
330    return 0;
[609]331}
[560]332
[795]333/** \brief Generate a random integer within a range.
334 *
[1231]335 *  Generate a random integer within the given range.
336 *
[874]337 *  This function never fails.
338 *
[795]339 *  \param min The lower bound of the integer range.
340 *  \param max The upper bound of the integer range.
[815]341 *  \return A random integer comprised between \p min  and \p max - 1
342 *  (inclusive).
[795]343 */
[2821]344int caca_rand(int min, int max)
[795]345{
[1022]346    static int need_init = 1;
347
348    if(need_init)
349    {
350        srand(getpid() + time(NULL));
351        need_init = 0;
352    }
353
[815]354    return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
[795]355}
356
[2074]357
[524]358/*
359 * XXX: The following functions are local.
360 */
361
[2821]362int caca_resize(caca_canvas_t *cv, int width, int height)
[552]363{
[2305]364    int x, y, f, old_width, old_height, new_size, old_size;
[552]365
[811]366    old_width = cv->width;
367    old_height = cv->height;
[552]368    old_size = old_width * old_height;
369
[2821]370    _caca_save_frame_info(cv);
[1381]371
[811]372    cv->width = width;
373    cv->height = height;
[552]374    new_size = width * height;
375
376    /* Step 1: if new area is bigger, resize the memory area now. */
377    if(new_size > old_size)
378    {
[842]379        for(f = 0; f < cv->framecount; f++)
380        {
[1338]381            cv->frames[f].chars = realloc(cv->frames[f].chars,
382                                          new_size * sizeof(uint32_t));
383            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
384                                          new_size * sizeof(uint32_t));
385            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
[874]386            {
[1362]387                seterrno(ENOMEM);
[874]388                return -1;
389            }
[842]390        }
[552]391    }
392
393    /* Step 2: move line data if necessary. */
394    if(width == old_width)
395    {
396        /* Width did not change, which means we do not need to move data. */
397        ;
398    }
399    else if(width > old_width)
400    {
401        /* New width is bigger than old width, which means we need to
402         * copy lines starting from the bottom of the screen otherwise
403         * we will overwrite information. */
[842]404        for(f = 0; f < cv->framecount; f++)
[552]405        {
[1338]406            uint32_t *chars = cv->frames[f].chars;
407            uint32_t *attrs = cv->frames[f].attrs;
[842]408
409            for(y = height < old_height ? height : old_height; y--; )
[552]410            {
[1338]411                uint32_t attr = cv->frames[f].curattr;
[1088]412
[842]413                for(x = old_width; x--; )
414                {
415                    chars[y * width + x] = chars[y * old_width + x];
[1254]416                    attrs[y * width + x] = attrs[y * old_width + x];
[842]417                }
418
419                /* Zero the end of the line */
420                for(x = width - old_width; x--; )
[1088]421                {
[842]422                    chars[y * width + old_width + x] = (uint32_t)' ';
[1254]423                    attrs[y * width + old_width + x] = attr;
[1088]424                }
[552]425            }
426        }
427    }
428    else
429    {
430        /* New width is smaller. Copy as many lines as possible. Ignore
431         * the first line, it is already in place. */
[2305]432        int lines = height < old_height ? height : old_height;
[552]433
[842]434        for(f = 0; f < cv->framecount; f++)
[552]435        {
[1338]436            uint32_t *chars = cv->frames[f].chars;
437            uint32_t *attrs = cv->frames[f].attrs;
[842]438
439            for(y = 1; y < lines; y++)
[552]440            {
[842]441                for(x = 0; x < width; x++)
442                {
443                    chars[y * width + x] = chars[y * old_width + x];
[1254]444                    attrs[y * width + x] = attrs[y * old_width + x];
[842]445                }
[552]446            }
447        }
448    }
449
450    /* Step 3: fill the bottom of the new screen if necessary. */
451    if(height > old_height)
452    {
[842]453        for(f = 0; f < cv->framecount; f++)
454        {
[1338]455            uint32_t *chars = cv->frames[f].chars;
456            uint32_t *attrs = cv->frames[f].attrs;
457            uint32_t attr = cv->frames[f].curattr;
[842]458
459            /* Zero the bottom of the screen */
460            for(x = (height - old_height) * width; x--; )
[1088]461            {
[842]462                chars[old_height * width + x] = (uint32_t)' ';
[1254]463                attrs[old_height * width + x] = attr;
[1088]464            }
[842]465        }
[552]466    }
467
468    /* Step 4: if new area is smaller, resize memory area now. */
[1338]469    if(new_size < old_size)
[552]470    {
[842]471        for(f = 0; f < cv->framecount; f++)
472        {
[1338]473            cv->frames[f].chars = realloc(cv->frames[f].chars,
474                                          new_size * sizeof(uint32_t));
475            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
476                                          new_size * sizeof(uint32_t));
477            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
[874]478            {
[1362]479                seterrno(ENOMEM);
[874]480                return -1;
481            }
[842]482        }
[552]483    }
[842]484
[1338]485    /* Set new size */
486    for(f = 0; f < cv->framecount; f++)
487    {
[1813]488        if(cv->frames[f].x > (int)width)
489            cv->frames[f].x = width;
490        if(cv->frames[f].y > (int)height)
491            cv->frames[f].y = height;
492
[1338]493        cv->frames[f].width = width;
494        cv->frames[f].height = height;
495    }
[874]496
[1338]497    /* Reset the current frame shortcuts */
[2821]498    _caca_load_frame_info(cv);
[1338]499
[874]500    return 0;
[552]501}
502
[2826]503/*
504 * XXX: The following functions are aliases.
505 */
506
507cucul_canvas_t * cucul_create_canvas(int, int) CACA_ALIAS(caca_create_canvas);
508int cucul_manage_canvas(cucul_canvas_t *, int (*)(void *), void *)
509    CACA_ALIAS(caca_manage_canvas);
510int cucul_unmanage_canvas(cucul_canvas_t *, int (*)(void *), void *)
511    CACA_ALIAS(caca_unmanage_canvas);
512int cucul_set_canvas_size(cucul_canvas_t *, int, int)
513    CACA_ALIAS(caca_set_canvas_size);
514int cucul_get_canvas_width(cucul_canvas_t const *)
515    CACA_ALIAS(caca_get_canvas_width);
516int cucul_get_canvas_height(cucul_canvas_t const *)
517    CACA_ALIAS(caca_get_canvas_height);
518uint8_t const * cucul_get_canvas_chars(cucul_canvas_t const *)
519    CACA_ALIAS(caca_get_canvas_chars);
520uint8_t const * cucul_get_canvas_attrs(cucul_canvas_t const *)
521    CACA_ALIAS(caca_get_canvas_attrs);
522int cucul_free_canvas(cucul_canvas_t *) CACA_ALIAS(caca_free_canvas);
523int cucul_rand(int, int) CACA_ALIAS(caca_rand);
524
Note: See TracBrowser for help on using the repository browser.