#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 |
Defines | |
#define | FONT_H 20 |
#define | FONT_W 9 |
Functions | |
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. | |
void | delete_board (struct board *b) |
deallocates memory space for a board | |
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 | |
static void | render_board (struct board *b) |
int | reset_board (struct board *b) |
reset the board to blank |
#define FONT_H 20 |
Definition at line 49 of file console_board.c.
Referenced by board_setup(), gui_init(), and render_board().
#define FONT_W 9 |
Definition at line 50 of file console_board.c.
Referenced by board_setup(), gui_init(), and render_board().
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 91 of file console_board.c.
References ast_calloc, ast_free, ast_log(), FONT_H, FONT_W, and LOG_WARNING.
Referenced by init_board(), and sdl_setup().
00093 { 00094 struct board *b = ast_calloc(1, sizeof (*b)); 00095 SDL_Rect br; 00096 00097 if (b == NULL) 00098 return NULL; 00099 /* font, points to the gui structure */ 00100 b->font = font; 00101 b->font_rects = font_rects; 00102 00103 /* Destination rectangle on the screen - reference is the whole screen */ 00104 b->p_rect = dest; 00105 b->screen = screen; 00106 00107 /* compute physical sizes */ 00108 b->p_h = b->p_rect->h/FONT_H; 00109 b->p_w = b->p_rect->w/FONT_W; 00110 00111 /* virtual sizes */ 00112 b->v_h = b->p_h * 10; /* XXX 10 times larger */ 00113 b->v_w = b->p_w; /* same width */ 00114 00115 /* the rectangle we actually use */ 00116 br.h = b->p_h * FONT_H; /* pixel sizes of the background */ 00117 br.w = b->p_w * FONT_W; 00118 br.x = br.y = 0; 00119 00120 /* allocate a buffer for the text */ 00121 b->text = ast_calloc(b->v_w*b->v_h + 1, 1); 00122 if (b->text == NULL) { 00123 ast_log(LOG_WARNING, "Unable to allocate board history memory.\n"); 00124 ast_free(b); 00125 return NULL; 00126 } 00127 memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */ 00128 00129 /* make a copy of the original rectangle, for cleaning up */ 00130 b->blank = SDL_CreateRGBSurface(screen->flags, br.w, br.h, 00131 screen->format->BitsPerPixel, 00132 screen->format->Rmask, screen->format->Gmask, 00133 screen->format->Bmask, screen->format->Amask); 00134 00135 if (b->blank == NULL) { 00136 ast_log(LOG_WARNING, "Unable to allocate board virtual screen: %s\n", 00137 SDL_GetError()); 00138 ast_free(b->text); 00139 ast_free(b); 00140 return NULL; 00141 } 00142 SDL_BlitSurface(screen, b->p_rect, b->blank, &br); 00143 00144 /* Set color key, if not alpha channel present */ 00145 //colorkey = SDL_MapRGB(b->board_surface->format, 0, 0, 0); 00146 //SDL_SetColorKey(b->board_surface, SDL_SRCCOLORKEY, colorkey); 00147 00148 b->cur_col = 0; /* current print column */ 00149 b->cur_line = 0; /* last line displayed */ 00150 00151 if (0) ast_log(LOG_WARNING, "Message board %dx%d@%d,%d successfully initialized\n", 00152 b->p_rect->w, b->p_rect->h, 00153 b->p_rect->x, b->p_rect->y); 00154 return b; 00155 }
void delete_board | ( | struct board * | b | ) |
deallocates memory space for a board
Definition at line 320 of file console_board.c.
References ast_free, board::blank, and board::text.
Referenced by cleanup_sdl().
00321 { 00322 if (b) { 00323 /* deletes the text */ 00324 if (b->text) 00325 ast_free (b->text); 00326 /* deallocates the blank surface */ 00327 SDL_FreeSurface(b->blank); 00328 /* deallocates the board */ 00329 ast_free(b); 00330 } 00331 }
void move_message_board | ( | struct board * | b, | |
int | dy | |||
) |
Definition at line 198 of file console_board.c.
References board::cur_line, board::p_h, render_board(), and board::v_h.
Referenced by eventhandler().
00199 { 00200 int cur = b->cur_line + dy; 00201 if (cur < 0) 00202 cur = 0; 00203 else if (cur >= b->v_h - b->p_h) 00204 cur = b->v_h - b->p_h - 1; 00205 b->cur_line = cur; 00206 render_board(b); 00207 }
int print_message | ( | struct board * | b, | |
const char * | s | |||
) |
Definition at line 227 of file console_board.c.
References ast_strlen_zero(), board::cur_col, and board::v_w.
Referenced by handle_keyboard_input(), keypad_digit(), keypad_pick_up(), and update_device_info().
00228 { 00229 int i, l, row, col; 00230 char *dst; 00231 00232 if (ast_strlen_zero(s)) 00233 return 0; 00234 00235 l = strlen(s); 00236 row = 0; 00237 col = b->cur_col; 00238 /* First, only check how much space we need. 00239 * Starting from the current print position, we move 00240 * it forward and down (if necessary) according to input 00241 * characters (including newlines, tabs, backspaces...). 00242 * At the end, row tells us how many rows to scroll, and 00243 * col (ignored) is the final print position. 00244 */ 00245 for (i = 0; i < l; i++) { 00246 switch (s[i]) { 00247 case '\r': 00248 col = 0; 00249 break; 00250 case '\n': 00251 col = 0; 00252 row++; 00253 break; 00254 case '\b': 00255 if (col > 0) 00256 col--; 00257 break; 00258 default: 00259 if (s[i] < 32) /* signed, so take up to 127 */ 00260 break; 00261 col++; 00262 if (col >= b->v_w) { 00263 col -= b->v_w; 00264 row++; 00265 } 00266 break; 00267 } 00268 } 00269 /* scroll the text window */ 00270 if (row > 0) { /* need to scroll by 'row' rows */ 00271 memcpy(b->text, b->text + row * b->v_w, b->v_w * (b->v_h - row)); 00272 /* clean the destination area */ 00273 dst = b->text + b->v_w * (b->v_h - row - 1) + b->cur_col; 00274 memset(dst, ' ', b->v_w - b->cur_col + b->v_w * row); 00275 } 00276 /* now do the actual printing. The print position is 'row' lines up 00277 * from the bottom of the buffer, start at the same 'cur_col' as before. 00278 * dst points to the beginning of the current line. 00279 */ 00280 dst = b->text + b->v_w * (b->v_h - row - 1); /* start of current line */ 00281 col = b->cur_col; 00282 for (i = 0; i < l; i++) { 00283 switch (s[i]) { 00284 case '\r': 00285 col = 0; 00286 break; 00287 case '\n': /* move to beginning of next line */ 00288 dst[col] = '\0'; /* mark the rest of the line as empty */ 00289 col = 0; 00290 dst += b->v_w; 00291 break; 00292 case '\b': /* one char back */ 00293 if (col > 0) 00294 col--; 00295 dst[col] = ' '; /* delete current char */ 00296 break; 00297 default: 00298 if (s[i] < 32) /* signed, so take up to 127 */ 00299 break; /* non printable */ 00300 dst[col] = s[i]; /* store character */ 00301 col++; 00302 if (col >= b->v_w) { 00303 col -= b->v_w; 00304 dst += b->v_w; 00305 } 00306 break; 00307 } 00308 } 00309 dst[col] = '\0'; /* the current position is empty */ 00310 b->cur_col = col; 00311 /* everything is printed now, must do the rendering */ 00312 render_board(b); 00313 return 1; 00314 }
const char* read_message | ( | const struct board * | b | ) |
return the whole text from a board
Definition at line 210 of file console_board.c.
References board::text.
Referenced by keypad_pick_up().
00211 { 00212 return b->text; 00213 }
static void render_board | ( | struct board * | b | ) | [static] |
Definition at line 162 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(), and reset_board().
00163 { 00164 int first_row = b->v_h - b->p_h - b->cur_line; 00165 int first_char = b->v_w * first_row; 00166 int last_char = first_char + b->p_h * b->v_w; 00167 int i, col; 00168 SDL_Rect dst; 00169 00170 /* top left char on the physical surface */ 00171 dst.w = FONT_W; 00172 dst.h = FONT_H; 00173 dst.x = b->p_rect->x; 00174 dst.y = b->p_rect->y; 00175 00176 00177 /* clean the surface board */ 00178 SDL_BlitSurface(b->blank, NULL, b->screen, b->p_rect); 00179 00180 /* blit all characters */ 00181 for (i = first_char, col = 0; i < last_char; i++) { 00182 int c = b->text[i] - 32; /* XXX first 32 chars are not printable */ 00183 if (c < 0) /* buffer terminator or anything else is a blank */ 00184 c = 0; 00185 SDL_BlitSurface(b->font, &b->font_rects[c], b->screen, &dst); 00186 /* point dst to next char position */ 00187 dst.x += dst.w; 00188 col++; 00189 if (col >= b->v_w) { /* next row */ 00190 dst.x = b->p_rect->x; 00191 dst.y += dst.h; 00192 col = 0; 00193 } 00194 } 00195 SDL_UpdateRects(b->screen, 1, b->p_rect); /* Update the screen */ 00196 }
int reset_board | ( | struct board * | b | ) |
reset the board to blank
Definition at line 215 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().
00216 { 00217 memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */ 00218 b->cur_col = 0; 00219 b->cur_line = 0; 00220 render_board(b); 00221 return 0; 00222 }