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