Sat Mar 10 01:54:15 2012

Asterisk developer's documentation


console_board.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright 2007-2008, Marta Carbone, Luigi Rizzo
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  *
00016  * $Revision: 126480 $
00017  */
00018 
00019 /* 
00020  * Message board implementation.
00021  *
00022  * A message board is a region of the SDL screen where
00023  * messages can be printed, like on a terminal window.
00024  *
00025  * At the moment we support fix-size font.
00026  *
00027  * The text is stored in a buffer
00028  * of fixed size (rows and cols). A portion of the buffer is
00029  * visible on the screen, and the visible window can be moved up and
00030  * down by dragging (not yet!)
00031  * 
00032  * TODO: font dynamic allocation
00033  *
00034  * The region where the text is displayed on the screen is defined
00035  * as keypad element, (the name is defined in the `region' variable
00036  * so the board geometry can be read from the skin or from the
00037  * configuration file).
00038  */
00039 
00040 #include "asterisk.h"   /* ast_strdupa */
00041 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 126480 $")
00042 #include "asterisk/utils.h"   /* ast_strdupa */
00043 #include "console_video.h" /* ast_strdupa */
00044 
00045 #ifdef HAVE_SDL   /* we only use this code if SDL is available */
00046 #include <SDL/SDL.h>
00047 
00048 /* Fonts characterization. XXX should be read from the file */
00049 #define FONT_H 20       /* char height, pixels */
00050 #define FONT_W 9        /* char width, pixels */
00051 
00052 struct board {
00053    int      kb_output;  /* identity of the board */
00054    /* pointer to the destination surface (on the keypad window) */
00055    SDL_Surface *screen; /* the main screen */
00056    SDL_Rect *p_rect; /* where to write on the main screen */
00057    SDL_Surface *blank;     /* original content of the window */
00058 
00059    int   v_h;  /* virtual text height, in lines */
00060    int   v_w;  /* virtual text width, in lines (probably same as p_w) */
00061    int   p_h;  /* physical (displayed) text height, in lines
00062           * XXX p_h * FONT_H = pixel_height */
00063    int   p_w;  /* physical (displayed) text width, in characters
00064           * XXX p_w * FONT_W = pixel_width */
00065 
00066    int   cur_col; /* print position (free character) on the last line */
00067    int   cur_line;   /* first (or last ?) virtual line displayed,
00068                 * 0 is the line at the bottom, 1 is the one above,...
00069                 */
00070 
00071    SDL_Surface     *font;     /* points to a surface in the gui structure */
00072    SDL_Rect *font_rects;   /* pointer to the font rects */
00073    char     *text;
00074             /* text buffer, v_h * v_w char.
00075              * We make sure the buffer is always full,
00076              * print on some position on the last line,
00077              * and scroll up when appending new text
00078              */
00079 };
00080 
00081 /*! \brief Initialize the board.
00082  * return 0 on success, 1 on error
00083  * TODO, if this is done at reload time,
00084  * free resources before allocate new ones
00085  * TODO: resource deallocation in case of error.
00086  * TODO: move the font load at gui_initialization
00087  * TODO: deallocation of the message history
00088  */
00089 struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
00090    SDL_Surface *font, SDL_Rect *font_rects);
00091 struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
00092    SDL_Surface *font, SDL_Rect *font_rects)
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 }
00156 
00157 /* Render the text on the board surface.
00158  * The first line to render is the one at v_h - p_h - cur_line,
00159  * the size is p_h * p_w.
00160  * XXX we assume here that p_w = v_w.
00161  */
00162 static void render_board(struct board *b)
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 }
00197 
00198 void move_message_board(struct board *b, int dy)
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 }
00208 
00209 /* return the content of a board */
00210 const char *read_message(const struct board *b)
00211 {
00212    return b->text;
00213 }
00214 
00215 int reset_board(struct board *b)
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 }
00223 /* Store the message on the history board
00224  * and blit on screen if required.
00225  * XXX now easy. only regular chars
00226  */
00227 int print_message(struct board *b, const char *s)
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 }
00315 
00316 /* deletes a board.
00317  * we make the free operation on any fields of the board structure allocated
00318  * in dynamic memory
00319  */
00320 void delete_board(struct board *b)
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 }
00332 
00333 #if 0
00334 /*! \brief refresh the screen, and also grab a bunch of events.
00335  */
00336 static int scroll_message(...)
00337 {
00338 if moving up, scroll text up;
00339     if (gui->message_board.screen_cur > 0)
00340    gui->message_board.screen_cur--;
00341 otherwise scroll text down.
00342     if ((b->screen_cur + b->p_line) < b->board_next) {
00343    gui->message_board.screen_cur++;
00344 #endif /* notyet */
00345 
00346 #endif /* HAVE_SDL */

Generated on Sat Mar 10 01:54:15 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7