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.17 2004/12/15 04:10:45 taylor
19 * outwnd_unix.cpp from fs2_open for logging to file in debug mode
20 * fixes for default function values
21 * always use vm_* functions for sanity sake
22 * make cfilearchiver 64-bit compatible
23 * fix crash on exit from double free()
24 * fix crash on startup from extra long GL extension string in debug
26 * Revision 1.16 2003/12/15 06:24:51 theoddone33
27 * Bumpy ride... hang on.
29 * Revision 1.15 2003/08/03 15:56:59 taylor
30 * simpler mouse usage; default ini settings in os_init(); cleanup
32 * Revision 1.14 2003/05/09 05:04:15 taylor
33 * better window min/max/focus support
35 * Revision 1.13 2003/05/04 04:56:53 taylor
36 * move SDL_Quit to os_deinit to fix fonttool segfault
38 * Revision 1.12 2003/02/20 17:41:07 theoddone33
39 * Userdir patch from Taylor Richards
41 * Revision 1.11 2002/07/28 21:45:30 theoddone33
42 * Add ctrl-z to iconify window
44 * Revision 1.10 2002/07/28 21:39:44 theoddone33
45 * Add alt-enter to toggle fullscreen and ctrl-g to toggle mouse grabbing
47 * Revision 1.9 2002/06/16 23:59:31 relnev
48 * untested joystick code
50 * Revision 1.8 2002/06/09 04:41:25 relnev
51 * added copyright header
53 * Revision 1.7 2002/06/05 04:03:32 relnev
54 * finished cfilesystem.
56 * removed some old code.
58 * fixed mouse save off-by-one.
62 * Revision 1.6 2002/05/31 03:34:02 theoddone33
66 * Revision 1.5 2002/05/30 23:46:29 theoddone33
67 * some minor key changes (not necessarily fixes)
69 * Revision 1.4 2002/05/30 16:50:24 theoddone33
70 * Keyboard partially fixed
72 * Revision 1.3 2002/05/29 06:25:13 theoddone33
73 * Keyboard input, mouse tracking now work
75 * Revision 1.2 2002/05/07 03:16:48 theoddone33
76 * The Great Newline Fix
78 * Revision 1.1.1.1 2002/05/03 03:28:10 root
82 * 7 6/30/99 5:53p Dave
83 * Put in new anti-camper code.
85 * 6 6/03/99 6:37p Dave
86 * More TNT fun. Made perspective bitmaps more flexible.
88 * 5 6/02/99 6:18p Dave
89 * Fixed TNT lockup problems! Wheeeee!
91 * 4 12/18/98 1:13a Dave
92 * Rough 1024x768 support for Direct3D. Proper detection and usage through
95 * 3 10/09/98 2:57p Dave
96 * Starting splitting up OS stuff.
98 * 2 10/08/98 2:38p Dave
99 * Cleanup up OsAPI code significantly. Removed old functions, centralized
100 * registry functions.
102 * 118 7/10/98 5:04p Dave
103 * Fix connection speed bug on standalone server.
105 * 117 5/24/98 2:28p Hoffoss
106 * Because we never really care about if the left or the right shift or
107 * alt key was used, but rather than either shift or alt was used, made
108 * both map to the left one. Solves some problems, causes none.
110 * 116 5/18/98 9:22p John
111 * Took out the annoying tab check.
113 * 115 5/18/98 11:17a John
114 * Fixed some bugs with software window and output window.
116 * 114 5/16/98 2:20p John
117 * Changed the os_suspend and resume to use a critical section to prevent
118 * threads from executing rather than just suspending the thread. Had to
119 * make sure gr_close was called before os_close.
121 * 113 5/15/98 4:49p John
123 * 112 5/15/98 3:36p John
124 * Fixed bug with new graphics window code and standalone server. Made
125 * hwndApp not be a global anymore.
127 * 111 5/14/98 5:42p John
128 * Revamped the whole window position/mouse code for the graphics windows.
130 * 110 5/04/98 11:08p Hoffoss
131 * Expanded on Force Feedback code, and moved it all into Joy_ff.cpp.
132 * Updated references everywhere to it.
146 #include "freespaceresource.h"
147 #include "managepilot.h"
150 #include "gamesequence.h"
151 #include "freespace.h"
152 #include "osregistry.h"
155 // ----------------------------------------------------------------------------------------------------
156 // OSAPI DEFINES/VARS
160 static int fAppActive = 1;
161 //static int main_window_inited = 0; // not used (here)
162 //static char szWinTitle[128]; // not used (here)
163 //static char szWinClass[128]; // not used (here)
164 //static int WinX, WinY, WinW, WinH; // not used (grsoft.cpp) in UNIX build
165 static int Os_inited = 0;
167 static CRITICAL_SECTION Os_lock;
169 int Os_debugger_running = 0;
171 // ----------------------------------------------------------------------------------------------------
172 // OSAPI FORWARD DECLARATIONS
176 // thread handler for the main message thread
177 DWORD win32_process(DWORD lparam);
179 DWORD win32_process1(DWORD lparam);
180 DWORD win32_process1(DWORD lparam);
183 // Fills in the Os_debugger_running with non-zero if debugger detected.
184 void os_check_debugger();
186 // called at shutdown. Makes sure all thread processing terminates.
190 // ----------------------------------------------------------------------------------------------------
194 // initialization/shutdown functions -----------------------------------------------
196 // detect users home directory
197 const char *detect_home(void)
199 return (getenv("HOME"));
202 // If app_name is NULL or ommited, then TITLE is used
203 // for the app name, which is where registry keys are stored.
204 void os_init(char * wclass, char * title, char *app_name, char *version_string )
210 /* set some sane defaults since we don't have a laucher... */
211 if (os_config_read_string(NULL, NOX("Videocard"), NULL) == NULL)
212 os_config_write_string(NULL, NOX("Videocard"), NOX("OpenGL (640x480)"));
214 if (os_config_read_string(NULL, NOX("NetworkConnection"), NULL) == NULL)
215 os_config_write_string(NULL, NOX("NetworkConnection"), NOX("lan"));
217 if (os_config_read_string(NULL, NOX("ConnectionSpeed"), NULL) == NULL)
218 os_config_write_string(NULL, NOX("ConnectionSpeed"), NOX("Slow"));
222 Os_lock = SDL_CreateMutex();
224 // check to see if we're running under msdev
230 // set the main window title
231 void os_set_title( char * title )
233 // the title is already set by SDL in gropengl.cpp
236 // call at program end
247 // window management -----------------------------------------------------------------
249 // Returns 0 if app is not the foreground app.
255 // Returns the handle to the main window
258 // STUB_FUNCTION; // not used/needed with UNIX builds?
263 // process management -----------------------------------------------------------------
265 // Sleeps for n milliseconds or until app becomes active.
266 void os_sleep(int ms)
271 // Used to stop message processing
274 ENTER_CRITICAL_SECTION(&Os_lock);
277 // resume message processing
280 LEAVE_CRITICAL_SECTION(&Os_lock);
284 // ----------------------------------------------------------------------------------------------------
285 // OSAPI FORWARD DECLARATIONS
288 // Fills in the Os_debugger_running with non-zero if debugger detected.
289 void os_check_debugger()
293 // called at shutdown. Makes sure all thread processing terminates.
296 SDL_DestroyMutex(Os_lock);
301 extern int SDLtoFS2[SDLK_LAST];
306 while (SDL_PollEvent (&e)) {
308 case SDL_MOUSEBUTTONDOWN:
309 case SDL_MOUSEBUTTONUP:
310 if (e.button.button <= HIGHEST_MOUSE_BUTTON)
311 mouse_mark_button(e.button.button, e.button.state);
314 if ((e.key.keysym.mod & KMOD_ALT) &&
315 (e.key.keysym.sym == SDLK_RETURN))
317 if (!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN))
318 SDL_WM_GrabInput (SDL_GRAB_ON);
319 SDL_WM_ToggleFullScreen (SDL_GetVideoSurface());
322 if ((e.key.keysym.mod & KMOD_CTRL) &&
323 (e.key.keysym.sym == SDLK_g))
325 /* DDOI - ignore grab changes when fullscreen */
326 if (!(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN))
328 if (SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON)
329 SDL_WM_GrabInput (SDL_GRAB_OFF);
331 SDL_WM_GrabInput (SDL_GRAB_ON);
335 if ((e.key.keysym.mod & KMOD_CTRL) &&
336 (e.key.keysym.sym == SDLK_z))
338 SDL_WM_IconifyWindow();
342 if (SDLtoFS2[e.key.keysym.sym])
343 key_mark (SDLtoFS2[e.key.keysym.sym], 1, 0);
346 if (SDLtoFS2[e.key.keysym.sym])
347 key_mark (SDLtoFS2[e.key.keysym.sym], 0, 0);
349 case SDL_ACTIVEEVENT:
350 if (e.active.state & SDL_APPACTIVE) {
351 fAppActive = e.active.gain;
352 gr_activate(fAppActive);
354 if (e.active.state & SDL_APPINPUTFOCUS) {
355 gr_activate(e.active.gain);
363 extern void joy_read();