console_board.c 9.5 KB

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