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.
16 * Routines for joystick reading.
38 extern int joy_bogus_reading;
39 int JOY_PORT = 513; //201h;
42 int joy_read_stick_asm2( int read_masks, int * event_buffer, int timeout );
43 int joy_read_stick_friendly2( int read_masks, int * event_buffer, int timeout );
44 int joy_read_stick_polled2( int read_masks, int * event_buffer, int timeout );
45 int joy_read_stick_bios2( int read_masks, int * event_buffer, int timeout );
46 int joy_read_buttons_bios2();
47 void joy_read_buttons_bios_end2();
52 // edi = pointer to buffer
53 // returns number of events
55 char joy_installed = 0;
58 typedef struct Button_info {
67 typedef struct Joy_info {
73 Button_info buttons[JOY_MAX_BUTTONS];
81 ubyte joy_read_buttons()
83 return ((~(inp(JOY_PORT) >> 4))&0xf);
86 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
90 for (i = 0; i < JOY_NUM_AXES; i++)
92 axis_min[i] = joystick.axis_min[i];
93 axis_center[i] = joystick.axis_center[i];
94 axis_max[i] = joystick.axis_max[i];
98 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
102 for (i = 0; i < JOY_NUM_AXES; i++)
104 joystick.axis_min[i] = axis_min[i];
105 joystick.axis_center[i] = axis_center[i];
106 joystick.axis_max[i] = axis_max[i];
111 ubyte joy_get_present_mask() {
112 return joystick.present_mask;
115 void joy_set_timer_rate(int max_value ) {
117 joystick.max_timer = max_value;
121 int joy_get_timer_rate() {
122 return joystick.max_timer;
129 if (!joy_installed) return;
132 for (i = 0; i < JOY_MAX_BUTTONS; i++)
134 joystick.buttons[i].ignore = 0;
135 joystick.buttons[i].state = 0;
136 joystick.buttons[i].timedown = 0;
137 joystick.buttons[i].downcount = 0;
138 joystick.buttons[i].upcount = 0;
145 void joy_handler(int ticks_this_time) {
148 Button_info * button;
150 // joystick.max_timer = ticks_this_time;
152 if ( joystick.slow_read & JOY_BIOS_READINGS ) {
153 joystick.read_count++;
154 if ( joystick.read_count > 7 ) {
155 joystick.read_count = 0;
156 value = joy_read_buttons_bios2();
157 joystick.last_value = value;
159 value = joystick.last_value;
162 value = joy_read_buttons(); //JOY_READ_BUTTONS;
165 for (i = 0; i < JOY_MAX_BUTTONS; i++)
167 button = &joystick.buttons[i];
168 if (!button->ignore) {
170 state = (value >> i) & 1;
171 else if (i==(value+4))
176 if ( button->last_state == state ) {
177 if (state) button->timedown += ticks_this_time;
180 button->downcount += state;
183 button->upcount += button->state;
186 button->last_state = state;
192 void joy_handler_end() { // Dummy function to help calculate size of joystick handler function
196 ubyte joy_read_raw_buttons() {
197 if ( joystick.slow_read & JOY_BIOS_READINGS )
198 return joy_read_buttons_bios2();
200 return joy_read_buttons(); //JOY_READ_BUTTONS;
203 void joy_set_slow_reading(int flag)
205 joystick.slow_read |= flag;
209 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
211 ubyte read_masks, org_masks;
212 int t, t1, t2, buffer[4*2+2];
213 int e, i, num_channels, c;
215 axis[0] = 0; axis[1] = 0;
216 axis[2] = 0; axis[3] = 0;
218 if (!joy_installed) return 0;
223 mask &= joystick.present_mask; // Don't read non-present channels
225 return 0; // Don't read if no stick connected.
228 if ( joystick.slow_read & JOY_SLOW_READINGS ) {
229 for (c=0; c<4; c++ ) {
230 if ( mask & (1 << c)) {
231 // Time out at (1/100th of a second)
233 if ( joystick.slow_read & JOY_POLLED_READINGS )
234 num_channels = joy_read_stick_polled2( (1 << c), buffer, 65536 );
235 else if ( joystick.slow_read & JOY_BIOS_READINGS )
236 num_channels = joy_read_stick_bios2( (1 << c), buffer, 65536 );
237 else if ( joystick.slow_read & JOY_FRIENDLY_READINGS )
238 num_channels = joy_read_stick_friendly2( (1 << c), buffer, (1193180/100) );
240 num_channels = joy_read_stick_asm2( (1 << c), buffer, (1193180/100) );
242 if ( num_channels > 0 ) {
246 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
252 t = t1 + joystick.max_timer - t2;
253 //mprintf( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t );
257 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
258 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
259 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
260 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
265 // Time out at (1/100th of a second)
266 if ( joystick.slow_read & JOY_POLLED_READINGS )
267 num_channels = joy_read_stick_polled2( mask, buffer, 65536 );
268 else if ( joystick.slow_read & JOY_BIOS_READINGS )
269 num_channels = joy_read_stick_bios2( mask, buffer, 65536 );
270 else if ( joystick.slow_read & JOY_FRIENDLY_READINGS )
271 num_channels = joy_read_stick_friendly2( mask, buffer, (1193180/100) );
273 num_channels = joy_read_stick_asm2( mask, buffer, (1193180/100) );
274 //mprintf(( 0, "(%d)\n", num_channels ));
276 for (i=0; i<num_channels; i++ ) {
280 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
286 t = t1 + joystick.max_timer - t2;
287 //mprintf(( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t ));
292 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
293 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
294 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
295 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
312 // if(FindArg("-joy209"))
313 // use_alt_joyport=1;
314 if(FindArg("-joy209"))
315 JOY_PORT = 521; //209h;
320 for (i = 0; i < JOY_MAX_BUTTONS; i++)
321 joystick.buttons[i].last_state = 0;
324 if ( !joy_installed ) {
327 //joystick.max_timer = 65536;
328 joystick.slow_read = 0;
329 joystick.read_count = 0;
330 joystick.last_value = 0;
332 //--------------- lock everything for the virtal memory ----------------------------------
333 if (!dpmi_lock_region ((void near *)joy_read_buttons_bios2, (char *)joy_read_buttons_bios_end2 - (char near *)joy_read_buttons_bios2)) {
334 printf( "Error locking joystick handler (read bios)!\n" );
340 if (!dpmi_lock_region ((void near *)joy_handler, (char *)joy_handler_end - (char near *)joy_handler)) {
341 printf( "Error locking joystick handler!\n" );
345 if (!dpmi_lock_region (&joystick, sizeof(Joy_info))) {
346 printf( "Error locking joystick handler's data!\n" );
350 timer_set_joyhandler(joy_handler);
353 // Do initial cheapy calibration...
354 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
356 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, temp_axis );
357 } while( joy_bogus_reading );
359 if ( joystick.present_mask & 3 )
369 if (!joy_installed) return;
375 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
377 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_min );
378 } while( joy_bogus_reading );
379 if ( joystick.present_mask & 3 )
387 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
389 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_max );
390 } while( joy_bogus_reading );
392 if ( joystick.present_mask & 3 )
400 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
402 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_center );
403 } while( joy_bogus_reading );
405 if ( joystick.present_mask & 3 )
411 void joy_set_cen_fake(int channel)
415 int minx, maxx, cenx;
419 for (i=0; i<4; i++ ) {
420 if ( (joystick.present_mask & (1<<i)) && (i!=channel) ) {
422 minx += joystick.axis_min[i];
423 maxx += joystick.axis_max[i];
424 cenx += joystick.axis_center[i];
431 joystick.axis_min[channel] = minx;
432 joystick.axis_max[channel] = maxx;
433 joystick.axis_center[channel] = cenx;
436 int joy_get_scaled_reading( int raw, int axn )
440 // Make sure it's calibrated properly.
441 //added/changed on 8/14/98 to allow smaller calibrating sticks to work (by Eivind Brendryen)--was <5
442 if ( joystick.axis_center[axn] - joystick.axis_min[axn] < 2 )
444 if ( joystick.axis_max[axn] - joystick.axis_center[axn] < 2 )
446 //end change - Victor Rachels (by Eivind Brendryen)
448 raw -= joystick.axis_center[axn];
451 d = joystick.axis_center[axn]-joystick.axis_min[axn];
453 d = joystick.axis_max[axn]-joystick.axis_center[axn];
467 //added on 4/13/99 by Victor Rachels to add deadzone control
468 dz = (joy_deadzone) * 6;
469 if ((x > (-1*dz)) && (x < dz))
471 //end this section addition -VR
476 int last_reading[4] = { 0, 0, 0, 0 };
478 void joy_get_pos( int *x, int *y )
483 if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
485 flags=joystick_read_raw_axis( JOY_1_X_AXIS+JOY_1_Y_AXIS, axis );
487 if ( joy_bogus_reading ) {
488 axis[0] = last_reading[0];
489 axis[1] = last_reading[1];
490 flags = JOY_1_X_AXIS+JOY_1_Y_AXIS;
492 last_reading[0] = axis[0];
493 last_reading[1] = axis[1];
496 if ( flags & JOY_1_X_AXIS )
497 *x = joy_get_scaled_reading( axis[0], 0 );
501 if ( flags & JOY_1_Y_AXIS )
502 *y = joy_get_scaled_reading( axis[1], 1 );
507 ubyte joy_read_stick( ubyte masks, int *axis )
512 if ((!joy_installed)||(!joy_present)) {
513 axis[0] = 0; axis[1] = 0;
514 axis[2] = 0; axis[3] = 0;
518 flags=joystick_read_raw_axis( masks, raw_axis );
520 if ( joy_bogus_reading ) {
521 axis[0] = last_reading[0];
522 axis[1] = last_reading[1];
523 axis[2] = last_reading[2];
524 axis[3] = last_reading[3];
527 last_reading[0] = axis[0];
528 last_reading[1] = axis[1];
529 last_reading[2] = axis[2];
530 last_reading[3] = axis[3];
533 if ( flags & JOY_1_X_AXIS )
534 axis[0] = joy_get_scaled_reading( raw_axis[0], 0 );
538 if ( flags & JOY_1_Y_AXIS )
539 axis[1] = joy_get_scaled_reading( raw_axis[1], 1 );
543 if ( flags & JOY_2_X_AXIS )
544 axis[2] = joy_get_scaled_reading( raw_axis[2], 2 );
548 if ( flags & JOY_2_Y_AXIS )
549 axis[3] = joy_get_scaled_reading( raw_axis[3], 3 );
559 if ((!joy_installed)||(!joy_present)) return 0;
561 return joy_read_raw_buttons();
564 void joy_get_btn_down_cnt( int *btn0, int *btn1 )
566 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
569 *btn0 = joystick.buttons[0].downcount;
570 joystick.buttons[0].downcount = 0;
571 *btn1 = joystick.buttons[1].downcount;
572 joystick.buttons[1].downcount = 0;
576 int joy_get_button_state( int btn )
580 if ((!joy_installed)||(!joy_present)) return 0;
582 if (btn >= JOY_MAX_BUTTONS)
586 count = joystick.buttons[btn].state;
592 int joy_get_button_up_cnt( int btn )
596 if ((!joy_installed)||(!joy_present)) return 0;
598 if (btn >= JOY_MAX_BUTTONS)
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 >= JOY_MAX_BUTTONS)
618 count = joystick.buttons[btn].downcount;
619 joystick.buttons[btn].downcount = 0;
626 fix joy_get_button_down_time( int btn )
630 if ((!joy_installed)||(!joy_present)) return 0;
631 if (btn >= JOY_MAX_BUTTONS)
635 count = joystick.buttons[btn].timedown;
636 joystick.buttons[btn].timedown = 0;
639 return fixmuldiv(count, 65536, 1193180 );
642 void joy_get_btn_up_cnt( int *btn0, int *btn1 )
644 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
647 *btn0 = joystick.buttons[0].upcount;
648 joystick.buttons[0].upcount = 0;
649 *btn1 = joystick.buttons[1].upcount;
650 joystick.buttons[1].upcount = 0;
654 void joy_set_btn_values( int btn, int state, fix timedown, int downcount, int upcount )
657 joystick.buttons[btn].ignore = 1;
658 joystick.buttons[btn].state = state;
659 joystick.buttons[btn].timedown = fixmuldiv( timedown, 1193180, 65536 );
660 joystick.buttons[btn].downcount = downcount;
661 joystick.buttons[btn].upcount = upcount;
667 if ( joystick.slow_read & JOY_BIOS_READINGS )
668 joystick.last_value = joy_read_buttons_bios2();