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.
32 extern int joy_bogus_reading;
33 int JOY_PORT = 513; //201h;
36 int joy_read_stick_asm2( int read_masks, int * event_buffer, int timeout );
37 int joy_read_stick_friendly2( int read_masks, int * event_buffer, int timeout );
38 int joy_read_stick_polled2( int read_masks, int * event_buffer, int timeout );
39 int joy_read_stick_bios2( int read_masks, int * event_buffer, int timeout );
40 int joy_read_buttons_bios2();
41 void joy_read_buttons_bios_end2();
46 // edi = pointer to buffer
47 // returns number of events
49 char joy_installed = 0;
52 typedef struct Button_info {
61 typedef struct Joy_info {
67 Button_info buttons[MAX_BUTTONS];
75 ubyte joy_read_buttons()
77 return ((~(inp(JOY_PORT) >> 4))&0xf);
80 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
85 axis_min[i] = joystick.axis_min[i];
86 axis_center[i] = joystick.axis_center[i];
87 axis_max[i] = joystick.axis_max[i];
91 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
96 joystick.axis_min[i] = axis_min[i];
97 joystick.axis_center[i] = axis_center[i];
98 joystick.axis_max[i] = axis_max[i];
103 ubyte joy_get_present_mask() {
104 return joystick.present_mask;
107 void joy_set_timer_rate(int max_value ) {
109 joystick.max_timer = max_value;
113 int joy_get_timer_rate() {
114 return joystick.max_timer;
121 if (!joy_installed) return;
124 for (i=0; i<MAX_BUTTONS; i++ ) {
125 joystick.buttons[i].ignore = 0;
126 joystick.buttons[i].state = 0;
127 joystick.buttons[i].timedown = 0;
128 joystick.buttons[i].downcount = 0;
129 joystick.buttons[i].upcount = 0;
136 void joy_handler(int ticks_this_time) {
139 Button_info * button;
141 // joystick.max_timer = ticks_this_time;
143 if ( joystick.slow_read & JOY_BIOS_READINGS ) {
144 joystick.read_count++;
145 if ( joystick.read_count > 7 ) {
146 joystick.read_count = 0;
147 value = joy_read_buttons_bios2();
148 joystick.last_value = value;
150 value = joystick.last_value;
153 value = joy_read_buttons(); //JOY_READ_BUTTONS;
156 for (i=0; i<MAX_BUTTONS; i++ ) {
157 button = &joystick.buttons[i];
158 if (!button->ignore) {
160 state = (value >> i) & 1;
161 else if (i==(value+4))
166 if ( button->last_state == state ) {
167 if (state) button->timedown += ticks_this_time;
170 button->downcount += state;
173 button->upcount += button->state;
176 button->last_state = state;
182 void joy_handler_end() { // Dummy function to help calculate size of joystick handler function
186 ubyte joy_read_raw_buttons() {
187 if ( joystick.slow_read & JOY_BIOS_READINGS )
188 return joy_read_buttons_bios2();
190 return joy_read_buttons(); //JOY_READ_BUTTONS;
193 void joy_set_slow_reading(int flag)
195 joystick.slow_read |= flag;
199 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
201 ubyte read_masks, org_masks;
202 int t, t1, t2, buffer[4*2+2];
203 int e, i, num_channels, c;
205 axis[0] = 0; axis[1] = 0;
206 axis[2] = 0; axis[3] = 0;
208 if (!joy_installed) return 0;
213 mask &= joystick.present_mask; // Don't read non-present channels
215 return 0; // Don't read if no stick connected.
218 if ( joystick.slow_read & JOY_SLOW_READINGS ) {
219 for (c=0; c<4; c++ ) {
220 if ( mask & (1 << c)) {
221 // Time out at (1/100th of a second)
223 if ( joystick.slow_read & JOY_POLLED_READINGS )
224 num_channels = joy_read_stick_polled2( (1 << c), buffer, 65536 );
225 else if ( joystick.slow_read & JOY_BIOS_READINGS )
226 num_channels = joy_read_stick_bios2( (1 << c), buffer, 65536 );
227 else if ( joystick.slow_read & JOY_FRIENDLY_READINGS )
228 num_channels = joy_read_stick_friendly2( (1 << c), buffer, (1193180/100) );
230 num_channels = joy_read_stick_asm2( (1 << c), buffer, (1193180/100) );
232 if ( num_channels > 0 ) {
236 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
242 t = t1 + joystick.max_timer - t2;
243 //mprintf( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t );
247 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
248 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
249 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
250 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
255 // Time out at (1/100th of a second)
256 if ( joystick.slow_read & JOY_POLLED_READINGS )
257 num_channels = joy_read_stick_polled2( mask, buffer, 65536 );
258 else if ( joystick.slow_read & JOY_BIOS_READINGS )
259 num_channels = joy_read_stick_bios2( mask, buffer, 65536 );
260 else if ( joystick.slow_read & JOY_FRIENDLY_READINGS )
261 num_channels = joy_read_stick_friendly2( mask, buffer, (1193180/100) );
263 num_channels = joy_read_stick_asm2( mask, buffer, (1193180/100) );
264 //mprintf(( 0, "(%d)\n", num_channels ));
266 for (i=0; i<num_channels; i++ ) {
270 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
276 t = t1 + joystick.max_timer - t2;
277 //mprintf(( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t ));
282 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
283 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
284 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
285 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
302 // if(FindArg("-joy209"))
303 // use_alt_joyport=1;
304 if(FindArg("-joy209"))
305 JOY_PORT = 521; //209h;
310 for (i=0; i<MAX_BUTTONS; i++ )
311 joystick.buttons[i].last_state = 0;
314 if ( !joy_installed ) {
317 //joystick.max_timer = 65536;
318 joystick.slow_read = 0;
319 joystick.read_count = 0;
320 joystick.last_value = 0;
322 //--------------- lock everything for the virtal memory ----------------------------------
323 if (!dpmi_lock_region ((void near *)joy_read_buttons_bios2, (char *)joy_read_buttons_bios_end2 - (char near *)joy_read_buttons_bios2)) {
324 printf( "Error locking joystick handler (read bios)!\n" );
330 if (!dpmi_lock_region ((void near *)joy_handler, (char *)joy_handler_end - (char near *)joy_handler)) {
331 printf( "Error locking joystick handler!\n" );
335 if (!dpmi_lock_region (&joystick, sizeof(Joy_info))) {
336 printf( "Error locking joystick handler's data!\n" );
340 timer_set_joyhandler(joy_handler);
343 // Do initial cheapy calibration...
344 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
346 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, temp_axis );
347 } while( joy_bogus_reading );
349 if ( joystick.present_mask & 3 )
359 if (!joy_installed) return;
365 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
367 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_min );
368 } while( joy_bogus_reading );
369 if ( joystick.present_mask & 3 )
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_max );
380 } while( joy_bogus_reading );
382 if ( joystick.present_mask & 3 )
390 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
392 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_center );
393 } while( joy_bogus_reading );
395 if ( joystick.present_mask & 3 )
401 void joy_set_cen_fake(int channel)
405 int minx, maxx, cenx;
409 for (i=0; i<4; i++ ) {
410 if ( (joystick.present_mask & (1<<i)) && (i!=channel) ) {
412 minx += joystick.axis_min[i];
413 maxx += joystick.axis_max[i];
414 cenx += joystick.axis_center[i];
421 joystick.axis_min[channel] = minx;
422 joystick.axis_max[channel] = maxx;
423 joystick.axis_center[channel] = cenx;
426 int joy_get_scaled_reading( int raw, int axn )
430 // Make sure it's calibrated properly.
431 //added/changed on 8/14/98 to allow smaller calibrating sticks to work (by Eivind Brendryen)--was <5
432 if ( joystick.axis_center[axn] - joystick.axis_min[axn] < 2 )
434 if ( joystick.axis_max[axn] - joystick.axis_center[axn] < 2 )
436 //end change - Victor Rachels (by Eivind Brendryen)
438 raw -= joystick.axis_center[axn];
441 d = joystick.axis_center[axn]-joystick.axis_min[axn];
443 d = joystick.axis_max[axn]-joystick.axis_center[axn];
457 //added on 4/13/99 by Victor Rachels to add deadzone control
458 dz = (joy_deadzone) * 6;
459 if ((x > (-1*dz)) && (x < dz))
461 //end this section addition -VR
466 int last_reading[4] = { 0, 0, 0, 0 };
468 void joy_get_pos( int *x, int *y )
473 if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
475 flags=joystick_read_raw_axis( JOY_1_X_AXIS+JOY_1_Y_AXIS, axis );
477 if ( joy_bogus_reading ) {
478 axis[0] = last_reading[0];
479 axis[1] = last_reading[1];
480 flags = JOY_1_X_AXIS+JOY_1_Y_AXIS;
482 last_reading[0] = axis[0];
483 last_reading[1] = axis[1];
486 if ( flags & JOY_1_X_AXIS )
487 *x = joy_get_scaled_reading( axis[0], 0 );
491 if ( flags & JOY_1_Y_AXIS )
492 *y = joy_get_scaled_reading( axis[1], 1 );
497 ubyte joy_read_stick( ubyte masks, int *axis )
502 if ((!joy_installed)||(!joy_present)) {
503 axis[0] = 0; axis[1] = 0;
504 axis[2] = 0; axis[3] = 0;
508 flags=joystick_read_raw_axis( masks, raw_axis );
510 if ( joy_bogus_reading ) {
511 axis[0] = last_reading[0];
512 axis[1] = last_reading[1];
513 axis[2] = last_reading[2];
514 axis[3] = last_reading[3];
517 last_reading[0] = axis[0];
518 last_reading[1] = axis[1];
519 last_reading[2] = axis[2];
520 last_reading[3] = axis[3];
523 if ( flags & JOY_1_X_AXIS )
524 axis[0] = joy_get_scaled_reading( raw_axis[0], 0 );
528 if ( flags & JOY_1_Y_AXIS )
529 axis[1] = joy_get_scaled_reading( raw_axis[1], 1 );
533 if ( flags & JOY_2_X_AXIS )
534 axis[2] = joy_get_scaled_reading( raw_axis[2], 2 );
538 if ( flags & JOY_2_Y_AXIS )
539 axis[3] = joy_get_scaled_reading( raw_axis[3], 3 );
549 if ((!joy_installed)||(!joy_present)) return 0;
551 return joy_read_raw_buttons();
554 void joy_get_btn_down_cnt( int *btn0, int *btn1 )
556 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
559 *btn0 = joystick.buttons[0].downcount;
560 joystick.buttons[0].downcount = 0;
561 *btn1 = joystick.buttons[1].downcount;
562 joystick.buttons[1].downcount = 0;
566 int joy_get_button_state( int btn )
570 if ((!joy_installed)||(!joy_present)) return 0;
572 if ( btn >= MAX_BUTTONS ) return 0;
575 count = joystick.buttons[btn].state;
581 int joy_get_button_up_cnt( int btn )
585 if ((!joy_installed)||(!joy_present)) return 0;
587 if ( btn >= MAX_BUTTONS ) return 0;
590 count = joystick.buttons[btn].upcount;
591 joystick.buttons[btn].upcount = 0;
597 int joy_get_button_down_cnt( int btn )
601 if ((!joy_installed)||(!joy_present)) return 0;
602 if ( btn >= MAX_BUTTONS ) return 0;
605 count = joystick.buttons[btn].downcount;
606 joystick.buttons[btn].downcount = 0;
613 fix joy_get_button_down_time( int btn )
617 if ((!joy_installed)||(!joy_present)) return 0;
618 if ( btn >= MAX_BUTTONS ) return 0;
621 count = joystick.buttons[btn].timedown;
622 joystick.buttons[btn].timedown = 0;
625 return fixmuldiv(count, 65536, 1193180 );
628 void joy_get_btn_up_cnt( int *btn0, int *btn1 )
630 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
633 *btn0 = joystick.buttons[0].upcount;
634 joystick.buttons[0].upcount = 0;
635 *btn1 = joystick.buttons[1].upcount;
636 joystick.buttons[1].upcount = 0;
640 void joy_set_btn_values( int btn, int state, fix timedown, int downcount, int upcount )
643 joystick.buttons[btn].ignore = 1;
644 joystick.buttons[btn].state = state;
645 joystick.buttons[btn].timedown = fixmuldiv( timedown, 1193180, 65536 );
646 joystick.buttons[btn].downcount = downcount;
647 joystick.buttons[btn].upcount = upcount;
653 if ( joystick.slow_read & JOY_BIOS_READINGS )
654 joystick.last_value = joy_read_buttons_bios2();
657 #endif // __ENV_DJGPP__