4 //#include "joystick.h"
14 char joy_installed = 0;
17 joystick_device j_joystick[4];
18 joystick_axis j_axis[MAX_AXES];
19 joystick_button j_button[MAX_BUTTONS];
21 int j_num_axes = 0, j_num_buttons = 0;
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 */
29 int j_Get_joydev_axis_number (int all_axis_number) {
30 int i, joy_axis_number = all_axis_number;
32 for (i = 0; i < j_axis[all_axis_number].joydev; i++) {
33 joy_axis_number -= j_joystick[i].num_axes;
36 return joy_axis_number;
40 int j_Get_joydev_button_number (int all_button_number) {
41 int i, joy_button_number = all_button_number;
43 for (i = 0; i < j_button[all_button_number].joydev; i++) {
44 joy_button_number -= j_joystick[i].num_buttons;
47 return joy_button_number;
51 int j_Update_state () {
52 /* int num_processed = 0, i;
53 struct js_event current_event;
54 struct JS_DATA_TYPE joy_data;
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();
65 j_button[i].last_state = j_button[i].state;
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, ¤t_event, sizeof (struct js_event)) > 0) {
73 switch (current_event.type & ~JS_EVENT_INIT) {
75 j_axis[j_axes_in_sticks[i] + current_event.number].value = current_event.value;
78 j_button[j_buttons_in_sticks[i] + current_event.number].state = current_event.value;
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;
92 return num_processed;*/
97 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
100 for (i = 0; i < 4; i++) {
101 j_axis[i].center_val = axis_center[i];
102 j_axis[i].min_val = axis_min[i];
103 j_axis[i].max_val = axis_max[i];
108 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
111 //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 :)
112 for (i = 0; i < 4; i++) {
114 axis_center[i] = j_axis[i].center_val;
115 axis_min[i] = j_axis[i].min_val;
116 axis_max[i] = j_axis[i].max_val;
121 void joy_set_min (int axis_number, int value) {
122 j_axis[axis_number].min_val = value;
126 void joy_set_center (int axis_number, int value) {
127 j_axis[axis_number].center_val = value;
131 void joy_set_max (int axis_number, int value) {
132 j_axis[axis_number].max_val = value;
136 ubyte joy_get_present_mask () {
141 void joy_set_timer_rate (int max_value) {
142 timer_rate = max_value;
146 int joy_get_timer_rate () {
154 if (!joy_installed) return;
156 for (i = 0; i < j_num_buttons; i++) {
157 j_button[i].timedown = 0;
158 j_button[i].downcount = 0;
164 ubyte joystick_read_raw_axis (ubyte mask, int *axes) {
169 for (i = 0; i <= j_num_axes; i++) {
170 axes[i] = j_axis[i].value;
177 /* 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. */
183 if (joy_installed) return 0;
186 if (!joy_installed) {
188 // printf ("Initializing joystick... ");
190 j_joystick[0].buffer = open ("/dev/js0", O_NONBLOCK);
191 j_joystick[1].buffer = open ("/dev/js1", O_NONBLOCK);
192 j_joystick[2].buffer = open ("/dev/js2", O_NONBLOCK);
193 j_joystick[3].buffer = open ("/dev/js3", O_NONBLOCK);
195 if (j_joystick[0].buffer >= 0 || j_joystick[1].buffer >= 0 || j_joystick[2].buffer >= 0 || j_joystick[3].buffer >= 0) {
196 // printf ("found: ");
198 for (i = 0; i < 4; i++) {
199 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);
205 if (!j_joystick[i].version) {
206 j_joystick[i].num_axes = 2;
207 j_joystick[i].num_buttons = 2;
208 // printf ("js%d (v0.x) " , i);
210 // printf ("js%d (v%d.%d.%d) ", i, (j_joystick[i].version & 0xff0000) >> 16, (j_joystick[i].version & 0xff00) >> 8, j_joystick[i].version & 0xff);
213 for (j = j_num_axes; j < (j_num_axes + j_joystick[i].num_axes); j++) {
214 j_axis[j].joydev = i;
215 if (j_joystick[i].version) {
216 j_axis[j].center_val = 0;
217 j_axis[j].max_val = 32767;
218 j_axis[j].min_val = -32767;
221 for (j = j_num_buttons; j < (j_num_buttons + j_joystick[i].num_buttons); j++) {
222 j_button[j].joydev = i;
225 j_num_axes += j_joystick[i].num_axes;
226 j_num_buttons += j_joystick[i].num_buttons;
229 j_joystick[i].num_buttons = 0;
230 j_joystick[i].num_axes = 0;
233 for (j = 0; j < i; j++) {
234 j_axes_in_sticks[i] += j_joystick[j].num_axes;
235 j_buttons_in_sticks[i] += j_joystick[j].num_buttons;
239 // printf ("no joysticks found\n");
245 if (j_num_axes > MAX_AXES)
246 j_num_axes = MAX_AXES;
247 if (j_num_buttons > MAX_BUTTONS)
248 j_num_buttons = MAX_BUTTONS;
262 if (!joy_installed) return;
264 for (i = 0; i < 4; i++) {
265 if (j_joystick[i].buffer>=0) {
266 printf ("closing js%d\n", i);
267 close (j_joystick[i].buffer);
269 j_joystick[i].buffer=-1;
281 int joy_get_scaled_reading(int raw, int axis_num)
285 raw -= j_axis[axis_num].center_val;
288 d = j_axis[axis_num].center_val - j_axis[axis_num].min_val;
290 d = j_axis[axis_num].max_val - j_axis[axis_num].center_val;
295 x = ((raw << 7) / d);
304 //added on 4/13/99 by Victor Rachels to add deadzone control
305 d = (joy_deadzone) * 6;
306 if ((x > (-1*d)) && (x < d))
308 //end this section addition -VR
314 void joy_get_pos(int *x, int *y) {
317 if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
319 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
321 *x = joy_get_scaled_reading( axis[0], 0 );
322 *y = joy_get_scaled_reading( axis[1], 1 );
326 int joy_get_btns () {
331 int joy_get_button_state (int btn) {
332 if(btn >= j_num_buttons)
336 return j_button[btn].state;
340 int joy_get_button_down_cnt (int btn) {
345 downcount = j_button[btn].downcount;
346 j_button[btn].downcount = 0;
352 //changed 6/24/99 to finally squish the timedown bug - Owen Evans
353 fix joy_get_button_down_time(int btn) {
357 if (j_button[btn].state) {
358 downtime = timer_get_fixed_seconds() - j_button[btn].timedown;
359 j_button[btn].timedown = timer_get_fixed_seconds();
373 void joy_set_slow_reading(int flag) {