Panda3D
gtkStats.cxx
1 // Filename: gtkStats.cxx
2 // Created by: drose (16Jan06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "pandatoolbase.h"
16 #include "gtkStats.h"
17 #include "gtkStatsServer.h"
18 #include "config_pstats.h"
19 #include "pystub.h"
20 
21 GtkWidget *main_window;
22 static GtkStatsServer *server = NULL;
23 
24 static gboolean
25 delete_event(GtkWidget *widget,
26  GdkEvent *event, gpointer data) {
27  // Returning FALSE to indicate we should destroy the main window
28  // when the user selects "close".
29  return FALSE;
30 }
31 
32 static void
33 destroy(GtkWidget *widget, gpointer data) {
34  gtk_main_quit();
35 }
36 
37 static gboolean
38 timer(gpointer data) {
39  static int count = 0;
40  server->poll();
41 
42  if (++count == 5) {
43  count = 0;
44  // Every once in a while, say once a second, we call this
45  // function, which should force gdk to make all changes visible.
46  // We do this in case we are getting starved and falling behind,
47  // so that the user still gets a chance to see *something* happen
48  // onscreen, even if it's just increasingly old data.
49  gdk_window_process_all_updates();
50  }
51 
52  return TRUE;
53 }
54 
55 int
56 main(int argc, char *argv[]) {
57  // A call to pystub() to force libpystub.so to be linked in.
58  pystub();
59 
60  gtk_init(&argc, &argv);
61 
62  main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
63 
64  gtk_window_set_title(GTK_WINDOW(main_window), "PStats");
65 
66  // Connect the delete and destroy events, so the user can exit the
67  // application by closing the main window.
68  g_signal_connect(G_OBJECT(main_window), "delete_event",
69  G_CALLBACK(delete_event), NULL);
70 
71  g_signal_connect(G_OBJECT(main_window), "destroy",
72  G_CALLBACK(destroy), NULL);
73 
74  ostringstream stream;
75  stream << "Listening on port " << pstats_port;
76  string str = stream.str();
77  GtkWidget *label = gtk_label_new(str.c_str());
78  gtk_container_add(GTK_CONTAINER(main_window), label);
79  gtk_widget_show(label);
80 
81  // Create the server object.
82  server = new GtkStatsServer;
83  if (!server->listen()) {
84  ostringstream stream;
85  stream
86  << "Unable to open port " << pstats_port
87  << ". Try specifying a different\n"
88  << "port number using pstats-port in your Config file.";
89  string str = stream.str();
90 
91  GtkWidget *dialog =
92  gtk_message_dialog_new(GTK_WINDOW(main_window),
93  GTK_DIALOG_DESTROY_WITH_PARENT,
94  GTK_MESSAGE_ERROR,
95  GTK_BUTTONS_CLOSE,
96  "%s", str.c_str());
97  gtk_dialog_run(GTK_DIALOG(dialog));
98  gtk_widget_destroy(dialog);
99  exit(1);
100  }
101 
102  gtk_widget_show(main_window);
103 
104  // Set up a timer to poll the pstats every so often.
105  g_timeout_add(200, timer, NULL);
106 
107  // Now get lost in the message loop.
108  gtk_main();
109 
110  return (0);
111 }
The class that owns the main loop, waiting for client connections.
void poll()
Checks for any network activity and handles it, if appropriate, and then returns. ...
Definition: pStatServer.cxx:96
bool listen(int port=-1)
Establishes a port number that the manager will listen on for TCP connections.
Definition: pStatServer.cxx:59