| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#include "asterisk.h" |
| 45 |
#include "asterisk/utils.h" |
| 46 |
#include "console_video.h" |
| 47 |
|
| 48 |
#ifdef HAVE_SDL |
| 49 |
#include <SDL/SDL.h> |
| 50 |
|
| 51 |
|
| 52 |
#define FONT_H 20 |
| 53 |
#define FONT_W 9 |
| 54 |
|
| 55 |
struct board { |
| 56 |
int kb_output; |
| 57 |
|
| 58 |
SDL_Surface *screen; |
| 59 |
SDL_Rect *p_rect; |
| 60 |
SDL_Surface *blank; |
| 61 |
|
| 62 |
int v_h; |
| 63 |
int v_w; |
| 64 |
int p_h; |
| 65 |
|
| 66 |
int p_w; |
| 67 |
|
| 68 |
|
| 69 |
int cur_col; |
| 70 |
int cur_line; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
SDL_Surface *font; |
| 75 |
SDL_Rect *font_rects; |
| 76 |
char *text; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest, |
| 93 |
SDL_Surface *font, SDL_Rect *font_rects); |
| 94 |
struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest, |
| 95 |
SDL_Surface *font, SDL_Rect *font_rects) |
| 96 |
{ |
| 97 |
struct board *b = ast_calloc(1, sizeof (*b)); |
| 98 |
SDL_Rect br; |
| 99 |
|
| 100 |
if (b == NULL) |
| 101 |
return NULL; |
| 102 |
|
| 103 |
b->font = font; |
| 104 |
b->font_rects = font_rects; |
| 105 |
|
| 106 |
|
| 107 |
b->p_rect = dest; |
| 108 |
b->screen = screen; |
| 109 |
|
| 110 |
|
| 111 |
b->p_h = b->p_rect->h/FONT_H; |
| 112 |
b->p_w = b->p_rect->w/FONT_W; |
| 113 |
|
| 114 |
|
| 115 |
b->v_h = b->p_h * 10; |
| 116 |
b->v_w = b->p_w; |
| 117 |
|
| 118 |
|
| 119 |
br.h = b->p_h * FONT_H; |
| 120 |
br.w = b->p_w * FONT_W; |
| 121 |
br.x = br.y = 0; |
| 122 |
|
| 123 |
|
| 124 |
b->text = ast_calloc(b->v_w*b->v_h + 1, 1); |
| 125 |
if (b->text == NULL) { |
| 126 |
ast_log(LOG_WARNING, "Unable to allocate board history memory.\n"); |
| 127 |
ast_free(b); |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
memset(b->text, ' ', b->v_w * b->v_h); |
| 131 |
|
| 132 |
|
| 133 |
b->blank = SDL_CreateRGBSurface(screen->flags, br.w, br.h, |
| 134 |
screen->format->BitsPerPixel, |
| 135 |
screen->format->Rmask, screen->format->Gmask, |
| 136 |
screen->format->Bmask, screen->format->Amask); |
| 137 |
|
| 138 |
if (b->blank == NULL) { |
| 139 |
ast_log(LOG_WARNING, "Unable to allocate board virtual screen: %s\n", |
| 140 |
SDL_GetError()); |
| 141 |
ast_free(b->text); |
| 142 |
ast_free(b); |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
SDL_BlitSurface(screen, b->p_rect, b->blank, &br); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
b->cur_col = 0; |
| 152 |
b->cur_line = 0; |
| 153 |
|
| 154 |
if (0) ast_log(LOG_WARNING, "Message board %dx%d@%d,%d successfully initialized\n", |
| 155 |
b->p_rect->w, b->p_rect->h, |
| 156 |
b->p_rect->x, b->p_rect->y); |
| 157 |
return b; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
static void render_board(struct board *b) |
| 166 |
{ |
| 167 |
int first_row = b->v_h - b->p_h - b->cur_line; |
| 168 |
int first_char = b->v_w * first_row; |
| 169 |
int last_char = first_char + b->p_h * b->v_w; |
| 170 |
int i, col; |
| 171 |
SDL_Rect dst; |
| 172 |
|
| 173 |
|
| 174 |
dst.w = FONT_W; |
| 175 |
dst.h = FONT_H; |
| 176 |
dst.x = b->p_rect->x; |
| 177 |
dst.y = b->p_rect->y; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
SDL_BlitSurface(b->blank, NULL, b->screen, b->p_rect); |
| 182 |
|
| 183 |
|
| 184 |
for (i = first_char, col = 0; i < last_char; i++) { |
| 185 |
int c = b->text[i] - 32; |
| 186 |
if (c < 0) |
| 187 |
c = 0; |
| 188 |
SDL_BlitSurface(b->font, &b->font_rects[c], b->screen, &dst); |
| 189 |
|
| 190 |
dst.x += dst.w; |
| 191 |
col++; |
| 192 |
if (col >= b->v_w) { |
| 193 |
dst.x = b->p_rect->x; |
| 194 |
dst.y += dst.h; |
| 195 |
col = 0; |
| 196 |
} |
| 197 |
} |
| 198 |
SDL_UpdateRects(b->screen, 1, b->p_rect); |
| 199 |
} |
| 200 |
|
| 201 |
void move_message_board(struct board *b, int dy) |
| 202 |
{ |
| 203 |
int cur = b->cur_line + dy; |
| 204 |
if (cur < 0) |
| 205 |
cur = 0; |
| 206 |
else if (cur >= b->v_h - b->p_h) |
| 207 |
cur = b->v_h - b->p_h - 1; |
| 208 |
b->cur_line = cur; |
| 209 |
render_board(b); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
const char *read_message(const struct board *b) |
| 214 |
{ |
| 215 |
return b->text; |
| 216 |
} |
| 217 |
|
| 218 |
int reset_board(struct board *b) |
| 219 |
{ |
| 220 |
memset(b->text, ' ', b->v_w * b->v_h); |
| 221 |
b->cur_col = 0; |
| 222 |
b->cur_line = 0; |
| 223 |
render_board(b); |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
int print_message(struct board *b, const char *s) |
| 231 |
{ |
| 232 |
int i, l, row, col; |
| 233 |
char *dst; |
| 234 |
|
| 235 |
if (ast_strlen_zero(s)) |
| 236 |
return 0; |
| 237 |
|
| 238 |
l = strlen(s); |
| 239 |
row = 0; |
| 240 |
col = b->cur_col; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
for (i = 0; i < l; i++) { |
| 249 |
switch (s[i]) { |
| 250 |
case '\r': |
| 251 |
col = 0; |
| 252 |
break; |
| 253 |
case '\n': |
| 254 |
col = 0; |
| 255 |
row++; |
| 256 |
break; |
| 257 |
case '\b': |
| 258 |
if (col > 0) |
| 259 |
col--; |
| 260 |
break; |
| 261 |
default: |
| 262 |
if (s[i] < 32) |
| 263 |
break; |
| 264 |
col++; |
| 265 |
if (col >= b->v_w) { |
| 266 |
col -= b->v_w; |
| 267 |
row++; |
| 268 |
} |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if (row > 0) { |
| 274 |
memcpy(b->text, b->text + row * b->v_w, b->v_w * (b->v_h - row)); |
| 275 |
|
| 276 |
dst = b->text + b->v_w * (b->v_h - row - 1) + b->cur_col; |
| 277 |
memset(dst, ' ', b->v_w - b->cur_col + b->v_w * row); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
dst = b->text + b->v_w * (b->v_h - row - 1); |
| 284 |
col = b->cur_col; |
| 285 |
for (i = 0; i < l; i++) { |
| 286 |
switch (s[i]) { |
| 287 |
case '\r': |
| 288 |
col = 0; |
| 289 |
break; |
| 290 |
case '\n': |
| 291 |
dst[col] = '\0'; |
| 292 |
col = 0; |
| 293 |
dst += b->v_w; |
| 294 |
break; |
| 295 |
case '\b': |
| 296 |
if (col > 0) |
| 297 |
col--; |
| 298 |
dst[col] = ' '; |
| 299 |
break; |
| 300 |
default: |
| 301 |
if (s[i] < 32) |
| 302 |
break; |
| 303 |
dst[col] = s[i]; |
| 304 |
col++; |
| 305 |
if (col >= b->v_w) { |
| 306 |
col -= b->v_w; |
| 307 |
dst += b->v_w; |
| 308 |
} |
| 309 |
break; |
| 310 |
} |
| 311 |
} |
| 312 |
dst[col] = '\0'; |
| 313 |
b->cur_col = col; |
| 314 |
|
| 315 |
render_board(b); |
| 316 |
return 1; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
void delete_board(struct board *b) |
| 324 |
{ |
| 325 |
if (b) { |
| 326 |
|
| 327 |
if (b->text) |
| 328 |
ast_free (b->text); |
| 329 |
|
| 330 |
SDL_FreeSurface(b->blank); |
| 331 |
|
| 332 |
ast_free(b); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
#if 0 |
| 337 |
|
| 338 |
|
| 339 |
static int scroll_message(...) |
| 340 |
{ |
| 341 |
if moving up, scroll text up; |
| 342 |
if (gui->message_board.screen_cur > 0) |
| 343 |
gui->message_board.screen_cur--; |
| 344 |
otherwise scroll text down. |
| 345 |
if ((b->screen_cur + b->p_line) < b->board_next) { |
| 346 |
gui->message_board.screen_cur++; |
| 347 |
#endif |
| 348 |
|
| 349 |
#endif |