add support for textareas, used for various dialog windows on the gui.

The main code to implement the textarea is in console_board.c,
and uses a simple png image with the font, blitting characters
on the designated areas of the main screen.
Additionally we provide some annotations in the image used
as a skin to indicate which areas are used for text messages.
(images will be committed separately).
At the moment the dialog area is only used to display a running
counter, just as a proof of concept.



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@97280 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Luigi Rizzo 2008-01-08 23:09:44 +00:00
parent 12964661f2
commit 11c492aaec
4 changed files with 402 additions and 58 deletions

View File

@ -102,4 +102,4 @@ $(if $(filter chan_misdn,$(EMBEDDED_MODS)),modules.link,chan_misdn.so): misdn_co
chan_vpb.oo: ASTCFLAGS:=$(filter-out -Wdeclaration-after-statement,$(ASTCFLAGS))
$(if $(filter chan_oss,$(EMBEDDED_MODS)),modules.link,chan_oss.so): console_video.o vgrabbers.o
$(if $(filter chan_oss,$(EMBEDDED_MODS)),modules.link,chan_oss.so): console_video.o vgrabbers.o console_board.o

316
channels/console_board.c Normal file
View File

@ -0,0 +1,316 @@
/*
* Message board implementation.
*
* A message board is a section of an sdl screen where
* messages can be printed, like on a terminal window.
* The text is stored in a buffer
* of fixed size (rows and cols). A portion of the buffer is
* visible on the screen, and the visible window can be moved up and
* down by dragging.
*
* TODO: font dynamic allocation
*
* OLD: The physical section displayed on the screen is defined
* as keypad element, (the name is defined in the `region' variable
* so the board geometry can be read from the skin or from the
* configuration file.
*
* OLD: To define a message board:
* - declare a board in the gui_info structure;
* - define a region name in the keypad skin and update
* the gui_key_map list;
* - add and manage focus events on its.
*
*/
#include "asterisk.h" /* ast_strdupa */
#include "asterisk/utils.h" /* ast_strdupa */
#ifndef HAVE_SDL
/* nothing */
#else
#include <SDL/SDL.h>
#define GUI_BUFFER_LEN 256 /* buffer lenght used for input buffers */
/* Fonts characterization, TODO, read from file */
#define FONT_H 20 /* char height, pixels */
#define FONT_W 9 /* char width, pixels */
struct board {
int kb_output; /* identity of the board */
/* pointer to the destination surface (on the keypad window) */
SDL_Surface *screen; /* the main screen */
SDL_Rect *p_rect; /* where to write on the main screen */
SDL_Surface *blank; /* original content of the window */
int v_h; /* virtual text height, in lines */
int v_w; /* virtual text width, in lines (probably same as p_w) */
int p_h; /* physical (displayed) text height, in lines
* XXX p_h * FONT_H = pixel_height */
int p_w; /* physical (displayed) text width, in characters
* XXX p_w * FONT_W = pixel_width */
int cur_col; /* print position (free character) on the last line */
int cur_line; /* first (or last ?) virtual line displayed,
* 0 is the line at the bottom, 1 is the one above,...
*/
SDL_Surface *font; /* points to a surface in the gui structure */
SDL_Rect *font_rects; /* pointer to the font rects */
char *text;
/* text buffer, v_h * v_w char.
* We make sure the buffer is always full,
* print on some position on the last line,
* and scroll up when appending new text
*/
};
/*! \brief 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
*/
struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
SDL_Surface *font, SDL_Rect *font_rects);
struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
SDL_Surface *font, SDL_Rect *font_rects)
{
struct board *b = ast_calloc(1, sizeof (*b));
SDL_Rect br;
if (b == NULL)
return NULL;
/* font, points to the gui structure */
b->font = font;
b->font_rects = font_rects;
/* Destination rectangle on the screen - reference is the whole screen */
b->p_rect = dest;
b->screen = screen;
/* compute physical sizes */
b->p_h = b->p_rect->h/FONT_H;
b->p_w = b->p_rect->w/FONT_W;
/* virtual sizes */
b->v_h = b->p_h * 10; /* XXX 10 times larger */
b->v_w = b->p_w; /* same width */
br.h = b->p_h * FONT_H; /* pixel sizes of the background */
br.w = b->p_w * FONT_W;
br.x = br.y = 0;
b->text = ast_calloc(b->v_w*b->v_h + 1, 1);
if (b->text == NULL) {
ast_log(LOG_WARNING, "Unable to allocate board history memory.\n");
ast_free(b);
return NULL;
}
memset(b->text, ' ', b->v_w * b->v_h); /* fill with spaces */
/* XXX make a copy of the original, for cleaning up */
b->blank = SDL_CreateRGBSurface(screen->flags, br.w, br.h,
screen->format->BitsPerPixel,
screen->format->Rmask, screen->format->Gmask,
screen->format->Bmask, screen->format->Amask);
if (b->blank == NULL) {
ast_log(LOG_WARNING, "Unable to allocate board virtual screen: %s\n",
SDL_GetError());
ast_free(b->text);
ast_free(b);
return NULL;
}
SDL_BlitSurface(screen, b->p_rect, b->blank, &br);
/* Set color key, if not alpha channel present */
//colorkey = SDL_MapRGB(b->board_surface->format, 0, 0, 0);
//SDL_SetColorKey(b->board_surface, SDL_SRCCOLORKEY, colorkey);
b->cur_col = 0; /* current print column */
b->cur_line = 0; /* last line displayed */
ast_log(LOG_WARNING, "Message board %dx%d@%d,%d successfully initialized\n",
b->p_rect->w, b->p_rect->h,
b->p_rect->x, b->p_rect->y);
return b;
}
#if 0
/*! \brief Remap and blit the virtual surface on the physical surface */
static void blit_on_screen(struct gui_info *gui, struct board *b)
{
/* Blit a section of the virtual board on the main surface */
SDL_Rect mapped_rect; /* coordinates related to the main surface */
mapped_rect.x = 0;
mapped_rect.y = b->rendering_offset;
mapped_rect.w = b->p_rect.w;
mapped_rect.h = b->p_rect.h;
/* Clean the surface (print backgroud) */
// This sould be done in the main loop
// SDL_BlitSurface(gui->keypad, NULL, gui->screen, &b->p_rect);
SDL_BlitSurface(gui->keypad, NULL, gui->screen, &b->p_rect);
/* Blit the virtual surface on the main surface */
SDL_BlitSurface(b->v_board, &mapped_rect, gui->screen, &b->p_rect);
/* Update the keypad screen, should be done in the main loop */
SDL_UpdateRects(gui->screen, 1, &gui->message_board.p_rect);
//SDL_UpdateRects(gui->screen, 1, &gui->message_board.p_rect);
}
#endif /* notyet */
/* Render the text on the board surface.
* The first line to render is the one at v_h - p_h - cur_line,
* the size is p_h * p_w.
* XXX we assume here that p_w = v_w.
*/
static void render_board(struct board *b)
{
int first_row = b->v_h - b->p_h - b->cur_line;
int first_char = b->v_w * first_row;
int last_char = first_char + b->p_h * b->v_w;
int i, col;
SDL_Rect dst;
/* top left char on the physical surface */
dst.w = FONT_W;
dst.h = FONT_H;
dst.x = b->p_rect->x;
dst.y = b->p_rect->y;
/* clean the surface board */
SDL_BlitSurface(b->blank, NULL, b->screen, b->p_rect);
/* blit all characters */
for (i = first_char, col = 0; i < last_char; i++) {
int c = b->text[i] - 32;
SDL_BlitSurface(b->font, &b->font_rects[c], b->screen, &dst);
/* point dst to next char position */
dst.x += dst.w;
col++;
if (col >= b->v_w) { /* next row */
dst.x = b->p_rect->x;
dst.y += dst.h;
col = 0;
}
}
/* Update the written portion of the keypad on the screen */
SDL_UpdateRects(b->screen, 1, b->p_rect);
}
/* Store the message on the history board
* and blit on screen if required.
* XXX now easy. only regular chars
*/
int print_message(struct board *b, const char *s);
int print_message(struct board *b, const char *s)
{
int i, l, row, col;
char *dst;
if (ast_strlen_zero(s))
return 0;
l = strlen(s);
row = 0;
col = b->cur_col;
/* First, only check how much space we need.
* Starting from the current print position, we move
* it forward and down (if necessary) according to input
* characters (including newlines, tabs, backspaces...).
* At the end, row tells us how many rows to scroll, and
* col (ignored) is the final print position.
*/
for (i = 0; i < l; i++) {
switch (s[i]) {
case '\r':
col = 0;
break;
case '\n':
col = 0;
row++;
break;
case '\b':
if (col > 0)
col--;
break;
default:
if (s[i] < 32) /* signed, so take up to 127 */
break;
col++;
if (col >= b->v_w) {
col -= b->v_w;
row++;
}
break;
}
}
/* scroll the text window */
if (row > 0) { /* need to scroll by 'row' rows */
memcpy(b->text, b->text + row * b->v_w, b->v_w * (b->v_h - row));
/* clean the destination area */
dst = b->text + b->v_w * (b->v_h - row - 1) + b->cur_col;
memset(dst, ' ', b->v_w - b->cur_col + b->v_w * row);
}
/* now do the actual printing. The print position is 'row' lines up
* from the bottom of the buffer, start at the same 'cur_col' as before.
* dst points to the beginning of the current line.
*/
dst = b->text + b->v_w * (b->v_h - row - 1); /* start of current line */
col = b->cur_col;
for (i = 0; i < l; i++) {
switch (s[i]) {
case '\r':
col = 0;
break;
case '\n': /* move to beginning of next line */
col = 0;
dst += b->v_w;
break;
case '\b': /* one char back */
if (col > 0)
col--;
break;
default:
if (s[i] < 32) /* signed, so take up to 127 */
break; /* non printable */
dst[col] = s[i]; /* store character */
col++;
if (col >= b->v_w) {
col -= b->v_w;
dst += b->v_w;
}
break;
}
}
b->cur_col = col;
/* everything is printed now, must do the rendering */
//board_dump(b);
render_board(b);
return 1;
}
#if 0
/*! \brief refresh the screen, and also grab a bunch of events.
*/
static int scroll_message(...)
{
if moving up, scroll text up;
if (gui->message_board.screen_cur > 0)
gui->message_board.screen_cur--;
otherwise scroll text down.
if ((b->screen_cur + b->p_line) < b->board_next) {
gui->message_board.screen_cur++;
#endif /* notyet */
#endif /* HAVE_SDL */

View File

@ -58,9 +58,6 @@ static int keypad_cfg_read(struct gui_info *gui, const char *val) { return 0; }
#ifdef HAVE_SDL_IMAGE
#include <SDL/SDL_image.h> /* for loading images */
#endif
#ifdef HAVE_SDL_TTF
#include <SDL/SDL_ttf.h> /* render text on sdl surfaces */
#endif
enum kp_type { KP_NONE, KP_RECT, KP_CIRCLE };
struct keypad_entry {
@ -94,6 +91,8 @@ enum drag_window { /* which window are we dragging */
DRAG_MESSAGE, /* message window */
};
struct board; /* external. we only need the pointer */
/*! \brief info related to the gui: button status, mouse coords, etc. */
struct gui_info {
char inbuf[GUI_BUFFER_LEN]; /* buffer for to-dial buffer */
@ -104,16 +103,26 @@ struct gui_info {
enum drag_window drag_window; /* which window are we dragging */
int x_drag; /* x coordinate where the drag starts */
int y_drag; /* y coordinate where the drag starts */
#ifdef HAVE_SDL_TTF
TTF_Font *font; /* font to be used */
#endif
/* support for display. */
SDL_Surface *screen; /* the main window */
int outfd; /* fd for output */
SDL_Surface *keypad; /* the skin for the keypad */
SDL_Rect kp_rect; /* portion of the skin to display - default all */
SDL_Surface *font; /* font to be used */
SDL_Rect font_rects[96]; /* only printable chars */
SDL_Rect kp_msg; /* incoming msg, relative to kpad */
SDL_Rect kp_msg_s; /* incoming msg, rel. to screen */
struct board *bd_msg;
SDL_Rect kp_edit; /* edit user input */
SDL_Rect kp_edit_s; /* incoming msg, rel. to screen */
struct board *bd_edit;
SDL_Rect kp_dialed; /* dialed number */
SDL_Rect kp_dialed_s; /* incoming msg, rel. to screen */
struct board *bd_dialed;
/* variable-size array mapping keypad regions to functions */
int kp_size, kp_used;
@ -132,17 +141,12 @@ static struct gui_info *cleanup_sdl(struct gui_info *gui)
if (gui == NULL)
return NULL;
#ifdef HAVE_SDL_TTF
/* unload font file */
if (gui->font) {
TTF_CloseFont(gui->font);
SDL_FreeSurface(gui->font);
gui->font = NULL;
}
/* uninitialize SDL_ttf library */
if ( TTF_WasInit() )
TTF_Quit();
#endif
if (gui->outfd > -1)
close(gui->outfd);
if (gui->keypad)
@ -240,7 +244,10 @@ enum skin_area {
/* regions of the skin - active area, fonts, etc. */
KEY_KEYPAD = 200, /* the keypad - default to the whole image */
KEY_FONT = 201, /* the font */
KEY_FONT = 201, /* the font. Maybe not really useful */
KEY_MESSAGE = 202, /* area for incoming messages */
KEY_DIALED = 203, /* area for dialed numbers */
KEY_EDIT = 204, /* area for editing user input */
/* areas outside the keypad - simulated */
KEY_OUT_OF_KEYPAD = 241,
@ -361,30 +368,7 @@ end
/* Print given text on the gui */
static int gui_output(struct video_desc *env, const char *text)
{
#ifndef HAVE_SDL_TTF
return 1; /* error, not supported */
#else
int x = 30, y = 20; /* XXX change */
SDL_Surface *output = NULL;
SDL_Color color = {0, 0, 0}; /* text color */
struct gui_info *gui = env->gui;
SDL_Rect dest = {gui->win[WIN_KEYPAD].rect.x + x, y};
/* clean surface each rewrite */
SDL_BlitSurface(gui->keypad, NULL, gui->screen, &gui->win[WIN_KEYPAD].rect);
output = TTF_RenderText_Solid(gui->font, text, color);
if (output == NULL) {
ast_log(LOG_WARNING, "Cannot render text on gui - %s\n", TTF_GetError());
return 1;
}
SDL_BlitSurface(output, NULL, gui->screen, &dest);
SDL_UpdateRects(gui->keypad, 1, &gui->win[WIN_KEYPAD].rect);
SDL_FreeSurface(output);
return 0; /* success */
#endif
}
#endif
@ -585,6 +569,10 @@ static void eventhandler(struct video_desc *env, const char *caption)
case SDL_MOUSEMOTION:
if (gui->drag_window == DRAG_LOCAL)
move_capture_source(env, ev[i].motion.x, ev[i].motion.y);
#if 0
else if (gui->drag_window == DRAG_MESSAGE)
scroll_message(...);
#endif
break;
case SDL_MOUSEBUTTONDOWN:
handle_mousedown(env, ev[i].button);
@ -631,7 +619,7 @@ static void keypad_setup(struct gui_info *gui, const char *kp_file);
/* TODO: consistency checks, check for bpp, widht and height */
/* Init the mask image used to grab the action. */
static struct gui_info *gui_init(const char *keypad_file)
static struct gui_info *gui_init(const char *keypad_file, const char *font)
{
struct gui_info *gui = ast_calloc(1, sizeof(*gui));
@ -650,21 +638,28 @@ static struct gui_info *gui_init(const char *keypad_file)
keypad_setup(gui, keypad_file);
if (gui->keypad == NULL) /* no keypad, we are done */
return gui;
#ifdef HAVE_SDL_TTF
/* Initialize SDL_ttf library and load font */
if (TTF_Init() == -1) {
ast_log(LOG_WARNING, "Unable to init SDL_ttf, no output available\n");
goto error;
}
/* XXX load image */
if (!ast_strlen_zero(font)) {
int i;
SDL_Rect *r;
#define GUI_FONTSIZE 28
gui->font = TTF_OpenFont( env->keypad_font, GUI_FONTSIZE);
if (!gui->font) {
ast_log(LOG_WARNING, "Unable to load font %s, no output available\n", env->keypad_font);
goto error;
gui->font = load_image(font);
if (!gui->font) {
ast_log(LOG_WARNING, "Unable to load font %s, no output available\n", font);
goto error;
}
ast_log(LOG_WARNING, "Loaded font %s\n", font);
/* XXX hardwired constants - 3 rows of 32 chars */
r = gui->font_rects;
#define FONT_H 20
#define FONT_W 9
for (i = 0; i < 96; r++, i++) {
r->x = (i % 32 ) * FONT_W;
r->y = (i / 32 ) * FONT_H;
r->w = FONT_W;
r->h = FONT_H;
}
}
ast_log(LOG_WARNING, "Loaded font %s\n", env->keypad_font);
#endif
gui->outfd = open ("/dev/null", O_WRONLY); /* discard output, temporary */
if (gui->outfd < 0) {
@ -749,6 +744,9 @@ static void keypad_setup(struct gui_info *gui, const char *kp_file)
fclose(fd);
}
struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
SDL_Surface *font, SDL_Rect *font_rects);
/*! \brief [re]set the main sdl window, useful in case of resize.
* We can tell the first from subsequent calls from the value of
* env->gui, which is NULL the first time.
@ -786,7 +784,7 @@ static void sdl_setup(struct video_desc *env)
if (depth < 16)
depth = 16;
if (!gui)
env->gui = gui = gui_init(env->keypad_file);
env->gui = gui = gui_init(env->keypad_file, env->keypad_font);
if (!gui)
goto no_sdl;
@ -799,6 +797,7 @@ static void sdl_setup(struct video_desc *env)
kp_h = gui->keypad->h;
}
}
/* XXX same for other boards */
#define BORDER 5 /* border around our windows */
maxw = env->rem_dpy.w + env->loc_dpy.w + kp_w;
maxh = MAX( MAX(env->rem_dpy.h, env->loc_dpy.h), kp_h);
@ -830,6 +829,17 @@ static void sdl_setup(struct video_desc *env)
dest->w = kp_w;
dest->h = kp_h;
SDL_BlitSurface(gui->keypad, src, gui->screen, dest);
if (gui->kp_msg.w > 0 && gui->kp_msg.h > 0) {
gui->kp_msg_s = gui->kp_msg;
gui->kp_msg_s.x += dest->x;
gui->kp_msg_s.y += dest->y;
if (!gui->bd_msg) { /* initial call */
gui->bd_msg = board_setup(gui->screen, &gui->kp_msg_s,
gui->font, gui->font_rects);
} else {
/* call a refresh */
}
}
SDL_UpdateRects(gui->screen, 1, dest);
}
return;
@ -889,6 +899,9 @@ static struct _s_k gui_key_map[] = {
{"WRITEMESSAGE", KEY_WRITEMESSAGE },
{"GUI_CLOSE", KEY_GUI_CLOSE },
{"KEYPAD", KEY_KEYPAD }, /* x0 y0 w h - active area of the keypad */
{"MESSAGE", KEY_MESSAGE }, /* x0 y0 w h - incoming messages */
{"DIALED", KEY_DIALED }, /* x0 y0 w h - dialed number */
{"EDIT", KEY_EDIT }, /* x0 y0 w h - edit user input */
{"FONT", KEY_FONT }, /* x0 yo w h rows cols - location and format of the font */
{NULL, 0 } };
@ -918,6 +931,7 @@ static int gui_map_token(const char *s)
static int keypad_cfg_read(struct gui_info *gui, const char *val)
{
struct keypad_entry e;
SDL_Rect *r = NULL;
char s1[16], s2[16];
int i, ret = 0; /* default, error */
@ -942,11 +956,19 @@ static int keypad_cfg_read(struct gui_info *gui, const char *val)
gui->kp_used = 0;
break;
case 5:
if (e.c == KEY_KEYPAD) { /* active keypad area */
gui->kp_rect.x = atoi(s2);
gui->kp_rect.y = e.x0;
gui->kp_rect.w = e.y0;
gui->kp_rect.h = e.x1;
if (e.c == KEY_KEYPAD) /* active keypad area */
r = &gui->kp_rect;
else if (e.c == KEY_MESSAGE)
r = &gui->kp_msg;
else if (e.c == KEY_DIALED)
r = &gui->kp_dialed;
else if (e.c == KEY_EDIT)
r = &gui->kp_edit;
if (r) {
r->x = atoi(s2);
r->y = e.x0;
r->w = e.y0;
r->h = e.x1;
break;
}
if (strcasecmp(s2, "circle")) /* invalid */

View File

@ -659,6 +659,8 @@ static struct ast_frame *get_video_frames(struct video_desc *env, struct ast_fra
return v->enc->enc_encap(&v->enc_out, v->mtu, tail);
}
int print_message(struct board *b, const char *s);
/*
* Helper thread to periodically poll the video source and enqueue the
* generated frames to the channel's queue.
@ -715,6 +717,10 @@ static void *video_thread(void *arg)
int fd = chan->alertpipe[1];
char *caption = NULL, buf[160];
sprintf(buf, "%d \r", count);
if (env->gui)
print_message(env->gui->bd_msg, buf);
/* determine if video format changed */
if (count++ % 10 == 0) {
if (env->out.sendvideo)