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

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