2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
15 * $Source: /cvs/cvsroot/d2x/arch/dos/joyc.c,v $
17 * $Author: schaffner $
18 * $Date: 2004-08-28 23:17:45 $
20 * Routines for joystick reading.
42 extern int joy_bogus_reading;
43 int JOY_PORT = 513; //201h;
46 int joy_read_stick_asm2( int read_masks, int * event_buffer, int timeout );
47 int joy_read_stick_friendly2( int read_masks, int * event_buffer, int timeout );
48 int joy_read_stick_polled2( int read_masks, int * event_buffer, int timeout );
49 int joy_read_stick_bios2( int read_masks, int * event_buffer, int timeout );
50 int joy_read_buttons_bios2();
51 void joy_read_buttons_bios_end2();
56 // edi = pointer to buffer
57 // returns number of events
59 char joy_installed = 0;
62 typedef struct Button_info {
71 typedef struct Joy_info {
77 Button_info buttons[MAX_BUTTONS];
85 ubyte joy_read_buttons()
87 return ((~(inp(JOY_PORT) >> 4))&0xf);
90 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
94 for (i = 0; i < JOY_NUM_AXES; i++)
96 axis_min[i] = joystick.axis_min[i];
97 axis_center[i] = joystick.axis_center[i];
98 axis_max[i] = joystick.axis_max[i];
102 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
106 for (i = 0; i < JOY_NUM_AXES; i++)
108 joystick.axis_min[i] = axis_min[i];
109 joystick.axis_center[i] = axis_center[i];
110 joystick.axis_max[i] = axis_max[i];
115 ubyte joy_get_present_mask() {
116 return joystick.present_mask;
119 void joy_set_timer_rate(int max_value ) {
121 joystick.max_timer = max_value;
125 int joy_get_timer_rate() {
126 return joystick.max_timer;
133 if (!joy_installed) return;
136 for (i=0; i<MAX_BUTTONS; i++ ) {
137 joystick.buttons[i].ignore = 0;
138 joystick.buttons[i].state = 0;
139 joystick.buttons[i].timedown = 0;
140 joystick.buttons[i].downcount = 0;
141 joystick.buttons[i].upcount = 0;
148 void joy_handler(int ticks_this_time) {
151 Button_info * button;
153 // joystick.max_timer = ticks_this_time;
155 if ( joystick.slow_read & JOY_BIOS_READINGS ) {
156 joystick.read_count++;
157 if ( joystick.read_count > 7 ) {
158 joystick.read_count = 0;
159 value = joy_read_buttons_bios2();
160 joystick.last_value = value;
162 value = joystick.last_value;
165 value = joy_read_buttons(); //JOY_READ_BUTTONS;
168 for (i=0; i<MAX_BUTTONS; i++ ) {
169 button = &joystick.buttons[i];
170 if (!button->ignore) {
172 state = (value >> i) & 1;
173 else if (i==(value+4))
178 if ( button->last_state == state ) {
179 if (state) button->timedown += ticks_this_time;
182 button->downcount += state;
185 button->upcount += button->state;
188 button->last_state = state;
194 void joy_handler_end() { // Dummy function to help calculate size of joystick handler function
198 ubyte joy_read_raw_buttons() {
199 if ( joystick.slow_read & JOY_BIOS_READINGS )
200 return joy_read_buttons_bios2();
202 return joy_read_buttons(); //JOY_READ_BUTTONS;
205 void joy_set_slow_reading(int flag)
207 joystick.slow_read |= flag;
211 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
213 ubyte read_masks, org_masks;
214 int t, t1, t2, buffer[4*2+2];
215 int e, i, num_channels, c;
217 axis[0] = 0; axis[1] = 0;
218 axis[2] = 0; axis[3] = 0;
220 if (!joy_installed) return 0;
225 mask &= joystick.present_mask; // Don't read non-present channels
227 return 0; // Don't read if no stick connected.
230 if ( joystick.slow_read & JOY_SLOW_READINGS ) {
231 for (c=0; c<4; c++ ) {
232 if ( mask & (1 << c)) {
233 // Time out at (1/100th of a second)
235 if ( joystick.slow_read & JOY_POLLED_READINGS )
236 num_channels = joy_read_stick_polled2( (1 << c), buffer, 65536 );
237 else if ( joystick.slow_read & JOY_BIOS_READINGS )
238 num_channels = joy_read_stick_bios2( (1 << c), buffer, 65536 );
239 else if ( joystick.slow_read & JOY_FRIENDLY_READINGS )
240 num_channels = joy_read_stick_friendly2( (1 << c), buffer, (1193180/100) );
242 num_channels = joy_read_stick_asm2( (1 << c), buffer, (1193180/100) );
244 if ( num_channels > 0 ) {
248 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
254 t = t1 + joystick.max_timer - t2;
255 //mprintf( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t );
259 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
260 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
261 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
262 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
267 // Time out at (1/100th of a second)
268 if ( joystick.slow_read & JOY_POLLED_READINGS )
269 num_channels = joy_read_stick_polled2( mask, buffer, 65536 );
270 else if ( joystick.slow_read & JOY_BIOS_READINGS )
271 num_channels = joy_read_stick_bios2( mask, buffer, 65536 );
272 else if ( joystick.slow_read & JOY_FRIENDLY_READINGS )
273 num_channels = joy_read_stick_friendly2( mask, buffer, (1193180/100) );
275 num_channels = joy_read_stick_asm2( mask, buffer, (1193180/100) );
276 //mprintf(( 0, "(%d)\n", num_channels ));
278 for (i=0; i<num_channels; i++ ) {
282 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
288 t = t1 + joystick.max_timer - t2;
289 //mprintf(( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t ));
294 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
295 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
296 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
297 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
314 // if(FindArg("-joy209"))
315 // use_alt_joyport=1;
316 if(FindArg("-joy209"))
317 JOY_PORT = 521; //209h;
322 for (i=0; i<MAX_BUTTONS; i++ )
323 joystick.buttons[i].last_state = 0;
326 if ( !joy_installed ) {
329 //joystick.max_timer = 65536;
330 joystick.slow_read = 0;
331 joystick.read_count = 0;
332 joystick.last_value = 0;
334 //--------------- lock everything for the virtal memory ----------------------------------
335 if (!dpmi_lock_region ((void near *)joy_read_buttons_bios2, (char *)joy_read_buttons_bios_end2 - (char near *)joy_read_buttons_bios2)) {
336 printf( "Error locking joystick handler (read bios)!\n" );
342 if (!dpmi_lock_region ((void near *)joy_handler, (char *)joy_handler_end - (char near *)joy_handler)) {
343 printf( "Error locking joystick handler!\n" );
347 if (!dpmi_lock_region (&joystick, sizeof(Joy_info))) {
348 printf( "Error locking joystick handler's data!\n" );
352 timer_set_joyhandler(joy_handler);
355 // Do initial cheapy calibration...
356 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
358 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, temp_axis );
359 } while( joy_bogus_reading );
361 if ( joystick.present_mask & 3 )
371 if (!joy_installed) return;
377 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
379 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_min );
380 } while( joy_bogus_reading );
381 if ( joystick.present_mask & 3 )
389 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
391 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_max );
392 } while( joy_bogus_reading );
394 if ( joystick.present_mask & 3 )
402 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
404 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_center );
405 } while( joy_bogus_reading );
407 if ( joystick.present_mask & 3 )
413 void joy_set_cen_fake(int channel)
417 int minx, maxx, cenx;
421 for (i=0; i<4; i++ ) {
422 if ( (joystick.present_mask & (1<<i)) && (i!=channel) ) {
424 minx += joystick.axis_min[i];
425 maxx += joystick.axis_max[i];
426 cenx += joystick.axis_center[i];
433 joystick.axis_min[channel] = minx;
434 joystick.axis_max[channel] = maxx;
435 joystick.axis_center[channel] = cenx;
438 int joy_get_scaled_reading( int raw, int axn )
442 // Make sure it's calibrated properly.
443 //added/changed on 8/14/98 to allow smaller calibrating sticks to work (by Eivind Brendryen)--was <5
444 if ( joystick.axis_center[axn] - joystick.axis_min[axn] < 2 )
446 if ( joystick.axis_max[axn] - joystick.axis_center[axn] < 2 )
448 //end change - Victor Rachels (by Eivind Brendryen)
450 raw -= joystick.axis_center[axn];
453 d = joystick.axis_center[axn]-joystick.axis_min[axn];
455 d = joystick.axis_max[axn]-joystick.axis_center[axn];
469 //added on 4/13/99 by Victor Rachels to add deadzone control
470 dz = (joy_deadzone) * 6;
471 if ((x > (-1*dz)) && (x < dz))
473 //end this section addition -VR
478 int last_reading[4] = { 0, 0, 0, 0 };
480 void joy_get_pos( int *x, int *y )
485 if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
487 flags=joystick_read_raw_axis( JOY_1_X_AXIS+JOY_1_Y_AXIS, axis );
489 if ( joy_bogus_reading ) {
490 axis[0] = last_reading[0];
491 axis[1] = last_reading[1];
492 flags = JOY_1_X_AXIS+JOY_1_Y_AXIS;
494 last_reading[0] = axis[0];
495 last_reading[1] = axis[1];
498 if ( flags & JOY_1_X_AXIS )
499 *x = joy_get_scaled_reading( axis[0], 0 );
503 if ( flags & JOY_1_Y_AXIS )
504 *y = joy_get_scaled_reading( axis[1], 1 );
509 ubyte joy_read_stick( ubyte masks, int *axis )
514 if ((!joy_installed)||(!joy_present)) {
515 axis[0] = 0; axis[1] = 0;
516 axis[2] = 0; axis[3] = 0;
520 flags=joystick_read_raw_axis( masks, raw_axis );
522 if ( joy_bogus_reading ) {
523 axis[0] = last_reading[0];
524 axis[1] = last_reading[1];
525 axis[2] = last_reading[2];
526 axis[3] = last_reading[3];
529 last_reading[0] = axis[0];
530 last_reading[1] = axis[1];
531 last_reading[2] = axis[2];
532 last_reading[3] = axis[3];
535 if ( flags & JOY_1_X_AXIS )
536 axis[0] = joy_get_scaled_reading( raw_axis[0], 0 );
540 if ( flags & JOY_1_Y_AXIS )
541 axis[1] = joy_get_scaled_reading( raw_axis[1], 1 );
545 if ( flags & JOY_2_X_AXIS )
546 axis[2] = joy_get_scaled_reading( raw_axis[2], 2 );
550 if ( flags & JOY_2_Y_AXIS )
551 axis[3] = joy_get_scaled_reading( raw_axis[3], 3 );
561 if ((!joy_installed)||(!joy_present)) return 0;
563 return joy_read_raw_buttons();
566 void joy_get_btn_down_cnt( int *btn0, int *btn1 )
568 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
571 *btn0 = joystick.buttons[0].downcount;
572 joystick.buttons[0].downcount = 0;
573 *btn1 = joystick.buttons[1].downcount;
574 joystick.buttons[1].downcount = 0;
578 int joy_get_button_state( int btn )
582 if ((!joy_installed)||(!joy_present)) return 0;
584 if ( btn >= MAX_BUTTONS ) return 0;
587 count = joystick.buttons[btn].state;
593 int joy_get_button_up_cnt( int btn )
597 if ((!joy_installed)||(!joy_present)) return 0;
599 if ( btn >= MAX_BUTTONS ) return 0;
602 count = joystick.buttons[btn].upcount;
603 joystick.buttons[btn].upcount = 0;
609 int joy_get_button_down_cnt( int btn )
613 if ((!joy_installed)||(!joy_present)) return 0;
614 if ( btn >= MAX_BUTTONS ) return 0;
617 count = joystick.buttons[btn].downcount;
618 joystick.buttons[btn].downcount = 0;
625 fix joy_get_button_down_time( int btn )
629 if ((!joy_installed)||(!joy_present)) return 0;
630 if ( btn >= MAX_BUTTONS ) return 0;
633 count = joystick.buttons[btn].timedown;
634 joystick.buttons[btn].timedown = 0;
637 return fixmuldiv(count, 65536, 1193180 );
640 void joy_get_btn_up_cnt( int *btn0, int *btn1 )
642 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
645 *btn0 = joystick.buttons[0].upcount;
646 joystick.buttons[0].upcount = 0;
647 *btn1 = joystick.buttons[1].upcount;
648 joystick.buttons[1].upcount = 0;
652 void joy_set_btn_values( int btn, int state, fix timedown, int downcount, int upcount )
655 joystick.buttons[btn].ignore = 1;
656 joystick.buttons[btn].state = state;
657 joystick.buttons[btn].timedown = fixmuldiv( timedown, 1193180, 65536 );
658 joystick.buttons[btn].downcount = downcount;
659 joystick.buttons[btn].upcount = upcount;
665 if ( joystick.slow_read & JOY_BIOS_READINGS )
666 joystick.last_value = joy_read_buttons_bios2();