console_board.c 9.5 KB

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