Only produce error message once, don't fill the screen with them...
[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 <sys/types.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <signal.h>
42 #include <sys/time.h>
43
44 #include <gtk/gtk.h>
45 #include <glib.h>
46
47 #include "asterisk/pbx.h"
48 #include "asterisk/config.h"
49 #include "asterisk/module.h"
50 #include "asterisk/logger.h"
51 #include "asterisk/options.h"
52 #include "asterisk/cli.h"
53 #include "asterisk/utils.h"
54
55 AST_MUTEX_DEFINE_STATIC(verb_lock);
56
57 static pthread_t console_thread;
58
59 static int inuse=0;
60 static int clipipe[2];
61 static int cleanupid = -1;
62
63 static char *dtext = "Asterisk PBX Console (GTK Version)";
64
65 static GtkWidget *window;
66 static GtkWidget *quit;
67 static GtkWidget *closew;
68 static GtkWidget *verb;
69 static GtkWidget *modules;
70 static GtkWidget *statusbar;
71 static GtkWidget *cli;
72
73 static struct timeval last;
74
75 static void update_statusbar(char *msg)
76 {
77         gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 1);
78         gtk_statusbar_push(GTK_STATUSBAR(statusbar), 1, msg);
79 }
80
81 static int unload_module(void *mod)
82 {
83         if (inuse) {
84                 /* Kill off the main thread */
85                 pthread_cancel(console_thread);
86                 gdk_threads_enter();
87                 gtk_widget_destroy(window);
88                 gdk_threads_leave();
89                 close(clipipe[0]);
90                 close(clipipe[1]);
91         }
92         return 0;
93 }
94
95 static int cleanup(void *useless)
96 {
97         gdk_threads_enter();
98         gtk_clist_thaw(GTK_CLIST(verb));
99         gtk_widget_queue_resize(verb->parent);
100         gtk_clist_moveto(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1, 0, 0, 0);
101         cleanupid = -1;
102         gdk_threads_leave();
103         return 0;
104 }
105
106
107 static void __verboser(const char *stuff, int opos, int replacelast, int complete)
108 {
109         char *s2[2];
110         struct timeval tv;
111         int ms;
112         s2[0] = (char *)stuff;
113         s2[1] = NULL;
114         gtk_clist_freeze(GTK_CLIST(verb));
115         if (replacelast) 
116                 gtk_clist_remove(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1);
117         gtk_clist_append(GTK_CLIST(verb), s2);
118         if (!ast_tvzero(last)) {
119                 gdk_threads_leave();
120                 gettimeofday(&tv, NULL);
121                 if (cleanupid > -1)
122                         gtk_timeout_remove(cleanupid);
123                 ms = ast_tvdiff_ms(tv, last);
124                 if (ms < 100) {
125                         /* We just got a message within 100ms, so just schedule an update
126                            in the near future */
127                         cleanupid = gtk_timeout_add(200, cleanup, NULL);
128                 } else {
129                         cleanup(&cleanupid);
130                 }
131                 last = tv;
132         } else {
133                 gettimeofday(&last, NULL);
134         }
135 }
136
137 static void verboser(const char *stuff, int opos, int replacelast, int complete) 
138 {
139         ast_mutex_lock(&verb_lock);
140         /* Lock appropriately if we're really being called in verbose mode */
141         __verboser(stuff, opos, replacelast, complete);
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, 0, 0, 1);
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
184 static void remove_module(void)
185 {
186         int res;
187         char *module;
188         char buf[256];
189         if (GTK_CLIST(modules)->selection) {
190                 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (int) GTK_CLIST(modules)->selection->data);
191                 gdk_threads_leave();
192                 res = ast_unload_resource(module, 0);
193                 gdk_threads_enter();
194                 if (res) {
195                         snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
196                         update_statusbar(buf);
197                 } else {
198                         snprintf(buf, sizeof(buf), "Module '%s' removed", module);
199                         update_statusbar(buf);
200                 }
201         }
202 }
203 static int reload_module(void *mod)
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), (int) 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 0;
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), (int) 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         ast_loader_unregister(mod_update);
311         gtk_main_quit();
312         inuse--;
313         ast_update_use_count();
314         ast_unregister_verbose(verboser);
315         ast_unload_resource("pbx_gtkconsole", 0);
316         if (option_verbose > 1)
317                 ast_verbose(VERBOSE_PREFIX_2 "GTK Console Monitor Exiting\n");
318         /* XXX Trying to quit after calling this makes asterisk segfault XXX */
319 }
320
321 static void exit_completely(GtkWidget *widget, gpointer data)
322 {
323 #if 0
324         /* Clever... */
325         ast_cli_command(clipipe[1], "quit");
326 #else
327         kill(getpid(), SIGTERM);
328 #endif
329 }
330
331 static void exit_nicely(GtkWidget *widget, gpointer data)
332 {
333         fflush(stdout);
334         gtk_widget_destroy(window);
335 }
336
337 static void *consolethread(void *data)
338 {
339         gtk_widget_show(window);
340         gdk_threads_enter();
341         gtk_main();
342         gdk_threads_leave();
343         return NULL;
344 }
345
346 static int cli_activate(void)
347 {
348         char buf[256] = "";
349         strncpy(buf, gtk_entry_get_text(GTK_ENTRY(cli)), sizeof(buf) - 1);
350         gtk_entry_set_text(GTK_ENTRY(cli), "");
351         if (strlen(buf)) {
352                 ast_cli_command(clipipe[1], buf);
353         }
354         return TRUE;
355 }
356
357 static int show_console(void)
358 {
359         GtkWidget *hbox;
360         GtkWidget *wbox;
361         GtkWidget *notebook;
362         GtkWidget *sw;
363         GtkWidget *bbox, *hbbox, *add, *removew, *reloadw;
364         char *modtitles[3] = { "Module", "Description", "Use Count" };
365         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
366         
367         statusbar = gtk_statusbar_new();
368         gtk_widget_show(statusbar);
369         
370         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
371                         GTK_SIGNAL_FUNC (exit_nicely), window);
372         gtk_signal_connect(GTK_OBJECT(window), "destroy",
373                         GTK_SIGNAL_FUNC (exit_now), window);
374         gtk_container_set_border_width(GTK_CONTAINER(window), 10);
375
376         quit = gtk_button_new_with_label("Quit Asterisk");
377         gtk_signal_connect(GTK_OBJECT(quit), "clicked",
378                         GTK_SIGNAL_FUNC (exit_completely), window);
379         gtk_widget_show(quit);
380
381         closew = gtk_button_new_with_label("Close Window");
382         gtk_signal_connect(GTK_OBJECT(closew), "clicked",
383                         GTK_SIGNAL_FUNC (exit_nicely), window);
384         gtk_widget_show(closew);
385
386         notebook = gtk_notebook_new();
387         verb = gtk_clist_new(1);
388         gtk_clist_columns_autosize(GTK_CLIST(verb));
389         sw = gtk_scrolled_window_new(NULL, NULL);
390         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
391         gtk_container_add(GTK_CONTAINER(sw), verb);
392         gtk_widget_show(verb);
393         gtk_widget_show(sw);
394         gtk_widget_set_usize(verb, 640, 400);
395         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, gtk_label_new("Verbose Status"));
396
397         
398         modules = gtk_clist_new_with_titles(3, modtitles);
399         gtk_clist_columns_autosize(GTK_CLIST(modules));
400         gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 0, TRUE);
401         gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 1, TRUE);
402         gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 2, TRUE);
403         gtk_clist_set_sort_column(GTK_CLIST(modules), 0);
404         gtk_clist_set_auto_sort(GTK_CLIST(modules), TRUE);
405         gtk_clist_column_titles_passive(GTK_CLIST(modules));
406         sw = gtk_scrolled_window_new(NULL, NULL);
407         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
408         gtk_container_add(GTK_CONTAINER(sw), modules);
409         gtk_clist_set_selection_mode(GTK_CLIST(modules), GTK_SELECTION_BROWSE);
410         gtk_widget_show(modules);
411         gtk_widget_show(sw);
412
413         add = gtk_button_new_with_label("Load...");
414         gtk_widget_show(add);
415         removew = gtk_button_new_with_label("Unload");
416         gtk_widget_show(removew);
417         reloadw = gtk_button_new_with_label("Reload");
418         gtk_widget_show(reloadw);
419         gtk_signal_connect(GTK_OBJECT(removew), "clicked",
420                         GTK_SIGNAL_FUNC (remove_module), window);
421         gtk_signal_connect(GTK_OBJECT(add), "clicked",
422                         GTK_SIGNAL_FUNC (add_module), window);
423         gtk_signal_connect(GTK_OBJECT(reloadw), "clicked",
424                         GTK_SIGNAL_FUNC (reload_module), window);
425                 
426         bbox = gtk_vbox_new(FALSE, 5);
427         gtk_widget_show(bbox);
428
429         gtk_widget_set_usize(bbox, 100, -1);
430         gtk_box_pack_start(GTK_BOX(bbox), add, FALSE, FALSE, 5);
431         gtk_box_pack_start(GTK_BOX(bbox), removew, FALSE, FALSE, 5);
432         gtk_box_pack_start(GTK_BOX(bbox), reloadw, FALSE, FALSE, 5);
433
434         hbbox = gtk_hbox_new(FALSE, 5);
435         gtk_widget_show(hbbox);
436         
437         gtk_box_pack_start(GTK_BOX(hbbox), sw, TRUE, TRUE, 5);
438         gtk_box_pack_start(GTK_BOX(hbbox), bbox, FALSE, FALSE, 5);
439
440         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbbox, gtk_label_new("Module Information"));
441
442         gtk_widget_show(notebook);
443
444         wbox = gtk_hbox_new(FALSE, 5);
445         gtk_widget_show(wbox);
446         gtk_box_pack_end(GTK_BOX(wbox), quit, FALSE, FALSE, 5);
447         gtk_box_pack_end(GTK_BOX(wbox), closew, FALSE, FALSE, 5);
448
449         hbox = gtk_vbox_new(FALSE, 0);
450         gtk_widget_show(hbox);
451         
452         /* Command line */
453         cli = gtk_entry_new();
454         gtk_widget_show(cli);
455
456         gtk_signal_connect(GTK_OBJECT(cli), "activate",
457                         GTK_SIGNAL_FUNC (cli_activate), NULL);
458
459         gtk_box_pack_start(GTK_BOX(hbox), notebook, TRUE, TRUE, 5);
460         gtk_box_pack_start(GTK_BOX(hbox), wbox, FALSE, FALSE, 5);
461         gtk_box_pack_start(GTK_BOX(hbox), cli, FALSE, FALSE, 0);
462         gtk_box_pack_start(GTK_BOX(hbox), statusbar, FALSE, FALSE, 0);
463         gtk_container_add(GTK_CONTAINER(window), hbox);
464         gtk_window_set_title(GTK_WINDOW(window), "Asterisk Console");
465         gtk_widget_grab_focus(cli);
466         ast_pthread_create(&console_thread, NULL, consolethread, NULL);
467         /* XXX Okay, seriously fix me! XXX */
468         usleep(100000);
469         ast_register_verbose(verboser);
470         gtk_clist_freeze(GTK_CLIST(verb));
471         ast_loader_register(mod_update);
472         gtk_clist_thaw(GTK_CLIST(verb));
473         gdk_input_add(clipipe[0], GDK_INPUT_READ, cliinput, NULL);
474         mod_update();
475         update_statusbar("Asterisk Console Ready");
476         return 0;
477 }
478
479
480 static int load_module(void *mod)
481 {
482         if (pipe(clipipe)) {
483                 ast_log(LOG_WARNING, "Unable to create CLI pipe\n");
484                 return -1;
485         }
486         g_thread_init(NULL);
487         if (gtk_init_check(NULL, NULL))  {
488                 if (!show_console()) {
489                         inuse++;
490                         ast_update_use_count();
491                         if (option_verbose > 1)
492                                 ast_verbose( VERBOSE_PREFIX_2 "Launched GTK Console monitor\n");                
493                 } else
494                         ast_log(LOG_WARNING, "Unable to start GTK console\n");
495         } else {
496                 if (option_debug)
497                         ast_log(LOG_DEBUG, "Unable to start GTK console monitor -- ignoring\n");
498                 else if (option_verbose > 1)
499                         ast_verbose( VERBOSE_PREFIX_2 "GTK is not available -- skipping monitor\n");
500         }
501         return 0;
502 }
503
504 static const char *description(void)
505 {
506         return dtext;
507 }
508
509 static const char *key(void)
510 {
511         return ASTERISK_GPL_KEY;
512 }
513
514 STD_MOD(MOD_0, reload_module, NULL, NULL);