2 * Asterisk -- A telephony toolkit for Linux.
4 * GTK Console monitor -- very kludgy right now
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
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)
22 #include <sys/types.h>
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>
36 #include <sys/signal.h>
40 /* For where to put dynamic tables */
41 #include "../asterisk.h"
42 #include "../astconf.h"
44 static pthread_mutex_t verb_lock = AST_MUTEX_INITIALIZER;
46 static pthread_t console_thread;
49 static int clipipe[2];
50 static int cleanupid = -1;
52 static char *dtext = "Asterisk PBX Console (GTK Version)";
54 static GtkWidget *window;
55 static GtkWidget *quit;
56 static GtkWidget *closew;
57 static GtkWidget *verb;
58 static GtkWidget *modules;
59 static GtkWidget *statusbar;
60 static GtkWidget *cli;
62 static struct timeval last;
64 static void update_statusbar(char *msg)
66 gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 1);
67 gtk_statusbar_push(GTK_STATUSBAR(statusbar), 1, msg);
70 int unload_module(void)
73 /* Kill off the main thread */
74 pthread_cancel(console_thread);
76 gtk_widget_destroy(window);
84 static int cleanup(void *useless)
87 gtk_clist_thaw(GTK_CLIST(verb));
88 gtk_widget_queue_resize(verb->parent);
89 gtk_clist_moveto(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1, 0, 0, 0);
96 static void __verboser(char *stuff, int opos, int replacelast, int complete)
103 gtk_clist_freeze(GTK_CLIST(verb));
105 gtk_clist_remove(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1);
106 gtk_clist_append(GTK_CLIST(verb), s2);
107 if (last.tv_sec || last.tv_usec) {
109 gettimeofday(&tv, NULL);
111 gtk_timeout_remove(cleanupid);
112 ms = (tv.tv_sec - last.tv_sec) * 1000 + (tv.tv_usec - last.tv_usec) / 1000;
114 /* We just got a message within 100ms, so just schedule an update
115 in the near future */
116 cleanupid = gtk_timeout_add(200, cleanup, NULL);
122 gettimeofday(&last, NULL);
126 static void verboser(char *stuff, int opos, int replacelast, int complete)
128 ast_pthread_mutex_lock(&verb_lock);
129 /* Lock appropriately if we're really being called in verbose mode */
130 __verboser(stuff, opos, replacelast, complete);
131 ast_pthread_mutex_unlock(&verb_lock);
134 static void cliinput(void *data, int source, GdkInputCondition ic)
136 static char buf[256];
137 static int offset = 0;
142 /* Read as much stuff is there */
143 res = read(source, buf + offset, sizeof(buf) - 1 - offset);
145 buf[res + offset] = '\0';
146 /* make sure we've null terminated whatever we have so far */
151 /* Keep the trailing \n */
155 __verboser(l, 0, 0, 1);
163 /* We have some left over */
164 memmove(buf, l, strlen(l) + 1);
165 offset = strlen(buf);
173 static void remove_module(void)
178 if (GTK_CLIST(modules)->selection) {
179 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (int) GTK_CLIST(modules)->selection->data);
181 res = ast_unload_resource(module, 0);
184 snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
185 update_statusbar(buf);
187 snprintf(buf, sizeof(buf), "Module '%s' removed", module);
188 update_statusbar(buf);
192 static void reload_module(void)
197 if (GTK_CLIST(modules)->selection) {
198 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (int) GTK_CLIST(modules)->selection->data);
199 module = strdup(module);
202 res = ast_unload_resource(module, 0);
205 snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
206 update_statusbar(buf);
209 res = ast_load_resource(module);
212 snprintf(buf, sizeof(buf), "Error reloading module '%s'", module);
214 snprintf(buf, sizeof(buf), "Module '%s' reloaded", module);
216 for (x=0; x < GTK_CLIST(modules)->rows; x++) {
217 if (!strcmp((char *)gtk_clist_get_row_data(GTK_CLIST(modules), x), module)) {
218 gtk_clist_select_row(GTK_CLIST(modules), x, -1);
222 update_statusbar(buf);
230 static void file_ok_sel(GtkWidget *w, GtkFileSelection *fs)
232 char tmp[AST_CONFIG_MAX_PATH];
233 char *module = gtk_file_selection_get_filename(fs);
235 snprintf((char *)tmp,sizeof(tmp)-1,"%s/",(char *)ast_config_AST_MODULE_DIR);
236 if (!strncmp(module, (char *)tmp, strlen(tmp)))
237 module += strlen(tmp);
239 if (ast_load_resource(module)) {
240 snprintf(buf, sizeof(buf), "Error loading module '%s'.", module);
241 update_statusbar(buf);
243 snprintf(buf, sizeof(buf), "Module '%s' loaded", module);
244 update_statusbar(buf);
247 gtk_widget_destroy(GTK_WIDGET(fs));
250 static void add_module(void)
252 char tmp[AST_CONFIG_MAX_PATH];
254 snprintf((char *)tmp,sizeof(tmp)-1,"%s/*.so",(char *)ast_config_AST_MODULE_DIR);
255 filew = gtk_file_selection_new("Load Module");
256 gtk_signal_connect(GTK_OBJECT (GTK_FILE_SELECTION(filew)->ok_button),
257 "clicked", GTK_SIGNAL_FUNC(file_ok_sel), filew);
258 gtk_signal_connect_object(GTK_OBJECT (GTK_FILE_SELECTION(filew)->cancel_button),
259 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(filew));
260 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), (char *)tmp);
261 gtk_widget_show(filew);
264 static int add_mod(char *module, char *description, int usecount)
269 snprintf(use, sizeof(use), "%d", usecount);
271 pass[1] = description;
274 row = gtk_clist_append(GTK_CLIST(modules), pass);
275 gtk_clist_set_row_data(GTK_CLIST(modules), row, module);
279 static int mod_update(void)
282 /* Update the mod stuff */
283 if (GTK_CLIST(modules)->selection) {
284 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (int) GTK_CLIST(modules)->selection->data);
286 gtk_clist_freeze(GTK_CLIST(modules));
287 gtk_clist_clear(GTK_CLIST(modules));
288 ast_update_module_list(add_mod);
290 gtk_clist_select_row(GTK_CLIST(modules), gtk_clist_find_row_from_data(GTK_CLIST(modules), module), -1);
291 gtk_clist_thaw(GTK_CLIST(modules));
295 static void exit_now(GtkWidget *widget, gpointer data)
297 ast_loader_unregister(mod_update);
300 ast_update_use_count();
301 ast_unregister_verbose(verboser);
302 ast_unload_resource("pbx_gtkconsole", 0);
303 if (option_verbose > 1)
304 ast_verbose(VERBOSE_PREFIX_2 "GTK Console Monitor Exiting\n");
305 /* XXX Trying to quit after calling this makes asterisk segfault XXX */
308 static void exit_completely(GtkWidget *widget, gpointer data)
312 ast_cli_command(clipipe[1], "quit");
314 kill(getpid(), SIGTERM);
318 static void exit_nicely(GtkWidget *widget, gpointer data)
321 gtk_widget_destroy(window);
324 static void *consolethread(void *data)
326 gtk_widget_show(window);
333 static int cli_activate(void)
336 strncpy(buf, gtk_entry_get_text(GTK_ENTRY(cli)), sizeof(buf));
337 gtk_entry_set_text(GTK_ENTRY(cli), "");
339 ast_cli_command(clipipe[1], buf);
344 static int show_console(void)
350 GtkWidget *bbox, *hbbox, *add, *removew, *reloadw;
351 char *modtitles[3] = { "Module", "Description", "Use Count" };
352 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
354 statusbar = gtk_statusbar_new();
355 gtk_widget_show(statusbar);
357 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
358 GTK_SIGNAL_FUNC (exit_nicely), window);
359 gtk_signal_connect(GTK_OBJECT(window), "destroy",
360 GTK_SIGNAL_FUNC (exit_now), window);
361 gtk_container_set_border_width(GTK_CONTAINER(window), 10);
363 quit = gtk_button_new_with_label("Quit Asterisk");
364 gtk_signal_connect(GTK_OBJECT(quit), "clicked",
365 GTK_SIGNAL_FUNC (exit_completely), window);
366 gtk_widget_show(quit);
368 closew = gtk_button_new_with_label("Close Window");
369 gtk_signal_connect(GTK_OBJECT(closew), "clicked",
370 GTK_SIGNAL_FUNC (exit_nicely), window);
371 gtk_widget_show(closew);
373 notebook = gtk_notebook_new();
374 verb = gtk_clist_new(1);
375 gtk_clist_columns_autosize(GTK_CLIST(verb));
376 sw = gtk_scrolled_window_new(NULL, NULL);
377 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
378 gtk_container_add(GTK_CONTAINER(sw), verb);
379 gtk_widget_show(verb);
381 gtk_widget_set_usize(verb, 640, 400);
382 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, gtk_label_new("Verbose Status"));
385 modules = gtk_clist_new_with_titles(3, modtitles);
386 gtk_clist_columns_autosize(GTK_CLIST(modules));
387 gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 0, TRUE);
388 gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 1, TRUE);
389 gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 2, TRUE);
390 gtk_clist_set_sort_column(GTK_CLIST(modules), 0);
391 gtk_clist_set_auto_sort(GTK_CLIST(modules), TRUE);
392 gtk_clist_column_titles_passive(GTK_CLIST(modules));
393 sw = gtk_scrolled_window_new(NULL, NULL);
394 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
395 gtk_container_add(GTK_CONTAINER(sw), modules);
396 gtk_clist_set_selection_mode(GTK_CLIST(modules), GTK_SELECTION_BROWSE);
397 gtk_widget_show(modules);
400 add = gtk_button_new_with_label("Load...");
401 gtk_widget_show(add);
402 removew = gtk_button_new_with_label("Unload");
403 gtk_widget_show(removew);
404 reloadw = gtk_button_new_with_label("Reload");
405 gtk_widget_show(reloadw);
406 gtk_signal_connect(GTK_OBJECT(removew), "clicked",
407 GTK_SIGNAL_FUNC (remove_module), window);
408 gtk_signal_connect(GTK_OBJECT(add), "clicked",
409 GTK_SIGNAL_FUNC (add_module), window);
410 gtk_signal_connect(GTK_OBJECT(reloadw), "clicked",
411 GTK_SIGNAL_FUNC (reload_module), window);
413 bbox = gtk_vbox_new(FALSE, 5);
414 gtk_widget_show(bbox);
416 gtk_widget_set_usize(bbox, 100, -1);
417 gtk_box_pack_start(GTK_BOX(bbox), add, FALSE, FALSE, 5);
418 gtk_box_pack_start(GTK_BOX(bbox), removew, FALSE, FALSE, 5);
419 gtk_box_pack_start(GTK_BOX(bbox), reloadw, FALSE, FALSE, 5);
421 hbbox = gtk_hbox_new(FALSE, 5);
422 gtk_widget_show(hbbox);
424 gtk_box_pack_start(GTK_BOX(hbbox), sw, TRUE, TRUE, 5);
425 gtk_box_pack_start(GTK_BOX(hbbox), bbox, FALSE, FALSE, 5);
427 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbbox, gtk_label_new("Module Information"));
429 gtk_widget_show(notebook);
431 wbox = gtk_hbox_new(FALSE, 5);
432 gtk_widget_show(wbox);
433 gtk_box_pack_end(GTK_BOX(wbox), quit, FALSE, FALSE, 5);
434 gtk_box_pack_end(GTK_BOX(wbox), closew, FALSE, FALSE, 5);
436 hbox = gtk_vbox_new(FALSE, 0);
437 gtk_widget_show(hbox);
440 cli = gtk_entry_new();
441 gtk_widget_show(cli);
443 gtk_signal_connect(GTK_OBJECT(cli), "activate",
444 GTK_SIGNAL_FUNC (cli_activate), NULL);
446 gtk_box_pack_start(GTK_BOX(hbox), notebook, TRUE, TRUE, 5);
447 gtk_box_pack_start(GTK_BOX(hbox), wbox, FALSE, FALSE, 5);
448 gtk_box_pack_start(GTK_BOX(hbox), cli, FALSE, FALSE, 0);
449 gtk_box_pack_start(GTK_BOX(hbox), statusbar, FALSE, FALSE, 0);
450 gtk_container_add(GTK_CONTAINER(window), hbox);
451 gtk_window_set_title(GTK_WINDOW(window), "Asterisk Console");
452 gtk_widget_grab_focus(cli);
453 pthread_create(&console_thread, NULL, consolethread, NULL);
454 /* XXX Okay, seriously fix me! XXX */
456 ast_register_verbose(verboser);
457 gtk_clist_freeze(GTK_CLIST(verb));
458 ast_loader_register(mod_update);
459 gtk_clist_thaw(GTK_CLIST(verb));
460 gdk_input_add(clipipe[0], GDK_INPUT_READ, cliinput, NULL);
462 update_statusbar("Asterisk Console Ready");
467 int load_module(void)
470 ast_log(LOG_WARNING, "Unable to create CLI pipe\n");
474 if (gtk_init_check(NULL, NULL)) {
475 if (!show_console()) {
477 ast_update_use_count();
478 if (option_verbose > 1)
479 ast_verbose( VERBOSE_PREFIX_2 "Launched GTK Console monitor\n");
481 ast_log(LOG_WARNING, "Unable to start GTK console\n");
484 ast_log(LOG_DEBUG, "Unable to start GTK console monitor -- ignoring\n");
485 else if (option_verbose > 1)
486 ast_verbose( VERBOSE_PREFIX_2 "GTK is not available -- skipping monitor\n");
496 char *description(void)
503 return ASTERISK_GPL_KEY;