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-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
31 // edi = pointer to buffer
32 // returns number of events
33 int joy_read_stick_asm( int read_masks, int * event_buffer, int timeout );
34 #pragma aux joy_read_stick_asm parm [ebx] [edi] [ecx] value [eax] modify exact [eax ebx ecx edx edi];
36 int joy_read_stick_polled( int read_masks, int * event_buffer, int timeout );
37 #pragma aux joy_read_stick_polled parm [ebx] [edi] [ecx] value [eax] modify exact [eax ebx ecx edx edi];
39 int joy_read_stick_bios( int read_masks, int * event_buffer, int timeout );
40 #pragma aux joy_read_stick_bios parm [ebx] [edi] [ecx] value [eax] modify exact [eax ebx ecx edx edi];
43 char joy_installed = 0;
46 #define JOY_READ_BUTTONS ((~(inp(0x201) >> 4))&0xf)
48 #define JOY_READ_BUTTONS_ARCADE (~(inp(0x2A1)))
49 #define JOY_MAX_BUTTONS 28
51 #define JOY_MAX_BUTTONS 20
54 typedef struct Button_info {
63 typedef struct Joy_info {
69 Button_info buttons[JOY_MAX_BUTTONS];
77 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
82 axis_min[i] = joystick.axis_min[i];
83 axis_center[i] = joystick.axis_center[i];
84 axis_max[i] = joystick.axis_max[i];
88 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
93 joystick.axis_min[i] = axis_min[i];
94 joystick.axis_center[i] = axis_center[i];
95 joystick.axis_max[i] = axis_max[i];
100 ubyte joy_get_present_mask() {
101 return joystick.present_mask;
104 void joy_set_timer_rate(int max_value ) {
106 joystick.max_timer = max_value;
110 int joy_get_timer_rate() {
111 return joystick.max_timer;
118 if (!joy_installed) return;
121 for (i = 0; i < JOY_MAX_BUTTONS; i++)
123 joystick.buttons[i].ignore = 0;
124 joystick.buttons[i].state = 0;
125 joystick.buttons[i].timedown = 0;
126 joystick.buttons[i].downcount = 0;
127 joystick.buttons[i].upcount = 0;
133 #pragma off (check_stack)
135 extern int joy_read_buttons_bios();
137 void joy_handler(int ticks_this_time) {
143 Button_info * button;
145 joystick.max_timer = ticks_this_time;
147 if ( joystick.slow_read & JOY_BIOS_READINGS ) {
148 joystick.read_count++;
149 if ( joystick.read_count > 7 ) {
150 joystick.read_count = 0;
151 value = joy_read_buttons_bios();
152 joystick.last_value = value;
154 value = joystick.last_value;
157 value = JOY_READ_BUTTONS;
159 valuea = JOY_READ_BUTTONS_ARCADE;
163 for (i = 0; i < JOY_MAX_BUTTONS; i++)
165 button = &joystick.buttons[i];
166 if (!button->ignore) {
168 state = (value >> i) & 1;
171 state = (valuea >> (i-20)) & 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
197 #pragma off (check_stack)
199 ubyte joy_read_raw_buttons() {
200 if ( joystick.slow_read & JOY_BIOS_READINGS )
201 return joy_read_buttons_bios();
203 return JOY_READ_BUTTONS;
206 void joy_set_slow_reading(int flag)
208 joystick.slow_read |= flag;
212 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
214 ubyte read_masks, org_masks;
215 int t, t1, t2, buffer[4*2+2];
216 int e, i, num_channels, c;
218 axis[0] = 0; axis[1] = 0;
219 axis[2] = 0; axis[3] = 0;
221 if (!joy_installed) return 0;
226 mask &= joystick.present_mask; // Don't read non-present channels
228 return 0; // Don't read if no stick connected.
231 if ( joystick.slow_read & JOY_SLOW_READINGS ) {
232 for (c=0; c<4; c++ ) {
233 if ( mask & (1 << c)) {
234 // Time out at (1/100th of a second)
236 if ( joystick.slow_read & JOY_POLLED_READINGS )
237 num_channels = joy_read_stick_polled( (1 << c), buffer, 65536 );
238 else if ( joystick.slow_read & JOY_BIOS_READINGS )
239 num_channels = joy_read_stick_bios( (1 << c), buffer, 65536 );
241 num_channels = joy_read_stick_asm( (1 << c), buffer, (1193180/100) );
243 if ( num_channels > 0 ) {
247 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
253 t = t1 + joystick.max_timer - t2;
254 //mprintf( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t );
258 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
259 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
260 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
261 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
266 // Time out at (1/100th of a second)
267 if ( joystick.slow_read & JOY_POLLED_READINGS )
268 num_channels = joy_read_stick_polled( mask, buffer, 65536 );
269 else if ( joystick.slow_read & JOY_BIOS_READINGS )
270 num_channels = joy_read_stick_bios( (1 << c), buffer, 65536 );
272 num_channels = joy_read_stick_asm( mask, buffer, (1193180/100) );
273 //mprintf(( 0, "(%d)\n", num_channels ));
275 for (i=0; i<num_channels; i++ ) {
279 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
285 t = t1 + joystick.max_timer - t2;
286 //mprintf(( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t ));
291 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
292 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
293 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
294 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
302 extern void timer_set_joyhandler( void (*joy_handler)() );
312 for (i = 0; i < JOY_MAX_BUTTONS; i++)
313 joystick.buttons[i].last_state = 0;
316 if ( !joy_installed ) {
319 joystick.max_timer = 65536;
320 joystick.slow_read = 0;
321 joystick.read_count = 0;
322 joystick.last_value = 0;
324 //--------------- lock everything for the virtal memory ----------------------------------
325 if (!dpmi_lock_region ((void near *)joy_handler, (char *)joy_handler_end - (char near *)joy_handler)) {
326 Error( "Can't lock joystick handler!\n" );
329 if (!dpmi_lock_region (&joystick, sizeof(Joy_info))) {
330 Error( "Can't lock joystick handler's data!\n" );
333 timer_set_joyhandler(joy_handler);
336 // Do initial cheapy calibration...
337 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
338 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, temp_axis );
340 if ( joystick.present_mask & 3 )
350 if (!joy_installed) return;
356 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
357 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_min );
358 if ( joystick.present_mask & 3 )
366 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
367 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_max );
368 if ( joystick.present_mask & 3 )
376 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
377 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_center );
378 if ( joystick.present_mask & 3 )
384 void joy_set_cen_fake(int channel)
388 int minx, maxx, cenx;
392 for (i=0; i<4; i++ ) {
393 if ( (joystick.present_mask & (1<<i)) && (i!=channel) ) {
395 minx += joystick.axis_min[i];
396 maxx += joystick.axis_max[i];
397 cenx += joystick.axis_center[i];
404 joystick.axis_min[channel] = minx;
405 joystick.axis_max[channel] = maxx;
406 joystick.axis_center[channel] = cenx;
409 int joy_get_scaled_reading( int raw, int axn )
413 // Make sure it's calibrated properly.
414 if ( joystick.axis_center[axn] - joystick.axis_min[axn] < 5 ) return 0;
415 if ( joystick.axis_max[axn] - joystick.axis_center[axn] < 5 ) return 0;
417 raw -= joystick.axis_center[axn];
420 d = joystick.axis_center[axn]-joystick.axis_min[axn];
422 d = joystick.axis_max[axn]-joystick.axis_center[axn];
430 if ( x < -128 ) x = -128;
431 if ( x > 127 ) x = 127;
436 void joy_get_pos( int *x, int *y )
441 if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
443 flags=joystick_read_raw_axis( JOY_1_X_AXIS+JOY_1_Y_AXIS, axis );
445 if ( flags & JOY_1_X_AXIS )
446 *x = joy_get_scaled_reading( axis[0], 0 );
450 if ( flags & JOY_1_Y_AXIS )
451 *y = joy_get_scaled_reading( axis[1], 1 );
456 ubyte joy_read_stick( ubyte masks, int *axis )
461 if ((!joy_installed)||(!joy_present)) {
462 axis[0] = 0; axis[1] = 0;
463 axis[2] = 0; axis[3] = 0;
467 flags=joystick_read_raw_axis( masks, raw_axis );
469 if ( flags & JOY_1_X_AXIS )
470 axis[0] = joy_get_scaled_reading( raw_axis[0], 0 );
474 if ( flags & JOY_1_Y_AXIS )
475 axis[1] = joy_get_scaled_reading( raw_axis[1], 1 );
479 if ( flags & JOY_2_X_AXIS )
480 axis[2] = joy_get_scaled_reading( raw_axis[2], 2 );
484 if ( flags & JOY_2_Y_AXIS )
485 axis[3] = joy_get_scaled_reading( raw_axis[3], 3 );
495 if ((!joy_installed)||(!joy_present)) return 0;
497 return joy_read_raw_buttons();
500 void joy_get_btn_down_cnt( int *btn0, int *btn1 )
502 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
505 *btn0 = joystick.buttons[0].downcount;
506 joystick.buttons[0].downcount = 0;
507 *btn1 = joystick.buttons[1].downcount;
508 joystick.buttons[1].downcount = 0;
512 int joy_get_button_state( int btn )
516 if ((!joy_installed)||(!joy_present)) return 0;
518 if (btn >= JOY_MAX_BUTTONS)
522 count = joystick.buttons[btn].state;
528 int joy_get_button_up_cnt( int btn )
532 if ((!joy_installed)||(!joy_present)) return 0;
534 if (btn >= JOY_MAX_BUTTONS)
538 count = joystick.buttons[btn].upcount;
539 joystick.buttons[btn].upcount = 0;
545 int joy_get_button_down_cnt( int btn )
549 if ((!joy_installed)||(!joy_present)) return 0;
550 if (btn >= JOY_MAX_BUTTONS)
554 count = joystick.buttons[btn].downcount;
555 joystick.buttons[btn].downcount = 0;
562 fix joy_get_button_down_time( int btn )
566 if ((!joy_installed)||(!joy_present)) return 0;
567 if (btn >= JOY_MAX_BUTTONS)
571 count = joystick.buttons[btn].timedown;
572 joystick.buttons[btn].timedown = 0;
575 return fixmuldiv(count, 65536, 1193180 );
578 void joy_get_btn_up_cnt( int *btn0, int *btn1 )
580 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
583 *btn0 = joystick.buttons[0].upcount;
584 joystick.buttons[0].upcount = 0;
585 *btn1 = joystick.buttons[1].upcount;
586 joystick.buttons[1].upcount = 0;
590 void joy_set_btn_values( int btn, int state, fix timedown, int downcount, int upcount )
593 joystick.buttons[btn].ignore = 1;
594 joystick.buttons[btn].state = state;
595 joystick.buttons[btn].timedown = fixmuldiv( timedown, 1193180, 65536 );
596 joystick.buttons[btn].downcount = downcount;
597 joystick.buttons[btn].upcount = upcount;
603 if ( joystick.slow_read & JOY_BIOS_READINGS )
604 joystick.last_value = joy_read_buttons_bios();