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