2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/OsApi/OsApi.cpp $
15 * Low level Windows code
18 * Revision 1.3 2002/06/09 04:41:25 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:48 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:10 root
28 * 7 6/30/99 5:53p Dave
29 * Put in new anti-camper code.
31 * 6 6/03/99 6:37p Dave
32 * More TNT fun. Made perspective bitmaps more flexible.
34 * 5 6/02/99 6:18p Dave
35 * Fixed TNT lockup problems! Wheeeee!
37 * 4 12/18/98 1:13a Dave
38 * Rough 1024x768 support for Direct3D. Proper detection and usage through
41 * 3 10/09/98 2:57p Dave
42 * Starting splitting up OS stuff.
44 * 2 10/08/98 2:38p Dave
45 * Cleanup up OsAPI code significantly. Removed old functions, centralized
48 * 118 7/10/98 5:04p Dave
49 * Fix connection speed bug on standalone server.
51 * 117 5/24/98 2:28p Hoffoss
52 * Because we never really care about if the left or the right shift or
53 * alt key was used, but rather than either shift or alt was used, made
54 * both map to the left one. Solves some problems, causes none.
56 * 116 5/18/98 9:22p John
57 * Took out the annoying tab check.
59 * 115 5/18/98 11:17a John
60 * Fixed some bugs with software window and output window.
62 * 114 5/16/98 2:20p John
63 * Changed the os_suspend and resume to use a critical section to prevent
64 * threads from executing rather than just suspending the thread. Had to
65 * make sure gr_close was called before os_close.
67 * 113 5/15/98 4:49p John
69 * 112 5/15/98 3:36p John
70 * Fixed bug with new graphics window code and standalone server. Made
71 * hwndApp not be a global anymore.
73 * 111 5/14/98 5:42p John
74 * Revamped the whole window position/mouse code for the graphics windows.
76 * 110 5/04/98 11:08p Hoffoss
77 * Expanded on Force Feedback code, and moved it all into Joy_ff.cpp.
78 * Updated references everywhere to it.
102 #include "freespaceresource.h"
103 #include "managepilot.h"
106 #include "gamesequence.h"
107 #include "freespace.h"
108 #include "osregistry.h"
111 // ----------------------------------------------------------------------------------------------------
112 // OSAPI DEFINES/VARS
116 static HINSTANCE hInstApp;
117 static HWND hwndApp = NULL;
118 static HANDLE hThread=NULL;
119 static DWORD ThreadID;
120 static int fAppActive = 0;
121 static int main_window_inited = 0;
122 static char szWinTitle[128];
123 static char szWinClass[128];
124 static int WinX, WinY, WinW, WinH;
125 static int Os_inited = 0;
127 static CRITICAL_SECTION Os_lock;
129 int Os_debugger_running = 0;
131 // ----------------------------------------------------------------------------------------------------
132 // OSAPI FORWARD DECLARATIONS
136 // thread handler for the main message thread
137 DWORD win32_process(DWORD lparam);
139 DWORD win32_process1(DWORD lparam);
140 DWORD win32_process1(DWORD lparam);
143 // Fills in the Os_debugger_running with non-zero if debugger detected.
144 void os_check_debugger();
146 // called at shutdown. Makes sure all thread processing terminates.
149 // go through all windows and try and find the one that matches the search string
150 BOOL __stdcall os_enum_windows( HWND hwnd, char * search_string );
152 // message handler for the main thread
153 LRESULT CALLBACK win32_message_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam);
155 // create the main window
156 BOOL win32_create_window();
159 // ----------------------------------------------------------------------------------------------------
163 // initialization/shutdown functions -----------------------------------------------
165 // If app_name is NULL or ommited, then TITLE is used
166 // for the app name, which is where registry keys are stored.
167 void os_init(char * wclass, char * title, char *app_name, char *version_string )
169 os_init_registry_stuff(Osreg_company_name, title, version_string);
171 strcpy( szWinTitle, title );
172 strcpy( szWinClass, wclass );
174 InitializeCriticalSection( &Os_lock );
177 // Create an even to signal that the window is created,
178 // so that we don't return from this function until
179 // the window is all properly created.
180 HANDLE Window_created = CreateEvent( NULL, FALSE, FALSE, NULL );
181 hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)win32_process, Window_created, 0, &ThreadID );
182 if ( WaitForSingleObject( Window_created, 5000 )==WAIT_TIMEOUT) { //INFINITE );
183 mprintf(( "Wait timeout!\n" ));
185 CloseHandle(Window_created);
186 Window_created = NULL;
194 // check to see if we're running under msdev
200 // set the main window title
201 void os_set_title( char * title )
203 strcpy( szWinTitle, title );
204 SetWindowText( hwndApp, szWinTitle );
207 // call at program end
210 // Tell the app to quit
211 PostMessage( hwndApp, WM_DESTROY, 0, 0 );
219 // window management -----------------------------------------------------------------
221 // Returns 1 if app is not the foreground app.
227 // Returns the handle to the main window
230 return (uint)hwndApp;
234 // process management -----------------------------------------------------------------
236 // Sleeps for n milliseconds or until app becomes active.
237 void os_sleep(int ms)
242 // Used to stop message processing
245 ENTER_CRITICAL_SECTION(&Os_lock);
248 // resume message processing
251 LEAVE_CRITICAL_SECTION(&Os_lock);
255 // ----------------------------------------------------------------------------------------------------
256 // OSAPI FORWARD DECLARATIONS
259 #pragma warning(disable:4702)
262 // thread handler for the main message thread
263 DWORD win32_process(DWORD lparam)
266 HANDLE Window_created = (HANDLE)lparam;
268 if ( !win32_create_window() )
271 // Let the app continue once the window is created
272 SetEvent(Window_created);
275 if (WaitMessage() == TRUE) {
276 EnterCriticalSection(&Os_lock);
277 while(PeekMessage(&msg,0,0,0,PM_REMOVE)) {
278 if ( msg.message == WM_DESTROY ) {
279 LeaveCriticalSection(&Os_lock);
281 // cleanup and exit this thread!!
282 DeleteCriticalSection(&Os_lock);
285 TranslateMessage(&msg);
286 DispatchMessage(&msg);
288 LeaveCriticalSection(&Os_lock);
295 DWORD win32_process1(DWORD lparam)
297 if ( !win32_create_window() )
304 DWORD win32_process2(DWORD lparam)
308 while(PeekMessage(&msg,0,0,0,PM_REMOVE)) {
309 TranslateMessage(&msg);
310 DispatchMessage(&msg);
316 #pragma warning(default:4702)
318 // Fills in the Os_debugger_running with non-zero if debugger detected.
319 void os_check_debugger()
322 char search_string[256];
327 Os_debugger_running = 0; // Assume its not
329 // Find my EXE file name
330 hMod = GetModuleHandle(NULL);
332 namelen = GetModuleFileName( hMod, myname, 127 );
333 if ( namelen < 1 ) return;
335 // Strip off the .EXE
336 p = strstr( myname, ".exe" );
340 // Move p to point to first letter of EXE filename
341 while( (*p!='\\') && (*p!='/') && (*p!=':') )
344 if ( strlen(p) < 1 ) return;
346 // Build what the debugger's window title would be if the debugger is running...
347 sprintf( search_string, "[run] - %s -", p );
349 // ... and then search for it.
350 EnumWindows( (int (__stdcall *)(struct HWND__ *,long))os_enum_windows, (long)&search_string );
353 // called at shutdown. Makes sure all thread processing terminates.
357 CloseHandle(hThread);
362 // go through all windows and try and find the one that matches the search string
363 BOOL __stdcall os_enum_windows( HWND hwnd, char * search_string )
368 len = GetWindowText( hwnd, tmp, 127 );
371 if ( strstr( tmp, search_string )) {
372 Os_debugger_running = 1; // found the search string!
373 return FALSE; // stop enumerating windows
377 return TRUE; // continue enumeration
382 // message handler for the main thread
383 LRESULT CALLBACK win32_message_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
389 case WM_QUERYNEWPALETTE:
390 // mprintf(( "WM: QueryNewPalette\n" ));
391 return TRUE; // Say that I've realized my own palette
393 case WM_PALETTECHANGED:
394 // mprintf(( "WM: PaletteChanged\n" ));
396 case WM_PALETTEISCHANGING:
397 // mprintf(( "WM: PaletteIsChanging\n" ));
400 case WM_DISPLAYCHANGE:
401 // mprintf(( "WM: DisplayChange\n" ));
405 mouse_mark_button( MOUSE_LEFT_BUTTON, 1 );
409 mouse_mark_button( MOUSE_LEFT_BUTTON, 0 );
413 mouse_mark_button( MOUSE_RIGHT_BUTTON, 1 );
417 mouse_mark_button( MOUSE_RIGHT_BUTTON, 0 );
421 mouse_mark_button( MOUSE_MIDDLE_BUTTON, 1 );
425 mouse_mark_button( MOUSE_MIDDLE_BUTTON, 0 );
437 latency = timeGetTime() - GetMessageTime();
441 nVirtKey = (int)wParam; // virtual-key code
442 lKeyData = (lParam>>16) & 255; // key data
443 if ( (lParam>>16) & 256 ) lKeyData += 0x80;
445 // Fix up print screen, whose OEM code is wrong under 95.
446 if ( nVirtKey == VK_SNAPSHOT ) {
447 lKeyData = KEY_PRINT_SCRN;
450 if (lKeyData == KEY_RSHIFT) // either shift is just a shift to us..
451 lKeyData = KEY_LSHIFT;
453 if (lKeyData == KEY_RALT) // Same with alt keys..
456 // mprintf(( "Key down = 0x%x|%x\n", lKeyData, nVirtKey ));
457 key_mark( lKeyData, 1, latency );
458 // mprintf(( "Key down = 0x%x\n", lKeyData ));
459 //Warning( LOCATION, "Key = 0x%x", lKeyData );
469 latency = timeGetTime() - GetMessageTime();
473 nVirtKey = (int) wParam; // virtual-key code
474 lKeyData = (lParam>>16) & 255; // key data
475 if ( (lParam>>16) & 256 ) lKeyData += 0x80;
477 // Fix up print screen, whose OEM code is wrong under 95.
478 if ( nVirtKey == VK_SNAPSHOT ) {
479 lKeyData = KEY_PRINT_SCRN;
482 if (lKeyData == KEY_RSHIFT) // either shift is just a shift to us..
483 lKeyData = KEY_LSHIFT;
485 if (lKeyData == KEY_RALT) // Same with alt keys..
488 // mprintf(( "Key up = 0x%x|%x\n", lKeyData, nVirtKey ));
489 if ( lKeyData == 0xB7 ) {
490 // Hack for PrintScreen which only sends one up message!
491 key_mark( lKeyData, 1, latency );
492 key_mark( lKeyData, 0, latency );
495 key_mark( lKeyData, 0, latency );
512 case WM_ACTIVATE: //APP: //
515 BOOL OldfAppActive = fAppActive;
517 // The application z-ordering has changed. If we are now the
518 // foreground application wParm will be TRUE.
519 if ( msg == WM_ACTIVATE ) {
520 if ( (LOWORD(wParam) == WA_ACTIVE) || (LOWORD(wParam)==WA_CLICKACTIVE)) {
521 fAppActive = TRUE; //(BOOL)wParam;
523 fAppActive = FALSE; //(BOOL)wParam;
527 fAppActive = (BOOL)wParam;
530 //mprintf(( "Activate: %d\n", fAppActive ));
532 if ( OldfAppActive != fAppActive ) {
536 // mprintf(( "Priority: HIGH\n" ));
540 SetThreadPriority( hThread, THREAD_PRIORITY_HIGHEST );
543 gr_activate(fAppActive);
544 //SetThreadPriority( hThread, THREAD_PRIORITY_TIME_CRITICAL );
546 //mprintf(( "Priority: LOW\n" ));
551 //mprintf(( "Mouse:Alive\n" ));
554 // key_outkey( KEY_PAUSE );
557 SetThreadPriority( hThread, THREAD_PRIORITY_NORMAL );
559 gr_activate(fAppActive);
566 // mprintf(( "WM_DESTROY called\n" ));
571 gameseq_post_event(GS_EVENT_QUIT_GAME);
575 // mprintf(( "Sys command called '%x'\n", wParam ));
576 if ( wParam != SC_SCREENSAVE ){
577 return DefWindowProc(hwnd, msg, wParam, lParam);
583 rtvoice_stream_data((uint)hwnd, (uint)wParam, (uint)lParam);
588 return DefWindowProc(hwnd, msg, wParam, lParam);
595 // create the main window
596 BOOL win32_create_window()
600 char *ptr = os_config_read_string(NULL, NOX("Videocard"), NULL);
601 if(ptr && strstr(ptr, NOX("Direct 3D -")) && Cmdline_window){
604 if(ptr && strstr(ptr, NOX("1024")) && Cmdline_window){
609 HINSTANCE hInst = GetModuleHandle(NULL);
611 wclass.hInstance = hInst;
612 wclass.lpszClassName = szWinClass;
613 wclass.lpfnWndProc = (WNDPROC)win32_message_handler;
615 wclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
617 wclass.style = CS_BYTEALIGNCLIENT|CS_VREDRAW | CS_HREDRAW; // | CS_OWNDC; //CS_DBLCLKS | CS_PARENTDC| CS_VREDRAW | CS_HREDRAW |;
619 wclass.cbSize = sizeof(WNDCLASSEX);
620 wclass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_APP_ICON) );
621 wclass.hIconSm = NULL; //LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1) );
622 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
623 wclass.lpszMenuName = NULL; //"FreeSpaceMenu";
624 wclass.cbClsExtra = 0;
625 wclass.cbWndExtra = 0;
626 wclass.hbrBackground = (HBRUSH)NULL;
628 if (!RegisterClassEx(&wclass)) return FALSE;
632 style = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
637 // make sure we adjust for the actual window border
640 x_add += GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
641 y_add += GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
643 // Make a 32x32 window. It never should get shown, because the graphics code
644 // will then size it.
646 hwndApp = CreateWindow( szWinClass, szWinTitle,
650 hires ? 1024 : 640 + x_add,
651 hires ? 768 : 480 + y_add,
652 NULL, (HMENU)NULL, hInst, (LPSTR)NULL);
654 // Make a 32x32 window. It never should get shown, because the graphics code
655 // will then size it.
656 hwndApp = CreateWindow( szWinClass, szWinTitle,
658 (GetSystemMetrics( SM_CXSCREEN )-32 )/2, //x
659 (GetSystemMetrics( SM_CYSCREEN )-32 )/2, //y
661 NULL, (HMENU)NULL, hInst, (LPSTR)NULL);
664 // Hack!! Turn off Window's cursor.
668 main_window_inited = 1;
674 ShowWindow( hwndApp, SW_SHOWNORMAL );
675 UpdateWindow( hwndApp );
687 EnterCriticalSection(&Os_lock);
688 while(PeekMessage(&msg,0,0,0,PM_NOREMOVE)) {
689 if ( msg.message == WM_DESTROY ) {
692 if (PeekMessage(&msg,0,0,0,PM_REMOVE)) {
693 TranslateMessage(&msg);
694 DispatchMessage(&msg);
698 LeaveCriticalSection(&Os_lock);