Panda3D
winStats.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 winStats.cxx
10  * @author drose
11  * @date 2003-12-02
12  */
13 
14 #include "pandatoolbase.h"
15 
16 #include "winStatsServer.h"
17 #include "config_pstatclient.h"
18 
19 #include <windows.h>
20 
21 static const char *toplevel_class_name = "pstats";
22 static WinStatsServer *server = nullptr;
23 
24 /**
25  *
26  */
27 static LONG WINAPI
28 toplevel_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
29  switch (msg) {
30  case WM_TIMER:
31  server->poll();
32  break;
33 
34  case WM_DESTROY:
35  PostQuitMessage(0);
36  break;
37 
38  default:
39  break;
40  }
41 
42  return DefWindowProc(hwnd, msg, wparam, lparam);
43 }
44 
45 
46 /**
47  * Creates the initial, toplevel window for the application.
48  */
49 static HWND
50 create_toplevel_window(HINSTANCE application) {
51  WNDCLASS wc;
52 
53  ZeroMemory(&wc, sizeof(WNDCLASS));
54  wc.lpfnWndProc = (WNDPROC)toplevel_window_proc;
55  wc.hInstance = application;
56  wc.lpszClassName = toplevel_class_name;
57 
58  if (!RegisterClass(&wc)) {
59  nout << "Could not register window class!\n";
60  exit(1);
61  }
62 
63  DWORD window_style = WS_POPUP | WS_SYSMENU | WS_ICONIC;
64 
65  std::ostringstream strm;
66  strm << "PStats " << pstats_port;
67  std::string window_name = strm.str();
68 
69  HWND toplevel_window =
70  CreateWindow(toplevel_class_name, window_name.c_str(), window_style,
71  CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
72  nullptr, nullptr, application, 0);
73  if (!toplevel_window) {
74  nout << "Could not create toplevel window!\n";
75  exit(1);
76  }
77 
78  return toplevel_window;
79 }
80 
81 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
82  HINSTANCE application = GetModuleHandle(nullptr);
83  HWND toplevel_window = create_toplevel_window(application);
84 
85  ShowWindow(toplevel_window, SW_SHOWMINIMIZED);
86 
87  // Create the server object.
88  server = new WinStatsServer;
89  if (!server->listen()) {
90  std::ostringstream stream;
91  stream
92  << "Unable to open port " << pstats_port
93  << ". Try specifying a different\n"
94  << "port number using pstats-port in your Config file.";
95  std::string str = stream.str();
96  MessageBox(toplevel_window, str.c_str(), "PStats error",
97  MB_OK | MB_ICONEXCLAMATION);
98  exit(1);
99  }
100 
101  // Set up a timer to poll the pstats every so often.
102  SetTimer(toplevel_window, 1, 200, nullptr);
103 
104  // Now get lost in the Windows message loop.
105  MSG msg;
106  int retval;
107  retval = GetMessage(&msg, nullptr, 0, 0);
108  while (retval != 0) {
109  if (retval == -1) {
110  nout << "Error processing message queue.\n";
111  exit(1);
112  }
113  TranslateMessage(&msg);
114  DispatchMessage(&msg);
115  retval = GetMessage(&msg, nullptr, 0, 0);
116  }
117 
118  return (0);
119 }
120 
121 // WinMain() is the correct way to start a Windows-only application, but it is
122 // sometimes more convenient during development to use main() instead, which
123 // doesn't squelch the stderr output.
124 
125 int main(int argc, char *argv[]) {
126  return WinMain(nullptr, nullptr, nullptr, 0);
127 }
The class that owns the main loop, waiting for client connections.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.