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