~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/channels/console_board.c -rw-r--r-- 9.5 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 10 months ago
                                                                                
130ba7ae Bernhard Schmidt
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright 2007-2008, Marta Carbone, Luigi Rizzo
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 *
 * $Revision$
 */

/*
 * Message board implementation.
 *
 * A message board is a region of the SDL screen where
 * messages can be printed, like on a terminal window.
 *
 * At the moment we support fix-size font.
 *
 * 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 (not yet!)
 *
 * TODO: font dynamic allocation
 *
 * The region where the text is 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).
 */

/*** MODULEINFO
	<support_level>extended</support_level>
 ***/

#include "asterisk.h"	/* ast_strdupa */
#include "asterisk/utils.h"	/* ast_strdupa */
#include "console_video.h"	/* ast_strdupa */

#ifdef HAVE_SDL	/* we only use this code if SDL is available */
#include <SDL/SDL.h>

/* Fonts characterization. XXX should be read from the 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 */

	/* the rectangle we actually use */
	br.h = b->p_h * FONT_H;	/* pixel sizes of the background */
	br.w = b->p_w * FONT_W;
	br.x = br.y = 0;

	/* allocate a buffer for the text */
	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 */

	/* make a copy of the original rectangle, 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 */

	if (0) 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;
}

/* 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;	/* XXX first 32 chars are not printable */
		if (c < 0) /* buffer terminator or anything else is a blank */
			c = 0;
		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;
		}
	}
	SDL_UpdateRects(b->screen, 1, b->p_rect);	/* Update the screen */
}

void move_message_board(struct board *b, int dy)
{
	int cur = b->cur_line + dy;
	if (cur < 0)
		cur = 0;
	else if (cur >= b->v_h - b->p_h)
		cur = b->v_h - b->p_h - 1;
	b->cur_line = cur;
	render_board(b);
}

/* return the content of a board */
const char *read_message(const struct board *b)
{
	return b->text;
}

int reset_board(struct board *b)
{
	memset(b->text, ' ', b->v_w * b->v_h);	/* fill with spaces */
	b->cur_col = 0;
	b->cur_line = 0;
	render_board(b);
	return 0;
}
/* 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 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 */
			dst[col] = '\0'; /* mark the rest of the line as empty */
			col = 0;
			dst += b->v_w;
			break;
		case '\b':	/* one char back */
			if (col > 0)
				col--;
			dst[col] = ' '; /* delete current char */
			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;
		}
	}
	dst[col] = '\0'; /* the current position is empty */
	b->cur_col = col;
	/* everything is printed now, must do the rendering */
	render_board(b);
	return 1;
}

/* deletes a board.
 * we make the free operation on any fields of the board structure allocated
 * in dynamic memory
 */
void delete_board(struct board *b)
{
	if (b) {
		/* deletes the text */
		if (b->text)
			ast_free (b->text);
		/* deallocates the blank surface */
		SDL_FreeSurface(b->blank);
		/* deallocates the board */
		ast_free(b);
	}
}

#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 */