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 #include "asterisk/utils.h" /* ast_strdupa */
46 #include "console_video.h" /* ast_strdupa */
48 #ifdef HAVE_SDL /* we only use this code if SDL is available */
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 */
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 */
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 */
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,...
74 SDL_Surface *font; /* points to a surface in the gui structure */
75 SDL_Rect *font_rects; /* pointer to the font rects */
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
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
92 struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
93 SDL_Surface *font, SDL_Rect *font_rects);
94 struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
95 SDL_Surface *font, SDL_Rect *font_rects)
97 struct board *b = ast_calloc(1, sizeof (*b));
102 /* font, points to the gui structure */
104 b->font_rects = font_rects;
106 /* Destination rectangle on the screen - reference is the whole screen */
110 /* compute physical sizes */
111 b->p_h = b->p_rect->h/FONT_H;
112 b->p_w = b->p_rect->w/FONT_W;
115 b->v_h = b->p_h * 10; /* XXX 10 times larger */
116 b->v_w = b->p_w; /* same width */
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;
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");
130 memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */
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);
138 if (b->blank == NULL) {
139 ast_log(LOG_WARNING, "Unable to allocate board virtual screen: %s\n",
145 SDL_BlitSurface(screen, b->p_rect, b->blank, &br);
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);
151 b->cur_col = 0; /* current print column */
152 b->cur_line = 0; /* last line displayed */
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);
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.
165 static void render_board(struct board *b)
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;
173 /* top left char on the physical surface */
176 dst.x = b->p_rect->x;
177 dst.y = b->p_rect->y;
180 /* clean the surface board */
181 SDL_BlitSurface(b->blank, NULL, b->screen, b->p_rect);
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 */
188 SDL_BlitSurface(b->font, &b->font_rects[c], b->screen, &dst);
189 /* point dst to next char position */
192 if (col >= b->v_w) { /* next row */
193 dst.x = b->p_rect->x;
198 SDL_UpdateRects(b->screen, 1, b->p_rect); /* Update the screen */
201 void move_message_board(struct board *b, int dy)
203 int cur = b->cur_line + dy;
206 else if (cur >= b->v_h - b->p_h)
207 cur = b->v_h - b->p_h - 1;
212 /* return the content of a board */
213 const char *read_message(const struct board *b)
218 int reset_board(struct board *b)
220 memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */
226 /* Store the message on the history board
227 * and blit on screen if required.
228 * XXX now easy. only regular chars
230 int print_message(struct board *b, const char *s)
235 if (ast_strlen_zero(s))
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.
248 for (i = 0; i < l; i++) {
262 if (s[i] < 32) /* signed, so take up to 127 */
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);
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.
283 dst = b->text + b->v_w * (b->v_h - row - 1); /* start of current line */
285 for (i = 0; i < l; i++) {
290 case '\n': /* move to beginning of next line */
291 dst[col] = '\0'; /* mark the rest of the line as empty */
295 case '\b': /* one char back */
298 dst[col] = ' '; /* delete current char */
301 if (s[i] < 32) /* signed, so take up to 127 */
302 break; /* non printable */
303 dst[col] = s[i]; /* store character */
312 dst[col] = '\0'; /* the current position is empty */
314 /* everything is printed now, must do the rendering */
320 * we make the free operation on any fields of the board structure allocated
323 void delete_board(struct board *b)
326 /* deletes the text */
329 /* deallocates the blank surface */
330 SDL_FreeSurface(b->blank);
331 /* deallocates the board */
337 /*! \brief refresh the screen, and also grab a bunch of events.
339 static int scroll_message(...)
341 if moving up, scroll text up;
342 if (gui->message_board.screen_cur > 0)
343 gui->message_board.screen_cur--;
344 otherwise scroll text down.
345 if ((b->screen_cur + b->p_line) < b->board_next) {
346 gui->message_board.screen_cur++;
349 #endif /* HAVE_SDL */