2 * $Logfile: /Freespace2/code/OsApi/OsApi.cpp $
7 * Low level Windows code
10 * Revision 1.2 2002/05/07 03:16:48 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:10 root
17 * 7 6/30/99 5:53p Dave
18 * Put in new anti-camper code.
20 * 6 6/03/99 6:37p Dave
21 * More TNT fun. Made perspective bitmaps more flexible.
23 * 5 6/02/99 6:18p Dave
24 * Fixed TNT lockup problems! Wheeeee!
26 * 4 12/18/98 1:13a Dave
27 * Rough 1024x768 support for Direct3D. Proper detection and usage through
30 * 3 10/09/98 2:57p Dave
31 * Starting splitting up OS stuff.
33 * 2 10/08/98 2:38p Dave
34 * Cleanup up OsAPI code significantly. Removed old functions, centralized
37 * 118 7/10/98 5:04p Dave
38 * Fix connection speed bug on standalone server.
40 * 117 5/24/98 2:28p Hoffoss
41 * Because we never really care about if the left or the right shift or
42 * alt key was used, but rather than either shift or alt was used, made
43 * both map to the left one. Solves some problems, causes none.
45 * 116 5/18/98 9:22p John
46 * Took out the annoying tab check.
48 * 115 5/18/98 11:17a John
49 * Fixed some bugs with software window and output window.
51 * 114 5/16/98 2:20p John
52 * Changed the os_suspend and resume to use a critical section to prevent
53 * threads from executing rather than just suspending the thread. Had to
54 * make sure gr_close was called before os_close.
56 * 113 5/15/98 4:49p John
58 * 112 5/15/98 3:36p John
59 * Fixed bug with new graphics window code and standalone server. Made
60 * hwndApp not be a global anymore.
62 * 111 5/14/98 5:42p John
63 * Revamped the whole window position/mouse code for the graphics windows.
65 * 110 5/04/98 11:08p Hoffoss
66 * Expanded on Force Feedback code, and moved it all into Joy_ff.cpp.
67 * Updated references everywhere to it.
91 #include "freespaceresource.h"
92 #include "managepilot.h"
95 #include "gamesequence.h"
96 #include "freespace.h"
97 #include "osregistry.h"
100 // ----------------------------------------------------------------------------------------------------
101 // OSAPI DEFINES/VARS
105 static HINSTANCE hInstApp;
106 static HWND hwndApp = NULL;
107 static HANDLE hThread=NULL;
108 static DWORD ThreadID;
109 static int fAppActive = 0;
110 static int main_window_inited = 0;
111 static char szWinTitle[128];
112 static char szWinClass[128];
113 static int WinX, WinY, WinW, WinH;
114 static int Os_inited = 0;
116 static CRITICAL_SECTION Os_lock;
118 int Os_debugger_running = 0;
120 // ----------------------------------------------------------------------------------------------------
121 // OSAPI FORWARD DECLARATIONS
125 // thread handler for the main message thread
126 DWORD win32_process(DWORD lparam);
128 DWORD win32_process1(DWORD lparam);
129 DWORD win32_process1(DWORD lparam);
132 // Fills in the Os_debugger_running with non-zero if debugger detected.
133 void os_check_debugger();
135 // called at shutdown. Makes sure all thread processing terminates.
138 // go through all windows and try and find the one that matches the search string
139 BOOL __stdcall os_enum_windows( HWND hwnd, char * search_string );
141 // message handler for the main thread
142 LRESULT CALLBACK win32_message_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam);
144 // create the main window
145 BOOL win32_create_window();
148 // ----------------------------------------------------------------------------------------------------
152 // initialization/shutdown functions -----------------------------------------------
154 // If app_name is NULL or ommited, then TITLE is used
155 // for the app name, which is where registry keys are stored.
156 void os_init(char * wclass, char * title, char *app_name, char *version_string )
158 os_init_registry_stuff(Osreg_company_name, title, version_string);
160 strcpy( szWinTitle, title );
161 strcpy( szWinClass, wclass );
163 InitializeCriticalSection( &Os_lock );
166 // Create an even to signal that the window is created,
167 // so that we don't return from this function until
168 // the window is all properly created.
169 HANDLE Window_created = CreateEvent( NULL, FALSE, FALSE, NULL );
170 hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)win32_process, Window_created, 0, &ThreadID );
171 if ( WaitForSingleObject( Window_created, 5000 )==WAIT_TIMEOUT) { //INFINITE );
172 mprintf(( "Wait timeout!\n" ));
174 CloseHandle(Window_created);
175 Window_created = NULL;
183 // check to see if we're running under msdev
189 // set the main window title
190 void os_set_title( char * title )
192 strcpy( szWinTitle, title );
193 SetWindowText( hwndApp, szWinTitle );
196 // call at program end
199 // Tell the app to quit
200 PostMessage( hwndApp, WM_DESTROY, 0, 0 );
208 // window management -----------------------------------------------------------------
210 // Returns 1 if app is not the foreground app.
216 // Returns the handle to the main window
219 return (uint)hwndApp;
223 // process management -----------------------------------------------------------------
225 // Sleeps for n milliseconds or until app becomes active.
226 void os_sleep(int ms)
231 // Used to stop message processing
234 ENTER_CRITICAL_SECTION(&Os_lock);
237 // resume message processing
240 LEAVE_CRITICAL_SECTION(&Os_lock);
244 // ----------------------------------------------------------------------------------------------------
245 // OSAPI FORWARD DECLARATIONS
248 #pragma warning(disable:4702)
251 // thread handler for the main message thread
252 DWORD win32_process(DWORD lparam)
255 HANDLE Window_created = (HANDLE)lparam;
257 if ( !win32_create_window() )
260 // Let the app continue once the window is created
261 SetEvent(Window_created);
264 if (WaitMessage() == TRUE) {
265 EnterCriticalSection(&Os_lock);
266 while(PeekMessage(&msg,0,0,0,PM_REMOVE)) {
267 if ( msg.message == WM_DESTROY ) {
268 LeaveCriticalSection(&Os_lock);
270 // cleanup and exit this thread!!
271 DeleteCriticalSection(&Os_lock);
274 TranslateMessage(&msg);
275 DispatchMessage(&msg);
277 LeaveCriticalSection(&Os_lock);
284 DWORD win32_process1(DWORD lparam)
286 if ( !win32_create_window() )
293 DWORD win32_process2(DWORD lparam)
297 while(PeekMessage(&msg,0,0,0,PM_REMOVE)) {
298 TranslateMessage(&msg);
299 DispatchMessage(&msg);
305 #pragma warning(default:4702)
307 // Fills in the Os_debugger_running with non-zero if debugger detected.
308 void os_check_debugger()
311 char search_string[256];
316 Os_debugger_running = 0; // Assume its not
318 // Find my EXE file name
319 hMod = GetModuleHandle(NULL);
321 namelen = GetModuleFileName( hMod, myname, 127 );
322 if ( namelen < 1 ) return;
324 // Strip off the .EXE
325 p = strstr( myname, ".exe" );
329 // Move p to point to first letter of EXE filename
330 while( (*p!='\\') && (*p!='/') && (*p!=':') )
333 if ( strlen(p) < 1 ) return;
335 // Build what the debugger's window title would be if the debugger is running...
336 sprintf( search_string, "[run] - %s -", p );
338 // ... and then search for it.
339 EnumWindows( (int (__stdcall *)(struct HWND__ *,long))os_enum_windows, (long)&search_string );
342 // called at shutdown. Makes sure all thread processing terminates.
346 CloseHandle(hThread);
351 // go through all windows and try and find the one that matches the search string
352 BOOL __stdcall os_enum_windows( HWND hwnd, char * search_string )
357 len = GetWindowText( hwnd, tmp, 127 );
360 if ( strstr( tmp, search_string )) {
361 Os_debugger_running = 1; // found the search string!
362 return FALSE; // stop enumerating windows
366 return TRUE; // continue enumeration
371 // message handler for the main thread
372 LRESULT CALLBACK win32_message_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
378 case WM_QUERYNEWPALETTE:
379 // mprintf(( "WM: QueryNewPalette\n" ));
380 return TRUE; // Say that I've realized my own palette
382 case WM_PALETTECHANGED:
383 // mprintf(( "WM: PaletteChanged\n" ));
385 case WM_PALETTEISCHANGING:
386 // mprintf(( "WM: PaletteIsChanging\n" ));
389 case WM_DISPLAYCHANGE:
390 // mprintf(( "WM: DisplayChange\n" ));
394 mouse_mark_button( MOUSE_LEFT_BUTTON, 1 );
398 mouse_mark_button( MOUSE_LEFT_BUTTON, 0 );
402 mouse_mark_button( MOUSE_RIGHT_BUTTON, 1 );
406 mouse_mark_button( MOUSE_RIGHT_BUTTON, 0 );
410 mouse_mark_button( MOUSE_MIDDLE_BUTTON, 1 );
414 mouse_mark_button( MOUSE_MIDDLE_BUTTON, 0 );
426 latency = timeGetTime() - GetMessageTime();
430 nVirtKey = (int)wParam; // virtual-key code
431 lKeyData = (lParam>>16) & 255; // key data
432 if ( (lParam>>16) & 256 ) lKeyData += 0x80;
434 // Fix up print screen, whose OEM code is wrong under 95.
435 if ( nVirtKey == VK_SNAPSHOT ) {
436 lKeyData = KEY_PRINT_SCRN;
439 if (lKeyData == KEY_RSHIFT) // either shift is just a shift to us..
440 lKeyData = KEY_LSHIFT;
442 if (lKeyData == KEY_RALT) // Same with alt keys..
445 // mprintf(( "Key down = 0x%x|%x\n", lKeyData, nVirtKey ));
446 key_mark( lKeyData, 1, latency );
447 // mprintf(( "Key down = 0x%x\n", lKeyData ));
448 //Warning( LOCATION, "Key = 0x%x", lKeyData );
458 latency = timeGetTime() - GetMessageTime();
462 nVirtKey = (int) wParam; // virtual-key code
463 lKeyData = (lParam>>16) & 255; // key data
464 if ( (lParam>>16) & 256 ) lKeyData += 0x80;
466 // Fix up print screen, whose OEM code is wrong under 95.
467 if ( nVirtKey == VK_SNAPSHOT ) {
468 lKeyData = KEY_PRINT_SCRN;
471 if (lKeyData == KEY_RSHIFT) // either shift is just a shift to us..
472 lKeyData = KEY_LSHIFT;
474 if (lKeyData == KEY_RALT) // Same with alt keys..
477 // mprintf(( "Key up = 0x%x|%x\n", lKeyData, nVirtKey ));
478 if ( lKeyData == 0xB7 ) {
479 // Hack for PrintScreen which only sends one up message!
480 key_mark( lKeyData, 1, latency );
481 key_mark( lKeyData, 0, latency );
484 key_mark( lKeyData, 0, latency );
501 case WM_ACTIVATE: //APP: //
504 BOOL OldfAppActive = fAppActive;
506 // The application z-ordering has changed. If we are now the
507 // foreground application wParm will be TRUE.
508 if ( msg == WM_ACTIVATE ) {
509 if ( (LOWORD(wParam) == WA_ACTIVE) || (LOWORD(wParam)==WA_CLICKACTIVE)) {
510 fAppActive = TRUE; //(BOOL)wParam;
512 fAppActive = FALSE; //(BOOL)wParam;
516 fAppActive = (BOOL)wParam;
519 //mprintf(( "Activate: %d\n", fAppActive ));
521 if ( OldfAppActive != fAppActive ) {
525 // mprintf(( "Priority: HIGH\n" ));
529 SetThreadPriority( hThread, THREAD_PRIORITY_HIGHEST );
532 gr_activate(fAppActive);
533 //SetThreadPriority( hThread, THREAD_PRIORITY_TIME_CRITICAL );
535 //mprintf(( "Priority: LOW\n" ));
540 //mprintf(( "Mouse:Alive\n" ));
543 // key_outkey( KEY_PAUSE );
546 SetThreadPriority( hThread, THREAD_PRIORITY_NORMAL );
548 gr_activate(fAppActive);
555 // mprintf(( "WM_DESTROY called\n" ));
560 gameseq_post_event(GS_EVENT_QUIT_GAME);
564 // mprintf(( "Sys command called '%x'\n", wParam ));
565 if ( wParam != SC_SCREENSAVE ){
566 return DefWindowProc(hwnd, msg, wParam, lParam);
572 rtvoice_stream_data((uint)hwnd, (uint)wParam, (uint)lParam);
577 return DefWindowProc(hwnd, msg, wParam, lParam);
584 // create the main window
585 BOOL win32_create_window()
589 char *ptr = os_config_read_string(NULL, NOX("Videocard"), NULL);
590 if(ptr && strstr(ptr, NOX("Direct 3D -")) && Cmdline_window){
593 if(ptr && strstr(ptr, NOX("1024")) && Cmdline_window){
598 HINSTANCE hInst = GetModuleHandle(NULL);
600 wclass.hInstance = hInst;
601 wclass.lpszClassName = szWinClass;
602 wclass.lpfnWndProc = (WNDPROC)win32_message_handler;
604 wclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
606 wclass.style = CS_BYTEALIGNCLIENT|CS_VREDRAW | CS_HREDRAW; // | CS_OWNDC; //CS_DBLCLKS | CS_PARENTDC| CS_VREDRAW | CS_HREDRAW |;
608 wclass.cbSize = sizeof(WNDCLASSEX);
609 wclass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_APP_ICON) );
610 wclass.hIconSm = NULL; //LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1) );
611 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
612 wclass.lpszMenuName = NULL; //"FreeSpaceMenu";
613 wclass.cbClsExtra = 0;
614 wclass.cbWndExtra = 0;
615 wclass.hbrBackground = (HBRUSH)NULL;
617 if (!RegisterClassEx(&wclass)) return FALSE;
621 style = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
626 // make sure we adjust for the actual window border
629 x_add += GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
630 y_add += GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
632 // Make a 32x32 window. It never should get shown, because the graphics code
633 // will then size it.
635 hwndApp = CreateWindow( szWinClass, szWinTitle,
639 hires ? 1024 : 640 + x_add,
640 hires ? 768 : 480 + y_add,
641 NULL, (HMENU)NULL, hInst, (LPSTR)NULL);
643 // Make a 32x32 window. It never should get shown, because the graphics code
644 // will then size it.
645 hwndApp = CreateWindow( szWinClass, szWinTitle,
647 (GetSystemMetrics( SM_CXSCREEN )-32 )/2, //x
648 (GetSystemMetrics( SM_CYSCREEN )-32 )/2, //y
650 NULL, (HMENU)NULL, hInst, (LPSTR)NULL);
653 // Hack!! Turn off Window's cursor.
657 main_window_inited = 1;
663 ShowWindow( hwndApp, SW_SHOWNORMAL );
664 UpdateWindow( hwndApp );
676 EnterCriticalSection(&Os_lock);
677 while(PeekMessage(&msg,0,0,0,PM_NOREMOVE)) {
678 if ( msg.message == WM_DESTROY ) {
681 if (PeekMessage(&msg,0,0,0,PM_REMOVE)) {
682 TranslateMessage(&msg);
683 DispatchMessage(&msg);
687 LeaveCriticalSection(&Os_lock);