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.
15 #pragma off (unreferenced)
16 static char rcsid[] = "$Id: joyc.c,v 1.1.1.1 2001-01-19 03:30:14 bradleyb Exp $";
17 #pragma on (unreferenced)
35 // edi = pointer to buffer
36 // returns number of events
37 int joy_read_stick_asm( int read_masks, int * event_buffer, int timeout );
38 #pragma aux joy_read_stick_asm parm [ebx] [edi] [ecx] value [eax] modify exact [eax ebx ecx edx edi];
40 int joy_read_stick_polled( int read_masks, int * event_buffer, int timeout );
41 #pragma aux joy_read_stick_polled parm [ebx] [edi] [ecx] value [eax] modify exact [eax ebx ecx edx edi];
43 int joy_read_stick_bios( int read_masks, int * event_buffer, int timeout );
44 #pragma aux joy_read_stick_bios parm [ebx] [edi] [ecx] value [eax] modify exact [eax ebx ecx edx edi];
47 char joy_installed = 0;
50 #define JOY_READ_BUTTONS ((~(inp(0x201) >> 4))&0xf)
52 #define JOY_READ_BUTTONS_ARCADE (~(inp(0x2A1)))
53 #define MAX_BUTTONS 28
55 #define MAX_BUTTONS 20
58 typedef struct Button_info {
67 typedef struct Joy_info {
73 Button_info buttons[MAX_BUTTONS];
81 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
86 axis_min[i] = joystick.axis_min[i];
87 axis_center[i] = joystick.axis_center[i];
88 axis_max[i] = joystick.axis_max[i];
92 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
97 joystick.axis_min[i] = axis_min[i];
98 joystick.axis_center[i] = axis_center[i];
99 joystick.axis_max[i] = axis_max[i];
104 ubyte joy_get_present_mask() {
105 return joystick.present_mask;
108 void joy_set_timer_rate(int max_value ) {
110 joystick.max_timer = max_value;
114 int joy_get_timer_rate() {
115 return joystick.max_timer;
122 if (!joy_installed) return;
125 for (i=0; i<MAX_BUTTONS; i++ ) {
126 joystick.buttons[i].ignore = 0;
127 joystick.buttons[i].state = 0;
128 joystick.buttons[i].timedown = 0;
129 joystick.buttons[i].downcount = 0;
130 joystick.buttons[i].upcount = 0;
136 #pragma off (check_stack)
138 extern int joy_read_buttons_bios();
140 void joy_handler(int ticks_this_time) {
146 Button_info * button;
148 joystick.max_timer = ticks_this_time;
150 if ( joystick.slow_read & JOY_BIOS_READINGS ) {
151 joystick.read_count++;
152 if ( joystick.read_count > 7 ) {
153 joystick.read_count = 0;
154 value = joy_read_buttons_bios();
155 joystick.last_value = value;
157 value = joystick.last_value;
160 value = JOY_READ_BUTTONS;
162 valuea = JOY_READ_BUTTONS_ARCADE;
166 for (i=0; i<MAX_BUTTONS; i++ ) {
167 button = &joystick.buttons[i];
168 if (!button->ignore) {
170 state = (value >> i) & 1;
173 state = (valuea >> (i-20)) & 1;
175 else if (i==(value+4))
180 if ( button->last_state == state ) {
181 if (state) button->timedown += ticks_this_time;
184 button->downcount += state;
187 button->upcount += button->state;
190 button->last_state = state;
196 void joy_handler_end() { // Dummy function to help calculate size of joystick handler function
199 #pragma off (check_stack)
201 ubyte joy_read_raw_buttons() {
202 if ( joystick.slow_read & JOY_BIOS_READINGS )
203 return joy_read_buttons_bios();
205 return JOY_READ_BUTTONS;
208 void joy_set_slow_reading(int flag)
210 joystick.slow_read |= flag;
214 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
216 ubyte read_masks, org_masks;
217 int t, t1, t2, buffer[4*2+2];
218 int e, i, num_channels, c;
220 axis[0] = 0; axis[1] = 0;
221 axis[2] = 0; axis[3] = 0;
223 if (!joy_installed) return 0;
228 mask &= joystick.present_mask; // Don't read non-present channels
230 return 0; // Don't read if no stick connected.
233 if ( joystick.slow_read & JOY_SLOW_READINGS ) {
234 for (c=0; c<4; c++ ) {
235 if ( mask & (1 << c)) {
236 // Time out at (1/100th of a second)
238 if ( joystick.slow_read & JOY_POLLED_READINGS )
239 num_channels = joy_read_stick_polled( (1 << c), buffer, 65536 );
240 else if ( joystick.slow_read & JOY_BIOS_READINGS )
241 num_channels = joy_read_stick_bios( (1 << c), buffer, 65536 );
243 num_channels = joy_read_stick_asm( (1 << c), buffer, (1193180/100) );
245 if ( num_channels > 0 ) {
249 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
255 t = t1 + joystick.max_timer - t2;
256 //mprintf( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t );
260 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
261 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
262 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
263 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
268 // Time out at (1/100th of a second)
269 if ( joystick.slow_read & JOY_POLLED_READINGS )
270 num_channels = joy_read_stick_polled( mask, buffer, 65536 );
271 else if ( joystick.slow_read & JOY_BIOS_READINGS )
272 num_channels = joy_read_stick_bios( (1 << c), buffer, 65536 );
274 num_channels = joy_read_stick_asm( mask, buffer, (1193180/100) );
275 //mprintf(( 0, "(%d)\n", num_channels ));
277 for (i=0; i<num_channels; i++ ) {
281 if ( joystick.slow_read & (JOY_POLLED_READINGS|JOY_BIOS_READINGS) ) {
287 t = t1 + joystick.max_timer - t2;
288 //mprintf(( 0, "%d, %d, %d, %d\n", t1, t2, joystick.max_timer, t ));
293 if ( e & 1 ) { axis[0] = t; read_masks |= 1; }
294 if ( e & 2 ) { axis[1] = t; read_masks |= 2; }
295 if ( e & 4 ) { axis[2] = t; read_masks |= 4; }
296 if ( e & 8 ) { axis[3] = t; read_masks |= 8; }
304 extern void timer_set_joyhandler( void (*joy_handler)() );
314 for (i=0; i<MAX_BUTTONS; i++ )
315 joystick.buttons[i].last_state = 0;
318 if ( !joy_installed ) {
321 joystick.max_timer = 65536;
322 joystick.slow_read = 0;
323 joystick.read_count = 0;
324 joystick.last_value = 0;
326 //--------------- lock everything for the virtal memory ----------------------------------
327 if (!dpmi_lock_region ((void near *)joy_handler, (char *)joy_handler_end - (char near *)joy_handler)) {
328 Error( "Can't lock joystick handler!\n" );
331 if (!dpmi_lock_region (&joystick, sizeof(Joy_info))) {
332 Error( "Can't lock joystick handler's data!\n" );
335 timer_set_joyhandler(joy_handler);
338 // Do initial cheapy calibration...
339 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
340 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, temp_axis );
342 if ( joystick.present_mask & 3 )
352 if (!joy_installed) return;
358 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
359 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_min );
360 if ( joystick.present_mask & 3 )
368 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
369 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_max );
370 if ( joystick.present_mask & 3 )
378 joystick.present_mask = JOY_ALL_AXIS; // Assume they're all present
379 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS, joystick.axis_center );
380 if ( joystick.present_mask & 3 )
386 void joy_set_cen_fake(int channel)
390 int minx, maxx, cenx;
394 for (i=0; i<4; i++ ) {
395 if ( (joystick.present_mask & (1<<i)) && (i!=channel) ) {
397 minx += joystick.axis_min[i];
398 maxx += joystick.axis_max[i];
399 cenx += joystick.axis_center[i];
406 joystick.axis_min[channel] = minx;
407 joystick.axis_max[channel] = maxx;
408 joystick.axis_center[channel] = cenx;
411 int joy_get_scaled_reading( int raw, int axn )
415 // Make sure it's calibrated properly.
416 if ( joystick.axis_center[axn] - joystick.axis_min[axn] < 5 ) return 0;
417 if ( joystick.axis_max[axn] - joystick.axis_center[axn] < 5 ) return 0;
419 raw -= joystick.axis_center[axn];
422 d = joystick.axis_center[axn]-joystick.axis_min[axn];
424 d = joystick.axis_max[axn]-joystick.axis_center[axn];
432 if ( x < -128 ) x = -128;
433 if ( x > 127 ) x = 127;
438 void joy_get_pos( int *x, int *y )
443 if ((!joy_installed)||(!joy_present)) { *x=*y=0; return; }
445 flags=joystick_read_raw_axis( JOY_1_X_AXIS+JOY_1_Y_AXIS, axis );
447 if ( flags & JOY_1_X_AXIS )
448 *x = joy_get_scaled_reading( axis[0], 0 );
452 if ( flags & JOY_1_Y_AXIS )
453 *y = joy_get_scaled_reading( axis[1], 1 );
458 ubyte joy_read_stick( ubyte masks, int *axis )
463 if ((!joy_installed)||(!joy_present)) {
464 axis[0] = 0; axis[1] = 0;
465 axis[2] = 0; axis[3] = 0;
469 flags=joystick_read_raw_axis( masks, raw_axis );
471 if ( flags & JOY_1_X_AXIS )
472 axis[0] = joy_get_scaled_reading( raw_axis[0], 0 );
476 if ( flags & JOY_1_Y_AXIS )
477 axis[1] = joy_get_scaled_reading( raw_axis[1], 1 );
481 if ( flags & JOY_2_X_AXIS )
482 axis[2] = joy_get_scaled_reading( raw_axis[2], 2 );
486 if ( flags & JOY_2_Y_AXIS )
487 axis[3] = joy_get_scaled_reading( raw_axis[3], 3 );
497 if ((!joy_installed)||(!joy_present)) return 0;
499 return joy_read_raw_buttons();
502 void joy_get_btn_down_cnt( int *btn0, int *btn1 )
504 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
507 *btn0 = joystick.buttons[0].downcount;
508 joystick.buttons[0].downcount = 0;
509 *btn1 = joystick.buttons[1].downcount;
510 joystick.buttons[1].downcount = 0;
514 int joy_get_button_state( int btn )
518 if ((!joy_installed)||(!joy_present)) return 0;
520 if ( btn >= MAX_BUTTONS ) return 0;
523 count = joystick.buttons[btn].state;
529 int joy_get_button_up_cnt( int btn )
533 if ((!joy_installed)||(!joy_present)) return 0;
535 if ( btn >= MAX_BUTTONS ) return 0;
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 >= MAX_BUTTONS ) return 0;
553 count = joystick.buttons[btn].downcount;
554 joystick.buttons[btn].downcount = 0;
561 fix joy_get_button_down_time( int btn )
565 if ((!joy_installed)||(!joy_present)) return 0;
566 if ( btn >= MAX_BUTTONS ) return 0;
569 count = joystick.buttons[btn].timedown;
570 joystick.buttons[btn].timedown = 0;
573 return fixmuldiv(count, 65536, 1193180 );
576 void joy_get_btn_up_cnt( int *btn0, int *btn1 )
578 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
581 *btn0 = joystick.buttons[0].upcount;
582 joystick.buttons[0].upcount = 0;
583 *btn1 = joystick.buttons[1].upcount;
584 joystick.buttons[1].upcount = 0;
588 void joy_set_btn_values( int btn, int state, fix timedown, int downcount, int upcount )
591 joystick.buttons[btn].ignore = 1;
592 joystick.buttons[btn].state = state;
593 joystick.buttons[btn].timedown = fixmuldiv( timedown, 1193180, 65536 );
594 joystick.buttons[btn].downcount = downcount;
595 joystick.buttons[btn].upcount = upcount;
601 if ( joystick.slow_read & JOY_BIOS_READINGS )
602 joystick.last_value = joy_read_buttons_bios();