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.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
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
29 * Revision 1.16 2003/12/15 06:24:51 theoddone33
30 * Bumpy ride... hang on.
32 * Revision 1.15 2003/08/03 15:56:59 taylor
33 * simpler mouse usage; default ini settings in os_init(); cleanup
35 * Revision 1.14 2003/05/09 05:04:15 taylor
36 * better window min/max/focus support
38 * Revision 1.13 2003/05/04 04:56:53 taylor
39 * move SDL_Quit to os_deinit to fix fonttool segfault
41 * Revision 1.12 2003/02/20 17:41:07 theoddone33
42 * Userdir patch from Taylor Richards
44 * Revision 1.11 2002/07/28 21:45:30 theoddone33
45 * Add ctrl-z to iconify window
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
50 * Revision 1.9 2002/06/16 23:59:31 relnev
51 * untested joystick code
53 * Revision 1.8 2002/06/09 04:41:25 relnev
54 * added copyright header
56 * Revision 1.7 2002/06/05 04:03:32 relnev
57 * finished cfilesystem.
59 * removed some old code.
61 * fixed mouse save off-by-one.
65 * Revision 1.6 2002/05/31 03:34:02 theoddone33
69 * Revision 1.5 2002/05/30 23:46:29 theoddone33
70 * some minor key changes (not necessarily fixes)
72 * Revision 1.4 2002/05/30 16:50:24 theoddone33
73 * Keyboard partially fixed
75 * Revision 1.3 2002/05/29 06:25:13 theoddone33
76 * Keyboard input, mouse tracking now work
78 * Revision 1.2 2002/05/07 03:16:48 theoddone33
79 * The Great Newline Fix
81 * Revision 1.1.1.1 2002/05/03 03:28:10 root
85 * 7 6/30/99 5:53p Dave
86 * Put in new anti-camper code.
88 * 6 6/03/99 6:37p Dave
89 * More TNT fun. Made perspective bitmaps more flexible.
91 * 5 6/02/99 6:18p Dave
92 * Fixed TNT lockup problems! Wheeeee!
94 * 4 12/18/98 1:13a Dave
95 * Rough 1024x768 support for Direct3D. Proper detection and usage through
98 * 3 10/09/98 2:57p Dave
99 * Starting splitting up OS stuff.
101 * 2 10/08/98 2:38p Dave
102 * Cleanup up OsAPI code significantly. Removed old functions, centralized
103 * registry functions.
105 * 118 7/10/98 5:04p Dave
106 * Fix connection speed bug on standalone server.
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.
113 * 116 5/18/98 9:22p John
114 * Took out the annoying tab check.
116 * 115 5/18/98 11:17a John
117 * Fixed some bugs with software window and output window.
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.
124 * 113 5/15/98 4:49p John
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.
130 * 111 5/14/98 5:42p John
131 * Revamped the whole window position/mouse code for the graphics windows.
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.
149 #include "freespaceresource.h"
150 #include "managepilot.h"
153 #include "gamesequence.h"
154 #include "freespace.h"
155 #include "osregistry.h"
158 // ----------------------------------------------------------------------------------------------------
159 // OSAPI DEFINES/VARS
163 static int fAppActive = 1;
164 static int Os_inited = 0;
165 static char windowTitle[128];
167 static SDL_mutex *Os_lock;
169 int Os_debugger_running = 0;
171 // ----------------------------------------------------------------------------------------------------
172 // OSAPI FORWARD DECLARATIONS
175 // Fills in the Os_debugger_running with non-zero if debugger detected.
176 void os_check_debugger();
178 // called at shutdown. Makes sure all thread processing terminates.
182 // ----------------------------------------------------------------------------------------------------
186 // initialization/shutdown functions -----------------------------------------------
188 extern void cf_create_directory( int dir_type );
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)
194 os_set_title( (app_name != NULL) ? app_name : title );
196 // do some first-run stuff if needed
197 if ( os_config_read_uint(NULL, NOX("StraightToSetup"), 1) == 1 ) {
198 // set some sane config defaults
199 os_config_write_string(NULL, NOX("Videocard"), NOX("OpenGL (640x480)"));
200 os_config_write_string(NULL, NOX("NetworkConnection"), NOX("lan"));
201 os_config_write_string(NULL, NOX("ConnectionSpeed"), NOX("Slow"));
203 // in case an installer didn't do it, populate directory structure to
204 // make sure everything is usable
205 cf_create_directory(CF_TYPE_MAPS);
206 cf_create_directory(CF_TYPE_TEXT);
207 cf_create_directory(CF_TYPE_MISSIONS);
208 cf_create_directory(CF_TYPE_MODELS);
209 cf_create_directory(CF_TYPE_TABLES);
210 cf_create_directory(CF_TYPE_SOUNDS_8B22K);
211 cf_create_directory(CF_TYPE_SOUNDS_16B11K);
212 cf_create_directory(CF_TYPE_VOICE_BRIEFINGS);
213 cf_create_directory(CF_TYPE_VOICE_CMD_BRIEF);
214 cf_create_directory(CF_TYPE_VOICE_DEBRIEFINGS);
215 cf_create_directory(CF_TYPE_VOICE_PERSONAS);
216 cf_create_directory(CF_TYPE_VOICE_SPECIAL);
217 cf_create_directory(CF_TYPE_VOICE_TRAINING);
218 cf_create_directory(CF_TYPE_MUSIC);
219 cf_create_directory(CF_TYPE_MOVIES);
220 cf_create_directory(CF_TYPE_INTERFACE);
221 cf_create_directory(CF_TYPE_FONT);
222 cf_create_directory(CF_TYPE_EFFECTS);
223 cf_create_directory(CF_TYPE_HUD);
224 cf_create_directory(CF_TYPE_PLAYER_IMAGES_MAIN);
225 cf_create_directory(CF_TYPE_CACHE);
226 cf_create_directory(CF_TYPE_SINGLE_PLAYERS);
227 cf_create_directory(CF_TYPE_MULTI_PLAYERS);
228 cf_create_directory(CF_TYPE_MULTI_CACHE);
229 cf_create_directory(CF_TYPE_CONFIG);
230 cf_create_directory(CF_TYPE_SQUAD_IMAGES_MAIN);
231 cf_create_directory(CF_TYPE_DEMOS);
232 cf_create_directory(CF_TYPE_CBANIMS);
233 cf_create_directory(CF_TYPE_INTEL_ANIMS);
235 // unset FirstRun flag
236 os_config_write_uint(NULL, NOX("StraightToSetup"), 0);
241 Os_lock = SDL_CreateMutex();
243 // check to see if we're running under msdev
249 // set the main window title
250 void os_set_title( const char *title )
252 extern SDL_Window *GL_window;
258 memset(windowTitle, 0, sizeof(windowTitle));
260 strncpy(windowTitle, title, sizeof(windowTitle)-1);
262 SDL_SetWindowTitle(GL_window, title);
265 const char *os_get_title()
270 // call at program end
279 // window management -----------------------------------------------------------------
281 // Returns 0 if app is not the foreground app.
287 // Returns the handle to the main window
290 // STUB_FUNCTION; // not used/needed with UNIX builds?
295 // process management -----------------------------------------------------------------
297 // Used to stop message processing
300 SDL_LockMutex(Os_lock);
303 // resume message processing
306 SDL_UnlockMutex(Os_lock);
310 // ----------------------------------------------------------------------------------------------------
311 // OSAPI FORWARD DECLARATIONS
314 // Fills in the Os_debugger_running with non-zero if debugger detected.
315 void os_check_debugger()
319 // called at shutdown. Makes sure all thread processing terminates.
322 SDL_DestroyMutex(Os_lock);
332 while (SDL_PollEvent (&e)) {
334 case SDL_MOUSEBUTTONDOWN:
335 case SDL_MOUSEBUTTONUP: {
336 if (e.motion.windowID > 0) {
337 mouse_mark_button(e.button.button, e.button.state);
343 case SDL_MOUSEMOTION: {
344 if (e.motion.windowID > 0) {
345 mouse_update_pos(e.motion.x, e.motion.y, e.motion.xrel, e.motion.yrel);
351 case SDL_TEXTINPUT: {
352 key_set_text_input((int)e.text.text[0]);
358 if (e.key.keysym.mod & KMOD_GUI) {
359 if (e.key.keysym.sym == SDLK_f ) {
360 gr_toggle_fullscreen();
361 // } else if (e.key.keysym.sym == SDLK_z) {
362 // SDL_MinimizeWindow(GL_window);
365 key_mark(e.key.keysym.scancode, 1, e.key.keysym.mod, 0);
372 if (e.key.keysym.mod & KMOD_GUI) {
373 // blank, just don't want to process up keys we skipped
376 key_mark(e.key.keysym.scancode, 0, e.key.keysym.mod, 0);
382 case SDL_ACTIVEEVENT:
383 if (e.active.state & SDL_APPACTIVE) {
384 fAppActive = e.active.gain;
385 gr_activate(fAppActive);
387 if (e.active.state & SDL_APPINPUTFOCUS) {
388 gr_activate(e.active.gain);
392 case SDL_JOYDEVICEADDED: {
393 if ( !Is_standalone ) {
400 case SDL_JOYDEVICEREMOVED: {
401 if (e.jdevice.which == joystick_get_id()) {
408 case SDL_JOYAXISMOTION: {
409 if (e.jaxis.which == joystick_get_id()) {
410 joystick_update_axis(e.jaxis.axis, e.jaxis.value);
416 case SDL_JOYBUTTONDOWN:
417 case SDL_JOYBUTTONUP: {
418 if (e.jbutton.which == joystick_get_id()) {
419 state = (e.jbutton.state == SDL_PRESSED) ? 1 : 0;
420 joy_mark_button((int)e.jbutton.button, state);
426 case SDL_JOYHATMOTION: {
427 if (e.jhat.which == joystick_get_id()) {
428 // can only handle one hat
429 if (e.jhat.hat == 0) {
430 switch (e.jhat.value) {
432 button = JOY_HATFORWARD;
436 button = JOY_HATBACK;
440 button = JOY_HATLEFT;
444 button = JOY_HATRIGHT;
448 // special case - will toggle all hat positions off
449 button = JOY_HATBACK;
454 joy_mark_button(button, state);
461 case SDL_WINDOWEVENT: {
462 switch (e.window.event) {
463 case SDL_WINDOWEVENT_RESIZED:
464 gr_set_viewport(e.window.data1, e.window.data2);
467 case SDL_WINDOWEVENT_FOCUS_LOST:
471 case SDL_WINDOWEVENT_FOCUS_GAINED:
475 case SDL_WINDOWEVENT_CLOSE:
476 // gameseq_post_event(GS_EVENT_QUIT_GAME);
484 gameseq_post_event(GS_EVENT_QUIT_GAME);
495 SDL_TriggerBreakpoint();