From 7cfcc07ec242553984f530cff1eead470b53ee79 Mon Sep 17 00:00:00 2001 From: Chayoung You Date: Sun, 20 Oct 2019 19:06:33 +0900 Subject: [PATCH] Merge two IOHIDManagers of InputMethodServer into one --- OSXCore/InputMethodServer.swift | 86 +++++++++++++++------------------ Podfile.lock | 6 +-- 2 files changed, 41 insertions(+), 51 deletions(-) diff --git a/OSXCore/InputMethodServer.swift b/OSXCore/InputMethodServer.swift index 41e468d0..4283e069 100644 --- a/OSXCore/InputMethodServer.swift +++ b/OSXCore/InputMethodServer.swift @@ -47,7 +47,6 @@ class IOKitty { let service: SIOService let connect: SIOConnect let manager: IOHIDManager - let rightGuiManager: IOHIDManager private var defaultCapsLockState: Bool = false var capsLockDate: Date? var rollback: (() -> Void)? @@ -67,17 +66,17 @@ class IOKitty { manager = IOHIDManager.create() manager.setDeviceMatching(page: kHIDPage_GenericDesktop, usage: kHIDUsage_GD_Keyboard) - manager.setInputValueMatching(min: kHIDUsage_KeyboardCapsLock, max: kHIDUsage_KeyboardCapsLock) - - rightGuiManager = IOHIDManager.create() - rightGuiManager.setDeviceMatching(page: kHIDPage_GenericDesktop, usage: kHIDUsage_GD_Keyboard) - rightGuiManager.setInputValueMatching(min: kHIDUsage_KeyboardRightGUI, max: kHIDUsage_KeyboardRightGUI) + manager.setInputValueMatching(min: kHIDUsage_KeyboardCapsLock, max: kHIDUsage_KeyboardRightGUI) let _self = Unmanaged.passUnretained(self) // Set input value callback manager.registerInputValueCallback({ inContext, _, _, value in - guard Configuration.shared.enableCapslockToToggleInputMode else { + let usage = Int(value.element.usage) + guard usage == kHIDUsage_KeyboardCapsLock || usage == kHIDUsage_KeyboardRightGUI else { + return + } + guard Configuration.shared.enableCapslockToToggleInputMode || Configuration.shared.switchLanguageForRightGui else { return } guard let inContext = inContext else { @@ -86,22 +85,40 @@ class IOKitty { } let pressed = value.integerValue > 0 - dlog(DEBUG_IOKIT_EVENT, "caps lock pressed: \(pressed)") - let _self = Unmanaged.fromOpaque(inContext).takeUnretainedValue() - if pressed { - _self.capsLockDate = Date() - dlog(DEBUG_IOKIT_EVENT, "caps lock pressed set in context") - } else { - if _self.defaultCapsLockState || (_self.capsLockDate != nil && !_self.capsLockTriggered) { - // long pressed - _self.defaultCapsLockState = !_self.defaultCapsLockState - _self.rollback?() - _self.rollback = nil + switch usage { + case kHIDUsage_KeyboardCapsLock: + guard Configuration.shared.enableCapslockToToggleInputMode else { + return + } + dlog(DEBUG_IOKIT_EVENT, "caps lock pressed: \(pressed)") + let _self = Unmanaged.fromOpaque(inContext).takeUnretainedValue() + if pressed { + _self.capsLockDate = Date() + dlog(DEBUG_IOKIT_EVENT, "caps lock pressed set in context") } else { - // short pressed - _ = _self.connect.setModifierLock(selector: .capsLock, state: _self.defaultCapsLockState) - _self.capsLockDate = nil + if _self.defaultCapsLockState || (_self.capsLockDate != nil && !_self.capsLockTriggered) { + // long pressed + _self.defaultCapsLockState = !_self.defaultCapsLockState + _self.rollback?() + _self.rollback = nil + } else { + // short pressed + _ = _self.connect.setModifierLock(selector: .capsLock, state: _self.defaultCapsLockState) + _self.capsLockDate = nil + } + } + case kHIDUsage_KeyboardRightGUI: + guard Configuration.shared.switchLanguageForRightGui else { + return } + dlog(DEBUG_IOKIT_EVENT, "right gui pressed: \(pressed)") + let _self = Unmanaged.fromOpaque(inContext).takeUnretainedValue() + if pressed { + _self.rightGuiPressed = true + dlog(DEBUG_IOKIT_EVENT, "right gui pressed set in context") + } + default: + break } // NSEvent.otherEvent(with: .applicationDefined, location: .zero, modifierFlags: .capsLock, timestamp: 0, windowNumber: 0, context: nil, subtype: 0, data1: 0, data2: 0)! }, context: _self.toOpaque()) @@ -111,30 +128,6 @@ class IOKitty { if r != kIOReturnSuccess { dlog(DEBUG_IOKIT_EVENT, "IOHIDManagerOpen failed") } - - rightGuiManager.registerInputValueCallback({ - inContext, _, _, value in - guard Configuration.shared.switchLanguageForRightGui else { - return - } - guard let inContext = inContext else { - dlog(true, "IOKit callback inContext is nil - impossible") - return - } - let pressed = value.integerValue > 0 - dlog(DEBUG_IOKIT_EVENT, "right gui pressed: \(pressed)") - let _self = Unmanaged.fromOpaque(inContext).takeUnretainedValue() - if pressed { - _self.rightGuiPressed = true - dlog(DEBUG_IOKIT_EVENT, "right gui pressed set in context") - } - - }, context: _self.toOpaque()) - - rightGuiManager.schedule(runloop: .current, mode: .default) - if rightGuiManager.open() != kIOReturnSuccess { - dlog(DEBUG_IOKIT_EVENT, "IOHIDManagerOpen failed") - } } deinit { @@ -142,9 +135,6 @@ class IOKitty { manager.unregisterInputValueCallback() let r = manager.close() assert(r == 0) - rightGuiManager.unschedule(runloop: .current, mode: .default) - rightGuiManager.unregisterInputValueCallback() - assert(rightGuiManager.close() == 0) } var capsLockTriggered: Bool { diff --git a/Podfile.lock b/Podfile.lock index f5ac7aec..603fab12 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -21,7 +21,7 @@ PODS: - SwiftCarbon/SwiftCarbon (0.1.0): - SwiftCarbon/HIToolbox - SwiftCoreServices (0.1.1) - - SwiftIOKit (0.1.0) + - SwiftIOKit (0.1.2) DEPENDENCIES: - cdebug @@ -55,8 +55,8 @@ SPEC CHECKSUMS: MASShortcut: d9e4909e878661cc42877cc9d6efbe638273ab57 SwiftCarbon: c48bbbff6c451022e89c46b85d0e9b44c64ef61b SwiftCoreServices: e4aebffc7143ff3e06920ecb89654f7c81f9768a - SwiftIOKit: 9dce3dec67e00dc74b95fde86f257aca78239853 + SwiftIOKit: 58e9b718109ba7c6681fc45c5888838627df28f1 PODFILE CHECKSUM: bd16d9e213df73a5a491232ac0d601cc5c52a6ff -COCOAPODS: 1.8.0 +COCOAPODS: 1.8.4