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>
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
47 AST_MUTEX_DEFINE_STATIC(verb_lock);
49 static pthread_t console_thread;
52 static int clipipe[2];
53 static int cleanupid = -1;
55 static char *dtext = "Asterisk PBX Console (GTK Version)";
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;
65 static struct timeval last;
67 static void update_statusbar(char *msg)
69 gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 1);
70 gtk_statusbar_push(GTK_STATUSBAR(statusbar), 1, msg);
73 int unload_module(void)
76 /* Kill off the main thread */
77 pthread_cancel(console_thread);
79 gtk_widget_destroy(window);
87 static int cleanup(void *useless)
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);
99 static void __verboser(const char *stuff, int opos, int replacelast, int complete)
104 s2[0] = (char *)stuff;
106 gtk_clist_freeze(GTK_CLIST(verb));
108 gtk_clist_remove(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1);
109 gtk_clist_append(GTK_CLIST(verb), s2);
110 if (!ast_tvzero(last)) {
112 gettimeofday(&tv, NULL);
114 gtk_timeout_remove(cleanupid);
115 ms = ast_tvdiff_ms(tv, last);
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);
125 gettimeofday(&last, NULL);
129 static void verboser(const char *stuff, int opos, int replacelast, int complete)
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);
137 static void cliinput(void *data, int source, GdkInputCondition ic)
139 static char buf[256];
140 static int offset = 0;
145 /* Read as much stuff is there */
146 res = read(source, buf + offset, sizeof(buf) - 1 - offset);
148 buf[res + offset] = '\0';
149 /* make sure we've null terminated whatever we have so far */
154 /* Keep the trailing \n */
158 __verboser(l, 0, 0, 1);
166 /* We have some left over */
167 memmove(buf, l, strlen(l) + 1);
168 offset = strlen(buf);
176 static void remove_module(void)
181 if (GTK_CLIST(modules)->selection) {
182 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (int) GTK_CLIST(modules)->selection->data);
184 res = ast_unload_resource(module, 0);
187 snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
188 update_statusbar(buf);
190 snprintf(buf, sizeof(buf), "Module '%s' removed", module);
191 update_statusbar(buf);
195 static void reload_module(void)
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);
205 res = ast_unload_resource(module, 0);
208 snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
209 update_statusbar(buf);
212 res = ast_load_resource(module);
215 snprintf(buf, sizeof(buf), "Error reloading module '%s'", module);
217 snprintf(buf, sizeof(buf), "Module '%s' reloaded", module);
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);
225 update_statusbar(buf);
233 static void file_ok_sel(GtkWidget *w, GtkFileSelection *fs)
235 char tmp[AST_CONFIG_MAX_PATH];
236 char *module = gtk_file_selection_get_filename(fs);
238 snprintf(tmp, sizeof(tmp), "%s/", ast_config_AST_MODULE_DIR);
239 if (!strncmp(module, (char *)tmp, strlen(tmp)))
240 module += strlen(tmp);
242 if (ast_load_resource(module)) {
243 snprintf(buf, sizeof(buf), "Error loading module '%s'.", module);
244 update_statusbar(buf);
246 snprintf(buf, sizeof(buf), "Module '%s' loaded", module);
247 update_statusbar(buf);
250 gtk_widget_destroy(GTK_WIDGET(fs));
253 static void add_module(void)
255 char tmp[AST_CONFIG_MAX_PATH];
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);
267 static int add_mod(const char *module, const char *description, int usecount, const char *like)
272 snprintf(use, sizeof(use), "%d", usecount);
274 pass[1] = description;
277 row = gtk_clist_append(GTK_CLIST(modules), pass);
278 gtk_clist_set_row_data(GTK_CLIST(modules), row, module);
282 static int mod_update(void)
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);
289 gtk_clist_freeze(GTK_CLIST(modules));
290 gtk_clist_clear(GTK_CLIST(modules));
291 ast_update_module_list(add_mod, NULL);
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));
298 static void exit_now(GtkWidget *widget, gpointer data)
300 ast_loader_unregister(mod_update);
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 */
311 static void exit_completely(GtkWidget *widget, gpointer data)
315 ast_cli_command(clipipe[1], "quit");
317 kill(getpid(), SIGTERM);
321 static void exit_nicely(GtkWidget *widget, gpointer data)
324 gtk_widget_destroy(window);
327 static void *consolethread(void *data)
329 gtk_widget_show(window);
336 static int cli_activate(void)
339 strncpy(buf, gtk_entry_get_text(GTK_ENTRY(cli)), sizeof(buf) - 1);
340 gtk_entry_set_text(GTK_ENTRY(cli), "");
342 ast_cli_command(clipipe[1], buf);
347 static int show_console(void)
353 GtkWidget *bbox, *hbbox, *add, *removew, *reloadw;
354 char *modtitles[3] = { "Module", "Description", "Use Count" };
355 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
357 statusbar = gtk_statusbar_new();
358 gtk_widget_show(statusbar);
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);
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);
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);
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);
384 gtk_widget_set_usize(verb, 640, 400);
385 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, gtk_label_new("Verbose Status"));
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);
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);
416 bbox = gtk_vbox_new(FALSE, 5);
417 gtk_widget_show(bbox);
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);
424 hbbox = gtk_hbox_new(FALSE, 5);
425 gtk_widget_show(hbbox);
427 gtk_box_pack_start(GTK_BOX(hbbox), sw, TRUE, TRUE, 5);
428 gtk_box_pack_start(GTK_BOX(hbbox), bbox, FALSE, FALSE, 5);
430 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbbox, gtk_label_new("Module Information"));
432 gtk_widget_show(notebook);
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);
439 hbox = gtk_vbox_new(FALSE, 0);
440 gtk_widget_show(hbox);
443 cli = gtk_entry_new();
444 gtk_widget_show(cli);
446 gtk_signal_connect(GTK_OBJECT(cli), "activate",
447 GTK_SIGNAL_FUNC (cli_activate), NULL);
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 */
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);
465 update_statusbar("Asterisk Console Ready");
470 int load_module(void)
473 ast_log(LOG_WARNING, "Unable to create CLI pipe\n");
477 if (gtk_init_check(NULL, NULL)) {
478 if (!show_console()) {
480 ast_update_use_count();
481 if (option_verbose > 1)
482 ast_verbose( VERBOSE_PREFIX_2 "Launched GTK Console monitor\n");
484 ast_log(LOG_WARNING, "Unable to start GTK console\n");
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");
499 char *description(void)
506 return ASTERISK_GPL_KEY;