// Get a DirectInput-compatible Guid // (equivalent to DIDEVICEINSTANCE guidProduct field) private Guid GetDeviceGuid(IntPtr handle) { // Retrieve a RID_DEVICE_INFO struct which contains the VID and PID RawInputDeviceInfo info = new RawInputDeviceInfo(); int size = info.Size; if (Functions.GetRawInputDeviceInfo(handle, RawInputDeviceInfoEnum.DEVICEINFO, info, ref size) < 0) { Debug.Print("[WinRawJoystick] Functions.GetRawInputDeviceInfo(DEVICEINFO) failed with error {0}", Marshal.GetLastWin32Error()); return(Guid.Empty); } // Todo: this Guid format is only valid for USB joysticks. // Bluetooth devices, such as OUYA controllers, have a totally // different PID/VID format in DirectInput. // Do we need to use the same guid or could we simply use PID/VID // there too? (Test with an OUYA controller.) int vid = info.Device.HID.VendorId; int pid = info.Device.HID.ProductId; return(new Guid( (pid << 16) | vid, 0, 0, 0, 0, (byte)'P', (byte)'I', (byte)'D', (byte)'V', (byte)'I', (byte)'D')); }
internal RawInputDigitizer(RawInputDeviceHandle device, RawInputDeviceInfo deviceInfo) : base(device, deviceInfo) { if (!IsSupported(deviceInfo.Hid.UsageAndPage)) { throw new ArgumentException($"UsagePage and Usage {deviceInfo.Hid.UsageAndPage} is not supported as a digitizer.", nameof(deviceInfo)); } }
internal RawInputMouse(RawInputDeviceHandle device, RawInputDeviceInfo deviceInfo) : base(device, deviceInfo) { if (deviceInfo.Type != RawInputDeviceType.Mouse) { throw new ArgumentException($"Device type must be {RawInputDeviceType.Mouse}.", nameof(deviceInfo)); } }
internal RawInputKeyboard(RawInputDeviceHandle device, RawInputDeviceInfo deviceInfo) : base(device, deviceInfo) { if (deviceInfo.Type != RawInputDeviceType.Keyboard) { throw new ArgumentException($"Device type must be {RawInputDeviceType.Keyboard}", nameof(deviceInfo)); } }
internal RawInputHid(RawInputDeviceHandle device, RawInputDeviceInfo deviceInfo) : base(device, deviceInfo) { if (deviceInfo.Type != RawInputDeviceType.Hid) { throw new ArgumentException($"Device type must be {RawInputDeviceType.Hid}.", nameof(deviceInfo)); } hidReader = new Lazy <HidReader>(() => new HidReader(GetPreparsedData())); }
internal static extern uint GetRawInputDeviceInfo(IntPtr hDevice, RawInputDeviceInfo command, IntPtr pData, ref uint size);
internal static extern uint GetRawInputDeviceInfo(IntPtr hDevice, RawInputDeviceInfo command, IntPtr pData, ref uint size);
public void RefreshDevices() { lock (UpdateLock) { // Mark all devices as disconnected. We will check which of those // are connected later on. for (int i = 0; i < mice.Count; i++) { MouseState state = mice[i]; state.IsConnected = false; mice[i] = state; } // Discover mouse devices foreach (RawInputDeviceList dev in WinRawInput.GetDeviceList()) { ContextHandle id = new ContextHandle(dev.Device); if (rawids.ContainsKey(id)) { // Device already registered, mark as connected MouseState state = mice[rawids[id]]; state.IsConnected = true; mice[rawids[id]] = state; continue; } // Unregistered device, find what it is string name = GetDeviceName(dev); if (name.ToLower().Contains("root")) { // This is a terminal services device, skip it. continue; } else if (dev.Type == RawInputDeviceType.MOUSE || dev.Type == RawInputDeviceType.HID) { // This is a mouse or a USB mouse device. In the latter case, discover if it really is a // mouse device by qeurying the registry. RegistryKey regkey = FindRegistryKey(name); if (regkey == null) { continue; } string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class") as string; if (deviceClass == null) { // Added to address osuTK issue 3198 with mouse on Windows 8 string deviceClassGUID = (string)regkey.GetValue("ClassGUID"); RegistryKey classGUIDKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\" + deviceClassGUID); deviceClass = classGUIDKey != null ? (string)classGUIDKey.GetValue("Class") : string.Empty; } // deviceDesc remained null on a new Win7 system - not sure why. // Since the description is not vital information, use a dummy description // when that happens. if (String.IsNullOrEmpty(deviceDesc)) { deviceDesc = "Windows Mouse " + mice.Count; } else { deviceDesc = deviceDesc.Substring(deviceDesc.LastIndexOf(';') + 1); } if (!String.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("mouse")) { if (!rawids.ContainsKey(new ContextHandle(dev.Device))) { // Register the device: RawInputDeviceInfo info = new RawInputDeviceInfo(); int devInfoSize = API.RawInputDeviceInfoSize; Functions.GetRawInputDeviceInfo(dev.Device, RawInputDeviceInfoEnum.DEVICEINFO, info, ref devInfoSize); RegisterRawDevice(Window, deviceDesc); MouseState state = new MouseState(); state.IsConnected = true; mice.Add(state); names.Add(deviceDesc); rawids.Add(new ContextHandle(dev.Device), mice.Count - 1); } } } } } }
public void RefreshDevices() { lock (UpdateLock) { for (int i = 0; i < keyboards.Count; i++) { KeyboardState state = keyboards[i]; state.IsConnected = false; keyboards[i] = state; } int count = WinRawInput.DeviceCount; RawInputDeviceList[] ridl = new RawInputDeviceList[count]; for (int i = 0; i < count; i++) { ridl[i] = new RawInputDeviceList(); } Functions.GetRawInputDeviceList(ridl, ref count, API.RawInputDeviceListSize); // Discover keyboard devices: foreach (RawInputDeviceList dev in ridl) { ContextHandle id = new ContextHandle(dev.Device); if (rawids.ContainsKey(id)) { // Device already registered, mark as connected KeyboardState state = keyboards[rawids[id]]; state.IsConnected = true; keyboards[rawids[id]] = state; continue; } string name = GetDeviceName(dev); if (name.ToLower().Contains("root")) { // This is a terminal services device, skip it. continue; } else if (dev.Type == RawInputDeviceType.KEYBOARD || dev.Type == RawInputDeviceType.HID) { // This is a keyboard or USB keyboard device. In the latter case, discover if it really is a // keyboard device by qeurying the registry. RegistryKey regkey = GetRegistryKey(name); if (regkey == null) { continue; } string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class"); string deviceClassGUID = (string)regkey.GetValue("ClassGUID"); // for windows 8 support via osuTK issue 3198 // making a guess at backwards compatability. Not sure what older windows returns in these cases... if (deviceClass == null || deviceClass.Equals(string.Empty)) { RegistryKey classGUIDKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\" + deviceClassGUID); deviceClass = classGUIDKey != null ? (string)classGUIDKey.GetValue("Class") : string.Empty; } if (String.IsNullOrEmpty(deviceDesc)) { Debug.Print("[Warning] Failed to retrieve device description, skipping this device."); continue; } else { deviceDesc = deviceDesc.Substring(deviceDesc.LastIndexOf(';') + 1); } if (!String.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("keyboard")) { // Register the keyboard: RawInputDeviceInfo info = new RawInputDeviceInfo(); int devInfoSize = API.RawInputDeviceInfoSize; Functions.GetRawInputDeviceInfo(dev.Device, RawInputDeviceInfoEnum.DEVICEINFO, info, ref devInfoSize); //KeyboardDevice kb = new KeyboardDevice(); //kb.Description = deviceDesc; //kb.NumberOfLeds = info.Device.Keyboard.NumberOfIndicators; //kb.NumberOfFunctionKeys = info.Device.Keyboard.NumberOfFunctionKeys; //kb.NumberOfKeys = info.Device.Keyboard.NumberOfKeysTotal; //kb.DeviceID = dev.Device; RegisterKeyboardDevice(window, deviceDesc); KeyboardState state = new KeyboardState(); state.IsConnected = true; keyboards.Add(state); names.Add(deviceDesc); rawids.Add(new ContextHandle(dev.Device), keyboards.Count - 1); } } } } }
public static extern uint GetRawInputDeviceInfo(IntPtr hDevice, RawInputUiCommand uiCommand, out RawInputDeviceInfo pData, uint pcbSize);