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: 126356 $")
00033
00034 #include <fcntl.h>
00035 #include <signal.h>
00036 #include <sys/time.h>
00037
00038 #include <gtk/gtk.h>
00039 #include <glib.h>
00040
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/config.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/logger.h"
00045 #include "asterisk/cli.h"
00046 #include "asterisk/utils.h"
00047 #include "asterisk/paths.h"
00048 #include "asterisk/term.h"
00049
00050 AST_MUTEX_DEFINE_STATIC(verb_lock);
00051
00052 static pthread_t console_thread;
00053
00054 static int inuse=0;
00055 static int clipipe[2];
00056 static int cleanupid = -1;
00057
00058 static GtkWidget *window;
00059 static GtkWidget *quit;
00060 static GtkWidget *closew;
00061 static GtkWidget *verb;
00062 static GtkWidget *modules;
00063 static GtkWidget *statusbar;
00064 static GtkWidget *cli;
00065
00066 static struct timeval last;
00067
00068 static void update_statusbar(char *msg)
00069 {
00070 gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 1);
00071 gtk_statusbar_push(GTK_STATUSBAR(statusbar), 1, msg);
00072 }
00073
00074 static int unload_module(void)
00075 {
00076 if (inuse) {
00077
00078 pthread_cancel(console_thread);
00079 gdk_threads_enter();
00080 gtk_widget_destroy(window);
00081 gdk_threads_leave();
00082 close(clipipe[0]);
00083 close(clipipe[1]);
00084 }
00085 return 0;
00086 }
00087
00088 static int cleanup(void *useless)
00089 {
00090 gdk_threads_enter();
00091 gtk_clist_thaw(GTK_CLIST(verb));
00092 gtk_widget_queue_resize(verb->parent);
00093 gtk_clist_moveto(GTK_CLIST(verb), GTK_CLIST(verb)->rows - 1, 0, 0, 0);
00094 cleanupid = -1;
00095 gdk_threads_leave();
00096 return 0;
00097 }
00098
00099
00100 static void __verboser(const char *_stuff)
00101 {
00102 char *s2[2];
00103 struct timeval tv;
00104 int ms;
00105 char *stuff;
00106
00107 stuff = ast_strdupa(_stuff);
00108 term_strip(stuff, stuff, strlen(stuff) + 1);
00109
00110 s2[0] = (char *)stuff;
00111 s2[1] = NULL;
00112 gtk_clist_freeze(GTK_CLIST(verb));
00113 gtk_clist_append(GTK_CLIST(verb), s2);
00114 if (!ast_tvzero(last)) {
00115 gdk_threads_leave();
00116 gettimeofday(&tv, NULL);
00117 if (cleanupid > -1)
00118 gtk_timeout_remove(cleanupid);
00119 ms = ast_tvdiff_ms(tv, last);
00120 if (ms < 100) {
00121
00122
00123 cleanupid = gtk_timeout_add(200, cleanup, NULL);
00124 } else {
00125 cleanup(&cleanupid);
00126 }
00127 last = tv;
00128 } else {
00129 gettimeofday(&last, NULL);
00130 }
00131 }
00132
00133 static void verboser(const char *stuff)
00134 {
00135 if (*stuff == 127) {
00136 stuff++;
00137 }
00138
00139 ast_mutex_lock(&verb_lock);
00140
00141 __verboser(stuff);
00142 ast_mutex_unlock(&verb_lock);
00143 }
00144
00145 static void cliinput(void *data, int source, GdkInputCondition ic)
00146 {
00147 static char buf[256];
00148 static int offset = 0;
00149 int res;
00150 char *c;
00151 char *l;
00152 char n;
00153
00154 res = read(source, buf + offset, sizeof(buf) - 1 - offset);
00155 if (res > -1)
00156 buf[res + offset] = '\0';
00157
00158 c = buf;
00159 l = buf;
00160 while(*c) {
00161 if (*c == '\n') {
00162
00163 c++;
00164 n = *c;
00165 *c = '\0';
00166 __verboser(l);
00167 *(c - 1) = '\0';
00168 *c = n;
00169 l = c;
00170 } else
00171 c++;
00172 }
00173 if (strlen(l)) {
00174
00175 memmove(buf, l, strlen(l) + 1);
00176 offset = strlen(buf);
00177 } else {
00178 offset = 0;
00179 }
00180
00181 }
00182
00183 static void remove_module(void)
00184 {
00185 int res;
00186 char *module;
00187 char buf[256];
00188 if (GTK_CLIST(modules)->selection) {
00189 module = (char *) gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
00190 gdk_threads_leave();
00191 res = ast_unload_resource(module, 0);
00192 gdk_threads_enter();
00193 if (res) {
00194 snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
00195 update_statusbar(buf);
00196 } else {
00197 snprintf(buf, sizeof(buf), "Module '%s' removed", module);
00198 update_statusbar(buf);
00199 }
00200 }
00201 }
00202
00203 static int reload(void)
00204 {
00205 int res, x;
00206 char *module;
00207 char buf[256];
00208 if (GTK_CLIST(modules)->selection) {
00209 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
00210 module = strdup(module);
00211 if (module) {
00212 gdk_threads_leave();
00213 res = ast_unload_resource(module, 0);
00214 gdk_threads_enter();
00215 if (res) {
00216 snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
00217 update_statusbar(buf);
00218 } else {
00219 gdk_threads_leave();
00220 res = ast_load_resource(module);
00221 gdk_threads_enter();
00222 if (res) {
00223 snprintf(buf, sizeof(buf), "Error reloading module '%s'", module);
00224 } else {
00225 snprintf(buf, sizeof(buf), "Module '%s' reloaded", module);
00226 }
00227 for (x=0; x < GTK_CLIST(modules)->rows; x++) {
00228 if (!strcmp((char *)gtk_clist_get_row_data(GTK_CLIST(modules), x), module)) {
00229 gtk_clist_select_row(GTK_CLIST(modules), x, -1);
00230 break;
00231 }
00232 }
00233 update_statusbar(buf);
00234
00235 }
00236 free(module);
00237 }
00238 }
00239
00240 return AST_MODULE_LOAD_SUCCESS;
00241 }
00242
00243 static void file_ok_sel(GtkWidget *w, GtkFileSelection *fs)
00244 {
00245 char tmp[PATH_MAX];
00246 char *module = gtk_file_selection_get_filename(fs);
00247 char buf[256];
00248 snprintf(tmp, sizeof(tmp), "%s/", ast_config_AST_MODULE_DIR);
00249 if (!strncmp(module, (char *)tmp, strlen(tmp)))
00250 module += strlen(tmp);
00251 gdk_threads_leave();
00252 if (ast_load_resource(module)) {
00253 snprintf(buf, sizeof(buf), "Error loading module '%s'.", module);
00254 update_statusbar(buf);
00255 } else {
00256 snprintf(buf, sizeof(buf), "Module '%s' loaded", module);
00257 update_statusbar(buf);
00258 }
00259 gdk_threads_enter();
00260 gtk_widget_destroy(GTK_WIDGET(fs));
00261 }
00262
00263 static void add_module(void)
00264 {
00265 char tmp[PATH_MAX];
00266 GtkWidget *filew;
00267 snprintf(tmp, sizeof(tmp), "%s/*.so", ast_config_AST_MODULE_DIR);
00268 filew = gtk_file_selection_new("Load Module");
00269 gtk_signal_connect(GTK_OBJECT (GTK_FILE_SELECTION(filew)->ok_button),
00270 "clicked", GTK_SIGNAL_FUNC(file_ok_sel), filew);
00271 gtk_signal_connect_object(GTK_OBJECT (GTK_FILE_SELECTION(filew)->cancel_button),
00272 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(filew));
00273 gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), (char *)tmp);
00274 gtk_widget_show(filew);
00275 }
00276
00277 static int add_mod(const char *module, const char *description, int usecount, const char *like)
00278 {
00279 char use[10];
00280 const char *pass[4];
00281 int row;
00282 snprintf(use, sizeof(use), "%d", usecount);
00283 pass[0] = module;
00284 pass[1] = description;
00285 pass[2] = use;
00286 pass[3] = NULL;
00287 row = gtk_clist_append(GTK_CLIST(modules), (char **) pass);
00288 gtk_clist_set_row_data(GTK_CLIST(modules), row, (char *) module);
00289 return 0;
00290 }
00291
00292 static int mod_update(void)
00293 {
00294 char *module= NULL;
00295
00296 if (GTK_CLIST(modules)->selection) {
00297 module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
00298 }
00299 gtk_clist_freeze(GTK_CLIST(modules));
00300 gtk_clist_clear(GTK_CLIST(modules));
00301 ast_update_module_list(add_mod, NULL);
00302 if (module)
00303 gtk_clist_select_row(GTK_CLIST(modules), gtk_clist_find_row_from_data(GTK_CLIST(modules), module), -1);
00304 gtk_clist_thaw(GTK_CLIST(modules));
00305 return 1;
00306 }
00307
00308 static void exit_now(GtkWidget *widget, gpointer data)
00309 {
00310 int res;
00311
00312 ast_loader_unregister(mod_update);
00313 gtk_main_quit();
00314 inuse--;
00315 ast_update_use_count();
00316 res = ast_unregister_verbose(verboser);
00317 ast_unload_resource("pbx_gtkconsole", 0);
00318 ast_verb(2, "GTK Console Monitor Exiting\n");
00319
00320 }
00321
00322 static void exit_completely(GtkWidget *widget, gpointer data)
00323 {
00324 #if 0
00325
00326 ast_cli_command(clipipe[1], "quit");
00327 #else
00328 kill(getpid(), SIGTERM);
00329 #endif
00330 }
00331
00332 static void exit_nicely(GtkWidget *widget, gpointer data)
00333 {
00334 fflush(stdout);
00335 gtk_widget_destroy(window);
00336 }
00337
00338 static void *consolethread(void *data)
00339 {
00340 gtk_widget_show(window);
00341 gdk_threads_enter();
00342 gtk_main();
00343 gdk_threads_leave();
00344 return NULL;
00345 }
00346
00347 static int cli_activate(void)
00348 {
00349 char buf[256] = "";
00350 strncpy(buf, gtk_entry_get_text(GTK_ENTRY(cli)), sizeof(buf) - 1);
00351 gtk_entry_set_text(GTK_ENTRY(cli), "");
00352 if (strlen(buf)) {
00353 ast_cli_command(clipipe[1], buf);
00354 }
00355 return TRUE;
00356 }
00357
00358 static int show_console(void)
00359 {
00360 GtkWidget *hbox;
00361 GtkWidget *wbox;
00362 GtkWidget *notebook;
00363 GtkWidget *sw;
00364 GtkWidget *bbox, *hbbox, *add, *removew, *reloadw;
00365 char *modtitles[3] = { "Module", "Description", "Use Count" };
00366 int res;
00367
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 res = 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 AST_MODULE_LOAD_FAILURE;
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 ast_verb(2, "Launched GTK Console monitor\n");
00495 } else
00496 ast_log(LOG_WARNING, "Unable to start GTK console\n");
00497 } else {
00498 if (option_debug)
00499 ast_log(LOG_DEBUG, "Unable to start GTK console monitor -- ignoring\n");
00500 else
00501 ast_verb(2, "GTK is not available -- skipping monitor\n");
00502 }
00503 return AST_MODULE_LOAD_SUCCESS;
00504 }
00505
00506 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GTK Console",
00507 .load = load_module,
00508 .unload = unload_module,
00509 .reload = reload,
00510 );