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