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