| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/win32/winapp.cpp $ |
| 21 | * $Revision: 36 $ |
| 22 | * $Date: 5/20/99 9:11p $ |
| 23 | * $Author: Samir $ |
| 24 | * |
| 25 | * Win32 Application Object. Encapsulates some app info for libraries |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/win32/winapp.cpp $ |
| 28 | * |
| 29 | * 36 5/20/99 9:11p Samir |
| 30 | * delay function now blocks if application is idle. |
| 31 | * |
| 32 | * 35 5/11/99 12:43a Kevin |
| 33 | * included shellapi.h for extracticon |
| 34 | * |
| 35 | * 34 5/10/99 11:56a Samir |
| 36 | * implemented commandline for mouseman (with a fix that prevented |
| 37 | * mouseman from working in game due to button enumeration errors). |
| 38 | * |
| 39 | * 33 5/09/99 11:05a Kevin |
| 40 | * Extract icon from launcher and use that as the app icon (task bar, etc) |
| 41 | * |
| 42 | * 32 5/02/99 3:06p Samir |
| 43 | * added handling for power management messages. |
| 44 | * |
| 45 | * 31 4/27/99 2:06p Samir |
| 46 | * added function to get system info. |
| 47 | * |
| 48 | * 30 4/24/99 5:44p Samir |
| 49 | * delay will sleep once to defer to OS. |
| 50 | * |
| 51 | * 29 4/06/99 8:30p Samir |
| 52 | * organized defer code so delay procedure doesn't stall on idle. |
| 53 | * |
| 54 | * 28 2/09/99 1:31a Jeff |
| 55 | * do call WaitMessage() while in idle loop...need to process network and |
| 56 | * multiplayer during multiplayer games...WaitMessage() doesn't guarantee |
| 57 | * that. |
| 58 | * |
| 59 | * 27 11/30/98 5:45p Samir |
| 60 | * console apps don't get forced into the foreground. |
| 61 | * |
| 62 | * 26 10/23/98 12:36a Jeff |
| 63 | * took out inline assembly check of MMX which caused some freaky |
| 64 | * optimization problem which fucked up OS detection in os_init in release |
| 65 | * builds. |
| 66 | * |
| 67 | * 25 10/22/98 10:45p Samir |
| 68 | * blanking window in release build. |
| 69 | * |
| 70 | * 24 10/22/98 4:28p Samir |
| 71 | * only force window into foreground if a release version. |
| 72 | * |
| 73 | * 23 10/21/98 11:08a Samir |
| 74 | * weird cases when app is not in foreground though the application object |
| 75 | * believes it is. force into foreground. |
| 76 | * |
| 77 | * 22 10/16/98 5:35p Kevin |
| 78 | * use WM_ACTIVATEAPP instead of WM_ACTIVATE |
| 79 | * |
| 80 | * 21 10/16/98 11:07a Samir |
| 81 | * new OS version check stuff. |
| 82 | * |
| 83 | * 20 10/08/98 7:26p Samir |
| 84 | * changed the prototype for the defer handler callback. |
| 85 | * |
| 86 | * 19 9/28/98 11:02a Kevin |
| 87 | * added Networking defer, and fixed some UI issues |
| 88 | * |
| 89 | * 18 9/16/98 6:38p Samir |
| 90 | * added minimize box |
| 91 | * |
| 92 | * 17 9/16/98 5:02p Samir |
| 93 | * reimplemented X style console. |
| 94 | * |
| 95 | * 16 9/14/98 4:03p Samir |
| 96 | * added console support. |
| 97 | * |
| 98 | * 15 6/29/98 6:45p Samir |
| 99 | * fixed WinCallbacks properly. |
| 100 | * |
| 101 | * 14 3/23/98 8:04p Samir |
| 102 | * defer handler now returns a bool. |
| 103 | * |
| 104 | * 13 2/26/98 1:00p Samir |
| 105 | * Added application activation functions. |
| 106 | * |
| 107 | * 12 2/23/98 5:07p Samir |
| 108 | * Modified init somewhat. |
| 109 | * |
| 110 | * 11 2/23/98 4:30p Samir |
| 111 | * added init function to oeApplication. |
| 112 | * |
| 113 | * 10 1/27/98 2:59p Samir |
| 114 | * Fixed some potential crashes. |
| 115 | * |
| 116 | * 9 12/03/97 7:37p Samir |
| 117 | * Added some system key support to MainWndProc. |
| 118 | * |
| 119 | * 8 11/17/97 4:56p Samir |
| 120 | * Fixed initialization of winapp data structures. |
| 121 | * |
| 122 | * 7 10/16/97 2:30p Samir |
| 123 | * Added Idle processing. |
| 124 | * |
| 125 | * 6 9/16/97 1:04p Samir |
| 126 | * Added delay function. |
| 127 | * |
| 128 | * 5 8/26/97 12:32p Samir |
| 129 | * Fixed F10 problem (having to do with system menu). This should be an |
| 130 | * option per application instead of all applications, so I should move |
| 131 | * this code somewhere else, perhaps. (WM_SYSCOMMAND) |
| 132 | * |
| 133 | * 4 8/01/97 7:30p Samir |
| 134 | * Better messaging support and NT support. |
| 135 | * |
| 136 | * 3 7/28/97 3:46p Samir |
| 137 | * Added Topmost window optional support and NT detection. |
| 138 | * |
| 139 | * 2 6/11/97 1:09p Samir |
| 140 | * Fixed up window creation. |
| 141 | * |
| 142 | * 1 6/10/97 4:53p Samir |
| 143 | * New application class. Ported code from old osObject. |
| 144 | * |
| 145 | * $NoKeywords: $ |
| 146 | */ |
| 147 | |
| 148 | #define OEAPP_INTERNAL_MODULE |
| 149 | |
| 150 | #include "Application.h" |
| 151 | #include "AppConsole.h" |
| 152 | #include "mono.h" |
| 153 | #include "networking.h" |
| 154 | |
| 155 | #include <shellapi.h> |
| 156 | #include <mmsystem.h> |
| 157 | #include <stdio.h> |
| 158 | #include <stdlib.h> |
| 159 | |
| 160 | // taken from winuser.h |
| 161 | #ifndef WHEEL_DELTA |
| 162 | #define WHEEL_DELTA 120 |
| 163 | #endif |
| 164 | #ifndef WM_MOUSEWHEEL |
| 165 | #define WM_MOUSEWHEEL 0x20a |
| 166 | #endif |
| 167 | |
| 168 | /* Main Windows Procedure for this OS object |
| 169 | */ |
| 170 | const int MAX_WIN32APPS = 4; |
| 171 | |
| 172 | static struct tAppNodes { |
| 173 | HWND hWnd; |
| 174 | oeWin32Application *app; |
| 175 | } Win32_AppObjects[MAX_WIN32APPS]; |
| 176 | |
| 177 | // system mouse info. |
| 178 | int16_t w32_msewhl_delta = 0; // -val = up, pos val = down, 0 = no change. |
| 179 | bool w32_mouseman_hack = false; |
| 180 | const uint32_t kWindowStyle_Windowed = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_BORDER | WS_MINIMIZEBOX; |
| 181 | const uint32_t kWindowStyle_FullScreen = WS_POPUP | WS_SYSMENU; |
| 182 | const uint32_t kWindowStyle_Console = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_BORDER | WS_MINIMIZEBOX; |
| 183 | |
| 184 | /* Win32 Application Object |
| 185 | This object entails initialization and cleanup of all operating system |
| 186 | elements, as well as data that libraries may need to initialize their |
| 187 | systems. |
| 188 | |
| 189 | The Win32 Application object creates the application window and housekeeps |
| 190 | the window and instance handle for the application. |
| 191 | |
| 192 | We also allow the option of setting these handles from outside the Application object. |
| 193 | */ |
| 194 | extern LRESULT WINAPI MyConProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 195 | extern void con_Defer(); |
| 196 | |
| 197 | bool oeWin32Application::os_initialized = false; |
| 198 | bool oeWin32Application::first_time = true; |
| 199 | |
| 200 | // this is the app's window proc. |
| 201 | LRESULT WINAPI MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 202 | |
| 203 | // Creates the window handle and instance |
| 204 | oeWin32Application::oeWin32Application(const char *name, unsigned flags, HInstance hinst) : oeApplication{}, m_MsgFn{} { |
| 205 | WNDCLASS wc; |
| 206 | RECT rect; |
| 207 | |
| 208 | if (oeWin32Application::first_time) { |
| 209 | int i; |
| 210 | for (i = 0; i < MAX_WIN32APPS; i++) { |
| 211 | Win32_AppObjects[i].hWnd = NULL; |
| 212 | Win32_AppObjects[i].app = NULL; |
| 213 | } |
| 214 | oeWin32Application::first_time = false; |
| 215 | } |
| 216 | |
| 217 | HICON dicon = ExtractIcon(nullptr, "descent 3.exe", 0); |
| 218 | wc.hCursor = NULL; |
| 219 | wc.hIcon = dicon; |
| 220 | wc.lpszMenuName = NULL; |
| 221 | wc.lpszClassName = (LPCSTR)name; |
| 222 | wc.hInstance = (HINSTANCE)hinst; |
| 223 | wc.style = CS_DBLCLKS; |
| 224 | wc.lpfnWndProc = (flags & OEAPP_CONSOLE) ? (WNDPROC)MyConProc : (WNDPROC)(MyWndProc); |
| 225 | wc.cbWndExtra = 0; |
| 226 | wc.cbClsExtra = 0; |
| 227 | |
| 228 | #ifdef RELEASE |
| 229 | wc.hbrBackground = |
| 230 | (flags & OEAPP_CONSOLE) ? (HBRUSH)GetStockObject(WHITE_BRUSH) : (HBRUSH)GetStockObject(BLACK_BRUSH); |
| 231 | #else |
| 232 | wc.hbrBackground = |
| 233 | (flags & OEAPP_CONSOLE) ? (HBRUSH)GetStockObject(WHITE_BRUSH) : (HBRUSH)GetStockObject(HOLLOW_BRUSH); |
| 234 | #endif |
| 235 | |
| 236 | if (!RegisterClass(&wc)) { |
| 237 | mprintf(0, "Failure to register window class (err:%x).\n", GetLastError()); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | if (flags & OEAPP_CONSOLE) { |
| 242 | m_X = CW_USEDEFAULT; |
| 243 | m_Y = CW_USEDEFAULT; |
| 244 | m_W = 640; |
| 245 | m_H = 480; |
| 246 | } else { |
| 247 | // initialize main window and display it. |
| 248 | #ifdef RELEASE |
| 249 | SetRect(&rect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); |
| 250 | #else |
| 251 | SetRect(&rect, 0, 0, 640, 480); |
| 252 | #endif |
| 253 | |
| 254 | int cx = 0; |
| 255 | int cy = 0; |
| 256 | |
| 257 | if (flags & OEAPP_WINDOWED) { |
| 258 | // adjust the window size to take the menu into account |
| 259 | uint32_t extendedStyle = (flags & OEAPP_TOPMOST) ? WS_EX_TOPMOST : 0; |
| 260 | AdjustWindowRectEx(&rect, kWindowStyle_Windowed & ~WS_OVERLAPPED, FALSE, extendedStyle); |
| 261 | #ifdef RELEASE |
| 262 | cx = CW_USEDEFAULT; |
| 263 | cy = CW_USEDEFAULT; |
| 264 | #else |
| 265 | cx = 0; |
| 266 | cy = 0; |
| 267 | #endif |
| 268 | } |
| 269 | |
| 270 | m_X = cx; |
| 271 | m_Y = cy; |
| 272 | m_W = rect.right - rect.left - 1; |
| 273 | m_H = rect.bottom - rect.top - 1; |
| 274 | } |
| 275 | |
| 276 | m_hInstance = hinst; |
| 277 | m_Flags = flags; |
| 278 | strcpy(m_WndName, name); |
| 279 | |
| 280 | os_init(); |
| 281 | |
| 282 | m_hWnd = NULL; |
| 283 | m_WasCreated = true; |
| 284 | m_DeferFunc = NULL; |
| 285 | w32_mouseman_hack = false; |
| 286 | } |
| 287 | |
| 288 | // Create object with a premade window handle/instance |
| 289 | oeWin32Application::oeWin32Application(tWin32AppInfo *appinfo) : oeApplication() { |
| 290 | RECT rect; |
| 291 | |
| 292 | // store handles |
| 293 | m_hWnd = appinfo->hwnd; |
| 294 | m_hInstance = appinfo->hinst; |
| 295 | m_Flags = appinfo->flags; |
| 296 | |
| 297 | // returns the dimensions of the window |
| 298 | GetWindowRect((HWND)m_hWnd, &rect); |
| 299 | appinfo->wnd_x = m_X = rect.left; |
| 300 | appinfo->wnd_y = m_Y = rect.bottom; |
| 301 | appinfo->wnd_w = m_W = rect.right - rect.left - 1; |
| 302 | appinfo->wnd_h = m_H = rect.bottom - rect.top - 1; |
| 303 | |
| 304 | m_WasCreated = false; |
| 305 | |
| 306 | os_init(); |
| 307 | |
| 308 | clear_handlers(); |
| 309 | |
| 310 | m_DeferFunc = NULL; |
| 311 | w32_mouseman_hack = false; |
| 312 | } |
| 313 | |
| 314 | oeWin32Application::~oeWin32Application() { |
| 315 | HWND hwnd = (HWND)m_hWnd; |
| 316 | HINSTANCE hinst = (HINSTANCE)m_hInstance; |
| 317 | char str[32]; |
| 318 | |
| 319 | // I guess we should destroy this window, here, now. |
| 320 | GetClassName(hwnd, str, sizeof(str)); |
| 321 | |
| 322 | if (m_WasCreated) { |
| 323 | // do this only if we created the window, not just initializing the window |
| 324 | if (hwnd) { |
| 325 | DestroyWindow(hwnd); |
| 326 | } |
| 327 | |
| 328 | UnregisterClass(str, hinst); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // initializes the object |
| 333 | void oeWin32Application::init() { |
| 334 | DWORD style, winstyle; |
| 335 | |
| 336 | if (!m_WasCreated) |
| 337 | return; |
| 338 | |
| 339 | if (m_Flags & OEAPP_CONSOLE) { |
| 340 | style = 0; |
| 341 | winstyle = kWindowStyle_Console; |
| 342 | } else { |
| 343 | style = (m_Flags & OEAPP_TOPMOST) ? WS_EX_TOPMOST : 0; |
| 344 | if (m_Flags & OEAPP_WINDOWED) { |
| 345 | winstyle = kWindowStyle_Windowed; |
| 346 | } else { |
| 347 | winstyle = kWindowStyle_FullScreen; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | m_hWnd = (HWnd)CreateWindowEx(style, (LPCSTR)m_WndName, (LPCSTR)m_WndName, winstyle, m_X, m_Y, m_W, m_H, NULL, NULL, |
| 352 | (HINSTANCE)m_hInstance, (LPVOID)this); |
| 353 | |
| 354 | if (m_hWnd == NULL) { |
| 355 | DWORD err = GetLastError(); |
| 356 | mprintf(0, "Failed to create game window (err: %x)\n", err); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | ShowWindow((HWND)m_hWnd, SW_SHOWNORMAL); |
| 361 | UpdateWindow((HWND)m_hWnd); |
| 362 | } |
| 363 | |
| 364 | // Function to retrieve information from object through a platform defined structure. |
| 365 | void oeWin32Application::get_info(void *info) { |
| 366 | tWin32AppInfo *appinfo = (tWin32AppInfo *)info; |
| 367 | |
| 368 | appinfo->hwnd = m_hWnd; |
| 369 | appinfo->hinst = m_hInstance; |
| 370 | appinfo->flags = m_Flags; |
| 371 | appinfo->wnd_x = m_X; |
| 372 | appinfo->wnd_y = m_Y; |
| 373 | appinfo->wnd_w = m_W; |
| 374 | appinfo->wnd_h = m_H; |
| 375 | } |
| 376 | |
| 377 | // Function to get the flags |
| 378 | int oeWin32Application::flags(void) const { return m_Flags; } |
| 379 | |
| 380 | void oeWin32Application::set_sizepos(int x, int y, int w, int h) { |
| 381 | if (!m_hWnd) |
| 382 | return; |
| 383 | |
| 384 | m_X = x; |
| 385 | m_Y = y; |
| 386 | m_W = w; |
| 387 | m_H = h; |
| 388 | |
| 389 | MoveWindow((HWND)m_hWnd, x, y, w, h, TRUE); |
| 390 | } |
| 391 | |
| 392 | // real defer code. |
| 393 | #define DEFER_PROCESS_ACTIVE 1 // process is still active |
| 394 | #define DEFER_PROCESS_INPUT_IDLE 2 // process input from os not pending. |
| 395 | |
| 396 | int oeWin32Application::defer_block() { |
| 397 | MSG msg; |
| 398 | |
| 399 | if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { |
| 400 | if (msg.message == WM_QUIT) { |
| 401 | // QUIT APP. |
| 402 | exit(1); |
| 403 | } else if (msg.message == WM_MOVE) { |
| 404 | mprintf(0, "move msg\n"); |
| 405 | } |
| 406 | TranslateMessage(&msg); |
| 407 | DispatchMessage(&msg); |
| 408 | |
| 409 | return DEFER_PROCESS_ACTIVE; |
| 410 | } else { |
| 411 | // IDLE PROCESSING |
| 412 | if (m_DeferFunc) { |
| 413 | (*m_DeferFunc)(this->active()); |
| 414 | } |
| 415 | |
| 416 | if (this->active()) { |
| 417 | #ifndef _DEBUG |
| 418 | if (GetForegroundWindow() != (HWND)this->m_hWnd && !(m_Flags & OEAPP_CONSOLE)) { |
| 419 | mprintf(0, "forcing this window into the foreground.\n"); |
| 420 | SetForegroundWindow((HWND)this->m_hWnd); |
| 421 | } |
| 422 | #endif |
| 423 | return (DEFER_PROCESS_ACTIVE + DEFER_PROCESS_INPUT_IDLE); |
| 424 | } else { |
| 425 | return (DEFER_PROCESS_INPUT_IDLE); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | DebugBreak(); |
| 430 | return (0); |
| 431 | } |
| 432 | |
| 433 | // defer returns some flags. essentially this function defers program control to OS. |
| 434 | unsigned oeWin32Application::defer() { |
| 435 | int result; |
| 436 | |
| 437 | con_Defer(); |
| 438 | |
| 439 | // system mouse info. |
| 440 | w32_msewhl_delta = 0; |
| 441 | |
| 442 | do { |
| 443 | result = defer_block(); |
| 444 | } while ((result == DEFER_PROCESS_ACTIVE) || (result == DEFER_PROCESS_INPUT_IDLE)); |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | // set a function to run when deferring to OS. |
| 450 | void oeWin32Application::set_defer_handler(void (*func)(bool)) { m_DeferFunc = func; } |
| 451 | |
| 452 | // initializes OS components. |
| 453 | void oeWin32Application::os_init() { |
| 454 | tWin32OS os; |
| 455 | int major, minor, build; |
| 456 | |
| 457 | os = oeWin32Application::version(&major, &minor, &build); |
| 458 | |
| 459 | /* We only need to do this once */ |
| 460 | if (!os_initialized) { |
| 461 | // Are we NT for 95. |
| 462 | if (os == NoWin32) { |
| 463 | MessageBox(NULL, "This application will only run under Win32 systems.", "Outrage Error", MB_OK); |
| 464 | exit(1); |
| 465 | } |
| 466 | |
| 467 | #ifdef _DEBUG |
| 468 | if (os == Win9x) { |
| 469 | mprintf(0, "Win9x system\n"); |
| 470 | } else if (os == WinNT) { |
| 471 | mprintf(0, "WinNT %d.%d.%d system\n", major, minor, build); |
| 472 | } else { |
| 473 | mprintf(0, "Win32 non-standard operating system\n"); |
| 474 | } |
| 475 | #endif |
| 476 | |
| 477 | os_initialized = true; |
| 478 | } |
| 479 | |
| 480 | m_NTFlag = (os == WinNT) ? true : false; |
| 481 | } |
| 482 | |
| 483 | // retreive full version information |
| 484 | tWin32OS oeWin32Application::version(int *major, int *minor, int *build, char *strinfo) { |
| 485 | OSVERSIONINFO osinfo; |
| 486 | tWin32OS os; |
| 487 | |
| 488 | osinfo.dwOSVersionInfoSize = sizeof(osinfo); |
| 489 | |
| 490 | // GetVersionEx is deprecated and triggers a couple warnings. However, it appears to still be |
| 491 | // fully-functional, and the recommended alternatives (IsWindows* macros) don't provide the |
| 492 | // info we want (major/minor/build) nor do they provide tests for EOL products like Win95. On |
| 493 | // modern Windows, this will just return a version describing Win8 (even if OS is 10/11/etc). |
| 494 | // TODO: re-evaluate whether or not we actually even care about this information |
| 495 | #pragma warning(suppress: 28159 4996) |
| 496 | if (!GetVersionEx(&osinfo)) { |
| 497 | return NoWin32; |
| 498 | } |
| 499 | |
| 500 | switch (osinfo.dwPlatformId) { |
| 501 | case VER_PLATFORM_WIN32_WINDOWS: |
| 502 | os = Win9x; |
| 503 | if (build) { |
| 504 | *build = LOWORD(osinfo.dwBuildNumber); |
| 505 | } |
| 506 | break; |
| 507 | |
| 508 | case VER_PLATFORM_WIN32_NT: |
| 509 | os = WinNT; |
| 510 | if (build) { |
| 511 | *build = osinfo.dwBuildNumber; |
| 512 | } |
| 513 | break; |
| 514 | |
| 515 | //@@ case VER_PLATFORM_WIN32_CE: |
| 516 | //@@ os = WinCE; |
| 517 | //@@ if (build) { |
| 518 | //@@ *build = osinfo.dwBuildNumber; |
| 519 | //@@ } |
| 520 | //@@ break; |
| 521 | |
| 522 | default: |
| 523 | os = NoWin32; |
| 524 | if (*build) { |
| 525 | *build = osinfo.dwBuildNumber; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | *major = (int)osinfo.dwMajorVersion; |
| 530 | *minor = (int)osinfo.dwMinorVersion; |
| 531 | |
| 532 | if (strinfo) { |
| 533 | strcpy(strinfo, osinfo.szCSDVersion); |
| 534 | } |
| 535 | |
| 536 | return os; |
| 537 | } |
| 538 | |
| 539 | // This Window Procedure is called from the global WindowProc. |
| 540 | LResult oeWin32Application::WndProc(HWnd hwnd, unsigned msg, WParam wParam, LParam lParam) { |
| 541 | switch (msg) { |
| 542 | case WM_ACTIVATEAPP: |
| 543 | m_AppActive = wParam ? true : false; |
| 544 | // mprintf(0, "WM_ACTIVATEAPP (%u,%l)\n", wParam, lParam); |
| 545 | break; |
| 546 | } |
| 547 | |
| 548 | return DefWindowProc((HWND)hwnd, (UINT)msg, (WPARAM)wParam, (LPARAM)lParam); |
| 549 | } |
| 550 | |
| 551 | // These functions allow you to add message handlers. |
| 552 | bool oeWin32Application::add_handler(unsigned msg, tOEWin32MsgCallback fn) { |
| 553 | // search for redundant callbacks. |
| 554 | for (const MessageFunction &rMsgFn : m_MsgFn) |
| 555 | if (rMsgFn.msg == msg && rMsgFn.fn == fn) |
| 556 | return true; |
| 557 | |
| 558 | for (MessageFunction &rMsgFn : m_MsgFn) { |
| 559 | if (rMsgFn.fn == nullptr) { |
| 560 | rMsgFn.msg = msg; |
| 561 | rMsgFn.fn = fn; |
| 562 | return true; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | DebugBreak(); // We have reached the max number of message functions! |
| 567 | |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | // These functions remove a handler |
| 572 | bool oeWin32Application::remove_handler(unsigned msg, tOEWin32MsgCallback fn) { |
| 573 | if (!fn) |
| 574 | DebugBreak(); |
| 575 | |
| 576 | for (MessageFunction &rMsgFn : m_MsgFn) { |
| 577 | if (msg == rMsgFn.msg && rMsgFn.fn == fn) { |
| 578 | rMsgFn.fn = nullptr; |
| 579 | return true; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | // Run handler for message (added by add_handler) |
| 587 | bool oeWin32Application::run_handler(HWnd wnd, unsigned msg, WParam wParam, LParam lParam) { |
| 588 | // run user-defined message handlers |
| 589 | // the guess here is that any callback that returns a 0, will not want to handle the window's WndProc function. |
| 590 | for (const MessageFunction &rMsgFn : m_MsgFn) |
| 591 | if (msg == rMsgFn.msg && rMsgFn.fn) |
| 592 | if (!(*rMsgFn.fn)(wnd, msg, wParam, lParam)) |
| 593 | return false; |
| 594 | |
| 595 | return true; |
| 596 | } |
| 597 | |
| 598 | void oeWin32Application::clear_handlers() { |
| 599 | for (MessageFunction &rMsgFn : m_MsgFn) |
| 600 | rMsgFn.fn = nullptr; |
| 601 | } |
| 602 | |
| 603 | void oeWin32Application::delay(float secs) { |
| 604 | int result; |
| 605 | DWORD msecs = (DWORD)(secs * 1000.0); |
| 606 | DWORD time_start; |
| 607 | |
| 608 | w32_msewhl_delta = 0; |
| 609 | |
| 610 | time_start = timeGetTime(); |
| 611 | Sleep(0); |
| 612 | while (timeGetTime() < (time_start + msecs)) { |
| 613 | this->defer_block(); |
| 614 | } |
| 615 | |
| 616 | // block if messages are still pending (for task switching too, this call will not return until messages are clear |
| 617 | do { |
| 618 | result = this->defer_block(); |
| 619 | } while (result == DEFER_PROCESS_ACTIVE || result == DEFER_PROCESS_INPUT_IDLE); |
| 620 | } |
| 621 | |
| 622 | LRESULT WINAPI MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { |
| 623 | int i = -1; |
| 624 | bool force_default = false; |
| 625 | |
| 626 | for (i = 0; i < MAX_WIN32APPS; i++) |
| 627 | if (Win32_AppObjects[i].hWnd == hWnd) |
| 628 | break; |
| 629 | |
| 630 | if (i == MAX_WIN32APPS) |
| 631 | i = -1; |
| 632 | |
| 633 | switch (msg) { |
| 634 | LPCREATESTRUCT lpCreateStruct; |
| 635 | case WM_CREATE: |
| 636 | // here we store the this pointer to the app object this instance belongs to. |
| 637 | lpCreateStruct = (LPCREATESTRUCT)lParam; |
| 638 | for (i = 0; i < MAX_WIN32APPS; i++) |
| 639 | if (Win32_AppObjects[i].hWnd == NULL) |
| 640 | break; |
| 641 | if (i == MAX_WIN32APPS) |
| 642 | debug_break(); |
| 643 | Win32_AppObjects[i].hWnd = hWnd; |
| 644 | Win32_AppObjects[i].app = (oeWin32Application *)lpCreateStruct->lpCreateParams; |
| 645 | |
| 646 | Win32_AppObjects[i].app->clear_handlers(); |
| 647 | |
| 648 | force_default = true; |
| 649 | break; |
| 650 | |
| 651 | case WM_DESTROY: |
| 652 | // get window handle and clear it. |
| 653 | if (i == MAX_WIN32APPS) |
| 654 | debug_break(); |
| 655 | Win32_AppObjects[i].hWnd = NULL; |
| 656 | Win32_AppObjects[i].app = NULL; |
| 657 | i = -1; |
| 658 | break; |
| 659 | |
| 660 | case WM_SYSCOMMAND: { |
| 661 | // bypass screen saver and system menu. |
| 662 | uint32_t maskedWParam = wParam & 0xFFF0; |
| 663 | if (maskedWParam == SC_SCREENSAVE || maskedWParam == SC_MONITORPOWER) |
| 664 | return 0; |
| 665 | if (maskedWParam == SC_KEYMENU) |
| 666 | return 0; |
| 667 | |
| 668 | // handle the close button |
| 669 | if (maskedWParam == SC_CLOSE) { |
| 670 | exit(1); |
| 671 | return 0; |
| 672 | } |
| 673 | } break; |
| 674 | |
| 675 | case WM_SYSKEYDOWN: |
| 676 | case WM_SYSKEYUP: |
| 677 | if (lParam & 0x20000000) |
| 678 | return 0; |
| 679 | break; |
| 680 | |
| 681 | case WM_POWERBROADCAST: // Won't allow OS to suspend operation for now. |
| 682 | mprintf(0, "WM_POWERBROADCAST=%u,%d\n", wParam, lParam); |
| 683 | if (wParam == PBT_APMQUERYSUSPEND) { |
| 684 | return BROADCAST_QUERY_DENY; |
| 685 | } |
| 686 | break; |
| 687 | |
| 688 | case WM_MOUSEWHEEL: |
| 689 | case 0xcc41: |
| 690 | if (w32_mouseman_hack) { |
| 691 | if (msg != 0xcc41) { |
| 692 | w32_msewhl_delta = HIWORD(wParam); |
| 693 | } else { |
| 694 | w32_msewhl_delta = (int16_t)(wParam); |
| 695 | } |
| 696 | } else if (msg == WM_MOUSEWHEEL) { |
| 697 | w32_msewhl_delta = HIWORD(wParam); |
| 698 | } |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | // if this window not on list, then run default window proc. |
| 703 | if (i < 0 || Win32_AppObjects[i].app == NULL || force_default) |
| 704 | return DefWindowProc(hWnd, msg, wParam, lParam); |
| 705 | |
| 706 | if (!Win32_AppObjects[i].app->run_handler((HWnd)hWnd, (unsigned)msg, (WParam)wParam, (LParam)lParam)) |
| 707 | return 0; |
| 708 | |
| 709 | // run user defined window procedure. |
| 710 | return (LRESULT)Win32_AppObjects[i].app->WndProc((HWnd)hWnd, (unsigned)msg, (WParam)wParam, (LParam)lParam); |
| 711 | } |
| 712 | |
| 713 | // detect if application can handle what we want of it. |
| 714 | bool oeWin32Application::GetSystemSpecs(const char *fname) { |
| 715 | FILE *fp = fopen(fname, "wt"); |
| 716 | tWin32OS os; |
| 717 | int maj, min, build; |
| 718 | char desc[256]{}; |
| 719 | |
| 720 | if (!fp) |
| 721 | return false; |
| 722 | |
| 723 | os = oeWin32Application::version(&maj, &min, &build, desc); |
| 724 | |
| 725 | fprintf(fp, "OS: %s %d.%d.%d %s\n", |
| 726 | (os == Win9x) ? "Win9x" |
| 727 | : (os == WinNT) ? "WinNT" |
| 728 | : (os == WinCE) ? "WinCE" |
| 729 | : "Non standard Win32", |
| 730 | maj, min, build, desc); |
| 731 | |
| 732 | // get system memory info |
| 733 | MEMORYSTATUSEX mem_stat{}; |
| 734 | |
| 735 | mem_stat.dwLength = sizeof(MEMORYSTATUSEX); |
| 736 | GlobalMemoryStatusEx(&mem_stat); |
| 737 | |
| 738 | fprintf(fp, "Memory:\n"); |
| 739 | fprintf(fp, "\tLoad:\t\t\t%u\n\tTotalPhys:\t\t%llu\n\tAvailPhys:\t\t%llu\nPageFile:\t\t%llu\n", |
| 740 | mem_stat.dwMemoryLoad, mem_stat.ullTotalPhys, mem_stat.ullAvailPhys, |
| 741 | mem_stat.ullTotalPageFile); |
| 742 | fprintf(fp, "\tPageFileFree:\t%llu\n\tVirtual:\t\t%llu\n\tVirtualFree:\t%llu\n", mem_stat.ullAvailPageFile, |
| 743 | mem_stat.ullTotalVirtual, mem_stat.ullAvailVirtual); |
| 744 | |
| 745 | fclose(fp); |
| 746 | |
| 747 | return true; |
| 748 | } |