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