static KeyboardState UpdateKeyboard(KeyboardState state, IOHIDValueRef val) { IOHIDElementRef elem = NativeMethods.IOHIDValueGetElement(val); int v_int = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32(); HIDPage page = NativeMethods.IOHIDElementGetUsagePage(elem); int usage = NativeMethods.IOHIDElementGetUsage(elem); // This will supress the debug printing below. Seems like it generates a lot of -1s. // Couldn't find any details in USB spec or Apple docs for this behavior. if (usage < 0) { return(state); } switch (page) { case HIDPage.GenericDesktop: case HIDPage.KeyboardOrKeypad: if (usage >= RawKeyMap.Length) { Debug.Print("[Warning] Key {0} not mapped.", usage); return(state); } state.SetKeyState(RawKeyMap[usage], (byte)usage, v_int != 0); break; } return(state); }
static MouseState UpdateMouse(MouseState state, IOHIDValueRef val) { IOHIDElementRef elem = NativeMethods.IOHIDValueGetElement(val); int v_int = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32(); //double v_physical = NativeMethods.IOHIDValueGetScaledValue(val, IOHIDValueScaleType.Physical); //double v_calbrated = NativeMethods.IOHIDValueGetScaledValue(val, IOHIDValueScaleType.Calibrated); HIDPage page = NativeMethods.IOHIDElementGetUsagePage(elem); int usage = NativeMethods.IOHIDElementGetUsage(elem); switch (page) { case HIDPage.GenericDesktop: switch ((HIDUsageGD)usage) { case HIDUsageGD.X: state.X += v_int; break; case HIDUsageGD.Y: state.Y += v_int; break; case HIDUsageGD.Wheel: state.WheelPrecise += v_int; break; } break; case HIDPage.Button: state[OpenTK.Input.MouseButton.Left + usage - 1] = v_int == 1; break; } return(state); }
/// <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; }
static KeyboardState UpdateKeyboard(KeyboardState state, IOHIDValueRef val) { IOHIDElementRef elem = NativeMethods.IOHIDValueGetElement(val); int v_int = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32(); HIDPage page = NativeMethods.IOHIDElementGetUsagePage(elem); int usage = NativeMethods.IOHIDElementGetUsage(elem); switch (page) { case HIDPage.GenericDesktop: case HIDPage.KeyboardOrKeypad: int raw = (int)usage; if (raw >= RawKeyMap.Length || raw < 0) { Debug.Print("[Warning] Key {0} not mapped.", raw); return(state); } Key key = RawKeyMap[raw]; state[key] = v_int != 0; break; } return(state); }
public static extern int IOHIDElementGetUsage(IOHIDElementRef elem);
public static extern IOHIDElementType IOHIDElementGetType( IOHIDElementRef element);
public static extern CFIndex IOHIDElementGetPhysicalMax(IOHIDElementRef element);
public static extern IOHIDElementCookie IOHIDElementGetCookie( IOHIDElementRef element);
static HatPosition GetJoystickHat(IOHIDValueRef val, IOHIDElementRef element) { HatPosition position = HatPosition.Centered; int max = NativeMethods.IOHIDElementGetLogicalMax(element).ToInt32(); int min = NativeMethods.IOHIDElementGetLogicalMin(element).ToInt32(); int value = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32() - min; int range = Math.Abs(max - min + 1); if (value >= 0) { if (range == 4) { // 4-position hat (no diagonals) // 0 = up; 1 = right; 2 = down; 3 = left // map to a 8-position hat (processed below) value *= 2; } if (range == 8) { // 0 = up; 1 = up-right; 2 = right; 3 = right-down; // 4 = down; 5 = down-left; 6 = left; 7 = up-left // Our HatPosition enum position = (HatPosition)value; } else { // Todo: implement support for continuous hats } } return position; }
static short GetJoystickAxis(IOHIDValueRef val, IOHIDElementRef element) { int max = NativeMethods.IOHIDElementGetLogicalMax(element).ToInt32(); int min = NativeMethods.IOHIDElementGetLogicalMin(element).ToInt32(); int offset = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32(); if (offset < min) offset = min; if (offset > max) offset = max; const int range = short.MaxValue - short.MinValue + 1; const int half_range = short.MaxValue + 1; return (short)((offset - min) * range / (max - min) + half_range); }
public static extern CFIndex IOHIDElementGetLogicalMax(IOHIDElementRef element);
internal static extern bool IOHIDElementHasNullState( IOHIDElementRef element);
public static extern HIDPage IOHIDElementGetUsagePage(IOHIDElementRef elem);
public static extern CFIndex IOHIDElementGetLogicalMin(IOHIDElementRef element);
static bool GetJoystickButton(IOHIDValueRef val, IOHIDElementRef element) { // Todo: analogue buttons are transformed to digital int value = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32(); return value >= 1; }
static JoystickHat TranslateJoystickHat(JoystickData joy, IOHIDElementRef elem) { JoystickHat hat; if (joy.ElementToHat.TryGetValue(elem, out hat)) { return hat; } return JoystickHat.Last + 1; }