source: libcaca/trunk/cucul/frame.c @ 1390

Last change on this file since 1390 was 1390, checked in by Sam Hocevar, 13 years ago
  • Implemented cucul_get_frame_name() and cucul_set_frame_name().
  • Renamed cucul_*_canvas_frame() to cucul_*_frame() because we bloody well know that a frame belongs to a canvas.
  • Property svn:keywords set to Id
File size: 6.9 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: frame.c 1390 2006-11-13 23:16:35Z 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 a small framework for canvas frame management.
16 */
17
18#include "config.h"
19#include "common.h"
20
21#if !defined(__KERNEL__)
22#   include <stdio.h>
23#   include <stdlib.h>
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30/** \brief Get the number of frames in a canvas.
31 *
32 *  Return the current canvas' frame count.
33 *
34 *  This function never fails.
35 *
36 *  \param cv A libcucul canvas
37 *  \return The frame count
38 */
39unsigned int cucul_get_frame_count(cucul_canvas_t *cv)
40{
41    return cv->framecount;
42}
43
44/** \brief Activate a given canvas frame.
45 *
46 *  Set the active canvas frame. All subsequent drawing operations will
47 *  be performed on that frame. The current painting context set by
48 *  cucul_set_attr() is inherited.
49 *
50 *  If the frame index is outside the canvas' frame range, nothing happens.
51 *
52 *  If an error occurs, -1 is returned and \b errno is set accordingly:
53 *  - \c EINVAL Requested frame is out of range.
54 *
55 *  \param cv A libcucul canvas
56 *  \param id The canvas frame to activate
57 *  \return 0 in case of success, -1 if an error occurred.
58 */
59int cucul_set_frame(cucul_canvas_t *cv, unsigned int id)
60{
61    if(id >= cv->framecount)
62    {
63        seterrno(EINVAL);
64        return -1;
65    }
66
67    _cucul_save_frame_info(cv);
68    cv->frame = id;
69    _cucul_load_frame_info(cv);
70
71    return 0;
72}
73
74/** \brief Get the current frame's name.
75 *
76 *  Return the current frame's name. The returned string is valid until
77 *  the frame is deleted or cucul_set_frame_name() is called to change
78 *  the frame name again.
79 *
80 *  This function never fails.
81 *
82 *  \param cv A libcucul canvas.
83 *  \return The current frame's name.
84 */
85char const *cucul_get_frame_name(cucul_canvas_t *cv)
86{
87    return cv->frames[cv->frame].name;
88}
89
90/** \brief Set the current frame's name.
91 *
92 *  Set the current frame's name. Upon creation, a frame has a default name
93 *  of \rc "frame#xxxxxxxx" where \c xxxxxxxx is a self-incrementing
94 *  hexadecimal number.
95 *
96 *  If an error occurs, -1 is returned and \b errno is set accordingly:
97 *  - \c ENOMEM Not enough memory to allocate new frame.
98 *
99 *  \param cv A libcucul canvas.
100 *  \return 0 in case of success, -1 if an error occurred.
101 */
102int cucul_set_frame_name(cucul_canvas_t *cv, char const *name)
103{
104    char *newname = strdup(name);
105
106    if(!newname)
107    {
108        seterrno(ENOMEM);
109        return -1;
110    }
111
112    free(cv->frames[cv->frame].name);
113    cv->frames[cv->frame].name = newname;
114
115    return 0;
116}
117
118/** \brief Add a frame to a canvas.
119 *
120 *  Create a new frame within the given canvas. Its contents and attributes
121 *  are copied from the currently active frame.
122 *
123 *  The frame index indicates where the frame should be inserted. Valid
124 *  values range from 0 to the current canvas frame count. If the frame
125 *  index is greater than or equals the current canvas frame count, the new
126 *  frame is appended at the end of the canvas.
127 *
128 *  The active frame does not change, but its index may be renumbered due
129 *  to the insertion.
130 *
131 *  If an error occurs, -1 is returned and \b errno is set accordingly:
132 *  - \c ENOMEM Not enough memory to allocate new frame.
133 *
134 *  \param cv A libcucul canvas
135 *  \param id The index where to insert the new frame
136 *  \return 0 in case of success, -1 if an error occurred.
137 */
138int cucul_create_frame(cucul_canvas_t *cv, unsigned int id)
139{
140    unsigned int size = cv->width * cv->height;
141    unsigned int f;
142
143    if(id > cv->framecount)
144        id = cv->framecount;
145
146    cv->framecount++;
147    cv->frames = realloc(cv->frames,
148                         sizeof(struct cucul_frame) * cv->framecount);
149
150    for(f = cv->framecount - 1; f > id; f--)
151        cv->frames[f] = cv->frames[f - 1];
152
153    if(cv->frame >= id)
154        cv->frame++;
155
156    cv->frames[id].width = cv->width;
157    cv->frames[id].height = cv->height;
158    cv->frames[id].chars = malloc(size * sizeof(uint32_t));
159    memcpy(cv->frames[id].chars, cv->chars, size * sizeof(uint32_t));
160    cv->frames[id].attrs = malloc(size * sizeof(uint32_t));
161    memcpy(cv->frames[id].attrs, cv->attrs, size * sizeof(uint32_t));
162    cv->frames[id].curattr = cv->curattr;
163
164    cv->frames[id].x = cv->frames[cv->frame].x;
165    cv->frames[id].y = cv->frames[cv->frame].y;
166    cv->frames[id].handlex = cv->frames[cv->frame].handlex;
167    cv->frames[id].handley = cv->frames[cv->frame].handley;
168
169    cv->frames[id].name = strdup("frame#--------");
170    sprintf(cv->frames[id].name + 6, "%.08x", cv->autoinc++);
171
172    return 0;
173}
174
175/** \brief Remove a frame from a canvas.
176 *
177 *  Delete a frame from a given canvas.
178 *
179 *  The frame index indicates the frame to delete. Valid values range from
180 *  0 to the current canvas frame count minus 1. If the frame index is
181 *  greater the or equals the current canvas frame count, the last frame
182 *  is deleted.
183 *
184 *  If the active frame is deleted, frame 0 becomes the new active frame.
185 *  Otherwise, the active frame does not change, but its index may be
186 *  renumbered due to the deletion.
187 *
188 *  If an error occurs, -1 is returned and \b errno is set accordingly:
189 *  - \c EINVAL Requested frame is out of range, or attempt to delete the
190 *    last frame of the canvas.
191 *
192 *  \param cv A libcucul canvas
193 *  \param id The index of the frame to delete
194 *  \return 0 in case of success, -1 if an error occurred.
195 */
196int cucul_free_frame(cucul_canvas_t *cv, unsigned int id)
197{
198    unsigned int f;
199
200    if(id >= cv->framecount)
201    {
202        seterrno(EINVAL);
203        return -1;
204    }
205
206    if(cv->framecount == 1)
207    {
208        seterrno(EINVAL);
209        return -1;
210    }
211
212    free(cv->frames[id].chars);
213    free(cv->frames[id].attrs);
214    free(cv->frames[id].name);
215
216    for(f = id + 1; f < cv->framecount; f++)
217        cv->frames[f - 1] = cv->frames[f];
218
219    cv->framecount--;
220    cv->frames = realloc(cv->frames,
221                         sizeof(struct cucul_frame) * cv->framecount);
222
223    if(cv->frame > id)
224        cv->frame--;
225    else if(cv->frame == id)
226    {
227        cv->frame = 0;
228        _cucul_load_frame_info(cv);
229    }
230
231    return 0;
232}
233
234/*
235 * XXX: the following functions are local.
236 */
237
238void _cucul_save_frame_info(cucul_canvas_t *cv)
239{
240    cv->frames[cv->frame].width = cv->width;
241    cv->frames[cv->frame].height = cv->height;
242
243    cv->frames[cv->frame].curattr = cv->curattr;
244}
245
246void _cucul_load_frame_info(cucul_canvas_t *cv)
247{
248    cv->width = cv->frames[cv->frame].width;
249    cv->height = cv->frames[cv->frame].height;
250
251    cv->chars = cv->frames[cv->frame].chars;
252    cv->attrs = cv->frames[cv->frame].attrs;
253
254    cv->curattr = cv->frames[cv->frame].curattr;
255}
256
Note: See TracBrowser for help on using the repository browser.