/// <summary> /// ctor and init /// </summary> /// <param name="device">A DXInput device</param> /// <param name="hwnd">The WinHandle of the main window</param> /// <param name="joystickNum">The 0.. n-1 Joystick from DX enum</param> public DevJoystick(SharpDX.DirectInput.Joystick device, Control hwnd) { // log.DebugFormat( "JoystickCls ctor - Entry with {0}", device.Information.ProductName ); m_device = device; m_hwnd = hwnd; // Set BufferSize in order to use buffered data. m_device.Properties.BufferSize = 128; // log.Debug( "Get JS Objects" ); try { // Set the data format to the c_dfDIJoystick pre-defined format. //m_device.SetDataFormat( DeviceDataFormat.Joystick ); // Set the cooperative level for the device. m_device.SetCooperativeLevel(m_hwnd, CooperativeLevel.NonExclusive | CooperativeLevel.Background); // Enumerate all the objects on the device. foreach (DeviceObjectInstance d in m_device.GetObjects( )) { // For axes that are returned, set the DIPROP_RANGE property for the // enumerated axis in order to scale min/max values. if ((0 != (d.ObjectId.Flags & DeviceObjectTypeFlags.Axis))) { // Set the range for the axis. m_device.Properties.Range = new InputRange(-1000, +1000); } } StartEventHandling( ); } catch (Exception ex) { Console.WriteLine("Get JS Objects failed:\n{0}", ex); } DevJoystick.RegisteredDevices++; }
/// <summary> /// ctor and init /// </summary> /// <param name="device">A DXInput device</param> /// <param name="hwnd">The WinHandle of the main window</param> /// <param name="joystickNum">The 0.. n-1 Joystick from DX enum</param> /// <param name="panel">The respective JS panel to show the properties</param> /// <param name="tabIndex">The Tab index in the GUI</param> public JoystickCls(SharpDX.DirectInput.Joystick device, Control hwnd, int joystickNum, UC_JoyPanel panel, int tabIndex) { log.DebugFormat("JoystickCls ctor - Entry with {0}", device.Information.ProductName); m_device = device; m_hwnd = hwnd; m_joystickNumber = joystickNum; // this remains fixed m_xmlInstance = joystickNum + 1; // initial assignment (is 1 based..) m_jPanel = panel; MyTabPageIndex = tabIndex; Activated_low = false; m_senseLimit = AppConfiguration.AppConfig.jsSenseLimit; // can be changed in the app.config file if it is still too little // Set BufferSize in order to use buffered data. m_device.Properties.BufferSize = 128; m_jPanel.Caption = m_device.Properties.ProductName; m_jPanel.nAxis = AxisCount.ToString( ); m_jPanel.nButtons = ButtonCount.ToString( ); m_jPanel.nPOVs = POVCount.ToString( ); m_jPanel.JsAssignment = 0; // default is no assignment m_ignoreButtons = new bool[m_state.Buttons.Length]; ResetButtons(m_ignoreButtons); m_modifierButtons = new bool[m_state.Buttons.Length]; ResetButtons(m_modifierButtons); log.Debug("Get JS Objects"); try { // Set the data format to the c_dfDIJoystick pre-defined format. //m_device.SetDataFormat( DeviceDataFormat.Joystick ); // Set the cooperative level for the device. m_device.SetCooperativeLevel(m_hwnd, CooperativeLevel.NonExclusive | CooperativeLevel.Background); // Enumerate all the objects on the device. foreach (DeviceObjectInstance d in m_device.GetObjects( )) { // For axes that are returned, set the DIPROP_RANGE property for the // enumerated axis in order to scale min/max values. if ((0 != (d.ObjectId.Flags & DeviceObjectTypeFlags.Axis))) { // Set the range for the axis. m_device.Properties.Range = new InputRange(-1000, +1000); } // Update the controls to reflect what objects the device supports. UpdateControls(d); } } catch (Exception ex) { log.Error("Get JS Objects failed", ex); } ApplySettings_low( ); // get whatever is needed here from Settings JoystickCls.RegisteredDevices++; Activated_low = true; }
/// <summary> /// Function to perform the creation of the DirectInput joystick object. /// </summary> /// <param name="directInput">The direct input interface.</param> /// <param name="deviceInfo">The device information for the gaming device to use.</param> /// <returns>The DirectInput joystick object.</returns> private DI.Joystick CreateJoystick(DI.DirectInput directInput, DirectInputDeviceInfo deviceInfo) { var result = new DI.Joystick(directInput, deviceInfo.InstanceGuid); IntPtr mainWindow = FindMainApplicationWindow(); if (mainWindow == IntPtr.Zero) { // We have no main window, and DI requires one, so we need to kill this. throw new InvalidOperationException(Resources.GORINP_ERR_DI_COULD_NOT_FIND_APP_WINDOW); } result.SetCooperativeLevel(mainWindow, DI.CooperativeLevel.Foreground | DI.CooperativeLevel.NonExclusive); result.Properties.AxisMode = DI.DeviceAxisMode.Absolute; // Set up dead zones. foreach (GorgonGamingDeviceAxis axis in Axis) { // Skip the throttle. Dead zones on the throttle don't work too well for regular joysticks. // Game pads may be another story, but the user can manage those settings if required. if (axis.Axis == GamingDeviceAxis.Throttle) { continue; } GorgonGamingDeviceAxisInfo info = Info.AxisInfo[axis.Axis]; DI.ObjectProperties properties = result.GetObjectPropertiesById(_info.AxisMappings[axis.Axis]); if (properties == null) { continue; } // Set a 0 dead zone. properties.DeadZone = 0; // Make the dead zone 10% of the range for all axes. float deadZone = axis.Axis == GamingDeviceAxis.Throttle ? 0.02f : 0.10f; int deadZoneActual = (int)(info.Range.Range * deadZone); axis.DeadZone = new GorgonRange(info.DefaultValue - deadZoneActual, info.DefaultValue + deadZoneActual); } return(result); }