pbx_gtkconsole.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * GTK Console monitor -- very kludgy right now
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. /*
  14. * I know this might seem somewhat pointless in its current phase, but one
  15. * of the most important parts of this module is demonstrate that modules
  16. * can require other external libraries and still be loaded (in this
  17. * case, a host of libraries involving gtk), so long as they are properly
  18. * linked (see the Makefile)
  19. */
  20. #include <sys/types.h>
  21. #include <asterisk/pbx.h>
  22. #include <asterisk/config.h>
  23. #include <asterisk/module.h>
  24. #include <asterisk/logger.h>
  25. #include <asterisk/options.h>
  26. #include <asterisk/cli.h>
  27. #include <asterisk/utils.h>
  28. #include <stdlib.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdarg.h>
  34. #include <signal.h>
  35. #include <sys/time.h>
  36. #include <gtk/gtk.h>
  37. #include <glib.h>
  38. /* For where to put dynamic tables */
  39. #include "../asterisk.h"
  40. #include "../astconf.h"
  41. AST_MUTEX_DEFINE_STATIC(verb_lock);
  42. static pthread_t console_thread;
  43. static int inuse=0;
  44. static int clipipe[2];
  45. static int cleanupid = -1;
  46. static char *dtext = "Asterisk PBX Console (GTK Version)";
  47. static GtkWidget *window;
  48. static GtkWidget *quit;
  49. static GtkWidget *closew;
  50. static GtkWidget *verb;
  51. static GtkWidget *modules;
  52. static GtkWidget *statusbar;
  53. static GtkWidget *cli;
  54. static struct timeval last;
  55. static void update_statusbar(char *msg)
  56. {
  57. gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 1);
  58. gtk_statusbar_push(GTK_STATUSBAR(statusbar), 1, msg);
  59. }
  60. int unload_module(void)
  61. {
  62. if (inuse) {
  63. /* Kill off the main thread */
  64. pthread_cancel(console_thread);
  65. gdk_threads_enter();
  66. gtk_widget_destroy(window);
  67. gdk_threads_leave();
  68. close(clipipe[0]);
  69. close(clipipe[1]);
  70. }
  71. return 0;
  72. }
  73. static int cleanup(void *useless)
  74. {
  75. gdk_threads_enter();
  76. gtk_clist_thaw(GTK_CLIST(verb));
  77. gtk_widget_queue_resize(verb->parent);
  78. gtk_clist_moveto(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1, 0, 0, 0);
  79. cleanupid = -1;
  80. gdk_threads_leave();
  81. return 0;
  82. }
  83. static void __verboser(const char *stuff, int opos, int replacelast, int complete)
  84. {
  85. char *s2[2];
  86. struct timeval tv;
  87. int ms;
  88. s2[0] = (char *)stuff;
  89. s2[1] = NULL;
  90. gtk_clist_freeze(GTK_CLIST(verb));
  91. if (replacelast)
  92. gtk_clist_remove(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1);
  93. gtk_clist_append(GTK_CLIST(verb), s2);
  94. if (last.tv_sec || last.tv_usec) {
  95. gdk_threads_leave();
  96. gettimeofday(&tv, NULL);
  97. if (cleanupid > -1)
  98. gtk_timeout_remove(cleanupid);
  99. ms = (tv.tv_sec - last.tv_sec) * 1000 + (tv.tv_usec - last.tv_usec) / 1000;
  100. if (ms < 100) {
  101. /* We just got a message within 100ms, so just schedule an update
  102. in the near future */
  103. cleanupid = gtk_timeout_add(200, cleanup, NULL);
  104. } else {
  105. cleanup(&cleanupid);
  106. }
  107. last = tv;
  108. } else {
  109. gettimeofday(&last, NULL);
  110. }
  111. }
  112. static void verboser(const char *stuff, int opos, int replacelast, int complete)
  113. {
  114. ast_mutex_lock(&verb_lock);
  115. /* Lock appropriately if we're really being called in verbose mode */
  116. __verboser(stuff, opos, replacelast, complete);
  117. ast_mutex_unlock(&verb_lock);
  118. }
  119. static void cliinput(void *data, int source, GdkInputCondition ic)
  120. {
  121. static char buf[256];
  122. static int offset = 0;
  123. int res;
  124. char *c;
  125. char *l;
  126. char n;
  127. /* Read as much stuff is there */
  128. res = read(source, buf + offset, sizeof(buf) - 1 - offset);
  129. if (res > -1)
  130. buf[res + offset] = '\0';
  131. /* make sure we've null terminated whatever we have so far */
  132. c = buf;
  133. l = buf;
  134. while(*c) {
  135. if (*c == '\n') {
  136. /* Keep the trailing \n */
  137. c++;
  138. n = *c;
  139. *c = '\0';
  140. __verboser(l, 0, 0, 1);
  141. *(c - 1) = '\0';
  142. *c = n;
  143. l = c;
  144. } else
  145. c++;
  146. }
  147. if (strlen(l)) {
  148. /* We have some left over */
  149. memmove(buf, l, strlen(l) + 1);
  150. offset = strlen(buf);
  151. } else {
  152. offset = 0;
  153. }
  154. }
  155. static void remove_module(void)
  156. {
  157. int res;
  158. char *module;
  159. char buf[256];
  160. if (GTK_CLIST(modules)->selection) {
  161. module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (int) GTK_CLIST(modules)->selection->data);
  162. gdk_threads_leave();
  163. res = ast_unload_resource(module, 0);
  164. gdk_threads_enter();
  165. if (res) {
  166. snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
  167. update_statusbar(buf);
  168. } else {
  169. snprintf(buf, sizeof(buf), "Module '%s' removed", module);
  170. update_statusbar(buf);
  171. }
  172. }
  173. }
  174. static void reload_module(void)
  175. {
  176. int res, x;
  177. char *module;
  178. char buf[256];
  179. if (GTK_CLIST(modules)->selection) {
  180. module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (int) GTK_CLIST(modules)->selection->data);
  181. module = strdup(module);
  182. if (module) {
  183. gdk_threads_leave();
  184. res = ast_unload_resource(module, 0);
  185. gdk_threads_enter();
  186. if (res) {
  187. snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
  188. update_statusbar(buf);
  189. } else {
  190. gdk_threads_leave();
  191. res = ast_load_resource(module);
  192. gdk_threads_enter();
  193. if (res) {
  194. snprintf(buf, sizeof(buf), "Error reloading module '%s'", module);
  195. } else {
  196. snprintf(buf, sizeof(buf), "Module '%s' reloaded", module);
  197. }
  198. for (x=0; x < GTK_CLIST(modules)->rows; x++) {
  199. if (!strcmp((char *)gtk_clist_get_row_data(GTK_CLIST(modules), x), module)) {
  200. gtk_clist_select_row(GTK_CLIST(modules), x, -1);
  201. break;
  202. }
  203. }
  204. update_statusbar(buf);
  205. }
  206. free(module);
  207. }
  208. }
  209. }
  210. static void file_ok_sel(GtkWidget *w, GtkFileSelection *fs)
  211. {
  212. char tmp[AST_CONFIG_MAX_PATH];
  213. char *module = gtk_file_selection_get_filename(fs);
  214. char buf[256];
  215. snprintf(tmp, sizeof(tmp), "%s/", ast_config_AST_MODULE_DIR);
  216. if (!strncmp(module, (char *)tmp, strlen(tmp)))
  217. module += strlen(tmp);
  218. gdk_threads_leave();
  219. if (ast_load_resource(module)) {
  220. snprintf(buf, sizeof(buf), "Error loading module '%s'.", module);
  221. update_statusbar(buf);
  222. } else {
  223. snprintf(buf, sizeof(buf), "Module '%s' loaded", module);
  224. update_statusbar(buf);
  225. }
  226. gdk_threads_enter();
  227. gtk_widget_destroy(GTK_WIDGET(fs));
  228. }
  229. static void add_module(void)
  230. {
  231. char tmp[AST_CONFIG_MAX_PATH];
  232. GtkWidget *filew;
  233. snprintf(tmp, sizeof(tmp), "%s/*.so", ast_config_AST_MODULE_DIR);
  234. filew = gtk_file_selection_new("Load Module");
  235. gtk_signal_connect(GTK_OBJECT (GTK_FILE_SELECTION(filew)->ok_button),
  236. "clicked", GTK_SIGNAL_FUNC(file_ok_sel), filew);
  237. gtk_signal_connect_object(GTK_OBJECT (GTK_FILE_SELECTION(filew)->cancel_button),
  238. "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(filew));
  239. gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), (char *)tmp);
  240. gtk_widget_show(filew);
  241. }
  242. static int add_mod(char *module, char *description, int usecount)
  243. {
  244. char use[10];
  245. char *pass[4];
  246. int row;
  247. snprintf(use, sizeof(use), "%d", usecount);
  248. pass[0] = module;
  249. pass[1] = description;
  250. pass[2] = use;
  251. pass[3] = NULL;
  252. row = gtk_clist_append(GTK_CLIST(modules), pass);
  253. gtk_clist_set_row_data(GTK_CLIST(modules), row, module);
  254. return 0;
  255. }
  256. static int mod_update(void)
  257. {
  258. char *module= NULL;
  259. /* Update the mod stuff */
  260. if (GTK_CLIST(modules)->selection) {
  261. module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (int) GTK_CLIST(modules)->selection->data);
  262. }
  263. gtk_clist_freeze(GTK_CLIST(modules));
  264. gtk_clist_clear(GTK_CLIST(modules));
  265. ast_update_module_list(add_mod);
  266. if (module)
  267. gtk_clist_select_row(GTK_CLIST(modules), gtk_clist_find_row_from_data(GTK_CLIST(modules), module), -1);
  268. gtk_clist_thaw(GTK_CLIST(modules));
  269. return 1;
  270. }
  271. static void exit_now(GtkWidget *widget, gpointer data)
  272. {
  273. ast_loader_unregister(mod_update);
  274. gtk_main_quit();
  275. inuse--;
  276. ast_update_use_count();
  277. ast_unregister_verbose(verboser);
  278. ast_unload_resource("pbx_gtkconsole", 0);
  279. if (option_verbose > 1)
  280. ast_verbose(VERBOSE_PREFIX_2 "GTK Console Monitor Exiting\n");
  281. /* XXX Trying to quit after calling this makes asterisk segfault XXX */
  282. }
  283. static void exit_completely(GtkWidget *widget, gpointer data)
  284. {
  285. #if 0
  286. /* Clever... */
  287. ast_cli_command(clipipe[1], "quit");
  288. #else
  289. kill(getpid(), SIGTERM);
  290. #endif
  291. }
  292. static void exit_nicely(GtkWidget *widget, gpointer data)
  293. {
  294. fflush(stdout);
  295. gtk_widget_destroy(window);
  296. }
  297. static void *consolethread(void *data)
  298. {
  299. gtk_widget_show(window);
  300. gdk_threads_enter();
  301. gtk_main();
  302. gdk_threads_leave();
  303. return NULL;
  304. }
  305. static int cli_activate(void)
  306. {
  307. char buf[256] = "";
  308. strncpy(buf, gtk_entry_get_text(GTK_ENTRY(cli)), sizeof(buf) - 1);
  309. gtk_entry_set_text(GTK_ENTRY(cli), "");
  310. if (strlen(buf)) {
  311. ast_cli_command(clipipe[1], buf);
  312. }
  313. return TRUE;
  314. }
  315. static int show_console(void)
  316. {
  317. GtkWidget *hbox;
  318. GtkWidget *wbox;
  319. GtkWidget *notebook;
  320. GtkWidget *sw;
  321. GtkWidget *bbox, *hbbox, *add, *removew, *reloadw;
  322. char *modtitles[3] = { "Module", "Description", "Use Count" };
  323. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  324. statusbar = gtk_statusbar_new();
  325. gtk_widget_show(statusbar);
  326. gtk_signal_connect(GTK_OBJECT(window), "delete_event",
  327. GTK_SIGNAL_FUNC (exit_nicely), window);
  328. gtk_signal_connect(GTK_OBJECT(window), "destroy",
  329. GTK_SIGNAL_FUNC (exit_now), window);
  330. gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  331. quit = gtk_button_new_with_label("Quit Asterisk");
  332. gtk_signal_connect(GTK_OBJECT(quit), "clicked",
  333. GTK_SIGNAL_FUNC (exit_completely), window);
  334. gtk_widget_show(quit);
  335. closew = gtk_button_new_with_label("Close Window");
  336. gtk_signal_connect(GTK_OBJECT(closew), "clicked",
  337. GTK_SIGNAL_FUNC (exit_nicely), window);
  338. gtk_widget_show(closew);
  339. notebook = gtk_notebook_new();
  340. verb = gtk_clist_new(1);
  341. gtk_clist_columns_autosize(GTK_CLIST(verb));
  342. sw = gtk_scrolled_window_new(NULL, NULL);
  343. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  344. gtk_container_add(GTK_CONTAINER(sw), verb);
  345. gtk_widget_show(verb);
  346. gtk_widget_show(sw);
  347. gtk_widget_set_usize(verb, 640, 400);
  348. gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, gtk_label_new("Verbose Status"));
  349. modules = gtk_clist_new_with_titles(3, modtitles);
  350. gtk_clist_columns_autosize(GTK_CLIST(modules));
  351. gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 0, TRUE);
  352. gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 1, TRUE);
  353. gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 2, TRUE);
  354. gtk_clist_set_sort_column(GTK_CLIST(modules), 0);
  355. gtk_clist_set_auto_sort(GTK_CLIST(modules), TRUE);
  356. gtk_clist_column_titles_passive(GTK_CLIST(modules));
  357. sw = gtk_scrolled_window_new(NULL, NULL);
  358. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  359. gtk_container_add(GTK_CONTAINER(sw), modules);
  360. gtk_clist_set_selection_mode(GTK_CLIST(modules), GTK_SELECTION_BROWSE);
  361. gtk_widget_show(modules);
  362. gtk_widget_show(sw);
  363. add = gtk_button_new_with_label("Load...");
  364. gtk_widget_show(add);
  365. removew = gtk_button_new_with_label("Unload");
  366. gtk_widget_show(removew);
  367. reloadw = gtk_button_new_with_label("Reload");
  368. gtk_widget_show(reloadw);
  369. gtk_signal_connect(GTK_OBJECT(removew), "clicked",
  370. GTK_SIGNAL_FUNC (remove_module), window);
  371. gtk_signal_connect(GTK_OBJECT(add), "clicked",
  372. GTK_SIGNAL_FUNC (add_module), window);
  373. gtk_signal_connect(GTK_OBJECT(reloadw), "clicked",
  374. GTK_SIGNAL_FUNC (reload_module), window);
  375. bbox = gtk_vbox_new(FALSE, 5);
  376. gtk_widget_show(bbox);
  377. gtk_widget_set_usize(bbox, 100, -1);
  378. gtk_box_pack_start(GTK_BOX(bbox), add, FALSE, FALSE, 5);
  379. gtk_box_pack_start(GTK_BOX(bbox), removew, FALSE, FALSE, 5);
  380. gtk_box_pack_start(GTK_BOX(bbox), reloadw, FALSE, FALSE, 5);
  381. hbbox = gtk_hbox_new(FALSE, 5);
  382. gtk_widget_show(hbbox);
  383. gtk_box_pack_start(GTK_BOX(hbbox), sw, TRUE, TRUE, 5);
  384. gtk_box_pack_start(GTK_BOX(hbbox), bbox, FALSE, FALSE, 5);
  385. gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbbox, gtk_label_new("Module Information"));
  386. gtk_widget_show(notebook);
  387. wbox = gtk_hbox_new(FALSE, 5);
  388. gtk_widget_show(wbox);
  389. gtk_box_pack_end(GTK_BOX(wbox), quit, FALSE, FALSE, 5);
  390. gtk_box_pack_end(GTK_BOX(wbox), closew, FALSE, FALSE, 5);
  391. hbox = gtk_vbox_new(FALSE, 0);
  392. gtk_widget_show(hbox);
  393. /* Command line */
  394. cli = gtk_entry_new();
  395. gtk_widget_show(cli);
  396. gtk_signal_connect(GTK_OBJECT(cli), "activate",
  397. GTK_SIGNAL_FUNC (cli_activate), NULL);
  398. gtk_box_pack_start(GTK_BOX(hbox), notebook, TRUE, TRUE, 5);
  399. gtk_box_pack_start(GTK_BOX(hbox), wbox, FALSE, FALSE, 5);
  400. gtk_box_pack_start(GTK_BOX(hbox), cli, FALSE, FALSE, 0);
  401. gtk_box_pack_start(GTK_BOX(hbox), statusbar, FALSE, FALSE, 0);
  402. gtk_container_add(GTK_CONTAINER(window), hbox);
  403. gtk_window_set_title(GTK_WINDOW(window), "Asterisk Console");
  404. gtk_widget_grab_focus(cli);
  405. ast_pthread_create(&console_thread, NULL, consolethread, NULL);
  406. /* XXX Okay, seriously fix me! XXX */
  407. usleep(100000);
  408. ast_register_verbose(verboser);
  409. gtk_clist_freeze(GTK_CLIST(verb));
  410. ast_loader_register(mod_update);
  411. gtk_clist_thaw(GTK_CLIST(verb));
  412. gdk_input_add(clipipe[0], GDK_INPUT_READ, cliinput, NULL);
  413. mod_update();
  414. update_statusbar("Asterisk Console Ready");
  415. return 0;
  416. }
  417. int load_module(void)
  418. {
  419. if (pipe(clipipe)) {
  420. ast_log(LOG_WARNING, "Unable to create CLI pipe\n");
  421. return -1;
  422. }
  423. g_thread_init(NULL);
  424. if (gtk_init_check(NULL, NULL)) {
  425. if (!show_console()) {
  426. inuse++;
  427. ast_update_use_count();
  428. if (option_verbose > 1)
  429. ast_verbose( VERBOSE_PREFIX_2 "Launched GTK Console monitor\n");
  430. } else
  431. ast_log(LOG_WARNING, "Unable to start GTK console\n");
  432. } else {
  433. if (option_debug)
  434. ast_log(LOG_DEBUG, "Unable to start GTK console monitor -- ignoring\n");
  435. else if (option_verbose > 1)
  436. ast_verbose( VERBOSE_PREFIX_2 "GTK is not available -- skipping monitor\n");
  437. }
  438. return 0;
  439. }
  440. int usecount(void)
  441. {
  442. return inuse;
  443. }
  444. char *description(void)
  445. {
  446. return dtext;
  447. }
  448. char *key(void)
  449. {
  450. return ASTERISK_GPL_KEY;
  451. }