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