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

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

=?utf-8?q?=20=20*=20Implement=20cucul=5Fput=5Ffigchar()=20to=20paste=20FIGlet/TOIlet=20characters=20in

=20=20=20=20=20=20=20=20=20=20=E2=96=8C=20=E2=96=8C=20=20=20=E2=96=9C=E2=96=9C=20=20=20=20=C2=A0=E2=96=8C=20=E2=96=8C=20=20=20=20=20=20=E2=96=9C=20=20=20=E2=96=8C=E2=96=90
=20=20=20=20=20=20=20=20=20=20=E2=96=99=E2=96=84=E2=96=8C=E2=96=9E=E2=96=80=E2=96=96=E2=96=90=E2=96=90=20=E2=96=9E=E2=96=80=E2=96=96=C2=A0=E2=96=8C=E2=96=96=E2=96=8C=E2=96=9E=E2=96=80=E2=96=96=E2=96=99=E2=96=80=E2=96=96=E2=96=90=20=E2=96=9E=E2=96=80=E2=96=8C=E2=96=90
=20=20=20=20=20=20=20=20=20=20=E2=96=8C=20=E2=96=8C=E2=96=9B=E2=96=80=20=E2=96=90=E2=96=90=20=E2=96=8C=20=E2=96=8C=C2=A0=E2=96=99=E2=96=9A=E2=96=8C=E2=96=8C=20=E2=96=8C=E2=96=8C=20=C2=A0=E2=96=90=20=E2=96=8C=20=E2=96=8C=E2=96=9D
=20=20=20=20=20=20=20=20=20=20=E2=96=98=20=E2=96=98=E2=96=9D=E2=96=80=E2=96=98=20=E2=96=98=E2=96=98=E2=96=9D=E2=96=80=20=C2=A0=E2=96=98=20=E2=96=98=E2=96=9D=E2=96=80=20=E2=96=98=20=20=20=E2=96=98=E2=96=9D=E2=96=80=E2=96=98=E2=96=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

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