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

Last change on this file since 1431 was 1431, checked in by Sam Hocevar, 16 years ago
  • Improved vt220 import by adding a persistent context to canvases.
  • 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 1431 2006-11-27 01:56:37Z 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    cv->autoinc = 0;
62
63    cv->frame = 0;
64    cv->framecount = 1;
65    cv->frames = malloc(sizeof(struct cucul_frame));
66    if(!cv->frames)
67    {
68        free(cv);
69        goto nomem;
70    }
71
72    cv->frames[0].width = cv->frames[0].height = 0;
73    cv->frames[0].chars = NULL;
74    cv->frames[0].attrs = NULL;
75    cv->frames[0].x = cv->frames[0].y = 0;
76    cv->frames[0].handlex = cv->frames[0].handley = 0;
77    cv->frames[0].curattr = 0;
78    cv->frames[0].name = strdup("frame#00000000");
79    cv->frames[0].import = NULL;
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        if(cv->frames[f].import)
194            free(cv->frames[f].import);
195    }
196
197    free(cv->frames);
198    free(cv);
199
200    return 0;
201}
202
203/** \brief Generate a random integer within a range.
204 *
205 *  Generate a random integer within the given range.
206 *
207 *  This function never fails.
208 *
209 *  \param min The lower bound of the integer range.
210 *  \param max The upper bound of the integer range.
211 *  \return A random integer comprised between \p min  and \p max - 1
212 *  (inclusive).
213 */
214int cucul_rand(int min, int max)
215{
216    static int need_init = 1;
217
218    if(need_init)
219    {
220        srand(getpid() + time(NULL));
221        need_init = 0;
222    }
223
224    return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
225}
226
227/*
228 * XXX: The following functions are local.
229 */
230
231int _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
232                                               unsigned int height)
233{
234    unsigned int x, y, f, old_width, old_height, new_size, old_size;
235
236    old_width = cv->width;
237    old_height = cv->height;
238    old_size = old_width * old_height;
239
240    _cucul_save_frame_info(cv);
241
242    cv->width = width;
243    cv->height = height;
244    new_size = width * height;
245
246    /* Step 1: if new area is bigger, resize the memory area now. */
247    if(new_size > old_size)
248    {
249        for(f = 0; f < cv->framecount; f++)
250        {
251            cv->frames[f].chars = realloc(cv->frames[f].chars,
252                                          new_size * sizeof(uint32_t));
253            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
254                                          new_size * sizeof(uint32_t));
255            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
256            {
257                seterrno(ENOMEM);
258                return -1;
259            }
260        }
261    }
262
263    /* Step 2: move line data if necessary. */
264    if(width == old_width)
265    {
266        /* Width did not change, which means we do not need to move data. */
267        ;
268    }
269    else if(width > old_width)
270    {
271        /* New width is bigger than old width, which means we need to
272         * copy lines starting from the bottom of the screen otherwise
273         * we will overwrite information. */
274        for(f = 0; f < cv->framecount; f++)
275        {
276            uint32_t *chars = cv->frames[f].chars;
277            uint32_t *attrs = cv->frames[f].attrs;
278
279            for(y = height < old_height ? height : old_height; y--; )
280            {
281                uint32_t attr = cv->frames[f].curattr;
282
283                for(x = old_width; x--; )
284                {
285                    chars[y * width + x] = chars[y * old_width + x];
286                    attrs[y * width + x] = attrs[y * old_width + x];
287                }
288
289                /* Zero the end of the line */
290                for(x = width - old_width; x--; )
291                {
292                    chars[y * width + old_width + x] = (uint32_t)' ';
293                    attrs[y * width + old_width + x] = attr;
294                }
295            }
296        }
297    }
298    else
299    {
300        /* New width is smaller. Copy as many lines as possible. Ignore
301         * the first line, it is already in place. */
302        unsigned int lines = height < old_height ? height : old_height;
303
304        for(f = 0; f < cv->framecount; f++)
305        {
306            uint32_t *chars = cv->frames[f].chars;
307            uint32_t *attrs = cv->frames[f].attrs;
308
309            for(y = 1; y < lines; y++)
310            {
311                for(x = 0; x < width; x++)
312                {
313                    chars[y * width + x] = chars[y * old_width + x];
314                    attrs[y * width + x] = attrs[y * old_width + x];
315                }
316            }
317        }
318    }
319
320    /* Step 3: fill the bottom of the new screen if necessary. */
321    if(height > old_height)
322    {
323        for(f = 0; f < cv->framecount; f++)
324        {
325            uint32_t *chars = cv->frames[f].chars;
326            uint32_t *attrs = cv->frames[f].attrs;
327            uint32_t attr = cv->frames[f].curattr;
328
329            /* Zero the bottom of the screen */
330            for(x = (height - old_height) * width; x--; )
331            {
332                chars[old_height * width + x] = (uint32_t)' ';
333                attrs[old_height * width + x] = attr;
334            }
335        }
336    }
337
338    /* Step 4: if new area is smaller, resize memory area now. */
339    if(new_size < old_size)
340    {
341        for(f = 0; f < cv->framecount; f++)
342        {
343            cv->frames[f].chars = realloc(cv->frames[f].chars,
344                                          new_size * sizeof(uint32_t));
345            cv->frames[f].attrs = realloc(cv->frames[f].attrs,
346                                          new_size * sizeof(uint32_t));
347            if(new_size && (!cv->frames[f].chars || !cv->frames[f].attrs))
348            {
349                seterrno(ENOMEM);
350                return -1;
351            }
352        }
353    }
354
355    /* Set new size */
356    for(f = 0; f < cv->framecount; f++)
357    {
358        cv->frames[f].width = width;
359        cv->frames[f].height = height;
360    }
361
362    /* Reset the current frame shortcuts */
363    _cucul_load_frame_info(cv);
364
365    return 0;
366}
367
Note: See TracBrowser for help on using the repository browser.