internal static void ThrowIfError(IOReturn result) { if (result != IOReturn.Success) { throw result.ToNSErrorException(); } }
void InputReportReceivedCallback(IntPtr context, IOReturn result, IntPtr senderRef, IOHIDReportType type, uint id, byte[] data, CFIndex length) { var device = GetCFObject <IOHIDDevice> (senderRef); device.OnInputReportReceived(result, type, (int)id, data); }
void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val) { if (disposed) { Debug.Print("DeviceValueReceived({0}, {1}, {2}, {3}) called on disposed {4}", context, res, sender, val, GetType()); return; } MouseState mouse; KeyboardState keyboard; if (MouseDevices.TryGetValue(context, out mouse)) { MouseDevices[context] = UpdateMouse(mouse, val); } else if (KeyboardDevices.TryGetValue(context, out keyboard)) { KeyboardDevices[context] = UpdateKeyboard(keyboard, val); } else { //Debug.Print ("Device {0:x} not found in list of keyboards or mice", sender); } }
void OnInputReportReceived(IOReturn result, IOHIDReportType type, int id, byte[] data) { if (InputReportReceived != null) { InputReportReceived(this, new IOHIDReportEventArgs(result, type, id, data)); } }
void OnRemoved(IOReturn result) { if (Removed != null) { Removed(this, new IOReturnEventArgs(result)); } }
static void InputValueReceivedCallback(IntPtr context, IOReturn result, IntPtr senderRef, IntPtr valueRef) { var device = GetCFObject <IOHIDDevice> (senderRef); var value = GetCFObject <IOHIDValue> (valueRef); device.OnValueReceived(result, value); }
void OnMatchingDeviceFound(IOReturn result, IOHIDDevice device) { if (MatchingDeviceFound != null) { MatchingDeviceFound(this, new IOHIDDeviceEventArgs(result, device)); } }
// Registers callbacks for device addition and removal. These callbacks // are called when we run the loop in CheckDevicesMode void RegisterHIDCallbacks(IOHIDManagerRef hidmanager) { try{ UnityEngine.Debug.Log("OSXHIDInterface> RegisterHIDCallbacks"); Native.IOHIDManagerRegisterDeviceMatchingCallback( hidmanager, HandleHIDDeviceAdded, IntPtr.Zero); Native.IOHIDManagerRegisterDeviceRemovalCallback( hidmanager, HandleHIDDeviceRemoved, IntPtr.Zero); Native.IOHIDManagerScheduleWithRunLoop(hidmanager, RunLoop, InputLoopMode); //Native.IOHIDManagerSetDeviceMatching(hidmanager, DeviceTypes.Ref); Native.IOHIDManagerSetDeviceMatchingMultiple(hidmanager, DeviceTypes.typeRef); Native.CFRelease(DeviceTypes.typeRef); IOReturn result = Native.IOHIDManagerOpen(hidmanager, (int)Native.IOHIDOptionsType.kIOHIDOptionsTypeNone); if (result == IOReturn.kIOReturnSuccess) { Native.CFRunLoopRunInMode(InputLoopMode, 0.0, true); hidCallbacksRegistered = true; } else { UnityEngine.Debug.LogError("OSXHIDInterface can't open hidmanager! Error:" + result); } }catch (Exception ex) { UnityEngine.Debug.LogException(ex); } }
void OnDeviceRemoved(IOReturn result, IOHIDDevice device) { if (DeviceRemoved != null) { DeviceRemoved(this, new IOHIDDeviceEventArgs(result, device)); } }
static void DeviceRemovedCallback(IntPtr context, IOReturn result, IntPtr senderRef, IntPtr deviceRef) { var manager = GetCFObject <IOHIDManager> (senderRef); var device = GetCFObject <IOHIDDevice> (deviceRef); manager.OnDeviceRemoved(result, device); }
void OnInputValueReceived(IOReturn result, IOHIDValue value) { if (InputValueReceived != null) { InputValueReceived(this, new IOHIDValueEventArgs(result, value)); } }
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); }
static void InputValueReceivedCallback(IntPtr context, IOReturn result, IntPtr senderRef, IntPtr valueRef) { return; // TODO: sender can be IOHIDQueue object var manager = GetCFObject <IOHIDManager> (senderRef); var value = GetCFObject <IOHIDValue> (valueRef); manager.OnInputValueReceived(result, value); }
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 DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val) { MouseState mouse; KeyboardState keyboard; if (MouseDevices.TryGetValue(sender, out mouse)) { MouseDevices[sender] = UpdateMouse(mouse, val); } else if (KeyboardDevices.TryGetValue(sender, out keyboard)) { KeyboardDevices[sender] = UpdateKeyboard(keyboard, val); } }
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); } }
public override void ConnectionComplete(IOBluetoothDevice device, IOReturn status) { // TODO: log errors here if (device == null || device.connectionTaskCompletionSource == null) { return; } if (status == IOReturn.Success) { device.connectionTaskCompletionSource.TrySetResult(device); } else { device.connectionTaskCompletionSource.TrySetException(status.ToNSErrorException()); } device.connectionTaskCompletionSource = null; }
//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"); } }
/// <summary> /// Handler of callback by OS planned to be used with default OSXDriver /// </summary> /// <param name="context">Context.</param> /// <param name="res">Res.</param> /// <param name="sender">Sender.</param> /// <param name="valRef">Value reference.</param> internal void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef valRef) { IOHIDElementRef element = Native.IOHIDValueGetElement(valRef); uint uid = Native.IOHIDElementGetCookie(element); int value; Native.IOHIDElementType type = Native.IOHIDElementGetType(element); GenericHIDDevice device; value = Native.IOHIDValueGetIntegerValue(valRef); //UnityEngine.Debug.Log ("DeviceValueReceived:"+value); try{ GCHandle gch = GCHandle.FromIntPtr(context); device = (GenericHIDDevice)gch.Target; } catch (Exception e) { UnityEngine.Debug.LogException(e); return; } byte[] typeBuff = BitConverter.GetBytes((uint)type); byte[] uidBuff = BitConverter.GetBytes(uid); byte[] valueBuff = BitConverter.GetBytes(value); byte[] result = new byte[typeBuff.Length + uidBuff.Length + valueBuff.Length]; System.Buffer.BlockCopy(typeBuff, 0, result, 0, typeBuff.Length); System.Buffer.BlockCopy(uidBuff, 0, result, typeBuff.Length, uidBuff.Length); System.Buffer.BlockCopy(valueBuff, 0, result, typeBuff.Length + uidBuff.Length, valueBuff.Length); device.__lastHIDReport.Status = HIDReport.ReadStatus.Success; device.__lastHIDReport.Data = result; }
/// <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); }
public IOReturnEventArgs (IOReturn result) { Result = result; }
public IOHIDReportEventArgs (IOReturn result, IOHIDReportType type, int id, byte[] data) : base (result) { Type = type; ID = id; Data = data; }
void OnRemoved (IOReturn result) { if (Removed != null) Removed (this, new IOReturnEventArgs (result)); }
void InputReportReceivedCallback (IntPtr context, IOReturn result, IntPtr senderRef, IOHIDReportType type, uint id, byte[] data, CFIndex length) { var device = GetCFObject<IOHIDDevice> (senderRef); device.OnInputReportReceived (result, type, (int)id, data); }
void DeviceRemoved(IntPtr inContext, IOReturn result, IntPtr sender) { RemoveDevice(sender); }
void OnInputReportReceived (IOReturn result, IOHIDReportType type, int id, byte[] data) { if (InputReportReceived != null) InputReportReceived (this, new IOHIDReportEventArgs (result, type, id, data)); }
public static int GetSubSystem(this IOReturn value) { return(Error.GetSubSystem((int)value)); }
public static int GetCode(this IOReturn value) { return(Error.GetCode((int)value)); }
void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val) { if (disposed) { Debug.Print("DeviceValueReceived({0}, {1}, {2}, {3}) called on disposed {4}", context, res, sender, val, GetType()); return; } MouseState mouse; KeyboardState keyboard; if (MouseDevices.TryGetValue(context, out mouse)) { MouseDevices[context] = UpdateMouse(mouse, val); } else if (KeyboardDevices.TryGetValue(context, out keyboard)) { KeyboardDevices[context] = UpdateKeyboard(keyboard, val); }else{ //Debug.Print ("Device {0:x} not found in list of keyboards or mice", sender); } }
public IOHIDValueEventArgs (IOReturn result, IOHIDValue value) : base (result) { Value = value; }
void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val) { MouseState mouse; KeyboardState keyboard; if (MouseDevices.TryGetValue(context, out mouse)) { MouseDevices[context] = UpdateMouse(mouse, val); } else if (KeyboardDevices.TryGetValue(context, out keyboard)) { KeyboardDevices[context] = UpdateKeyboard(keyboard, val); }else{ //Debug.Print ("Device {0:x} not found in list of keyboards or mice", sender); } }
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); } }
internal void OpenDevice() { IOReturn result = Native.IOHIDDeviceOpen(__deviceHandle, (int)Native.IOHIDOptionsType.kIOHIDOptionsTypeNone); IsOpen = (result == IOReturn.kIOReturnSuccess); }
public IOHIDDeviceEventArgs (IOReturn result, IOHIDDevice device) : base (result) { Device = device; }
static void InputValueReceivedCallback (IntPtr context, IOReturn result, IntPtr senderRef, IntPtr valueRef) { var device = GetCFObject<IOHIDDevice> (senderRef); var value = GetCFObject<IOHIDValue> (valueRef); device.OnValueReceived (result, value); }
public IOHIDDeviceEventArgs(IOReturn result, IOHIDDevice device) : base(result) { Device = device; }
static void RemovedCallback (IntPtr context, IOReturn result, IntPtr senderRef) { var device = GetCFObject<IOHIDDevice> (senderRef); device.OnRemoved (result); }
/// <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 IOHIDReportEventArgs(IOReturn result, IOHIDReportType type, int id, byte[] data) : base(result) { Type = type; ID = id; Data = data; }
void DeviceRemoved(IntPtr inContext, IOReturn result, IntPtr sender) { RemoveDevice (sender); }
public IOReturnEventArgs(IOReturn result) { Result = result; }
void OnMatchingDeviceFound (IOReturn result, IOHIDDevice device) { if (MatchingDeviceFound != null) MatchingDeviceFound (this, new IOHIDDeviceEventArgs (result, device)); }
static void DeviceRemovedCallback (IntPtr context, IOReturn result, IntPtr senderRef, IntPtr deviceRef) { var manager = GetCFObject<IOHIDManager> (senderRef); var device = GetCFObject<IOHIDDevice> (deviceRef); manager.OnDeviceRemoved (result, device); }
/// <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)); } }
void OnDeviceRemoved (IOReturn result, IOHIDDevice device) { if (DeviceRemoved != null) DeviceRemoved (this, new IOHIDDeviceEventArgs (result, device)); }
/// <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); }
static void InputValueReceivedCallback (IntPtr context, IOReturn result, IntPtr senderRef, IntPtr valueRef) { return; // TODO: sender can be IOHIDQueue object var manager = GetCFObject<IOHIDManager> (senderRef); var value = GetCFObject<IOHIDValue> (valueRef); manager.OnInputValueReceived (result, value); }
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); }
void OnInputValueReceived (IOReturn result, IOHIDValue value) { if (InputValueReceived != null) InputValueReceived (this, new IOHIDValueEventArgs (result, value)); }
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 IOHIDValueEventArgs(IOReturn result, IOHIDValue value) : base(result) { Value = value; }
void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val) { try { if (disposed) { Debug.Print("DeviceValueReceived({0}, {1}, {2}, {3}) called on disposed {4}", context, res, sender, val, GetType()); return; } MouseData mouse; KeyboardData keyboard; JoystickData joystick; if (MouseDevices.TryGetValue(context, out mouse)) { UpdateMouse(mouse, val); } else if (KeyboardDevices.TryGetValue(context, out keyboard)) { UpdateKeyboard(keyboard, val); } else if (JoystickDevices.TryGetValue(context, out joystick)) { UpdateJoystick(joystick, val); } else { //Debug.Print ("Device {0:x} not found in list of keyboards or mice", sender); } } catch (Exception e) { Debug.Print("[Mac] Exception in managed callback: {0}", e); } }
public override void ConnectionComplete (IOBluetoothDevice device, IOReturn status) { // TODO: log errors here if (device == null || device.connectionTaskCompletionSource == null) return; if (status == IOReturn.Success) device.connectionTaskCompletionSource.TrySetResult (device); else device.connectionTaskCompletionSource.TrySetException (status.ToNSErrorException ()); device.connectionTaskCompletionSource = null; }