[115] | 1 | /* |
---|
[672] | 2 | * libcucul Canvas for ultrafast compositing of Unicode letters |
---|
[527] | 3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
[268] | 4 | * All Rights Reserved |
---|
[115] | 5 | * |
---|
[769] | 6 | * $Id: frame.c 1431 2006-11-27 01:56:37Z sam $ |
---|
| 7 | * |
---|
[268] | 8 | * This library is free software; you can redistribute it and/or |
---|
[522] | 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. |
---|
[115] | 12 | */ |
---|
| 13 | |
---|
[769] | 14 | /* |
---|
[842] | 15 | * This file contains a small framework for canvas frame management. |
---|
[205] | 16 | */ |
---|
| 17 | |
---|
[115] | 18 | #include "config.h" |
---|
[859] | 19 | #include "common.h" |
---|
[115] | 20 | |
---|
[568] | 21 | #if !defined(__KERNEL__) |
---|
| 22 | # include <stdio.h> |
---|
| 23 | # include <stdlib.h> |
---|
| 24 | # include <string.h> |
---|
| 25 | #endif |
---|
[115] | 26 | |
---|
[524] | 27 | #include "cucul.h" |
---|
| 28 | #include "cucul_internals.h" |
---|
[115] | 29 | |
---|
[842] | 30 | /** \brief Get the number of frames in a canvas. |
---|
[257] | 31 | * |
---|
[1231] | 32 | * Return the current canvas' frame count. |
---|
[257] | 33 | * |
---|
[920] | 34 | * This function never fails. |
---|
| 35 | * |
---|
[842] | 36 | * \param cv A libcucul canvas |
---|
| 37 | * \return The frame count |
---|
[257] | 38 | */ |
---|
[1390] | 39 | unsigned int cucul_get_frame_count(cucul_canvas_t *cv) |
---|
[121] | 40 | { |
---|
[842] | 41 | return cv->framecount; |
---|
[153] | 42 | } |
---|
| 43 | |
---|
[842] | 44 | /** \brief Activate a given canvas frame. |
---|
[257] | 45 | * |
---|
[1231] | 46 | * Set the active canvas frame. All subsequent drawing operations will |
---|
| 47 | * be performed on that frame. The current painting context set by |
---|
[1257] | 48 | * cucul_set_attr() is inherited. |
---|
[842] | 49 | * |
---|
| 50 | * If the frame index is outside the canvas' frame range, nothing happens. |
---|
| 51 | * |
---|
[920] | 52 | * If an error occurs, -1 is returned and \b errno is set accordingly: |
---|
| 53 | * - \c EINVAL Requested frame is out of range. |
---|
| 54 | * |
---|
[842] | 55 | * \param cv A libcucul canvas |
---|
[1390] | 56 | * \param id The canvas frame to activate |
---|
[920] | 57 | * \return 0 in case of success, -1 if an error occurred. |
---|
[257] | 58 | */ |
---|
[1390] | 59 | int cucul_set_frame(cucul_canvas_t *cv, unsigned int id) |
---|
[153] | 60 | { |
---|
[1390] | 61 | if(id >= cv->framecount) |
---|
[920] | 62 | { |
---|
[1362] | 63 | seterrno(EINVAL); |
---|
[920] | 64 | return -1; |
---|
| 65 | } |
---|
[153] | 66 | |
---|
[1381] | 67 | _cucul_save_frame_info(cv); |
---|
[1390] | 68 | cv->frame = id; |
---|
[1381] | 69 | _cucul_load_frame_info(cv); |
---|
[121] | 70 | |
---|
[920] | 71 | return 0; |
---|
[121] | 72 | } |
---|
| 73 | |
---|
[1390] | 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 | */ |
---|
| 85 | char 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 |
---|
[1397] | 93 | * of \c "frame#xxxxxxxx" where \c xxxxxxxx is a self-incrementing |
---|
[1390] | 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. |
---|
[1397] | 100 | * \param name The name to give to the current frame. |
---|
[1390] | 101 | * \return 0 in case of success, -1 if an error occurred. |
---|
| 102 | */ |
---|
| 103 | int cucul_set_frame_name(cucul_canvas_t *cv, char const *name) |
---|
| 104 | { |
---|
| 105 | char *newname = strdup(name); |
---|
| 106 | |
---|
| 107 | if(!newname) |
---|
| 108 | { |
---|
| 109 | seterrno(ENOMEM); |
---|
| 110 | return -1; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | free(cv->frames[cv->frame].name); |
---|
| 114 | cv->frames[cv->frame].name = newname; |
---|
| 115 | |
---|
| 116 | return 0; |
---|
| 117 | } |
---|
| 118 | |
---|
[842] | 119 | /** \brief Add a frame to a canvas. |
---|
[257] | 120 | * |
---|
[1341] | 121 | * Create a new frame within the given canvas. Its contents and attributes |
---|
| 122 | * are copied from the currently active frame. |
---|
[842] | 123 | * |
---|
| 124 | * The frame index indicates where the frame should be inserted. Valid |
---|
| 125 | * values range from 0 to the current canvas frame count. If the frame |
---|
[1338] | 126 | * index is greater than or equals the current canvas frame count, the new |
---|
[842] | 127 | * frame is appended at the end of the canvas. |
---|
| 128 | * |
---|
| 129 | * The active frame does not change, but its index may be renumbered due |
---|
| 130 | * to the insertion. |
---|
| 131 | * |
---|
[920] | 132 | * If an error occurs, -1 is returned and \b errno is set accordingly: |
---|
| 133 | * - \c ENOMEM Not enough memory to allocate new frame. |
---|
| 134 | * |
---|
[842] | 135 | * \param cv A libcucul canvas |
---|
[1357] | 136 | * \param id The index where to insert the new frame |
---|
[920] | 137 | * \return 0 in case of success, -1 if an error occurred. |
---|
[257] | 138 | */ |
---|
[1390] | 139 | int cucul_create_frame(cucul_canvas_t *cv, unsigned int id) |
---|
[121] | 140 | { |
---|
[1341] | 141 | unsigned int size = cv->width * cv->height; |
---|
[842] | 142 | unsigned int f; |
---|
[135] | 143 | |
---|
[1341] | 144 | if(id > cv->framecount) |
---|
| 145 | id = cv->framecount; |
---|
[153] | 146 | |
---|
[842] | 147 | cv->framecount++; |
---|
[1338] | 148 | cv->frames = realloc(cv->frames, |
---|
| 149 | sizeof(struct cucul_frame) * cv->framecount); |
---|
[121] | 150 | |
---|
[1341] | 151 | for(f = cv->framecount - 1; f > id; f--) |
---|
[1338] | 152 | cv->frames[f] = cv->frames[f - 1]; |
---|
[153] | 153 | |
---|
[1342] | 154 | if(cv->frame >= id) |
---|
| 155 | cv->frame++; |
---|
| 156 | |
---|
[1341] | 157 | cv->frames[id].width = cv->width; |
---|
| 158 | cv->frames[id].height = cv->height; |
---|
| 159 | cv->frames[id].chars = malloc(size * sizeof(uint32_t)); |
---|
| 160 | memcpy(cv->frames[id].chars, cv->chars, size * sizeof(uint32_t)); |
---|
| 161 | cv->frames[id].attrs = malloc(size * sizeof(uint32_t)); |
---|
| 162 | memcpy(cv->frames[id].attrs, cv->attrs, size * sizeof(uint32_t)); |
---|
| 163 | cv->frames[id].curattr = cv->curattr; |
---|
[153] | 164 | |
---|
[1342] | 165 | cv->frames[id].x = cv->frames[cv->frame].x; |
---|
| 166 | cv->frames[id].y = cv->frames[cv->frame].y; |
---|
| 167 | cv->frames[id].handlex = cv->frames[cv->frame].handlex; |
---|
| 168 | cv->frames[id].handley = cv->frames[cv->frame].handley; |
---|
[153] | 169 | |
---|
[1390] | 170 | cv->frames[id].name = strdup("frame#--------"); |
---|
[1431] | 171 | sprintf(cv->frames[id].name + 6, "%.08x", ++cv->autoinc); |
---|
[1390] | 172 | |
---|
[1431] | 173 | cv->frames[id].import = NULL; |
---|
| 174 | |
---|
[920] | 175 | return 0; |
---|
[153] | 176 | } |
---|
| 177 | |
---|
[842] | 178 | /** \brief Remove a frame from a canvas. |
---|
[257] | 179 | * |
---|
[1231] | 180 | * Delete a frame from a given canvas. |
---|
[842] | 181 | * |
---|
| 182 | * The frame index indicates the frame to delete. Valid values range from |
---|
| 183 | * 0 to the current canvas frame count minus 1. If the frame index is |
---|
[1431] | 184 | * greater than or equals the current canvas frame count, the last frame |
---|
[842] | 185 | * is deleted. |
---|
| 186 | * |
---|
| 187 | * If the active frame is deleted, frame 0 becomes the new active frame. |
---|
| 188 | * Otherwise, the active frame does not change, but its index may be |
---|
| 189 | * renumbered due to the deletion. |
---|
| 190 | * |
---|
[920] | 191 | * If an error occurs, -1 is returned and \b errno is set accordingly: |
---|
| 192 | * - \c EINVAL Requested frame is out of range, or attempt to delete the |
---|
| 193 | * last frame of the canvas. |
---|
| 194 | * |
---|
[811] | 195 | * \param cv A libcucul canvas |
---|
[1357] | 196 | * \param id The index of the frame to delete |
---|
[920] | 197 | * \return 0 in case of success, -1 if an error occurred. |
---|
[257] | 198 | */ |
---|
[1390] | 199 | int cucul_free_frame(cucul_canvas_t *cv, unsigned int id) |
---|
[153] | 200 | { |
---|
[842] | 201 | unsigned int f; |
---|
[115] | 202 | |
---|
[1341] | 203 | if(id >= cv->framecount) |
---|
[920] | 204 | { |
---|
[1362] | 205 | seterrno(EINVAL); |
---|
[920] | 206 | return -1; |
---|
| 207 | } |
---|
[135] | 208 | |
---|
[842] | 209 | if(cv->framecount == 1) |
---|
[920] | 210 | { |
---|
[1362] | 211 | seterrno(EINVAL); |
---|
[920] | 212 | return -1; |
---|
| 213 | } |
---|
[135] | 214 | |
---|
[1341] | 215 | free(cv->frames[id].chars); |
---|
| 216 | free(cv->frames[id].attrs); |
---|
[1390] | 217 | free(cv->frames[id].name); |
---|
[1431] | 218 | if(cv->frames[id].import) |
---|
| 219 | free(cv->frames[id].import); |
---|
[153] | 220 | |
---|
[1341] | 221 | for(f = id + 1; f < cv->framecount; f++) |
---|
[1338] | 222 | cv->frames[f - 1] = cv->frames[f]; |
---|
[153] | 223 | |
---|
[842] | 224 | cv->framecount--; |
---|
[1338] | 225 | cv->frames = realloc(cv->frames, |
---|
| 226 | sizeof(struct cucul_frame) * cv->framecount); |
---|
[115] | 227 | |
---|
[1341] | 228 | if(cv->frame > id) |
---|
[842] | 229 | cv->frame--; |
---|
[1341] | 230 | else if(cv->frame == id) |
---|
[1338] | 231 | { |
---|
[842] | 232 | cv->frame = 0; |
---|
[1381] | 233 | _cucul_load_frame_info(cv); |
---|
[1338] | 234 | } |
---|
[121] | 235 | |
---|
[920] | 236 | return 0; |
---|
[115] | 237 | } |
---|
| 238 | |
---|
[1338] | 239 | /* |
---|
| 240 | * XXX: the following functions are local. |
---|
| 241 | */ |
---|
| 242 | |
---|
[1381] | 243 | void _cucul_save_frame_info(cucul_canvas_t *cv) |
---|
[1338] | 244 | { |
---|
| 245 | cv->frames[cv->frame].width = cv->width; |
---|
| 246 | cv->frames[cv->frame].height = cv->height; |
---|
| 247 | |
---|
| 248 | cv->frames[cv->frame].curattr = cv->curattr; |
---|
| 249 | } |
---|
| 250 | |
---|
[1381] | 251 | void _cucul_load_frame_info(cucul_canvas_t *cv) |
---|
[1338] | 252 | { |
---|
| 253 | cv->width = cv->frames[cv->frame].width; |
---|
| 254 | cv->height = cv->frames[cv->frame].height; |
---|
| 255 | |
---|
| 256 | cv->chars = cv->frames[cv->frame].chars; |
---|
| 257 | cv->attrs = cv->frames[cv->frame].attrs; |
---|
| 258 | |
---|
| 259 | cv->curattr = cv->frames[cv->frame].curattr; |
---|
| 260 | } |
---|
| 261 | |
---|