// 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 IDisposable.Dispose()
        {
            if (hidmanager != IntPtr.Zero)
            {
                //throw exception don't know why
//				if(RunLoop!=IntPtr.Zero && InputLoopMode!=IntPtr.Zero)
//				Native.IOHIDDeviceUnscheduleWithRunLoop(hidmanager,
//				                                        RunLoop, InputLoopMode);


                Debug.Log("Try to remove OSXHIDInterface registers");

                Native.IOHIDManagerRegisterDeviceMatchingCallback(
                    hidmanager, IntPtr.Zero, IntPtr.Zero);
                Native.IOHIDManagerRegisterDeviceRemovalCallback(
                    hidmanager, IntPtr.Zero, IntPtr.Zero);

                Debug.Log("Try to release HIDManager");
                Native.CFRelease(hidmanager);
            }

            lock (syncRoot){
                if (Generics != null)
                {
                    foreach (KeyValuePair <string, HIDDevice> entry in Generics)
                    {
                        entry.Value.Dispose();
                    }


                    Generics.Clear();
                }


                Debug.Log("Try to remove Drivers");
                if (__drivers != null)
                {
                    __drivers.Clear();
                }
            }
        }