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:15 bradleyb Exp $";
17 #pragma on (unreferenced)
21 #define WIN32_LEAN_AND_MEAN
39 //#include "win\cyberimp.h"
40 //#include "spw_inp.h"
42 #define WIN_TACTILE_ON
44 #define JOY_READ_BUTTONS
45 #define MAX_BUTTONS 20
46 #define JOY_POLL_RATE 75 // 100 ms.
49 // Data Structures ============================================================
51 typedef struct Button_info {
60 typedef struct Joy_info {
66 Button_info buttons[MAX_BUTTONS];
72 int raw_x1, raw_y1, raw_z1, raw_r1, raw_u1, raw_v1, pov;
75 LRESULT joy_handler_win(HWND hWnd, UINT joymsg, UINT wParam, LPARAM lParam);
78 // Globals ====================================================================
81 int SpecialDevice = 0;
83 char SBall_installed = 0;
84 char joy_installed = 0;
88 // Extra Controls structures
89 //SPW_Control SBall_Control; // Space Ball Interface
91 static JOYCAPS WinJoyCaps;
92 static BOOL CHStickHack=0;
95 // Functions ==================================================================
98 Reset joystick information and request joystick notification from
99 Windows... Calibration shouldn't be a problem since Windows 95 takes
100 care of that. Also attaches a window to the joystick.
102 int joy_init(int joy, int spjoy)
109 // Reset Joystick information.
111 memset(&joystick, 0, sizeof(joystick));
113 for (i=0; i<MAX_BUTTONS; i++)
114 joystick.buttons[i].last_state = 0;
116 if ( !joy_installed ) {
119 joystick.max_timer = 65536;
120 joystick.read_count = 0;
121 joystick.last_value = 0;
125 // Special joystick interface initialize.
131 // if (FindArg("-CyberImpact"))
132 // SpecialDevice = TACTILE_CYBERNET;
134 if (i = FindArg("-Iforce")) {
135 SpecialDevice = TACTILE_IMMERSION;
136 if (atoi(Args[i+1]) != 0) port = atoi(Args[i+1]);
140 mprintf((0, "Using Special Joystick!\n"));
142 switch(SpecialDevice)
144 #ifdef WIN_TACTILE_ON
145 case TACTILE_CYBERNET:
146 TactileStick = TACTILE_CYBERNET;
147 if (!Tactile_open(0)) joy_present = 0;
148 else joy_present = 1;
151 case TACTILE_IMMERSION:
152 TactileStick = TACTILE_IMMERSION;
153 if (!Tactile_open(port)) joy_present = 0;
154 else joy_present = 1;
158 if (!joy_present) SpecialDevice = 0;
161 joy_present = (char)joyGetNumDevs();
169 // closes to joystick by releasing it from the application window
175 switch (SpecialDevice)
177 case TACTILE_CYBERNET:
181 case TACTILE_IMMERSION:
185 joyReleaseCapture(joystick.joyid);
191 // joy_get/set_cal_vals
192 // functions that access all of the joystick axis'.
193 // An array of at least four elements must be passed to each function
195 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
199 for (i=0; i < 7; i++)
201 axis_min[i] = joystick.axis_min[i];
202 axis_center[i] = joystick.axis_center[i];
203 axis_max[i] = joystick.axis_max[i];
208 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
212 for (i=0; i < 7; i++)
214 joystick.axis_min[i] = axis_min[i];
215 joystick.axis_center[i] = axis_center[i];
216 joystick.axis_max[i] = axis_max[i];
221 // joy_get_present_mask
222 // returns the joystick axis' available to be used and accessed
224 ubyte joy_get_present_mask()
226 return joystick.present_mask;
232 // resets joystick button parameters
238 for (i=0; i<MAX_BUTTONS; i++ ) {
239 joystick.buttons[i].ignore = 0;
240 joystick.buttons[i].state = 0;
241 joystick.buttons[i].timedown = 0;
242 joystick.buttons[i].downcount = 0;
243 joystick.buttons[i].upcount = 0;
248 // joy_read_raw_buttons
249 // returns the status of the buttons at that moment.
251 ubyte joy_read_raw_buttons()
256 if (!joy_present) { return 0; }
258 memset(&joy, 0, sizeof(joy));
259 joy.dwSize = sizeof(joy);
260 joy.dwFlags = JOY_RETURNBUTTONS | JOY_USEDEADZONE;
261 joyGetPosEx(joystick.joyid, &joy);
263 // For now we support just four buttons
264 value = value | ((ubyte)joy.dwButtons & JOY_BUTTON1);
265 value = value | ((ubyte)joy.dwButtons & JOY_BUTTON2);
266 value = value | ((ubyte)joy.dwButtons & JOY_BUTTON3);
267 value = value | ((ubyte)joy.dwButtons & JOY_BUTTON4);
273 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
276 ubyte read_masks = 0;
279 if (!joy_installed) return 0;
280 if (!joy_present) return 0;
282 for (i = 0; i < 7; i++)
285 axis[3] = JOY_POVCENTERED;
287 switch (SpecialDevice)
289 case TACTILE_CYBERNET:
290 //@@ CyberImpact_ReadRawValues(mask, axis);
291 //@@ joystick.raw_x1 = axis[0];
292 //@@ joystick.raw_y1 = axis[1];
293 //@@ joystick.raw_z1 = axis[2];
294 //@@ joystick.raw_r1 = axis[4];
295 //@@ joystick.raw_u1 = axis[5];
296 //@@ joystick.raw_v1 = axis[6];
297 //@@ joystick.pov = axis[3];
300 case TACTILE_IMMERSION:
301 #ifdef WIN_TACTILE_ON
302 IForce_ReadRawValues(axis);
304 joystick.raw_x1 = axis[0];
305 joystick.raw_y1 = axis[1];
306 joystick.raw_z1 = axis[2];
307 joystick.raw_r1 = axis[4];
308 joystick.raw_u1 = axis[5];
309 joystick.raw_v1 = axis[6];
310 joystick.pov = axis[3];
314 memset(&joy, 0, sizeof(joy));
315 joy.dwSize = sizeof(joy);
316 joy.dwFlags = JOY_RETURNALL | JOY_USEDEADZONE;
317 joyGetPosEx(joystick.joyid, &joy);
318 joystick.raw_x1 = joy.dwXpos;
319 joystick.raw_y1 = joy.dwYpos;
320 joystick.raw_z1 = joy.dwZpos;
321 joystick.raw_r1 = joy.dwRpos;
322 joystick.raw_u1 = joy.dwUpos;
323 joystick.raw_v1 = joy.dwVpos;
324 joystick.pov = joy.dwPOV;
327 mask &= joystick.present_mask;
328 if (mask == 0) return 0; // Don't read if no stick present
330 if (mask & JOY_1_X_AXIS) {
331 axis[0] = joystick.raw_x1;
332 read_masks |= JOY_1_X_AXIS;
334 if (mask & JOY_1_Y_AXIS) {
335 axis[1] = joystick.raw_y1;
336 read_masks |= JOY_1_Y_AXIS;
338 if (mask & JOY_1_Z_AXIS) {
339 axis[2] = joystick.raw_z1;
340 read_masks |= JOY_1_Z_AXIS;
342 if (mask & JOY_1_R_AXIS) {
343 axis[4] = joystick.raw_r1;
344 read_masks |= JOY_1_R_AXIS;
346 if (mask & JOY_1_U_AXIS) {
347 axis[5] = joystick.raw_u1;
348 read_masks |= JOY_1_U_AXIS;
350 if (mask & JOY_1_V_AXIS) {
351 axis[6] = joystick.raw_v1;
352 read_masks |= JOY_1_V_AXIS;
354 if (mask & JOY_1_POV) {
355 axis[3] = joystick.pov;
356 read_masks |= JOY_1_POV;
365 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, joystick.axis_min );
366 if ( joystick.present_mask & 3 )
375 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, joystick.axis_max );
376 if ( joystick.present_mask & 3 )
385 switch (SpecialDevice)
387 case TACTILE_CYBERNET:
388 //@@ CyberImpact_CalibCenter();
392 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, joystick.axis_center );
393 if ( joystick.present_mask & 3 )
400 void joy_set_cen_fake(int channel)
402 // Do we need this???
406 int joy_get_scaled_reading( int raw, int axn )
410 // Make sure it's calibrated properly.
411 if ( joystick.axis_center[axn] - joystick.axis_min[axn] < 128 ) return 0;
412 if ( joystick.axis_max[axn] - joystick.axis_center[axn] < 128 ) return 0;
414 raw -= joystick.axis_center[axn];
417 d = joystick.axis_center[axn]-joystick.axis_min[axn];
419 d = joystick.axis_max[axn]-joystick.axis_center[axn];
427 if ( x < -128 ) x = -128;
428 if ( x > 127 ) x = 127;
434 //@@ if ((joystick.axis_max[axn] - joystick.axis_min[axn]) == 0) return 0;
435 //@@ if (axn == 3) return raw;
437 //@@ x = (256 * raw)/(joystick.axis_max[axn]-joystick.axis_min[axn]);
444 void joy_get_pos( int *x, int *y )
450 if (!joy_installed || !joy_present) { *x = *y = 0; return; }
452 flags=joystick_read_raw_axis( JOY_1_X_AXIS+JOY_1_Y_AXIS, axis );
454 if ( flags & JOY_1_X_AXIS )
455 *x = joy_get_scaled_reading( axis[0], 0 );
459 if ( flags & JOY_1_Y_AXIS )
460 *y = joy_get_scaled_reading( axis[1], 1 );
466 ubyte joy_read_stick( ubyte masks, int *axis )
472 if ((!joy_installed)||(!joy_present)) {
473 axis[0] = 0; axis[1] = 0;
474 axis[2] = 0; axis[3] = 0;
475 axis[4] = 0; axis[5] = 0;
480 flags=joystick_read_raw_axis( masks, raw_axis );
482 if ( flags & JOY_1_X_AXIS )
483 axis[0] = joy_get_scaled_reading( raw_axis[0], 0 );
487 if ( flags & JOY_1_Y_AXIS )
488 axis[1] = joy_get_scaled_reading( raw_axis[1], 1 );
492 if ( flags & JOY_1_Z_AXIS )
493 axis[2] = joy_get_scaled_reading( raw_axis[2], 2 );
497 if ( flags & JOY_1_R_AXIS )
498 axis[4] = joy_get_scaled_reading( raw_axis[4], 4 );
502 if ( flags & JOY_1_U_AXIS )
503 axis[5] = joy_get_scaled_reading( raw_axis[5], 5 );
507 if ( flags & JOY_1_V_AXIS )
508 axis[6] = joy_get_scaled_reading( raw_axis[6], 6 );
512 if ( flags & JOY_1_POV )
513 axis[3] = joy_get_scaled_reading( raw_axis[3], 3 );
523 if ((!joy_installed)||(!joy_present)) return 0;
524 return joy_read_raw_buttons();
528 void joy_get_btn_down_cnt( int *btn0, int *btn1 )
530 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
532 *btn0 = joystick.buttons[0].downcount;
533 joystick.buttons[0].downcount = 0;
534 *btn1 = joystick.buttons[1].downcount;
535 joystick.buttons[1].downcount = 0;
539 int joy_get_button_state( int btn )
543 if ((!joy_installed)||(!joy_present)) return 0;
545 if ( btn >= MAX_BUTTONS ) return 0;
547 count = joystick.buttons[btn].state;
553 int joy_get_button_up_cnt( int btn )
557 if ((!joy_installed)||(!joy_present)) return 0;
559 if ( btn >= MAX_BUTTONS ) return 0;
561 count = joystick.buttons[btn].upcount;
562 joystick.buttons[btn].upcount = 0;
568 int joy_get_button_down_cnt( int btn )
572 if ((!joy_installed)||(!joy_present)) return 0;
573 if ( btn >= MAX_BUTTONS ) return 0;
575 count = joystick.buttons[btn].downcount;
576 joystick.buttons[btn].downcount = 0;
582 fix joy_get_button_down_time( int btn )
586 if ((!joy_installed)||(!joy_present)) return 0;
587 if ( btn >= MAX_BUTTONS ) return 0;
589 count = joystick.buttons[btn].timedown;
590 joystick.buttons[btn].timedown = 0;
592 return count; //fixdiv(count, 1000);
596 void joy_get_btn_up_cnt( int *btn0, int *btn1 )
598 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
600 *btn0 = joystick.buttons[0].upcount;
601 joystick.buttons[0].upcount = 0;
602 *btn1 = joystick.buttons[1].upcount;
603 joystick.buttons[1].upcount = 0;
607 void joy_set_btn_values( int btn, int state, fix timedown, int downcount, int upcount )
609 joystick.buttons[btn].ignore = 1;
610 joystick.buttons[btn].state = state;
611 joystick.buttons[btn].timedown = timedown; //fixmul(timedown,1000); //convert to miliseconds
612 joystick.buttons[btn].downcount = downcount;
613 joystick.buttons[btn].upcount = upcount;
617 // Windows 95 Custom joystick configuration.
618 // These are 'low level' routines to get information about a joystick
620 // Note: spjoy is for 'unique' joysticks.
622 int joy95_init_stick(int joy, int spjoy)
631 switch (SpecialDevice)
633 case TACTILE_CYBERNET:
634 //@@ joystick.present_mask = JOY_1_X_AXIS | JOY_1_Y_AXIS | JOY_1_Z_AXIS | JOY_1_POV;
635 //@@ joystick.has_pov = 1;
638 case TACTILE_IMMERSION:
639 mprintf((0, "Joystick name= I-Force compatible device.\n"));
643 // if (spjoy) return 1;
647 if (joy == 1) joystick.joyid = joyid = JOYSTICKID1;
651 mmresult = joyGetDevCaps(joyid, &WinJoyCaps, sizeof(WinJoyCaps));
652 if (mmresult != JOYERR_NOERROR) {
653 mprintf((1, "Attempt to get Joystick %d caps failed.\n", joy));
656 mprintf ((0,"Joystick name=%s\n",WinJoyCaps.szPname));
658 // Tell our Window App. about this joystick.
659 joySetThreshold(joyid, WinJoyCaps.wXmax/256);
660 mmresult = joySetCapture(GetLibraryWindow(),
665 if (mmresult != JOYERR_NOERROR) {
666 mprintf((1, "Unable to capture joystick %d. Error=%d\n", joy,mmresult));
670 // Get raw axis' min and max.
671 joystick.threshold = WinJoyCaps.wXmax/256;
672 joystick.max_timer = WinJoyCaps.wPeriodMax;
674 joystick.threshold = WinJoyCaps.wXmax/256;
675 joystick.max_timer = WinJoyCaps.wPeriodMax;
676 joystick.axis_min[0] = WinJoyCaps.wXmin;
677 joystick.axis_min[1] = WinJoyCaps.wYmin;
678 joystick.axis_min[2] = WinJoyCaps.wZmin;
679 joystick.axis_min[4] = WinJoyCaps.wRmin;
680 joystick.axis_min[5] = WinJoyCaps.wUmin;
681 joystick.axis_min[6] = WinJoyCaps.wVmin;
682 joystick.axis_max[0] = WinJoyCaps.wXmax;
683 joystick.axis_max[1] = WinJoyCaps.wYmax;
684 joystick.axis_max[2] = WinJoyCaps.wZmax;
685 joystick.axis_max[4] = WinJoyCaps.wRmax;
686 joystick.axis_max[5] = WinJoyCaps.wUmax;
687 joystick.axis_max[6] = WinJoyCaps.wVmax;
688 joystick.axis_center[0] = (WinJoyCaps.wXmax-WinJoyCaps.wXmin)/2;
689 joystick.axis_center[1] = (WinJoyCaps.wYmax-WinJoyCaps.wYmin)/2;
690 joystick.axis_center[2] = (WinJoyCaps.wZmax-WinJoyCaps.wZmin)/2;
691 joystick.axis_center[4] = (WinJoyCaps.wRmax-WinJoyCaps.wRmin)/2;
692 joystick.axis_center[5] = (WinJoyCaps.wUmax-WinJoyCaps.wUmin)/2;
693 joystick.axis_center[6] = (WinJoyCaps.wVmax-WinJoyCaps.wVmin)/2;
695 joystick.present_mask = JOY_1_X_AXIS | JOY_1_Y_AXIS;
697 if (WinJoyCaps.wCaps & JOYCAPS_HASPOV) {
698 joystick.has_pov = 1;
699 joystick.present_mask |= JOY_1_POV;
701 else joystick.has_pov = 0;
703 if (WinJoyCaps.wCaps & JOYCAPS_HASZ)
704 joystick.present_mask |= JOY_1_Z_AXIS;
705 if (WinJoyCaps.wCaps & JOYCAPS_HASR)
706 joystick.present_mask |= JOY_1_R_AXIS;
707 if (WinJoyCaps.wCaps & JOYCAPS_HASU)
708 joystick.present_mask |= JOY_1_U_AXIS;
709 if (WinJoyCaps.wCaps & JOYCAPS_HASV)
710 joystick.present_mask |= JOY_1_V_AXIS;
712 joy95_get_name(JOYSTICKID1, joyname, 255);
713 if (!strcmpi(joyname, "CH Flightstick Pro") || FindArg("-ordinaljoy")) {
716 else CHStickHack = 0;
722 void joy95_get_name(int joyid, char *name, int namesize)
729 registry_handle *rhandle;
734 switch(SpecialDevice)
736 case TACTILE_IMMERSION:
737 strcpy(name, "I-Force Force-Feedback");
741 res = joyGetDevCaps(joyid, &jc, sizeof(jc));
742 if (res != JOYERR_NOERROR) {
743 strcpy(name, "NO JOYSTICK DETECTED");
747 // we have the reg key!
748 registry_setpath(HKEY_LOCAL_MACHINE);
749 strcpy(regpath, "System\\CurrentControlSet\\control\\MediaResources\\joystick\\");
750 strcat(regpath, jc.szRegKey);
751 strcat(regpath, "\\CurrentJoystickSettings");
752 sprintf(reglabel, "Joystick%dOEMName", joyid+1);
754 if ((rhandle = registry_open(regpath))) {
755 if (!registry_getstring(rhandle, reglabel, name, namesize)) {
756 registry_close(rhandle);
757 strcpy(name, "JOYSTICK");
760 registry_close(rhandle);
762 // have reg entry for full name
763 strcpy(regpath, "System\\CurrentControlSet\\control\\MediaProperties\\PrivateProperties\\Joystick\\OEM\\");
764 strcat(regpath, name);
766 if ((rhandle = registry_open(regpath))) {
767 if (!registry_getstring(rhandle, "OEMName", name, namesize)) {
768 registry_close(rhandle);
769 strcpy(name, "JOYSTICK");
772 registry_close(rhandle);
774 else strcpy(name, "JOYSTICK");
776 else strcpy(name, "JOYSTICK");
782 This function takes the place of the joy_handler function for dos.
783 The main window procedure will call this function whenever a joystick message
786 LRESULT joy_handler_win(HWND hWnd, UINT joymsg, UINT wParam, LPARAM lParam)
790 static DWORD old_time = 0xffffffff;
797 // take care of first time initialization
798 if (!joy_installed) return 1;
799 if (old_time == 0xffffffff) {
800 old_time = timer_get_fixed_seconds();
804 // read and translate buttons 0-3
810 // For now we support just four buttons
811 memset(&joy, 0, sizeof(joy));
812 joy.dwSize = sizeof(joy);
813 joy.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNPOV;
814 joyGetPosEx(joystick.joyid, &joy);
817 if (joy.dwPOV == JOY_POVCENTERED && joy.dwButtons > 0) {
818 value = (1 << (joy.dwButtons-1));
821 mprintf_at((0, 2, 32, "RAW:%08x OUR:%08x\n", joy.dwButtons, value));
824 value = joy.dwButtons;
829 new_time = timer_get_fixed_seconds();
830 if (new_time < old_time) old_time = abs(new_time - old_time);
831 interval_time = new_time - old_time;
833 for (i = 0; i < MAX_BUTTONS; i++)
835 // Check status of each button and translate information to the button
837 button = &joystick.buttons[i];
839 if (!button->ignore) {
840 if ( i < (MAX_BUTTONS-4) ) state = (value >> i) & 1;
843 if ( button->last_state == state ) {
845 button->timedown += interval_time; //interval_ms;
849 button->downcount += state;
852 button->upcount += button->state;
855 button->last_state = state;
867 joyReleaseCapture(joystick.joyid);
871 void joy_start_poll()
873 joySetCapture(GetLibraryWindow(),
882 // joy_get/set_timer_rate
883 // these functions are currently implemented for compatability with
884 // DOS Descent2, but may be unecessary for now.
886 void joy_set_timer_rate(int max_value )
888 // No real effect, since Joystick runs on its own timer in Windows, for now.
892 int joy_get_timer_rate()
894 return joystick.max_timer;
897 void joy_handler(int ticks_this_time)
899 // replaced by joy_handler_win for Windows
902 void joy_set_slow_reading(int flag)
904 // No need to do this. In Windows, there are only slow readings.