]> icculus.org git repositories - taylor/freespace2.git/blob - src/io/joy-unix.cpp
fixed demo dogfight multiplayer mission
[taylor/freespace2.git] / src / io / joy-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 #include "pstypes.h"
10 #include "joy.h"
11 #include "fix.h"
12 #include "key.h"
13 #include "timer.h"
14 #include "osregistry.h"
15 #include "joy_ff.h"
16 #include "osapi.h"
17
18 static int Joy_inited = 0;
19 int joy_num_sticks = 0;
20 int Dead_zone_size = 10;
21 int Cur_joystick = -1;
22 int Joy_sensitivity = 9;
23
24 int joy_pollrate = 1000 / 18;  // poll at 18Hz
25
26 static int Joy_last_x_reading = 0;
27 static int Joy_last_y_reading = 0;
28
29 typedef struct joy_button_info {
30         int     actual_state;           // Set if the button is physically down
31         int     state;                          // Set when the button goes from up to down, cleared on down to up.  Different than actual_state after a flush.
32         int     down_count;
33         int     up_count;
34         int     down_time;
35         uint    last_down_check;        // timestamp in milliseconds of last 
36 } joy_button_info;
37
38 Joy_info joystick;
39
40 int JOYSTICKID1 = 0;    // DDOI - temporary
41 static SDL_Joystick *sdljoy;
42
43 joy_button_info joy_buttons[JOY_TOTAL_BUTTONS];
44
45
46 void joy_close()
47 {
48         if (!Joy_inited)
49                 return;
50
51         Joy_inited = 0;
52         joy_num_sticks = 0;
53
54         SDL_QuitSubSystem (SDL_INIT_JOYSTICK);
55 }
56
57 void joy_get_caps (int max)
58 {
59         SDL_Joystick *joy;
60         int j;
61
62         for (j=0; j < JOY_NUM_AXES; j++)
63                 joystick.axis_valid[j] = 0;
64
65         for (j=JOYSTICKID1; j<JOYSTICKID1+max; j++) {
66                 joy = SDL_JoystickOpen (j);
67                 if (joy)
68                 {
69                         nprintf (("JOYSTICK", "Joystick #%d: %s\n", j - JOYSTICKID1 + 1, SDL_JoystickName(j)));
70                         if (j == Cur_joystick) {
71                                 for (int i = 0; i < SDL_JoystickNumAxes(joy); i++)
72                                 {
73                                         joystick.axis_valid[i] = 1;
74                                 }
75                         }
76                         SDL_JoystickClose (joy);
77                 }
78         }
79 }
80
81 int joy_down(int btn)
82 {
83         int tmp;
84
85         if ( joy_num_sticks < 1 ) return 0;
86         if ( (btn < 0) || (btn >= JOY_TOTAL_BUTTONS )) return 0;
87
88         tmp = joy_buttons[btn].state;
89
90         return tmp;
91 }
92
93 int joy_down_count(int btn, int reset_count)
94 {
95         int tmp;
96
97         if ( joy_num_sticks < 1 ) return 0;
98         if ( (btn < 0) || (btn >= JOY_TOTAL_BUTTONS)) return 0;
99
100         tmp = joy_buttons[btn].down_count;
101         if ( reset_count ) {
102                 joy_buttons[btn].down_count = 0;
103         }
104
105         return tmp;
106 }
107
108 float joy_down_time(int btn)
109 {
110         float                           rval;
111         unsigned int    now;
112         joy_button_info         *bi;
113
114         if ( joy_num_sticks < 1 ) return 0.0f;
115         if ( (btn < 0) || (btn >= JOY_TOTAL_BUTTONS)) return 0.0f;
116         bi = &joy_buttons[btn];
117
118         now = timer_get_milliseconds();
119
120         if ( bi->down_time == 0 && joy_down(btn) ) {
121                 bi->down_time += joy_pollrate;
122         }
123
124         if ( (now - bi->last_down_check) > 0)
125                 rval = i2fl(bi->down_time) / (now - bi->last_down_check);
126         else
127                 rval = 0.0f;
128
129         bi->down_time = 0;
130         bi->last_down_check = now;
131
132         if (rval < 0)
133                 rval = 0.0f;
134         if (rval > 1)
135                 rval = 1.0f;
136
137         return rval;
138 }
139
140 void joy_flush()
141 {
142         int                     i;
143         joy_button_info *bi;
144
145         if ( joy_num_sticks < 1 ) return;
146
147         for ( i = 0; i < JOY_TOTAL_BUTTONS; i++) {
148                 bi = &joy_buttons[i];
149                 bi->state               = 0;
150                 bi->down_count  = 0;
151                 bi->up_count    = 0;
152                 bi->down_time   = 0;
153                 bi->last_down_check = timer_get_milliseconds();
154         }
155 }
156
157 int joy_get_pos(int *x, int *y, int *z, int *rx)
158 {
159         int axis[JOY_NUM_AXES];
160         
161         if (x) *x = 0;
162         if (y) *y = 0;
163         if (z) *z = 0;
164         if (rx) *rx = 0;
165         
166         if (joy_num_sticks < 1) return 0;
167
168         joystick_read_raw_axis( 6, axis );
169
170         //      joy_get_scaled_reading will return a value represents the joystick pos from -1 to +1
171         if (x && joystick.axis_valid[0])
172                 *x = joy_get_scaled_reading(axis[0], 0);
173         if (y && joystick.axis_valid[1])
174                 *y = joy_get_scaled_reading(axis[1], 1);
175         if (z && joystick.axis_valid[2])
176                 *z = joy_get_unscaled_reading(axis[2], 2);
177         if (rx && joystick.axis_valid[3])
178                 *rx = joy_get_scaled_reading(axis[3], 3);
179
180         if (x)
181                 Joy_last_x_reading = *x;
182
183         if (y)
184                 Joy_last_x_reading = *y;
185
186         return 1;
187 }
188
189 int joy_get_scaled_reading(int raw, int axn)
190 {
191         STUB_FUNCTION;
192         
193         return 0;
194 }
195
196 int joy_get_unscaled_reading(int raw, int axn)
197 {
198         STUB_FUNCTION;
199         
200         return 0;
201 }
202
203 int joy_init()
204 {
205         int i, n, count;
206
207         if (Joy_inited)
208                 return 0;
209
210         if (SDL_InitSubSystem (SDL_INIT_JOYSTICK)<0)
211         {
212                 mprintf(("Could not initialize joystick\n"));
213                 return 0;
214         }
215
216         Joy_inited = 1;
217         n = SDL_NumJoysticks ();
218
219         // DDOI - FIXME
220         //Cur_joystick = os_config_read_unit (NULL, "CurrentJoystick", JOYSTICKID1);
221         Cur_joystick = 0;
222
223         joy_get_caps(n);
224
225         if (n < 1) {
226                 mprintf(("No joystick driver detected\n"));
227                 return 0;
228         }
229
230         joy_flush ();
231
232         joy_num_sticks = n;
233
234         // Fake a calibration
235         if (joy_num_sticks > 0) {
236                 joy_set_cen();
237                 for (i=0; i<4; i++) {
238                         joystick.axis_min[i] = 0;
239                         joystick.axis_max[i] = joystick.axis_center[i]*2;
240                 }
241         }
242
243         return joy_num_sticks;
244 }
245
246 void joy_set_cen()
247 {
248         joystick_read_raw_axis( 2, joystick.axis_center );
249 }
250
251 int joystick_read_raw_axis(int num_axes, int *axis)
252 {
253         return 0;
254 }
255
256 void joy_ff_adjust_handling(int speed)
257 {
258 //      STUB_FUNCTION;
259 }
260
261 void joy_ff_afterburn_off()
262 {
263 //      STUB_FUNCTION;
264 }
265
266 void joy_ff_afterburn_on()
267 {
268 //      STUB_FUNCTION;
269 }
270
271 void joy_ff_deathroll()
272 {
273 //      STUB_FUNCTION;
274 }
275
276 void joy_ff_docked()
277 {
278 //      STUB_FUNCTION;
279 }
280
281 void joy_ff_explode()
282 {
283 //      STUB_FUNCTION;
284 }
285
286 void joy_ff_fly_by(int mag)
287 {
288 //      STUB_FUNCTION;
289 }
290
291 void joy_ff_mission_init(vector v)
292 {
293 //      STUB_FUNCTION;
294 }
295
296 void joy_ff_play_dir_effect(float x, float y)
297 {
298 //      STUB_FUNCTION;
299 }
300
301 void joy_ff_play_primary_shoot(int gain)
302 {
303 //      STUB_FUNCTION;
304 }
305
306 void joy_ff_play_reload_effect()
307 {
308 //      STUB_FUNCTION;
309 }
310
311 void joy_ff_play_secondary_shoot(int gain)
312 {
313 //      STUB_FUNCTION;
314 }
315
316 void joy_ff_play_vector_effect(vector *v, float scaler)
317 {
318 //      STUB_FUNCTION;
319 }
320
321 void joy_ff_stop_effects()
322 {
323         joy_ff_afterburn_off();
324 }