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