// Get a DirectInput-compatible Guid // (equivalent to DIDEVICEINSTANCE guidProduct field) 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')); }
public void RefreshDevices() { lock (this.UpdateLock) { for (int local_0 = 0; local_0 < this.keyboards.Count; ++local_0) { KeyboardState local_1 = this.keyboards[local_0]; local_1.IsConnected = false; this.keyboards[local_0] = local_1; } int local_2 = WinRawInput.DeviceCount; RawInputDeviceList[] local_3 = new RawInputDeviceList[local_2]; for (int local_4 = 0; local_4 < local_2; ++local_4) { local_3[local_4] = new RawInputDeviceList(); } Functions.GetRawInputDeviceList(local_3, out local_2, API.RawInputDeviceListSize); foreach (RawInputDeviceList item_0 in local_3) { ContextHandle local_6 = new ContextHandle(item_0.Device); if (this.rawids.ContainsKey(local_6)) { KeyboardState local_7 = this.keyboards[this.rawids[local_6]]; local_7.IsConnected = true; this.keyboards[this.rawids[local_6]] = local_7; } else { string local_8 = WinRawKeyboard.GetDeviceName(item_0); if (!local_8.ToLower().Contains("root") && (item_0.Type == RawInputDeviceType.KEYBOARD || item_0.Type == RawInputDeviceType.HID)) { RegistryKey local_9 = WinRawKeyboard.GetRegistryKey(local_8); string local_10 = (string)local_9.GetValue("DeviceDesc"); string local_11 = (string)local_9.GetValue("Class"); if (!string.IsNullOrEmpty(local_10)) { string local_10_1 = local_10.Substring(local_10.LastIndexOf(';') + 1); if (!string.IsNullOrEmpty(local_11) && local_11.ToLower().Equals("keyboard")) { RawInputDeviceInfo local_12 = new RawInputDeviceInfo(); int local_13 = API.RawInputDeviceInfoSize; Functions.GetRawInputDeviceInfo(item_0.Device, RawInputDeviceInfoEnum.DEVICEINFO, local_12, out local_13); WinRawKeyboard.RegisterKeyboardDevice(this.window, local_10_1); this.keyboards.Add(new KeyboardState() { IsConnected = true }); this.names.Add(local_10_1); this.rawids.Add(new ContextHandle(item_0.Device), this.keyboards.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); string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class"); 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 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 OpenTK 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 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 OpenTK 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 (this.UpdateLock) { for (int local_0 = 0; local_0 < this.keyboards.Count; ++local_0) { KeyboardState local_1 = this.keyboards[local_0]; local_1.IsConnected = false; this.keyboards[local_0] = local_1; } int local_2 = WinRawInput.DeviceCount; RawInputDeviceList[] local_3 = new RawInputDeviceList[local_2]; for (int local_4 = 0; local_4 < local_2; ++local_4) local_3[local_4] = new RawInputDeviceList(); Functions.GetRawInputDeviceList(local_3, out local_2, API.RawInputDeviceListSize); foreach (RawInputDeviceList item_0 in local_3) { ContextHandle local_6 = new ContextHandle(item_0.Device); if (this.rawids.ContainsKey(local_6)) { KeyboardState local_7 = this.keyboards[this.rawids[local_6]]; local_7.IsConnected = true; this.keyboards[this.rawids[local_6]] = local_7; } else { string local_8 = WinRawKeyboard.GetDeviceName(item_0); if (!local_8.ToLower().Contains("root") && (item_0.Type == RawInputDeviceType.KEYBOARD || item_0.Type == RawInputDeviceType.HID)) { RegistryKey local_9 = WinRawKeyboard.GetRegistryKey(local_8); string local_10 = (string) local_9.GetValue("DeviceDesc"); string local_11 = (string) local_9.GetValue("Class"); if (!string.IsNullOrEmpty(local_10)) { string local_10_1 = local_10.Substring(local_10.LastIndexOf(';') + 1); if (!string.IsNullOrEmpty(local_11) && local_11.ToLower().Equals("keyboard")) { RawInputDeviceInfo local_12 = new RawInputDeviceInfo(); int local_13 = API.RawInputDeviceInfoSize; Functions.GetRawInputDeviceInfo(item_0.Device, RawInputDeviceInfoEnum.DEVICEINFO, local_12, out local_13); WinRawKeyboard.RegisterKeyboardDevice(this.window, local_10_1); this.keyboards.Add(new KeyboardState() { IsConnected = true }); this.names.Add(local_10_1); this.rawids.Add(new ContextHandle(item_0.Device), this.keyboards.Count - 1); } } } } } } }
internal void UpdateKeyboardList() { 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: for (int i = 0; i < count; i++) { uint size = 0; Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICENAME, IntPtr.Zero, ref size); IntPtr name_ptr = Marshal.AllocHGlobal((IntPtr)size); Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICENAME, name_ptr, ref size); string name = Marshal.PtrToStringAnsi(name_ptr); Marshal.FreeHGlobal(name_ptr); if (name.ToLower().Contains("root")) { // This is a terminal services device, skip it. continue; } else if (ridl[i].Type == RawInputDeviceType.KEYBOARD || ridl[i].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. // remove the \??\ name = name.Substring(4); string[] split = name.Split('#'); string id_01 = split[0]; // ACPI (Class code) string id_02 = split[1]; // PNP0303 (SubClass code) string id_03 = split[2]; // 3&13c0b0c5&0 (Protocol code) // The final part is the class GUID and is not needed here string findme = string.Format( @"System\CurrentControlSet\Enum\{0}\{1}\{2}", id_01, id_02, id_03); RegistryKey regkey = Registry.LocalMachine.OpenSubKey(findme); string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class"); if (!String.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("keyboard")) { KeyboardDevice kb = new KeyboardDevice(); kb.Description = deviceDesc; // Register the keyboard: RawInputDeviceInfo info = new RawInputDeviceInfo(); int devInfoSize = API.RawInputDeviceInfoSize; Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICEINFO, info, ref devInfoSize); kb.NumberOfLeds = info.Device.Keyboard.NumberOfIndicators; kb.NumberOfFunctionKeys = info.Device.Keyboard.NumberOfFunctionKeys; kb.NumberOfKeys = info.Device.Keyboard.NumberOfKeysTotal; //kb.DeviceID = (info.Device.Keyboard.Type << 32) + info.Device.Keyboard.SubType; kb.DeviceID = ridl[i].Device; //if (!keyboards.Contains(kb)) //{ this.RegisterKeyboardDevice(kb); keyboards.Add(kb); //} } } } }
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; } 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 mouse devices foreach (RawInputDeviceList dev in ridl) { 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); string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class") as string; if(deviceClass == null){ // Added to address OpenTK 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 = 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) { // 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; } 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 mouse devices foreach (RawInputDeviceList dev in ridl) { 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); string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class"); 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); } } } } } }
internal void UpdateKeyboardList() { 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: for (int i = 0; i < count; i++) { uint size = 0; Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICENAME, IntPtr.Zero, ref size); IntPtr name_ptr = Marshal.AllocHGlobal((IntPtr)size); Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICENAME, name_ptr, ref size); string name = Marshal.PtrToStringAnsi(name_ptr); Marshal.FreeHGlobal(name_ptr); if (name.ToLower().Contains("root")) { // This is a terminal services device, skip it. continue; } else if (ridl[i].Type == RawInputDeviceType.KEYBOARD || ridl[i].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. // remove the \??\ name = name.Substring(4); string[] split = name.Split('#'); string id_01 = split[0]; // ACPI (Class code) string id_02 = split[1]; // PNP0303 (SubClass code) string id_03 = split[2]; // 3&13c0b0c5&0 (Protocol code) // The final part is the class GUID and is not needed here string findme = string.Format( @"System\CurrentControlSet\Enum\{0}\{1}\{2}", id_01, id_02, id_03); RegistryKey regkey = Registry.LocalMachine.OpenSubKey(findme); string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class"); if (!String.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("keyboard")) { KeyboardDevice kb = new KeyboardDevice(); kb.Description = deviceDesc; // Register the keyboard: RawInputDeviceInfo info = new RawInputDeviceInfo(); int devInfoSize = API.RawInputDeviceInfoSize; Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICEINFO, info, ref devInfoSize); kb.NumberOfLeds = info.Device.Keyboard.NumberOfIndicators; kb.NumberOfFunctionKeys = info.Device.Keyboard.NumberOfFunctionKeys; kb.NumberOfKeys = info.Device.Keyboard.NumberOfKeysTotal; //kb.DeviceID = (info.Device.Keyboard.Type << 32) + info.Device.Keyboard.SubType; kb.DeviceID = ridl[i].Device; //if (!keyboards.Contains(kb)) //{ this.RegisterKeyboardDevice(kb); keyboards.Add(kb); //} } } } }
public int RegisterDevices() { 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 mouse devices: for (int i = 0; i < count; i++) { uint size = 0; Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICENAME, IntPtr.Zero, ref size); IntPtr name_ptr = Marshal.AllocHGlobal((IntPtr)size); Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICENAME, name_ptr, ref size); string name = Marshal.PtrToStringAnsi(name_ptr); Marshal.FreeHGlobal(name_ptr); if (name.ToLower().Contains("root")) { // This is a terminal services device, skip it. continue; } else if (ridl[i].Type == RawInputDeviceType.MOUSE || ridl[i].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. // remove the \??\ name = name.Substring(4); string[] split = name.Split('#'); string id_01 = split[0]; // ACPI (Class code) string id_02 = split[1]; // PNP0303 (SubClass code) string id_03 = split[2]; // 3&13c0b0c5&0 (Protocol code) // The final part is the class GUID and is not needed here string findme = string.Format( @"System\CurrentControlSet\Enum\{0}\{1}\{2}", id_01, id_02, id_03); RegistryKey regkey = Registry.LocalMachine.OpenSubKey(findme); string deviceDesc = (string)regkey.GetValue("DeviceDesc"); deviceDesc = deviceDesc.Substring(deviceDesc.LastIndexOf(';') + 1); string deviceClass = (string)regkey.GetValue("Class"); if (!String.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("mouse")) { OpenTK.Input.MouseDevice mouse = new OpenTK.Input.MouseDevice(); mouse.Description = deviceDesc; // Register the keyboard: RawInputDeviceInfo info = new RawInputDeviceInfo(); int devInfoSize = API.RawInputDeviceInfoSize; Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICEINFO, info, ref devInfoSize); mouse.NumberOfButtons = info.Device.Mouse.NumberOfButtons; mouse.NumberOfWheels = info.Device.Mouse.HasHorizontalWheel ? 1 : 0; mouse.DeviceID = ridl[i].Device;//(IntPtr)info.Device.Mouse.Id; this.RegisterRawDevice(mouse); mice.Add(mouse); } } } return(count); }
public int RegisterDevices() { 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 mouse devices: for (int i = 0; i < count; i++) { uint size = 0; Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICENAME, IntPtr.Zero, ref size); IntPtr name_ptr = Marshal.AllocHGlobal((IntPtr)size); Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICENAME, name_ptr, ref size); string name = Marshal.PtrToStringAnsi(name_ptr); Marshal.FreeHGlobal(name_ptr); if (name.ToLower().Contains("root")) { // This is a terminal services device, skip it. continue; } else if (ridl[i].Type == RawInputDeviceType.MOUSE || ridl[i].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. // remove the \??\ name = name.Substring(4); string[] split = name.Split('#'); string id_01 = split[0]; // ACPI (Class code) string id_02 = split[1]; // PNP0303 (SubClass code) string id_03 = split[2]; // 3&13c0b0c5&0 (Protocol code) // The final part is the class GUID and is not needed here string findme = string.Format( @"System\CurrentControlSet\Enum\{0}\{1}\{2}", id_01, id_02, id_03); RegistryKey regkey = Registry.LocalMachine.OpenSubKey(findme); string deviceDesc = (string)regkey.GetValue("DeviceDesc"); deviceDesc = deviceDesc.Substring(deviceDesc.LastIndexOf(';') + 1); string deviceClass = (string)regkey.GetValue("Class"); if (!String.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("mouse")) { OpenTK.Input.MouseDevice mouse = new OpenTK.Input.MouseDevice(); mouse.Description = deviceDesc; // Register the keyboard: RawInputDeviceInfo info = new RawInputDeviceInfo(); int devInfoSize = API.RawInputDeviceInfoSize; Functions.GetRawInputDeviceInfo(ridl[i].Device, RawInputDeviceInfoEnum.DEVICEINFO, info, ref devInfoSize); mouse.NumberOfButtons = info.Device.Mouse.NumberOfButtons; mouse.NumberOfWheels = info.Device.Mouse.HasHorizontalWheel ? 1 : 0; mouse.DeviceID = ridl[i].Device;//(IntPtr)info.Device.Mouse.Id; this.RegisterRawDevice(mouse); mice.Add(mouse); } } } return count; }
internal static int GetRawInputDeviceInfo(IntPtr Device, [MarshalAs(UnmanagedType.U4)] RawInputDeviceInfoEnum Command, [In, Out] RawInputDeviceInfo Data, [In, Out] ref int Size);
public void RefreshDevices() { lock (UpdateLock) { for (var i = 0; i < keyboards.Count; i++) { var state = keyboards[i]; state.IsConnected = false; keyboards[i] = state; } uint count = WinRawInput.DeviceCount; var ridl = new RawInputDeviceList[count]; for (var i = 0; i < count; i++) { ridl[i] = new RawInputDeviceList(); } User32.RawInput.GetRawInputDeviceList(ridl, ref count, RawInputDeviceList.SizeInBytes); // Discover keyboard devices: foreach (var dev in ridl) { var id = new ContextHandle(dev.Device); if (rawids.ContainsKey(id)) { // Device already registered, mark as connected var state = keyboards[rawids[id]]; state.IsConnected = true; keyboards[rawids[id]] = state; continue; } var name = GetDeviceName(dev); if (name.ToLower().Contains("root")) { // This is a terminal services device, skip it. } 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. var 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 OpenTK issue 3198 // making a guess at backwards compatability. Not sure what older windows returns in these cases... if (deviceClass == null || deviceClass.Equals(string.Empty)) { var 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; } deviceDesc = deviceDesc.Substring(deviceDesc.LastIndexOf(';') + 1); if (!string.IsNullOrEmpty(deviceClass) && deviceClass.ToLower().Equals("keyboard")) { // Register the keyboard: var info = new RawInputDeviceInfo(); var devInfoSize = RawInputDeviceInfo.SizeInBytes; User32.RawInput.GetRawInputDeviceInfo( dev.Device, GetRawInputDeviceInfoEnum.DeviceInfo, ref info, ref devInfoSize ); RegisterKeyboardDevice(window, deviceDesc); var state = new KeyboardState { IsConnected = true }; keyboards.Add(state); names.Add(deviceDesc); rawids.Add(new ContextHandle(dev.Device), keyboards.Count - 1); } } } } }
// Get a DirectInput-compatible Guid // (equivalent to DIDEVICEINSTANCE guidProduct field) 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'); }