private static void InputReceived(System.IntPtr context, System.IntPtr res, System.IntPtr sender, System.IntPtr value) { System.IntPtr element = MacNative.IOHIDValueGetElement(value); System.IntPtr cookie = MacNative.IOHIDElementGetCookie(element); ushort page = MacNative.IOHIDElementGetUsagePage(element); int usage = MacNative.IOHIDElementGetUsage(element); switch (page) { case HIDDesktop: switch (usage) { // X case HIDX: ManageXAxis(context, element, value); break; case HIDY: ManageYAxis(context, element, value); break; } break; case HIDButtons: ManageButtons(context, cookie, value); break; } }
private static void DeviceAdded(System.IntPtr context, System.IntPtr res, System.IntPtr sender, System.IntPtr device) { if (MacNative.IOHIDDeviceOpen(device, IntPtr.Zero) == IntPtr.Zero) { if (MacNative.IOHIDDeviceConformsTo(device, HIDDesktop, HIDJoystick) || MacNative.IOHIDDeviceConformsTo(device, HIDDesktop, HIDGamePad) || MacNative.IOHIDDeviceConformsTo(device, HIDDesktop, HIDMultiAxis) || MacNative.IOHIDDeviceConformsTo(device, HIDDesktop, HIDWheel)) { System.IntPtr product = MacNative.IOHIDDeviceGetProperty(device, MacNative.CFString("Product")); string productName = MacNative.CString(product); int index = 0; foreach (Engine.Joystick joystick in engine.joysticks) { if (joystick == null) { engine.joysticks [index] = new Engine.Joystick(); engine.joysticks [index].index = index; engine.joysticks [index].id = device.ToInt64(); engine.joysticks [index].name = productName; MacNative.IOHIDDeviceRegisterInputValueCallback(device, DeviceInputCallback, device); MacNative.IOHIDDeviceScheduleWithRunLoop(device, runLoop, defaultLoopMode); return; } index++; } } MacNative.IOHIDDeviceClose(device, IntPtr.Zero); } }
private static void ManageYAxis(System.IntPtr device, System.IntPtr element, System.IntPtr value) { Engine.Joystick joystick = GetJoystick(device); if (joystick == null) { return; } int min = MacNative.IOHIDElementGetLogicalMin(element).ToInt32(); int max = MacNative.IOHIDElementGetLogicalMax(element).ToInt32(); int axis = MacNative.IOHIDValueGetIntegerValue(value).ToInt32(); joystick.y = Normalize(axis, min, max); }
private static void ManageButtons(System.IntPtr device, System.IntPtr cookie, System.IntPtr value) { if (cookie.ToInt32() < 0 || cookie.ToInt32() > 19) { return; } Engine.Joystick joystick = GetJoystick(device); if (joystick == null) { return; } int buttonPressed = MacNative.IOHIDValueGetIntegerValue(value).ToInt32(); joystick.buttons [cookie.ToInt32()] = buttonPressed >= 1; }
private static void DeviceRemoved(System.IntPtr context, System.IntPtr res, System.IntPtr sender, System.IntPtr device) { foreach (Engine.Joystick joystick in engine.joysticks) { if (joystick != null) { if (joystick.id == device.ToInt64()) { engine.joysticks [joystick.index] = null; MacNative.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero); MacNative.IOHIDDeviceUnscheduleFromRunLoop(device, runLoop, defaultLoopMode); MacNative.IOHIDDeviceClose(device, IntPtr.Zero); return; } } } }
public static void Initialize(Engine _engine) { runLoop = MacNative.CFRunLoopGetMain(); if (runLoop == IntPtr.Zero) { runLoop = MacNative.CFRunLoopGetCurrent(); } if (runLoop == IntPtr.Zero) { throw(new Exception("[mac-input] unable to get a RunLoop")); } // avoid the RunLoop to be garbaged MacNative.CFRetain(runLoop); System.IntPtr hidManager = MacNative.IOHIDManagerCreate(IntPtr.Zero, IntPtr.Zero); if (hidManager == IntPtr.Zero) { throw(new Exception("[mac-input] unable to get a HidManager()")); } DeviceAddedCallback = DeviceAdded; MacNative.IOHIDManagerRegisterDeviceMatchingCallback(hidManager, DeviceAddedCallback, IntPtr.Zero); DeviceRemovedCallback = DeviceRemoved; MacNative.IOHIDManagerRegisterDeviceRemovalCallback(hidManager, DeviceRemovedCallback, IntPtr.Zero); DeviceInputCallback = InputReceived; MacNative.IOHIDManagerScheduleWithRunLoop(hidManager, runLoop, defaultLoopMode); MacNative.IOHIDManagerSetDeviceMatching(hidManager, IntPtr.Zero); MacNative.IOHIDManagerOpen(hidManager, IntPtr.Zero); engine = _engine; engine.OnAfterUpdate += new Engine.AfterUpdateEventHandler(RunLoopStep); }
// this is run at each refresh private static void RunLoopStep(object sender) { MacNative.CFRunLoopRunInMode(defaultLoopMode, 0, false); }