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