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.13 2003/05/04 04:56:53 taylor
19 * move SDL_Quit to os_deinit to fix fonttool segfault
21 * Revision 1.12 2003/02/20 17:41:07 theoddone33
22 * Userdir patch from Taylor Richards
24 * Revision 1.11 2002/07/28 21:45:30 theoddone33
25 * Add ctrl-z to iconify window
27 * Revision 1.10 2002/07/28 21:39:44 theoddone33
28 * Add alt-enter to toggle fullscreen and ctrl-g to toggle mouse grabbing
30 * Revision 1.9 2002/06/16 23:59:31 relnev
31 * untested joystick code
33 * Revision 1.8 2002/06/09 04:41:25 relnev
34 * added copyright header
36 * Revision 1.7 2002/06/05 04:03:32 relnev
37 * finished cfilesystem.
39 * removed some old code.
41 * fixed mouse save off-by-one.
45 * Revision 1.6 2002/05/31 03:34:02 theoddone33
49 * Revision 1.5 2002/05/30 23:46:29 theoddone33
50 * some minor key changes (not necessarily fixes)
52 * Revision 1.4 2002/05/30 16:50:24 theoddone33
53 * Keyboard partially fixed
55 * Revision 1.3 2002/05/29 06:25:13 theoddone33
56 * Keyboard input, mouse tracking now work
58 * Revision 1.2 2002/05/07 03:16:48 theoddone33
59 * The Great Newline Fix
61 * Revision 1.1.1.1 2002/05/03 03:28:10 root
65 * 7 6/30/99 5:53p Dave
66 * Put in new anti-camper code.
68 * 6 6/03/99 6:37p Dave
69 * More TNT fun. Made perspective bitmaps more flexible.
71 * 5 6/02/99 6:18p Dave
72 * Fixed TNT lockup problems! Wheeeee!
74 * 4 12/18/98 1:13a Dave
75 * Rough 1024x768 support for Direct3D. Proper detection and usage through
78 * 3 10/09/98 2:57p Dave
79 * Starting splitting up OS stuff.
81 * 2 10/08/98 2:38p Dave
82 * Cleanup up OsAPI code significantly. Removed old functions, centralized
85 * 118 7/10/98 5:04p Dave
86 * Fix connection speed bug on standalone server.
88 * 117 5/24/98 2:28p Hoffoss
89 * Because we never really care about if the left or the right shift or
90 * alt key was used, but rather than either shift or alt was used, made
91 * both map to the left one. Solves some problems, causes none.
93 * 116 5/18/98 9:22p John
94 * Took out the annoying tab check.
96 * 115 5/18/98 11:17a John
97 * Fixed some bugs with software window and output window.
99 * 114 5/16/98 2:20p John
100 * Changed the os_suspend and resume to use a critical section to prevent
101 * threads from executing rather than just suspending the thread. Had to
102 * make sure gr_close was called before os_close.
104 * 113 5/15/98 4:49p John
106 * 112 5/15/98 3:36p John
107 * Fixed bug with new graphics window code and standalone server. Made
108 * hwndApp not be a global anymore.
110 * 111 5/14/98 5:42p John
111 * Revamped the whole window position/mouse code for the graphics windows.
113 * 110 5/04/98 11:08p Hoffoss
114 * Expanded on Force Feedback code, and moved it all into Joy_ff.cpp.
115 * Updated references everywhere to it.
129 #include "freespaceresource.h"
130 #include "managepilot.h"
133 #include "gamesequence.h"
134 #include "freespace.h"
135 #include "osregistry.h"
138 // ----------------------------------------------------------------------------------------------------
139 // OSAPI DEFINES/VARS
143 static int fAppActive = 0;
144 static int main_window_inited = 0;
145 static char szWinTitle[128];
146 static char szWinClass[128];
147 static int WinX, WinY, WinW, WinH;
148 static int Os_inited = 0;
150 static CRITICAL_SECTION Os_lock;
152 int Os_debugger_running = 0;
154 // ----------------------------------------------------------------------------------------------------
155 // OSAPI FORWARD DECLARATIONS
159 // thread handler for the main message thread
160 DWORD win32_process(DWORD lparam);
162 DWORD win32_process1(DWORD lparam);
163 DWORD win32_process1(DWORD lparam);
166 // Fills in the Os_debugger_running with non-zero if debugger detected.
167 void os_check_debugger();
169 // called at shutdown. Makes sure all thread processing terminates.
173 // ----------------------------------------------------------------------------------------------------
177 // initialization/shutdown functions -----------------------------------------------
179 // detect users home directory
180 const char *detect_home(void)
182 return (getenv("HOME"));
185 void default_registry()
187 /* set some sane defaults since we don't have a laucher... */
188 if (os_config_read_string(NULL, NOX("Videocard"), NULL) == NULL)
189 os_config_write_string(NULL, NOX("Videocard"), NOX("OpenGL (640x480)"));
191 if (os_config_read_string(NULL, NOX("NetworkConnection"), NULL) == NULL)
192 os_config_write_string(NULL, NOX("NetworkConnection"), NOX("lan"));
194 if (os_config_read_string(NULL, NOX("ConnectionSpeed"), NULL) == NULL)
195 os_config_write_string(NULL, NOX("ConnectionSpeed"), NOX("Slow"));
198 // If app_name is NULL or ommited, then TITLE is used
199 // for the app name, which is where registry keys are stored.
200 void os_init(char * wclass, char * title, char *app_name, char *version_string )
206 // check to see if we're running under msdev
212 // set the main window title
213 void os_set_title( char * title )
218 // call at program end
229 // window management -----------------------------------------------------------------
231 static int app_active = 1;
232 // Returns 1 if app is not the foreground app.
238 // Returns the handle to the main window
246 // process management -----------------------------------------------------------------
248 // Sleeps for n milliseconds or until app becomes active.
249 void os_sleep(int ms)
254 // Used to stop message processing
257 ENTER_CRITICAL_SECTION(&Os_lock);
260 // resume message processing
263 LEAVE_CRITICAL_SECTION(&Os_lock);
267 // ----------------------------------------------------------------------------------------------------
268 // OSAPI FORWARD DECLARATIONS
271 // Fills in the Os_debugger_running with non-zero if debugger detected.
272 void os_check_debugger()
276 // called at shutdown. Makes sure all thread processing terminates.
282 extern int SDLtoFS2[SDLK_LAST];
287 while (SDL_PollEvent (&e)) {
289 case SDL_MOUSEBUTTONDOWN:
290 if (e.button.button == SDL_BUTTON_LEFT)
291 mouse_mark_button (MOUSE_LEFT_BUTTON,1);
292 else if (e.button.button == SDL_BUTTON_RIGHT)
293 mouse_mark_button (MOUSE_RIGHT_BUTTON,1);
294 else if (e.button.button == SDL_BUTTON_MIDDLE)
295 mouse_mark_button (MOUSE_MIDDLE_BUTTON, 1);
297 case SDL_MOUSEBUTTONUP:
298 if (e.button.button == SDL_BUTTON_LEFT)
299 mouse_mark_button (MOUSE_LEFT_BUTTON,0);
300 else if (e.button.button == SDL_BUTTON_RIGHT)
301 mouse_mark_button (MOUSE_RIGHT_BUTTON,0);
302 else if (e.button.button == SDL_BUTTON_MIDDLE)
303 mouse_mark_button (MOUSE_MIDDLE_BUTTON, 0);
306 if ((e.key.keysym.mod & KMOD_ALT) &&
307 (e.key.keysym.sym == SDLK_RETURN))
309 if (!(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN))
310 SDL_WM_GrabInput (SDL_GRAB_ON);
311 SDL_WM_ToggleFullScreen (SDL_GetVideoSurface());
314 if ((e.key.keysym.mod & KMOD_CTRL) &&
315 (e.key.keysym.sym == SDLK_g))
317 /* DDOI - ignore grab changes when fullscreen */
318 if (!(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN))
320 if (SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON)
321 SDL_WM_GrabInput (SDL_GRAB_OFF);
323 SDL_WM_GrabInput (SDL_GRAB_ON);
327 if ((e.key.keysym.mod & KMOD_CTRL) &&
328 (e.key.keysym.sym == SDLK_z))
330 SDL_WM_IconifyWindow();
334 if (SDLtoFS2[e.key.keysym.sym])
335 key_mark (SDLtoFS2[e.key.keysym.sym], 1, 0);
338 if (SDLtoFS2[e.key.keysym.sym])
339 key_mark (SDLtoFS2[e.key.keysym.sym], 0, 0);
341 case SDL_ACTIVEEVENT:
342 if (e.active.state == SDL_APPACTIVE)
343 app_active = e.active.gain;
351 extern int joy_pollrate;
352 extern void joy_process(int time_delta);
354 static Uint32 lasttic = 0;
355 Uint32 curtic = SDL_GetTicks();
356 Uint32 delta = curtic - lasttic;
358 while (delta >= joy_pollrate) {
361 lasttic += joy_pollrate;
363 delta = curtic - lasttic;