6 //#include "joystick.h"
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;
94 return num_processed;*/
99 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
102 for (i = 0; i < 4; i++) {
103 j_axis[i].center_val = axis_center[i];
104 j_axis[i].min_val = axis_min[i];
105 j_axis[i].max_val = axis_max[i];
110 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max) {
113 //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 :)
114 for (i = 0; i < 4; i++) {
116 axis_center[i] = j_axis[i].center_val;
117 axis_min[i] = j_axis[i].min_val;
118 axis_max[i] = j_axis[i].max_val;
123 void joy_set_min (int axis_number, int value) {
124 j_axis[axis_number].min_val = value;
128 void joy_set_center (int axis_number, int value) {
129 j_axis[axis_number].center_val = value;
133 void joy_set_max (int axis_number, int value) {
134 j_axis[axis_number].max_val = value;
138 ubyte joy_get_present_mask () {
143 void joy_set_timer_rate (int max_value) {
144 timer_rate = max_value;
148 int joy_get_timer_rate () {
156 if (!joy_installed) return;
158 for (i = 0; i < j_num_buttons; i++) {
159 j_button[i].timedown = 0;
160 j_button[i].downcount = 0;
166 ubyte joystick_read_raw_axis (ubyte mask, int *axes) {
171 for (i = 0; i <= j_num_axes; i++) {
172 axes[i] = j_axis[i].value;
179 /* 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. */
185 if (joy_installed) return 0;
188 if (!joy_installed) {
190 // printf ("Initializing joystick... ");
192 j_joystick[0].buffer = open ("/dev/js0", O_NONBLOCK);
193 j_joystick[1].buffer = open ("/dev/js1", O_NONBLOCK);
194 j_joystick[2].buffer = open ("/dev/js2", O_NONBLOCK);
195 j_joystick[3].buffer = open ("/dev/js3", O_NONBLOCK);
197 if (j_joystick[0].buffer >= 0 || j_joystick[1].buffer >= 0 || j_joystick[2].buffer >= 0 || j_joystick[3].buffer >= 0) {
198 // printf ("found: ");
200 for (i = 0; i < 4; i++) {
201 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);
207 if (!j_joystick[i].version) {
208 j_joystick[i].num_axes = 2;
209 j_joystick[i].num_buttons = 2;
210 // printf ("js%d (v0.x) " , i);
212 // 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);
215 for (j = j_num_axes; j < (j_num_axes + j_joystick[i].num_axes); j++) {
216 j_axis[j].joydev = i;
217 if (j_joystick[i].version) {
218 j_axis[j].center_val = 0;
219 j_axis[j].max_val = 32767;
220 j_axis[j].min_val = -32767;
223 for (j = j_num_buttons; j < (j_num_buttons + j_joystick[i].num_buttons); j++) {
224 j_button[j].joydev = i;
227 j_num_axes += j_joystick[i].num_axes;
228 j_num_buttons += j_joystick[i].num_buttons;
231 j_joystick[i].num_buttons = 0;
232 j_joystick[i].num_axes = 0;
235 for (j = 0; j < i; j++) {
236 j_axes_in_sticks[i] += j_joystick[j].num_axes;
237 j_buttons_in_sticks[i] += j_joystick[j].num_buttons;
241 // printf ("no joysticks found\n");
247 if (j_num_axes > MAX_AXES)
248 j_num_axes = MAX_AXES;
249 if (j_num_buttons > MAX_BUTTONS)
250 j_num_buttons = MAX_BUTTONS;
264 if (!joy_installed) return;
266 for (i = 0; i < 4; i++) {
267 if (j_joystick[i].buffer>=0) {
268 printf ("closing js%d\n", i);
269 close (j_joystick[i].buffer);
271 j_joystick[i].buffer=-1;
283 int joy_get_scaled_reading(int raw, int axis_num)
287 raw -= j_axis[axis_num].center_val;
290 d = j_axis[axis_num].center_val - j_axis[axis_num].min_val;
292 d = j_axis[axis_num].max_val - j_axis[axis_num].center_val;
297 x = ((raw << 7) / d);
306 //added on 4/13/99 by Victor Rachels to add deadzone control
307 d = (joy_deadzone) * 6;
308 if ((x > (-1*d)) && (x < d))
310 //end this section addition -VR
316 void joy_get_pos(int *x, int *y) {
319 if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
321 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
323 *x = joy_get_scaled_reading( axis[0], 0 );
324 *y = joy_get_scaled_reading( axis[1], 1 );
328 int joy_get_btns () {
333 int joy_get_button_state (int btn) {
334 if(btn >= j_num_buttons)
338 return j_button[btn].state;
342 int joy_get_button_down_cnt (int btn) {
347 downcount = j_button[btn].downcount;
348 j_button[btn].downcount = 0;
354 //changed 6/24/99 to finally squish the timedown bug - Owen Evans
355 fix joy_get_button_down_time(int btn) {
359 if (j_button[btn].state) {
360 downtime = timer_get_fixed_seconds() - j_button[btn].timedown;
361 j_button[btn].timedown = timer_get_fixed_seconds();
375 void joy_set_slow_reading(int flag) {
379 #endif // __ENV_LINUX__