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