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