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

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