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