// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.Runtime.InteropServices; using SharpDX.Win32; namespace SharpDX.XInput { /// /// A XInput controller. /// public class Controller { private readonly UserIndex userIndex; private static readonly IXInput xinput; /// /// Initializes a new instance of the class. /// /// Index of the user. public Controller(UserIndex userIndex = UserIndex.Any) { if(xinput == null) { throw new NotSupportedException("XInput 1.4 or 1.3 or 9.1.0 is not installed"); } this.userIndex = userIndex; } /// /// Gets the associated with this controller. /// /// The index of the user. public UserIndex UserIndex { get { return this.userIndex; } } /// /// Gets the battery information. /// /// Type of the battery device. /// /// unsigned int XInputGetBatteryInformation([In] XUSER_INDEX dwUserIndex,[In] BATTERY_DEVTYPE devType,[Out] XINPUT_BATTERY_INFORMATION* pBatteryInformation) public BatteryInformation GetBatteryInformation(BatteryDeviceType batteryDeviceType) { BatteryInformation temp; var result = ErrorCodeHelper.ToResult(xinput.XInputGetBatteryInformation((int)userIndex, batteryDeviceType, out temp)); result.CheckError(); return temp; } /// /// Gets the capabilities. /// /// Type of the device query. /// /// unsigned int XInputGetCapabilities([In] XUSER_INDEX dwUserIndex,[In] XINPUT_DEVQUERYTYPE dwFlags,[Out] XINPUT_CAPABILITIES* pCapabilities) public Capabilities GetCapabilities(DeviceQueryType deviceQueryType) { Capabilities temp; var result = ErrorCodeHelper.ToResult(xinput.XInputGetCapabilities((int)userIndex, deviceQueryType, out temp)); result.CheckError(); return temp; } /// /// Gets the capabilities. /// /// Type of the device query. /// The capabilities of this controller. /// true if the controller is connected, false otherwise. public bool GetCapabilities(DeviceQueryType deviceQueryType, out Capabilities capabilities) { return xinput.XInputGetCapabilities((int)userIndex, deviceQueryType, out capabilities) == 0; } /// /// Gets the keystroke. /// /// The flag. /// The keystroke. /// /// unsigned int XInputGetKeystroke([In] XUSER_INDEX dwUserIndex,[In] unsigned int dwReserved,[Out] XINPUT_KEYSTROKE* pKeystroke) public Result GetKeystroke(DeviceQueryType deviceQueryType, out Keystroke keystroke) { var result = ErrorCodeHelper.ToResult(xinput.XInputGetKeystroke((int)userIndex, (int)deviceQueryType, out keystroke)); return result; } /// /// Gets the state. /// /// The state of this controller. public State GetState() { State temp; var result = ErrorCodeHelper.ToResult(xinput.XInputGetState((int)userIndex, out temp)); result.CheckError(); return temp; } /// /// Gets the state. /// /// The state of this controller. /// true if the controller is connected, false otherwise. public bool GetState(out State state) { return xinput.XInputGetState((int)userIndex, out state) == 0; } /// /// Sets the reporting. /// /// if set to true [enable reporting]. public static void SetReporting(bool enableReporting) { if(xinput != null) { xinput.XInputEnable(enableReporting); } } /// /// Sets the vibration. /// /// The vibration. /// public Result SetVibration(Vibration vibration) { var result = ErrorCodeHelper.ToResult(xinput.XInputSetState((int)userIndex, vibration)); result.CheckError(); return result; } /// /// Gets a value indicating whether this instance is connected. /// /// /// true if this instance is connected; otherwise, false. /// public bool IsConnected { get { State temp; return xinput.XInputGetState((int)userIndex, out temp) == 0; } } #if !WINDOWS_UWP static Controller() { if(LoadLibrary("xinput1_4.dll") != IntPtr.Zero) { xinput = new XInput14(); } else if (LoadLibrary("xinput1_3.dll") != IntPtr.Zero) { xinput = new XInput13(); } else if (LoadLibrary("xinput9_1_0.dll") != IntPtr.Zero) { xinput = new XInput910(); } } [DllImport("kernel32", CharSet = CharSet.Unicode, EntryPoint = "LoadLibrary", SetLastError = true)] private static extern IntPtr LoadLibrary(string lpFileName); #else static Controller() { xinput = new XInput14(); } #endif } }