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