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