16 char joy_installed = 0;
19 joystick_device j_joystick[4];
20 joystick_axis j_axis[MAX_AXES];
21 joystick_button j_button[MAX_BUTTONS];
23 int j_num_axes = 0, j_num_buttons = 0;
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 */
31 int j_Get_joydev_axis_number (int all_axis_number) {
32 int i, joy_axis_number = all_axis_number;
34 for (i = 0; i < j_axis[all_axis_number].joydev; i++) {
35 joy_axis_number -= j_joystick[i].num_axes;
38 return joy_axis_number;
42 int j_Get_joydev_button_number (int all_button_number) {
43 int i, joy_button_number = all_button_number;
45 for (i = 0; i < j_button[all_button_number].joydev; i++) {
46 joy_button_number -= j_joystick[i].num_buttons;
49 return joy_button_number;
53 int j_Update_state () {
54 int num_processed = 0, i;
55 struct js_event current_event;
56 struct JS_DATA_TYPE joy_data;
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();
67 j_button[i].last_state = j_button[i].state;
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, ¤t_event, sizeof (struct js_event)) > 0) {
75 switch (current_event.type & ~JS_EVENT_INIT) {
77 j_axis[j_axes_in_sticks[i] + current_event.number].value = current_event.value;
80 j_button[j_buttons_in_sticks[i] + current_event.number].state = current_event.value;
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;
98 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
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];
109 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
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++) {
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;
122 void joy_set_min (int axis_number, int value) {
123 j_axis[axis_number].min_val = value;
127 void joy_set_center (int axis_number, int value) {
128 j_axis[axis_number].center_val = value;
132 void joy_set_max (int axis_number, int value) {
133 j_axis[axis_number].max_val = value;
137 ubyte joy_get_present_mask () {
142 void joy_set_timer_rate (int max_value) {
143 timer_rate = max_value;
147 int joy_get_timer_rate () {
155 if (!joy_installed) return;
157 for (i = 0; i < j_num_buttons; i++) {
158 j_button[i].timedown = 0;
159 j_button[i].downcount = 0;
165 ubyte joystick_read_raw_axis (ubyte mask, int *axes) {
170 for (i = 0; i < j_num_axes; i++) {
171 axes[i] = j_axis[i].value;
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. */
184 if (joy_installed) return 0;
187 if (!joy_installed) {
189 printf ("Initializing joystick... ");
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);
196 if (j_joystick[0].buffer >= 0 || j_joystick[1].buffer >= 0 || j_joystick[2].buffer >= 0 || j_joystick[3].buffer >= 0) {
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);
209 printf ("js%d (v%d.%d.%d) ",
211 (j_joystick[i].version & 0xff0000) >> 16,
212 (j_joystick[i].version & 0xff00) >> 8,
213 j_joystick[i].version & 0xff);
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;
224 for (j = j_num_buttons; j < (j_num_buttons + j_joystick[i].num_buttons); j++) {
225 j_button[j].joydev = i;
228 j_num_axes += j_joystick[i].num_axes;
229 j_num_buttons += j_joystick[i].num_buttons;
232 j_joystick[i].num_buttons = 0;
233 j_joystick[i].num_axes = 0;
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;
242 printf ("no joysticks found\n");
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;
265 if (!joy_installed) return;
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);
272 j_joystick[i].buffer=-1;
284 int joy_get_scaled_reading(int raw, int axis_num)
288 raw -= j_axis[axis_num].center_val;
291 d = j_axis[axis_num].center_val - j_axis[axis_num].min_val;
293 d = j_axis[axis_num].max_val - j_axis[axis_num].center_val;
298 x = ((raw << 7) / d);
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))
311 //end this section addition -VR
317 void joy_get_pos(int *x, int *y) {
320 if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
322 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
324 *x = joy_get_scaled_reading( axis[0], 0 );
325 *y = joy_get_scaled_reading( axis[1], 1 );
329 int joy_get_btns () {
334 int joy_get_button_state (int btn) {
335 if(btn >= j_num_buttons)
339 return j_button[btn].state;
343 int joy_get_button_down_cnt (int btn) {
348 downcount = j_button[btn].downcount;
349 j_button[btn].downcount = 0;
355 //changed 6/24/99 to finally squish the timedown bug - Owen Evans
356 fix joy_get_button_down_time(int btn) {
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();
376 void joy_set_slow_reading(int flag) {
380 #endif // __ENV_LINUX__