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