Panda3D
|
00001 // Filename: winStats.cxx 00002 // Created by: drose (02Dec03) 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 00017 #include "winStatsServer.h" 00018 #include "config_pstats.h" 00019 #include "pystub.h" 00020 00021 #include <windows.h> 00022 00023 static const char *toplevel_class_name = "pstats"; 00024 static WinStatsServer *server = NULL; 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: toplevel_window_proc 00028 // Description: 00029 //////////////////////////////////////////////////////////////////// 00030 static LONG WINAPI 00031 toplevel_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { 00032 switch (msg) { 00033 case WM_TIMER: 00034 server->poll(); 00035 break; 00036 00037 case WM_DESTROY: 00038 PostQuitMessage(0); 00039 break; 00040 00041 default: 00042 break; 00043 } 00044 00045 return DefWindowProc(hwnd, msg, wparam, lparam); 00046 } 00047 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function: create_toplevel_window 00051 // Description: Creates the initial, toplevel window for the 00052 // application. 00053 //////////////////////////////////////////////////////////////////// 00054 static HWND 00055 create_toplevel_window(HINSTANCE application) { 00056 WNDCLASS wc; 00057 00058 ZeroMemory(&wc, sizeof(WNDCLASS)); 00059 wc.lpfnWndProc = (WNDPROC)toplevel_window_proc; 00060 wc.hInstance = application; 00061 wc.lpszClassName = toplevel_class_name; 00062 00063 if (!RegisterClass(&wc)) { 00064 nout << "Could not register window class!\n"; 00065 exit(1); 00066 } 00067 00068 DWORD window_style = WS_POPUP | WS_SYSMENU | WS_ICONIC; 00069 00070 ostringstream strm; 00071 strm << "PStats " << pstats_port; 00072 string window_name = strm.str(); 00073 00074 HWND toplevel_window = 00075 CreateWindow(toplevel_class_name, window_name.c_str(), window_style, 00076 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 00077 NULL, NULL, application, 0); 00078 if (!toplevel_window) { 00079 nout << "Could not create toplevel window!\n"; 00080 exit(1); 00081 } 00082 00083 return toplevel_window; 00084 } 00085 00086 00087 // WinMain() is the correct way to start a Windows-only application, 00088 // but it is sometimes more convenient during development to use 00089 // main() instead, which doesn't squelch the stderr output. 00090 00091 #ifndef DEVELOP_WINSTATS 00092 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 00093 #else 00094 int main(int argc, char *argv[]) 00095 #endif 00096 { 00097 HINSTANCE application = GetModuleHandle(NULL); 00098 HWND toplevel_window = create_toplevel_window(application); 00099 00100 ShowWindow(toplevel_window, SW_SHOWMINIMIZED); 00101 00102 // Create the server object. 00103 server = new WinStatsServer; 00104 if (!server->listen()) { 00105 ostringstream stream; 00106 stream 00107 << "Unable to open port " << pstats_port 00108 << ". Try specifying a different\n" 00109 << "port number using pstats-port in your Config file."; 00110 string str = stream.str(); 00111 MessageBox(toplevel_window, str.c_str(), "PStats error", 00112 MB_OK | MB_ICONEXCLAMATION); 00113 exit(1); 00114 } 00115 00116 // Set up a timer to poll the pstats every so often. 00117 SetTimer(toplevel_window, 1, 200, NULL); 00118 00119 // Now get lost in the Windows message loop. 00120 MSG msg; 00121 int retval; 00122 retval = GetMessage(&msg, NULL, 0, 0); 00123 while (retval != 0) { 00124 if (retval == -1) { 00125 nout << "Error processing message queue.\n"; 00126 exit(1); 00127 } 00128 TranslateMessage(&msg); 00129 DispatchMessage(&msg); 00130 retval = GetMessage(&msg, NULL, 0, 0); 00131 } 00132 00133 return (0); 00134 }