static BOOL CALLBACK DIJoystick_EnumAxisObjectsProc(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) { joystick_type* joystick = (joystick_type*)pvRef; DIPROPRANGE diprg; HRESULT hr; joystick->axes[joystick->num_axes].guid = lpddoi->guidType; joystick->axes[joystick->num_axes].name = (char *)malloc(strlen(lpddoi->tszName) + 1); strcpy(joystick->axes[joystick->num_axes].name, lpddoi->tszName); joystick->axes[joystick->num_axes].offset = lpddoi->dwOfs; /*ErrorMsg("got axis %s, offset %i",lpddoi->tszName, lpddoi->dwOfs);*/ diprg.diph.dwSize = sizeof(diprg); diprg.diph.dwHeaderSize = sizeof(diprg.diph); diprg.diph.dwObj = lpddoi->dwOfs; diprg.diph.dwHow = DIPH_BYOFFSET; diprg.lMin = 0; diprg.lMax = 255; hr = IDirectInputDevice2_SetProperty(joystick->did, DIPROP_RANGE, &diprg.diph); if (FAILED(hr)) /* if this fails, don't use this axis */ { free(joystick->axes[joystick->num_axes].name); joystick->axes[joystick->num_axes].name = NULL; return DIENUM_CONTINUE; } #ifdef JOY_DEBUG if (FAILED(hr)) { ErrorMsg("DirectInput SetProperty() joystick axis %s failed - %s\n", joystick->axes[joystick->num_axes].name, DirectXDecodeError(hr)); } #endif /* Set axis dead zone to 0; we need accurate #'s for analog joystick reading. */ hr = SetDIDwordProperty(joystick->did, DIPROP_DEADZONE, lpddoi->dwOfs, DIPH_BYOFFSET, 0); #ifdef JOY_DEBUG if (FAILED(hr)) { ErrorMsg("DirectInput SetProperty() joystick axis %s dead zone failed - %s\n", joystick->axes[joystick->num_axes].name, DirectXDecodeError(hr)); } #endif joystick->num_axes++; return DIENUM_CONTINUE; }
// this function sets the input range to -100 <-> +100 (we already do that in sdl) // it also sets the dead zone which we do not support in sdl yet.. probably easy to do though... void SetUpJoystickAxis(int joystick) { DIPROPRANGE diprg; bool DeadzoneNotSet = false; int i; // set axis range to (-100 ... +100) // This lets us test against 0 to see which way the stick is pointed. diprg.diph.dwSize = sizeof(diprg); diprg.diph.dwHeaderSize = sizeof(diprg.diph); diprg.diph.dwHow = DIPH_BYOFFSET; diprg.lMin = -100; diprg.lMax = +100; DebugPrintf( "SetUpJoystickAxis: setting up %d axes for joystick #%d='%s'\n", JoystickInfo[ joystick ].NumAxis, joystick, JoystickInfo[ joystick ].Name ); if (JoystickInfo[joystick].Axis[AXIS_XAxis].exists) { DebugPrintf( "SetUpJoystickAxis: setting up X axis range=(%d,%d) for joystick #%d='%s'\n", diprg.lMin, diprg.lMax, joystick, JoystickInfo[ joystick ].Name ); diprg.diph.dwObj = DIJOFS_X; if( !lpdiJoystick[joystick] ) { JoystickInfo[joystick].Axis[AXIS_XAxis].exists = false; // cannot set range, therefore do not allow axis DebugPrintf( "SetUpJoystickAxis: failed to set range for X axis because lpdiJoystick[joystick] is NULL\n" ); } else if ( lpdiJoystick[joystick]->lpVtbl->SetProperty(lpdiJoystick[joystick], DIPROP_RANGE, &diprg.diph) != DI_OK) { JoystickInfo[joystick].Axis[AXIS_XAxis].exists = false; // cannot set range, therefore do not allow axis DebugPrintf( "SetUpJoystickAxis: failed to set range for X axis\n" ); } else { // set dead zone // Units are ten thousandths, so multiply %age by 100. DebugPrintf( "SetUpJoystickAxis: setting up X axis deadzone=%d for joystick #%d='%s'\n", JoystickInfo[ joystick ].Axis[AXIS_XAxis].deadzone, joystick, JoystickInfo[ joystick ].Name ); if (SetDIDwordProperty(lpdiJoystick[joystick], DIPROP_DEADZONE, DIJOFS_X, DIPH_BYOFFSET, (DWORD)(JoystickInfo[joystick].Axis[AXIS_XAxis].deadzone * 100)) != DI_OK) { // cannot set deadzone - but just ignore for now... DeadzoneNotSet = true; DebugPrintf( "SetUpJoystickAxis: failed to set deadzone for X axis\n" ); } } } if (JoystickInfo[joystick].Axis[AXIS_YAxis].exists) { DebugPrintf( "SetUpJoystickAxis: setting up Y axis range=(%d,%d) for joystick #%d='%s'\n", diprg.lMin, diprg.lMax, joystick, JoystickInfo[ joystick ].Name ); diprg.diph.dwObj = DIJOFS_Y; if(lpdiJoystick[joystick]->lpVtbl->SetProperty(lpdiJoystick[joystick], DIPROP_RANGE, &diprg.diph) != DI_OK) { JoystickInfo[joystick].Axis[AXIS_YAxis].exists = false; // cannot set range, therefore do not allow axis DebugPrintf( "SetUpJoystickAxis: failed to set range for Y axis\n" ); }else { DebugPrintf( "SetUpJoystickAxis: setting up Y axis deadzone=%d for joystick #%d='%s'\n", JoystickInfo[ joystick ].Axis[AXIS_YAxis].deadzone, joystick, JoystickInfo[ joystick ].Name ); if (SetDIDwordProperty(lpdiJoystick[joystick], DIPROP_DEADZONE, DIJOFS_Y, DIPH_BYOFFSET, (DWORD)(JoystickInfo[joystick].Axis[AXIS_YAxis].deadzone * 100)) != DI_OK) { // cannot set deadzone - but just ignore for now... DeadzoneNotSet = true; DebugPrintf( "SetUpJoystickAxis: failed to set deadzone for Y axis\n" ); } } } if (JoystickInfo[joystick].Axis[AXIS_ZAxis].exists) { DebugPrintf( "SetUpJoystickAxis: setting up Z axis range=(%d,%d) for joystick #%d='%s'\n", diprg.lMin, diprg.lMax, joystick, JoystickInfo[ joystick ].Name ); diprg.diph.dwObj = DIJOFS_Z; if(lpdiJoystick[joystick]->lpVtbl->SetProperty(lpdiJoystick[joystick], DIPROP_RANGE, &diprg.diph) != DI_OK) { JoystickInfo[joystick].Axis[AXIS_ZAxis].exists = false; // cannot set range, therefore do not allow axis DebugPrintf( "SetUpJoystickAxis: failed to set range for Z axis\n" ); }else { DebugPrintf( "SetUpJoystickAxis: setting up Z axis deadzone=%d for joystick #%d='%s'\n", JoystickInfo[ joystick ].Axis[AXIS_ZAxis].deadzone, joystick, JoystickInfo[ joystick ].Name ); if (SetDIDwordProperty(lpdiJoystick[joystick], DIPROP_DEADZONE, DIJOFS_Z, DIPH_BYOFFSET, (DWORD)(JoystickInfo[joystick].Axis[AXIS_ZAxis].deadzone * 100)) != DI_OK) { // cannot set deadzone - but just ignore for now... DeadzoneNotSet = true; DebugPrintf( "SetUpJoystickAxis: failed to set deadzone for Z axis\n" ); } } } if (JoystickInfo[joystick].Axis[AXIS_RxAxis].exists) { DebugPrintf( "SetUpJoystickAxis: setting up Rx axis range=(%d,%d) for joystick #%d='%s'\n", diprg.lMin, diprg.lMax, joystick, JoystickInfo[ joystick ].Name ); diprg.diph.dwObj = DIJOFS_RX; if(lpdiJoystick[joystick]->lpVtbl->SetProperty(lpdiJoystick[joystick], DIPROP_RANGE, &diprg.diph) != DI_OK) { JoystickInfo[joystick].Axis[AXIS_RxAxis].exists = false; // cannot set range, therefore do not allow axis DebugPrintf( "SetUpJoystickAxis: failed to set range for Rx axis\n" ); }else { DebugPrintf( "SetUpJoystickAxis: setting up Rx axis deadzone=%d for joystick #%d='%s'\n", JoystickInfo[ joystick ].Axis[AXIS_RxAxis].deadzone, joystick, JoystickInfo[ joystick ].Name ); if (SetDIDwordProperty(lpdiJoystick[joystick], DIPROP_DEADZONE, DIJOFS_RX, DIPH_BYOFFSET, (DWORD)(JoystickInfo[joystick].Axis[AXIS_RxAxis].deadzone * 100)) != DI_OK) { // cannot set deadzone - but just ignore for now... DeadzoneNotSet = true; DebugPrintf( "SetUpJoystickAxis: failed to set deadzone for Rx axis\n" ); } } } if (JoystickInfo[joystick].Axis[AXIS_RyAxis].exists) { DebugPrintf( "SetUpJoystickAxis: setting up Ry axis range=(%d,%d) for joystick #%d='%s'\n", diprg.lMin, diprg.lMax, joystick, JoystickInfo[ joystick ].Name ); diprg.diph.dwObj = DIJOFS_RY; if(lpdiJoystick[joystick]->lpVtbl->SetProperty(lpdiJoystick[joystick], DIPROP_RANGE, &diprg.diph) != DI_OK) { JoystickInfo[joystick].Axis[AXIS_RyAxis].exists = false; // cannot set range, therefore do not allow axis DebugPrintf( "SetUpJoystickAxis: failed to set range for Ry axis\n" ); }else { DebugPrintf( "SetUpJoystickAxis: setting up Ry axis deadzone=%d for joystick #%d='%s'\n", JoystickInfo[ joystick ].Axis[AXIS_RyAxis].deadzone, joystick, JoystickInfo[ joystick ].Name ); if (SetDIDwordProperty(lpdiJoystick[joystick], DIPROP_DEADZONE, DIJOFS_RY, DIPH_BYOFFSET, (DWORD)(JoystickInfo[joystick].Axis[AXIS_RyAxis].deadzone * 100)) != DI_OK) { // cannot set deadzone - but just ignore for now... DeadzoneNotSet = true; DebugPrintf( "SetUpJoystickAxis: failed to set deadzone for Ry axis\n" ); } } } if (JoystickInfo[joystick].Axis[AXIS_RzAxis].exists) { DebugPrintf( "SetUpJoystickAxis: setting up Rz axis range=(%d,%d) for joystick #%d='%s'\n", diprg.lMin, diprg.lMax, joystick, JoystickInfo[ joystick ].Name ); diprg.diph.dwObj = DIJOFS_RZ; if(lpdiJoystick[joystick]->lpVtbl->SetProperty(lpdiJoystick[joystick], DIPROP_RANGE, &diprg.diph) != DI_OK) { JoystickInfo[joystick].Axis[AXIS_RzAxis].exists = false; // cannot set range, therefore do not allow axis DebugPrintf( "SetUpJoystickAxis: failed to set range for Rz axis\n" ); }else { DebugPrintf( "SetUpJoystickAxis: setting up Rz axis deadzone=%d for joystick #%d='%s'\n", JoystickInfo[ joystick ].Axis[AXIS_RzAxis].deadzone, joystick, JoystickInfo[ joystick ].Name ); if (SetDIDwordProperty(lpdiJoystick[joystick], DIPROP_DEADZONE, DIJOFS_RZ, DIPH_BYOFFSET, (DWORD)(JoystickInfo[joystick].Axis[AXIS_RzAxis].deadzone * 100)) != DI_OK) { // cannot set deadzone - but just ignore for now... DeadzoneNotSet = true; DebugPrintf( "SetUpJoystickAxis: failed to set deadzone for Rz axis\n" ); } } } for (i = 0; i < 2; i++) { if (JoystickInfo[joystick].Axis[AXIS_SliderAxis0 + i].exists) { DebugPrintf( "SetUpJoystickAxis: setting up Slider%d range=(%d,%d) for joystick #%d='%s'\n", i, diprg.lMin, diprg.lMax, joystick, JoystickInfo[ joystick ].Name ); diprg.diph.dwObj = DIJOFS_SLIDER(i); if(lpdiJoystick[joystick]->lpVtbl->SetProperty(lpdiJoystick[joystick], DIPROP_RANGE, &diprg.diph) != DI_OK) { JoystickInfo[joystick].Axis[AXIS_SliderAxis0 + i].exists = false; // cannot set range, therefore do not allow axis DebugPrintf( "SetUpJoystickAxis: failed to set range for Slider%d axis\n", i ); }else { DebugPrintf( "SetUpJoystickAxis: setting up Slider%d deadzone=%d for joystick #%d='%s'\n", i, JoystickInfo[ joystick ].Axis[AXIS_SliderAxis0 + i].deadzone, joystick, JoystickInfo[ joystick ].Name ); if (SetDIDwordProperty(lpdiJoystick[joystick], DIPROP_DEADZONE, DIJOFS_SLIDER(i), DIPH_BYOFFSET, (DWORD)(JoystickInfo[joystick].Axis[AXIS_SliderAxis0 + i].deadzone * 100)) != DI_OK) { // cannot set deadzone - but just ignore for now... DeadzoneNotSet = true; DebugPrintf( "SetUpJoystickAxis: failed to set deadzone for Slider%d axis\n", i ); } } } } // could do something about unset deadzones here... if (DeadzoneNotSet) DeadzoneNotSet = false; }