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