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

Last change on this file since 1342 was 1342, checked in by Sam Hocevar, 16 years ago
  • Add cursor and handle support to canvases. Unused yet.
  • Property svn:keywords set to Id
File size: 10.5 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 1342 2006-11-11 14:24:35Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains the main functions used by \e libcucul applications
16 *  to initialise a drawing context.
17 */
18
19#include "config.h"
20#include "common.h"
21
22#if !defined(__KERNEL__)
23#   include <stdio.h>
24#   include <stdlib.h>
25#   include <string.h>
26#   include <time.h>
27#   if defined(HAVE_ERRNO_H)
28#       include <errno.h>
29#   endif
30#   include <sys/types.h>
31#   if defined(HAVE_UNISTD_H)
32#       include <unistd.h>
33#   endif
34#endif
35
36#include "cucul.h"
37#include "cucul_internals.h"
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
65    cv->curattr = 0x00000000;
66    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
67
68    cv->width = cv->height = 0;
69    cv->chars = NULL;
70    cv->attrs = NULL;
71
72    cv->frame = 0;
73    cv->framecount = 1;
74    cv->frames = malloc(sizeof(struct cucul_frame));
75    if(!cv->frames)
76    {
77        free(cv);
78        goto nomem;
79    }
80
81    cv->frames[0].width = cv->frames[0].height = 0;
82    cv->frames[0].chars = NULL;
83    cv->frames[0].attrs = NULL;
84    cv->frames[0].x = cv->frames[0].y = 0;
85    cv->frames[0].handlex = cv->frames[0].handley = 0;
86    cv->frames[0].curattr = cv->curattr;
87
88    if(_cucul_set_canvas_size(cv, width, height) < 0)
89    {
90#if defined(HAVE_ERRNO_H)
91        int saved_errno = errno;
92#endif
93        free(cv->frames);
94        free(cv);
95#if defined(HAVE_ERRNO_H)
96        errno = saved_errno;
97#endif
98        return NULL;
99    }
100
101    return cv;
102
103nomem:
104#if defined(HAVE_ERRNO_H)
105    errno = ENOMEM;
106#endif
107    return NULL;
108}
109
110/** \brief Resize a canvas.
111 *
112 *  Set the canvas' width and height, in character cells.
113 *
114 *  The contents of the canvas are preserved to the extent of the new
115 *  canvas size. Newly allocated character cells at the right and/or at
116 *  the bottom of the canvas are filled with spaces.
117 *
118 *  It is an error to try to resize the canvas if an output driver has
119 *  been attached to the canvas using caca_create_display(). You need to
120 *  remove the output driver using caca_free_display() before you can change
121 *  the  canvas size again. However, the caca output driver can cause a
122 *  canvas resize through user interaction. See the caca_event() documentation
123 *  for more about this.
124 *
125 *  If an error occurs, -1 is returned and \b errno is set accordingly:
126 *  - \c EBUSY The canvas is in use by a display driver and cannot be resized.
127 *  - \c ENOMEM Not enough memory for the requested canvas size. If this
128 *    happens, the canvas handle becomes invalid and should not be used.
129 *
130 *  \param cv A libcucul canvas
131 *  \param width The desired canvas width
132 *  \param height The desired canvas height
133 *  \return 0 in case of success, -1 if an error occurred.
134 */
135int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
136                                              unsigned int height)
137{
138    if(cv->refcount)
139    {
140#if defined(HAVE_ERRNO_H)
141        errno = EBUSY;
142#endif
143        return -1;
144    }
145
146    return _cucul_set_canvas_size(cv, width, height);
147}
148
149/** \brief Get the canvas width.
150 *
151 *  Return the current canvas' width, in character cells.
152 *
153 *  This function never fails.
154 *
155 *  \param cv A libcucul canvas
156 *  \return The canvas width.
157 */
158unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
159{
160    return cv->width;
161}
162
163/** \brief Get the canvas height.
164 *
165 *  Returns the current canvas' height, in character cells.
166 *
167 *  This function never fails.
168 *
169 *  \param cv A libcucul canvas
170 *  \return The canvas height.
171 */
172unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
173{
174    return cv->height;
175}
176
177/** \brief Uninitialise \e libcucul.
178 *
179 *  Free all resources allocated by cucul_create_canvas(). After
180 *  this function has been called, no other \e libcucul functions may be
181 *  used unless a new call to cucul_create_canvas() is done.
182 *
183 *  If an error occurs, -1 is returned and \b errno is set accordingly:
184 *  - \c EBUSY The canvas is in use by a display driver and cannot be freed.
185 *
186 *  \param cv A libcucul canvas
187 *  \return 0 in case of success, -1 if an error occurred.
188 */
189int cucul_free_canvas(cucul_canvas_t *cv)
190{
191    unsigned int f;
192
193    if(cv->refcount)
194    {
195#if defined(HAVE_ERRNO_H)
196        errno = EBUSY;
197#endif
198        return -1;
199    }
200
201    for(f = 0; f < cv->framecount; f++)
202    {
203        free(cv->frames[f].chars);
204        free(cv->frames[f].attrs);
205    }
206
207    free(cv->frames);
208    free(cv);
209
210    return 0;
211}
212
213/** \brief Generate a random integer within a range.
214 *
215 *  Generate a random integer within the given range.
216 *
217 *  This function never fails.
218 *
219 *  \param min The lower bound of the integer range.
220 *  \param max The upper bound of the integer range.
221 *  \return A random integer comprised between \p min  and \p max - 1
222 *  (inclusive).
223 */
224int cucul_rand(int min, int max)
225{
226    static int need_init = 1;
227
228    if(need_init)
229    {
230        srand(getpid() + time(NULL));
231        need_init = 0;
232    }
233
234    return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
235}
236
237/*
238 * XXX: The following functions are local.
239 */
240
241int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
242                                               unsigned int height)
243{
244    unsigned int x, y, f, old_width, old_height, new_size, old_size;
245
246    old_width = cv->width;
247    old_height = cv->height;
248    old_size = old_width * old_height;
249
250    cv->width = width;
251    cv->height = height;
252    new_size = width * height;
253
254    /* Step 1: if new area is bigger, resize the memory area now. */
255    if(new_size > old_size)
256    {
257        for(f = 0; f < cv->framecount; f++)
258        {
259            cv->frames[f].chars = realloc(cv->frames[f].chars,
260                                          new_size * sizeof(uint32_t));
261            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
262                                          new_size * sizeof(uint32_t));
263            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
264            {
265#if defined(HAVE_ERRNO_H)
266                errno = ENOMEM;
267#endif
268                return -1;
269            }
270        }
271    }
272
273    /* Step 2: move line data if necessary. */
274    if(width == old_width)
275    {
276        /* Width did not change, which means we do not need to move data. */
277        ;
278    }
279    else if(width > old_width)
280    {
281        /* New width is bigger than old width, which means we need to
282         * copy lines starting from the bottom of the screen otherwise
283         * we will overwrite information. */
284        for(f = 0; f < cv->framecount; f++)
285        {
286            uint32_t *chars = cv->frames[f].chars;
287            uint32_t *attrs = cv->frames[f].attrs;
288
289            for(y = height < old_height ? height : old_height; y--; )
290            {
291                uint32_t attr = cv->frames[f].curattr;
292
293                for(x = old_width; x--; )
294                {
295                    chars[y * width + x] = chars[y * old_width + x];
296                    attrs[y * width + x] = attrs[y * old_width + x];
297                }
298
299                /* Zero the end of the line */
300                for(x = width - old_width; x--; )
301                {
302                    chars[y * width + old_width + x] = (uint32_t)' ';
303                    attrs[y * width + old_width + x] = attr;
304                }
305            }
306        }
307    }
308    else
309    {
310        /* New width is smaller. Copy as many lines as possible. Ignore
311         * the first line, it is already in place. */
312        unsigned int lines = height < old_height ? height : old_height;
313
314        for(f = 0; f < cv->framecount; f++)
315        {
316            uint32_t *chars = cv->frames[f].chars;
317            uint32_t *attrs = cv->frames[f].attrs;
318
319            for(y = 1; y < lines; y++)
320            {
321                for(x = 0; x < width; x++)
322                {
323                    chars[y * width + x] = chars[y * old_width + x];
324                    attrs[y * width + x] = attrs[y * old_width + x];
325                }
326            }
327        }
328    }
329
330    /* Step 3: fill the bottom of the new screen if necessary. */
331    if(height > old_height)
332    {
333        for(f = 0; f < cv->framecount; f++)
334        {
335            uint32_t *chars = cv->frames[f].chars;
336            uint32_t *attrs = cv->frames[f].attrs;
337            uint32_t attr = cv->frames[f].curattr;
338
339            /* Zero the bottom of the screen */
340            for(x = (height - old_height) * width; x--; )
341            {
342                chars[old_height * width + x] = (uint32_t)' ';
343                attrs[old_height * width + x] = attr;
344            }
345        }
346    }
347
348    /* Step 4: if new area is smaller, resize memory area now. */
349    if(new_size < old_size)
350    {
351        for(f = 0; f < cv->framecount; f++)
352        {
353            cv->frames[f].chars = realloc(cv->frames[f].chars,
354                                          new_size * sizeof(uint32_t));
355            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
356                                          new_size * sizeof(uint32_t));
357            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
358            {
359#if defined(HAVE_ERRNO_H)
360                errno = ENOMEM;
361#endif
362                return -1;
363            }
364        }
365    }
366
367    /* Set new size */
368    for(f = 0; f < cv->framecount; f++)
369    {
370        cv->frames[f].width = width;
371        cv->frames[f].height = height;
372    }
373
374    /* Reset the current frame shortcuts */
375    cv->chars = cv->frames[cv->frame].chars;
376    cv->attrs = cv->frames[cv->frame].attrs;
377
378    return 0;
379}
380
Note: See TracBrowser for help on using the repository browser.