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