void DeviceRemoved(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) { if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse) && MouseDevices.ContainsKey(device)) { Debug.Print("Mouse device {0} disconnected", device); // Keep the device in case it comes back later on MouseState state = MouseDevices[device]; state.IsConnected = false; MouseDevices[device] = state; } if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard) && KeyboardDevices.ContainsKey(device)) { Debug.Print("Keyboard device {0} disconnected", device); // Keep the device in case it comes back later on KeyboardState state = KeyboardDevices[device]; state.IsConnected = false; KeyboardDevices[device] = state; } NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, null, IntPtr.Zero); NativeMethods.IOHIDDeviceUnscheduleWithRunLoop(device, RunLoop, InputLoopMode); }
internal static extern IOReturn IOHIDDeviceSetReportWithCallback( IOHIDDeviceRef device, IOHIDReportType reportType, int reportID, IntPtr report, int reportLength, CFTimeInterval timeout, IOHIDReportCallback callback, IntPtr context); //AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
// [DllImport(hid)] // internal static extern long AllocateHIDObjectFromIOHIDDeviceRef(IOHIDDeviceRef inIOHIDDeviceRef); internal static long AllocateHIDObjectFromIOHIDDeviceRef(IOHIDDeviceRef inIOHIDDeviceRef) { long result = 0; if (inIOHIDDeviceRef != IntPtr.Zero) { // Set up the matching criteria for the devices we're interested in. // We are interested in instances of class IOHIDDevice. // matchingDict is consumed below( in IOServiceGetMatchingService ) // so we have no leak here. //CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOHIDDeviceKey); byte[] utf8Bytes = Encoding.UTF8.GetBytes(Native.kIOHIDDeviceKey); //char[] charArr=Native.IOHIDDeviceKey.ToCharArray( IntPtr bufferIntPtr = Marshal.AllocHGlobal(utf8Bytes.Length); Marshal.Copy(utf8Bytes, 0, bufferIntPtr, utf8Bytes.Length); IntPtr matchingDictRef = Native.IOServiceMatching(bufferIntPtr); matchingDictRef = Native.IOServiceMatching(Native.CFSTR(Native.kIOHIDDeviceKey)); Marshal.FreeHGlobal(bufferIntPtr); if (matchingDictRef != IntPtr.Zero) { Native.CFDictionary dict = new Native.CFDictionary(matchingDictRef); // Add a key for locationID to our matching dictionary. This works for matching to // IOHIDDevices, so we will only look for a device attached to that particular port // on the machine. IntPtr tCFTypeRef = Native.IOHIDDeviceGetProperty(inIOHIDDeviceRef, Native.CFSTR(Native.kIOHIDLocationIDKey)); if (tCFTypeRef != IntPtr.Zero) { dict[Native.kIOHIDLocationIDKey] = tCFTypeRef; //CFDictionaryAddValue (matchingDictRef, CFSTR (Native.IOHIDLocationIDKey), tCFTypeRef); // CFRelease( tCFTypeRef ); // don't release objects that we "Get". // IOServiceGetMatchingService assumes that we already know that there is only one device // that matches. This way we don't have to do the whole iteration dance to look at each // device that matches. This is a new API in 10.2 //result = Native.IOServiceGetMatchingService (kIOMasterPortDefault, matchingDictRef); result = Native.IOServiceGetMatchingService(IntPtr.Zero, matchingDictRef); } // Note: We're not leaking the matchingDict. // One reference is consumed by IOServiceGetMatchingServices } } return(result); }
void DeviceAdded(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) { if (NativeMethods.IOHIDDeviceOpen(device, IOOptionBits.Zero) == IOReturn.Zero) { if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse)) { if (!MouseDevices.ContainsKey(device)) { Debug.Print("Mouse device {0:x} discovered, sender is {1:x}", device, sender); MouseState state = new MouseState(); state.IsConnected = true; MouseIndexToDevice.Add(MouseDevices.Count, device); MouseDevices.Add(device, state); } else { Debug.Print("Mouse device {0:x} reconnected, sender is {1:x}", device, sender); MouseState state = MouseDevices[device]; state.IsConnected = true; MouseDevices[device] = state; } } if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard)) { if (!KeyboardDevices.ContainsKey(device)) { Debug.Print("Keyboard device {0:x} discovered, sender is {1:x}", device, sender); KeyboardState state = new KeyboardState(); state.IsConnected = true; KeyboardIndexToDevice.Add(KeyboardDevices.Count, device); KeyboardDevices.Add(device, state); } else { Debug.Print("Keyboard device {0:x} reconnected, sender is {1:x}", device, sender); KeyboardState state = KeyboardDevices[device]; state.IsConnected = true; KeyboardDevices[device] = state; } } // The device is not normally available in the InputValueCallback (HandleDeviceValueReceived), so we include // the device identifier as the context variable, so we can identify it and figure out the device later. // Thanks to Jase: http://www.opentk.com/node/2800 NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, HandleDeviceValueReceived, device); NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, RunLoop, InputLoopMode); } }
void DeviceAdded(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) { if (NativeMethods.IOHIDDeviceOpen(device, IOOptionBits.Zero) == IOReturn.Zero) { if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse)) { if (!MouseDevices.ContainsKey(device)) { Debug.Print("Mouse device {0} discovered", device); MouseState state = new MouseState(); state.IsConnected = true; MouseIndexToDevice.Add(MouseDevices.Count, device); MouseDevices.Add(device, state); } else { Debug.Print("Mouse device {0} reconnected", device); MouseState state = MouseDevices[device]; state.IsConnected = true; MouseDevices[device] = state; } } if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard)) { if (!KeyboardDevices.ContainsKey(device)) { Debug.Print("Keyboard device {0} discovered", device); KeyboardState state = new KeyboardState(); state.IsConnected = true; KeyboardIndexToDevice.Add(KeyboardDevices.Count, device); KeyboardDevices.Add(device, state); } else { Debug.Print("Keyboard device {0} reconnected", device); KeyboardState state = KeyboardDevices[device]; state.IsConnected = true; KeyboardDevices[device] = state; } } NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, HandleDeviceValueReceived, IntPtr.Zero); NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, RunLoop, InputLoopMode); } }
void RemoveDevice(IOHIDDeviceRef deviceRef) { if (deviceRef == IntPtr.Zero) { Debug.LogWarning("IOHIDeviceRef equal to IntPtr.Zero"); return; } int product_id = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDProductIDKey)))).ToInteger(); int vendor_id = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDVendorIDKey)))).ToInteger(); int location = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDLocationIDKey)))).ToInteger(); string transport = (new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDTransportKey)))).ToString(); string path = String.Format("{0:s}_{1,4:X}_{2,4:X}_{3:X}", transport, vendor_id, product_id, location); //"%s_%04hx_%04hx_%x" lock (syncRoot) { if (!__Generics.ContainsKey(path)) { return; } HIDDevice hidDevice = __Generics [path]; Generics.Remove(path); Debug.Log("Device " + hidDevice.index + " PID:" + hidDevice.PID + " VID:" + hidDevice.VID + " Disconnected"); } Debug.Log("Try to unshedule,unregister and close device"); Native.IOHIDDeviceUnscheduleFromRunLoop(deviceRef, RunLoop, InputLoopMode); Native.IOHIDDeviceRegisterInputValueCallback(deviceRef, IntPtr.Zero, IntPtr.Zero); Native.IOHIDDeviceRegisterRemovalCallback(deviceRef, null, IntPtr.Zero); Native.IOHIDDeviceClose(deviceRef, (int)Native.IOHIDOptionsType.kIOHIDOptionsTypeNone); this.DeviceDisconnectEvent(this, new DeviceEventArgs <string>(path)); }
//static void input_callback(void *context, IOReturn ret, void *sender, //IOHIDReportType type, uint32_t id, uint8_t *data, CFIndex len) internal void InputReportCallback( IntPtr inContext, // context from IOHIDDeviceRegisterInputReportCallback IOReturn inResult, // completion result for the input report operation IOHIDDeviceRef inSender, // IOHIDDeviceRef of the device this report is from Native.IOHIDReportType inType, // the report type uint inReportID, // the report ID IntPtr inReport, // pointer to the report data int inReportLength) { GenericHIDDevice hidDevice; if (inContext != IntPtr.Zero) { hidDevice = (GenericHIDDevice)GCHandle.FromIntPtr(inContext).Target; if (hidDevice.__deviceHandle != inSender) { return; } byte[] buffer = new byte[hidDevice.InputReportByteLength]; Marshal.Copy(inReport, buffer, 0, hidDevice.InputReportByteLength); hidDevice.__lastHIDReport.Data = buffer; hidDevice.__lastHIDReport.Status = HIDReport.ReadStatus.Success; //UnityEngine.Debug.Log (BitConverter.ToString (buffer)); hidDevice.IsReadInProgress = false; //Marshal.FreeHGlobal (inReport); //Native.IOHIDDeviceRegisterInputReportCallback (hidDevice.__deviceHandle, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero); hidDevice.InputAutoResetEvent.Set(); } else { UnityEngine.Debug.LogError("InputReportCallback inContext ZeroPointer"); } }
internal void OutputReportCallback( IntPtr inContext, // context from IOHIDDeviceRegisterInputReportCallback IOReturn inResult, // completion result for the input report operation IOHIDDeviceRef inSender, // IOHIDDeviceRef of the device this report is from Native.IOHIDReportType inType, // the report type uint inReportID, // the report ID IntPtr inReport, // pointer to the report data int inReportLength) { GenericHIDDevice hidDevice; if (inContext != IntPtr.Zero) { hidDevice = (GenericHIDDevice)GCHandle.FromIntPtr(inContext).Target; hidDevice.OutputAutoResetEvent.Set(); } else { UnityEngine.Debug.LogError("OutputReportCallback inContext ZeroPointer"); } }
void DeviceAdded(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) { try { bool recognized = false; if (NativeMethods.IOHIDDeviceOpen(device, IOOptionBits.Zero) == IOReturn.Zero) { if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse)) { AddMouse(sender, device); recognized = true; } if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard)) { AddKeyboard(sender, device); recognized = true; } bool is_joystick = false; is_joystick |= NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick); is_joystick |= NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.GamePad); is_joystick |= NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.MultiAxisController); is_joystick |= NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Wheel); // Todo: any other interesting devices under HIDPage.Simulation + HIDUsageSim? if (is_joystick) { AddJoystick(sender, device); recognized = true; } if (recognized) { // The device is not normally available in the InputValueCallback (HandleDeviceValueReceived), so we include // the device identifier as the context variable, so we can identify it and figure out the device later. // Thanks to Jase: http://www.opentk.com/node/2800 NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, HandleDeviceValueReceived, device); NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, RunLoop, InputLoopMode); } } } catch (Exception e) { Debug.Print("[Mac] Exception in managed callback: {0}", e); } }
public static extern void IOHIDDeviceScheduleWithRunLoop( IOHIDDeviceRef device, CFRunLoop inCFRunLoop, CFString inCFRunLoopMode);
public static extern CFArrayRef IOHIDDeviceCopyMatchingElements( IOHIDDeviceRef inIOHIDDeviceRef, // IOHIDDeviceRef for the HID device CFDictionaryRef inMatchingCFDictRef, // the matching dictionary IOOptionBits inOptions); // Option bits
internal static extern IOReturn IOHIDDeviceSetReport( IOHIDDeviceRef device, IOHIDReportType reportType, int reportID, IntPtr report, CFIndex reportLength); // AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
public static extern IOReturn IOHIDDeviceClose( IOHIDDeviceRef IOHIDDeviceRef, // IOHIDDeviceRef for the HID device IOOptionBits inOptions ); // Option bits to be sent down to the HID device
public static extern void IOHIDDeviceRegisterRemovalCallback( IOHIDDeviceRef device, IOHIDCallback callback, IntPtr context);
public static extern CFArrayRef IOHIDDeviceCopyMatchingElements( IOHIDDeviceRef device, CFDictionaryRef matching, IOOptionBits options);
/// <summary> /// Devices the removed. /// </summary> /// <param name="context">Context.</param> /// <param name="res">Res.</param> /// <param name="sender">Sender.</param> /// <param name="device">Device.</param> void HidDeviceRemoved(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef deviceRef) { RemoveDevice(deviceRef); }
/// <summary> /// Devices the removed. /// </summary> /// <param name="context">Context.</param> /// <param name="res">Res.</param> /// <param name="sender">Sender.</param> /// <param name="device">Device.</param> void HidDeviceRemoved(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef deviceRef) { RemoveDevice (deviceRef); }
void RemoveDevice(IOHIDDeviceRef deviceRef){ if (deviceRef == IntPtr.Zero) { Debug.LogWarning("IOHIDeviceRef equal to IntPtr.Zero"); return; } int product_id = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDProductIDKey)))).ToInteger(); int vendor_id =(int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDVendorIDKey)))).ToInteger(); int location=(int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDLocationIDKey)))).ToInteger(); string transport=(new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDTransportKey)))).ToString(); string path=String.Format("{0:s}_{1,4:X}_{2,4:X}_{3:X}", transport, vendor_id, product_id, location);//"%s_%04hx_%04hx_%x" lock (syncRoot) { if (!__Generics.ContainsKey (path)) return; HIDDevice hidDevice = __Generics [path]; Generics.Remove (path); Debug.Log ("Device "+hidDevice.index+" PID:" + hidDevice.PID + " VID:" + hidDevice.VID + " Disconnected"); } Debug.Log ("Try to unshedule,unregister and close device"); Native.IOHIDDeviceUnscheduleFromRunLoop(deviceRef, RunLoop, InputLoopMode); Native.IOHIDDeviceRegisterInputValueCallback(deviceRef,IntPtr.Zero,IntPtr.Zero); Native.IOHIDDeviceRegisterRemovalCallback (deviceRef, null, IntPtr.Zero); Native.IOHIDDeviceClose (deviceRef,(int)Native.IOHIDOptionsType.kIOHIDOptionsTypeNone); this.DeviceDisconnectEvent(this,new DeviceEventArgs<string>(path)); }
/// <summary> /// Devices the added. /// </summary> /// <param name="context">Context.</param> /// <param name="res">Res.</param> /// <param name="sender">Sender.</param> /// <param name="device">Device.</param> void HidDeviceAdded(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef deviceRef) { //IOReturn success = Native.IOHIDDeviceOpen (device, (int)Native.IOHIDOptionsType.kIOHIDOptionsTypeNone); if (deviceRef == IntPtr.Zero) { Debug.LogWarning("IOHIDeviceRef of Added Device equal to IntPtr.Zero"); return; } int product_id = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDProductIDKey)))).ToInteger(); int vendor_id =(int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDVendorIDKey)))).ToInteger(); string manufacturer=(new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDManufacturerKey)))).ToString(); string description =manufacturer+" "+(new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDProductKey)))).ToString(); int location=(int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDLocationIDKey)))).ToInteger(); string transport=(new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDTransportKey)))).ToString(); string path=String.Format("{0:s}_{1,4:X}_{2,4:X}_{3:X}", transport, vendor_id, product_id, location);//"%s_%04hx_%04hx_%x" //string serial=(new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDSerialNumberKey)))).ToString(); if(Generics.ContainsKey(path)) return; GenericHIDDevice hidDevice; IDevice joyDevice = null; // IDevice<IAxisDetails, IButtonDetails, IDeviceExtension> joyDevice = null; ///loop thru specific drivers and attach the driver to device if compatible if (__drivers != null) foreach (var driver in __drivers) { hidDevice=new GenericHIDDevice(GetIndexForDeviceWithID(path),vendor_id, product_id, path,deviceRef, this,path,description); if ((joyDevice = driver.ResolveDevice(hidDevice)) != null) { lock(syncRoot){ __Generics[path] = hidDevice; } //if (context != IntPtr.Zero) { Native.IOHIDDeviceRegisterRemovalCallback(deviceRef,HandleDeviceRemoved,context); //}else{ // Debug.LogWarning("IOHIDDeviceRegisterRemovalCallback not registerd cos of Context IntPtr.Zero"); //} Debug.Log("Device PID:" + joyDevice.PID + " VID:" + joyDevice.VID + "["+joyDevice.Name+"] attached to " + driver.GetType().ToString()); break; } } if (joyDevice == null) {//set default driver as resolver if no custom driver match device hidDevice=new GenericHIDDevice(GetIndexForDeviceWithID(path),vendor_id, product_id,path, deviceRef, this,path,description); if ((joyDevice = defaultDriver.ResolveDevice(hidDevice)) != null) { lock(syncRoot){ __Generics[path] = hidDevice; } //if (context != IntPtr.Zero) { Native.IOHIDDeviceRegisterRemovalCallback(deviceRef,HandleDeviceRemoved,context); //}else{ // Debug.LogWarning("IOHIDDeviceRegisterRemovalCallback not registerd cos of Context IntPtr.Zero"); //} Debug.Log("Device PID:" + joyDevice.PID + " VID:" + joyDevice.VID + "["+joyDevice.Name+"] attached to " + defaultDriver.GetType().ToString()); } else { Debug.LogWarning("Device PID:" + product_id.ToString() + " VID:" + vendor_id.ToString() + " not found compatible driver on the system.Removed!"); } } if(joyDevice!=null) this.DeviceConnectEvent(this,new DeviceEventArgs<IDevice>(joyDevice)); }
public static extern void IOHIDDeviceRegisterInputReportCallback( IOHIDDeviceRef device, // IOHIDDeviceRef for the HID device IntPtr report, // pointer to the report data ( uint8_t's ) int reportLength, // number of bytes in the report ( CFIndex ) IOHIDReportCallback callback, // the callback routine IntPtr context); //AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
internal static extern IOReturn IOHIDDeviceGetReport( IOHIDDeviceRef device, IOHIDReportType reportType, CFIndex reportID, IntPtr report, IntPtr pReportLength);
public static extern void IOHIDDeviceRegisterRemovalCallback( IOHIDDeviceRef device, IOHIDCallback callback, IntPtr context);
void DeviceAdded(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) { if (NativeMethods.IOHIDDeviceOpen(device, IOOptionBits.Zero) == IOReturn.Zero) { if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse)) { if (!MouseDevices.ContainsKey(device)) { Debug.Print("Mouse device {0:x} discovered, sender is {1:x}", device, sender); MouseState state = new MouseState(); state.IsConnected = true; MouseIndexToDevice.Add(MouseDevices.Count, device); MouseDevices.Add(device, state); } else { Debug.Print("Mouse device {0:x} reconnected, sender is {1:x}", device, sender); MouseState state = MouseDevices[device]; state.IsConnected = true; MouseDevices[device] = state; } } if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard)) { if (!KeyboardDevices.ContainsKey(device)) { Debug.Print("Keyboard device {0:x} discovered, sender is {1:x}", device, sender); KeyboardState state = new KeyboardState(); state.IsConnected = true; KeyboardIndexToDevice.Add(KeyboardDevices.Count, device); KeyboardDevices.Add(device, state); } else { Debug.Print("Keyboard device {0:x} reconnected, sender is {1:x}", device, sender); KeyboardState state = KeyboardDevices[device]; state.IsConnected = true; KeyboardDevices[device] = state; } } // The device is not normally available in the InputValueCallback (HandleDeviceValueReceived), so we include // the device identifier as the context variable, so we can identify it and figure out the device later. // Thanks to Jase: http://www.opentk.com/node/2800 NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, HandleDeviceValueReceived, device); NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, RunLoop, InputLoopMode); } }
/// <summary> /// Devices the added. /// </summary> /// <param name="context">Context.</param> /// <param name="res">Res.</param> /// <param name="sender">Sender.</param> /// <param name="device">Device.</param> void HidDeviceAdded(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef deviceRef) { //IOReturn success = Native.IOHIDDeviceOpen (device, (int)Native.IOHIDOptionsType.kIOHIDOptionsTypeNone); if (deviceRef == IntPtr.Zero) { Debug.LogWarning("IOHIDeviceRef of Added Device equal to IntPtr.Zero"); return; } int product_id = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDProductIDKey)))).ToInteger(); int vendor_id = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDVendorIDKey)))).ToInteger(); string manufacturer = (new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDManufacturerKey)))).ToString(); string description = manufacturer + " " + (new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDProductKey)))).ToString(); int location = (int)(new Native.CFNumber(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDLocationIDKey)))).ToInteger(); string transport = (new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDTransportKey)))).ToString(); string path = String.Format("{0:s}_{1,4:X}_{2,4:X}_{3:X}", transport, vendor_id, product_id, location); //"%s_%04hx_%04hx_%x" //string serial=(new Native.CFString(Native.IOHIDDeviceGetProperty(deviceRef, Native.CFSTR(Native.kIOHIDSerialNumberKey)))).ToString(); if (Generics.ContainsKey(path)) { return; } GenericHIDDevice hidDevice; IDevice joyDevice = null; // IDevice<IAxisDetails, IButtonDetails, IDeviceExtension> joyDevice = null; ///loop thru specific drivers and attach the driver to device if compatible if (__drivers != null) { foreach (var driver in __drivers) { hidDevice = new GenericHIDDevice(GetIndexForDeviceWithID(path), vendor_id, product_id, path, deviceRef, this, path, description); if ((joyDevice = driver.ResolveDevice(hidDevice)) != null) { lock (syncRoot){ __Generics[path] = hidDevice; } //if (context != IntPtr.Zero) { Native.IOHIDDeviceRegisterRemovalCallback(deviceRef, HandleDeviceRemoved, context); //}else{ // Debug.LogWarning("IOHIDDeviceRegisterRemovalCallback not registerd cos of Context IntPtr.Zero"); //} Debug.Log("Device PID:" + joyDevice.PID + " VID:" + joyDevice.VID + "[" + joyDevice.Name + "] attached to " + driver.GetType().ToString()); break; } } } if (joyDevice == null) { //set default driver as resolver if no custom driver match device hidDevice = new GenericHIDDevice(GetIndexForDeviceWithID(path), vendor_id, product_id, path, deviceRef, this, path, description); if ((joyDevice = defaultDriver.ResolveDevice(hidDevice)) != null) { lock (syncRoot){ __Generics[path] = hidDevice; } //if (context != IntPtr.Zero) { Native.IOHIDDeviceRegisterRemovalCallback(deviceRef, HandleDeviceRemoved, context); //}else{ // Debug.LogWarning("IOHIDDeviceRegisterRemovalCallback not registerd cos of Context IntPtr.Zero"); //} Debug.Log("Device PID:" + joyDevice.PID + " VID:" + joyDevice.VID + "[" + joyDevice.Name + "] attached to " + defaultDriver.GetType().ToString()); } else { Debug.LogWarning("Device PID:" + product_id.ToString() + " VID:" + vendor_id.ToString() + " not found compatible driver on the system.Removed!"); } } if (joyDevice != null) { this.DeviceConnectEvent(this, new DeviceEventArgs <IDevice>(joyDevice)); } }
public static extern IOReturn IOHIDDeviceClose(IOHIDDeviceRef IOHIDDeviceRef, // IOHIDDeviceRef for the HID device IOOptionBits inOptions); // Option bits to be sent down to the HID device
internal static extern IOReturn IOHIDDeviceSetReport( IOHIDDeviceRef device, IOHIDReportType reportType, int reportID, IntPtr report, CFIndex reportLength);// AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
// [DllImport(hid)] // internal static extern long AllocateHIDObjectFromIOHIDDeviceRef(IOHIDDeviceRef inIOHIDDeviceRef); internal static long AllocateHIDObjectFromIOHIDDeviceRef(IOHIDDeviceRef inIOHIDDeviceRef) { long result =0 ; if (inIOHIDDeviceRef!=IntPtr.Zero) { // Set up the matching criteria for the devices we're interested in. // We are interested in instances of class IOHIDDevice. // matchingDict is consumed below( in IOServiceGetMatchingService ) // so we have no leak here. //CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOHIDDeviceKey); byte[] utf8Bytes = Encoding.UTF8.GetBytes(Native.kIOHIDDeviceKey); //char[] charArr=Native.IOHIDDeviceKey.ToCharArray( IntPtr bufferIntPtr = Marshal.AllocHGlobal(utf8Bytes.Length); Marshal.Copy(utf8Bytes, 0, bufferIntPtr, utf8Bytes.Length); IntPtr matchingDictRef = Native.IOServiceMatching (bufferIntPtr); matchingDictRef = Native.IOServiceMatching (Native.CFSTR(Native.kIOHIDDeviceKey)); Marshal.FreeHGlobal(bufferIntPtr); if (matchingDictRef != IntPtr.Zero) { Native.CFDictionary dict=new Native.CFDictionary(matchingDictRef); // Add a key for locationID to our matching dictionary. This works for matching to // IOHIDDevices, so we will only look for a device attached to that particular port // on the machine. IntPtr tCFTypeRef = Native.IOHIDDeviceGetProperty (inIOHIDDeviceRef, Native.CFSTR (Native.kIOHIDLocationIDKey)); if (tCFTypeRef != IntPtr.Zero) { dict[Native.kIOHIDLocationIDKey]=tCFTypeRef; //CFDictionaryAddValue (matchingDictRef, CFSTR (Native.IOHIDLocationIDKey), tCFTypeRef); // CFRelease( tCFTypeRef ); // don't release objects that we "Get". // IOServiceGetMatchingService assumes that we already know that there is only one device // that matches. This way we don't have to do the whole iteration dance to look at each // device that matches. This is a new API in 10.2 //result = Native.IOServiceGetMatchingService (kIOMasterPortDefault, matchingDictRef); result = Native.IOServiceGetMatchingService (IntPtr.Zero, matchingDictRef); } // Note: We're not leaking the matchingDict. // One reference is consumed by IOServiceGetMatchingServices } } return result; }
void DeviceRemoved(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) { if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse) && MouseDevices.ContainsKey(device)) { Debug.Print("Mouse device {0:x} disconnected, sender is {1:x}", device, sender); // Keep the device in case it comes back later on MouseState state = MouseDevices[device]; state.IsConnected = false; MouseDevices[device] = state; } if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard) && KeyboardDevices.ContainsKey(device)) { Debug.Print("Keyboard device {0:x} disconnected, sender is {1:x}", device, sender); // Keep the device in case it comes back later on KeyboardState state = KeyboardDevices[device]; state.IsConnected = false; KeyboardDevices[device] = state; } NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, null, IntPtr.Zero); NativeMethods.IOHIDDeviceUnscheduleWithRunLoop(device, RunLoop, InputLoopMode); }
public static extern void IOHIDDeviceRegisterInputReportCallback( IOHIDDeviceRef device, // IOHIDDeviceRef for the HID device IntPtr report, // pointer to the report data ( uint8_t's ) int reportLength,// number of bytes in the report ( CFIndex ) IOHIDReportCallback callback, // the callback routine IntPtr context); //AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
public static extern CFArrayRef IOHIDDeviceCopyMatchingElements( IOHIDDeviceRef device, CFDictionaryRef matching, IOOptionBits options);
public static extern CFTypeRef IOHIDDeviceGetProperty( IOHIDDeviceRef device, CFStringRef key);
internal static extern void IOHIDDeviceUnscheduleFromRunLoop( IOHIDDeviceRef device, CFRunLoopRef runLoop, CFStringRef runLoopMode);// AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
internal static extern void IOHIDDeviceUnscheduleFromRunLoop( IOHIDDeviceRef device, CFRunLoopRef runLoop, CFStringRef runLoopMode); // AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
internal static extern IOReturn IOHIDDeviceSetReportWithCallback( IOHIDDeviceRef device, IOHIDReportType reportType, int reportID, IntPtr report, int reportLength, CFTimeInterval timeout, IOHIDReportCallback callback, IntPtr context) ;//AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
public static extern IOReturn IOHIDDeviceOpen( IOHIDDeviceRef manager, IOOptionBits opts);
internal static extern IOReturn IOHIDDeviceGetReport( IOHIDDeviceRef device, IOHIDReportType reportType, CFIndex reportID, IntPtr report, IntPtr pReportLength);
public static extern CFTypeRef IOHIDDeviceGetProperty( IOHIDDeviceRef device, CFStringRef key);
public static extern IOReturn IOHIDDeviceOpen( IOHIDDeviceRef manager, IOOptionBits opts);
public static extern bool IOHIDDeviceConformsTo( IOHIDDeviceRef inIOHIDDeviceRef, // IOHIDDeviceRef for the HID device HIDPage inUsagePage, // the usage page to test conformance with int inUsage); // the usage to test conformance with
public static extern bool IOHIDDeviceConformsTo( IOHIDDeviceRef inIOHIDDeviceRef, // IOHIDDeviceRef for the HID device HIDPage inUsagePage, // the usage page to test conformance with int inUsage); // the usage to test conformance with
public static extern void IOHIDDeviceRegisterInputValueCallback( IOHIDDeviceRef device, IOHIDValueCallback callback, IntPtr context);
public static extern void IOHIDDeviceRegisterInputValueCallback( IOHIDDeviceRef device, IntPtr callback, IntPtr context);
public static extern void IOHIDDeviceUnscheduleWithRunLoop( IOHIDDeviceRef device, CFRunLoop inCFRunLoop, CFString inCFRunLoopMode);
public static extern void IOHIDDeviceUnscheduleFromRunLoop( IOHIDDeviceRef device, CFRunLoop inCFRunLoop, CFString inCFRunLoopMode);
public static extern IOReturn IOHIDDeviceClose( IOHIDDeviceRef device, IOOptionBits options);
void DeviceRemoved(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) { try { bool recognized = false; if (MouseDevices.ContainsKey(device)) { RemoveMouse(sender, device); recognized = true; } if (KeyboardDevices.ContainsKey(device)) { RemoveKeyboard(sender, device); recognized = true; } if (JoystickDevices.ContainsKey(device)) { RemoveJoystick(sender, device); recognized = true; } if (recognized) { NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero); NativeMethods.IOHIDDeviceUnscheduleFromRunLoop(device, RunLoop, InputLoopMode); } } catch (Exception e) { Debug.Print("[Mac] Exception in managed callback: {0}", e); } }
void DeviceAdded(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) { if (NativeMethods.IOHIDDeviceOpen(device, IOOptionBits.Zero) == IOReturn.Zero) { if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse)) { if (!MouseDevices.ContainsKey(device)) { Debug.Print("Mouse device {0} discovered", device); MouseState state = new MouseState(); state.IsConnected = true; MouseIndexToDevice.Add(MouseDevices.Count, device); MouseDevices.Add(device, state); } else { Debug.Print("Mouse device {0} reconnected", device); MouseState state = MouseDevices[device]; state.IsConnected = true; MouseDevices[device] = state; } } if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard)) { if (!KeyboardDevices.ContainsKey(device)) { Debug.Print("Keyboard device {0} discovered", device); KeyboardState state = new KeyboardState(); state.IsConnected = true; KeyboardIndexToDevice.Add(KeyboardDevices.Count, device); KeyboardDevices.Add(device, state); } else { Debug.Print("Keyboard device {0} reconnected", device); KeyboardState state = KeyboardDevices[device]; state.IsConnected = true; KeyboardDevices[device] = state; } } NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, HandleDeviceValueReceived, IntPtr.Zero); NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, RunLoop, InputLoopMode); } }