Asterisk - The Open Source Telephony Project GIT-master-7e7a603
console_board.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright 2007-2008, Marta Carbone, Luigi Rizzo
5 *
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
11 *
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
15 *
16 * $Revision$
17 */
18
19/*
20 * Message board implementation.
21 *
22 * A message board is a region of the SDL screen where
23 * messages can be printed, like on a terminal window.
24 *
25 * At the moment we support fix-size font.
26 *
27 * The text is stored in a buffer
28 * of fixed size (rows and cols). A portion of the buffer is
29 * visible on the screen, and the visible window can be moved up and
30 * down by dragging (not yet!)
31 *
32 * TODO: font dynamic allocation
33 *
34 * The region where the text is displayed on the screen is defined
35 * as keypad element, (the name is defined in the `region' variable
36 * so the board geometry can be read from the skin or from the
37 * configuration file).
38 */
39
40/*** MODULEINFO
41 <support_level>extended</support_level>
42 ***/
43
44#include "asterisk.h" /* ast_strdupa */
45#include "asterisk/utils.h" /* ast_strdupa */
46#include "console_video.h" /* ast_strdupa */
47
48#ifdef HAVE_SDL /* we only use this code if SDL is available */
49#include <SDL/SDL.h>
50
51/* Fonts characterization. XXX should be read from the file */
52#define FONT_H 20 /* char height, pixels */
53#define FONT_W 9 /* char width, pixels */
54
55struct board {
56 int kb_output; /* identity of the board */
57 /* pointer to the destination surface (on the keypad window) */
58 SDL_Surface *screen; /* the main screen */
59 SDL_Rect *p_rect; /* where to write on the main screen */
60 SDL_Surface *blank; /* original content of the window */
61
62 int v_h; /* virtual text height, in lines */
63 int v_w; /* virtual text width, in lines (probably same as p_w) */
64 int p_h; /* physical (displayed) text height, in lines
65 * XXX p_h * FONT_H = pixel_height */
66 int p_w; /* physical (displayed) text width, in characters
67 * XXX p_w * FONT_W = pixel_width */
68
69 int cur_col; /* print position (free character) on the last line */
70 int cur_line; /* first (or last ?) virtual line displayed,
71 * 0 is the line at the bottom, 1 is the one above,...
72 */
73
74 SDL_Surface *font; /* points to a surface in the gui structure */
75 SDL_Rect *font_rects; /* pointer to the font rects */
76 char *text;
77 /* text buffer, v_h * v_w char.
78 * We make sure the buffer is always full,
79 * print on some position on the last line,
80 * and scroll up when appending new text
81 */
82};
83
84/*! \brief Initialize the board.
85 * return 0 on success, 1 on error
86 * TODO, if this is done at reload time,
87 * free resources before allocate new ones
88 * TODO: resource deallocation in case of error.
89 * TODO: move the font load at gui_initialization
90 * TODO: deallocation of the message history
91 */
92struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
93 SDL_Surface *font, SDL_Rect *font_rects);
94struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
95 SDL_Surface *font, SDL_Rect *font_rects)
96{
97 struct board *b = ast_calloc(1, sizeof (*b));
98 SDL_Rect br;
99
100 if (b == NULL)
101 return NULL;
102 /* font, points to the gui structure */
103 b->font = font;
104 b->font_rects = font_rects;
105
106 /* Destination rectangle on the screen - reference is the whole screen */
107 b->p_rect = dest;
108 b->screen = screen;
109
110 /* compute physical sizes */
111 b->p_h = b->p_rect->h/FONT_H;
112 b->p_w = b->p_rect->w/FONT_W;
113
114 /* virtual sizes */
115 b->v_h = b->p_h * 10; /* XXX 10 times larger */
116 b->v_w = b->p_w; /* same width */
117
118 /* the rectangle we actually use */
119 br.h = b->p_h * FONT_H; /* pixel sizes of the background */
120 br.w = b->p_w * FONT_W;
121 br.x = br.y = 0;
122
123 /* allocate a buffer for the text */
124 b->text = ast_calloc(b->v_w*b->v_h + 1, 1);
125 if (b->text == NULL) {
126 ast_log(LOG_WARNING, "Unable to allocate board history memory.\n");
127 ast_free(b);
128 return NULL;
129 }
130 memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */
131
132 /* make a copy of the original rectangle, for cleaning up */
133 b->blank = SDL_CreateRGBSurface(screen->flags, br.w, br.h,
134 screen->format->BitsPerPixel,
135 screen->format->Rmask, screen->format->Gmask,
136 screen->format->Bmask, screen->format->Amask);
137
138 if (b->blank == NULL) {
139 ast_log(LOG_WARNING, "Unable to allocate board virtual screen: %s\n",
140 SDL_GetError());
141 ast_free(b->text);
142 ast_free(b);
143 return NULL;
144 }
145 SDL_BlitSurface(screen, b->p_rect, b->blank, &br);
146
147 /* Set color key, if not alpha channel present */
148 //colorkey = SDL_MapRGB(b->board_surface->format, 0, 0, 0);
149 //SDL_SetColorKey(b->board_surface, SDL_SRCCOLORKEY, colorkey);
150
151 b->cur_col = 0; /* current print column */
152 b->cur_line = 0; /* last line displayed */
153
154 if (0) ast_log(LOG_WARNING, "Message board %dx%d@%d,%d successfully initialized\n",
155 b->p_rect->w, b->p_rect->h,
156 b->p_rect->x, b->p_rect->y);
157 return b;
158}
159
160/* Render the text on the board surface.
161 * The first line to render is the one at v_h - p_h - cur_line,
162 * the size is p_h * p_w.
163 * XXX we assume here that p_w = v_w.
164 */
165static void render_board(struct board *b)
166{
167 int first_row = b->v_h - b->p_h - b->cur_line;
168 int first_char = b->v_w * first_row;
169 int last_char = first_char + b->p_h * b->v_w;
170 int i, col;
171 SDL_Rect dst;
172
173 /* top left char on the physical surface */
174 dst.w = FONT_W;
175 dst.h = FONT_H;
176 dst.x = b->p_rect->x;
177 dst.y = b->p_rect->y;
178
179
180 /* clean the surface board */
181 SDL_BlitSurface(b->blank, NULL, b->screen, b->p_rect);
182
183 /* blit all characters */
184 for (i = first_char, col = 0; i < last_char; i++) {
185 int c = b->text[i] - 32; /* XXX first 32 chars are not printable */
186 if (c < 0) /* buffer terminator or anything else is a blank */
187 c = 0;
188 SDL_BlitSurface(b->font, &b->font_rects[c], b->screen, &dst);
189 /* point dst to next char position */
190 dst.x += dst.w;
191 col++;
192 if (col >= b->v_w) { /* next row */
193 dst.x = b->p_rect->x;
194 dst.y += dst.h;
195 col = 0;
196 }
197 }
198 SDL_UpdateRects(b->screen, 1, b->p_rect); /* Update the screen */
199}
200
201void move_message_board(struct board *b, int dy)
202{
203 int cur = b->cur_line + dy;
204 if (cur < 0)
205 cur = 0;
206 else if (cur >= b->v_h - b->p_h)
207 cur = b->v_h - b->p_h - 1;
208 b->cur_line = cur;
209 render_board(b);
210}
211
212/* return the content of a board */
213const char *read_message(const struct board *b)
214{
215 return b->text;
216}
217
218int reset_board(struct board *b)
219{
220 memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */
221 b->cur_col = 0;
222 b->cur_line = 0;
223 render_board(b);
224 return 0;
225}
226/* Store the message on the history board
227 * and blit on screen if required.
228 * XXX now easy. only regular chars
229 */
230int print_message(struct board *b, const char *s)
231{
232 int i, l, row, col;
233 char *dst;
234
235 if (ast_strlen_zero(s))
236 return 0;
237
238 l = strlen(s);
239 row = 0;
240 col = b->cur_col;
241 /* First, only check how much space we need.
242 * Starting from the current print position, we move
243 * it forward and down (if necessary) according to input
244 * characters (including newlines, tabs, backspaces...).
245 * At the end, row tells us how many rows to scroll, and
246 * col (ignored) is the final print position.
247 */
248 for (i = 0; i < l; i++) {
249 switch (s[i]) {
250 case '\r':
251 col = 0;
252 break;
253 case '\n':
254 col = 0;
255 row++;
256 break;
257 case '\b':
258 if (col > 0)
259 col--;
260 break;
261 default:
262 if (s[i] < 32) /* signed, so take up to 127 */
263 break;
264 col++;
265 if (col >= b->v_w) {
266 col -= b->v_w;
267 row++;
268 }
269 break;
270 }
271 }
272 /* scroll the text window */
273 if (row > 0) { /* need to scroll by 'row' rows */
274 memcpy(b->text, b->text + row * b->v_w, b->v_w * (b->v_h - row));
275 /* clean the destination area */
276 dst = b->text + b->v_w * (b->v_h - row - 1) + b->cur_col;
277 memset(dst, ' ', b->v_w - b->cur_col + b->v_w * row);
278 }
279 /* now do the actual printing. The print position is 'row' lines up
280 * from the bottom of the buffer, start at the same 'cur_col' as before.
281 * dst points to the beginning of the current line.
282 */
283 dst = b->text + b->v_w * (b->v_h - row - 1); /* start of current line */
284 col = b->cur_col;
285 for (i = 0; i < l; i++) {
286 switch (s[i]) {
287 case '\r':
288 col = 0;
289 break;
290 case '\n': /* move to beginning of next line */
291 dst[col] = '\0'; /* mark the rest of the line as empty */
292 col = 0;
293 dst += b->v_w;
294 break;
295 case '\b': /* one char back */
296 if (col > 0)
297 col--;
298 dst[col] = ' '; /* delete current char */
299 break;
300 default:
301 if (s[i] < 32) /* signed, so take up to 127 */
302 break; /* non printable */
303 dst[col] = s[i]; /* store character */
304 col++;
305 if (col >= b->v_w) {
306 col -= b->v_w;
307 dst += b->v_w;
308 }
309 break;
310 }
311 }
312 dst[col] = '\0'; /* the current position is empty */
313 b->cur_col = col;
314 /* everything is printed now, must do the rendering */
315 render_board(b);
316 return 1;
317}
318
319/* deletes a board.
320 * we make the free operation on any fields of the board structure allocated
321 * in dynamic memory
322 */
323void delete_board(struct board *b)
324{
325 if (b) {
326 /* deletes the text */
327 if (b->text)
328 ast_free (b->text);
329 /* deallocates the blank surface */
330 SDL_FreeSurface(b->blank);
331 /* deallocates the board */
332 ast_free(b);
333 }
334}
335
336#if 0
337/*! \brief refresh the screen, and also grab a bunch of events.
338 */
339static int scroll_message(...)
340{
341if moving up, scroll text up;
342 if (gui->message_board.screen_cur > 0)
343 gui->message_board.screen_cur--;
344otherwise scroll text down.
345 if ((b->screen_cur + b->p_line) < b->board_next) {
346 gui->message_board.screen_cur++;
347#endif /* notyet */
348
349#endif /* HAVE_SDL */
char * text
Definition: app_queue.c:1639
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
void delete_board(struct board *b)
deallocates memory space for a board
kb_output
void move_message_board(struct board *b, int dy)
const char * read_message(const struct board *b)
return the whole text from a board
int reset_board(struct board *b)
reset the board to blank
int print_message(struct board *b, const char *s)
#define LOG_WARNING
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
static struct test_val b
static struct test_val c
Utility functions.