6 #include <linux/joystick.h>
16 #include "linux_joystick.h"
18 char joy_installed = 0;
21 joystick_device j_joystick[4];
22 joystick_axis j_axis[MAX_AXES];
23 joystick_button j_button[MAX_BUTTONS];
25 int j_num_axes = 0, j_num_buttons = 0;
28 int j_axes_in_sticks[4]; /* number of axes in the first [x] sticks */
29 int j_buttons_in_sticks[4]; /* number of buttons in the first [x] sticks */
33 int j_Get_joydev_axis_number (int all_axis_number) {
34 int i, joy_axis_number = all_axis_number;
36 for (i = 0; i < j_axis[all_axis_number].joydev; i++) {
37 joy_axis_number -= j_joystick[i].num_axes;
40 return joy_axis_number;
44 int j_Get_joydev_button_number (int all_button_number) {
45 int i, joy_button_number = all_button_number;
47 for (i = 0; i < j_button[all_button_number].joydev; i++) {
48 joy_button_number -= j_joystick[i].num_buttons;
51 return joy_button_number;
55 int j_Update_state () {
56 int num_processed = 0, i;
57 struct js_event current_event;
58 struct JS_DATA_TYPE joy_data;
60 for (i = 0; i < j_num_buttons; i++) {
61 //changed 6/24/1999 to finally squish the timedown bug - Owen Evans
62 if (j_button[i].state != j_button[i].last_state) {
63 if (j_button[i].state) {
64 j_button[i].downcount++;
65 j_button[i].timedown = timer_get_fixed_seconds();
69 j_button[i].last_state = j_button[i].state;
72 for (i = 0; i < 4; i++) {
73 if (j_joystick[i].buffer >= 0) {
74 if (j_joystick[i].version) {
75 while (read (j_joystick[i].buffer, ¤t_event, sizeof (struct js_event)) > 0) {
77 switch (current_event.type & ~JS_EVENT_INIT) {
79 j_axis[j_axes_in_sticks[i] + current_event.number].value = current_event.value;
82 j_button[j_buttons_in_sticks[i] + current_event.number].state = current_event.value;
87 read (j_joystick[i].buffer, &joy_data, JS_RETURN);
88 j_axis[j_axes_in_sticks[i] + 0].value = joy_data.x;
89 j_axis[j_axes_in_sticks[i] + 1].value = joy_data.y;
90 j_button[j_buttons_in_sticks[i] + 0].state = (joy_data.buttons & 0x01);
91 j_button[j_buttons_in_sticks[i] + 1].state = (joy_data.buttons & 0x02) >> 1;
100 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
103 for (i = 0; i < 4; i++) {
104 j_axis[i].center_val = axis_center[i];
105 j_axis[i].min_val = axis_min[i];
106 j_axis[i].max_val = axis_max[i];
111 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
114 //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 :)
115 for (i = 0; i < 4; i++) {
117 axis_center[i] = j_axis[i].center_val;
118 axis_min[i] = j_axis[i].min_val;
119 axis_max[i] = j_axis[i].max_val;
124 void joy_set_min (int axis_number, int value) {
125 j_axis[axis_number].min_val = value;
129 void joy_set_center (int axis_number, int value) {
130 j_axis[axis_number].center_val = value;
134 void joy_set_max (int axis_number, int value) {
135 j_axis[axis_number].max_val = value;
139 ubyte joy_get_present_mask () {
144 void joy_set_timer_rate (int max_value) {
145 timer_rate = max_value;
149 int joy_get_timer_rate () {
157 if (!joy_installed) return;
159 for (i = 0; i < j_num_buttons; i++) {
160 j_button[i].timedown = 0;
161 j_button[i].downcount = 0;
167 ubyte joystick_read_raw_axis (ubyte mask, int *axes) {
172 for (i = 0; i < j_num_axes; i++) {
173 axes[i] = j_axis[i].value;
180 /* 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. */
186 if (joy_installed) return 0;
189 if (!joy_installed) {
191 printf ("Initializing joystick... ");
193 j_joystick[0].buffer = open ("/dev/js0", O_NONBLOCK);
194 j_joystick[1].buffer = open ("/dev/js1", O_NONBLOCK);
195 j_joystick[2].buffer = open ("/dev/js2", O_NONBLOCK);
196 j_joystick[3].buffer = open ("/dev/js3", O_NONBLOCK);
198 if (j_joystick[0].buffer >= 0 || j_joystick[1].buffer >= 0 || j_joystick[2].buffer >= 0 || j_joystick[3].buffer >= 0) {
201 for (i = 0; i < 4; i++) {
202 if (j_joystick[i].buffer >= 0) {
203 ioctl (j_joystick[i].buffer, JSIOCGAXES, &j_joystick[i].num_axes);
204 ioctl (j_joystick[i].buffer, JSIOCGBUTTONS, &j_joystick[i].num_buttons);
205 ioctl (j_joystick[i].buffer, JSIOCGVERSION, &j_joystick[i].version);
206 if (!j_joystick[i].version) {
207 j_joystick[i].num_axes = 2;
208 j_joystick[i].num_buttons = 2;
209 printf ("js%d (v0.x) " , i);
211 printf ("js%d (v%d.%d.%d) ",
213 (j_joystick[i].version & 0xff0000) >> 16,
214 (j_joystick[i].version & 0xff00) >> 8,
215 j_joystick[i].version & 0xff);
218 for (j = j_num_axes; j < (j_num_axes + j_joystick[i].num_axes); j++) {
219 j_axis[j].joydev = i;
220 if (j_joystick[i].version) {
221 j_axis[j].center_val = 0;
222 j_axis[j].max_val = 32767;
223 j_axis[j].min_val = -32767;
226 for (j = j_num_buttons; j < (j_num_buttons + j_joystick[i].num_buttons); j++) {
227 j_button[j].joydev = i;
230 j_num_axes += j_joystick[i].num_axes;
231 j_num_buttons += j_joystick[i].num_buttons;
234 j_joystick[i].num_buttons = 0;
235 j_joystick[i].num_axes = 0;
238 for (j = 0; j < i; j++) {
239 j_axes_in_sticks[i] += j_joystick[j].num_axes;
240 j_buttons_in_sticks[i] += j_joystick[j].num_buttons;
244 printf ("no joysticks found\n");
250 if (j_num_axes > MAX_AXES)
251 j_num_axes = MAX_AXES;
252 if (j_num_buttons > MAX_BUTTONS)
253 j_num_buttons = MAX_BUTTONS;
267 if (!joy_installed) return;
269 for (i = 0; i < 4; i++) {
270 if (j_joystick[i].buffer>=0) {
271 printf ("closing js%d\n", i);
272 close (j_joystick[i].buffer);
274 j_joystick[i].buffer=-1;
286 int joy_get_scaled_reading(int raw, int axis_num)
290 raw -= j_axis[axis_num].center_val;
293 d = j_axis[axis_num].center_val - j_axis[axis_num].min_val;
295 d = j_axis[axis_num].max_val - j_axis[axis_num].center_val;
300 x = ((raw << 7) / d);
309 //added on 4/13/99 by Victor Rachels to add deadzone control
310 d = (joy_deadzone) * 6;
311 if ((x > (-1*d)) && (x < d))
313 //end this section addition -VR
319 void joy_get_pos(int *x, int *y) {
322 if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
324 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
326 *x = joy_get_scaled_reading( axis[0], 0 );
327 *y = joy_get_scaled_reading( axis[1], 1 );
331 int joy_get_btns () {
336 int joy_get_button_state (int btn) {
337 if(btn >= j_num_buttons)
341 return j_button[btn].state;
345 int joy_get_button_down_cnt (int btn) {
350 downcount = j_button[btn].downcount;
351 j_button[btn].downcount = 0;
357 //changed 6/24/99 to finally squish the timedown bug - Owen Evans
358 fix joy_get_button_down_time(int btn) {
362 if (j_button[btn].state) {
363 downtime = timer_get_fixed_seconds() - j_button[btn].timedown;
364 j_button[btn].timedown = timer_get_fixed_seconds();
378 void joy_set_slow_reading(int flag) {
382 #endif // __ENV_LINUX__