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