00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00033
00034 #include <sys/types.h>
00035 #include <stdlib.h>
00036 #include <fcntl.h>
00037 #include <unistd.h>
00038 #include <stdio.h>
00039 #include <string.h>
00040 #include <stdarg.h>
00041 #include <signal.h>
00042 #include <sys/time.h>
00043
00044 #include <gtk/gtk.h>
00045 #include <glib.h>
00046
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/config.h"
00049 #include "asterisk/module.h"
00050 #include "asterisk/logger.h"
00051 #include "asterisk/options.h"
00052 #include "asterisk/cli.h"
00053 #include "asterisk/utils.h"
00054 #include "asterisk/paths.h"
00055 #include "asterisk/term.h"
00056
00057 AST_MUTEX_DEFINE_STATIC(verb_lock);
00058
00059 static pthread_t console_thread;
00060
00061 static int inuse=0;
00062 static int clipipe[2];
00063 static int cleanupid = -1;
00064
00065 static GtkWidget *window;
00066 static GtkWidget *quit;
00067 static GtkWidget *closew;
00068 static GtkWidget *verb;
00069 static GtkWidget *modules;
00070 static GtkWidget *statusbar;
00071 static GtkWidget *cli;
00072
00073 static struct timeval last;
00074
00075 static void update_statusbar(char *msg)
00076 {
00077 gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 1);
00078 gtk_statusbar_push(GTK_STATUSBAR(statusbar), 1, msg);
00079 }
00080
00081 static int unload_module(void)
00082 {
00083 if (inuse) {
00084
00085 pthread_cancel(console_thread);
00086 gdk_threads_enter();
00087 gtk_widget_destroy(window);
00088 gdk_threads_leave();
00089 close(clipipe[0]);
00090 close(clipipe[1]);
00091 }
00092 return 0;
00093 }
00094
00095 static int cleanup(void *useless)
00096 {
00097 gdk_threads_enter();
00098 gtk_clist_thaw(GTK_CLIST(verb));
00099 gtk_widget_queue_resize(verb->parent);
00100 gtk_clist_moveto(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1, 0, 0, 0);
00101 cleanupid = -1;
00102 gdk_threads_leave();
00103 return 0;
00104 }
00105
00106
00107 static void __verboser(const char *_stuff)
00108 {
00109 char *s2[2];
00110 struct timeval tv;
00111 int ms;
00112 char *stuff;
00113
00114 stuff = ast_strdupa(_stuff);
00115 term_strip(stuff, stuff, strlen(stuff) + 1);
00116
00117 s2[0] = (char *)stuff;
00118 s2[1] = NULL;
00119 gtk_clist_freeze(GTK_CLIST(verb));
00120 gtk_clist_append(GTK_CLIST(verb), s2);
00121 if (!ast_tvzero(last)) {
00122 gdk_threads_leave();
00123 gettimeofday(&tv, NULL);
00124 if (cleanupid > -1)
00125 gtk_timeout_remove(cleanupid);
00126 ms = ast_tvdiff_ms(tv, last);
00127 if (ms < 100) {
00128
00129
00130 cleanupid = gtk_timeout_add(200, cleanup, NULL);
00131 } else {
00132 cleanup(&cleanupid);
00133 }
00134 last = tv;
00135 } else {
00136 gettimeofday(&last, NULL);
00137 }
00138 }
00139
00140 static void verboser(const char *stuff)
00141 {
00142 ast_mutex_lock(&verb_lock);
00143
00144 __verboser(stuff);
00145 ast_mutex_unlock(&verb_lock);
00146 }
00147
00148 static void cliinput(void *data, int source, GdkInputCondition ic)
00149 {
00150 static char buf[256];
00151 static int offset = 0;
00152 int res;
00153 char *c;
00154 char *l;
00155 char n;
00156
00157 res = read(source, buf + offset, sizeof(buf) - 1 - offset);
00158 if (res > -1)
00159 buf[res + offset] = '\0';
00160
00161 c = buf;
00162 l = buf;
00163 while(*c) {
00164 if (*c == '\n') {
00165
00166 c++;
00167 n = *c;
00168 *c = '\0';
00169 __verboser(l);
00170 *(c - 1) = '\0';
00171 *c = n;
00172 l = c;
00173 } else
00174 c++;
00175 }
00176 if (strlen(l)) {
00177
00178 memmove(buf, l, strlen(l) + 1);
00179 offset = strlen(buf);
00180 } else {
00181 offset = 0;
00182 }
00183
00184 }
00185
00186 static void remove_module(void)
00187 {
00188 int res;
00189 char *module;
00190 char buf[256];
00191 if (GTK_CLIST(modules)->selection) {
00192 module = (char *) gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
00193 gdk_threads_leave();
00194 res = ast_unload_resource(module, 0);
00195 gdk_threads_enter();
00196 if (res) {
00197 snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
00198 update_statusbar(buf);
00199 } else {
00200 snprintf(buf, sizeof(buf), "Module '%s' removed", module);
00201 update_statusbar(buf);
00202 }
00203 }
00204 }
00205
00206 static int reload(void)
00207 {
00208 int res, x;
00209 char *module;
00210 char buf[256];
00211 if (GTK_CLIST(modules)->selection) {
00212 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
00213 module = strdup(module);
00214 if (module) {
00215 gdk_threads_leave();
00216 res = ast_unload_resource(module, 0);
00217 gdk_threads_enter();
00218 if (res) {
00219 snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
00220 update_statusbar(buf);
00221 } else {
00222 gdk_threads_leave();
00223 res = ast_load_resource(module);
00224 gdk_threads_enter();
00225 if (res) {
00226 snprintf(buf, sizeof(buf), "Error reloading module '%s'", module);
00227 } else {
00228 snprintf(buf, sizeof(buf), "Module '%s' reloaded", module);
00229 }
00230 for (x=0; x < GTK_CLIST(modules)->rows; x++) {
00231 if (!strcmp((char *)gtk_clist_get_row_data(GTK_CLIST(modules), x), module)) {
00232 gtk_clist_select_row(GTK_CLIST(modules), x, -1);
00233 break;
00234 }
00235 }
00236 update_statusbar(buf);
00237
00238 }
00239 free(module);
00240 }
00241 }
00242
00243 return 0;
00244 }
00245
00246 static void file_ok_sel(GtkWidget *w, GtkFileSelection *fs)
00247 {
00248 char tmp[PATH_MAX];
00249 char *module = gtk_file_selection_get_filename(fs);
00250 char buf[256];
00251 snprintf(tmp, sizeof(tmp), "%s/", ast_config_AST_MODULE_DIR);
00252 if (!strncmp(module, (char *)tmp, strlen(tmp)))
00253 module += strlen(tmp);
00254 gdk_threads_leave();
00255 if (ast_load_resource(module)) {
00256 snprintf(buf, sizeof(buf), "Error loading module '%s'.", module);
00257 update_statusbar(buf);
00258 } else {
00259 snprintf(buf, sizeof(buf), "Module '%s' loaded", module);
00260 update_statusbar(buf);
00261 }
00262 gdk_threads_enter();
00263 gtk_widget_destroy(GTK_WIDGET(fs));
00264 }
00265
00266 static void add_module(void)
00267 {
00268 char tmp[PATH_MAX];
00269 GtkWidget *filew;
00270 snprintf(tmp, sizeof(tmp), "%s/*.so", ast_config_AST_MODULE_DIR);
00271 filew = gtk_file_selection_new("Load Module");
00272 gtk_signal_connect(GTK_OBJECT (GTK_FILE_SELECTION(filew)->ok_button),
00273 "clicked", GTK_SIGNAL_FUNC(file_ok_sel), filew);
00274 gtk_signal_connect_object(GTK_OBJECT (GTK_FILE_SELECTION(filew)->cancel_button),
00275 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(filew));
00276 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), (char *)tmp);
00277 gtk_widget_show(filew);
00278 }
00279
00280 static int add_mod(const char *module, const char *description, int usecount, const char *like)
00281 {
00282 char use[10];
00283 const char *pass[4];
00284 int row;
00285 snprintf(use, sizeof(use), "%d", usecount);
00286 pass[0] = module;
00287 pass[1] = description;
00288 pass[2] = use;
00289 pass[3] = NULL;
00290 row = gtk_clist_append(GTK_CLIST(modules), (char **) pass);
00291 gtk_clist_set_row_data(GTK_CLIST(modules), row, (char *) module);
00292 return 0;
00293 }
00294
00295 static int mod_update(void)
00296 {
00297 char *module= NULL;
00298
00299 if (GTK_CLIST(modules)->selection) {
00300 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
00301 }
00302 gtk_clist_freeze(GTK_CLIST(modules));
00303 gtk_clist_clear(GTK_CLIST(modules));
00304 ast_update_module_list(add_mod, NULL);
00305 if (module)
00306 gtk_clist_select_row(GTK_CLIST(modules), gtk_clist_find_row_from_data(GTK_CLIST(modules), module), -1);
00307 gtk_clist_thaw(GTK_CLIST(modules));
00308 return 1;
00309 }
00310
00311 static void exit_now(GtkWidget *widget, gpointer data)
00312 {
00313 ast_loader_unregister(mod_update);
00314 gtk_main_quit();
00315 inuse--;
00316 ast_update_use_count();
00317 ast_unregister_verbose(verboser);
00318 ast_unload_resource("pbx_gtkconsole", 0);
00319 if (option_verbose > 1)
00320 ast_verbose(VERBOSE_PREFIX_2 "GTK Console Monitor Exiting\n");
00321
00322 }
00323
00324 static void exit_completely(GtkWidget *widget, gpointer data)
00325 {
00326 #if 0
00327
00328 ast_cli_command(clipipe[1], "quit");
00329 #else
00330 kill(getpid(), SIGTERM);
00331 #endif
00332 }
00333
00334 static void exit_nicely(GtkWidget *widget, gpointer data)
00335 {
00336 fflush(stdout);
00337 gtk_widget_destroy(window);
00338 }
00339
00340 static void *consolethread(void *data)
00341 {
00342 gtk_widget_show(window);
00343 gdk_threads_enter();
00344 gtk_main();
00345 gdk_threads_leave();
00346 return NULL;
00347 }
00348
00349 static int cli_activate(void)
00350 {
00351 char buf[256] = "";
00352 strncpy(buf, gtk_entry_get_text(GTK_ENTRY(cli)), sizeof(buf) - 1);
00353 gtk_entry_set_text(GTK_ENTRY(cli), "");
00354 if (strlen(buf)) {
00355 ast_cli_command(clipipe[1], buf);
00356 }
00357 return TRUE;
00358 }
00359
00360 static int show_console(void)
00361 {
00362 GtkWidget *hbox;
00363 GtkWidget *wbox;
00364 GtkWidget *notebook;
00365 GtkWidget *sw;
00366 GtkWidget *bbox, *hbbox, *add, *removew, *reloadw;
00367 char *modtitles[3] = { "Module", "Description", "Use Count" };
00368 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00369
00370 statusbar = gtk_statusbar_new();
00371 gtk_widget_show(statusbar);
00372
00373 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
00374 GTK_SIGNAL_FUNC (exit_nicely), window);
00375 gtk_signal_connect(GTK_OBJECT(window), "destroy",
00376 GTK_SIGNAL_FUNC (exit_now), window);
00377 gtk_container_set_border_width(GTK_CONTAINER(window), 10);
00378
00379 quit = gtk_button_new_with_label("Quit Asterisk");
00380 gtk_signal_connect(GTK_OBJECT(quit), "clicked",
00381 GTK_SIGNAL_FUNC (exit_completely), window);
00382 gtk_widget_show(quit);
00383
00384 closew = gtk_button_new_with_label("Close Window");
00385 gtk_signal_connect(GTK_OBJECT(closew), "clicked",
00386 GTK_SIGNAL_FUNC (exit_nicely), window);
00387 gtk_widget_show(closew);
00388
00389 notebook = gtk_notebook_new();
00390 verb = gtk_clist_new(1);
00391 gtk_clist_columns_autosize(GTK_CLIST(verb));
00392 sw = gtk_scrolled_window_new(NULL, NULL);
00393 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
00394 gtk_container_add(GTK_CONTAINER(sw), verb);
00395 gtk_widget_show(verb);
00396 gtk_widget_show(sw);
00397 gtk_widget_set_usize(verb, 640, 400);
00398 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, gtk_label_new("Verbose Status"));
00399
00400
00401 modules = gtk_clist_new_with_titles(3, modtitles);
00402 gtk_clist_columns_autosize(GTK_CLIST(modules));
00403 gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 0, TRUE);
00404 gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 1, TRUE);
00405 gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 2, TRUE);
00406 gtk_clist_set_sort_column(GTK_CLIST(modules), 0);
00407 gtk_clist_set_auto_sort(GTK_CLIST(modules), TRUE);
00408 gtk_clist_column_titles_passive(GTK_CLIST(modules));
00409 sw = gtk_scrolled_window_new(NULL, NULL);
00410 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
00411 gtk_container_add(GTK_CONTAINER(sw), modules);
00412 gtk_clist_set_selection_mode(GTK_CLIST(modules), GTK_SELECTION_BROWSE);
00413 gtk_widget_show(modules);
00414 gtk_widget_show(sw);
00415
00416 add = gtk_button_new_with_label("Load...");
00417 gtk_widget_show(add);
00418 removew = gtk_button_new_with_label("Unload");
00419 gtk_widget_show(removew);
00420 reloadw = gtk_button_new_with_label("Reload");
00421 gtk_widget_show(reloadw);
00422 gtk_signal_connect(GTK_OBJECT(removew), "clicked",
00423 GTK_SIGNAL_FUNC (remove_module), window);
00424 gtk_signal_connect(GTK_OBJECT(add), "clicked",
00425 GTK_SIGNAL_FUNC (add_module), window);
00426 gtk_signal_connect(GTK_OBJECT(reloadw), "clicked",
00427 GTK_SIGNAL_FUNC (reload), window);
00428
00429 bbox = gtk_vbox_new(FALSE, 5);
00430 gtk_widget_show(bbox);
00431
00432 gtk_widget_set_usize(bbox, 100, -1);
00433 gtk_box_pack_start(GTK_BOX(bbox), add, FALSE, FALSE, 5);
00434 gtk_box_pack_start(GTK_BOX(bbox), removew, FALSE, FALSE, 5);
00435 gtk_box_pack_start(GTK_BOX(bbox), reloadw, FALSE, FALSE, 5);
00436
00437 hbbox = gtk_hbox_new(FALSE, 5);
00438 gtk_widget_show(hbbox);
00439
00440 gtk_box_pack_start(GTK_BOX(hbbox), sw, TRUE, TRUE, 5);
00441 gtk_box_pack_start(GTK_BOX(hbbox), bbox, FALSE, FALSE, 5);
00442
00443 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbbox, gtk_label_new("Module Information"));
00444
00445 gtk_widget_show(notebook);
00446
00447 wbox = gtk_hbox_new(FALSE, 5);
00448 gtk_widget_show(wbox);
00449 gtk_box_pack_end(GTK_BOX(wbox), quit, FALSE, FALSE, 5);
00450 gtk_box_pack_end(GTK_BOX(wbox), closew, FALSE, FALSE, 5);
00451
00452 hbox = gtk_vbox_new(FALSE, 0);
00453 gtk_widget_show(hbox);
00454
00455
00456 cli = gtk_entry_new();
00457 gtk_widget_show(cli);
00458
00459 gtk_signal_connect(GTK_OBJECT(cli), "activate",
00460 GTK_SIGNAL_FUNC (cli_activate), NULL);
00461
00462 gtk_box_pack_start(GTK_BOX(hbox), notebook, TRUE, TRUE, 5);
00463 gtk_box_pack_start(GTK_BOX(hbox), wbox, FALSE, FALSE, 5);
00464 gtk_box_pack_start(GTK_BOX(hbox), cli, FALSE, FALSE, 0);
00465 gtk_box_pack_start(GTK_BOX(hbox), statusbar, FALSE, FALSE, 0);
00466 gtk_container_add(GTK_CONTAINER(window), hbox);
00467 gtk_window_set_title(GTK_WINDOW(window), "Asterisk Console");
00468 gtk_widget_grab_focus(cli);
00469 ast_pthread_create(&console_thread, NULL, consolethread, NULL);
00470
00471 usleep(100000);
00472 ast_register_verbose(verboser);
00473 gtk_clist_freeze(GTK_CLIST(verb));
00474 ast_loader_register(mod_update);
00475 gtk_clist_thaw(GTK_CLIST(verb));
00476 gdk_input_add(clipipe[0], GDK_INPUT_READ, cliinput, NULL);
00477 mod_update();
00478 update_statusbar("Asterisk Console Ready");
00479 return 0;
00480 }
00481
00482
00483 static int load_module(void)
00484 {
00485 if (pipe(clipipe)) {
00486 ast_log(LOG_WARNING, "Unable to create CLI pipe\n");
00487 return -1;
00488 }
00489 g_thread_init(NULL);
00490 if (gtk_init_check(NULL, NULL)) {
00491 if (!show_console()) {
00492 inuse++;
00493 ast_update_use_count();
00494 if (option_verbose > 1)
00495 ast_verbose( VERBOSE_PREFIX_2 "Launched GTK Console monitor\n");
00496 } else
00497 ast_log(LOG_WARNING, "Unable to start GTK console\n");
00498 } else {
00499 if (option_debug)
00500 ast_log(LOG_DEBUG, "Unable to start GTK console monitor -- ignoring\n");
00501 else if (option_verbose > 1)
00502 ast_verbose( VERBOSE_PREFIX_2 "GTK is not available -- skipping monitor\n");
00503 }
00504 return 0;
00505 }
00506
00507 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GTK Console",
00508 .load = load_module,
00509 .unload = unload_module,
00510 .reload = reload,
00511 );