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