]> icculus.org git repositories - taylor/freespace2.git/blob - src/osapi/os_unix.cpp
Bumpy ride... hang on.
[taylor/freespace2.git] / src / osapi / os_unix.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/OsApi/OsApi.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Low level Windows code
16  *
17  * $Log$
18  * Revision 1.16  2003/12/15 06:24:51  theoddone33
19  * Bumpy ride... hang on.
20  *
21  * Revision 1.15  2003/08/03 15:56:59  taylor
22  * simpler mouse usage; default ini settings in os_init(); cleanup
23  *
24  * Revision 1.14  2003/05/09 05:04:15  taylor
25  * better window min/max/focus support
26  *
27  * Revision 1.13  2003/05/04 04:56:53  taylor
28  * move SDL_Quit to os_deinit to fix fonttool segfault
29  *
30  * Revision 1.12  2003/02/20 17:41:07  theoddone33
31  * Userdir patch from Taylor Richards
32  *
33  * Revision 1.11  2002/07/28 21:45:30  theoddone33
34  * Add ctrl-z to iconify window
35  *
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
38  *
39  * Revision 1.9  2002/06/16 23:59:31  relnev
40  * untested joystick code
41  *
42  * Revision 1.8  2002/06/09 04:41:25  relnev
43  * added copyright header
44  *
45  * Revision 1.7  2002/06/05 04:03:32  relnev
46  * finished cfilesystem.
47  *
48  * removed some old code.
49  *
50  * fixed mouse save off-by-one.
51  *
52  * sound cleanups.
53  *
54  * Revision 1.6  2002/05/31 03:34:02  theoddone33
55  * Fix Keyboard
56  * Add titlebar
57  *
58  * Revision 1.5  2002/05/30 23:46:29  theoddone33
59  * some minor key changes (not necessarily fixes)
60  *
61  * Revision 1.4  2002/05/30 16:50:24  theoddone33
62  * Keyboard partially fixed
63  *
64  * Revision 1.3  2002/05/29 06:25:13  theoddone33
65  * Keyboard input, mouse tracking now work
66  *
67  * Revision 1.2  2002/05/07 03:16:48  theoddone33
68  * The Great Newline Fix
69  *
70  * Revision 1.1.1.1  2002/05/03 03:28:10  root
71  * Initial import.
72  * 
73  * 
74  * 7     6/30/99 5:53p Dave
75  * Put in new anti-camper code.
76  * 
77  * 6     6/03/99 6:37p Dave
78  * More TNT fun. Made perspective bitmaps more flexible.
79  * 
80  * 5     6/02/99 6:18p Dave
81  * Fixed TNT lockup problems! Wheeeee!
82  * 
83  * 4     12/18/98 1:13a Dave
84  * Rough 1024x768 support for Direct3D. Proper detection and usage through
85  * the launcher.
86  * 
87  * 3     10/09/98 2:57p Dave
88  * Starting splitting up OS stuff.
89  * 
90  * 2     10/08/98 2:38p Dave
91  * Cleanup up OsAPI code significantly. Removed old functions, centralized
92  * registry functions.
93  * 
94  * 118   7/10/98 5:04p Dave
95  * Fix connection speed bug on standalone server.
96  * 
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.
101  * 
102  * 116   5/18/98 9:22p John
103  * Took out the annoying tab check.
104  * 
105  * 115   5/18/98 11:17a John
106  * Fixed some bugs with software window and output window.
107  * 
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.
112  * 
113  * 113   5/15/98 4:49p John
114  * 
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.
118  * 
119  * 111   5/14/98 5:42p John
120  * Revamped the whole window position/mouse code for the graphics windows.
121  * 
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.
125  *
126  * $NoKeywords: $
127  */
128
129 #include "pstypes.h"
130 #include "osapi.h"
131 #include "key.h"
132 #include "palman.h"
133 #include "mouse.h"
134 #include "outwnd.h"
135 #include "2d.h"
136 #include "cfile.h"
137 #include "sound.h"
138 #include "freespaceresource.h"
139 #include "managepilot.h"
140 #include "joy.h"
141 #include "joy_ff.h"
142 #include "gamesequence.h"
143 #include "freespace.h"
144 #include "osregistry.h"
145 #include "cmdline.h"
146
147 // ----------------------------------------------------------------------------------------------------
148 // OSAPI DEFINES/VARS
149 //
150
151 // os-wide globals
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;
158
159 static CRITICAL_SECTION Os_lock;
160
161 int Os_debugger_running = 0;
162
163 // ----------------------------------------------------------------------------------------------------
164 // OSAPI FORWARD DECLARATIONS
165 //
166
167 #ifdef THREADED
168         // thread handler for the main message thread
169         DWORD win32_process(DWORD lparam);
170 #else
171         DWORD win32_process1(DWORD lparam);
172         DWORD win32_process1(DWORD lparam);
173 #endif
174
175 // Fills in the Os_debugger_running with non-zero if debugger detected.
176 void os_check_debugger();
177
178 // called at shutdown. Makes sure all thread processing terminates.
179 void os_deinit();
180
181
182 // ----------------------------------------------------------------------------------------------------
183 // OSAPI FUNCTIONS
184 //
185
186 // initialization/shutdown functions -----------------------------------------------
187
188 // detect users home directory
189 const char *detect_home(void)
190 {
191         return (getenv("HOME"));
192 }
193
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 )
197 {
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)"));
201         
202         if (os_config_read_string(NULL, NOX("NetworkConnection"), NULL) == NULL)
203                 os_config_write_string(NULL, NOX("NetworkConnection"), NOX("lan"));
204         
205         if (os_config_read_string(NULL, NOX("ConnectionSpeed"), NULL) == NULL)
206                 os_config_write_string(NULL, NOX("ConnectionSpeed"), NOX("Slow"));
207
208         Os_inited = 1;
209
210         Os_lock = SDL_CreateMutex();
211
212         // check to see if we're running under msdev
213         os_check_debugger();
214
215         atexit(os_deinit);
216 }
217
218 // set the main window title
219 void os_set_title( char * title )
220 {
221         // the title is already set by SDL in gropengl.cpp
222 }
223
224 // call at program end
225 void os_cleanup()
226 {
227         STUB_FUNCTION;
228         
229 #ifndef NDEBUG
230                 outwnd_close();
231 #endif
232 }
233
234
235 // window management -----------------------------------------------------------------
236
237 // Returns 0 if app is not the foreground app.
238 int os_foreground()
239 {
240         return fAppActive;
241 }
242
243 // Returns the handle to the main window
244 uint os_get_window()
245 {
246 //      STUB_FUNCTION;  // not used/needed with UNIX builds?
247         return 0;
248 }
249
250
251 // process management -----------------------------------------------------------------
252
253 // Sleeps for n milliseconds or until app becomes active.
254 void os_sleep(int ms)
255 {
256         usleep(ms*1000);
257 }
258
259 // Used to stop message processing
260 void os_suspend()
261 {
262         ENTER_CRITICAL_SECTION(&Os_lock);       
263 }
264
265 // resume message processing
266 void os_resume()
267 {
268         LEAVE_CRITICAL_SECTION(&Os_lock);       
269 }
270
271
272 // ----------------------------------------------------------------------------------------------------
273 // OSAPI FORWARD DECLARATIONS
274 //
275
276 // Fills in the Os_debugger_running with non-zero if debugger detected.
277 void os_check_debugger()
278 {
279 }
280
281 // called at shutdown. Makes sure all thread processing terminates.
282 void os_deinit()
283 {
284         SDL_DestroyMutex(Os_lock);
285
286         SDL_Quit();
287 }
288
289 extern int SDLtoFS2[SDLK_LAST];
290 void os_poll()
291 {
292         SDL_Event e;
293
294         while (SDL_PollEvent (&e)) {
295                 switch (e.type) {
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);
300                                 break;
301                         case SDL_KEYDOWN:
302                                 if ((e.key.keysym.mod & KMOD_ALT) &&
303                                     (e.key.keysym.sym == SDLK_RETURN))
304                                 {
305                                         if (!Cmdline_no_grab && !(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN))
306                                                 SDL_WM_GrabInput (SDL_GRAB_ON);
307                                         SDL_WM_ToggleFullScreen (SDL_GetVideoSurface());
308                                         break;
309                                 }
310                                 if ((e.key.keysym.mod & KMOD_CTRL) &&
311                                     (e.key.keysym.sym == SDLK_g))
312                                 {
313                                         /* DDOI - ignore grab changes when fullscreen */
314                                         if (!(SDL_GetVideoSurface()->flags & SDL_FULLSCREEN))
315                                         {
316                                                 if (SDL_WM_GrabInput(SDL_GRAB_QUERY)==SDL_GRAB_ON)
317                                                         SDL_WM_GrabInput (SDL_GRAB_OFF);
318                                                 else
319                                                         SDL_WM_GrabInput (SDL_GRAB_ON);
320                                         }
321                                         break;
322                                 }
323                                 if ((e.key.keysym.mod & KMOD_CTRL) &&
324                                     (e.key.keysym.sym == SDLK_z))
325                                 {
326                                         SDL_WM_IconifyWindow();
327                                         break;
328                                 }
329
330                                 if (SDLtoFS2[e.key.keysym.sym])
331                                         key_mark (SDLtoFS2[e.key.keysym.sym], 1, 0);
332                                 break;
333                         case SDL_KEYUP:
334                                 if (SDLtoFS2[e.key.keysym.sym])
335                                         key_mark (SDLtoFS2[e.key.keysym.sym], 0, 0);
336                                 break;
337                         case SDL_ACTIVEEVENT:
338                                 if (e.active.state & SDL_APPACTIVE) {
339                                         fAppActive = e.active.gain;
340                                         gr_activate(fAppActive);
341                                 }
342                                 if (e.active.state & SDL_APPINPUTFOCUS) {
343                                         gr_activate(e.active.gain);
344                                 }
345                                 break;
346                         default:
347                                 break;
348                 }
349         }
350         
351     extern void joy_read();
352     joy_read();
353 }
354
355 void debug_int3()
356 {
357         STUB_FUNCTION;
358 }