Wed Jan 8 2020 09:50:10

Asterisk developer's documentation


console_board.c File Reference
#include "asterisk.h"
#include "asterisk/utils.h"
#include "console_video.h"
#include <SDL/SDL.h>

Go to the source code of this file.

Data Structures

struct  board
 

Macros

#define FONT_H   20 /* char height, pixels */
 
#define FONT_W   9 /* char width, pixels */
 

Functions

struct boardboard_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, free resources before allocate new ones TODO: resource deallocation in case of error. TODO: move the font load at gui_initialization TODO: deallocation of the message history. More...
 
void delete_board (struct board *b)
 deallocates memory space for a board More...
 
void move_message_board (struct board *b, int dy)
 
int print_message (struct board *b, const char *s)
 
const char * read_message (const struct board *b)
 return the whole text from a board More...
 
static void render_board (struct board *b)
 
int reset_board (struct board *b)
 reset the board to blank More...
 

Macro Definition Documentation

#define FONT_H   20 /* char height, pixels */

Definition at line 53 of file console_board.c.

Referenced by board_setup(), and render_board().

#define FONT_W   9 /* char width, pixels */

Definition at line 54 of file console_board.c.

Referenced by board_setup(), and render_board().

Function Documentation

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, free resources before allocate new ones TODO: resource deallocation in case of error. TODO: move the font load at gui_initialization TODO: deallocation of the message history.

Definition at line 95 of file console_board.c.

References ast_calloc, ast_free, ast_log(), board::blank, board::cur_col, board::cur_line, board::font, FONT_H, board::font_rects, FONT_W, LOG_WARNING, board::p_h, board::p_rect, board::p_w, board::screen, board::text, board::v_h, and board::v_w.

Referenced by init_board(), and sdl_setup().

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 }
int p_w
Definition: console_board.c:67
int cur_col
Definition: console_board.c:70
#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
SDL_Surface * blank
Definition: console_board.c:61
SDL_Surface * font
Definition: console_board.c:75
int cur_line
Definition: console_board.c:71
char * text
Definition: console_board.c:77
#define FONT_H
Definition: console_board.c:53
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
#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
SDL_Surface * screen
Definition: console_board.c:59
int v_w
Definition: console_board.c:64
void delete_board ( struct board b)

deallocates memory space for a board

Definition at line 324 of file console_board.c.

References ast_free, board::blank, and board::text.

Referenced by cleanup_sdl().

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 }
SDL_Surface * blank
Definition: console_board.c:61
char * text
Definition: console_board.c:77
#define ast_free(a)
Definition: astmm.h:97
void move_message_board ( struct board b,
int  dy 
)

Definition at line 202 of file console_board.c.

References board::cur_line, board::p_h, render_board(), and board::v_h.

Referenced by eventhandler().

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 }
int p_h
Definition: console_board.c:65
int cur_line
Definition: console_board.c:71
static void render_board(struct board *b)
int v_h
Definition: console_board.c:63
int print_message ( struct board b,
const char *  s 
)

Definition at line 231 of file console_board.c.

References ast_strlen_zero(), board::cur_col, render_board(), board::text, board::v_h, and board::v_w.

Referenced by handle_keyboard_input(), handle_mousedown(), keypad_digit(), keypad_pick_up(), and update_device_info().

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 }
int cur_col
Definition: console_board.c:70
char * text
Definition: console_board.c:77
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static void render_board(struct board *b)
int v_h
Definition: console_board.c:63
int v_w
Definition: console_board.c:64
const char* read_message ( const struct board b)

return the whole text from a board

Definition at line 214 of file console_board.c.

References board::text.

Referenced by keypad_pick_up().

215 {
216  return b->text;
217 }
char * text
Definition: console_board.c:77
static void render_board ( struct board b)
static

Definition at line 166 of file console_board.c.

References board::blank, board::cur_line, board::font, FONT_H, board::font_rects, FONT_W, board::p_h, board::p_rect, board::screen, board::text, board::v_h, and board::v_w.

Referenced by move_message_board(), print_message(), and reset_board().

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 }
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
SDL_Surface * blank
Definition: console_board.c:61
SDL_Surface * font
Definition: console_board.c:75
int cur_line
Definition: console_board.c:71
char * text
Definition: console_board.c:77
#define FONT_H
Definition: console_board.c:53
#define FONT_W
Definition: console_board.c:54
int v_h
Definition: console_board.c:63
SDL_Surface * screen
Definition: console_board.c:59
int v_w
Definition: console_board.c:64
int reset_board ( struct board b)

reset the board to blank

Definition at line 219 of file console_board.c.

References board::cur_col, board::cur_line, render_board(), board::text, board::v_h, and board::v_w.

Referenced by keypad_pick_up(), and update_device_info().

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 }
int cur_col
Definition: console_board.c:70
int cur_line
Definition: console_board.c:71
char * text
Definition: console_board.c:77
static void render_board(struct board *b)
int v_h
Definition: console_board.c:63
int v_w
Definition: console_board.c:64