/// <summary> /// ctor and init /// </summary> /// <param name="device">A DXInput device</param> /// <param name="hwnd">The WinHandle of the main window</param> /// <param name="panel">The respective JS panel to show the properties</param> public GamepadCls( SharpDX.XInput.Controller device, UC_GpadPanel panel, int tabIndex ) { log.DebugFormat( "GamepadCls ctor - Entry with index {0}", device.ToString( ) ); m_device = device; m_gPanel = panel; MyTabPageIndex = tabIndex; Activated = false; m_senseLimit = AppConfiguration.AppConfig.gpSenseLimit; // can be changed in the app.config file if it is still too little // Set BufferSize in order to use buffered data. log.Debug( "Get GP Objects" ); try { m_gpCaps = m_device.GetCapabilities( DeviceQueryType.Gamepad ); } catch ( Exception ex ) { log.Error( "Get GamepadCapabilities failed", ex ); } m_gPanel.Caption = DevName; int n = 0; if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.DPadDown ) ) n++; if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.DPadLeft ) ) n++; if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.DPadRight ) ) n++; if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.DPadUp ) ) n++; m_gPanel.nDPads = n.ToString( ); m_gPanel.DPadE = ( n > 0 ); n = 0; if ( ( m_gpCaps.Gamepad.LeftThumbX != 0 ) || ( m_gpCaps.Gamepad.LeftThumbY != 0 ) || Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.LeftThumb ) ) { n++; m_gPanel.TStickLE = true; } if ( ( m_gpCaps.Gamepad.RightThumbX != 0 ) || ( m_gpCaps.Gamepad.RightThumbY != 0 ) || Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.RightThumb ) ) { n++; m_gPanel.TStickRE = true; } m_gPanel.nTSticks = n.ToString( ); n = 0; if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.A ) ) n++; if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.B ) ) n++; if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.X ) ) n++; if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.Y ) ) n++; if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.Start ) ) { n++; m_gPanel.StartE = true; } if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.Back ) ) { n++; m_gPanel.BackE = true; } if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.LeftShoulder ) ) { n++; m_gPanel.ShoulderLE = true; } if ( Bit( m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.RightShoulder ) ) { n++; m_gPanel.ShoulderRE = true; } m_gPanel.nButtons = n.ToString( ); n = 0; if ( m_gpCaps.Gamepad.LeftTrigger > 0 ) { n++; m_gPanel.TriggerLE = true; } if ( m_gpCaps.Gamepad.RightTrigger > 0 ) { n++; m_gPanel.TriggerRE = true; } m_gPanel.nTriggers = n.ToString( ); m_gPanel.ButtonE = true; // what else ... ApplySettings( ); // get whatever is needed here from Settings Activated = true; }
/// <summary> /// ctor and init /// </summary> /// <param name="device">A DXInput device</param> /// <param name="hwnd">The WinHandle of the main window</param> /// <param name="panel">The respective JS panel to show the properties</param> public GamepadCls(SharpDX.XInput.Controller device, UC_GpadPanel panel, int tabIndex) { log.DebugFormat("GamepadCls ctor - Entry with index {0}", device.ToString( )); m_device = device; m_gPanel = panel; MyTabPageIndex = tabIndex; Activated = false; m_senseLimit = AppConfiguration.AppConfig.gpSenseLimit; // can be changed in the app.config file if it is still too little // Set BufferSize in order to use buffered data. log.Debug("Get GP Objects"); try { m_gpCaps = m_device.GetCapabilities(DeviceQueryType.Gamepad); } catch (Exception ex) { log.Error("Get GamepadCapabilities failed", ex); } m_gPanel.Caption = DevName; int n = 0; if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.DPadDown)) { n++; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.DPadLeft)) { n++; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.DPadRight)) { n++; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.DPadUp)) { n++; } m_gPanel.nDPads = n.ToString( ); m_gPanel.DPadE = (n > 0); n = 0; if ((m_gpCaps.Gamepad.LeftThumbX != 0) || (m_gpCaps.Gamepad.LeftThumbY != 0) || Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.LeftThumb)) { n++; m_gPanel.TStickLE = true; } if ((m_gpCaps.Gamepad.RightThumbX != 0) || (m_gpCaps.Gamepad.RightThumbY != 0) || Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.RightThumb)) { n++; m_gPanel.TStickRE = true; } m_gPanel.nTSticks = n.ToString( ); n = 0; if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.A)) { n++; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.B)) { n++; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.X)) { n++; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.Y)) { n++; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.Start)) { n++; m_gPanel.StartE = true; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.Back)) { n++; m_gPanel.BackE = true; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.LeftShoulder)) { n++; m_gPanel.ShoulderLE = true; } if (Bit(m_gpCaps.Gamepad.Buttons, GamepadButtonFlags.RightShoulder)) { n++; m_gPanel.ShoulderRE = true; } m_gPanel.nButtons = n.ToString( ); n = 0; if (m_gpCaps.Gamepad.LeftTrigger > 0) { n++; m_gPanel.TriggerLE = true; } if (m_gpCaps.Gamepad.RightTrigger > 0) { n++; m_gPanel.TriggerRE = true; } m_gPanel.nTriggers = n.ToString( ); m_gPanel.ButtonE = true; // what else ... ApplySettings( ); // get whatever is needed here from Settings Activated = true; }
/// <summary> /// Aquire the DInput joystick devices /// </summary> /// <returns></returns> public bool InitDirectInput( ) { log.Debug( "Entry" ); // Enumerate joysticks in the system. int tabs = 0; SharpDX.XInput.UserIndex gpDeviceIndex = SharpDX.XInput.UserIndex.Any; try { // Initialize DirectInput log.Debug( "Instantiate DirectInput" ); var directInput = new DirectInput( ); log.Debug( "Get Keyboard device" ); m_Keyboard = new KeyboardCls( new Keyboard( directInput ), this ); // scan the Input for attached devices log.Debug( "Scan GameControl devices" ); int nJs = 1; // number the Joystick Tabs foreach ( DeviceInstance instance in directInput.GetDevices( DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly ) ) { log.InfoFormat( "GameControl: #{0} Type:{1} Device:{2}", tabs, instance.Type.ToString( ), instance.ProductName ); // Create the device interface log.Debug( "Create the device interface" ); SharpDX.DirectInput.Joystick jsDevice = null; SharpDX.XInput.Controller gpDevice = null; JoystickCls js = null; GamepadCls gs = null; if ( m_AppSettings.DetectGamepad && ( instance.Usage == SharpDX.Multimedia.UsageId.GenericGamepad ) ) { // detect Gamepad only if the user wishes to do so for ( SharpDX.XInput.UserIndex i = SharpDX.XInput.UserIndex.One; i < SharpDX.XInput.UserIndex.Four; i++ ) { gpDevice = new SharpDX.XInput.Controller( i ); if ( gpDevice.IsConnected ) { log.InfoFormat( "Scan Input {0} for gamepad - {1}", i, gpDevice.GetCapabilities( SharpDX.XInput.DeviceQueryType.Gamepad ).ToString( ) ); gpDeviceIndex = i; break; } } } else { jsDevice = new Joystick( directInput, instance.InstanceGuid ); log.DebugFormat( "Create the device interface for: {0}", jsDevice.Information.ProductName ); } // we have the first tab made as reference so TabPage[0] already exists if ( tabs == 0 ) { // first panel - The Tab content exists already if ( gpDevice != null ) { log.Debug( "Add first Gamepad panel" ); tc1.TabPages[tabs].Text = "Gamepad "; UC_GpadPanel uUC_GpadPanelNew = new UC_GpadPanel( ); tc1.TabPages[tabs].Controls.Add( uUC_GpadPanelNew ); uUC_GpadPanelNew.Size = UC_JoyPanel.Size; uUC_GpadPanelNew.Location = UC_JoyPanel.Location; UC_JoyPanel.Enabled = false; UC_JoyPanel.Visible = false; // don't use this one log.Debug( "Create Gamepad instance" ); gs = new GamepadCls( gpDevice, uUC_GpadPanelNew, tabs ); // does all device related activities for that particular item gs.SetDeviceName( instance.ProductName ); tc1.TabPages[tabs].ToolTipText = String.Format( "{0}\n{1}", gs.DevName, " " ); toolTip1.SetToolTip( tc1.TabPages[tabs], tc1.TabPages[tabs].ToolTipText ); } else { log.Debug( "Add first Joystick panel" ); log.Debug( "Create Joystick instance" ); tc1.TabPages[tabs].Text = String.Format( "Joystick {0}", nJs++ ); js = new JoystickCls( jsDevice, this, tabs + 1, UC_JoyPanel, tabs ); // does all device related activities for that particular item tc1.TabPages[tabs].ToolTipText = String.Format( "{0}\n{1}", js.DevName, js.DevInstanceGUID ); toolTip1.SetToolTip( tc1.TabPages[tabs], tc1.TabPages[tabs].ToolTipText ); } } else { if ( gpDevice != null ) { log.Debug( "Add next Gamepad panel" ); tc1.TabPages.Add( "Gamepad " ); UC_GpadPanel uUC_GpadPanelNew = new UC_GpadPanel( ); tc1.TabPages[tabs].Controls.Add( uUC_GpadPanelNew ); uUC_GpadPanelNew.Size = UC_JoyPanel.Size; uUC_GpadPanelNew.Location = UC_JoyPanel.Location; log.Debug( "Create Gamepad instance" ); gs = new GamepadCls( gpDevice, uUC_GpadPanelNew, tabs ); // does all device related activities for that particular item gs.SetDeviceName( instance.ProductName ); tc1.TabPages[tabs].ToolTipText = String.Format( "{0}\n{1}", gs.DevName, " " ); toolTip1.SetToolTip( tc1.TabPages[tabs], tc1.TabPages[tabs].ToolTipText ); } else { log.Debug( "Add next Joystick panel" ); // setup the further tab contents along the reference one in TabPage[0] (the control named UC_JoyPanel) tc1.TabPages.Add( String.Format( "Joystick {0}", nJs++ ) ); UC_JoyPanel uUC_JoyPanelNew = new UC_JoyPanel( ); tc1.TabPages[tabs].Controls.Add( uUC_JoyPanelNew ); uUC_JoyPanelNew.Size = UC_JoyPanel.Size; uUC_JoyPanelNew.Location = UC_JoyPanel.Location; log.Debug( "Create Joystick instance" ); js = new JoystickCls( jsDevice, this, tabs + 1, uUC_JoyPanelNew, tabs ); // does all device related activities for that particular item tc1.TabPages[tabs].ToolTipText = String.Format( "{0}\n{1}", js.DevName, js.DevInstanceGUID ); toolTip1.SetToolTip( tc1.TabPages[tabs], tc1.TabPages[tabs].ToolTipText ); } } if ( gpDevice != null ) { m_Gamepad = gs; SetGamepadTab( tc1.TabPages[tabs] ); // indicates the gamepad tab (murks..) MyColors.GamepadColor = MyColors.TabColor[tabs]; // save it for future use } else if ( js != null ) { m_Joystick.Add( js ); // add to joystick list tc1.TabPages[tabs].Tag = ( m_Joystick.Count - 1 ); // used to find the tab for polling } tc1.TabPages[tabs].BackColor = MyColors.TabColor[tabs]; // each tab has its own color // next tab tabs++; if ( tabs >= JoystickCls.JSnum_MAX ) break; // cannot load more JSticks than predefined Tabs } log.DebugFormat( "Added {0} GameControl devices", tabs ); if ( tabs == 0 ) { log.Warn( "Unable to find and/or create any joystick devices." ); MessageBox.Show( "Unable to create a joystick device. Program will exit.", "No joystick found", MessageBoxButtons.OK, MessageBoxIcon.Information ); return false; } // load the profile items from the XML log.Debug( "Init ActionTree" ); InitActionTree( true ); } catch ( Exception ex ) { log.Debug( "InitDirectInput failed unexpectedly", ex ); return false; } return true; }