Skip to content
Permalink
Browse files

Merge two IOHIDManagers of InputMethodServer into one

  • Loading branch information...
yous committed Oct 20, 2019
1 parent cfa5d0a commit 7cfcc07ec242553984f530cff1eead470b53ee79
Showing with 41 additions and 51 deletions.
  1. +38 −48 OSXCore/InputMethodServer.swift
  2. +3 −3 Podfile.lock
@@ -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<IOKitty>.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<IOKitty>.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<IOKitty>.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,40 +128,13 @@ 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<IOKitty>.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 {
manager.unschedule(runloop: .current, mode: .default)
manager.unregisterInputValueCallback()
let r = manager.close()
assert(r == 0)
rightGuiManager.unschedule(runloop: .current, mode: .default)
rightGuiManager.unregisterInputValueCallback()
assert(rightGuiManager.close() == 0)
}

var capsLockTriggered: Bool {
@@ -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

0 comments on commit 7cfcc07

Please sign in to comment.
You can’t perform that action at this time.