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

Last change on this file since 2040 was 2040, checked in by Sam Hocevar, 13 years ago
  • Rename _cucul_set_canvas_size into cucul_set_canvas_size to prevent collisions due to symbol mangling.
  • Property svn:keywords set to Id
File size: 10.7 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 2040 2007-11-23 22:34:35Z 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
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 *  If as a result of the resize the cursor coordinates fall outside the
110 *  new canvas boundaries, they are readjusted. For instance, if the
111 *  current X cursor coordinate is 11 and the requested width is 10, the
112 *  new X cursor coordinate will be 10.
113 *
114 *  It is an error to try to resize the canvas if an output driver has
115 *  been attached to the canvas using caca_create_display(). You need to
116 *  remove the output driver using caca_free_display() before you can change
117 *  the canvas size again. However, the caca output driver can cause a
118 *  canvas resize through user interaction. See the caca_event() documentation
119 *  for more about this.
120 *
121 *  If an error occurs, -1 is returned and \b errno is set accordingly:
122 *  - \c EBUSY The canvas is in use by a display driver and cannot be resized.
123 *  - \c ENOMEM Not enough memory for the requested canvas size. If this
124 *    happens, the canvas handle becomes invalid and should not be used.
125 *
126 *  \param cv A libcucul canvas
127 *  \param width The desired canvas width
128 *  \param height The desired canvas height
129 *  \return 0 in case of success, -1 if an error occurred.
130 */
131int cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
132                                              unsigned int height)
133{
134    if(cv->refcount)
135    {
136        seterrno(EBUSY);
137        return -1;
138    }
139
140    return __cucul_set_canvas_size(cv, width, height);
141}
142
143/** \brief Get the canvas width.
144 *
145 *  Return the current canvas' width, in character cells.
146 *
147 *  This function never fails.
148 *
149 *  \param cv A libcucul canvas
150 *  \return The canvas width.
151 */
152unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
153{
154    return cv->width;
155}
156
157/** \brief Get the canvas height.
158 *
159 *  Returns the current canvas' height, in character cells.
160 *
161 *  This function never fails.
162 *
163 *  \param cv A libcucul canvas
164 *  \return The canvas height.
165 */
166unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
167{
168    return cv->height;
169}
170
171/** \brief Uninitialise \e libcucul.
172 *
173 *  Free all resources allocated by cucul_create_canvas(). After
174 *  this function has been called, no other \e libcucul functions may be
175 *  used unless a new call to cucul_create_canvas() is done.
176 *
177 *  If an error occurs, -1 is returned and \b errno is set accordingly:
178 *  - \c EBUSY The canvas is in use by a display driver and cannot be freed.
179 *
180 *  \param cv A libcucul canvas
181 *  \return 0 in case of success, -1 if an error occurred.
182 */
183int cucul_free_canvas(cucul_canvas_t *cv)
184{
185    unsigned int f;
186
187    if(cv->refcount)
188    {
189        seterrno(EBUSY);
190        return -1;
191    }
192
193    for(f = 0; f < cv->framecount; f++)
194    {
195        free(cv->frames[f].chars);
196        free(cv->frames[f].attrs);
197        free(cv->frames[f].name);
198    }
199
200    free(cv->frames);
201    free(cv);
202
203    return 0;
204}
205
206/** \brief Generate a random integer within a range.
207 *
208 *  Generate a random integer within the given range.
209 *
210 *  This function never fails.
211 *
212 *  \param min The lower bound of the integer range.
213 *  \param max The upper bound of the integer range.
214 *  \return A random integer comprised between \p min  and \p max - 1
215 *  (inclusive).
216 */
217int cucul_rand(int min, int max)
218{
219    static int need_init = 1;
220
221    if(need_init)
222    {
223        srand(getpid() + time(NULL));
224        need_init = 0;
225    }
226
227    return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
228}
229
230/*
231 * XXX: The following functions are local.
232 */
233
234int __cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
235                                                unsigned int height)
236{
237    unsigned int x, y, f, old_width, old_height, new_size, old_size;
238
239    old_width = cv->width;
240    old_height = cv->height;
241    old_size = old_width * old_height;
242
243    _cucul_save_frame_info(cv);
244
245    cv->width = width;
246    cv->height = height;
247    new_size = width * height;
248
249    /* Step 1: if new area is bigger, resize the memory area now. */
250    if(new_size > old_size)
251    {
252        for(f = 0; f < cv->framecount; f++)
253        {
254            cv->frames[f].chars = realloc(cv->frames[f].chars,
255                                          new_size * sizeof(uint32_t));
256            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
257                                          new_size * sizeof(uint32_t));
258            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
259            {
260                seterrno(ENOMEM);
261                return -1;
262            }
263        }
264    }
265
266    /* Step 2: move line data if necessary. */
267    if(width == old_width)
268    {
269        /* Width did not change, which means we do not need to move data. */
270        ;
271    }
272    else if(width > old_width)
273    {
274        /* New width is bigger than old width, which means we need to
275         * copy lines starting from the bottom of the screen otherwise
276         * we will overwrite information. */
277        for(f = 0; f < cv->framecount; f++)
278        {
279            uint32_t *chars = cv->frames[f].chars;
280            uint32_t *attrs = cv->frames[f].attrs;
281
282            for(y = height < old_height ? height : old_height; y--; )
283            {
284                uint32_t attr = cv->frames[f].curattr;
285
286                for(x = old_width; x--; )
287                {
288                    chars[y * width + x] = chars[y * old_width + x];
289                    attrs[y * width + x] = attrs[y * old_width + x];
290                }
291
292                /* Zero the end of the line */
293                for(x = width - old_width; x--; )
294                {
295                    chars[y * width + old_width + x] = (uint32_t)' ';
296                    attrs[y * width + old_width + x] = attr;
297                }
298            }
299        }
300    }
301    else
302    {
303        /* New width is smaller. Copy as many lines as possible. Ignore
304         * the first line, it is already in place. */
305        unsigned int lines = height < old_height ? height : old_height;
306
307        for(f = 0; f < cv->framecount; f++)
308        {
309            uint32_t *chars = cv->frames[f].chars;
310            uint32_t *attrs = cv->frames[f].attrs;
311
312            for(y = 1; y < lines; y++)
313            {
314                for(x = 0; x < width; x++)
315                {
316                    chars[y * width + x] = chars[y * old_width + x];
317                    attrs[y * width + x] = attrs[y * old_width + x];
318                }
319            }
320        }
321    }
322
323    /* Step 3: fill the bottom of the new screen if necessary. */
324    if(height > old_height)
325    {
326        for(f = 0; f < cv->framecount; f++)
327        {
328            uint32_t *chars = cv->frames[f].chars;
329            uint32_t *attrs = cv->frames[f].attrs;
330            uint32_t attr = cv->frames[f].curattr;
331
332            /* Zero the bottom of the screen */
333            for(x = (height - old_height) * width; x--; )
334            {
335                chars[old_height * width + x] = (uint32_t)' ';
336                attrs[old_height * width + x] = attr;
337            }
338        }
339    }
340
341    /* Step 4: if new area is smaller, resize memory area now. */
342    if(new_size < old_size)
343    {
344        for(f = 0; f < cv->framecount; f++)
345        {
346            cv->frames[f].chars = realloc(cv->frames[f].chars,
347                                          new_size * sizeof(uint32_t));
348            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
349                                          new_size * sizeof(uint32_t));
350            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
351            {
352                seterrno(ENOMEM);
353                return -1;
354            }
355        }
356    }
357
358    /* Set new size */
359    for(f = 0; f < cv->framecount; f++)
360    {
361        if(cv->frames[f].x > (int)width)
362            cv->frames[f].x = width;
363        if(cv->frames[f].y > (int)height)
364            cv->frames[f].y = height;
365
366        cv->frames[f].width = width;
367        cv->frames[f].height = height;
368    }
369
370    /* Reset the current frame shortcuts */
371    _cucul_load_frame_info(cv);
372
373    return 0;
374}
375
Note: See TracBrowser for help on using the repository browser.