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

Last change on this file since 1452 was 1452, checked in by Sam Hocevar, 13 years ago
  • Add a no warranty clause to the code.
  • Property svn:keywords set to Id
File size: 10.3 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 1452 2006-12-11 15:48:46Z sam $
7 *
8 *  This library is free software. It commes 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
37/** \brief Initialise a \e libcucul canvas.
38 *
39 *  Initialise internal \e libcucul structures and the backend that will
40 *  be used for subsequent graphical operations. It must be the first
41 *  \e libcucul function to be called in a function. cucul_free_canvas()
42 *  should be called at the end of the program to free all allocated resources.
43 *
44 *  Both the cursor and the canvas' handle are initialised at the top-left
45 *  corner.
46 *
47 *  If an error occurs, NULL is returned and \b errno is set accordingly:
48 *  - \c ENOMEM Not enough memory for the requested canvas size.
49 *
50 *  \param width The desired canvas width
51 *  \param height The desired canvas height
52 *  \return A libcucul canvas handle upon success, NULL if an error occurred.
53 */
54cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height)
55{
56    cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
57
58    if(!cv)
59        goto nomem;
60
61    cv->refcount = 0;
62    cv->autoinc = 0;
63
64    cv->frame = 0;
65    cv->framecount = 1;
66    cv->frames = malloc(sizeof(struct cucul_frame));
67    if(!cv->frames)
68    {
69        free(cv);
70        goto nomem;
71    }
72
73    cv->frames[0].width = cv->frames[0].height = 0;
74    cv->frames[0].chars = NULL;
75    cv->frames[0].attrs = NULL;
76    cv->frames[0].x = cv->frames[0].y = 0;
77    cv->frames[0].handlex = cv->frames[0].handley = 0;
78    cv->frames[0].curattr = 0;
79    cv->frames[0].name = strdup("frame#00000000");
80
81    _cucul_load_frame_info(cv);
82    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
83
84    if(_cucul_set_canvas_size(cv, width, height) < 0)
85    {
86        int saved_errno = geterrno();
87        free(cv->frames[0].name);
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        free(cv->frames[f].name);
193    }
194
195    free(cv->frames);
196    free(cv);
197
198    return 0;
199}
200
201/** \brief Generate a random integer within a range.
202 *
203 *  Generate a random integer within the given range.
204 *
205 *  This function never fails.
206 *
207 *  \param min The lower bound of the integer range.
208 *  \param max The upper bound of the integer range.
209 *  \return A random integer comprised between \p min  and \p max - 1
210 *  (inclusive).
211 */
212int cucul_rand(int min, int max)
213{
214    static int need_init = 1;
215
216    if(need_init)
217    {
218        srand(getpid() + time(NULL));
219        need_init = 0;
220    }
221
222    return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
223}
224
225/*
226 * XXX: The following functions are local.
227 */
228
229int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
230                                               unsigned int height)
231{
232    unsigned int x, y, f, old_width, old_height, new_size, old_size;
233
234    old_width = cv->width;
235    old_height = cv->height;
236    old_size = old_width * old_height;
237
238    _cucul_save_frame_info(cv);
239
240    cv->width = width;
241    cv->height = height;
242    new_size = width * height;
243
244    /* Step 1: if new area is bigger, resize the memory area now. */
245    if(new_size > old_size)
246    {
247        for(f = 0; f < cv->framecount; f++)
248        {
249            cv->frames[f].chars = realloc(cv->frames[f].chars,
250                                          new_size * sizeof(uint32_t));
251            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
252                                          new_size * sizeof(uint32_t));
253            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
254            {
255                seterrno(ENOMEM);
256                return -1;
257            }
258        }
259    }
260
261    /* Step 2: move line data if necessary. */
262    if(width == old_width)
263    {
264        /* Width did not change, which means we do not need to move data. */
265        ;
266    }
267    else if(width > old_width)
268    {
269        /* New width is bigger than old width, which means we need to
270         * copy lines starting from the bottom of the screen otherwise
271         * we will overwrite information. */
272        for(f = 0; f < cv->framecount; f++)
273        {
274            uint32_t *chars = cv->frames[f].chars;
275            uint32_t *attrs = cv->frames[f].attrs;
276
277            for(y = height < old_height ? height : old_height; y--; )
278            {
279                uint32_t attr = cv->frames[f].curattr;
280
281                for(x = old_width; x--; )
282                {
283                    chars[y * width + x] = chars[y * old_width + x];
284                    attrs[y * width + x] = attrs[y * old_width + x];
285                }
286
287                /* Zero the end of the line */
288                for(x = width - old_width; x--; )
289                {
290                    chars[y * width + old_width + x] = (uint32_t)' ';
291                    attrs[y * width + old_width + x] = attr;
292                }
293            }
294        }
295    }
296    else
297    {
298        /* New width is smaller. Copy as many lines as possible. Ignore
299         * the first line, it is already in place. */
300        unsigned int lines = height < old_height ? height : old_height;
301
302        for(f = 0; f < cv->framecount; f++)
303        {
304            uint32_t *chars = cv->frames[f].chars;
305            uint32_t *attrs = cv->frames[f].attrs;
306
307            for(y = 1; y < lines; y++)
308            {
309                for(x = 0; x < width; x++)
310                {
311                    chars[y * width + x] = chars[y * old_width + x];
312                    attrs[y * width + x] = attrs[y * old_width + x];
313                }
314            }
315        }
316    }
317
318    /* Step 3: fill the bottom of the new screen if necessary. */
319    if(height > old_height)
320    {
321        for(f = 0; f < cv->framecount; f++)
322        {
323            uint32_t *chars = cv->frames[f].chars;
324            uint32_t *attrs = cv->frames[f].attrs;
325            uint32_t attr = cv->frames[f].curattr;
326
327            /* Zero the bottom of the screen */
328            for(x = (height - old_height) * width; x--; )
329            {
330                chars[old_height * width + x] = (uint32_t)' ';
331                attrs[old_height * width + x] = attr;
332            }
333        }
334    }
335
336    /* Step 4: if new area is smaller, resize memory area now. */
337    if(new_size < old_size)
338    {
339        for(f = 0; f < cv->framecount; f++)
340        {
341            cv->frames[f].chars = realloc(cv->frames[f].chars,
342                                          new_size * sizeof(uint32_t));
343            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
344                                          new_size * sizeof(uint32_t));
345            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
346            {
347                seterrno(ENOMEM);
348                return -1;
349            }
350        }
351    }
352
353    /* Set new size */
354    for(f = 0; f < cv->framecount; f++)
355    {
356        cv->frames[f].width = width;
357        cv->frames[f].height = height;
358    }
359
360    /* Reset the current frame shortcuts */
361    _cucul_load_frame_info(cv);
362
363    return 0;
364}
365
Note: See TracBrowser for help on using the repository browser.