console_board.c 9.5 KB

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