Panda3D
 All Classes Functions Variables Enumerations
gtkStats.cxx
00001 // Filename: gtkStats.cxx
00002 // Created by:  drose (16Jan06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "pandatoolbase.h"
00016 #include "gtkStats.h"
00017 #include "gtkStatsServer.h"
00018 #include "config_pstats.h"
00019 #include "pystub.h"
00020 
00021 GtkWidget *main_window;
00022 static GtkStatsServer *server = NULL;
00023 
00024 static gboolean
00025 delete_event(GtkWidget *widget,
00026        GdkEvent *event, gpointer data) {
00027   // Returning FALSE to indicate we should destroy the main window
00028   // when the user selects "close".
00029   return FALSE;
00030 }
00031 
00032 static void
00033 destroy(GtkWidget *widget, gpointer data) {
00034   gtk_main_quit();
00035 }
00036 
00037 static gboolean
00038 timer(gpointer data) {
00039   static int count = 0;
00040   server->poll();
00041 
00042   if (++count == 5) {
00043     count = 0;
00044     // Every once in a while, say once a second, we call this
00045     // function, which should force gdk to make all changes visible.
00046     // We do this in case we are getting starved and falling behind,
00047     // so that the user still gets a chance to see *something* happen
00048     // onscreen, even if it's just increasingly old data.
00049     gdk_window_process_all_updates();
00050   }
00051 
00052   return TRUE;
00053 }
00054 
00055 int
00056 main(int argc, char *argv[]) {
00057   // A call to pystub() to force libpystub.so to be linked in.
00058   pystub();
00059 
00060   gtk_init(&argc, &argv);
00061 
00062   main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00063 
00064   gtk_window_set_title(GTK_WINDOW(main_window), "PStats");
00065 
00066   // Connect the delete and destroy events, so the user can exit the
00067   // application by closing the main window.
00068   g_signal_connect(G_OBJECT(main_window), "delete_event",
00069        G_CALLBACK(delete_event), NULL);
00070   
00071   g_signal_connect(G_OBJECT(main_window), "destroy",
00072        G_CALLBACK(destroy), NULL);
00073 
00074   ostringstream stream;
00075   stream << "Listening on port " << pstats_port;
00076   string str = stream.str();
00077   GtkWidget *label = gtk_label_new(str.c_str());
00078   gtk_container_add(GTK_CONTAINER(main_window), label);
00079   gtk_widget_show(label);
00080 
00081   // Create the server object.
00082   server = new GtkStatsServer;
00083   if (!server->listen()) {
00084     ostringstream stream;
00085     stream 
00086       << "Unable to open port " << pstats_port
00087       << ".  Try specifying a different\n"
00088       << "port number using pstats-port in your Config file.";
00089     string str = stream.str();
00090 
00091     GtkWidget *dialog = 
00092       gtk_message_dialog_new(GTK_WINDOW(main_window),
00093            GTK_DIALOG_DESTROY_WITH_PARENT,
00094            GTK_MESSAGE_ERROR,
00095            GTK_BUTTONS_CLOSE,
00096            str.c_str());
00097     gtk_dialog_run(GTK_DIALOG(dialog));
00098     gtk_widget_destroy(dialog);
00099     exit(1);
00100   }
00101 
00102   gtk_widget_show(main_window);
00103 
00104   // Set up a timer to poll the pstats every so often.
00105   g_timeout_add(200, timer, NULL);
00106 
00107   // Now get lost in the message loop.
00108   gtk_main();
00109 
00110   return (0);
00111 }
 All Classes Functions Variables Enumerations