Fixes Park() argument handling
[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/cli.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/paths.h"
48 #include "asterisk/term.h"
49
50 AST_MUTEX_DEFINE_STATIC(verb_lock);
51
52 static pthread_t console_thread;
53
54 static int inuse=0;
55 static int clipipe[2];
56 static int cleanupid = -1;
57
58 static GtkWidget *window;
59 static GtkWidget *quit;
60 static GtkWidget *closew;
61 static GtkWidget *verb;
62 static GtkWidget *modules;
63 static GtkWidget *statusbar;
64 static GtkWidget *cli;
65
66 static struct timeval last;
67
68 static void update_statusbar(char *msg)
69 {
70         gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 1);
71         gtk_statusbar_push(GTK_STATUSBAR(statusbar), 1, msg);
72 }
73
74 static int unload_module(void)
75 {
76         if (inuse) {
77                 /* Kill off the main thread */
78                 pthread_cancel(console_thread);
79                 gdk_threads_enter();
80                 gtk_widget_destroy(window);
81                 gdk_threads_leave();
82                 close(clipipe[0]);
83                 close(clipipe[1]);
84         }
85         return 0;
86 }
87
88 static int cleanup(void *useless)
89 {
90         gdk_threads_enter();
91         gtk_clist_thaw(GTK_CLIST(verb));
92         gtk_widget_queue_resize(verb->parent);
93         gtk_clist_moveto(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1, 0, 0, 0);
94         cleanupid = -1;
95         gdk_threads_leave();
96         return 0;
97 }
98
99
100 static void __verboser(const char *_stuff)
101 {
102         char *s2[2];
103         struct timeval tv;
104         int ms;
105         char *stuff;
106
107         stuff = ast_strdupa(_stuff);
108         term_strip(stuff, stuff, strlen(stuff) + 1);
109
110         s2[0] = (char *)stuff;
111         s2[1] = NULL;
112         gtk_clist_freeze(GTK_CLIST(verb));
113         gtk_clist_append(GTK_CLIST(verb), s2);
114         if (!ast_tvzero(last)) {
115                 gdk_threads_leave();
116                 gettimeofday(&tv, NULL);
117                 if (cleanupid > -1)
118                         gtk_timeout_remove(cleanupid);
119                 ms = ast_tvdiff_ms(tv, last);
120                 if (ms < 100) {
121                         /* We just got a message within 100ms, so just schedule an update
122                            in the near future */
123                         cleanupid = gtk_timeout_add(200, cleanup, NULL);
124                 } else {
125                         cleanup(&cleanupid);
126                 }
127                 last = tv;
128         } else {
129                 gettimeofday(&last, NULL);
130         }
131 }
132
133 static void verboser(const char *stuff) 
134 {
135         if (*stuff == 127) {
136                 stuff++;
137         }
138
139         ast_mutex_lock(&verb_lock);
140         /* Lock appropriately if we're really being called in verbose mode */
141         __verboser(stuff);
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);
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 static void remove_module(void)
184 {
185         int res;
186         char *module;
187         char buf[256];
188         if (GTK_CLIST(modules)->selection) {
189                 module = (char *) gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
190                 gdk_threads_leave();
191                 res = ast_unload_resource(module, 0);
192                 gdk_threads_enter();
193                 if (res) {
194                         snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
195                         update_statusbar(buf);
196                 } else {
197                         snprintf(buf, sizeof(buf), "Module '%s' removed", module);
198                         update_statusbar(buf);
199                 }
200         }
201 }
202
203 static int reload(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), (long) 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         return AST_MODULE_LOAD_SUCCESS;
241 }
242
243 static void file_ok_sel(GtkWidget *w, GtkFileSelection *fs)
244 {
245         char tmp[PATH_MAX];
246         char *module = gtk_file_selection_get_filename(fs);
247         char buf[256];
248         snprintf(tmp, sizeof(tmp), "%s/", ast_config_AST_MODULE_DIR);
249         if (!strncmp(module, (char *)tmp, strlen(tmp))) 
250                 module += strlen(tmp);
251         gdk_threads_leave();
252         if (ast_load_resource(module)) {
253                 snprintf(buf, sizeof(buf), "Error loading module '%s'.", module);
254                 update_statusbar(buf);
255         } else {
256                 snprintf(buf, sizeof(buf), "Module '%s' loaded", module);
257                 update_statusbar(buf);
258         }
259         gdk_threads_enter();
260         gtk_widget_destroy(GTK_WIDGET(fs));
261 }
262
263 static void add_module(void)
264 {
265         char tmp[PATH_MAX];
266         GtkWidget *filew;
267         snprintf(tmp, sizeof(tmp), "%s/*.so", ast_config_AST_MODULE_DIR);
268         filew = gtk_file_selection_new("Load Module");
269         gtk_signal_connect(GTK_OBJECT (GTK_FILE_SELECTION(filew)->ok_button),
270                                         "clicked", GTK_SIGNAL_FUNC(file_ok_sel), filew);
271         gtk_signal_connect_object(GTK_OBJECT (GTK_FILE_SELECTION(filew)->cancel_button),
272                                         "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(filew));
273         gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), (char *)tmp);
274         gtk_widget_show(filew);
275 }
276
277 static int add_mod(const char *module, const char *description, int usecount, const char *like)
278 {
279         char use[10];
280         const char *pass[4];
281         int row;
282         snprintf(use, sizeof(use), "%d", usecount);
283         pass[0] = module;
284         pass[1] = description;
285         pass[2] = use;
286         pass[3] = NULL;
287         row = gtk_clist_append(GTK_CLIST(modules), (char **) pass);
288         gtk_clist_set_row_data(GTK_CLIST(modules), row, (char *) module);
289         return 0;       
290 }
291
292 static int mod_update(void)
293 {
294         char *module= NULL;
295         /* Update the mod stuff */
296         if (GTK_CLIST(modules)->selection) {
297                 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
298         }
299         gtk_clist_freeze(GTK_CLIST(modules));
300         gtk_clist_clear(GTK_CLIST(modules));
301         ast_update_module_list(add_mod, NULL);
302         if (module)
303                 gtk_clist_select_row(GTK_CLIST(modules), gtk_clist_find_row_from_data(GTK_CLIST(modules), module), -1);
304         gtk_clist_thaw(GTK_CLIST(modules));
305         return 1;
306 }
307
308 static void exit_now(GtkWidget *widget, gpointer data)
309 {
310         int res;
311
312         ast_loader_unregister(mod_update);
313         gtk_main_quit();
314         inuse--;
315         ast_update_use_count();
316         res = ast_unregister_verbose(verboser);
317         ast_unload_resource("pbx_gtkconsole", 0);
318         ast_verb(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         int res;
367
368         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
369         
370         statusbar = gtk_statusbar_new();
371         gtk_widget_show(statusbar);
372         
373         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
374                         GTK_SIGNAL_FUNC (exit_nicely), window);
375         gtk_signal_connect(GTK_OBJECT(window), "destroy",
376                         GTK_SIGNAL_FUNC (exit_now), window);
377         gtk_container_set_border_width(GTK_CONTAINER(window), 10);
378
379         quit = gtk_button_new_with_label("Quit Asterisk");
380         gtk_signal_connect(GTK_OBJECT(quit), "clicked",
381                         GTK_SIGNAL_FUNC (exit_completely), window);
382         gtk_widget_show(quit);
383
384         closew = gtk_button_new_with_label("Close Window");
385         gtk_signal_connect(GTK_OBJECT(closew), "clicked",
386                         GTK_SIGNAL_FUNC (exit_nicely), window);
387         gtk_widget_show(closew);
388
389         notebook = gtk_notebook_new();
390         verb = gtk_clist_new(1);
391         gtk_clist_columns_autosize(GTK_CLIST(verb));
392         sw = gtk_scrolled_window_new(NULL, NULL);
393         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
394         gtk_container_add(GTK_CONTAINER(sw), verb);
395         gtk_widget_show(verb);
396         gtk_widget_show(sw);
397         gtk_widget_set_usize(verb, 640, 400);
398         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, gtk_label_new("Verbose Status"));
399
400         
401         modules = gtk_clist_new_with_titles(3, modtitles);
402         gtk_clist_columns_autosize(GTK_CLIST(modules));
403         gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 0, TRUE);
404         gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 1, TRUE);
405         gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 2, TRUE);
406         gtk_clist_set_sort_column(GTK_CLIST(modules), 0);
407         gtk_clist_set_auto_sort(GTK_CLIST(modules), TRUE);
408         gtk_clist_column_titles_passive(GTK_CLIST(modules));
409         sw = gtk_scrolled_window_new(NULL, NULL);
410         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
411         gtk_container_add(GTK_CONTAINER(sw), modules);
412         gtk_clist_set_selection_mode(GTK_CLIST(modules), GTK_SELECTION_BROWSE);
413         gtk_widget_show(modules);
414         gtk_widget_show(sw);
415
416         add = gtk_button_new_with_label("Load...");
417         gtk_widget_show(add);
418         removew = gtk_button_new_with_label("Unload");
419         gtk_widget_show(removew);
420         reloadw = gtk_button_new_with_label("Reload");
421         gtk_widget_show(reloadw);
422         gtk_signal_connect(GTK_OBJECT(removew), "clicked",
423                         GTK_SIGNAL_FUNC (remove_module), window);
424         gtk_signal_connect(GTK_OBJECT(add), "clicked",
425                         GTK_SIGNAL_FUNC (add_module), window);
426         gtk_signal_connect(GTK_OBJECT(reloadw), "clicked",
427                         GTK_SIGNAL_FUNC (reload), window);
428                 
429         bbox = gtk_vbox_new(FALSE, 5);
430         gtk_widget_show(bbox);
431
432         gtk_widget_set_usize(bbox, 100, -1);
433         gtk_box_pack_start(GTK_BOX(bbox), add, FALSE, FALSE, 5);
434         gtk_box_pack_start(GTK_BOX(bbox), removew, FALSE, FALSE, 5);
435         gtk_box_pack_start(GTK_BOX(bbox), reloadw, FALSE, FALSE, 5);
436
437         hbbox = gtk_hbox_new(FALSE, 5);
438         gtk_widget_show(hbbox);
439         
440         gtk_box_pack_start(GTK_BOX(hbbox), sw, TRUE, TRUE, 5);
441         gtk_box_pack_start(GTK_BOX(hbbox), bbox, FALSE, FALSE, 5);
442
443         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbbox, gtk_label_new("Module Information"));
444
445         gtk_widget_show(notebook);
446
447         wbox = gtk_hbox_new(FALSE, 5);
448         gtk_widget_show(wbox);
449         gtk_box_pack_end(GTK_BOX(wbox), quit, FALSE, FALSE, 5);
450         gtk_box_pack_end(GTK_BOX(wbox), closew, FALSE, FALSE, 5);
451
452         hbox = gtk_vbox_new(FALSE, 0);
453         gtk_widget_show(hbox);
454         
455         /* Command line */
456         cli = gtk_entry_new();
457         gtk_widget_show(cli);
458
459         gtk_signal_connect(GTK_OBJECT(cli), "activate",
460                         GTK_SIGNAL_FUNC (cli_activate), NULL);
461
462         gtk_box_pack_start(GTK_BOX(hbox), notebook, TRUE, TRUE, 5);
463         gtk_box_pack_start(GTK_BOX(hbox), wbox, FALSE, FALSE, 5);
464         gtk_box_pack_start(GTK_BOX(hbox), cli, FALSE, FALSE, 0);
465         gtk_box_pack_start(GTK_BOX(hbox), statusbar, FALSE, FALSE, 0);
466         gtk_container_add(GTK_CONTAINER(window), hbox);
467         gtk_window_set_title(GTK_WINDOW(window), "Asterisk Console");
468         gtk_widget_grab_focus(cli);
469         ast_pthread_create(&console_thread, NULL, consolethread, NULL);
470         /* XXX Okay, seriously fix me! XXX */
471         usleep(100000);
472         res = ast_register_verbose(verboser);
473         gtk_clist_freeze(GTK_CLIST(verb));
474         ast_loader_register(mod_update);
475         gtk_clist_thaw(GTK_CLIST(verb));
476         gdk_input_add(clipipe[0], GDK_INPUT_READ, cliinput, NULL);
477         mod_update();
478         update_statusbar("Asterisk Console Ready");
479         return 0;
480 }
481
482
483 static int load_module(void)
484 {
485         if (pipe(clipipe)) {
486                 ast_log(LOG_WARNING, "Unable to create CLI pipe\n");
487                 return AST_MODULE_LOAD_FAILURE;
488         }
489         g_thread_init(NULL);
490         if (gtk_init_check(NULL, NULL))  {
491                 if (!show_console()) {
492                         inuse++;
493                         ast_update_use_count();
494                         ast_verb(2, "Launched GTK Console monitor\n");
495                 } else
496                         ast_log(LOG_WARNING, "Unable to start GTK console\n");
497         } else {
498                 if (option_debug)
499                         ast_log(LOG_DEBUG, "Unable to start GTK console monitor -- ignoring\n");
500                 else
501                         ast_verb(2, "GTK is not available -- skipping monitor\n");
502         }
503         return AST_MODULE_LOAD_SUCCESS;
504 }
505
506 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GTK Console",
507                 .load = load_module,
508                 .unload = unload_module,
509                 .reload = reload,
510                );