]> icculus.org git repositories - taylor/freespace2.git/blob - src/osapi/os_win.cpp
finished cfilesystem.
[taylor/freespace2.git] / src / osapi / os_win.cpp
1 /*
2  * $Logfile: /Freespace2/code/OsApi/OsApi.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Low level Windows code
8  *
9  * $Log$
10  * Revision 1.2  2002/05/07 03:16:48  theoddone33
11  * The Great Newline Fix
12  *
13  * Revision 1.1.1.1  2002/05/03 03:28:10  root
14  * Initial import.
15  * 
16  * 
17  * 7     6/30/99 5:53p Dave
18  * Put in new anti-camper code.
19  * 
20  * 6     6/03/99 6:37p Dave
21  * More TNT fun. Made perspective bitmaps more flexible.
22  * 
23  * 5     6/02/99 6:18p Dave
24  * Fixed TNT lockup problems! Wheeeee!
25  * 
26  * 4     12/18/98 1:13a Dave
27  * Rough 1024x768 support for Direct3D. Proper detection and usage through
28  * the launcher.
29  * 
30  * 3     10/09/98 2:57p Dave
31  * Starting splitting up OS stuff.
32  * 
33  * 2     10/08/98 2:38p Dave
34  * Cleanup up OsAPI code significantly. Removed old functions, centralized
35  * registry functions.
36  * 
37  * 118   7/10/98 5:04p Dave
38  * Fix connection speed bug on standalone server.
39  * 
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.
44  * 
45  * 116   5/18/98 9:22p John
46  * Took out the annoying tab check.
47  * 
48  * 115   5/18/98 11:17a John
49  * Fixed some bugs with software window and output window.
50  * 
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.
55  * 
56  * 113   5/15/98 4:49p John
57  * 
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.
61  * 
62  * 111   5/14/98 5:42p John
63  * Revamped the whole window position/mouse code for the graphics windows.
64  * 
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.
68  *
69  * $NoKeywords: $
70  */
71
72 #include <windows.h>
73 #include <windowsx.h>
74 #include <commctrl.h>
75 #include <stdio.h>
76 #include <io.h>
77 #include <fcntl.h>
78 #include <winsock.h>
79 #include <stdarg.h>
80 #include <direct.h>
81
82 #include "pstypes.h"
83 #include "osapi.h"
84 #include "key.h"
85 #include "palman.h"
86 #include "mouse.h"
87 #include "outwnd.h"
88 #include "2d.h"
89 #include "cfile.h"
90 #include "sound.h"
91 #include "freespaceresource.h"
92 #include "managepilot.h"
93 #include "joy.h"
94 #include "joy_ff.h"
95 #include "gamesequence.h"
96 #include "freespace.h"
97 #include "osregistry.h"
98 #include "cmdline.h"
99
100 // ----------------------------------------------------------------------------------------------------
101 // OSAPI DEFINES/VARS
102 //
103
104 // os-wide globals
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;
115
116 static CRITICAL_SECTION Os_lock;
117
118 int Os_debugger_running = 0;
119
120 // ----------------------------------------------------------------------------------------------------
121 // OSAPI FORWARD DECLARATIONS
122 //
123
124 #ifdef THREADED
125         // thread handler for the main message thread
126         DWORD win32_process(DWORD lparam);
127 #else
128         DWORD win32_process1(DWORD lparam);
129         DWORD win32_process1(DWORD lparam);
130 #endif
131
132 // Fills in the Os_debugger_running with non-zero if debugger detected.
133 void os_check_debugger();
134
135 // called at shutdown. Makes sure all thread processing terminates.
136 void os_deinit();
137
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 );
140
141 // message handler for the main thread
142 LRESULT CALLBACK win32_message_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam);
143
144 // create the main window
145 BOOL win32_create_window();
146
147
148 // ----------------------------------------------------------------------------------------------------
149 // OSAPI FUNCTIONS
150 //
151
152 // initialization/shutdown functions -----------------------------------------------
153
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 )
157 {
158         os_init_registry_stuff(Osreg_company_name, title, version_string);
159         
160         strcpy( szWinTitle, title );
161         strcpy( szWinClass, wclass );   
162
163         InitializeCriticalSection( &Os_lock );
164
165         #ifdef THREADED
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" ));
173                 }
174                 CloseHandle(Window_created);
175                 Window_created = NULL;
176         #else
177                 win32_process1(0);
178         #endif // THREADED
179
180         // initialized
181         Os_inited = 1;
182
183         // check to see if we're running under msdev
184         os_check_debugger();
185
186         atexit(os_deinit);
187 }
188
189 // set the main window title
190 void os_set_title( char * title )
191 {
192         strcpy( szWinTitle, title );
193         SetWindowText( hwndApp, szWinTitle );
194 }
195
196 // call at program end
197 void os_cleanup()
198 {
199         // Tell the app to quit
200         PostMessage( hwndApp, WM_DESTROY, 0, 0 );
201         
202         #ifndef NDEBUG
203                 outwnd_close();
204         #endif
205 }
206
207
208 // window management -----------------------------------------------------------------
209
210 // Returns 1 if app is not the foreground app.
211 int os_foreground()
212 {
213         return fAppActive;
214 }
215
216 // Returns the handle to the main window
217 uint os_get_window()
218 {
219         return (uint)hwndApp;
220 }
221
222
223 // process management -----------------------------------------------------------------
224
225 // Sleeps for n milliseconds or until app becomes active.
226 void os_sleep(int ms)
227 {
228         Sleep(ms);
229 }
230
231 // Used to stop message processing
232 void os_suspend()
233 {
234         ENTER_CRITICAL_SECTION(&Os_lock);       
235 }
236
237 // resume message processing
238 void os_resume()
239 {
240         LEAVE_CRITICAL_SECTION(&Os_lock);       
241 }
242
243
244 // ----------------------------------------------------------------------------------------------------
245 // OSAPI FORWARD DECLARATIONS
246 //
247
248 #pragma warning(disable:4702)
249 #ifdef THREADED
250
251 // thread handler for the main message thread
252 DWORD win32_process(DWORD lparam)
253 {
254         MSG msg;
255         HANDLE Window_created = (HANDLE)lparam;
256
257         if ( !win32_create_window() )
258                 return 0;
259
260         // Let the app continue once the window is created
261         SetEvent(Window_created);
262
263         while (1)       {       
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);
269
270                                         // cleanup and exit this thread!!
271                                         DeleteCriticalSection(&Os_lock);
272                                         return 0;
273                                 }
274                                 TranslateMessage(&msg);
275                                 DispatchMessage(&msg);
276                         }
277                         LeaveCriticalSection(&Os_lock);
278                 } 
279         }
280         return 0;
281 }
282
283 #else
284 DWORD win32_process1(DWORD lparam)
285 {
286         if ( !win32_create_window() )
287                 return 0;
288
289         return 0;
290 }
291
292
293 DWORD win32_process2(DWORD lparam)
294 {
295         MSG msg;
296
297         while(PeekMessage(&msg,0,0,0,PM_REMOVE))        {
298                 TranslateMessage(&msg);
299                 DispatchMessage(&msg);
300         }
301
302         return 0;
303 }
304 #endif // THREADED
305 #pragma warning(default:4702)
306
307 // Fills in the Os_debugger_running with non-zero if debugger detected.
308 void os_check_debugger()
309 {
310         HMODULE hMod;
311         char search_string[256];
312         char myname[128];
313         int namelen;
314         char * p;
315
316         Os_debugger_running = 0;                // Assume its not
317
318         // Find my EXE file name
319         hMod = GetModuleHandle(NULL);
320         if ( !hMod ) return;
321         namelen = GetModuleFileName( hMod, myname, 127 );       
322         if ( namelen < 1 ) return;
323         
324         // Strip off the .EXE
325         p = strstr( myname, ".exe" );
326         if (!p) return;
327         *p = '\0';
328
329         // Move p to point to first letter of EXE filename
330         while( (*p!='\\') && (*p!='/') && (*p!=':') )
331                 p--;
332         p++;    
333         if ( strlen(p) < 1 ) return;
334
335         // Build what the debugger's window title would be if the debugger is running...
336         sprintf( search_string, "[run] - %s -", p );
337
338         // ... and then search for it.
339         EnumWindows( (int (__stdcall *)(struct HWND__ *,long))os_enum_windows, (long)&search_string );
340 }
341
342 // called at shutdown. Makes sure all thread processing terminates.
343 void os_deinit()
344 {
345         if (hThread)    {
346                 CloseHandle(hThread);
347                 hThread = NULL;
348         }
349 }
350
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 )
353 {
354         char tmp[128];
355         int len;
356
357         len = GetWindowText( hwnd, tmp, 127 );
358          
359         if ( len )      {
360                 if ( strstr( tmp, search_string ))      {
361                         Os_debugger_running = 1;                // found the search string!
362                         return FALSE;   // stop enumerating windows
363                 }
364         }
365
366         return TRUE;    // continue enumeration
367 }
368
369
370 int Got_message = 0;
371 // message handler for the main thread
372 LRESULT CALLBACK win32_message_handler(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
373 {
374         // Got_message++;
375
376         switch(msg)     {
377
378         case WM_QUERYNEWPALETTE:
379                 // mprintf(( "WM: QueryNewPalette\n" ));
380                 return TRUE;    // Say that I've realized my own palette
381                 break;
382         case WM_PALETTECHANGED:
383                 // mprintf(( "WM: PaletteChanged\n" ));
384                 break;
385         case WM_PALETTEISCHANGING:
386                 // mprintf(( "WM: PaletteIsChanging\n" ));
387                 break;
388
389         case WM_DISPLAYCHANGE:
390                 // mprintf(( "WM: DisplayChange\n" ));
391                 break;
392
393         case WM_LBUTTONDOWN:
394                 mouse_mark_button( MOUSE_LEFT_BUTTON, 1 );
395                 break;
396
397         case WM_LBUTTONUP:
398                 mouse_mark_button( MOUSE_LEFT_BUTTON, 0 );
399                 break;
400
401         case WM_RBUTTONDOWN:
402                 mouse_mark_button( MOUSE_RIGHT_BUTTON, 1 );
403                 break;
404
405         case WM_RBUTTONUP:
406                 mouse_mark_button( MOUSE_RIGHT_BUTTON, 0 );
407                 break;
408
409         case WM_MBUTTONDOWN:
410                 mouse_mark_button( MOUSE_MIDDLE_BUTTON, 1 );
411                 break;
412
413         case WM_MBUTTONUP:
414                 mouse_mark_button( MOUSE_MIDDLE_BUTTON, 0 );
415                 break;
416
417         case WM_TIMER:
418                 break;
419
420         case WM_SYSKEYDOWN:
421         case WM_KEYDOWN:        {               
422                         int nVirtKey;
423                         uint lKeyData;
424
425                         int latency;
426                         latency = timeGetTime() - GetMessageTime();
427                         if ( latency < 0 )
428                                 latency=0;
429
430                         nVirtKey = (int)wParam;    // virtual-key code 
431                         lKeyData = (lParam>>16) & 255;          // key data 
432                         if ( (lParam>>16) & 256 ) lKeyData += 0x80;
433
434                         // Fix up print screen, whose OEM code is wrong under 95.
435                         if ( nVirtKey == VK_SNAPSHOT )  {
436                                 lKeyData = KEY_PRINT_SCRN;      
437                         }
438
439                         if (lKeyData == KEY_RSHIFT)  // either shift is just a shift to us..
440                                 lKeyData = KEY_LSHIFT;
441
442                         if (lKeyData == KEY_RALT)  // Same with alt keys..
443                                 lKeyData = KEY_LALT;
444
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 );                  
449                 }
450                 break;
451
452         case WM_SYSKEYUP:
453         case WM_KEYUP:   {              
454                         int nVirtKey;
455                         uint lKeyData;
456
457                         int latency;
458                         latency = timeGetTime() - GetMessageTime();
459                         if ( latency < 0 )
460                                 latency=0;
461
462                         nVirtKey = (int) wParam;    // virtual-key code 
463                         lKeyData = (lParam>>16) & 255;          // key data 
464                         if ( (lParam>>16) & 256 ) lKeyData += 0x80;
465
466                         // Fix up print screen, whose OEM code is wrong under 95.
467                         if ( nVirtKey == VK_SNAPSHOT )  {
468                                 lKeyData = KEY_PRINT_SCRN;      
469                         }
470
471                         if (lKeyData == KEY_RSHIFT)  // either shift is just a shift to us..
472                                 lKeyData = KEY_LSHIFT;
473
474                         if (lKeyData == KEY_RALT)  // Same with alt keys..
475                                 lKeyData = KEY_LALT;
476
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 );
482
483                         } else {
484                                 key_mark( lKeyData, 0, latency );
485                         }                       
486                 }               
487                 break;
488
489         case WM_KILLFOCUS:
490                 key_lost_focus();
491
492                 gr_activate(0);
493                 break;
494
495         case WM_SETFOCUS:
496                 key_got_focus();
497
498                 gr_activate(1);
499                 break;
500
501         case WM_ACTIVATE:                       //APP:  //
502         case WM_ACTIVATEAPP:
503                 {
504                         BOOL OldfAppActive = fAppActive;
505                         
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;
511                                 } else {
512                                         fAppActive = FALSE;     //(BOOL)wParam;
513                                 }
514
515                         } else {
516                                 fAppActive = (BOOL)wParam;
517                         }
518
519                         //mprintf(( "Activate: %d\n", fAppActive ));
520
521                         if ( OldfAppActive != fAppActive )      {
522
523                                 if ( fAppActive )       {
524                                         // maximize it
525                                         //      mprintf(( "Priority: HIGH\n" ));
526                                         joy_reacquire_ff();
527
528 #ifdef THREADED
529                                         SetThreadPriority( hThread, THREAD_PRIORITY_HIGHEST );
530 #endif
531
532                                         gr_activate(fAppActive);
533                                         //SetThreadPriority( hThread, THREAD_PRIORITY_TIME_CRITICAL );
534                                 } else {
535                                         //mprintf(( "Priority: LOW\n" ));
536                                         joy_unacquire_ff();
537                                         if (Mouse_hidden)       {
538                                                 Mouse_hidden=0;
539                                                 //ShowCursor(1);
540                                                 //mprintf(( "Mouse:Alive\n" ));         
541                                         }
542                                         // minimize it
543                                         // key_outkey( KEY_PAUSE );
544
545 #ifdef THREADED
546                                         SetThreadPriority( hThread, THREAD_PRIORITY_NORMAL );
547 #endif
548                                         gr_activate(fAppActive);
549                                 }
550                         }
551                 }
552                 break;
553
554         case WM_DESTROY:
555                 // mprintf(( "WM_DESTROY called\n" ));
556                 PostQuitMessage(0);
557                 break;
558
559         case WM_CLOSE:
560                 gameseq_post_event(GS_EVENT_QUIT_GAME);
561                 break;
562
563         case WM_SYSCOMMAND:
564                 // mprintf(( "Sys command called '%x'\n", wParam ));
565                  if ( wParam != SC_SCREENSAVE ){
566                          return DefWindowProc(hwnd, msg, wParam, lParam);
567                  }
568                  break;
569
570 /*
571         case MM_WIM_DATA:
572                 rtvoice_stream_data((uint)hwnd, (uint)wParam, (uint)lParam);
573                 break;
574 */
575
576         default:
577                 return DefWindowProc(hwnd, msg, wParam, lParam);
578                 break;
579         }
580
581         return 0;
582 }
583
584 // create the main window
585 BOOL win32_create_window()
586 {
587         int windowed = 0;
588         int hires = 0;
589         char *ptr = os_config_read_string(NULL, NOX("Videocard"), NULL);        
590         if(ptr && strstr(ptr, NOX("Direct 3D -")) && Cmdline_window){
591                 windowed = 1;
592         }
593         if(ptr && strstr(ptr, NOX("1024")) && Cmdline_window){
594                 hires = 1;
595         }
596
597         WNDCLASSEX wclass;
598         HINSTANCE hInst = GetModuleHandle(NULL);
599
600         wclass.hInstance                = hInst;
601         wclass.lpszClassName = szWinClass;
602         wclass.lpfnWndProc      = (WNDPROC)win32_message_handler;               
603         if(windowed){
604                 wclass.style                    = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
605         } else {
606                 wclass.style                    = CS_BYTEALIGNCLIENT|CS_VREDRAW | CS_HREDRAW;   // | CS_OWNDC;  //CS_DBLCLKS | CS_PARENTDC| CS_VREDRAW | CS_HREDRAW |;  
607         }               
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;
616
617         if (!RegisterClassEx(&wclass)) return FALSE;
618         
619         int style;
620         if(windowed){
621                 style = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
622         } else {
623                 style = WS_POPUP;
624         }       
625
626         // make sure we adjust for the actual window border     
627         int x_add = 0;
628         int y_add = 0;
629         x_add += GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
630         y_add += GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
631
632         // Make a 32x32 window.  It never should get shown, because the graphics code
633         // will then size it.
634         if(windowed){
635                 hwndApp = CreateWindow( szWinClass, szWinTitle,
636                                                                         style,   
637                                                                         CW_USEDEFAULT,
638                                                                         CW_USEDEFAULT,
639                                                                         hires ? 1024 : 640 + x_add,
640                                                                         hires ? 768 : 480 + y_add,
641                                                                         NULL, (HMENU)NULL, hInst, (LPSTR)NULL); 
642         } else {
643                 // Make a 32x32 window.  It never should get shown, because the graphics code
644                 // will then size it.
645                 hwndApp = CreateWindow( szWinClass, szWinTitle,
646                                                                         style,   
647                                                                         (GetSystemMetrics( SM_CXSCREEN )-32 )/2,        //x
648                                                                         (GetSystemMetrics( SM_CYSCREEN )-32 )/2,        //y
649                                                                         32,32,                                                                  // wh
650                                                                         NULL, (HMENU)NULL, hInst, (LPSTR)NULL); 
651         }
652
653         // Hack!! Turn off Window's cursor.
654         ShowCursor(0);
655         ClipCursor(NULL);
656
657         main_window_inited = 1;
658         #ifndef NDEBUG
659                 outwnd_init(1);
660         #endif  
661
662         if(windowed){
663                 ShowWindow( hwndApp, SW_SHOWNORMAL );
664                 UpdateWindow( hwndApp );
665         }
666
667         return TRUE;
668 }
669
670 void os_poll()
671 {
672 #ifndef THREADED
673         win32_process2(0);
674 #else
675         MSG msg;
676         EnterCriticalSection(&Os_lock);
677         while(PeekMessage(&msg,0,0,0,PM_NOREMOVE))      {               
678                 if ( msg.message == WM_DESTROY )        {
679                         break;
680                 }
681                 if (PeekMessage(&msg,0,0,0,PM_REMOVE))  {
682                         TranslateMessage(&msg);
683                         DispatchMessage(&msg);
684                 }               
685                 Got_message++;
686         }
687         LeaveCriticalSection(&Os_lock);
688 #endif
689 }
690
691 void debug_int3()
692 {
693         gr_activate(0);
694         _asm { int 3 };
695         gr_activate(1);
696 }
697