]> icculus.org git repositories - taylor/freespace2.git/blob - src/osapi/osapi.cpp
send quit game event when OS window gets closed
[taylor/freespace2.git] / src / osapi / osapi.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/OsApi/OsApi.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Low level Windows code
16  *
17  * $Log$
18  * Revision 1.18  2005/10/01 21:49:11  taylor
19  * don't use CTRL-Z for minimizing since it's an extrememly common key combo in the game
20  *
21  * Revision 1.17  2004/12/15 04:10:45  taylor
22  * outwnd_unix.cpp from fs2_open for logging to file in debug mode
23  * fixes for default function values
24  * always use vm_* functions for sanity sake
25  * make cfilearchiver 64-bit compatible
26  * fix crash on exit from double free()
27  * fix crash on startup from extra long GL extension string in debug
28  *
29  * Revision 1.16  2003/12/15 06:24:51  theoddone33
30  * Bumpy ride... hang on.
31  *
32  * Revision 1.15  2003/08/03 15:56:59  taylor
33  * simpler mouse usage; default ini settings in os_init(); cleanup
34  *
35  * Revision 1.14  2003/05/09 05:04:15  taylor
36  * better window min/max/focus support
37  *
38  * Revision 1.13  2003/05/04 04:56:53  taylor
39  * move SDL_Quit to os_deinit to fix fonttool segfault
40  *
41  * Revision 1.12  2003/02/20 17:41:07  theoddone33
42  * Userdir patch from Taylor Richards
43  *
44  * Revision 1.11  2002/07/28 21:45:30  theoddone33
45  * Add ctrl-z to iconify window
46  *
47  * Revision 1.10  2002/07/28 21:39:44  theoddone33
48  * Add alt-enter to toggle fullscreen and ctrl-g to toggle mouse grabbing
49  *
50  * Revision 1.9  2002/06/16 23:59:31  relnev
51  * untested joystick code
52  *
53  * Revision 1.8  2002/06/09 04:41:25  relnev
54  * added copyright header
55  *
56  * Revision 1.7  2002/06/05 04:03:32  relnev
57  * finished cfilesystem.
58  *
59  * removed some old code.
60  *
61  * fixed mouse save off-by-one.
62  *
63  * sound cleanups.
64  *
65  * Revision 1.6  2002/05/31 03:34:02  theoddone33
66  * Fix Keyboard
67  * Add titlebar
68  *
69  * Revision 1.5  2002/05/30 23:46:29  theoddone33
70  * some minor key changes (not necessarily fixes)
71  *
72  * Revision 1.4  2002/05/30 16:50:24  theoddone33
73  * Keyboard partially fixed
74  *
75  * Revision 1.3  2002/05/29 06:25:13  theoddone33
76  * Keyboard input, mouse tracking now work
77  *
78  * Revision 1.2  2002/05/07 03:16:48  theoddone33
79  * The Great Newline Fix
80  *
81  * Revision 1.1.1.1  2002/05/03 03:28:10  root
82  * Initial import.
83  *
84  *
85  * 7     6/30/99 5:53p Dave
86  * Put in new anti-camper code.
87  *
88  * 6     6/03/99 6:37p Dave
89  * More TNT fun. Made perspective bitmaps more flexible.
90  *
91  * 5     6/02/99 6:18p Dave
92  * Fixed TNT lockup problems! Wheeeee!
93  *
94  * 4     12/18/98 1:13a Dave
95  * Rough 1024x768 support for Direct3D. Proper detection and usage through
96  * the launcher.
97  *
98  * 3     10/09/98 2:57p Dave
99  * Starting splitting up OS stuff.
100  *
101  * 2     10/08/98 2:38p Dave
102  * Cleanup up OsAPI code significantly. Removed old functions, centralized
103  * registry functions.
104  *
105  * 118   7/10/98 5:04p Dave
106  * Fix connection speed bug on standalone server.
107  *
108  * 117   5/24/98 2:28p Hoffoss
109  * Because we never really care about if the left or the right shift or
110  * alt key was used, but rather than either shift or alt was used, made
111  * both map to the left one.  Solves some problems, causes none.
112  *
113  * 116   5/18/98 9:22p John
114  * Took out the annoying tab check.
115  *
116  * 115   5/18/98 11:17a John
117  * Fixed some bugs with software window and output window.
118  *
119  * 114   5/16/98 2:20p John
120  * Changed the os_suspend and resume to use a critical section to prevent
121  * threads from executing rather than just suspending the thread.  Had to
122  * make sure gr_close was called before os_close.
123  *
124  * 113   5/15/98 4:49p John
125  *
126  * 112   5/15/98 3:36p John
127  * Fixed bug with new graphics window code and standalone server.  Made
128  * hwndApp not be a global anymore.
129  *
130  * 111   5/14/98 5:42p John
131  * Revamped the whole window position/mouse code for the graphics windows.
132  *
133  * 110   5/04/98 11:08p Hoffoss
134  * Expanded on Force Feedback code, and moved it all into Joy_ff.cpp.
135  * Updated references everywhere to it.
136  *
137  * $NoKeywords: $
138  */
139
140 #include "pstypes.h"
141 #include "osapi.h"
142 #include "key.h"
143 #include "palman.h"
144 #include "mouse.h"
145 #include "outwnd.h"
146 #include "2d.h"
147 #include "cfile.h"
148 #include "sound.h"
149 #include "freespaceresource.h"
150 #include "managepilot.h"
151 #include "joy.h"
152 #include "joy_ff.h"
153 #include "gamesequence.h"
154 #include "freespace.h"
155 #include "osregistry.h"
156 #include "cmdline.h"
157
158 // ----------------------------------------------------------------------------------------------------
159 // OSAPI DEFINES/VARS
160 //
161
162 // os-wide globals
163 static int                      fAppActive = 1;
164 static int                      Os_inited = 0;
165 static char                     windowTitle[128];
166
167 static SDL_mutex *Os_lock;
168 static SDL_Window *Os_window = NULL;
169
170 int Os_debugger_running = 0;
171
172 // ----------------------------------------------------------------------------------------------------
173 // OSAPI FORWARD DECLARATIONS
174 //
175
176 // Fills in the Os_debugger_running with non-zero if debugger detected.
177 void os_check_debugger();
178
179 // called at shutdown. Makes sure all thread processing terminates.
180 void os_deinit();
181
182
183 // ----------------------------------------------------------------------------------------------------
184 // OSAPI FUNCTIONS
185 //
186
187 // initialization/shutdown functions -----------------------------------------------
188
189
190 // If app_name is NULL or ommited, then TITLE is used
191 // for the app name, which is where registry keys are stored.
192 void os_init(const char *wclass, const char *title, const char *app_name, const char *version_string)
193 {
194         platform_init();
195
196         os_set_title( (app_name != NULL) ? app_name : title );
197
198         // do some first-run stuff if needed
199         if ( os_config_read_uint(NULL, "StraightToSetup", 1) == 1 ) {
200                 // set some sane config defaults
201                 os_init_registry_stuff();
202
203                 // unset first-run flag
204                 os_config_write_uint(NULL, "StraightToSetup", 0);
205         }
206
207         Os_inited = 1;
208
209         Os_lock = SDL_CreateMutex();
210
211         // check to see if we're running under msdev
212         os_check_debugger();
213
214         atexit(os_deinit);
215 }
216
217 // set the main window title
218 void os_set_title( const char *title )
219 {
220         if ( !title ) {
221                 return;
222         }
223
224         SDL_strlcpy(windowTitle, title, SDL_arraysize(windowTitle));
225
226         if ( !Os_window ) {
227                 return;
228         }
229
230         SDL_SetWindowTitle(Os_window, title);
231 }
232
233 const char *os_get_title()
234 {
235         return windowTitle;
236 }
237
238 // call at program end
239 void os_cleanup()
240 {
241 #ifndef NDEBUG
242                 outwnd_close();
243 #endif
244 }
245
246 void os_set_icon()
247 {
248         #include "app_icon.h"
249
250         Uint32 rmask, gmask, bmask, amask;
251
252         if ( !Os_window ) {
253                 return;
254         }
255
256 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
257         int shift = (app_icon.bytes_per_pixel == 3) ? 8 : 0;
258         rmask = 0xff000000 >> shift;
259         gmask = 0x00ff0000 >> shift;
260         bmask = 0x0000ff00 >> shift;
261         amask = 0x000000ff >> shift;
262 #else
263         rmask = 0x000000ff;
264         gmask = 0x0000ff00;
265         bmask = 0x00ff0000;
266         amask = (app_icon.bytes_per_pixel == 3) ? 0 : 0xff000000;
267 #endif
268
269         SDL_Surface *icon = SDL_CreateRGBSurfaceFrom((void*)app_icon.pixel_data, app_icon.width,
270                 app_icon.height, app_icon.bytes_per_pixel*8, app_icon.bytes_per_pixel*app_icon.width,
271                 rmask, gmask, bmask, amask);
272
273         SDL_SetWindowIcon(Os_window, icon);
274
275         SDL_FreeSurface(icon);
276 }
277
278 // window management -----------------------------------------------------------------
279
280 // Returns 0 if app is not the foreground app.
281 int os_foreground()
282 {
283         return fAppActive;
284 }
285
286 // Returns the handle to the main window
287 SDL_Window *os_get_window()
288 {
289         return Os_window;
290 }
291
292 void os_set_window(SDL_Window *win)
293 {
294         Os_window = win;
295 }
296
297 // process management -----------------------------------------------------------------
298
299 // Used to stop message processing
300 void os_suspend()
301 {
302         SDL_LockMutex(Os_lock);
303 }
304
305 // resume message processing
306 void os_resume()
307 {
308         SDL_UnlockMutex(Os_lock);
309 }
310
311
312 // ----------------------------------------------------------------------------------------------------
313 // OSAPI FORWARD DECLARATIONS
314 //
315
316 // Fills in the Os_debugger_running with non-zero if debugger detected.
317 void os_check_debugger()
318 {
319 }
320
321 // called at shutdown. Makes sure all thread processing terminates.
322 void os_deinit()
323 {
324         platform_close();
325
326         SDL_DestroyMutex(Os_lock);
327
328         SDL_Quit();
329 }
330
331 void os_poll()
332 {
333         SDL_Event e;
334         int button, state;
335
336         while (SDL_PollEvent (&e)) {
337                 switch (e.type) {
338                         case SDL_MOUSEBUTTONDOWN:
339                         case SDL_MOUSEBUTTONUP: {
340                                 if (e.motion.windowID > 0) {
341                                         mouse_mark_button(e.button.button, e.button.state);
342                                 }
343
344                                 break;
345                         }
346
347                         case SDL_MOUSEMOTION: {
348                                 if (e.motion.windowID > 0) {
349                                         mouse_update_pos(e.motion.x, e.motion.y, e.motion.xrel, e.motion.yrel);
350                                 }
351
352                                 break;
353                         }
354
355                         case SDL_TEXTINPUT: {
356                                 key_set_text_input((int)e.text.text[0]);
357
358                                 break;
359                         }
360
361                         case SDL_KEYDOWN: {
362                                 if ( (e.key.keysym.mod & KMOD_GUI) || (platform_get_kmod() & KMOD_GUI) ) {
363                                         if ( !e.key.repeat ) {
364                                                 if (e.key.keysym.sym == SDLK_f) {
365                                                         gr_toggle_fullscreen();
366                                                 } else if (e.key.keysym.sym == SDLK_z) {
367                                                         SDL_MinimizeWindow(Os_window);
368                                                 } else if (e.key.keysym.sym == SDLK_p) {
369                                                         key_mark(SDL_SCANCODE_PRINTSCREEN, 1, 0, 0);
370                                                 }
371                                         }
372                                 } else {
373                                         key_mark(e.key.keysym.scancode, 1, e.key.keysym.mod, 0);
374                                 }
375
376                                 break;
377                         }
378
379                         case SDL_KEYUP: {
380                                 if ( (e.key.keysym.mod & KMOD_GUI) || (platform_get_kmod() & KMOD_GUI) ) {
381                                         // blank, just don't want to process up keys we skipped
382                                         // the down for
383                                 } else {
384                                         key_mark(e.key.keysym.scancode, 0, e.key.keysym.mod, 0);
385                                 }
386
387                                 break;
388                         }
389
390                         case SDL_JOYDEVICEADDED: {
391                                 if ( !Is_standalone ) {
392                                         joy_reinit(e.jdevice.which);
393                                 }
394
395                                 break;
396                         }
397
398                         case SDL_JOYDEVICEREMOVED: {
399                                 // if the active joystick is removed then maybe reinit
400                                 if ( !Is_standalone && (e.jdevice.which == joystick_get_id()) ) {
401                                         joy_reinit();
402                                 }
403
404                                 break;
405                         }
406
407                         case SDL_JOYAXISMOTION: {
408                                 if (e.jaxis.which == joystick_get_id()) {
409                                         joystick_update_axis(e.jaxis.axis, e.jaxis.value);
410                                 }
411
412                                 break;
413                         }
414
415                         case SDL_JOYBUTTONDOWN:
416                         case SDL_JOYBUTTONUP: {
417                                 if (e.jbutton.which == joystick_get_id()) {
418                                         state = (e.jbutton.state == SDL_PRESSED) ? 1 : 0;
419                                         joy_mark_button((int)e.jbutton.button, state);
420                                 }
421
422                                 break;
423                         }
424
425                         case SDL_JOYHATMOTION: {
426                                 if (e.jhat.which == joystick_get_id()) {
427                                         // can only handle one hat
428                                         if (e.jhat.hat == 0) {
429                                                 switch (e.jhat.value) {
430                                                         case SDL_HAT_UP:
431                                                                 button = JOY_HATFORWARD;
432                                                                 state = 1;
433                                                                 break;
434                                                         case SDL_HAT_DOWN:
435                                                                 button = JOY_HATBACK;
436                                                                 state = 1;
437                                                                 break;
438                                                         case SDL_HAT_LEFT:
439                                                                 button = JOY_HATLEFT;
440                                                                 state = 1;
441                                                                 break;
442                                                         case SDL_HAT_RIGHT:
443                                                                 button = JOY_HATRIGHT;
444                                                                 state = 1;
445                                                                 break;
446                                                         default:
447                                                                 // special case - will toggle all hat positions off
448                                                                 button = JOY_HATBACK;
449                                                                 state = 0;
450                                                                 break;
451                                                 }
452
453                                                 joy_mark_button(button, state);
454                                         }
455                                 }
456
457                                 break;
458                         }
459
460                         case SDL_WINDOWEVENT: {
461                                 switch (e.window.event) {
462                                         case SDL_WINDOWEVENT_RESIZED:
463                                                 gr_set_viewport(e.window.data1, e.window.data2);
464                                                 // ungrab mouse, it will be grabbed again if needed
465                                                 mouse_grab(0);
466                                                 break;
467
468                                         case SDL_WINDOWEVENT_FOCUS_LOST:
469                                                 fAppActive = 0;
470                                                 // io stuff
471                                                 mouse_grab(0);
472                                                 joy_unacquire_ff();
473                                                 break;
474
475                                         case SDL_WINDOWEVENT_FOCUS_GAINED:
476                                                 fAppActive = 1;
477                                                 // io stuff
478                                                 joy_reacquire_ff();
479                                                 break;
480
481                                         case SDL_WINDOWEVENT_MINIMIZED:
482                                                 fAppActive = 0;
483                                                 // io stuff
484                                                 mouse_grab(0);
485                                                 joy_unacquire_ff();
486                                                 // make sure game pauses
487                                                 game_process_pause_key();
488                                                 // graphics
489                                                 gr_activate(fAppActive);
490                                                 break;
491
492                                         case SDL_WINDOWEVENT_MAXIMIZED:
493                                         case SDL_WINDOWEVENT_RESTORED: {
494                                                 fAppActive = 1;
495                                                 // io stuff
496                                                 mouse_grab(0);
497                                                 joy_reacquire_ff();
498                                                 // graphics
499                                                 gr_activate(fAppActive);
500                                                 break;
501                                         }
502
503                                         case SDL_WINDOWEVENT_CLOSE:
504                                                 gameseq_post_event(GS_EVENT_QUIT_GAME);
505                                                 break;
506                                 }
507
508                                 break;
509                         }
510
511                         case SDL_QUIT:
512                                 gameseq_post_event(GS_EVENT_QUIT_GAME);
513                                 break;
514
515                         default:
516                                 break;
517                 }
518         }
519 }
520
521 void debug_int3()
522 {
523         SDL_bool mode = SDL_GetRelativeMouseMode();
524         SDL_SetRelativeMouseMode(SDL_FALSE);
525         SDL_bool grab = SDL_GetWindowGrab(Os_window);
526         SDL_SetWindowGrab(Os_window, SDL_FALSE);
527
528         SDL_TriggerBreakpoint();
529
530         SDL_SetRelativeMouseMode(mode);
531         SDL_SetWindowGrab(Os_window, grab);
532 }