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.15 2003/08/03 15:56:59 taylor
19 * simpler mouse usage; default ini settings in os_init(); cleanup
21 * Revision 1.14 2003/05/09 05:04:15 taylor
22 * better window min/max/focus support
24 * Revision 1.13 2003/05/04 04:56:53 taylor
25 * move SDL_Quit to os_deinit to fix fonttool segfault
27 * Revision 1.12 2003/02/20 17:41:07 theoddone33
28 * Userdir patch from Taylor Richards
30 * Revision 1.11 2002/07/28 21:45:30 theoddone33
31 * Add ctrl-z to iconify window
33 * Revision 1.10 2002/07/28 21:39:44 theoddone33
34 * Add alt-enter to toggle fullscreen and ctrl-g to toggle mouse grabbing
36 * Revision 1.9 2002/06/16 23:59:31 relnev
37 * untested joystick code
39 * Revision 1.8 2002/06/09 04:41:25 relnev
40 * added copyright header
42 * Revision 1.7 2002/06/05 04:03:32 relnev
43 * finished cfilesystem.
45 * removed some old code.
47 * fixed mouse save off-by-one.
51 * Revision 1.6 2002/05/31 03:34:02 theoddone33
55 * Revision 1.5 2002/05/30 23:46:29 theoddone33
56 * some minor key changes (not necessarily fixes)
58 * Revision 1.4 2002/05/30 16:50:24 theoddone33
59 * Keyboard partially fixed
61 * Revision 1.3 2002/05/29 06:25:13 theoddone33
62 * Keyboard input, mouse tracking now work
64 * Revision 1.2 2002/05/07 03:16:48 theoddone33
65 * The Great Newline Fix
67 * Revision 1.1.1.1 2002/05/03 03:28:10 root
71 * 7 6/30/99 5:53p Dave
72 * Put in new anti-camper code.
74 * 6 6/03/99 6:37p Dave
75 * More TNT fun. Made perspective bitmaps more flexible.
77 * 5 6/02/99 6:18p Dave
78 * Fixed TNT lockup problems! Wheeeee!
80 * 4 12/18/98 1:13a Dave
81 * Rough 1024x768 support for Direct3D. Proper detection and usage through
84 * 3 10/09/98 2:57p Dave
85 * Starting splitting up OS stuff.
87 * 2 10/08/98 2:38p Dave
88 * Cleanup up OsAPI code significantly. Removed old functions, centralized
91 * 118 7/10/98 5:04p Dave
92 * Fix connection speed bug on standalone server.
94 * 117 5/24/98 2:28p Hoffoss
95 * Because we never really care about if the left or the right shift or
96 * alt key was used, but rather than either shift or alt was used, made
97 * both map to the left one. Solves some problems, causes none.
99 * 116 5/18/98 9:22p John
100 * Took out the annoying tab check.
102 * 115 5/18/98 11:17a John
103 * Fixed some bugs with software window and output window.
105 * 114 5/16/98 2:20p John
106 * Changed the os_suspend and resume to use a critical section to prevent
107 * threads from executing rather than just suspending the thread. Had to
108 * make sure gr_close was called before os_close.
110 * 113 5/15/98 4:49p John
112 * 112 5/15/98 3:36p John
113 * Fixed bug with new graphics window code and standalone server. Made
114 * hwndApp not be a global anymore.
116 * 111 5/14/98 5:42p John
117 * Revamped the whole window position/mouse code for the graphics windows.
119 * 110 5/04/98 11:08p Hoffoss
120 * Expanded on Force Feedback code, and moved it all into Joy_ff.cpp.
121 * Updated references everywhere to it.
135 #include "freespaceresource.h"
136 #include "managepilot.h"
139 #include "gamesequence.h"
140 #include "freespace.h"
141 #include "osregistry.h"
144 // ----------------------------------------------------------------------------------------------------
145 // OSAPI DEFINES/VARS
149 static int fAppActive = 1;
150 //static int main_window_inited = 0; // not used (here)
151 //static char szWinTitle[128]; // not used (here)
152 //static char szWinClass[128]; // not used (here)
153 //static int WinX, WinY, WinW, WinH; // not used (grsoft.cpp) in UNIX build
154 static int Os_inited = 0;
156 static CRITICAL_SECTION Os_lock;
158 int Os_debugger_running = 0;
160 // ----------------------------------------------------------------------------------------------------
161 // OSAPI FORWARD DECLARATIONS
165 // thread handler for the main message thread
166 DWORD win32_process(DWORD lparam);
168 DWORD win32_process1(DWORD lparam);
169 DWORD win32_process1(DWORD lparam);
172 // Fills in the Os_debugger_running with non-zero if debugger detected.
173 void os_check_debugger();
175 // called at shutdown. Makes sure all thread processing terminates.
179 // ----------------------------------------------------------------------------------------------------
183 // initialization/shutdown functions -----------------------------------------------
185 // detect users home directory
186 const char *detect_home(void)
188 return (getenv("HOME"));
191 // If app_name is NULL or ommited, then TITLE is used
192 // for the app name, which is where registry keys are stored.
193 void os_init(char * wclass, char * title, char *app_name, char *version_string )
195 /* set some sane defaults since we don't have a laucher... */
196 if (os_config_read_string(NULL, NOX("Videocard"), NULL) == NULL)
197 os_config_write_string(NULL, NOX("Videocard"), NOX("OpenGL (640x480)"));
199 if (os_config_read_string(NULL, NOX("NetworkConnection"), NULL) == NULL)
200 os_config_write_string(NULL, NOX("NetworkConnection"), NOX("lan"));
202 if (os_config_read_string(NULL, NOX("ConnectionSpeed"), NULL) == NULL)
203 os_config_write_string(NULL, NOX("ConnectionSpeed"), NOX("Slow"));
207 Os_lock = SDL_CreateMutex();
209 // check to see if we're running under msdev
215 // set the main window title
216 void os_set_title( char * title )
218 // the title is already set by SDL in gropengl.cpp
221 // call at program end
232 // window management -----------------------------------------------------------------
234 // Returns 0 if app is not the foreground app.
240 // Returns the handle to the main window
243 // STUB_FUNCTION; // not used/needed with UNIX builds?
248 // process management -----------------------------------------------------------------
250 // Sleeps for n milliseconds or until app becomes active.
251 void os_sleep(int ms)
256 // Used to stop message processing
259 ENTER_CRITICAL_SECTION(&Os_lock);
262 // resume message processing
265 LEAVE_CRITICAL_SECTION(&Os_lock);
269 // ----------------------------------------------------------------------------------------------------
270 // OSAPI FORWARD DECLARATIONS
273 // Fills in the Os_debugger_running with non-zero if debugger detected.
274 void os_check_debugger()
278 // called at shutdown. Makes sure all thread processing terminates.
281 SDL_DestroyMutex(Os_lock);
286 extern int SDLtoFS2[SDLK_LAST];
291 while (SDL_PollEvent (&e)) {
293 case SDL_MOUSEBUTTONDOWN:
294 case SDL_MOUSEBUTTONUP:
295 if (e.button.button <= HIGHEST_MOUSE_BUTTON)
296 mouse_mark_button(e.button.button, e.button.state);
299 if ((e.key.keysym.mod & KMOD_ALT) &&
300 (e.key.keysym.sym == SDLK_RETURN))
302 if (!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN))
303 SDL_WM_GrabInput (SDL_GRAB_ON);
304 SDL_WM_ToggleFullScreen (SDL_GetVideoSurface());
307 if ((e.key.keysym.mod & KMOD_CTRL) &&
308 (e.key.keysym.sym == SDLK_g))
310 /* DDOI - ignore grab changes when fullscreen */
311 if (!(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN))
313 if (SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON)
314 SDL_WM_GrabInput (SDL_GRAB_OFF);
316 SDL_WM_GrabInput (SDL_GRAB_ON);
320 if ((e.key.keysym.mod & KMOD_CTRL) &&
321 (e.key.keysym.sym == SDLK_z))
323 SDL_WM_IconifyWindow();
327 if (SDLtoFS2[e.key.keysym.sym])
328 key_mark (SDLtoFS2[e.key.keysym.sym], 1, 0);
331 if (SDLtoFS2[e.key.keysym.sym])
332 key_mark (SDLtoFS2[e.key.keysym.sym], 0, 0);
334 case SDL_ACTIVEEVENT:
335 if (e.active.state & SDL_APPACTIVE) {
336 fAppActive = e.active.gain;
337 gr_activate(fAppActive);
339 if (e.active.state & SDL_APPINPUTFOCUS) {
340 gr_activate(e.active.gain);
349 extern int joy_pollrate;
350 extern void joy_process(int time_delta);
352 static Uint32 lasttic = 0;
353 Uint32 curtic = SDL_GetTicks();
354 Uint32 delta = curtic - lasttic;
356 while (delta >= (uint)joy_pollrate) {
359 lasttic += joy_pollrate;
361 delta = curtic - lasttic;