Sat Aug 6 00:39:31 2011

Asterisk developer's documentation


pbx_gtkconsole.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief GTK Console monitor -- very kludgy right now
00022  * 
00023  */
00024 
00025 /*** MODULEINFO
00026    <depend>gtk</depend>
00027    <defaultenabled>no</defaultenabled>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 117507 $")
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       /* Kill off the main thread */
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          /* We just got a message within 100ms, so just schedule an update
00129             in the near future */
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    if (*stuff == 127) {
00143       stuff++;
00144    }
00145 
00146    ast_mutex_lock(&verb_lock);
00147    /* Lock appropriately if we're really being called in verbose mode */
00148    __verboser(stuff);
00149    ast_mutex_unlock(&verb_lock);
00150 }
00151 
00152 static void cliinput(void *data, int source, GdkInputCondition ic)
00153 {
00154    static char buf[256];
00155    static int offset = 0;
00156    int res;
00157    char *c;
00158    char *l;
00159    char n;
00160    /* Read as much stuff is there */
00161    res = read(source, buf + offset, sizeof(buf) - 1 - offset);
00162    if (res > -1)
00163       buf[res + offset] = '\0';
00164    /* make sure we've null terminated whatever we have so far */
00165    c = buf;
00166    l = buf;
00167    while(*c) {
00168       if (*c == '\n') {
00169          /* Keep the trailing \n */
00170          c++;
00171          n = *c;
00172          *c = '\0';
00173          __verboser(l);
00174          *(c - 1) = '\0';
00175          *c = n;
00176          l = c;
00177       } else
00178       c++;
00179    }
00180    if (strlen(l)) {
00181       /* We have some left over */
00182       memmove(buf, l, strlen(l) + 1);
00183       offset = strlen(buf);
00184    } else {
00185       offset = 0;
00186    }
00187 
00188 }
00189 
00190 static void remove_module(void)
00191 {
00192    int res;
00193    char *module;
00194    char buf[256];
00195    if (GTK_CLIST(modules)->selection) {
00196       module = (char *) gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
00197       gdk_threads_leave();
00198       res = ast_unload_resource(module, 0);
00199       gdk_threads_enter();
00200       if (res) {
00201          snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
00202          update_statusbar(buf);
00203       } else {
00204          snprintf(buf, sizeof(buf), "Module '%s' removed", module);
00205          update_statusbar(buf);
00206       }
00207    }
00208 }
00209 
00210 static int reload(void)
00211 {
00212    int res, x;
00213    char *module;
00214    char buf[256];
00215    if (GTK_CLIST(modules)->selection) {
00216       module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
00217       module = strdup(module);
00218       if (module) {
00219          gdk_threads_leave();
00220          res = ast_unload_resource(module, 0);
00221          gdk_threads_enter();
00222          if (res) {
00223             snprintf(buf, sizeof(buf), "Module '%s' is in use", module);
00224             update_statusbar(buf);
00225          } else {
00226             gdk_threads_leave();
00227             res = ast_load_resource(module);
00228             gdk_threads_enter();
00229             if (res) {
00230                snprintf(buf, sizeof(buf), "Error reloading module '%s'", module);
00231             } else {
00232                snprintf(buf, sizeof(buf), "Module '%s' reloaded", module);
00233             }
00234             for (x=0; x < GTK_CLIST(modules)->rows; x++) {
00235                if (!strcmp((char *)gtk_clist_get_row_data(GTK_CLIST(modules), x), module)) {
00236                   gtk_clist_select_row(GTK_CLIST(modules), x, -1);
00237                   break;
00238                }
00239             }
00240             update_statusbar(buf);
00241             
00242          }
00243          free(module);
00244       }
00245    }
00246 
00247    return 0;
00248 }
00249 
00250 static void file_ok_sel(GtkWidget *w, GtkFileSelection *fs)
00251 {
00252    char tmp[PATH_MAX];
00253    char *module = gtk_file_selection_get_filename(fs);
00254    char buf[256];
00255    snprintf(tmp, sizeof(tmp), "%s/", ast_config_AST_MODULE_DIR);
00256    if (!strncmp(module, (char *)tmp, strlen(tmp))) 
00257       module += strlen(tmp);
00258    gdk_threads_leave();
00259    if (ast_load_resource(module)) {
00260       snprintf(buf, sizeof(buf), "Error loading module '%s'.", module);
00261       update_statusbar(buf);
00262    } else {
00263       snprintf(buf, sizeof(buf), "Module '%s' loaded", module);
00264       update_statusbar(buf);
00265    }
00266    gdk_threads_enter();
00267    gtk_widget_destroy(GTK_WIDGET(fs));
00268 }
00269 
00270 static void add_module(void)
00271 {
00272    char tmp[PATH_MAX];
00273    GtkWidget *filew;
00274    snprintf(tmp, sizeof(tmp), "%s/*.so", ast_config_AST_MODULE_DIR);
00275    filew = gtk_file_selection_new("Load Module");
00276    gtk_signal_connect(GTK_OBJECT (GTK_FILE_SELECTION(filew)->ok_button),
00277                "clicked", GTK_SIGNAL_FUNC(file_ok_sel), filew);
00278    gtk_signal_connect_object(GTK_OBJECT (GTK_FILE_SELECTION(filew)->cancel_button),
00279                "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(filew));
00280    gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), (char *)tmp);
00281    gtk_widget_show(filew);
00282 }
00283 
00284 static int add_mod(const char *module, const char *description, int usecount, const char *like)
00285 {
00286    char use[10];
00287    const char *pass[4];
00288    int row;
00289    snprintf(use, sizeof(use), "%d", usecount);
00290    pass[0] = module;
00291    pass[1] = description;
00292    pass[2] = use;
00293    pass[3] = NULL;
00294    row = gtk_clist_append(GTK_CLIST(modules), (char **) pass);
00295    gtk_clist_set_row_data(GTK_CLIST(modules), row, (char *) module);
00296    return 0;   
00297 }
00298 
00299 static int mod_update(void)
00300 {
00301    char *module= NULL;
00302    /* Update the mod stuff */
00303    if (GTK_CLIST(modules)->selection) {
00304       module= (char *)gtk_clist_get_row_data(GTK_CLIST(modules), (long) GTK_CLIST(modules)->selection->data);
00305    }
00306    gtk_clist_freeze(GTK_CLIST(modules));
00307    gtk_clist_clear(GTK_CLIST(modules));
00308    ast_update_module_list(add_mod, NULL);
00309    if (module)
00310       gtk_clist_select_row(GTK_CLIST(modules), gtk_clist_find_row_from_data(GTK_CLIST(modules), module), -1);
00311    gtk_clist_thaw(GTK_CLIST(modules));
00312    return 1;
00313 }
00314 
00315 static void exit_now(GtkWidget *widget, gpointer data)
00316 {
00317    ast_loader_unregister(mod_update);
00318    gtk_main_quit();
00319    inuse--;
00320    ast_update_use_count();
00321    ast_unregister_verbose(verboser);
00322    ast_unload_resource("pbx_gtkconsole", 0);
00323    if (option_verbose > 1)
00324       ast_verbose(VERBOSE_PREFIX_2 "GTK Console Monitor Exiting\n");
00325    /* XXX Trying to quit after calling this makes asterisk segfault XXX */
00326 }
00327 
00328 static void exit_completely(GtkWidget *widget, gpointer data)
00329 {
00330 #if 0
00331    /* Clever... */
00332    ast_cli_command(clipipe[1], "quit");
00333 #else
00334    kill(getpid(), SIGTERM);
00335 #endif
00336 }
00337 
00338 static void exit_nicely(GtkWidget *widget, gpointer data)
00339 {
00340    fflush(stdout);
00341    gtk_widget_destroy(window);
00342 }
00343 
00344 static void *consolethread(void *data)
00345 {
00346    gtk_widget_show(window);
00347    gdk_threads_enter();
00348    gtk_main();
00349    gdk_threads_leave();
00350    return NULL;
00351 }
00352 
00353 static int cli_activate(void)
00354 {
00355    char buf[256] = "";
00356    strncpy(buf, gtk_entry_get_text(GTK_ENTRY(cli)), sizeof(buf) - 1);
00357    gtk_entry_set_text(GTK_ENTRY(cli), "");
00358    if (strlen(buf)) {
00359       ast_cli_command(clipipe[1], buf);
00360    }
00361    return TRUE;
00362 }
00363 
00364 static int show_console(void)
00365 {
00366    GtkWidget *hbox;
00367    GtkWidget *wbox;
00368    GtkWidget *notebook;
00369    GtkWidget *sw;
00370    GtkWidget *bbox, *hbbox, *add, *removew, *reloadw;
00371    char *modtitles[3] = { "Module", "Description", "Use Count" };
00372    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00373    
00374    statusbar = gtk_statusbar_new();
00375    gtk_widget_show(statusbar);
00376    
00377    gtk_signal_connect(GTK_OBJECT(window), "delete_event",
00378          GTK_SIGNAL_FUNC (exit_nicely), window);
00379    gtk_signal_connect(GTK_OBJECT(window), "destroy",
00380          GTK_SIGNAL_FUNC (exit_now), window);
00381    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
00382 
00383    quit = gtk_button_new_with_label("Quit Asterisk");
00384    gtk_signal_connect(GTK_OBJECT(quit), "clicked",
00385          GTK_SIGNAL_FUNC (exit_completely), window);
00386    gtk_widget_show(quit);
00387 
00388    closew = gtk_button_new_with_label("Close Window");
00389    gtk_signal_connect(GTK_OBJECT(closew), "clicked",
00390          GTK_SIGNAL_FUNC (exit_nicely), window);
00391    gtk_widget_show(closew);
00392 
00393    notebook = gtk_notebook_new();
00394    verb = gtk_clist_new(1);
00395    gtk_clist_columns_autosize(GTK_CLIST(verb));
00396    sw = gtk_scrolled_window_new(NULL, NULL);
00397    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
00398    gtk_container_add(GTK_CONTAINER(sw), verb);
00399    gtk_widget_show(verb);
00400    gtk_widget_show(sw);
00401    gtk_widget_set_usize(verb, 640, 400);
00402    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, gtk_label_new("Verbose Status"));
00403 
00404    
00405    modules = gtk_clist_new_with_titles(3, modtitles);
00406    gtk_clist_columns_autosize(GTK_CLIST(modules));
00407    gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 0, TRUE);
00408    gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 1, TRUE);
00409    gtk_clist_set_column_auto_resize(GTK_CLIST(modules), 2, TRUE);
00410    gtk_clist_set_sort_column(GTK_CLIST(modules), 0);
00411    gtk_clist_set_auto_sort(GTK_CLIST(modules), TRUE);
00412    gtk_clist_column_titles_passive(GTK_CLIST(modules));
00413    sw = gtk_scrolled_window_new(NULL, NULL);
00414    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
00415    gtk_container_add(GTK_CONTAINER(sw), modules);
00416    gtk_clist_set_selection_mode(GTK_CLIST(modules), GTK_SELECTION_BROWSE);
00417    gtk_widget_show(modules);
00418    gtk_widget_show(sw);
00419 
00420    add = gtk_button_new_with_label("Load...");
00421    gtk_widget_show(add);
00422    removew = gtk_button_new_with_label("Unload");
00423    gtk_widget_show(removew);
00424    reloadw = gtk_button_new_with_label("Reload");
00425    gtk_widget_show(reloadw);
00426    gtk_signal_connect(GTK_OBJECT(removew), "clicked",
00427          GTK_SIGNAL_FUNC (remove_module), window);
00428    gtk_signal_connect(GTK_OBJECT(add), "clicked",
00429          GTK_SIGNAL_FUNC (add_module), window);
00430    gtk_signal_connect(GTK_OBJECT(reloadw), "clicked",
00431          GTK_SIGNAL_FUNC (reload), window);
00432       
00433    bbox = gtk_vbox_new(FALSE, 5);
00434    gtk_widget_show(bbox);
00435 
00436    gtk_widget_set_usize(bbox, 100, -1);
00437    gtk_box_pack_start(GTK_BOX(bbox), add, FALSE, FALSE, 5);
00438    gtk_box_pack_start(GTK_BOX(bbox), removew, FALSE, FALSE, 5);
00439    gtk_box_pack_start(GTK_BOX(bbox), reloadw, FALSE, FALSE, 5);
00440 
00441    hbbox = gtk_hbox_new(FALSE, 5);
00442    gtk_widget_show(hbbox);
00443    
00444    gtk_box_pack_start(GTK_BOX(hbbox), sw, TRUE, TRUE, 5);
00445    gtk_box_pack_start(GTK_BOX(hbbox), bbox, FALSE, FALSE, 5);
00446 
00447    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbbox, gtk_label_new("Module Information"));
00448 
00449    gtk_widget_show(notebook);
00450 
00451    wbox = gtk_hbox_new(FALSE, 5);
00452    gtk_widget_show(wbox);
00453    gtk_box_pack_end(GTK_BOX(wbox), quit, FALSE, FALSE, 5);
00454    gtk_box_pack_end(GTK_BOX(wbox), closew, FALSE, FALSE, 5);
00455 
00456    hbox = gtk_vbox_new(FALSE, 0);
00457    gtk_widget_show(hbox);
00458    
00459    /* Command line */
00460    cli = gtk_entry_new();
00461    gtk_widget_show(cli);
00462 
00463    gtk_signal_connect(GTK_OBJECT(cli), "activate",
00464          GTK_SIGNAL_FUNC (cli_activate), NULL);
00465 
00466    gtk_box_pack_start(GTK_BOX(hbox), notebook, TRUE, TRUE, 5);
00467    gtk_box_pack_start(GTK_BOX(hbox), wbox, FALSE, FALSE, 5);
00468    gtk_box_pack_start(GTK_BOX(hbox), cli, FALSE, FALSE, 0);
00469    gtk_box_pack_start(GTK_BOX(hbox), statusbar, FALSE, FALSE, 0);
00470    gtk_container_add(GTK_CONTAINER(window), hbox);
00471    gtk_window_set_title(GTK_WINDOW(window), "Asterisk Console");
00472    gtk_widget_grab_focus(cli);
00473    ast_pthread_create(&console_thread, NULL, consolethread, NULL);
00474    /* XXX Okay, seriously fix me! XXX */
00475    usleep(100000);
00476    ast_register_verbose(verboser);
00477    gtk_clist_freeze(GTK_CLIST(verb));
00478    ast_loader_register(mod_update);
00479    gtk_clist_thaw(GTK_CLIST(verb));
00480    gdk_input_add(clipipe[0], GDK_INPUT_READ, cliinput, NULL);
00481    mod_update();
00482    update_statusbar("Asterisk Console Ready");
00483    return 0;
00484 }
00485 
00486 
00487 static int load_module(void)
00488 {
00489    if (pipe(clipipe)) {
00490       ast_log(LOG_WARNING, "Unable to create CLI pipe\n");
00491       return -1;
00492    }
00493    g_thread_init(NULL);
00494    if (gtk_init_check(NULL, NULL))  {
00495       if (!show_console()) {
00496          inuse++;
00497          ast_update_use_count();
00498          if (option_verbose > 1)
00499             ast_verbose( VERBOSE_PREFIX_2 "Launched GTK Console monitor\n");     
00500       } else
00501          ast_log(LOG_WARNING, "Unable to start GTK console\n");
00502    } else {
00503       if (option_debug)
00504          ast_log(LOG_DEBUG, "Unable to start GTK console monitor -- ignoring\n");
00505       else if (option_verbose > 1)
00506          ast_verbose( VERBOSE_PREFIX_2 "GTK is not available -- skipping monitor\n");
00507    }
00508    return 0;
00509 }
00510 
00511 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GTK Console",
00512       .load = load_module,
00513       .unload = unload_module,
00514       .reload = reload,
00515           );

Generated on Sat Aug 6 00:39:31 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7