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.
17 #define WIN32_LEAN_AND_MEAN
35 //#include "win\cyberimp.h"
36 //#include "spw_inp.h"
38 #define WIN_TACTILE_ON
40 #define JOY_READ_BUTTONS
41 #define JOY_MAX_BUTTONS 20
42 #define JOY_POLL_RATE 75 // 100 ms.
45 // Data Structures ============================================================
47 typedef struct Button_info {
56 typedef struct Joy_info {
62 Button_info buttons[JOY_MAX_BUTTONS];
68 int raw_x1, raw_y1, raw_z1, raw_r1, raw_u1, raw_v1, pov;
71 LRESULT joy_handler_win(HWND hWnd, UINT joymsg, UINT wParam, LPARAM lParam);
74 // Globals ====================================================================
77 int SpecialDevice = 0;
79 char SBall_installed = 0;
80 char joy_installed = 0;
84 // Extra Controls structures
85 //SPW_Control SBall_Control; // Space Ball Interface
87 static JOYCAPS WinJoyCaps;
88 static BOOL CHStickHack=0;
91 // Functions ==================================================================
94 Reset joystick information and request joystick notification from
95 Windows... Calibration shouldn't be a problem since Windows 95 takes
96 care of that. Also attaches a window to the joystick.
98 int joy_init(int joy, int spjoy)
105 // Reset Joystick information.
107 memset(&joystick, 0, sizeof(joystick));
109 for (i = 0; i < JOY_MAX_BUTTONS; i++)
110 joystick.buttons[i].last_state = 0;
112 if ( !joy_installed ) {
115 joystick.max_timer = 65536;
116 joystick.read_count = 0;
117 joystick.last_value = 0;
121 // Special joystick interface initialize.
127 // if (FindArg("-CyberImpact"))
128 // SpecialDevice = TACTILE_CYBERNET;
130 if (i = FindArg("-Iforce")) {
131 SpecialDevice = TACTILE_IMMERSION;
132 if (atoi(Args[i+1]) != 0) port = atoi(Args[i+1]);
136 mprintf((0, "Using Special Joystick!\n"));
138 switch(SpecialDevice)
140 #ifdef WIN_TACTILE_ON
141 case TACTILE_CYBERNET:
142 TactileStick = TACTILE_CYBERNET;
143 if (!Tactile_open(0)) joy_present = 0;
144 else joy_present = 1;
147 case TACTILE_IMMERSION:
148 TactileStick = TACTILE_IMMERSION;
149 if (!Tactile_open(port)) joy_present = 0;
150 else joy_present = 1;
154 if (!joy_present) SpecialDevice = 0;
157 joy_present = (char)joyGetNumDevs();
165 // closes to joystick by releasing it from the application window
171 switch (SpecialDevice)
173 case TACTILE_CYBERNET:
177 case TACTILE_IMMERSION:
181 joyReleaseCapture(joystick.joyid);
187 // joy_get/set_cal_vals
188 // functions that access all of the joystick axis'.
189 // An array of at least four elements must be passed to each function
191 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
195 for (i=0; i < 7; i++)
197 axis_min[i] = joystick.axis_min[i];
198 axis_center[i] = joystick.axis_center[i];
199 axis_max[i] = joystick.axis_max[i];
204 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
208 for (i=0; i < 7; i++)
210 joystick.axis_min[i] = axis_min[i];
211 joystick.axis_center[i] = axis_center[i];
212 joystick.axis_max[i] = axis_max[i];
217 // joy_get_present_mask
218 // returns the joystick axis' available to be used and accessed
220 ubyte joy_get_present_mask()
222 return joystick.present_mask;
228 // resets joystick button parameters
234 for (i = 0; i < JOY_MAX_BUTTONS; i++)
236 joystick.buttons[i].ignore = 0;
237 joystick.buttons[i].state = 0;
238 joystick.buttons[i].timedown = 0;
239 joystick.buttons[i].downcount = 0;
240 joystick.buttons[i].upcount = 0;
245 // joy_read_raw_buttons
246 // returns the status of the buttons at that moment.
248 ubyte joy_read_raw_buttons()
253 if (!joy_present) { return 0; }
255 memset(&joy, 0, sizeof(joy));
256 joy.dwSize = sizeof(joy);
257 joy.dwFlags = JOY_RETURNBUTTONS | JOY_USEDEADZONE;
258 joyGetPosEx(joystick.joyid, &joy);
260 // For now we support just four buttons
261 value = value | ((ubyte)joy.dwButtons & JOY_BUTTON1);
262 value = value | ((ubyte)joy.dwButtons & JOY_BUTTON2);
263 value = value | ((ubyte)joy.dwButtons & JOY_BUTTON3);
264 value = value | ((ubyte)joy.dwButtons & JOY_BUTTON4);
270 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
273 ubyte read_masks = 0;
276 if (!joy_installed) return 0;
277 if (!joy_present) return 0;
279 for (i = 0; i < 7; i++)
282 axis[3] = JOY_POVCENTERED;
284 switch (SpecialDevice)
286 case TACTILE_CYBERNET:
287 //@@ CyberImpact_ReadRawValues(mask, axis);
288 //@@ joystick.raw_x1 = axis[0];
289 //@@ joystick.raw_y1 = axis[1];
290 //@@ joystick.raw_z1 = axis[2];
291 //@@ joystick.raw_r1 = axis[4];
292 //@@ joystick.raw_u1 = axis[5];
293 //@@ joystick.raw_v1 = axis[6];
294 //@@ joystick.pov = axis[3];
297 case TACTILE_IMMERSION:
298 #ifdef WIN_TACTILE_ON
299 IForce_ReadRawValues(axis);
301 joystick.raw_x1 = axis[0];
302 joystick.raw_y1 = axis[1];
303 joystick.raw_z1 = axis[2];
304 joystick.raw_r1 = axis[4];
305 joystick.raw_u1 = axis[5];
306 joystick.raw_v1 = axis[6];
307 joystick.pov = axis[3];
311 memset(&joy, 0, sizeof(joy));
312 joy.dwSize = sizeof(joy);
313 joy.dwFlags = JOY_RETURNALL | JOY_USEDEADZONE;
314 joyGetPosEx(joystick.joyid, &joy);
315 joystick.raw_x1 = joy.dwXpos;
316 joystick.raw_y1 = joy.dwYpos;
317 joystick.raw_z1 = joy.dwZpos;
318 joystick.raw_r1 = joy.dwRpos;
319 joystick.raw_u1 = joy.dwUpos;
320 joystick.raw_v1 = joy.dwVpos;
321 joystick.pov = joy.dwPOV;
324 mask &= joystick.present_mask;
325 if (mask == 0) return 0; // Don't read if no stick present
327 if (mask & JOY_1_X_AXIS) {
328 axis[0] = joystick.raw_x1;
329 read_masks |= JOY_1_X_AXIS;
331 if (mask & JOY_1_Y_AXIS) {
332 axis[1] = joystick.raw_y1;
333 read_masks |= JOY_1_Y_AXIS;
335 if (mask & JOY_1_Z_AXIS) {
336 axis[2] = joystick.raw_z1;
337 read_masks |= JOY_1_Z_AXIS;
339 if (mask & JOY_1_R_AXIS) {
340 axis[4] = joystick.raw_r1;
341 read_masks |= JOY_1_R_AXIS;
343 if (mask & JOY_1_U_AXIS) {
344 axis[5] = joystick.raw_u1;
345 read_masks |= JOY_1_U_AXIS;
347 if (mask & JOY_1_V_AXIS) {
348 axis[6] = joystick.raw_v1;
349 read_masks |= JOY_1_V_AXIS;
351 if (mask & JOY_1_POV) {
352 axis[3] = joystick.pov;
353 read_masks |= JOY_1_POV;
362 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, joystick.axis_min );
363 if ( joystick.present_mask & 3 )
372 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, joystick.axis_max );
373 if ( joystick.present_mask & 3 )
382 switch (SpecialDevice)
384 case TACTILE_CYBERNET:
385 //@@ CyberImpact_CalibCenter();
389 joystick.present_mask = joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, joystick.axis_center );
390 if ( joystick.present_mask & 3 )
397 void joy_set_cen_fake(int channel)
399 // Do we need this???
403 int joy_get_scaled_reading( int raw, int axn )
407 // Make sure it's calibrated properly.
408 if ( joystick.axis_center[axn] - joystick.axis_min[axn] < 128 ) return 0;
409 if ( joystick.axis_max[axn] - joystick.axis_center[axn] < 128 ) return 0;
411 raw -= joystick.axis_center[axn];
414 d = joystick.axis_center[axn]-joystick.axis_min[axn];
416 d = joystick.axis_max[axn]-joystick.axis_center[axn];
424 if ( x < -128 ) x = -128;
425 if ( x > 127 ) x = 127;
431 //@@ if ((joystick.axis_max[axn] - joystick.axis_min[axn]) == 0) return 0;
432 //@@ if (axn == 3) return raw;
434 //@@ x = (256 * raw)/(joystick.axis_max[axn]-joystick.axis_min[axn]);
441 void joy_get_pos( int *x, int *y )
447 if (!joy_installed || !joy_present) { *x = *y = 0; return; }
449 flags=joystick_read_raw_axis( JOY_1_X_AXIS+JOY_1_Y_AXIS, axis );
451 if ( flags & JOY_1_X_AXIS )
452 *x = joy_get_scaled_reading( axis[0], 0 );
456 if ( flags & JOY_1_Y_AXIS )
457 *y = joy_get_scaled_reading( axis[1], 1 );
463 ubyte joy_read_stick( ubyte masks, int *axis )
469 if ((!joy_installed)||(!joy_present)) {
470 axis[0] = 0; axis[1] = 0;
471 axis[2] = 0; axis[3] = 0;
472 axis[4] = 0; axis[5] = 0;
477 flags=joystick_read_raw_axis( masks, raw_axis );
479 if ( flags & JOY_1_X_AXIS )
480 axis[0] = joy_get_scaled_reading( raw_axis[0], 0 );
484 if ( flags & JOY_1_Y_AXIS )
485 axis[1] = joy_get_scaled_reading( raw_axis[1], 1 );
489 if ( flags & JOY_1_Z_AXIS )
490 axis[2] = joy_get_scaled_reading( raw_axis[2], 2 );
494 if ( flags & JOY_1_R_AXIS )
495 axis[4] = joy_get_scaled_reading( raw_axis[4], 4 );
499 if ( flags & JOY_1_U_AXIS )
500 axis[5] = joy_get_scaled_reading( raw_axis[5], 5 );
504 if ( flags & JOY_1_V_AXIS )
505 axis[6] = joy_get_scaled_reading( raw_axis[6], 6 );
509 if ( flags & JOY_1_POV )
510 axis[3] = joy_get_scaled_reading( raw_axis[3], 3 );
520 if ((!joy_installed)||(!joy_present)) return 0;
521 return joy_read_raw_buttons();
525 void joy_get_btn_down_cnt( int *btn0, int *btn1 )
527 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
529 *btn0 = joystick.buttons[0].downcount;
530 joystick.buttons[0].downcount = 0;
531 *btn1 = joystick.buttons[1].downcount;
532 joystick.buttons[1].downcount = 0;
536 int joy_get_button_state( int btn )
540 if ((!joy_installed)||(!joy_present)) return 0;
542 if (btn >= JOY_MAX_BUTTONS)
545 count = joystick.buttons[btn].state;
551 int joy_get_button_up_cnt( int btn )
555 if ((!joy_installed)||(!joy_present)) return 0;
557 if (btn >= JOY_MAX_BUTTONS)
560 count = joystick.buttons[btn].upcount;
561 joystick.buttons[btn].upcount = 0;
567 int joy_get_button_down_cnt( int btn )
571 if ((!joy_installed)||(!joy_present)) return 0;
572 if (btn >= JOY_MAX_BUTTONS)
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 >= JOY_MAX_BUTTONS)
590 count = joystick.buttons[btn].timedown;
591 joystick.buttons[btn].timedown = 0;
593 return count; //fixdiv(count, 1000);
597 void joy_get_btn_up_cnt( int *btn0, int *btn1 )
599 if ((!joy_installed)||(!joy_present)) { *btn0=*btn1=0; return; }
601 *btn0 = joystick.buttons[0].upcount;
602 joystick.buttons[0].upcount = 0;
603 *btn1 = joystick.buttons[1].upcount;
604 joystick.buttons[1].upcount = 0;
608 void joy_set_btn_values( int btn, int state, fix timedown, int downcount, int upcount )
610 joystick.buttons[btn].ignore = 1;
611 joystick.buttons[btn].state = state;
612 joystick.buttons[btn].timedown = timedown; //fixmul(timedown,1000); //convert to miliseconds
613 joystick.buttons[btn].downcount = downcount;
614 joystick.buttons[btn].upcount = upcount;
618 // Windows 95 Custom joystick configuration.
619 // These are 'low level' routines to get information about a joystick
621 // Note: spjoy is for 'unique' joysticks.
623 int joy95_init_stick(int joy, int spjoy)
632 switch (SpecialDevice)
634 case TACTILE_CYBERNET:
635 //@@ joystick.present_mask = JOY_1_X_AXIS | JOY_1_Y_AXIS | JOY_1_Z_AXIS | JOY_1_POV;
636 //@@ joystick.has_pov = 1;
639 case TACTILE_IMMERSION:
640 mprintf((0, "Joystick name= I-Force compatible device.\n"));
644 // if (spjoy) return 1;
648 if (joy == 1) joystick.joyid = joyid = JOYSTICKID1;
652 mmresult = joyGetDevCaps(joyid, &WinJoyCaps, sizeof(WinJoyCaps));
653 if (mmresult != JOYERR_NOERROR) {
654 mprintf((1, "Attempt to get Joystick %d caps failed.\n", joy));
657 mprintf ((0,"Joystick name=%s\n",WinJoyCaps.szPname));
659 // Tell our Window App. about this joystick.
660 joySetThreshold(joyid, WinJoyCaps.wXmax/256);
661 mmresult = joySetCapture(GetLibraryWindow(),
666 if (mmresult != JOYERR_NOERROR) {
667 mprintf((1, "Unable to capture joystick %d. Error=%d\n", joy,mmresult));
671 // Get raw axis' min and max.
672 joystick.threshold = WinJoyCaps.wXmax/256;
673 joystick.max_timer = WinJoyCaps.wPeriodMax;
675 joystick.threshold = WinJoyCaps.wXmax/256;
676 joystick.max_timer = WinJoyCaps.wPeriodMax;
677 joystick.axis_min[0] = WinJoyCaps.wXmin;
678 joystick.axis_min[1] = WinJoyCaps.wYmin;
679 joystick.axis_min[2] = WinJoyCaps.wZmin;
680 joystick.axis_min[4] = WinJoyCaps.wRmin;
681 joystick.axis_min[5] = WinJoyCaps.wUmin;
682 joystick.axis_min[6] = WinJoyCaps.wVmin;
683 joystick.axis_max[0] = WinJoyCaps.wXmax;
684 joystick.axis_max[1] = WinJoyCaps.wYmax;
685 joystick.axis_max[2] = WinJoyCaps.wZmax;
686 joystick.axis_max[4] = WinJoyCaps.wRmax;
687 joystick.axis_max[5] = WinJoyCaps.wUmax;
688 joystick.axis_max[6] = WinJoyCaps.wVmax;
689 joystick.axis_center[0] = (WinJoyCaps.wXmax-WinJoyCaps.wXmin)/2;
690 joystick.axis_center[1] = (WinJoyCaps.wYmax-WinJoyCaps.wYmin)/2;
691 joystick.axis_center[2] = (WinJoyCaps.wZmax-WinJoyCaps.wZmin)/2;
692 joystick.axis_center[4] = (WinJoyCaps.wRmax-WinJoyCaps.wRmin)/2;
693 joystick.axis_center[5] = (WinJoyCaps.wUmax-WinJoyCaps.wUmin)/2;
694 joystick.axis_center[6] = (WinJoyCaps.wVmax-WinJoyCaps.wVmin)/2;
696 joystick.present_mask = JOY_1_X_AXIS | JOY_1_Y_AXIS;
698 if (WinJoyCaps.wCaps & JOYCAPS_HASPOV) {
699 joystick.has_pov = 1;
700 joystick.present_mask |= JOY_1_POV;
702 else joystick.has_pov = 0;
704 if (WinJoyCaps.wCaps & JOYCAPS_HASZ)
705 joystick.present_mask |= JOY_1_Z_AXIS;
706 if (WinJoyCaps.wCaps & JOYCAPS_HASR)
707 joystick.present_mask |= JOY_1_R_AXIS;
708 if (WinJoyCaps.wCaps & JOYCAPS_HASU)
709 joystick.present_mask |= JOY_1_U_AXIS;
710 if (WinJoyCaps.wCaps & JOYCAPS_HASV)
711 joystick.present_mask |= JOY_1_V_AXIS;
713 joy95_get_name(JOYSTICKID1, joyname, 255);
714 if (!strcmpi(joyname, "CH Flightstick Pro") || FindArg("-ordinaljoy")) {
717 else CHStickHack = 0;
723 void joy95_get_name(int joyid, char *name, int namesize)
730 registry_handle *rhandle;
735 switch(SpecialDevice)
737 case TACTILE_IMMERSION:
738 strcpy(name, "I-Force Force-Feedback");
742 res = joyGetDevCaps(joyid, &jc, sizeof(jc));
743 if (res != JOYERR_NOERROR) {
744 strcpy(name, "NO JOYSTICK DETECTED");
748 // we have the reg key!
749 registry_setpath(HKEY_LOCAL_MACHINE);
750 strcpy(regpath, "System\\CurrentControlSet\\control\\MediaResources\\joystick\\");
751 strcat(regpath, jc.szRegKey);
752 strcat(regpath, "\\CurrentJoystickSettings");
753 sprintf(reglabel, "Joystick%dOEMName", joyid+1);
755 if ((rhandle = registry_open(regpath))) {
756 if (!registry_getstring(rhandle, reglabel, name, namesize)) {
757 registry_close(rhandle);
758 strcpy(name, "JOYSTICK");
761 registry_close(rhandle);
763 // have reg entry for full name
764 strcpy(regpath, "System\\CurrentControlSet\\control\\MediaProperties\\PrivateProperties\\Joystick\\OEM\\");
765 strcat(regpath, name);
767 if ((rhandle = registry_open(regpath))) {
768 if (!registry_getstring(rhandle, "OEMName", name, namesize)) {
769 registry_close(rhandle);
770 strcpy(name, "JOYSTICK");
773 registry_close(rhandle);
775 else strcpy(name, "JOYSTICK");
777 else strcpy(name, "JOYSTICK");
783 This function takes the place of the joy_handler function for dos.
784 The main window procedure will call this function whenever a joystick message
787 LRESULT joy_handler_win(HWND hWnd, UINT joymsg, UINT wParam, LPARAM lParam)
791 static DWORD old_time = 0xffffffff;
798 // take care of first time initialization
799 if (!joy_installed) return 1;
800 if (old_time == 0xffffffff) {
801 old_time = timer_get_fixed_seconds();
805 // read and translate buttons 0-3
811 // For now we support just four buttons
812 memset(&joy, 0, sizeof(joy));
813 joy.dwSize = sizeof(joy);
814 joy.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNPOV;
815 joyGetPosEx(joystick.joyid, &joy);
818 if (joy.dwPOV == JOY_POVCENTERED && joy.dwButtons > 0) {
819 value = (1 << (joy.dwButtons-1));
822 mprintf_at((0, 2, 32, "RAW:%08x OUR:%08x\n", joy.dwButtons, value));
825 value = joy.dwButtons;
830 new_time = timer_get_fixed_seconds();
831 if (new_time < old_time) old_time = abs(new_time - old_time);
832 interval_time = new_time - old_time;
834 for (i = 0; i < JOY_MAX_BUTTONS; i++)
836 // Check status of each button and translate information to the button
838 button = &joystick.buttons[i];
840 if (!button->ignore) {
841 if (i < (JOY_MAX_BUTTONS - 4))
842 state = (value >> i) & 1;
845 if ( button->last_state == state ) {
847 button->timedown += interval_time; //interval_ms;
851 button->downcount += state;
854 button->upcount += button->state;
857 button->last_state = state;
869 joyReleaseCapture(joystick.joyid);
873 void joy_start_poll()
875 joySetCapture(GetLibraryWindow(),
884 // joy_get/set_timer_rate
885 // these functions are currently implemented for compatability with
886 // DOS Descent2, but may be unecessary for now.
888 void joy_set_timer_rate(int max_value )
890 // No real effect, since Joystick runs on its own timer in Windows, for now.
894 int joy_get_timer_rate()
896 return joystick.max_timer;
899 void joy_handler(int ticks_this_time)
901 // replaced by joy_handler_win for Windows
904 void joy_set_slow_reading(int flag)
906 // No need to do this. In Windows, there are only slow readings.