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