Wed Jan 8 2020 09:49:46

Asterisk developer's documentation


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: 369001 $
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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
46 #include "asterisk/utils.h" /* ast_strdupa */
47 #include "console_video.h" /* ast_strdupa */
48 
49 #ifdef HAVE_SDL /* we only use this code if SDL is available */
50 #include <SDL/SDL.h>
51 
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 */
55 
56 struct board {
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 */
62 
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 */
69 
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,...
73  */
74 
75  SDL_Surface *font; /* points to a surface in the gui structure */
76  SDL_Rect *font_rects; /* pointer to the font rects */
77  char *text;
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
82  */
83 };
84 
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
92  */
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)
97 {
98  struct board *b = ast_calloc(1, sizeof (*b));
99  SDL_Rect br;
100 
101  if (b == NULL)
102  return NULL;
103  /* font, points to the gui structure */
104  b->font = font;
105  b->font_rects = font_rects;
106 
107  /* Destination rectangle on the screen - reference is the whole screen */
108  b->p_rect = dest;
109  b->screen = screen;
110 
111  /* compute physical sizes */
112  b->p_h = b->p_rect->h/FONT_H;
113  b->p_w = b->p_rect->w/FONT_W;
114 
115  /* virtual sizes */
116  b->v_h = b->p_h * 10; /* XXX 10 times larger */
117  b->v_w = b->p_w; /* same width */
118 
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;
122  br.x = br.y = 0;
123 
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");
128  ast_free(b);
129  return NULL;
130  }
131  memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */
132 
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);
138 
139  if (b->blank == NULL) {
140  ast_log(LOG_WARNING, "Unable to allocate board virtual screen: %s\n",
141  SDL_GetError());
142  ast_free(b->text);
143  ast_free(b);
144  return NULL;
145  }
146  SDL_BlitSurface(screen, b->p_rect, b->blank, &br);
147 
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);
151 
152  b->cur_col = 0; /* current print column */
153  b->cur_line = 0; /* last line displayed */
154 
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);
158  return b;
159 }
160 
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.
165  */
166 static void render_board(struct board *b)
167 {
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;
171  int i, col;
172  SDL_Rect dst;
173 
174  /* top left char on the physical surface */
175  dst.w = FONT_W;
176  dst.h = FONT_H;
177  dst.x = b->p_rect->x;
178  dst.y = b->p_rect->y;
179 
180 
181  /* clean the surface board */
182  SDL_BlitSurface(b->blank, NULL, b->screen, b->p_rect);
183 
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 */
188  c = 0;
189  SDL_BlitSurface(b->font, &b->font_rects[c], b->screen, &dst);
190  /* point dst to next char position */
191  dst.x += dst.w;
192  col++;
193  if (col >= b->v_w) { /* next row */
194  dst.x = b->p_rect->x;
195  dst.y += dst.h;
196  col = 0;
197  }
198  }
199  SDL_UpdateRects(b->screen, 1, b->p_rect); /* Update the screen */
200 }
201 
202 void move_message_board(struct board *b, int dy)
203 {
204  int cur = b->cur_line + dy;
205  if (cur < 0)
206  cur = 0;
207  else if (cur >= b->v_h - b->p_h)
208  cur = b->v_h - b->p_h - 1;
209  b->cur_line = cur;
210  render_board(b);
211 }
212 
213 /* return the content of a board */
214 const char *read_message(const struct board *b)
215 {
216  return b->text;
217 }
218 
219 int reset_board(struct board *b)
220 {
221  memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */
222  b->cur_col = 0;
223  b->cur_line = 0;
224  render_board(b);
225  return 0;
226 }
227 /* Store the message on the history board
228  * and blit on screen if required.
229  * XXX now easy. only regular chars
230  */
231 int print_message(struct board *b, const char *s)
232 {
233  int i, l, row, col;
234  char *dst;
235 
236  if (ast_strlen_zero(s))
237  return 0;
238 
239  l = strlen(s);
240  row = 0;
241  col = b->cur_col;
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.
248  */
249  for (i = 0; i < l; i++) {
250  switch (s[i]) {
251  case '\r':
252  col = 0;
253  break;
254  case '\n':
255  col = 0;
256  row++;
257  break;
258  case '\b':
259  if (col > 0)
260  col--;
261  break;
262  default:
263  if (s[i] < 32) /* signed, so take up to 127 */
264  break;
265  col++;
266  if (col >= b->v_w) {
267  col -= b->v_w;
268  row++;
269  }
270  break;
271  }
272  }
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);
279  }
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.
283  */
284  dst = b->text + b->v_w * (b->v_h - row - 1); /* start of current line */
285  col = b->cur_col;
286  for (i = 0; i < l; i++) {
287  switch (s[i]) {
288  case '\r':
289  col = 0;
290  break;
291  case '\n': /* move to beginning of next line */
292  dst[col] = '\0'; /* mark the rest of the line as empty */
293  col = 0;
294  dst += b->v_w;
295  break;
296  case '\b': /* one char back */
297  if (col > 0)
298  col--;
299  dst[col] = ' '; /* delete current char */
300  break;
301  default:
302  if (s[i] < 32) /* signed, so take up to 127 */
303  break; /* non printable */
304  dst[col] = s[i]; /* store character */
305  col++;
306  if (col >= b->v_w) {
307  col -= b->v_w;
308  dst += b->v_w;
309  }
310  break;
311  }
312  }
313  dst[col] = '\0'; /* the current position is empty */
314  b->cur_col = col;
315  /* everything is printed now, must do the rendering */
316  render_board(b);
317  return 1;
318 }
319 
320 /* deletes a board.
321  * we make the free operation on any fields of the board structure allocated
322  * in dynamic memory
323  */
324 void delete_board(struct board *b)
325 {
326  if (b) {
327  /* deletes the text */
328  if (b->text)
329  ast_free (b->text);
330  /* deallocates the blank surface */
331  SDL_FreeSurface(b->blank);
332  /* deallocates the board */
333  ast_free(b);
334  }
335 }
336 
337 #if 0
338 /*! \brief refresh the screen, and also grab a bunch of events.
339  */
340 static int scroll_message(...)
341 {
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++;
348 #endif /* notyet */
349 
350 #endif /* HAVE_SDL */
int p_w
Definition: console_board.c:67
Asterisk main include file. File version handling, generic pbx functions.
int cur_col
Definition: console_board.c:70
int reset_board(struct board *b)
reset the board to blank
#define LOG_WARNING
Definition: logger.h:144
int p_h
Definition: console_board.c:65
SDL_Rect * p_rect
Definition: console_board.c:60
SDL_Rect * font_rects
Definition: console_board.c:76
char * text
Definition: app_queue.c:1091
SDL_Surface * blank
Definition: console_board.c:61
SDL_Surface * font
Definition: console_board.c:75
int cur_line
Definition: console_board.c:71
void delete_board(struct board *b)
deallocates memory space for a board
char * text
Definition: console_board.c:77
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
struct board * board_setup(SDL_Surface *screen, SDL_Rect *dest, SDL_Surface *font, SDL_Rect *font_rects)
Initialize the board. return 0 on success, 1 on error TODO, if this is done at reload time...
Definition: console_board.c:95
#define FONT_H
Definition: console_board.c:53
static void render_board(struct board *b)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_free(a)
Definition: astmm.h:97
void move_message_board(struct board *b, int dy)
const char * read_message(const struct board *b)
return the whole text from a board
#define FONT_W
Definition: console_board.c:54
#define ast_calloc(a, b)
Definition: astmm.h:82
int v_h
Definition: console_board.c:63
int print_message(struct board *b, const char *s)
int kb_output
Definition: console_board.c:57
SDL_Surface * screen
Definition: console_board.c:59
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int v_w
Definition: console_board.c:64