2 * GUI for console video.
3 * The routines here are in charge of loading the keypad and handling events.
8 #include "console_video.h"
9 #include "asterisk/lock.h"
10 #include "asterisk/frame.h"
11 #include "asterisk/utils.h" /* ast_calloc and ast_realloc */
12 #include <math.h> /* sqrt */
14 enum kp_type { KP_NONE, KP_RECT, KP_CIRCLE };
16 int c; /* corresponding character */
17 int x0, y0, x1, y1, h; /* arguments */
21 /* our representation of a displayed window. SDL can only do one main
22 * window so we map everything within that one
24 enum { WIN_LOCAL, WIN_REMOTE, WIN_KEYPAD, WIN_MAX };
25 /* our representation of a displayed window. SDL can only do one main
26 * window so we map everything within that one
28 struct display_window {
30 SDL_Rect rect; /* location of the window */
32 #define GUI_BUFFER_LEN 256 /* buffer lenght used for input buffers */
34 struct keypad_entry; /* defined in console_gui.c */
36 /*! \brief info related to the gui: button status, mouse coords, etc. */
38 char inbuf[GUI_BUFFER_LEN]; /* buffer for to-dial buffer */
39 int inbuf_pos; /* next free position in inbuf */
40 char msgbuf[GUI_BUFFER_LEN]; /* buffer for text-message buffer */
41 int msgbuf_pos; /* next free position in msgbuf */
42 int text_mode; /* switch to-dial and text-message mode */
43 int drag_mode; /* switch phone and drag-source mode */
44 int x_drag; /* x coordinate where the drag starts */
45 int y_drag; /* y coordinate where the drag starts */
47 TTF_Font *font; /* font to be used */
49 int outfd; /* fd for output */
50 SDL_Surface *keypad; /* the pixmap for the keypad */
52 struct keypad_entry *kp;
54 struct display_window win[WIN_MAX];
57 static void cleanup_sdl(struct video_desc *env)
60 struct gui_info *gui = env->gui;
64 /* unload font file */
66 TTF_CloseFont(gui->font);
70 /* uninitialize SDL_ttf library */
75 SDL_FreeSurface(gui->keypad);
78 /* uninitialize the SDL environment */
79 for (i = 0; i < WIN_MAX; i++) {
81 SDL_FreeYUVOverlay(gui->win[i].bmp);
83 bzero(gui->win, sizeof(gui->win));
84 /* XXX free the keys entries */
89 env->screen = NULL; /* XXX check reference */
91 ast_mutex_destroy(&(env->in.dec_in_lock));
95 * Display video frames (from local or remote stream) using the SDL library.
96 * - Set the video mode to use the resolution specified by the codec context
97 * - Create a YUV Overlay to copy the frame into it;
98 * - After the frame is copied into the overlay, display it
100 * The size is taken from the configuration.
102 * 'out' is 0 for remote video, 1 for the local video
104 static void show_frame(struct video_desc *env, int out)
106 AVPicture *p_in, p_out;
107 struct fbuf_t *b_in, *b_out;
109 struct gui_info *gui = env->gui;
114 if (out == WIN_LOCAL) { /* webcam/x11 to sdl */
115 b_in = &env->out.enc_in;
116 b_out = &env->out.loc_dpy;
119 /* copy input format from the decoding context */
120 AVCodecContext *c = env->in.dec_ctx;
121 b_in = &env->in.dec_out;
122 b_in->pix_fmt = c->pix_fmt;
126 b_out = &env->in.rem_dpy;
127 p_in = (AVPicture *)env->in.d_frame;
129 bmp = gui->win[out].bmp;
130 SDL_LockYUVOverlay(bmp);
131 /* output picture info - this is sdl, YUV420P */
132 bzero(&p_out, sizeof(p_out));
133 p_out.data[0] = bmp->pixels[0];
134 p_out.data[1] = bmp->pixels[1];
135 p_out.data[2] = bmp->pixels[2];
136 p_out.linesize[0] = bmp->pitches[0];
137 p_out.linesize[1] = bmp->pitches[1];
138 p_out.linesize[2] = bmp->pitches[2];
140 my_scale(b_in, p_in, b_out, &p_out);
142 /* lock to protect access to Xlib by different threads. */
143 SDL_DisplayYUVOverlay(bmp, &gui->win[out].rect);
144 SDL_UnlockYUVOverlay(bmp);
148 * GUI layout, structure and management
151 For the GUI we use SDL to create a large surface (env->screen)
152 containing tree sections: remote video on the left, local video
153 on the right, and the keypad with all controls and text windows
155 The central section is built using two images: one is the skin,
156 the other one is a mask where the sensitive areas of the skin
157 are colored in different grayscale levels according to their
158 functions. The mapping between colors and function is defined
159 in the 'enum pixel_value' below.
161 Mouse and keyboard events are detected on the whole surface, and
162 handled differently according to their location, as follows:
164 - drag on the local video window are used to move the captured
165 area (in the case of X11 grabber) or the picture-in-picture
166 location (in case of camera included on the X11 grab).
167 - click on the keypad are mapped to the corresponding key;
168 - drag on some keypad areas (sliders etc.) are mapped to the
169 corresponding functions;
170 - keystrokes are used as keypad functions, or as text input
171 if we are in text-input mode.
173 To manage these behavior we use two status variables,
174 that defines if keyboard events should be redirect to dialing functions
175 or to write message functions, and if mouse events should be used
176 to implement keypad functionalities or to drag the capture device.
178 Configuration options control the appeareance of the gui:
180 keypad = /tmp/phone.jpg ; the keypad on the screen
181 keypad_font = /tmp/font.ttf ; the font to use for output
186 /* enumerate for the pixel value. 0..127 correspond to ascii chars */
188 /* answer/close functions */
192 /* other functions */
194 KEY_AUTOANSWER = 131,
196 KEY_LOCALVIDEO = 133,
197 KEY_REMOTEVIDEO = 134,
198 KEY_WRITEMESSAGE = 135,
199 KEY_GUI_CLOSE = 136, /* close gui */
201 /* other areas within the keypad */
202 KEY_DIGIT_BACKGROUND = 255,
204 /* areas outside the keypad - simulated */
205 KEY_OUT_OF_KEYPAD = 251,
211 * Handlers for the various keypad functions
214 /*! \brief append a character, or reset if '\0' */
215 static void append_char(char *str, int *str_pos, const char c)
220 else if (i < GUI_BUFFER_LEN - 1)
223 i = GUI_BUFFER_LEN - 1; /* unnecessary, i think */
228 /* accumulate digits, possibly call dial if in connected mode */
229 static void keypad_digit(struct video_desc *env, int digit)
231 if (env->owner) { /* we have a call, send the digit */
232 struct ast_frame f = { AST_FRAME_DTMF, 0 };
235 ast_queue_frame(env->owner, &f);
236 } else { /* no call, accumulate digits */
237 append_char(env->gui->inbuf, &env->gui->inbuf_pos, digit);
241 /* this is a wrapper for actions that are available through the cli */
242 /* TODO append arg to command and send the resulting string as cli command */
243 static void keypad_send_command(struct video_desc *env, char *command)
245 ast_log(LOG_WARNING, "keypad_send_command(%s) called\n", command);
246 ast_cli_command(env->gui->outfd, command);
250 /* function used to toggle on/off the status of some variables */
251 static char *keypad_toggle(struct video_desc *env, int index)
253 ast_log(LOG_WARNING, "keypad_toggle(%i) called\n", index);
257 env->out.sendvideo = !env->out.sendvideo;
261 struct chan_oss_pvt *o = find_desc(oss_active);
265 case KEY_AUTOANSWER: {
266 struct chan_oss_pvt *o = find_desc(oss_active);
267 o->autoanswer = !o->autoanswer;
275 char *console_do_answer(int fd);
277 * Function called when the pick up button is pressed
278 * perform actions according the channel status:
280 * - if no one is calling us and no digits was pressed,
281 * the operation have no effects,
282 * - if someone is calling us we answer to the call.
283 * - if we have no call in progress and we pressed some
284 * digit, send the digit to the console.
286 static void keypad_pick_up(struct video_desc *env)
288 struct gui_info *gui = env->gui;
290 ast_log(LOG_WARNING, "keypad_pick_up called\n");
292 if (env->owner) { /* someone is calling us, just answer */
293 console_do_answer(-1);
294 } else if (gui->inbuf_pos) { /* we have someone to call */
295 ast_cli_command(gui->outfd, gui->inbuf);
298 append_char(gui->inbuf, &gui->inbuf_pos, '\0'); /* clear buffer */
301 #if 0 /* still unused */
303 * As an alternative to SDL_TTF, we can simply load the font from
304 * an image and blit characters on the background of the GUI.
306 * To generate a font we can use the 'fly' command with the
307 * following script (3 lines with 32 chars each)
312 string 255,255,255, 0, 0,giant, !"#$%&'()*+,-./0123456789:;<=>?
313 string 255,255,255, 0,20,giant,@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
314 string 255,255,255, 0,40,giant,`abcdefghijklmnopqrstuvwxyz{|}~
319 /* Print given text on the gui */
320 static int gui_output(struct video_desc *env, const char *text)
323 return 1; /* error, not supported */
325 int x = 30, y = 20; /* XXX change */
326 SDL_Surface *output = NULL;
327 SDL_Color color = {0, 0, 0}; /* text color */
328 struct gui_info *gui = env->gui;
329 SDL_Rect dest = {gui->win[WIN_KEYPAD].rect.x + x, y};
331 /* clean surface each rewrite */
332 SDL_BlitSurface(gui->keypad, NULL, env->screen, &gui->win[WIN_KEYPAD].rect);
334 output = TTF_RenderText_Solid(gui->font, text, color);
335 if (output == NULL) {
336 ast_log(LOG_WARNING, "Cannot render text on gui - %s\n", TTF_GetError());
340 SDL_BlitSurface(output, NULL, env->screen, &dest);
342 SDL_UpdateRects(gui->keypad, 1, &gui->win[WIN_KEYPAD].rect);
343 SDL_FreeSurface(output);
344 return 0; /* success */
349 static int video_geom(struct fbuf_t *b, const char *s);
350 static void sdl_setup(struct video_desc *env);
351 static int kp_match_area(const struct keypad_entry *e, int x, int y);
354 * Handle SDL_MOUSEBUTTONDOWN type, finding the palette
355 * index value and calling the right callback.
357 * x, y are referred to the upper left corner of the main SDL window.
359 static void handle_button_event(struct video_desc *env, SDL_MouseButtonEvent button)
361 uint8_t index = KEY_OUT_OF_KEYPAD; /* the key or region of the display we clicked on */
364 ast_log(LOG_WARNING, "event %d %d have %d/%d regions at %p\n",
365 button.x, button.y, env->gui->kp_used, env->gui->kp_size, env->gui->kp);
367 /* for each click we come back in normal mode */
368 env->gui->text_mode = 0;
370 /* define keypad boundary */
371 if (button.x < env->in.rem_dpy.w)
372 index = KEY_REM_DPY; /* click on remote video */
373 else if (button.x > env->in.rem_dpy.w + env->gui->keypad->w)
374 index = KEY_LOC_DPY; /* click on local video */
375 else if (button.y > env->gui->keypad->h)
376 index = KEY_OUT_OF_KEYPAD; /* click outside the keypad */
377 else if (env->gui->kp) {
379 for (i = 0; i < env->gui->kp_used; i++) {
380 if (kp_match_area(&env->gui->kp[i], button.x - env->in.rem_dpy.w, button.y)) {
381 index = env->gui->kp[i].c;
387 /* exec the function */
388 if (index < 128) { /* surely clicked on the keypad, don't care which key */
389 keypad_digit(env, index);
393 /* answer/close function */
398 keypad_send_command(env, "console hangup");
401 /* other functions */
405 keypad_toggle(env, index);
410 case KEY_REMOTEVIDEO:
412 case KEY_WRITEMESSAGE:
413 /* goes in text-mode */
414 env->gui->text_mode = 1;
418 /* press outside the keypad. right increases size, center decreases, left drags */
421 if (button.button == SDL_BUTTON_LEFT) {
422 if (index == KEY_LOC_DPY) {
423 /* store points where the drag start
424 * and switch in drag mode */
425 env->gui->x_drag = button.x;
426 env->gui->y_drag = button.y;
427 env->gui->drag_mode = 1;
432 struct fbuf_t *fb = index == KEY_LOC_DPY ? &env->out.loc_dpy : &env->in.rem_dpy;
433 sprintf(buf, "%c%dx%d", button.button == SDL_BUTTON_RIGHT ? '>' : '<',
439 case KEY_OUT_OF_KEYPAD:
445 case KEY_DIGIT_BACKGROUND:
448 ast_log(LOG_WARNING, "function not yet defined %i\n", index);
453 * Handle SDL_KEYDOWN type event, put the key pressed
454 * in the dial buffer or in the text-message buffer,
455 * depending on the text_mode variable value.
457 * key is the SDLKey structure corresponding to the key pressed.
459 static void handle_keyboard_input(struct video_desc *env, SDLKey key)
461 if (env->gui->text_mode) {
462 /* append in the text-message buffer */
463 if (key == SDLK_RETURN) {
464 /* send the text message and return in normal mode */
465 env->gui->text_mode = 0;
466 keypad_send_command(env, "send text");
468 /* accumulate the key in the message buffer */
469 append_char(env->gui->msgbuf, &env->gui->msgbuf_pos, key);
473 /* append in the dial buffer */
474 append_char(env->gui->inbuf, &env->gui->inbuf_pos, key);
481 * Check if the grab point is inside the X screen.
483 * x represent the new grab value
484 * limit represent the upper value to use
486 static int boundary_checks(int x, int limit)
488 return (x <= 0) ? 0 : (x > limit ? limit : x);
491 /* implement superlinear acceleration on the movement */
492 static int move_accel(int delta)
494 int d1 = delta*delta / 100;
495 return (delta > 0) ? delta + d1 : delta - d1;
499 * Move the source of the captured video.
501 * x_final_drag and y_final_drag are the coordinates where the drag ends,
502 * start coordinares are in the gui_info structure.
504 static void move_capture_source(struct video_desc *env, int x_final_drag, int y_final_drag)
506 int new_x, new_y; /* new coordinates for grabbing local video */
507 int x = env->out.loc_src.x; /* old value */
508 int y = env->out.loc_src.y; /* old value */
510 /* move the origin */
511 #define POLARITY -1 /* +1 or -1 depending on the desired direction */
512 new_x = x + POLARITY*move_accel(x_final_drag - env->gui->x_drag) * 3;
513 new_y = y + POLARITY*move_accel(y_final_drag - env->gui->y_drag) * 3;
515 env->gui->x_drag = x_final_drag; /* update origin */
516 env->gui->y_drag = y_final_drag;
518 /* check boundary and let the source to grab from the new points */
519 env->out.loc_src.x = boundary_checks(new_x, env->out.screen_width - env->out.loc_src.w);
520 env->out.loc_src.y = boundary_checks(new_y, env->out.screen_height - env->out.loc_src.h);
525 * I am seeing some kind of deadlock or stall around
526 * SDL_PumpEvents() while moving the window on a remote X server
527 * (both xfree-4.4.0 and xorg 7.2)
528 * and windowmaker. It is unclear what causes it.
531 /* grab a bunch of events */
532 static void eventhandler(struct video_desc *env)
536 SDL_Event ev[N_EVENTS];
538 #define MY_EV (SDL_MOUSEBUTTONDOWN|SDL_KEYDOWN)
539 while ( (n = SDL_PeepEvents(ev, N_EVENTS, SDL_GETEVENT, SDL_ALLEVENTS)) > 0) {
540 for (i = 0; i < n; i++) {
542 ast_log(LOG_WARNING, "------ event %d at %d %d\n",
543 ev[i].type, ev[i].button.x, ev[i].button.y);
545 switch (ev[i].type) {
547 handle_keyboard_input(env, ev[i].key.keysym.sym);
549 case SDL_MOUSEMOTION:
550 if (env->gui->drag_mode != 0)
551 move_capture_source(env, ev[i].motion.x, ev[i].motion.y);
553 case SDL_MOUSEBUTTONDOWN:
554 handle_button_event(env, ev[i].button);
556 case SDL_MOUSEBUTTONUP:
557 if (env->gui->drag_mode != 0) {
558 move_capture_source(env, ev[i].button.x, ev[i].button.y);
559 env->gui->drag_mode = 0;
567 struct timeval b, a = ast_tvnow();
569 //SDL_Lock_EventThread();
572 i = ast_tvdiff_ms(b, a);
574 fprintf(stderr, "-------- SDL_PumpEvents took %dms\n", i);
575 //SDL_Unlock_EventThread();
579 static SDL_Surface *get_keypad(const char *file)
583 #ifdef HAVE_SDL_IMAGE
584 temp = IMG_Load(file);
586 temp = SDL_LoadBMP(file);
589 fprintf(stderr, "Unable to load image %s: %s\n",
590 file, SDL_GetError());
594 /* TODO: consistency checks, check for bpp, widht and height */
595 /* Init the mask image used to grab the action. */
596 static struct gui_info *gui_init(struct video_desc *env)
598 struct gui_info *gui = ast_calloc(1, sizeof(*gui));
602 /* initialize keypad status */
606 /* initialize grab coordinates */
607 env->out.loc_src.x = 0;
608 env->out.loc_src.y = 0;
610 /* initialize keyboard buffer */
611 append_char(gui->inbuf, &gui->inbuf_pos, '\0');
612 append_char(gui->msgbuf, &gui->msgbuf_pos, '\0');
615 /* Initialize SDL_ttf library and load font */
616 if (TTF_Init() == -1) {
617 ast_log(LOG_WARNING, "Unable to init SDL_ttf, no output available\n");
621 #define GUI_FONTSIZE 28
622 gui->font = TTF_OpenFont( env->keypad_font, GUI_FONTSIZE);
624 ast_log(LOG_WARNING, "Unable to load font %s, no output available\n", env->keypad_font);
627 ast_log(LOG_WARNING, "Loaded font %s\n", env->keypad_font);
630 gui->outfd = open ("/dev/null", O_WRONLY); /* discard output, temporary */
631 if (gui->outfd < 0) {
632 ast_log(LOG_WARNING, "Unable output fd\n");
642 /* setup an sdl overlay and associated info, return 0 on success, != 0 on error */
643 static int set_win(SDL_Surface *screen, struct display_window *win, int fmt,
644 int w, int h, int x, int y)
646 win->bmp = SDL_CreateYUVOverlay(w, h, fmt, screen);
647 if (win->bmp == NULL)
648 return -1; /* error */
656 static int keypad_cfg_read(struct gui_info *gui, const char *val);
658 static void keypad_setup(struct video_desc *env)
664 if (env->gui->keypad)
666 env->gui->keypad = get_keypad(env->keypad_file);
667 if (!env->gui->keypad)
671 * If the keypad image has a comment field, try to read
672 * the button location from there. The block must be
673 * keypad_entry = token shape x0 y0 x1 y1 h
675 * (basically, lines have the same format as config file entries.
676 * same as the keypad_entry.
677 * You can add it to a jpeg file using wrjpgcom
679 do { /* only once, in fact */
680 const char region[] = "region";
681 int reg_len = strlen(region);
682 const unsigned char *s, *e;
684 fd = open(env->keypad_file, O_RDONLY);
686 ast_log(LOG_WARNING, "fail to open %s\n", env->keypad_file);
689 l = lseek(fd, 0, SEEK_END);
691 ast_log(LOG_WARNING, "fail to lseek %s\n", env->keypad_file);
694 p = mmap(NULL, l, PROT_READ, 0, fd, 0);
696 ast_log(LOG_WARNING, "fail to mmap %s size %ld\n", env->keypad_file, (long)l);
699 e = (const unsigned char *)p + l;
700 for (s = p; s < e - 20 ; s++) {
701 if (!memcmp(s, region, reg_len)) { /* keyword found */
702 /* reset previous entries */
703 keypad_cfg_read(env->gui, "reset");
707 for ( ;s < e - 20; s++) {
709 const unsigned char *s1;
710 if (index(" \t\r\n", *s)) /* ignore blanks */
712 if (*s > 127) /* likely end of comment */
714 if (memcmp(s, region, reg_len)) /* keyword not found */
717 l = MIN(sizeof(buf), e - s);
718 ast_copy_string(buf, s, l);
719 s1 = ast_skip_blanks(buf); /* between token and '=' */
720 if (*s1++ != '=') /* missing separator */
722 if (*s1 == '>') /* skip => */
724 keypad_cfg_read(env->gui, ast_skip_blanks(s1));
725 /* now wait for a newline */
727 while (s1 < e - 20 && !index("\r\n", *s1) && *s1 < 128)
738 /* [re]set the main sdl window, useful in case of resize */
739 static void sdl_setup(struct video_desc *env)
741 int dpy_fmt = SDL_IYUV_OVERLAY; /* YV12 causes flicker in SDL */
742 int depth, maxw, maxh;
743 const SDL_VideoInfo *info = SDL_GetVideoInfo();
744 int kp_w = 0, kp_h = 0; /* keypad width and height */
746 /* We want at least 16bpp to support YUV overlays.
747 * E.g with SDL_VIDEODRIVER = aalib the default is 8
749 depth = info->vfmt->BitsPerPixel;
753 * initialize the SDL environment. We have one large window
754 * with local and remote video, and a keypad.
755 * At the moment we arrange them statically, as follows:
756 * - on the left, the remote video;
757 * - on the center, the keypad
758 * - on the right, the local video
759 * We need to read in the skin for the keypad before creating the main
760 * SDL window, because the size is only known here.
763 env->gui = gui_init(env);
764 ast_log(LOG_WARNING, "gui_init returned %p\n", env->gui);
767 ast_log(LOG_WARNING, "keypad_setup returned %p %d\n",
768 env->gui->keypad, env->gui->kp_used);
769 if (env->gui->keypad) {
770 kp_w = env->gui->keypad->w;
771 kp_h = env->gui->keypad->h;
774 #define BORDER 5 /* border around our windows */
775 maxw = env->in.rem_dpy.w + env->out.loc_dpy.w + kp_w;
776 maxh = MAX( MAX(env->in.rem_dpy.h, env->out.loc_dpy.h), kp_h);
779 env->screen = SDL_SetVideoMode(maxw, maxh, depth, 0);
781 ast_log(LOG_ERROR, "SDL: could not set video mode - exiting\n");
785 SDL_WM_SetCaption("Asterisk console Video Output", NULL);
786 if (set_win(env->screen, &env->gui->win[WIN_REMOTE], dpy_fmt,
787 env->in.rem_dpy.w, env->in.rem_dpy.h, BORDER, BORDER))
789 if (set_win(env->screen, &env->gui->win[WIN_LOCAL], dpy_fmt,
790 env->out.loc_dpy.w, env->out.loc_dpy.h,
791 3*BORDER+env->in.rem_dpy.w + kp_w, BORDER))
794 /* display the skin, but do not free it as we need it later to
795 * restore text areas and maybe sliders too.
797 if (env->gui && env->gui->keypad) {
798 struct SDL_Rect *dest = &env->gui->win[WIN_KEYPAD].rect;
799 dest->x = 2*BORDER + env->in.rem_dpy.w;
801 dest->w = env->gui->keypad->w;
802 dest->h = env->gui->keypad->h;
803 SDL_BlitSurface(env->gui->keypad, NULL, env->screen, dest);
804 SDL_UpdateRects(env->screen, 1, dest);
806 env->in.dec_in_cur = &env->in.dec_in[0];
807 env->in.dec_in_dpy = NULL; /* nothing to display */
811 if (env->sdl_ok == 0) /* free resources in case of errors */
816 * Functions to determine if a point is within a region. Return 1 if success.
817 * First rotate the point, with
818 * x' = (x - x0) * cos A + (y - y0) * sin A
819 * y' = -(x - x0) * sin A + (y - y0) * cos A
820 * where cos A = (x1-x0)/l, sin A = (y1 - y0)/l, and
821 * l = sqrt( (x1-x0)^2 + (y1-y0)^2
822 * Then determine inclusion by simple comparisons i.e.:
823 * rectangle: x >= 0 && x < l && y >= 0 && y < h
824 * ellipse: (x-xc)^2/l^2 + (y-yc)^2/h2 < 1
826 static int kp_match_area(const struct keypad_entry *e, int x, int y)
828 double xp, dx = (e->x1 - e->x0);
829 double yp, dy = (e->y1 - e->y0);
830 double l = sqrt(dx*dx + dy*dy);
833 if (l > 1) { /* large enough */
834 xp = ((x - e->x0)*dx + (y - e->y0)*dy)/l;
835 yp = (-(x - e->x0)*dy + (y - e->y0)*dx)/l;
836 if (e->type == KP_RECT) {
837 ret = (xp >= 0 && xp < l && yp >=0 && yp < l);
838 } else if (e->type == KP_CIRCLE) {
839 dx = xp*xp/(l*l) + yp*yp/(e->h*e->h);
844 ast_log(LOG_WARNING, "result %d [%d] for match %d,%d in type %d p0 %d,%d p1 %d,%d h %d\n",
845 ret, e->c, x, y, e->type, e->x0, e->y0, e->x1, e->y1, e->h);
850 struct _s_k { const char *s; int k; };
851 static struct _s_k gui_key_map[] = {
852 {"PICK_UP", KEY_PICK_UP },
853 {"PICKUP", KEY_PICK_UP },
854 {"HANG_UP", KEY_HANG_UP },
855 {"HANGUP", KEY_HANG_UP },
857 {"AUTOANSWER", KEY_AUTOANSWER },
858 {"SENDVIDEO", KEY_SENDVIDEO },
859 {"LOCALVIDEO", KEY_LOCALVIDEO },
860 {"REMOTEVIDEO", KEY_REMOTEVIDEO },
861 {"WRITEMESSAGE", KEY_WRITEMESSAGE },
862 {"GUI_CLOSE", KEY_GUI_CLOSE },
865 /*! \brief read a keypad entry line in the format
867 * token circle xc yc diameter
868 * token circle xc yc x1 y1 h # ellipse, main diameter and height
869 * token rect x0 y0 x1 y1 h # rectangle with main side and eight
870 * token is the token to be returned, either a character or a symbol
872 * Return 1 on success, 0 on error.
874 static int keypad_cfg_read(struct gui_info *gui, const char *val)
876 struct keypad_entry e;
880 if (gui == NULL || val == NULL)
883 bzero(&e, sizeof(e));
884 i = sscanf(val, "%14s %14s %d %d %d %d %d",
885 s1, s2, &e.x0, &e.y0, &e.x1, &e.y1, &e.h);
890 case 1: /* only "reset" is allowed */
891 if (strcasecmp(s1, "reset")) /* invalid */
897 case 5: /* token circle xc yc diameter */
898 if (strcasecmp(s2, "circle")) /* invalid */
901 e.y1 = e.y0; /* map radius in x1 y1 */
902 e.x1 = e.x0 + e.h; /* map radius in x1 y1 */
903 e.x0 = e.x0 - e.h; /* map radius in x1 y1 */
906 case 7: /* token circle|rect x0 y0 x1 y1 h */
907 if (e.x1 < e.x0 || e.h <= 0) {
908 ast_log(LOG_WARNING, "error in coordinates\n");
912 if (!strcasecmp(s2, "circle")) {
913 /* for a circle we specify the diameter but store center and radii */
915 e.x0 = (e.x1 + e.x0) / 2;
916 e.y0 = (e.y1 + e.y0) / 2;
918 } else if (!strcasecmp(s2, "rect")) {
924 // ast_log(LOG_WARNING, "reading [%s] returns %d %d\n", val, i, ret);
927 /* map the string into token to be returned */
929 if (i > 0 || s1[1] == '\0') /* numbers or single characters */
930 e.c = (i > 9) ? i : s1[0];
933 for (p = gui_key_map; p->s; p++) {
934 if (!strcasecmp(p->s, s1)) {
941 ast_log(LOG_WARNING, "missing token\n");
944 if (gui->kp_size == 0) {
945 gui->kp = ast_calloc(10, sizeof(e));
946 if (gui->kp == NULL) {
947 ast_log(LOG_WARNING, "cannot allocate kp");
952 if (gui->kp_size == gui->kp_used) { /* must allocate */
953 struct keypad_entry *a = ast_realloc(gui->kp, sizeof(e)*(gui->kp_size+10));
955 ast_log(LOG_WARNING, "cannot reallocate kp");
961 if (gui->kp_size == gui->kp_used)
963 gui->kp[gui->kp_used++] = e;
964 ast_log(LOG_WARNING, "now %d regions\n", gui->kp_used);