2 * Asterisk -- An open source telephony toolkit.
4 * Copyright 2007-2008, Marta Carbone, Luigi Rizzo
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.
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.
20 * Message board implementation.
22 * A message board is a region of the SDL screen where
23 * messages can be printed, like on a terminal window.
25 * At the moment we support fix-size font.
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!)
32 * TODO: font dynamic allocation
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).
41 <support_level>extended</support_level>
44 #include "asterisk.h" /* ast_strdupa */
45 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
46 #include "asterisk/utils.h" /* ast_strdupa */
47 #include "console_video.h" /* ast_strdupa */
49 #ifdef HAVE_SDL /* we only use this code if SDL is available */
52 /* Fonts characterization. XXX should be read from the file */
53 #define FONT_H 20 /* char height, pixels */
54 #define FONT_W 9 /* char width, pixels */
57 int kb_output; /* identity of the board */
58 /* pointer to the destination surface (on the keypad window) */
59 SDL_Surface *screen; /* the main screen */
60 SDL_Rect *p_rect; /* where to write on the main screen */
61 SDL_Surface *blank; /* original content of the window */
63 int v_h; /* virtual text height, in lines */
64 int v_w; /* virtual text width, in lines (probably same as p_w) */
65 int p_h; /* physical (displayed) text height, in lines
66 * XXX p_h * FONT_H = pixel_height */
67 int p_w; /* physical (displayed) text width, in characters
68 * XXX p_w * FONT_W = pixel_width */
70 int cur_col; /* print position (free character) on the last line */
71 int cur_line; /* first (or last ?) virtual line displayed,
72 * 0 is the line at the bottom, 1 is the one above,...
75 SDL_Surface *font; /* points to a surface in the gui structure */
76 SDL_Rect *font_rects; /* pointer to the font rects */
78 /* text buffer, v_h * v_w char.
79 * We make sure the buffer is always full,
80 * print on some position on the last line,
81 * and scroll up when appending new text
85 /*! \brief Initialize the board.
86 * return 0 on success, 1 on error
87 * TODO, if this is done at reload time,
88 * free resources before allocate new ones
89 * TODO: resource deallocation in case of error.
90 * TODO: move the font load at gui_initialization
91 * TODO: deallocation of the message history
93 struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
94 SDL_Surface *font, SDL_Rect *font_rects);
95 struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
96 SDL_Surface *font, SDL_Rect *font_rects)
98 struct board *b = ast_calloc(1, sizeof (*b));
103 /* font, points to the gui structure */
105 b->font_rects = font_rects;
107 /* Destination rectangle on the screen - reference is the whole screen */
111 /* compute physical sizes */
112 b->p_h = b->p_rect->h/FONT_H;
113 b->p_w = b->p_rect->w/FONT_W;
116 b->v_h = b->p_h * 10; /* XXX 10 times larger */
117 b->v_w = b->p_w; /* same width */
119 /* the rectangle we actually use */
120 br.h = b->p_h * FONT_H; /* pixel sizes of the background */
121 br.w = b->p_w * FONT_W;
124 /* allocate a buffer for the text */
125 b->text = ast_calloc(b->v_w*b->v_h + 1, 1);
126 if (b->text == NULL) {
127 ast_log(LOG_WARNING, "Unable to allocate board history memory.\n");
131 memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */
133 /* make a copy of the original rectangle, for cleaning up */
134 b->blank = SDL_CreateRGBSurface(screen->flags, br.w, br.h,
135 screen->format->BitsPerPixel,
136 screen->format->Rmask, screen->format->Gmask,
137 screen->format->Bmask, screen->format->Amask);
139 if (b->blank == NULL) {
140 ast_log(LOG_WARNING, "Unable to allocate board virtual screen: %s\n",
146 SDL_BlitSurface(screen, b->p_rect, b->blank, &br);
148 /* Set color key, if not alpha channel present */
149 //colorkey = SDL_MapRGB(b->board_surface->format, 0, 0, 0);
150 //SDL_SetColorKey(b->board_surface, SDL_SRCCOLORKEY, colorkey);
152 b->cur_col = 0; /* current print column */
153 b->cur_line = 0; /* last line displayed */
155 if (0) ast_log(LOG_WARNING, "Message board %dx%d@%d,%d successfully initialized\n",
156 b->p_rect->w, b->p_rect->h,
157 b->p_rect->x, b->p_rect->y);
161 /* Render the text on the board surface.
162 * The first line to render is the one at v_h - p_h - cur_line,
163 * the size is p_h * p_w.
164 * XXX we assume here that p_w = v_w.
166 static void render_board(struct board *b)
168 int first_row = b->v_h - b->p_h - b->cur_line;
169 int first_char = b->v_w * first_row;
170 int last_char = first_char + b->p_h * b->v_w;
174 /* top left char on the physical surface */
177 dst.x = b->p_rect->x;
178 dst.y = b->p_rect->y;
181 /* clean the surface board */
182 SDL_BlitSurface(b->blank, NULL, b->screen, b->p_rect);
184 /* blit all characters */
185 for (i = first_char, col = 0; i < last_char; i++) {
186 int c = b->text[i] - 32; /* XXX first 32 chars are not printable */
187 if (c < 0) /* buffer terminator or anything else is a blank */
189 SDL_BlitSurface(b->font, &b->font_rects[c], b->screen, &dst);
190 /* point dst to next char position */
193 if (col >= b->v_w) { /* next row */
194 dst.x = b->p_rect->x;
199 SDL_UpdateRects(b->screen, 1, b->p_rect); /* Update the screen */
202 void move_message_board(struct board *b, int dy)
204 int cur = b->cur_line + dy;
207 else if (cur >= b->v_h - b->p_h)
208 cur = b->v_h - b->p_h - 1;
213 /* return the content of a board */
214 const char *read_message(const struct board *b)
219 int reset_board(struct board *b)
221 memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */
227 /* Store the message on the history board
228 * and blit on screen if required.
229 * XXX now easy. only regular chars
231 int print_message(struct board *b, const char *s)
236 if (ast_strlen_zero(s))
242 /* First, only check how much space we need.
243 * Starting from the current print position, we move
244 * it forward and down (if necessary) according to input
245 * characters (including newlines, tabs, backspaces...).
246 * At the end, row tells us how many rows to scroll, and
247 * col (ignored) is the final print position.
249 for (i = 0; i < l; i++) {
263 if (s[i] < 32) /* signed, so take up to 127 */
273 /* scroll the text window */
274 if (row > 0) { /* need to scroll by 'row' rows */
275 memcpy(b->text, b->text + row * b->v_w, b->v_w * (b->v_h - row));
276 /* clean the destination area */
277 dst = b->text + b->v_w * (b->v_h - row - 1) + b->cur_col;
278 memset(dst, ' ', b->v_w - b->cur_col + b->v_w * row);
280 /* now do the actual printing. The print position is 'row' lines up
281 * from the bottom of the buffer, start at the same 'cur_col' as before.
282 * dst points to the beginning of the current line.
284 dst = b->text + b->v_w * (b->v_h - row - 1); /* start of current line */
286 for (i = 0; i < l; i++) {
291 case '\n': /* move to beginning of next line */
292 dst[col] = '\0'; /* mark the rest of the line as empty */
296 case '\b': /* one char back */
299 dst[col] = ' '; /* delete current char */
302 if (s[i] < 32) /* signed, so take up to 127 */
303 break; /* non printable */
304 dst[col] = s[i]; /* store character */
313 dst[col] = '\0'; /* the current position is empty */
315 /* everything is printed now, must do the rendering */
321 * we make the free operation on any fields of the board structure allocated
324 void delete_board(struct board *b)
327 /* deletes the text */
330 /* deallocates the blank surface */
331 SDL_FreeSurface(b->blank);
332 /* deallocates the board */
338 /*! \brief refresh the screen, and also grab a bunch of events.
340 static int scroll_message(...)
342 if moving up, scroll text up;
343 if (gui->message_board.screen_cur > 0)
344 gui->message_board.screen_cur--;
345 otherwise scroll text down.
346 if ((b->screen_cur + b->p_line) < b->board_next) {
347 gui->message_board.screen_cur++;
350 #endif /* HAVE_SDL */