joystick support
[btb/d2x.git] / input / linux / joystick.c
1 #include <conf.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "joystick.h"
5 #include <sys/ioctl.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 #include "timer.h"
10 #include "pstypes.h"
11 #include "mono.h"
12 #include "joy.h"
13
14 char joy_installed = 0;
15 char joy_present = 0;
16
17 joystick_device j_joystick[4];
18 joystick_axis j_axis[MAX_AXES];
19 joystick_button j_button[MAX_BUTTONS];
20
21 int j_num_axes = 0, j_num_buttons = 0;
22 int timer_rate;
23
24 int j_axes_in_sticks[4];        /* number of axes in the first [x] sticks */
25 int j_buttons_in_sticks[4];     /* number of buttons in the first [x] sticks */
26
27 int joy_deadzone = 0;
28
29 int j_Get_joydev_axis_number (int all_axis_number) {
30         int i, joy_axis_number = all_axis_number;
31
32         for (i = 0; i < j_axis[all_axis_number].joydev; i++) {
33                 joy_axis_number -= j_joystick[i].num_axes;
34         }               
35
36         return joy_axis_number;
37 }
38
39
40 int j_Get_joydev_button_number (int all_button_number) {
41         int i, joy_button_number = all_button_number;
42
43         for (i = 0; i < j_button[all_button_number].joydev; i++) {
44                 joy_button_number -= j_joystick[i].num_buttons;
45         }               
46
47         return joy_button_number;
48 }
49
50
51 int j_Update_state () {
52         int num_processed = 0, i;
53         struct js_event current_event;
54         struct JS_DATA_TYPE joy_data;
55
56         for (i = 0; i < j_num_buttons; i++) {
57                 //changed 6/24/1999 to finally squish the timedown bug - Owen Evans 
58                 if (j_button[i].state != j_button[i].last_state) {
59                         if (j_button[i].state) {
60                                 j_button[i].downcount++;
61                                 j_button[i].timedown = timer_get_fixed_seconds();
62                         }
63                 }
64                 //end changed - OE
65                 j_button[i].last_state = j_button[i].state;
66         }
67
68         for (i = 0; i < 4; i++) {
69                 if (j_joystick[i].buffer >= 0) {
70                         if (j_joystick[i].version) {
71                                 while (read (j_joystick[i].buffer, &current_event, sizeof (struct js_event)) > 0) {
72                                         num_processed++;
73                                         switch (current_event.type & ~JS_EVENT_INIT) {
74                                                 case JS_EVENT_AXIS:
75                                                         j_axis[j_axes_in_sticks[i] + current_event.number].value = current_event.value;
76                                                         break;
77                                                 case JS_EVENT_BUTTON:
78                                                         j_button[j_buttons_in_sticks[i] + current_event.number].state = current_event.value;
79                                                         break;
80                                         }
81                                 }
82                         } else {
83                                 read (j_joystick[i].buffer, &joy_data, JS_RETURN);
84                                 j_axis[j_axes_in_sticks[i] + 0].value = joy_data.x;
85                                 j_axis[j_axes_in_sticks[i] + 1].value = joy_data.y;
86                                 j_button[j_buttons_in_sticks[i] + 0].state = (joy_data.buttons & 0x01);
87                                 j_button[j_buttons_in_sticks[i] + 1].state = (joy_data.buttons & 0x02) >> 1;
88                         }
89                 }
90         }
91
92         return num_processed;
93 }
94
95
96 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
97         int i;
98
99         for (i = 0; i < 4; i++) {
100                 j_axis[i].center_val = axis_center[i];
101                 j_axis[i].min_val = axis_min[i];
102                 j_axis[i].max_val = axis_max[i];
103         }
104 }
105
106
107 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
108         int i;
109
110         //edited 05/18/99 Matt Mueller - we should return all axes instead of j_num_axes, since they are all given to us in joy_set_cal_vals ( and because checker complains :)
111         for (i = 0; i < 4; i++) {
112         //end edit -MM
113                 axis_center[i] = j_axis[i].center_val;
114                 axis_min[i] = j_axis[i].min_val;
115                 axis_max[i] = j_axis[i].max_val;
116         }
117 }
118
119
120 void joy_set_min (int axis_number, int value) {
121         j_axis[axis_number].min_val = value;
122 }
123
124
125 void joy_set_center (int axis_number, int value) {
126         j_axis[axis_number].center_val = value;
127 }
128
129
130 void joy_set_max (int axis_number, int value) {
131         j_axis[axis_number].max_val = value;
132 }
133
134
135 ubyte joy_get_present_mask () {
136         return 1;
137 }
138
139
140 void joy_set_timer_rate (int max_value) {
141         timer_rate = max_value;
142 }
143
144
145 int joy_get_timer_rate () {
146         return timer_rate;
147 }
148
149
150 void joy_flush () {
151         int i;
152
153         if (!joy_installed) return;
154
155         for (i = 0; i < j_num_buttons; i++) {
156                 j_button[i].timedown = 0;       
157                 j_button[i].downcount = 0;      
158         }
159         
160 }
161
162
163 ubyte joystick_read_raw_axis (ubyte mask, int *axes) {
164         int i;
165         
166         j_Update_state();
167
168         for (i = 0; i < j_num_axes; i++) {
169                 axes[i] = j_axis[i].value;
170         }
171
172         return 0;
173 }
174
175
176 /* joy_init () is pretty huge, a bit klunky, and by no means pretty.  But who cares?  It does the job and it's only run once. */
177
178
179 int joy_init () {
180         int i, j;
181
182         if (joy_installed) return 0;
183         joy_flush ();
184
185         if (!joy_installed)     {
186
187                 printf ("Initializing joystick... ");
188
189                 j_joystick[0].buffer = open ("/dev/js0", O_NONBLOCK);
190                 j_joystick[1].buffer = open ("/dev/js1", O_NONBLOCK);
191                 j_joystick[2].buffer = open ("/dev/js2", O_NONBLOCK);
192                 j_joystick[3].buffer = open ("/dev/js3", O_NONBLOCK);
193                 
194                 if (j_joystick[0].buffer >= 0 || j_joystick[1].buffer >= 0 || j_joystick[2].buffer >= 0 || j_joystick[3].buffer >= 0) {
195                         printf ("found: ");
196
197                         for (i = 0; i < 4; i++) {
198                                 if (j_joystick[i].buffer >= 0) {
199                                         ioctl (j_joystick[i].buffer, JSIOCGAXES, &j_joystick[i].num_axes);
200                                         ioctl (j_joystick[i].buffer, JSIOCGBUTTONS, &j_joystick[i].num_buttons);
201                                         ioctl (j_joystick[i].buffer, JSIOCGVERSION, &j_joystick[i].version);
202                                         if (!j_joystick[i].version) {
203                                                 j_joystick[i].num_axes = 2;
204                                                 j_joystick[i].num_buttons = 2;
205                                                 printf ("js%d (v0.x)  " , i);
206                                         } else {
207                                                 printf ("js%d (v%d.%d.%d)  ",
208                                                         i, 
209                                                         (j_joystick[i].version & 0xff0000) >> 16,
210                                                         (j_joystick[i].version & 0xff00) >> 8,
211                                                         j_joystick[i].version & 0xff);
212                                         }                                               
213
214                                         for (j = j_num_axes; j < (j_num_axes + j_joystick[i].num_axes); j++) {
215                                                 j_axis[j].joydev = i;
216                                                 if (j_joystick[i].version) {
217                                                         j_axis[j].center_val = 0;
218                                                         j_axis[j].max_val = 32767;
219                                                         j_axis[j].min_val = -32767;
220                                                 }
221                                         }
222                                         for (j = j_num_buttons; j < (j_num_buttons + j_joystick[i].num_buttons); j++) {
223                                                 j_button[j].joydev = i;
224                                         }
225
226                                         j_num_axes += j_joystick[i].num_axes;
227                                         j_num_buttons += j_joystick[i].num_buttons;
228                                         
229                                 } else {
230                                         j_joystick[i].num_buttons = 0;
231                                         j_joystick[i].num_axes = 0;
232                                 }       
233
234                                 for (j = 0; j < i; j++) {
235                                         j_axes_in_sticks[i] += j_joystick[j].num_axes;
236                                         j_buttons_in_sticks[i] += j_joystick[j].num_buttons;
237                                 }
238                         }
239                 } else {
240                         printf ("no joysticks found\n");
241                         return 0;
242                 }               
243
244                 printf ("\n");
245
246                 if (j_num_axes > MAX_AXES)
247                         j_num_axes = MAX_AXES;
248                 if (j_num_buttons > MAX_BUTTONS)
249                         j_num_buttons = MAX_BUTTONS;
250
251                 joy_present = 1;
252                 joy_installed = 1;
253                 return 1;
254         }
255
256         return 1;
257 }
258
259
260 void joy_close() {
261         int i;
262
263         if (!joy_installed) return;
264
265         for (i = 0; i < 4; i++) {
266                 if (j_joystick[i].buffer>=0) {
267                         printf ("closing js%d\n", i);
268                         close (j_joystick[i].buffer);
269                 }
270                 j_joystick[i].buffer=-1;
271         }
272
273         joy_present=0;
274         joy_installed=0;
275 }
276
277
278 void joy_set_cen() {
279 }
280
281
282 int joy_get_scaled_reading(int raw, int axis_num)
283 {
284  int d, x;
285
286   raw -= j_axis[axis_num].center_val;
287
288    if (raw < 0)
289     d = j_axis[axis_num].center_val - j_axis[axis_num].min_val;
290    else if (raw > 0)
291     d = j_axis[axis_num].max_val - j_axis[axis_num].center_val;
292    else
293     d = 0;
294
295    if (d)
296     x = ((raw << 7) / d);
297    else
298     x = 0;
299
300    if ( x < -128 )
301     x = -128;
302    if ( x > 127 )
303     x = 127;
304
305 //added on 4/13/99 by Victor Rachels to add deadzone control
306   d =  (joy_deadzone) * 6;
307    if ((x > (-1*d)) && (x < d))
308     x = 0;
309 //end this section addition -VR
310
311   return x;
312 }
313
314
315 void joy_get_pos(int *x, int *y) {
316         int axis[MAX_AXES];
317
318         if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
319
320         joystick_read_raw_axis (JOY_ALL_AXIS, axis);
321
322         *x = joy_get_scaled_reading( axis[0], 0 );
323         *y = joy_get_scaled_reading( axis[1], 1 );
324 }
325
326
327 int joy_get_btns () {
328         return 0;
329 }
330
331
332 int joy_get_button_state (int btn) {
333   if(btn >= j_num_buttons)
334    return 0;
335         j_Update_state ();
336
337         return j_button[btn].state;
338 }
339
340
341 int joy_get_button_down_cnt (int btn) {
342         int downcount;
343
344         j_Update_state ();
345
346         downcount = j_button[btn].downcount;
347         j_button[btn].downcount = 0;
348
349         return downcount;
350 }
351
352
353 //changed 6/24/99 to finally squish the timedown bug - Owen Evans
354 fix joy_get_button_down_time(int btn)  {
355         fix downtime;
356         j_Update_state ();
357
358         if (j_button[btn].state) {
359                 downtime = timer_get_fixed_seconds() - j_button[btn].timedown;
360                 j_button[btn].timedown = timer_get_fixed_seconds();
361         } else {
362                 downtime = 0;
363         }
364
365         return downtime;
366 }
367 //end changed - OE
368
369 void joy_poll() {
370
371 }
372
373
374 void joy_set_slow_reading(int flag) {
375
376 }
377