source: libcaca/trunk/caca/frame.c @ 4333

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 10 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 *  This file contains a small framework for canvas frame management.
15 */
16
17#include "config.h"
18
19#if !defined(__KERNEL__)
20#   include <stdio.h>
21#   include <stdlib.h>
22#   include <string.h>
23#endif
24
25#include "caca.h"
26#include "caca_internals.h"
27
28/** \brief Get the number of frames in a canvas.
29 *
30 *  Return the current canvas' frame count.
31 *
32 *  This function never fails.
33 *
34 *  \param cv A libcaca canvas
35 *  \return The frame count
36 */
37int caca_get_frame_count(caca_canvas_t const *cv)
38{
39    return cv->framecount;
40}
41
42/** \brief Activate a given canvas frame.
43 *
44 *  Set the active canvas frame. All subsequent drawing operations will
45 *  be performed on that frame. The current painting context set by
46 *  caca_set_attr() is inherited.
47 *
48 *  If the frame index is outside the canvas' frame range, nothing happens.
49 *
50 *  If an error occurs, -1 is returned and \b errno is set accordingly:
51 *  - \c EINVAL Requested frame is out of range.
52 *
53 *  \param cv A libcaca canvas
54 *  \param id The canvas frame to activate
55 *  \return 0 in case of success, -1 if an error occurred.
56 */
57int caca_set_frame(caca_canvas_t *cv, int id)
58{
59    if(id < 0 || id >= cv->framecount)
60    {
61        seterrno(EINVAL);
62        return -1;
63    }
64
65    /* Bail out if no operation is required */
66    if(id == cv->frame)
67        return 0;
68
69    _caca_save_frame_info(cv);
70    cv->frame = id;
71    _caca_load_frame_info(cv);
72
73    if(!cv->dirty_disabled)
74        caca_add_dirty_rect(cv, 0, 0, cv->width, cv->height);
75
76    return 0;
77}
78
79/** \brief Get the current frame's name.
80 *
81 *  Return the current frame's name. The returned string is valid until
82 *  the frame is deleted or caca_set_frame_name() is called to change
83 *  the frame name again.
84 *
85 *  This function never fails.
86 *
87 *  \param cv A libcaca canvas.
88 *  \return The current frame's name.
89 */
90char const *caca_get_frame_name(caca_canvas_t const *cv)
91{
92    return cv->frames[cv->frame].name;
93}
94
95/** \brief Set the current frame's name.
96 *
97 *  Set the current frame's name. Upon creation, a frame has a default name
98 *  of \c "frame#xxxxxxxx" where \c xxxxxxxx is a self-incrementing
99 *  hexadecimal number.
100 *
101 *  If an error occurs, -1 is returned and \b errno is set accordingly:
102 *  - \c ENOMEM Not enough memory to allocate new frame.
103 *
104 *  \param cv A libcaca canvas.
105 *  \param name The name to give to the current frame.
106 *  \return 0 in case of success, -1 if an error occurred.
107 */
108int caca_set_frame_name(caca_canvas_t *cv, char const *name)
109{
110    char *newname = strdup(name);
111
112    if(!newname)
113    {
114        seterrno(ENOMEM);
115        return -1;
116    }
117
118    free(cv->frames[cv->frame].name);
119    cv->frames[cv->frame].name = newname;
120
121    return 0;
122}
123
124/** \brief Add a frame to a canvas.
125 *
126 *  Create a new frame within the given canvas. Its contents and attributes
127 *  are copied from the currently active frame.
128 *
129 *  The frame index indicates where the frame should be inserted. Valid
130 *  values range from 0 to the current canvas frame count. If the frame
131 *  index is greater than or equals the current canvas frame count, the new
132 *  frame is appended at the end of the canvas. If the frame index is less
133 *  than zero, the new frame is inserted at index 0.
134 *
135 *  The active frame does not change, but its index may be renumbered due
136 *  to the insertion.
137 *
138 *  If an error occurs, -1 is returned and \b errno is set accordingly:
139 *  - \c ENOMEM Not enough memory to allocate new frame.
140 *
141 *  \param cv A libcaca canvas
142 *  \param id The index where to insert the new frame
143 *  \return 0 in case of success, -1 if an error occurred.
144 */
145int caca_create_frame(caca_canvas_t *cv, int id)
146{
147    int size = cv->width * cv->height;
148    int f;
149
150    if(id < 0)
151        id = 0;
152    else if(id > cv->framecount)
153        id = cv->framecount;
154
155    cv->framecount++;
156    cv->frames = realloc(cv->frames,
157                         sizeof(struct caca_frame) * cv->framecount);
158
159    for(f = cv->framecount - 1; f > id; f--)
160        cv->frames[f] = cv->frames[f - 1];
161
162    if(cv->frame >= id)
163        cv->frame++;
164
165    cv->frames[id].width = cv->width;
166    cv->frames[id].height = cv->height;
167    cv->frames[id].chars = malloc(size * sizeof(uint32_t));
168    memcpy(cv->frames[id].chars, cv->chars, size * sizeof(uint32_t));
169    cv->frames[id].attrs = malloc(size * sizeof(uint32_t));
170    memcpy(cv->frames[id].attrs, cv->attrs, size * sizeof(uint32_t));
171    cv->frames[id].curattr = cv->curattr;
172
173    cv->frames[id].x = cv->frames[cv->frame].x;
174    cv->frames[id].y = cv->frames[cv->frame].y;
175    cv->frames[id].handlex = cv->frames[cv->frame].handlex;
176    cv->frames[id].handley = cv->frames[cv->frame].handley;
177
178    cv->frames[id].name = strdup("frame#--------");
179    sprintf(cv->frames[id].name + 6, "%.08x", ++cv->autoinc);
180
181    return 0;
182}
183
184/** \brief Remove a frame from a canvas.
185 *
186 *  Delete a frame from a given canvas.
187 *
188 *  The frame index indicates the frame to delete. Valid values range from
189 *  0 to the current canvas frame count minus 1. If the frame index is
190 *  greater than or equals the current canvas frame count, the last frame
191 *  is deleted.
192 *
193 *  If the active frame is deleted, frame 0 becomes the new active frame.
194 *  Otherwise, the active frame does not change, but its index may be
195 *  renumbered due to the deletion.
196 *
197 *  If an error occurs, -1 is returned and \b errno is set accordingly:
198 *  - \c EINVAL Requested frame is out of range, or attempt to delete the
199 *    last frame of the canvas.
200 *
201 *  \param cv A libcaca canvas
202 *  \param id The index of the frame to delete
203 *  \return 0 in case of success, -1 if an error occurred.
204 */
205int caca_free_frame(caca_canvas_t *cv, int id)
206{
207    int f;
208
209    if(id < 0 || id >= cv->framecount)
210    {
211        seterrno(EINVAL);
212        return -1;
213    }
214
215    if(cv->framecount == 1)
216    {
217        seterrno(EINVAL);
218        return -1;
219    }
220
221    free(cv->frames[id].chars);
222    free(cv->frames[id].attrs);
223    free(cv->frames[id].name);
224
225    for(f = id + 1; f < cv->framecount; f++)
226        cv->frames[f - 1] = cv->frames[f];
227
228    cv->framecount--;
229    cv->frames = realloc(cv->frames,
230                         sizeof(struct caca_frame) * cv->framecount);
231
232    if(cv->frame > id)
233        cv->frame--;
234    else if(cv->frame == id)
235    {
236        cv->frame = 0;
237        _caca_load_frame_info(cv);
238        if(!cv->dirty_disabled)
239            caca_add_dirty_rect(cv, 0, 0, cv->width, cv->height);
240    }
241
242    return 0;
243}
244
245/*
246 * XXX: the following functions are local.
247 */
248
249void _caca_save_frame_info(caca_canvas_t *cv)
250{
251    cv->frames[cv->frame].width = cv->width;
252    cv->frames[cv->frame].height = cv->height;
253
254    cv->frames[cv->frame].curattr = cv->curattr;
255}
256
257void _caca_load_frame_info(caca_canvas_t *cv)
258{
259    cv->width = cv->frames[cv->frame].width;
260    cv->height = cv->frames[cv->frame].height;
261
262    cv->chars = cv->frames[cv->frame].chars;
263    cv->attrs = cv->frames[cv->frame].attrs;
264
265    cv->curattr = cv->frames[cv->frame].curattr;
266}
267
268/*
269 * XXX: The following functions are aliases.
270 */
271
272int cucul_get_frame_count(cucul_canvas_t const *)
273         CACA_ALIAS(caca_get_frame_count);
274int cucul_set_frame(cucul_canvas_t *, int) CACA_ALIAS(caca_set_frame);
275char const *cucul_get_frame_name(cucul_canvas_t const *)
276         CACA_ALIAS(caca_get_frame_name);
277int cucul_set_frame_name(cucul_canvas_t *, char const *)
278         CACA_ALIAS(caca_set_frame_name);
279int cucul_create_frame(cucul_canvas_t *, int) CACA_ALIAS(caca_create_frame);
280int cucul_free_frame(cucul_canvas_t *, int) CACA_ALIAS(caca_free_frame);
281
Note: See TracBrowser for help on using the repository browser.