console_gui.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /*
  2. * GUI for console video.
  3. * The routines here are in charge of loading the keypad and handling events.
  4. * $Revision$
  5. */
  6. /*
  7. * GUI layout, structure and management
  8. For the GUI we use SDL to create a large surface (gui->screen) with 4 areas:
  9. remote video on the left, local video on the right, keypad with all controls
  10. and text windows in the center, and source device thumbnails on the top.
  11. The top row is not displayed if no devices are specified in the config file.
  12. ________________________________________________________________
  13. | ______ ______ ______ ______ ______ ______ ______ |
  14. | | tn.1 | | tn.2 | | tn.3 | | tn.4 | | tn.5 | | tn.6 | | tn.7 | |
  15. | |______| |______| |______| |______| |______| |______| |______| |
  16. | ______ ______ ______ ______ ______ ______ ______ |
  17. | |______| |______| |______| |______| |______| |______| |______| |
  18. | _________________ __________________ _________________ |
  19. | | | | | | | |
  20. | | | | | | | |
  21. | | | | | | | |
  22. | | remote video | | | | local video | |
  23. | | | | | | ______ | |
  24. | | | | keypad | | | PIP || |
  25. | | | | | | |______|| |
  26. | |_________________| | | |_________________| |
  27. | | | |
  28. | | | |
  29. | |__________________| |
  30. |________________________________________________________________|
  31. The central section is built using an image (jpg, png, maybe gif too)
  32. for the skin, and other GUI elements. Comments embedded in the image
  33. indicate to what function each area is mapped to.
  34. Another image (png with transparency) is used for the font.
  35. Mouse and keyboard events are detected on the whole surface, and
  36. handled differently according to their location:
  37. - center/right click on the local/remote window are used to resize
  38. the corresponding window;
  39. - clicks on the thumbnail start/stop sources and select them as
  40. primary or secondary video sources;
  41. - drag on the local video window are used to move the captured
  42. area (in the case of X11 grabber) or the picture-in-picture position;
  43. - keystrokes on the keypad are mapped to the corresponding key;
  44. keystrokes are used as keypad functions, or as text input
  45. if we are in text-input mode.
  46. - drag on some keypad areas (sliders etc.) are mapped to the
  47. corresponding functions (mute/unmute audio and video,
  48. enable/disable Picture-in-Picture, freeze the incoming video,
  49. dial numbers, pick up or hang up a call, ...)
  50. Configuration options control the appeareance of the gui:
  51. keypad = /tmp/kpad2.jpg ; the skin
  52. keypad_font = /tmp/font.png ; the font to use for output
  53. For future implementation, intresting features can be the following:
  54. - save of the whole SDL window as a picture
  55. - audio output device switching
  56. The audio switching feature should allow changing the device
  57. or switching to a recorded message for audio sent to remote party.
  58. The selection of the device should happen clicking on a marker in the layout.
  59. For this reason above the thumbnails row in the layout we would like a new row,
  60. the elements composing the row could be message boards, reporting the name of the
  61. device or the path of the message to be played.
  62. For video input freeze and entire window capture, we define 2 new key types,
  63. those should be activated pressing the buttons on the keypad, associated with
  64. new regions inside the keypad pictureas comments
  65. *
  66. */
  67. /*** MODULEINFO
  68. <support_level>extended</support_level>
  69. ***/
  70. #include "asterisk.h"
  71. #include "console_video.h"
  72. #include "asterisk/lock.h"
  73. #include "asterisk/frame.h"
  74. #include "asterisk/utils.h" /* ast_calloc and ast_realloc */
  75. #include <math.h> /* sqrt */
  76. /* We use a maximum of 12 'windows' in the GUI */
  77. enum { WIN_LOCAL, WIN_REMOTE, WIN_KEYPAD, WIN_SRC1,
  78. WIN_SRC2, WIN_SRC3, WIN_SRC4, WIN_SRC5,
  79. WIN_SRC6, WIN_SRC7, WIN_SRC8, WIN_SRC9, WIN_MAX };
  80. #ifndef HAVE_SDL /* stubs if we don't have any sdl */
  81. static void show_frame(struct video_desc *env, int out) {}
  82. static void sdl_setup(struct video_desc *env) {}
  83. static struct gui_info *cleanup_sdl(struct gui_info* g, int n) { return NULL; }
  84. static void eventhandler(struct video_desc *env, const char *caption) {}
  85. static int keypad_cfg_read(struct gui_info *gui, const char *val) { return 0; }
  86. #else /* HAVE_SDL, the real rendering code */
  87. #include <SDL/SDL.h>
  88. #include <SDL/SDL_syswm.h>
  89. #ifdef HAVE_SDL_IMAGE
  90. #include <SDL/SDL_image.h> /* for loading images */
  91. #endif
  92. #ifdef HAVE_X11
  93. /* Need to hook into X for SDL_WINDOWID handling */
  94. #include <X11/Xlib.h>
  95. #endif
  96. #define BORDER 5 /* border around our windows */
  97. #define SRC_MSG_BD_H 20 /* height of the message board below those windows */
  98. enum kp_type { KP_NONE, KP_RECT, KP_CIRCLE };
  99. struct keypad_entry {
  100. int c; /* corresponding character */
  101. int x0, y0, x1, y1, h; /* arguments */
  102. enum kp_type type;
  103. };
  104. /* our representation of a displayed window. SDL can only do one main
  105. * window so we map everything within that one
  106. */
  107. struct display_window {
  108. SDL_Overlay *bmp;
  109. SDL_Rect rect; /* location of the window */
  110. };
  111. /* each thumbnail message board has a rectangle associated for the geometry,
  112. * and a board structure, we include these two elements in a singole structure */
  113. struct thumb_bd {
  114. SDL_Rect rect; /* the rect for geometry and background */
  115. struct board *board; /* the board */
  116. };
  117. struct gui_info {
  118. enum kb_output kb_output; /* where the keyboard output goes */
  119. struct drag_info drag; /* info on the window are we dragging */
  120. /* support for display. */
  121. SDL_Surface *screen; /* the main window */
  122. int outfd; /* fd for output */
  123. SDL_Surface *keypad; /* the skin for the keypad */
  124. SDL_Rect kp_rect; /* portion of the skin to display - default all */
  125. SDL_Surface *font; /* font to be used */
  126. SDL_Rect font_rects[96]; /* only printable chars */
  127. /* each of the following board has two rectangles,
  128. * [0] is the geometry relative to the keypad,
  129. * [1] is the geometry relative to the whole screen
  130. * we do not use the thumb_bd for these boards because here we need
  131. * 2 rectangles for geometry
  132. */
  133. SDL_Rect kp_msg[2]; /* incoming msg, relative to kpad */
  134. struct board *bd_msg;
  135. SDL_Rect kp_edit[2]; /* edit user input */
  136. struct board *bd_edit;
  137. SDL_Rect kp_dialed[2]; /* dialed number */
  138. struct board *bd_dialed;
  139. /* other boards are one associated with the source windows
  140. * above the keypad in the layout, we only have the geometry
  141. * relative to the whole screen
  142. */
  143. struct thumb_bd thumb_bd_array[MAX_VIDEO_SOURCES];
  144. /* variable-size array mapping keypad regions to functions */
  145. int kp_size, kp_used;
  146. struct keypad_entry *kp;
  147. struct display_window win[WIN_MAX];
  148. };
  149. /*! \brief free the resources in struct gui_info and the descriptor itself.
  150. * Return NULL so we can assign the value back to the descriptor in case.
  151. */
  152. static struct gui_info *cleanup_sdl(struct gui_info *gui, int device_num)
  153. {
  154. int i;
  155. if (gui == NULL)
  156. return NULL;
  157. /* unload font file */
  158. if (gui->font) {
  159. SDL_FreeSurface(gui->font);
  160. gui->font = NULL;
  161. }
  162. if (gui->outfd > -1)
  163. close(gui->outfd);
  164. if (gui->keypad)
  165. SDL_FreeSurface(gui->keypad);
  166. gui->keypad = NULL;
  167. if (gui->kp)
  168. ast_free(gui->kp);
  169. /* uninitialize the SDL environment */
  170. for (i = 0; i < WIN_MAX; i++) {
  171. if (gui->win[i].bmp)
  172. SDL_FreeYUVOverlay(gui->win[i].bmp);
  173. }
  174. memset(gui, '\0', sizeof(gui));
  175. /* deallocates the space allocated for the keypad message boards */
  176. if (gui->bd_dialed)
  177. delete_board(gui->bd_dialed);
  178. if (gui->bd_msg)
  179. delete_board(gui->bd_msg);
  180. /* deallocates the space allocated for the thumbnail message boards */
  181. for (i = 0; i < device_num; i++) {
  182. if (gui->thumb_bd_array[i].board) /* may be useless */
  183. delete_board(gui->thumb_bd_array[i].board);
  184. }
  185. ast_free(gui);
  186. SDL_Quit();
  187. return NULL;
  188. }
  189. /* messages to be displayed in the sources message boards
  190. * below the source windows
  191. */
  192. /* costants defined to describe status of devices */
  193. #define IS_PRIMARY 1
  194. #define IS_SECONDARY 2
  195. #define IS_ON 4
  196. char* src_msgs[] = {
  197. " OFF",
  198. "1 OFF",
  199. " 2 OFF",
  200. "1+2 OFF",
  201. " ON",
  202. "1 ON",
  203. " 2 ON",
  204. "1+2 ON",
  205. };
  206. /*
  207. * Display video frames (from local or remote stream) using the SDL library.
  208. * - Set the video mode to use the resolution specified by the codec context
  209. * - Create a YUV Overlay to copy the frame into it;
  210. * - After the frame is copied into the overlay, display it
  211. *
  212. * The size is taken from the configuration.
  213. *
  214. * 'out' is 0 for remote video, 1 for the local video
  215. */
  216. static void show_frame(struct video_desc *env, int out)
  217. {
  218. AVPicture *p_in, p_out;
  219. struct fbuf_t *b_in, *b_out;
  220. SDL_Overlay *bmp;
  221. struct gui_info *gui = env->gui;
  222. if (!gui)
  223. return;
  224. if (out == WIN_LOCAL) { /* webcam/x11 to sdl */
  225. b_in = &env->enc_in;
  226. b_out = &env->loc_dpy;
  227. p_in = NULL;
  228. } else if (out == WIN_REMOTE) {
  229. /* copy input format from the decoding context */
  230. AVCodecContext *c;
  231. if (env->in == NULL) /* XXX should not happen - decoder not ready */
  232. return;
  233. c = env->in->dec_ctx;
  234. b_in = &env->in->dec_out;
  235. b_in->pix_fmt = c->pix_fmt;
  236. b_in->w = c->width;
  237. b_in->h = c->height;
  238. b_out = &env->rem_dpy;
  239. p_in = (AVPicture *)env->in->d_frame;
  240. } else {
  241. int i = out-WIN_SRC1;
  242. b_in = env->out.devices[i].dev_buf;
  243. if (b_in == NULL)
  244. return;
  245. p_in = NULL;
  246. b_out = &env->src_dpy[i];
  247. }
  248. bmp = gui->win[out].bmp;
  249. SDL_LockYUVOverlay(bmp);
  250. /* output picture info - this is sdl, YUV420P */
  251. memset(&p_out, '\0', sizeof(p_out));
  252. p_out.data[0] = bmp->pixels[0];
  253. p_out.data[1] = bmp->pixels[1];
  254. p_out.data[2] = bmp->pixels[2];
  255. p_out.linesize[0] = bmp->pitches[0];
  256. p_out.linesize[1] = bmp->pitches[1];
  257. p_out.linesize[2] = bmp->pitches[2];
  258. my_scale(b_in, p_in, b_out, &p_out);
  259. /* lock to protect access to Xlib by different threads. */
  260. SDL_DisplayYUVOverlay(bmp, &gui->win[out].rect);
  261. SDL_UnlockYUVOverlay(bmp);
  262. }
  263. /*
  264. * Identifiers for regions of the main window.
  265. * Values between 0 and 127 correspond to ASCII characters.
  266. * The corresponding strings to be used in the skin comment section
  267. * are defined in gui_key_map.
  268. */
  269. enum skin_area {
  270. /* answer/close functions */
  271. KEY_PICK_UP = 128,
  272. KEY_HANG_UP = 129,
  273. KEY_MUTE = 130,
  274. KEY_AUTOANSWER = 131,
  275. KEY_SENDVIDEO = 132,
  276. KEY_LOCALVIDEO = 133,
  277. KEY_REMOTEVIDEO = 134,
  278. KEY_FLASH = 136,
  279. /* sensitive areas for the various text windows */
  280. KEY_MESSAGEBOARD = 140,
  281. KEY_DIALEDBOARD = 141,
  282. KEY_EDITBOARD = 142,
  283. KEY_GUI_CLOSE = 199, /* close gui */
  284. /* regions of the skin - displayed area, fonts, etc.
  285. * XXX NOTE these are not sensitive areas.
  286. */
  287. KEY_KEYPAD = 200, /* the keypad - default to the whole image */
  288. KEY_FONT = 201, /* the font. Maybe not really useful */
  289. KEY_MESSAGE = 202, /* area for incoming messages */
  290. KEY_DIALED = 203, /* area for dialed numbers */
  291. KEY_EDIT = 204, /* area for editing user input */
  292. #ifdef notyet /* XXX for future implementation */
  293. KEY_AUDIO_SRCS = 210,
  294. /*indexes between 210 and 219 (or more) have been reserved for the "keys"
  295. associated with the audio device markers, clicking on these markers
  296. will change the source device for audio output */
  297. #endif
  298. /* Keys related to video sources */
  299. KEY_FREEZE = 220, /* freeze the incoming video */
  300. KEY_CAPTURE = 221, /* capture the whole SDL window as a picture */
  301. KEY_PIP = 230,
  302. /*indexes between 231 and 239 have been reserved for the "keys"
  303. associated with the device thumbnails, clicking on these pictures
  304. will change the source device for primary or secondary (PiP) video output*/
  305. KEY_SRCS_WIN = 231, /* till 239 */
  306. /* areas outside the keypad - simulated */
  307. KEY_OUT_OF_KEYPAD = 241,
  308. KEY_REM_DPY = 242,
  309. KEY_LOC_DPY = 243,
  310. KEY_RESET = 253, /* the 'reset' keyword */
  311. KEY_NONE = 254, /* invalid area */
  312. KEY_DIGIT_BACKGROUND = 255, /* other areas within the keypad */
  313. };
  314. /*
  315. * Handlers for the various keypad functions
  316. */
  317. /* accumulate digits, possibly call dial if in connected mode */
  318. static void keypad_digit(struct video_desc *env, int digit)
  319. {
  320. if (env->owner) { /* we have a call, send the digit */
  321. struct ast_frame f = { AST_FRAME_DTMF, 0 };
  322. f.subclass = digit;
  323. ast_queue_frame(env->owner, &f);
  324. } else { /* no call, accumulate digits */
  325. char buf[2] = { digit, '\0' };
  326. if (env->gui->bd_msg) /* XXX not strictly necessary ... */
  327. print_message(env->gui->bd_msg, buf);
  328. }
  329. }
  330. /* function used to toggle on/off the status of some variables */
  331. static char *keypad_toggle(struct video_desc *env, int index)
  332. {
  333. ast_log(LOG_WARNING, "keypad_toggle(%i) called\n", index);
  334. switch (index) {
  335. case KEY_SENDVIDEO: /* send or do not send video */
  336. env->out.sendvideo = !env->out.sendvideo;
  337. break;
  338. case KEY_PIP: /* enable or disable Picture in Picture */
  339. env->out.picture_in_picture = !env->out.picture_in_picture;
  340. break;
  341. case KEY_MUTE: /* send or do not send audio */
  342. ast_cli_command(env->gui->outfd, "console mute toggle");
  343. break;
  344. case KEY_FREEZE: /* freeze/unfreeze the incoming frames */
  345. env->frame_freeze = !env->frame_freeze;
  346. break;
  347. #ifdef notyet
  348. case KEY_AUTOANSWER: {
  349. struct chan_oss_pvt *o = find_desc(oss_active);
  350. o->autoanswer = !o->autoanswer;
  351. }
  352. break;
  353. #endif
  354. }
  355. return NULL;
  356. }
  357. char *console_do_answer(int fd);
  358. /*
  359. * Function called when the pick up button is pressed
  360. * perform actions according the channel status:
  361. *
  362. * - if no one is calling us and no digits was pressed,
  363. * the operation have no effects,
  364. * - if someone is calling us we answer to the call.
  365. * - if we have no call in progress and we pressed some
  366. * digit, send the digit to the console.
  367. */
  368. static void keypad_pick_up(struct video_desc *env)
  369. {
  370. struct gui_info *gui = env->gui;
  371. ast_log(LOG_WARNING, "keypad_pick_up called\n");
  372. if (env->owner) { /* someone is calling us, just answer */
  373. ast_cli_command(gui->outfd, "console answer");
  374. } else { /* we have someone to call */
  375. char buf[160];
  376. const char *who = ast_skip_blanks(read_message(gui->bd_msg));
  377. buf[sizeof(buf) - 1] = '\0';
  378. snprintf(buf, sizeof(buf), "console dial %s", who);
  379. ast_log(LOG_WARNING, "doing <%s>\n", buf);
  380. print_message(gui->bd_dialed, "\n");
  381. print_message(gui->bd_dialed, who);
  382. reset_board(gui->bd_msg);
  383. ast_cli_command(gui->outfd, buf);
  384. }
  385. }
  386. #if 0 /* still unused */
  387. /*
  388. * As an alternative to SDL_TTF, we can simply load the font from
  389. * an image and blit characters on the background of the GUI.
  390. *
  391. * To generate a font we can use the 'fly' command with the
  392. * following script (3 lines with 32 chars each)
  393. size 320,64
  394. name font.png
  395. transparent 0,0,0
  396. string 255,255,255, 0, 0,giant, !"#$%&'()*+,-./0123456789:;<=>?
  397. string 255,255,255, 0,20,giant,@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
  398. string 255,255,255, 0,40,giant,`abcdefghijklmnopqrstuvwxyz{|}~
  399. end
  400. */
  401. /* Print given text on the gui */
  402. static int gui_output(struct video_desc *env, const char *text)
  403. {
  404. return 1; /* error, not supported */
  405. }
  406. #endif
  407. static int video_geom(struct fbuf_t *b, const char *s);
  408. static void sdl_setup(struct video_desc *env);
  409. static int kp_match_area(const struct keypad_entry *e, int x, int y);
  410. static void set_drag(struct drag_info *drag, int x, int y, enum drag_window win)
  411. {
  412. drag->x_start = x;
  413. drag->y_start = y;
  414. drag->drag_window = win;
  415. }
  416. static int update_device_info(struct video_desc *env, int i)
  417. {
  418. reset_board(env->gui->thumb_bd_array[i].board);
  419. print_message(env->gui->thumb_bd_array[i].board,
  420. src_msgs[env->out.devices[i].status_index]);
  421. return 0;
  422. }
  423. /*! \brief Changes the video output (local video) source, controlling if
  424. * it is already using that video device,
  425. * and switching the correct fields of env->out.
  426. * grabbers are always open and saved in the device table.
  427. * The secondary or the primary device can be changed,
  428. * according to the "button" parameter:
  429. * the primary device is changed if button = SDL_BUTTON_LEFT;
  430. * the secondary device is changed if button = not SDL_BUTTON_LEFT;
  431. *
  432. * the correct message boards of the sources are also updated
  433. * with the new status
  434. *
  435. * \param env = pointer to the video environment descriptor
  436. * \param index = index of the device the caller wants to use are primary or secondary device
  437. * \param button = button clicked on the mouse
  438. *
  439. * returns 0 on success,
  440. * returns 1 on error
  441. */
  442. static int switch_video_out(struct video_desc *env, int index, Uint8 button)
  443. {
  444. int *p; /* pointer to the index of the device to select */
  445. if (index >= env->out.device_num) {
  446. ast_log(LOG_WARNING, "no devices\n");
  447. return 1;
  448. }
  449. /* select primary or secondary */
  450. p = (button == SDL_BUTTON_LEFT) ? &env->out.device_primary :
  451. &env->out.device_secondary;
  452. /* controls if the device is already selected */
  453. if (index == *p) {
  454. ast_log(LOG_WARNING, "device %s already selected\n", env->out.devices[index].name);
  455. return 0;
  456. }
  457. ast_log(LOG_WARNING, "switching to %s...\n", env->out.devices[index].name);
  458. /* already open */
  459. if (env->out.devices[index].grabber) {
  460. /* we also have to update the messages in the source
  461. message boards below the source windows */
  462. /* first we update the board of the previous source */
  463. if (p == &env->out.device_primary)
  464. env->out.devices[*p].status_index &= ~IS_PRIMARY;
  465. else
  466. env->out.devices[*p].status_index &= ~IS_SECONDARY;
  467. update_device_info(env, *p);
  468. /* update the index used as primary or secondary */
  469. *p = index;
  470. ast_log(LOG_WARNING, "done\n");
  471. /* then we update the board of the new primary or secondary source */
  472. if (p == &env->out.device_primary)
  473. env->out.devices[*p].status_index |= IS_PRIMARY;
  474. else
  475. env->out.devices[*p].status_index |= IS_SECONDARY;
  476. update_device_info(env, *p);
  477. return 0;
  478. }
  479. /* device is off, just do nothing */
  480. ast_log(LOG_WARNING, "device is down\n");
  481. return 1;
  482. }
  483. /*! \brief tries to switch the state of a device from on to off or off to on
  484. * we also have to update the status of the device and the correct message board
  485. *
  486. * \param index = the device that must be turned on or off
  487. * \param env = pointer to the video environment descriptor
  488. *
  489. * returns:
  490. * - 0 on falure switching from off to on
  491. * - 1 on success in switching from off to on
  492. * - 2 on success in switching from on to off
  493. */
  494. static int turn_on_off(int index, struct video_desc *env)
  495. {
  496. struct video_device *p = &env->out.devices[index];
  497. if (index >= env->out.device_num) {
  498. ast_log(LOG_WARNING, "no devices\n");
  499. return 0;
  500. }
  501. if (!p->grabber) { /* device off */
  502. void *g_data; /* result of grabber_open() */
  503. struct grab_desc *g;
  504. int i;
  505. /* see if the device can be used by one of the existing drivers */
  506. for (i = 0; (g = console_grabbers[i]); i++) {
  507. /* try open the device */
  508. g_data = g->open(p->name, &env->out.loc_src_geometry, env->out.fps);
  509. if (!g_data) /* no luck, try the next driver */
  510. continue;
  511. p->grabber = g;
  512. p->grabber_data = g_data;
  513. /* update the status of the source */
  514. p->status_index |= IS_ON;
  515. /* print the new message in the message board */
  516. update_device_info(env, index);
  517. return 1; /* open succeded */
  518. }
  519. return 0; /* failure */
  520. } else {
  521. /* the grabber must be closed */
  522. p->grabber_data = p->grabber->close(p->grabber_data);
  523. p->grabber = NULL;
  524. /* dev_buf is already freed by grabber->close() */
  525. p->dev_buf = NULL;
  526. /* update the status of the source */
  527. p->status_index &= ~IS_ON;
  528. /* print the new message in the message board */
  529. update_device_info(env, index);
  530. return 2; /* closed */
  531. }
  532. }
  533. /*
  534. * Handle SDL_MOUSEBUTTONDOWN type, finding the palette
  535. * index value and calling the right callback.
  536. *
  537. * x, y are referred to the upper left corner of the main SDL window.
  538. */
  539. static void handle_mousedown(struct video_desc *env, SDL_MouseButtonEvent button)
  540. {
  541. uint8_t index = KEY_OUT_OF_KEYPAD; /* the key or region of the display we clicked on */
  542. struct gui_info *gui = env->gui;
  543. int i; /* integer variable used as iterator */
  544. int x; /* integer variable usable as a container */
  545. /* total width of source device thumbnails */
  546. int src_wins_tot_w = env->out.device_num*(SRC_WIN_W+BORDER)+BORDER;
  547. /* x coordinate of the center of the keypad */
  548. int x0 = MAX(env->rem_dpy.w+gui->keypad->w/2+2*BORDER, src_wins_tot_w/2);
  549. #if 0
  550. ast_log(LOG_WARNING, "event %d %d have %d/%d regions at %p\n",
  551. button.x, button.y, gui->kp_used, gui->kp_size, gui->kp);
  552. #endif
  553. /* for each mousedown we end previous drag */
  554. gui->drag.drag_window = DRAG_NONE;
  555. /* define keypad boundary */
  556. /* XXX this should be extended for clicks on different audio device markers */
  557. if (button.y >= (env->out.device_num ? SRC_WIN_H+2*BORDER+SRC_MSG_BD_H : 0)) {
  558. /* if control reaches this point this means that the clicked point is
  559. below the row of the additional sources windows*/
  560. /* adjust the y coordinate as if additional devices windows were not present */
  561. button.y -= (env->out.device_num ? SRC_WIN_H+2*BORDER+SRC_MSG_BD_H : 0);
  562. if (button.y < BORDER)
  563. index = KEY_OUT_OF_KEYPAD;
  564. else if (button.y >= MAX(MAX(env->rem_dpy.h, env->loc_dpy.h), gui->keypad->h))
  565. index = KEY_OUT_OF_KEYPAD;
  566. else if (button.x < x0 - gui->keypad->w/2 - BORDER - env->rem_dpy.w)
  567. index = KEY_OUT_OF_KEYPAD;
  568. else if (button.x < x0 - gui->keypad->w/2 - BORDER)
  569. index = KEY_REM_DPY;
  570. else if (button.x < x0 - gui->keypad->w/2)
  571. index = KEY_OUT_OF_KEYPAD;
  572. else if (button.x >= x0 + gui->keypad->w/2 + BORDER + env->loc_dpy.w)
  573. index = KEY_OUT_OF_KEYPAD;
  574. else if (button.x >= x0 + gui->keypad->w/2 + BORDER)
  575. index = KEY_LOC_DPY;
  576. else if (button.x >= x0 + gui->keypad->w/2)
  577. index = KEY_OUT_OF_KEYPAD;
  578. else if (gui->kp) {
  579. /* we have to calculate the first coordinate
  580. inside the keypad before calling the kp_match_area*/
  581. int x_keypad = button.x - (x0 - gui->keypad->w/2);
  582. /* find the key clicked (if one was clicked) */
  583. for (i = 0; i < gui->kp_used; i++) {
  584. if (kp_match_area(&gui->kp[i],x_keypad, button.y - BORDER)) {
  585. index = gui->kp[i].c;
  586. break;
  587. }
  588. }
  589. }
  590. } else if (button.y < BORDER) {
  591. index = KEY_OUT_OF_KEYPAD;
  592. } else { /* we are in the thumbnail area */
  593. x = x0 - src_wins_tot_w/2 + BORDER;
  594. if (button.y >= BORDER + SRC_WIN_H)
  595. index = KEY_OUT_OF_KEYPAD;
  596. else if (button.x < x)
  597. index = KEY_OUT_OF_KEYPAD;
  598. else if (button.x < x + src_wins_tot_w - BORDER) {
  599. /* note that the additional device windows
  600. are numbered from left to right
  601. starting from 0, with a maximum of 8, the index associated on a click is:
  602. KEY_SRCS_WIN + number_of_the_window */
  603. for (i = 1; i <= env->out.device_num; i++) {
  604. if (button.x < x+i*(SRC_WIN_W+BORDER)-BORDER) {
  605. index = KEY_SRCS_WIN+i-1;
  606. break;
  607. } else if (button.x < x+i*(SRC_WIN_W+BORDER)) {
  608. index = KEY_OUT_OF_KEYPAD;
  609. break;
  610. }
  611. }
  612. } else
  613. index = KEY_OUT_OF_KEYPAD;
  614. }
  615. /* exec the function */
  616. if (index < 128) { /* surely clicked on the keypad, don't care which key */
  617. keypad_digit(env, index);
  618. return;
  619. }
  620. else if (index >= KEY_SRCS_WIN && index < KEY_SRCS_WIN+env->out.device_num) {
  621. index -= KEY_SRCS_WIN; /* index of the window, equal to the device index in the table */
  622. /* if one of the additional device windows is clicked with
  623. left or right mouse button, we have to switch to that device */
  624. if (button.button == SDL_BUTTON_RIGHT || button.button == SDL_BUTTON_LEFT) {
  625. switch_video_out(env, index, button.button);
  626. return;
  627. }
  628. /* turn on or off the devices selectively with other mouse buttons */
  629. else {
  630. int ret = turn_on_off(index, env);
  631. /* print a message according to what happened */
  632. if (!ret)
  633. ast_log(LOG_WARNING, "unable to turn on device %s\n",
  634. env->out.devices[index].name);
  635. else if (ret == 1)
  636. ast_log(LOG_WARNING, "device %s changed state to on\n",
  637. env->out.devices[index].name);
  638. else if (ret == 2)
  639. ast_log(LOG_WARNING, "device %s changed state to off\n",
  640. env->out.devices[index].name);
  641. return;
  642. }
  643. }
  644. /* XXX for future implementation
  645. else if (click on audio source marker)
  646. change audio source device
  647. */
  648. switch (index) {
  649. /* answer/close function */
  650. case KEY_PICK_UP:
  651. keypad_pick_up(env);
  652. break;
  653. case KEY_HANG_UP:
  654. ast_cli_command(gui->outfd, "console hangup");
  655. break;
  656. /* other functions */
  657. case KEY_MUTE: /* send or not send the audio */
  658. case KEY_AUTOANSWER:
  659. case KEY_SENDVIDEO: /* send or not send the video */
  660. case KEY_PIP: /* activate/deactivate picture in picture mode */
  661. case KEY_FREEZE: /* freeze/unfreeze the incoming video */
  662. keypad_toggle(env, index);
  663. break;
  664. case KEY_LOCALVIDEO:
  665. break;
  666. case KEY_REMOTEVIDEO:
  667. break;
  668. #ifdef notyet /* XXX for future implementations */
  669. case KEY_CAPTURE:
  670. break;
  671. #endif
  672. case KEY_MESSAGEBOARD:
  673. if (button.button == SDL_BUTTON_LEFT)
  674. set_drag(&gui->drag, button.x, button.y, DRAG_MESSAGE);
  675. break;
  676. /* press outside the keypad. right increases size, center decreases, left drags */
  677. case KEY_LOC_DPY:
  678. case KEY_REM_DPY:
  679. if (button.button == SDL_BUTTON_LEFT) {
  680. /* values used to find the position of the picture in picture (if present) */
  681. int pip_loc_x = (double)env->out.pip_x/env->enc_in.w * env->loc_dpy.w;
  682. int pip_loc_y = (double)env->out.pip_y/env->enc_in.h * env->loc_dpy.h;
  683. /* check if picture in picture is active and the click was on it */
  684. if (index == KEY_LOC_DPY && env->out.picture_in_picture &&
  685. button.x >= x0+gui->keypad->w/2+BORDER+pip_loc_x &&
  686. button.x < x0+gui->keypad->w/2+BORDER+pip_loc_x+env->loc_dpy.w/3 &&
  687. button.y >= BORDER+pip_loc_y &&
  688. button.y < BORDER+pip_loc_y+env->loc_dpy.h/3) {
  689. /* set the y cordinate to his previous value */
  690. button.y += (env->out.device_num ? SRC_WIN_H+2*BORDER+SRC_MSG_BD_H : 0);
  691. /* starts dragging the picture inside the picture */
  692. set_drag(&gui->drag, button.x, button.y, DRAG_PIP);
  693. }
  694. else if (index == KEY_LOC_DPY) {
  695. /* set the y cordinate to his previous value */
  696. button.y += (env->out.device_num ? SRC_WIN_H+2*BORDER+SRC_MSG_BD_H : 0);
  697. /* click in the local display, but not on the PiP */
  698. set_drag(&gui->drag, button.x, button.y, DRAG_LOCAL);
  699. }
  700. break;
  701. } else {
  702. char buf[128];
  703. struct fbuf_t *fb = index == KEY_LOC_DPY ? &env->loc_dpy : &env->rem_dpy;
  704. sprintf(buf, "%c%dx%d", button.button == SDL_BUTTON_RIGHT ? '>' : '<',
  705. fb->w, fb->h);
  706. video_geom(fb, buf);
  707. sdl_setup(env);
  708. /* writes messages in the source boards, those can be
  709. modified during the execution, because of the events
  710. this must be done here, otherwise the status of sources will not be
  711. shown after sdl_setup */
  712. for (i = 0; i < env->out.device_num; i++) {
  713. update_device_info(env, i);
  714. }
  715. /* we also have to refresh other boards,
  716. to avoid messages to disappear after video resize */
  717. print_message(gui->bd_msg, " \b");
  718. print_message(gui->bd_dialed, " \b");
  719. }
  720. break;
  721. case KEY_OUT_OF_KEYPAD:
  722. ast_log(LOG_WARNING, "nothing clicked, coordinates: %d, %d\n", button.x, button.y);
  723. break;
  724. case KEY_DIGIT_BACKGROUND:
  725. break;
  726. default:
  727. ast_log(LOG_WARNING, "function not yet defined %i\n", index);
  728. }
  729. }
  730. /*
  731. * Handle SDL_KEYDOWN type event, put the key pressed
  732. * in the dial buffer or in the text-message buffer,
  733. * depending on the text_mode variable value.
  734. *
  735. * key is the SDLKey structure corresponding to the key pressed.
  736. * Note that SDL returns modifiers (ctrl, shift, alt) as independent
  737. * information so the key itself is not enough and we need to
  738. * use a translation table, below - one line per entry,
  739. * plain, shift, ctrl, ... using the first char as key.
  740. */
  741. static const char * const us_kbd_map[] = {
  742. "`~", "1!", "2@", "3#", "4$", "5%", "6^",
  743. "7&", "8*", "9(", "0)", "-_", "=+", "[{",
  744. "]}", "\\|", ";:", "'\"", ",<", ".>", "/?",
  745. "jJ\n",
  746. NULL
  747. };
  748. static char map_key(SDL_keysym *ks)
  749. {
  750. const char *s, **p = us_kbd_map;
  751. int c = ks->sym;
  752. if (c == '\r') /* map cr into lf */
  753. c = '\n';
  754. if (c >= SDLK_NUMLOCK && c <= SDLK_COMPOSE)
  755. return 0; /* only a modifier */
  756. if (ks->mod == 0)
  757. return c;
  758. while ((s = *p) && s[0] != c)
  759. p++;
  760. if (s) { /* see if we have a modifier and a chance to use it */
  761. int l = strlen(s), mod = 0;
  762. if (l > 1)
  763. mod |= (ks->mod & KMOD_SHIFT) ? 1 : 0;
  764. if (l > 2 + mod)
  765. mod |= (ks->mod & KMOD_CTRL) ? 2 : 0;
  766. if (l > 4 + mod)
  767. mod |= (ks->mod & KMOD_ALT) ? 4 : 0;
  768. c = s[mod];
  769. }
  770. if (ks->mod & (KMOD_CAPS|KMOD_SHIFT) && c >= 'a' && c <='z')
  771. c += 'A' - 'a';
  772. return c;
  773. }
  774. static void handle_keyboard_input(struct video_desc *env, SDL_keysym *ks)
  775. {
  776. char buf[2] = { map_key(ks), '\0' };
  777. struct gui_info *gui = env->gui;
  778. if (buf[0] == 0) /* modifier ? */
  779. return;
  780. switch (gui->kb_output) {
  781. default:
  782. break;
  783. case KO_INPUT: /* to be completed */
  784. break;
  785. case KO_MESSAGE:
  786. if (gui->bd_msg) {
  787. print_message(gui->bd_msg, buf);
  788. if (buf[0] == '\r' || buf[0] == '\n') {
  789. keypad_pick_up(env);
  790. }
  791. }
  792. break;
  793. case KO_DIALED: /* to be completed */
  794. break;
  795. }
  796. return;
  797. }
  798. static void grabber_move(struct video_device *, int dx, int dy);
  799. int compute_drag(int *start, int end, int magnifier);
  800. int compute_drag(int *start, int end, int magnifier)
  801. {
  802. int delta = end - *start;
  803. #define POLARITY -1
  804. /* add a small quadratic term */
  805. delta += delta * delta * (delta > 0 ? 1 : -1 )/100;
  806. delta *= POLARITY * magnifier;
  807. #undef POLARITY
  808. *start = end;
  809. return delta;
  810. }
  811. /*! \brief This function moves the picture in picture,
  812. * controlling the limits of the containing buffer
  813. * to avoid problems deriving from going through the limits.
  814. *
  815. * \param env = pointer to the descriptor of the video environment
  816. * \param dx = the variation of the x position
  817. * \param dy = the variation of the y position
  818. */
  819. static void pip_move(struct video_desc* env, int dx, int dy) {
  820. int new_pip_x = env->out.pip_x+dx;
  821. int new_pip_y = env->out.pip_y+dy;
  822. /* going beyond the left borders */
  823. if (new_pip_x < 0)
  824. new_pip_x = 0;
  825. /* going beyond the right borders */
  826. else if (new_pip_x > env->enc_in.w - env->enc_in.w/3)
  827. new_pip_x = env->enc_in.w - env->enc_in.w/3;
  828. /* going beyond the top borders */
  829. if (new_pip_y < 0)
  830. new_pip_y = 0;
  831. /* going beyond the bottom borders */
  832. else if (new_pip_y > env->enc_in.h - env->enc_in.h/3)
  833. new_pip_y = env->enc_in.h - env->enc_in.h/3;
  834. env->out.pip_x = new_pip_x;
  835. env->out.pip_y = new_pip_y;
  836. }
  837. /*
  838. * I am seeing some kind of deadlock or stall around
  839. * SDL_PumpEvents() while moving the window on a remote X server
  840. * (both xfree-4.4.0 and xorg 7.2)
  841. * and windowmaker. It is unclear what causes it.
  842. */
  843. /*! \brief refresh the screen, and also grab a bunch of events.
  844. */
  845. static void eventhandler(struct video_desc *env, const char *caption)
  846. {
  847. struct gui_info *gui = env->gui;
  848. struct drag_info *drag;
  849. #define N_EVENTS 32
  850. int i, n;
  851. SDL_Event ev[N_EVENTS];
  852. if (!gui)
  853. return;
  854. drag = &gui->drag;
  855. if (caption)
  856. SDL_WM_SetCaption(caption, NULL);
  857. #define MY_EV (SDL_MOUSEBUTTONDOWN|SDL_KEYDOWN)
  858. while ( (n = SDL_PeepEvents(ev, N_EVENTS, SDL_GETEVENT, SDL_ALLEVENTS)) > 0) {
  859. for (i = 0; i < n; i++) {
  860. #if 0
  861. ast_log(LOG_WARNING, "------ event %d at %d %d\n",
  862. ev[i].type, ev[i].button.x, ev[i].button.y);
  863. #endif
  864. switch (ev[i].type) {
  865. default:
  866. ast_log(LOG_WARNING, "------ event %d at %d %d\n",
  867. ev[i].type, ev[i].button.x, ev[i].button.y);
  868. break;
  869. case SDL_ACTIVEEVENT:
  870. #if 0 /* do not react, we don't want to die because the window is minimized */
  871. if (ev[i].active.gain == 0 && ev[i].active.state & SDL_APPACTIVE) {
  872. ast_log(LOG_WARNING, "/* somebody has killed us ? */\n");
  873. ast_cli_command(gui->outfd, "stop now");
  874. }
  875. #endif
  876. break;
  877. case SDL_KEYUP: /* ignore, for the time being */
  878. break;
  879. case SDL_KEYDOWN:
  880. handle_keyboard_input(env, &ev[i].key.keysym);
  881. break;
  882. case SDL_MOUSEMOTION:
  883. case SDL_MOUSEBUTTONUP:
  884. if (drag->drag_window == DRAG_LOCAL && env->out.device_num) {
  885. /* move the capture source */
  886. int dx = compute_drag(&drag->x_start, ev[i].motion.x, 3);
  887. int dy = compute_drag(&drag->y_start, ev[i].motion.y, 3);
  888. grabber_move(&env->out.devices[env->out.device_primary], dx, dy);
  889. } else if (drag->drag_window == DRAG_PIP) {
  890. /* move the PiP image inside the frames of the enc_in buffers */
  891. int dx = ev[i].motion.x - drag->x_start;
  892. int dy = ev[i].motion.y - drag->y_start;
  893. /* dx and dy value are directly applied to env->out.pip_x and
  894. env->out.pip_y, so they must work as if the format was cif */
  895. dx = (double)dx*env->enc_in.w/env->loc_dpy.w;
  896. dy = (double)dy*env->enc_in.h/env->loc_dpy.h;
  897. /* sets starts to a new value */
  898. drag->x_start = ev[i].motion.x;
  899. drag->y_start = ev[i].motion.y;
  900. /* ast_log(LOG_WARNING, "moving: %d, %d\n", dx, dy); */
  901. pip_move(env, dx, dy);
  902. } else if (drag->drag_window == DRAG_MESSAGE) {
  903. /* scroll up/down the window */
  904. int dy = compute_drag(&drag->y_start, ev[i].motion.y, 1);
  905. move_message_board(gui->bd_msg, dy);
  906. }
  907. if (ev[i].type == SDL_MOUSEBUTTONUP)
  908. drag->drag_window = DRAG_NONE;
  909. break;
  910. case SDL_MOUSEBUTTONDOWN:
  911. handle_mousedown(env, ev[i].button);
  912. break;
  913. }
  914. }
  915. }
  916. if (1) {
  917. struct timeval b, a = ast_tvnow();
  918. int i;
  919. //SDL_Lock_EventThread();
  920. SDL_PumpEvents();
  921. b = ast_tvnow();
  922. i = ast_tvdiff_ms(b, a);
  923. if (i > 3)
  924. fprintf(stderr, "-------- SDL_PumpEvents took %dms\n", i);
  925. //SDL_Unlock_EventThread();
  926. }
  927. }
  928. static SDL_Surface *load_image(const char *file)
  929. {
  930. SDL_Surface *temp;
  931. #ifdef HAVE_SDL_IMAGE
  932. temp = IMG_Load(file);
  933. #else
  934. temp = SDL_LoadBMP(file);
  935. #endif
  936. if (temp == NULL)
  937. fprintf(stderr, "Unable to load image %s: %s\n",
  938. file, SDL_GetError());
  939. return temp;
  940. }
  941. static void keypad_setup(struct gui_info *gui, const char *kp_file);
  942. /* TODO: consistency checks, check for bpp, widht and height */
  943. /* Init the mask image used to grab the action. */
  944. static struct gui_info *gui_init(const char *keypad_file, const char *font)
  945. {
  946. struct gui_info *gui = ast_calloc(1, sizeof(*gui));
  947. if (gui == NULL)
  948. return NULL;
  949. /* initialize keypad status */
  950. gui->kb_output = KO_MESSAGE; /* XXX temp */
  951. gui->drag.drag_window = DRAG_NONE;
  952. gui->outfd = -1;
  953. keypad_setup(gui, keypad_file);
  954. if (gui->keypad == NULL) /* no keypad, we are done */
  955. return gui;
  956. /* XXX load image */
  957. if (!ast_strlen_zero(font)) {
  958. int i;
  959. SDL_Rect *r;
  960. gui->font = load_image(font);
  961. if (!gui->font) {
  962. ast_log(LOG_WARNING, "Unable to load font %s, no output available\n", font);
  963. goto error;
  964. }
  965. ast_log(LOG_WARNING, "Loaded font %s\n", font);
  966. /* XXX hardwired constants - 3 rows of 32 chars */
  967. r = gui->font_rects;
  968. #define FONT_H 20
  969. #define FONT_W 9
  970. for (i = 0; i < 96; r++, i++) {
  971. r->x = (i % 32 ) * FONT_W;
  972. r->y = (i / 32 ) * FONT_H;
  973. r->w = FONT_W;
  974. r->h = FONT_H;
  975. }
  976. }
  977. gui->outfd = open ("/dev/null", O_WRONLY); /* discard output, temporary */
  978. if (gui->outfd < 0) {
  979. ast_log(LOG_WARNING, "Unable output fd\n");
  980. goto error;
  981. }
  982. return gui;
  983. error:
  984. ast_free(gui);
  985. return NULL;
  986. }
  987. /* setup an sdl overlay and associated info, return 0 on success, != 0 on error */
  988. static int set_win(SDL_Surface *screen, struct display_window *win, int fmt,
  989. int w, int h, int x, int y)
  990. {
  991. win->bmp = SDL_CreateYUVOverlay(w, h, fmt, screen);
  992. if (win->bmp == NULL)
  993. return -1; /* error */
  994. win->rect.x = x;
  995. win->rect.y = y;
  996. win->rect.w = w;
  997. win->rect.h = h;
  998. return 0;
  999. }
  1000. static int keypad_cfg_read(struct gui_info *gui, const char *val);
  1001. static void keypad_setup(struct gui_info *gui, const char *kp_file)
  1002. {
  1003. FILE *fd;
  1004. char buf[1024];
  1005. const char region[] = "region";
  1006. int reg_len = strlen(region);
  1007. int in_comment = 0;
  1008. if (gui->keypad)
  1009. return;
  1010. gui->keypad = load_image(kp_file);
  1011. if (!gui->keypad)
  1012. return;
  1013. /* now try to read the keymap from the file. */
  1014. fd = fopen(kp_file, "r");
  1015. if (fd == NULL) {
  1016. ast_log(LOG_WARNING, "fail to open %s\n", kp_file);
  1017. return;
  1018. }
  1019. /*
  1020. * If the keypad image has a comment field, try to read
  1021. * the button location from there. The block must start with
  1022. * a comment (or empty) line, and continue with entries like:
  1023. * region = token shape x0 y0 x1 y1 h
  1024. * ...
  1025. * (basically, lines have the same format as config file entries).
  1026. * You can add it to a jpeg file using wrjpgcom
  1027. */
  1028. while (fgets(buf, sizeof(buf), fd)) {
  1029. char *s;
  1030. if (!strstr(buf, region)) { /* no keyword yet */
  1031. if (!in_comment) /* still waiting for initial comment block */
  1032. continue;
  1033. else
  1034. break;
  1035. }
  1036. if (!in_comment) { /* first keyword, reset previous entries */
  1037. keypad_cfg_read(gui, "reset");
  1038. in_comment = 1;
  1039. }
  1040. s = ast_skip_blanks(buf);
  1041. ast_trim_blanks(s);
  1042. if (memcmp(s, region, reg_len))
  1043. break; /* keyword not found */
  1044. s = ast_skip_blanks(s + reg_len); /* space between token and '=' */
  1045. if (*s++ != '=') /* missing separator */
  1046. break;
  1047. if (*s == '>') /* skip '>' if present */
  1048. s++;
  1049. keypad_cfg_read(gui, ast_skip_blanks(s));
  1050. }
  1051. fclose(fd);
  1052. }
  1053. struct board *board_setup(SDL_Surface *screen, SDL_Rect *dest,
  1054. SDL_Surface *font, SDL_Rect *font_rects);
  1055. /*! \brief initialize the boards we have in the keypad */
  1056. static void init_board(struct gui_info *gui, struct board **dst, SDL_Rect *r, int dx, int dy)
  1057. {
  1058. if (r[0].w == 0 || r[0].h == 0)
  1059. return; /* not available */
  1060. r[1] = r[0]; /* copy geometry */
  1061. r[1].x += dx; /* add offset of main window */
  1062. r[1].y += dy;
  1063. if (*dst == NULL) { /* initial call */
  1064. *dst = board_setup(gui->screen, &r[1], gui->font, gui->font_rects);
  1065. } else {
  1066. /* call a refresh */
  1067. }
  1068. }
  1069. #ifdef HAVE_X11
  1070. /*
  1071. * SDL is not very robust on error handling, so we need to trap ourselves
  1072. * at least the most obvious failure conditions, e.g. a bad SDL_WINDOWID.
  1073. * As of sdl-1.2.13, SDL_SetVideoMode crashes with bad parameters, so
  1074. * we need to do the explicit X calls to make sure the window is correct.
  1075. * And around these calls, we must trap X errors.
  1076. */
  1077. static int my_x_handler(Display *d, XErrorEvent *e)
  1078. {
  1079. ast_log(LOG_WARNING, "%s error_code %d\n", __FUNCTION__, e->error_code);
  1080. return 0;
  1081. }
  1082. #endif /* HAVE_X11 */
  1083. /*! \brief [re]set the main sdl window, useful in case of resize.
  1084. * We can tell the first from subsequent calls from the value of
  1085. * env->gui, which is NULL the first time.
  1086. */
  1087. static void sdl_setup(struct video_desc *env)
  1088. {
  1089. int dpy_fmt = SDL_IYUV_OVERLAY; /* YV12 causes flicker in SDL */
  1090. int depth, maxw, maxh;
  1091. const SDL_VideoInfo *info;
  1092. int kp_w = 0, kp_h = 0; /* keypad width and height */
  1093. struct gui_info *gui = env->gui;
  1094. /* Some helper variables used for filling the SDL window */
  1095. int x0; /* the x coordinate of the center of the keypad */
  1096. int x1; /* userful for calculating of the size of the parent window */
  1097. int y0; /* y coordinate of the keypad, the remote window and the local window */
  1098. int src_wins_tot_w; /* total width of the source windows */
  1099. int i;
  1100. int x; /* useful for the creation of the source windows; */
  1101. #ifdef HAVE_X11
  1102. const char *e = getenv("SDL_WINDOWID");
  1103. if (!ast_strlen_zero(e)) {
  1104. XWindowAttributes a;
  1105. int (*old_x_handler)(Display *d, XErrorEvent *e) = XSetErrorHandler(my_x_handler);
  1106. Display *d = XOpenDisplay(getenv("DISPLAY"));
  1107. long w = atol(e);
  1108. int success = w ? XGetWindowAttributes(d, w, &a) : 0;
  1109. XSetErrorHandler(old_x_handler);
  1110. if (!success) {
  1111. ast_log(LOG_WARNING, "%s error in window\n", __FUNCTION__);
  1112. return;
  1113. }
  1114. }
  1115. #endif
  1116. /*
  1117. * initialize the SDL environment. We have one large window
  1118. * with local and remote video, and a keypad.
  1119. * At the moment we arrange them statically, as follows:
  1120. * - top row: thumbnails for local video sources;
  1121. * - next row: message boards for local video sources
  1122. * - on the left, the remote video;
  1123. * - on the center, the keypad
  1124. * - on the right, the local video
  1125. * We need to read in the skin for the keypad before creating the main
  1126. * SDL window, because the size is only known here.
  1127. */
  1128. if (gui == NULL && SDL_Init(SDL_INIT_VIDEO)) {
  1129. ast_log(LOG_WARNING, "Could not initialize SDL - %s\n",
  1130. SDL_GetError());
  1131. /* again not fatal, just we won't display anything */
  1132. return;
  1133. }
  1134. info = SDL_GetVideoInfo();
  1135. /* We want at least 16bpp to support YUV overlays.
  1136. * E.g with SDL_VIDEODRIVER = aalib the default is 8
  1137. */
  1138. if (!info || !info->vfmt) {
  1139. ast_log(LOG_WARNING, "Bad SDL_GetVideoInfo - %s\n",
  1140. SDL_GetError());
  1141. return;
  1142. }
  1143. depth = info->vfmt->BitsPerPixel;
  1144. if (depth < 16)
  1145. depth = 16;
  1146. if (!gui)
  1147. env->gui = gui = gui_init(env->keypad_file, env->keypad_font);
  1148. if (!gui)
  1149. goto no_sdl;
  1150. if (gui->keypad) {
  1151. if (gui->kp_rect.w > 0 && gui->kp_rect.h > 0) {
  1152. kp_w = gui->kp_rect.w;
  1153. kp_h = gui->kp_rect.h;
  1154. } else {
  1155. kp_w = gui->keypad->w;
  1156. kp_h = gui->keypad->h;
  1157. }
  1158. }
  1159. /* total width of the thumbnails */
  1160. src_wins_tot_w = env->out.device_num*(SRC_WIN_W+BORDER)+BORDER;
  1161. /* x coordinate of the center of the keypad */
  1162. x0 = MAX(env->rem_dpy.w+kp_w/2+2*BORDER, src_wins_tot_w/2);
  1163. /* from center of the keypad to right border */
  1164. x1 = MAX(env->loc_dpy.w+kp_w/2+2*BORDER, src_wins_tot_w/2);
  1165. /* total width of the SDL window to create */
  1166. maxw = x0+x1;
  1167. /* total height of the mother window to create */
  1168. maxh = MAX( MAX(env->rem_dpy.h, env->loc_dpy.h), kp_h)+2*BORDER;
  1169. maxh += env->out.device_num ? (2*BORDER+SRC_WIN_H+SRC_MSG_BD_H) : 0;
  1170. gui->screen = SDL_SetVideoMode(maxw, maxh, depth, 0);
  1171. if (!gui->screen) {
  1172. ast_log(LOG_ERROR, "SDL: could not set video mode - exiting\n");
  1173. goto no_sdl;
  1174. }
  1175. #ifdef HAVE_X11
  1176. /*
  1177. * Annoying as it may be, if SDL_WINDOWID is set, SDL does
  1178. * not grab keyboard/mouse events or expose or other stuff,
  1179. * and it does not handle resize either.
  1180. * So we need to implement workarounds here.
  1181. */
  1182. do {
  1183. /* First, handle the event mask */
  1184. XWindowAttributes attr;
  1185. long want;
  1186. SDL_SysWMinfo info;
  1187. Display *SDL_Display;
  1188. Window win;
  1189. const char *e = getenv("SDL_WINDOWID");
  1190. if (ast_strlen_zero(e)) /* no external window, don't bother doing this */
  1191. break;
  1192. SDL_VERSION(&info.version); /* it is important to set the version */
  1193. if (SDL_GetWMInfo(&info) != 1) {
  1194. fprintf(stderr, "no wm info\n");
  1195. break;
  1196. }
  1197. SDL_Display = info.info.x11.display;
  1198. if (SDL_Display == NULL)
  1199. break;
  1200. win = info.info.x11.window;
  1201. /*
  1202. * A list of events we want.
  1203. * Leave ResizeRedirectMask to the parent.
  1204. */
  1205. want = KeyPressMask | KeyReleaseMask | ButtonPressMask |
  1206. ButtonReleaseMask | EnterWindowMask |
  1207. LeaveWindowMask | PointerMotionMask |
  1208. Button1MotionMask |
  1209. Button2MotionMask | Button3MotionMask |
  1210. Button4MotionMask | Button5MotionMask |
  1211. ButtonMotionMask | KeymapStateMask |
  1212. ExposureMask | VisibilityChangeMask |
  1213. StructureNotifyMask | /* ResizeRedirectMask | */
  1214. SubstructureNotifyMask | SubstructureRedirectMask |
  1215. FocusChangeMask | PropertyChangeMask |
  1216. ColormapChangeMask | OwnerGrabButtonMask;
  1217. memset(&attr, '\0', sizeof(attr));
  1218. XGetWindowAttributes(SDL_Display, win, &attr);
  1219. /* the following events can be delivered only to one client.
  1220. * So check which ones are going to someone else, and drop
  1221. * them from our request.
  1222. */
  1223. {
  1224. /* ev are the events for a single recipient */
  1225. long ev = ButtonPressMask | ResizeRedirectMask |
  1226. SubstructureRedirectMask;
  1227. ev &= (attr.all_event_masks & ~attr.your_event_mask);
  1228. /* now ev contains 1 for single-recipient events owned by others.
  1229. * We must clear those bits in 'want'
  1230. * and then add the bits in 'attr.your_event_mask' to 'want'
  1231. */
  1232. want &= ~ev;
  1233. want |= attr.your_event_mask;
  1234. }
  1235. XSelectInput(SDL_Display, win, want);
  1236. /* Second, handle resize.
  1237. * We do part of the things that X11Resize does,
  1238. * but also generate a ConfigureNotify event so
  1239. * the owner of the window has a chance to do something
  1240. * with it.
  1241. */
  1242. XResizeWindow(SDL_Display, win, maxw, maxh);
  1243. {
  1244. XConfigureEvent ce = {
  1245. .type = ConfigureNotify,
  1246. .serial = 0,
  1247. .send_event = 1, /* TRUE */
  1248. .display = SDL_Display,
  1249. .event = win,
  1250. .window = win,
  1251. .x = 0,
  1252. .y = 0,
  1253. .width = maxw,
  1254. .height = maxh,
  1255. .border_width = 0,
  1256. .above = 0,
  1257. .override_redirect = 0 };
  1258. XSendEvent(SDL_Display, win, 1 /* TRUE */, StructureNotifyMask, (XEvent *)&ce);
  1259. }
  1260. } while (0);
  1261. #endif /* HAVE_X11 */
  1262. y0 = env->out.device_num ? (3*BORDER+SRC_WIN_H+SRC_MSG_BD_H) : BORDER;
  1263. SDL_WM_SetCaption("Asterisk console Video Output", NULL);
  1264. /* intialize the windows for local and remote video */
  1265. if (set_win(gui->screen, &gui->win[WIN_REMOTE], dpy_fmt,
  1266. env->rem_dpy.w, env->rem_dpy.h, x0-kp_w/2-BORDER-env->rem_dpy.w, y0))
  1267. goto no_sdl;
  1268. /* unfreeze incoming frames if set (to avoid showing nothing) */
  1269. env->frame_freeze = 0;
  1270. if (set_win(gui->screen, &gui->win[WIN_LOCAL], dpy_fmt,
  1271. env->loc_dpy.w, env->loc_dpy.h,
  1272. x0+kp_w/2+BORDER, y0))
  1273. goto no_sdl;
  1274. /* initialize device_num source windows (thumbnails) and boards
  1275. (for a maximum of 9 additional windows and boards) */
  1276. x = x0 - src_wins_tot_w/2 + BORDER;
  1277. for (i = 0; i < env->out.device_num; i++){
  1278. struct thumb_bd *p = &gui->thumb_bd_array[i];
  1279. if (set_win(gui->screen, &gui->win[i+WIN_SRC1], dpy_fmt,
  1280. SRC_WIN_W, SRC_WIN_H, x+i*(BORDER+SRC_WIN_W), BORDER))
  1281. goto no_sdl;
  1282. /* set geometry for the rect for the message board of the device */
  1283. p->rect.w = SRC_WIN_W;
  1284. p->rect.h = SRC_MSG_BD_H;
  1285. p->rect.x = x+i*(BORDER+SRC_WIN_W);
  1286. p->rect.y = 2*BORDER+SRC_WIN_H;
  1287. /* the white color is used as background */
  1288. SDL_FillRect(gui->screen, &p->rect,
  1289. SDL_MapRGB(gui->screen->format, 255, 255, 255));
  1290. /* if necessary, initialize boards for the sources */
  1291. if (!p->board)
  1292. p->board =
  1293. board_setup(gui->screen, &p->rect,
  1294. gui->font, gui->font_rects);
  1295. /* update board rect */
  1296. SDL_UpdateRect(gui->screen, p->rect.x, p->rect.y, p->rect.w, p->rect.h);
  1297. }
  1298. /* display the skin, but do not free it as we need it later to
  1299. restore text areas and maybe sliders too */
  1300. if (gui->keypad) {
  1301. struct SDL_Rect *dest = &gui->win[WIN_KEYPAD].rect;
  1302. struct SDL_Rect *src = (gui->kp_rect.w > 0 && gui->kp_rect.h > 0) ? & gui->kp_rect : NULL;
  1303. /* set the coordinates of the keypad relative to the main screen */
  1304. dest->x = x0-kp_w/2;
  1305. dest->y = y0;
  1306. dest->w = kp_w;
  1307. dest->h = kp_h;
  1308. SDL_BlitSurface(gui->keypad, src, gui->screen, dest);
  1309. init_board(gui, &gui->bd_msg, gui->kp_msg, dest->x, dest->y);
  1310. init_board(gui, &gui->bd_dialed, gui->kp_dialed, dest->x, dest->y);
  1311. SDL_UpdateRects(gui->screen, 1, dest);
  1312. }
  1313. return;
  1314. no_sdl:
  1315. /* free resources in case of errors */
  1316. env->gui = cleanup_sdl(gui, env->out.device_num);
  1317. }
  1318. /*
  1319. * Functions to determine if a point is within a region. Return 1 if success.
  1320. * First rotate the point, with
  1321. * x' = (x - x0) * cos A + (y - y0) * sin A
  1322. * y' = -(x - x0) * sin A + (y - y0) * cos A
  1323. * where cos A = (x1-x0)/l, sin A = (y1 - y0)/l, and
  1324. * l = sqrt( (x1-x0)^2 + (y1-y0)^2
  1325. * Then determine inclusion by simple comparisons i.e.:
  1326. * rectangle: x >= 0 && x < l && y >= 0 && y < h
  1327. * ellipse: (x-xc)^2/l^2 + (y-yc)^2/h2 < 1
  1328. */
  1329. static int kp_match_area(const struct keypad_entry *e, int x, int y)
  1330. {
  1331. double xp, dx = (e->x1 - e->x0);
  1332. double yp, dy = (e->y1 - e->y0);
  1333. double l = sqrt(dx*dx + dy*dy);
  1334. int ret = 0;
  1335. if (l > 1) { /* large enough */
  1336. xp = ((x - e->x0)*dx + (y - e->y0)*dy)/l;
  1337. yp = (-(x - e->x0)*dy + (y - e->y0)*dx)/l;
  1338. if (e->type == KP_RECT) {
  1339. ret = (xp >= 0 && xp < l && yp >=0 && yp < e->h);
  1340. } else if (e->type == KP_CIRCLE) {
  1341. dx = xp*xp/(l*l) + yp*yp/(e->h*e->h);
  1342. ret = (dx < 1);
  1343. }
  1344. }
  1345. #if 0
  1346. ast_log(LOG_WARNING, "result %d [%d] for match %d,%d in type %d p0 %d,%d p1 %d,%d h %d\n",
  1347. ret, e->c, x, y, e->type, e->x0, e->y0, e->x1, e->y1, e->h);
  1348. #endif
  1349. return ret;
  1350. }
  1351. struct _s_k { const char *s; int k; };
  1352. static const struct _s_k gui_key_map[] = {
  1353. {"FREEZE", KEY_FREEZE},
  1354. {"PIP", KEY_PIP},
  1355. {"PICK_UP", KEY_PICK_UP },
  1356. {"PICKUP", KEY_PICK_UP },
  1357. {"HANG_UP", KEY_HANG_UP },
  1358. {"HANGUP", KEY_HANG_UP },
  1359. {"MUTE", KEY_MUTE },
  1360. {"FLASH", KEY_FLASH },
  1361. {"AUTOANSWER", KEY_AUTOANSWER },
  1362. {"SENDVIDEO", KEY_SENDVIDEO },
  1363. {"LOCALVIDEO", KEY_LOCALVIDEO },
  1364. {"REMOTEVIDEO", KEY_REMOTEVIDEO },
  1365. {"GUI_CLOSE", KEY_GUI_CLOSE },
  1366. {"MESSAGEBOARD", KEY_MESSAGEBOARD },
  1367. {"DIALEDBOARD", KEY_DIALEDBOARD },
  1368. {"EDITBOARD", KEY_EDITBOARD },
  1369. {"KEYPAD", KEY_KEYPAD }, /* x0 y0 w h - active area of the keypad */
  1370. {"MESSAGE", KEY_MESSAGE }, /* x0 y0 w h - incoming messages */
  1371. {"DIALED", KEY_DIALED }, /* x0 y0 w h - dialed number */
  1372. {"EDIT", KEY_EDIT }, /* x0 y0 w h - edit user input */
  1373. {"FONT", KEY_FONT }, /* x0 yo w h rows cols - location and format of the font */
  1374. {NULL, 0 } };
  1375. static int gui_map_token(const char *s)
  1376. {
  1377. /* map the string into token to be returned */
  1378. int i = atoi(s);
  1379. struct _s_k *p;
  1380. if (i > 0 || s[1] == '\0') /* numbers or single characters */
  1381. return (i > 9) ? i : s[0];
  1382. for (p = gui_key_map; p->s; p++) {
  1383. if (!strcasecmp(p->s, s))
  1384. return p->k;
  1385. }
  1386. return KEY_NONE; /* not found */
  1387. }
  1388. /*! \brief read a keypad entry line in the format
  1389. * reset
  1390. * token circle xc yc diameter
  1391. * token circle xc yc x1 y1 h # ellipse, main diameter and height
  1392. * token rect x0 y0 x1 y1 h # rectangle with main side and eight
  1393. * token x0 y0 w h # horizontal rectangle (short format)
  1394. * # this is used e.g. for message boards
  1395. * token is the token to be returned, either a character or a symbol
  1396. * as KEY_* above
  1397. * Return 1 on success, 0 on error.
  1398. */
  1399. static int keypad_cfg_read(struct gui_info *gui, const char *val)
  1400. {
  1401. struct keypad_entry e;
  1402. SDL_Rect *r = NULL;
  1403. char s1[16], s2[16];
  1404. int i, ret = 0; /* default, error */
  1405. if (gui == NULL || val == NULL)
  1406. return 0;
  1407. s1[0] = s2[0] = '\0';
  1408. memset(&e, '\0', sizeof(e));
  1409. i = sscanf(val, "%14s %14s %d %d %d %d %d",
  1410. s1, s2, &e.x0, &e.y0, &e.x1, &e.y1, &e.h);
  1411. e.c = gui_map_token(s1);
  1412. if (e.c == KEY_NONE)
  1413. return 0; /* nothing found */
  1414. switch (i) {
  1415. default:
  1416. break;
  1417. case 1: /* only "reset" is allowed */
  1418. if (e.c != KEY_RESET)
  1419. break;
  1420. if (gui->kp)
  1421. gui->kp_used = 0;
  1422. break;
  1423. case 5:
  1424. if (e.c == KEY_KEYPAD) /* active keypad area */
  1425. r = &gui->kp_rect;
  1426. else if (e.c == KEY_MESSAGE)
  1427. r = gui->kp_msg;
  1428. else if (e.c == KEY_DIALED)
  1429. r = gui->kp_dialed;
  1430. else if (e.c == KEY_EDIT)
  1431. r = gui->kp_edit;
  1432. if (r) {
  1433. r->x = atoi(s2); /* this becomes x0 */
  1434. r->y = e.x0; /* this becomes y0 */
  1435. r->w = e.y0; /* this becomes w */
  1436. r->h = e.x1; /* this becomes h */
  1437. break;
  1438. }
  1439. if (strcasecmp(s2, "circle")) /* invalid */
  1440. break;
  1441. /* token circle xc yc diameter */
  1442. e.h = e.x1;
  1443. e.y1 = e.y0; /* map radius in x1 y1 */
  1444. e.x1 = e.x0 + e.h; /* map radius in x1 y1 */
  1445. e.x0 = e.x0 - e.h; /* map radius in x1 y1 */
  1446. /* fallthrough */
  1447. case 7:
  1448. if (e.c == KEY_FONT) { /* font - x0 y0 w h rows cols */
  1449. ast_log(LOG_WARNING, "font not supported yet\n");
  1450. break;
  1451. }
  1452. /* token circle|rect x0 y0 x1 y1 h */
  1453. if (e.x1 < e.x0 || e.h <= 0) {
  1454. ast_log(LOG_WARNING, "error in coordinates\n");
  1455. e.type = 0;
  1456. break;
  1457. }
  1458. if (!strcasecmp(s2, "circle")) {
  1459. /* for a circle we specify the diameter but store center and radii */
  1460. e.type = KP_CIRCLE;
  1461. e.x0 = (e.x1 + e.x0) / 2;
  1462. e.y0 = (e.y1 + e.y0) / 2;
  1463. e.h = e.h / 2;
  1464. } else if (!strcasecmp(s2, "rect")) {
  1465. e.type = KP_RECT;
  1466. } else
  1467. break;
  1468. ret = 1;
  1469. }
  1470. // ast_log(LOG_WARNING, "reading [%s] returns %d %d\n", val, i, ret);
  1471. if (ret == 0)
  1472. return 0;
  1473. if (gui->kp_size == 0) {
  1474. gui->kp = ast_calloc(10, sizeof(e));
  1475. if (gui->kp == NULL) {
  1476. ast_log(LOG_WARNING, "cannot allocate kp\n");
  1477. return 0;
  1478. }
  1479. gui->kp_size = 10;
  1480. }
  1481. if (gui->kp_size == gui->kp_used) { /* must allocate */
  1482. struct keypad_entry *a = ast_realloc(gui->kp, sizeof(e)*(gui->kp_size+10));
  1483. if (a == NULL) {
  1484. ast_log(LOG_WARNING, "cannot reallocate kp\n");
  1485. return 0;
  1486. }
  1487. gui->kp = a;
  1488. gui->kp_size += 10;
  1489. }
  1490. if (gui->kp_size == gui->kp_used)
  1491. return 0;
  1492. gui->kp[gui->kp_used++] = e;
  1493. // ast_log(LOG_WARNING, "now %d regions\n", gui->kp_used);
  1494. return 1;
  1495. }
  1496. #endif /* HAVE_SDL */