2 * $Logfile: /Freespace2/code/OsApi/OsApi.cpp $
7 * Low level Windows code
10 * Revision 1.1 2002/05/03 03:28:10 root
14 * 7 6/30/99 5:53p Dave
15 * Put in new anti-camper code.
17 * 6 6/03/99 6:37p Dave
18 * More TNT fun. Made perspective bitmaps more flexible.
20 * 5 6/02/99 6:18p Dave
21 * Fixed TNT lockup problems! Wheeeee!
23 * 4 12/18/98 1:13a Dave
24 * Rough 1024x768 support for Direct3D. Proper detection and usage through
27 * 3 10/09/98 2:57p Dave
28 * Starting splitting up OS stuff.
30 * 2 10/08/98 2:38p Dave
31 * Cleanup up OsAPI code significantly. Removed old functions, centralized
34 * 118 7/10/98 5:04p Dave
35 * Fix connection speed bug on standalone server.
37 * 117 5/24/98 2:28p Hoffoss
38 * Because we never really care about if the left or the right shift or
39 * alt key was used, but rather than either shift or alt was used, made
40 * both map to the left one. Solves some problems, causes none.
42 * 116 5/18/98 9:22p John
43 * Took out the annoying tab check.
45 * 115 5/18/98 11:17a John
46 * Fixed some bugs with software window and output window.
48 * 114 5/16/98 2:20p John
49 * Changed the os_suspend and resume to use a critical section to prevent
50 * threads from executing rather than just suspending the thread. Had to
51 * make sure gr_close was called before os_close.
53 * 113 5/15/98 4:49p John
55 * 112 5/15/98 3:36p John
56 * Fixed bug with new graphics window code and standalone server. Made
57 * hwndApp not be a global anymore.
59 * 111 5/14/98 5:42p John
60 * Revamped the whole window position/mouse code for the graphics windows.
62 * 110 5/04/98 11:08p Hoffoss
63 * Expanded on Force Feedback code, and moved it all into Joy_ff.cpp.
64 * Updated references everywhere to it.
88 #include "freespaceresource.h"
89 #include "managepilot.h"
92 #include "gamesequence.h"
93 #include "freespace.h"
94 #include "osregistry.h"
97 // ----------------------------------------------------------------------------------------------------
102 static HINSTANCE hInstApp;
103 static HWND hwndApp = NULL;
104 static HANDLE hThread=NULL;
105 static DWORD ThreadID;
106 static int fAppActive = 0;
107 static int main_window_inited = 0;
108 static char szWinTitle[128];
109 static char szWinClass[128];
110 static int WinX, WinY, WinW, WinH;
111 static int Os_inited = 0;
113 static CRITICAL_SECTION Os_lock;
115 int Os_debugger_running = 0;
117 // ----------------------------------------------------------------------------------------------------
118 // OSAPI FORWARD DECLARATIONS
122 // thread handler for the main message thread
123 DWORD win32_process(DWORD lparam);
125 DWORD win32_process1(DWORD lparam);
126 DWORD win32_process1(DWORD lparam);
129 // Fills in the Os_debugger_running with non-zero if debugger detected.
130 void os_check_debugger();
132 // called at shutdown. Makes sure all thread processing terminates.
135 // go through all windows and try and find the one that matches the search string
136 BOOL __stdcall os_enum_windows( HWND hwnd, char * search_string );
138 // message handler for the main thread
139 LRESULT CALLBACK win32_message_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam);
141 // create the main window
142 BOOL win32_create_window();
145 // ----------------------------------------------------------------------------------------------------
149 // initialization/shutdown functions -----------------------------------------------
151 // If app_name is NULL or ommited, then TITLE is used
152 // for the app name, which is where registry keys are stored.
153 void os_init(char * wclass, char * title, char *app_name, char *version_string )
155 os_init_registry_stuff(Osreg_company_name, title, version_string);
157 strcpy( szWinTitle, title );
158 strcpy( szWinClass, wclass );
160 InitializeCriticalSection( &Os_lock );
163 // Create an even to signal that the window is created,
164 // so that we don't return from this function until
165 // the window is all properly created.
166 HANDLE Window_created = CreateEvent( NULL, FALSE, FALSE, NULL );
167 hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)win32_process, Window_created, 0, &ThreadID );
168 if ( WaitForSingleObject( Window_created, 5000 )==WAIT_TIMEOUT) { //INFINITE );
169 mprintf(( "Wait timeout!\n" ));
171 CloseHandle(Window_created);
172 Window_created = NULL;
180 // check to see if we're running under msdev
186 // set the main window title
187 void os_set_title( char * title )
189 strcpy( szWinTitle, title );
190 SetWindowText( hwndApp, szWinTitle );
193 // call at program end
196 // Tell the app to quit
197 PostMessage( hwndApp, WM_DESTROY, 0, 0 );
205 // window management -----------------------------------------------------------------
207 // Returns 1 if app is not the foreground app.
213 // Returns the handle to the main window
216 return (uint)hwndApp;
220 // process management -----------------------------------------------------------------
222 // Sleeps for n milliseconds or until app becomes active.
223 void os_sleep(int ms)
228 // Used to stop message processing
231 ENTER_CRITICAL_SECTION(&Os_lock);
234 // resume message processing
237 LEAVE_CRITICAL_SECTION(&Os_lock);
241 // ----------------------------------------------------------------------------------------------------
242 // OSAPI FORWARD DECLARATIONS
245 #pragma warning(disable:4702)
248 // thread handler for the main message thread
249 DWORD win32_process(DWORD lparam)
252 HANDLE Window_created = (HANDLE)lparam;
254 if ( !win32_create_window() )
257 // Let the app continue once the window is created
258 SetEvent(Window_created);
261 if (WaitMessage() == TRUE) {
262 EnterCriticalSection(&Os_lock);
263 while(PeekMessage(&msg,0,0,0,PM_REMOVE)) {
264 if ( msg.message == WM_DESTROY ) {
265 LeaveCriticalSection(&Os_lock);
267 // cleanup and exit this thread!!
268 DeleteCriticalSection(&Os_lock);
271 TranslateMessage(&msg);
272 DispatchMessage(&msg);
274 LeaveCriticalSection(&Os_lock);
281 DWORD win32_process1(DWORD lparam)
283 if ( !win32_create_window() )
290 DWORD win32_process2(DWORD lparam)
294 while(PeekMessage(&msg,0,0,0,PM_REMOVE)) {
295 TranslateMessage(&msg);
296 DispatchMessage(&msg);
302 #pragma warning(default:4702)
304 // Fills in the Os_debugger_running with non-zero if debugger detected.
305 void os_check_debugger()
308 char search_string[256];
313 Os_debugger_running = 0; // Assume its not
315 // Find my EXE file name
316 hMod = GetModuleHandle(NULL);
318 namelen = GetModuleFileName( hMod, myname, 127 );
319 if ( namelen < 1 ) return;
321 // Strip off the .EXE
322 p = strstr( myname, ".exe" );
326 // Move p to point to first letter of EXE filename
327 while( (*p!='\\') && (*p!='/') && (*p!=':') )
330 if ( strlen(p) < 1 ) return;
332 // Build what the debugger's window title would be if the debugger is running...
333 sprintf( search_string, "[run] - %s -", p );
335 // ... and then search for it.
336 EnumWindows( (int (__stdcall *)(struct HWND__ *,long))os_enum_windows, (long)&search_string );
339 // called at shutdown. Makes sure all thread processing terminates.
343 CloseHandle(hThread);
348 // go through all windows and try and find the one that matches the search string
349 BOOL __stdcall os_enum_windows( HWND hwnd, char * search_string )
354 len = GetWindowText( hwnd, tmp, 127 );
357 if ( strstr( tmp, search_string )) {
358 Os_debugger_running = 1; // found the search string!
359 return FALSE; // stop enumerating windows
363 return TRUE; // continue enumeration
368 // message handler for the main thread
369 LRESULT CALLBACK win32_message_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
375 case WM_QUERYNEWPALETTE:
376 // mprintf(( "WM: QueryNewPalette\n" ));
377 return TRUE; // Say that I've realized my own palette
379 case WM_PALETTECHANGED:
380 // mprintf(( "WM: PaletteChanged\n" ));
382 case WM_PALETTEISCHANGING:
383 // mprintf(( "WM: PaletteIsChanging\n" ));
386 case WM_DISPLAYCHANGE:
387 // mprintf(( "WM: DisplayChange\n" ));
391 mouse_mark_button( MOUSE_LEFT_BUTTON, 1 );
395 mouse_mark_button( MOUSE_LEFT_BUTTON, 0 );
399 mouse_mark_button( MOUSE_RIGHT_BUTTON, 1 );
403 mouse_mark_button( MOUSE_RIGHT_BUTTON, 0 );
407 mouse_mark_button( MOUSE_MIDDLE_BUTTON, 1 );
411 mouse_mark_button( MOUSE_MIDDLE_BUTTON, 0 );
423 latency = timeGetTime() - GetMessageTime();
427 nVirtKey = (int)wParam; // virtual-key code
428 lKeyData = (lParam>>16) & 255; // key data
429 if ( (lParam>>16) & 256 ) lKeyData += 0x80;
431 // Fix up print screen, whose OEM code is wrong under 95.
432 if ( nVirtKey == VK_SNAPSHOT ) {
433 lKeyData = KEY_PRINT_SCRN;
436 if (lKeyData == KEY_RSHIFT) // either shift is just a shift to us..
437 lKeyData = KEY_LSHIFT;
439 if (lKeyData == KEY_RALT) // Same with alt keys..
442 // mprintf(( "Key down = 0x%x|%x\n", lKeyData, nVirtKey ));
443 key_mark( lKeyData, 1, latency );
444 // mprintf(( "Key down = 0x%x\n", lKeyData ));
445 //Warning( LOCATION, "Key = 0x%x", lKeyData );
455 latency = timeGetTime() - GetMessageTime();
459 nVirtKey = (int) wParam; // virtual-key code
460 lKeyData = (lParam>>16) & 255; // key data
461 if ( (lParam>>16) & 256 ) lKeyData += 0x80;
463 // Fix up print screen, whose OEM code is wrong under 95.
464 if ( nVirtKey == VK_SNAPSHOT ) {
465 lKeyData = KEY_PRINT_SCRN;
468 if (lKeyData == KEY_RSHIFT) // either shift is just a shift to us..
469 lKeyData = KEY_LSHIFT;
471 if (lKeyData == KEY_RALT) // Same with alt keys..
474 // mprintf(( "Key up = 0x%x|%x\n", lKeyData, nVirtKey ));
475 if ( lKeyData == 0xB7 ) {
476 // Hack for PrintScreen which only sends one up message!
477 key_mark( lKeyData, 1, latency );
478 key_mark( lKeyData, 0, latency );
481 key_mark( lKeyData, 0, latency );
498 case WM_ACTIVATE: //APP: //
501 BOOL OldfAppActive = fAppActive;
503 // The application z-ordering has changed. If we are now the
504 // foreground application wParm will be TRUE.
505 if ( msg == WM_ACTIVATE ) {
506 if ( (LOWORD(wParam) == WA_ACTIVE) || (LOWORD(wParam)==WA_CLICKACTIVE)) {
507 fAppActive = TRUE; //(BOOL)wParam;
509 fAppActive = FALSE; //(BOOL)wParam;
513 fAppActive = (BOOL)wParam;
516 //mprintf(( "Activate: %d\n", fAppActive ));
518 if ( OldfAppActive != fAppActive ) {
522 // mprintf(( "Priority: HIGH\n" ));
526 SetThreadPriority( hThread, THREAD_PRIORITY_HIGHEST );
529 gr_activate(fAppActive);
530 //SetThreadPriority( hThread, THREAD_PRIORITY_TIME_CRITICAL );
532 //mprintf(( "Priority: LOW\n" ));
537 //mprintf(( "Mouse:Alive\n" ));
540 // key_outkey( KEY_PAUSE );
543 SetThreadPriority( hThread, THREAD_PRIORITY_NORMAL );
545 gr_activate(fAppActive);
552 // mprintf(( "WM_DESTROY called\n" ));
557 gameseq_post_event(GS_EVENT_QUIT_GAME);
561 // mprintf(( "Sys command called '%x'\n", wParam ));
562 if ( wParam != SC_SCREENSAVE ){
563 return DefWindowProc(hwnd, msg, wParam, lParam);
569 rtvoice_stream_data((uint)hwnd, (uint)wParam, (uint)lParam);
574 return DefWindowProc(hwnd, msg, wParam, lParam);
581 // create the main window
582 BOOL win32_create_window()
586 char *ptr = os_config_read_string(NULL, NOX("Videocard"), NULL);
587 if(ptr && strstr(ptr, NOX("Direct 3D -")) && Cmdline_window){
590 if(ptr && strstr(ptr, NOX("1024")) && Cmdline_window){
595 HINSTANCE hInst = GetModuleHandle(NULL);
597 wclass.hInstance = hInst;
598 wclass.lpszClassName = szWinClass;
599 wclass.lpfnWndProc = (WNDPROC)win32_message_handler;
601 wclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
603 wclass.style = CS_BYTEALIGNCLIENT|CS_VREDRAW | CS_HREDRAW; // | CS_OWNDC; //CS_DBLCLKS | CS_PARENTDC| CS_VREDRAW | CS_HREDRAW |;
605 wclass.cbSize = sizeof(WNDCLASSEX);
606 wclass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_APP_ICON) );
607 wclass.hIconSm = NULL; //LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1) );
608 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
609 wclass.lpszMenuName = NULL; //"FreeSpaceMenu";
610 wclass.cbClsExtra = 0;
611 wclass.cbWndExtra = 0;
612 wclass.hbrBackground = (HBRUSH)NULL;
614 if (!RegisterClassEx(&wclass)) return FALSE;
618 style = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
623 // make sure we adjust for the actual window border
626 x_add += GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
627 y_add += GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
629 // Make a 32x32 window. It never should get shown, because the graphics code
630 // will then size it.
632 hwndApp = CreateWindow( szWinClass, szWinTitle,
636 hires ? 1024 : 640 + x_add,
637 hires ? 768 : 480 + y_add,
638 NULL, (HMENU)NULL, hInst, (LPSTR)NULL);
640 // Make a 32x32 window. It never should get shown, because the graphics code
641 // will then size it.
642 hwndApp = CreateWindow( szWinClass, szWinTitle,
644 (GetSystemMetrics( SM_CXSCREEN )-32 )/2, //x
645 (GetSystemMetrics( SM_CYSCREEN )-32 )/2, //y
647 NULL, (HMENU)NULL, hInst, (LPSTR)NULL);
650 // Hack!! Turn off Window's cursor.
654 main_window_inited = 1;
660 ShowWindow( hwndApp, SW_SHOWNORMAL );
661 UpdateWindow( hwndApp );
673 EnterCriticalSection(&Os_lock);
674 while(PeekMessage(&msg,0,0,0,PM_NOREMOVE)) {
675 if ( msg.message == WM_DESTROY ) {
678 if (PeekMessage(&msg,0,0,0,PM_REMOVE)) {
679 TranslateMessage(&msg);
680 DispatchMessage(&msg);
684 LeaveCriticalSection(&Os_lock);