From 6417c0193e3239a584104db1da5cadb3f72fd621 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Wed, 1 Nov 2023 11:58:15 +0800 Subject: [PATCH] CapsLockToggler // Fix a memory-leak issue. --- .../SwiftyCapsLockToggler.swift | 62 ++++++++++++++----- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/Packages/HangarRash_SwiftyCapsLockToggler/Sources/SwiftyCapsLockToggler/SwiftyCapsLockToggler.swift b/Packages/HangarRash_SwiftyCapsLockToggler/Sources/SwiftyCapsLockToggler/SwiftyCapsLockToggler.swift index f24517f5..6027ccc9 100644 --- a/Packages/HangarRash_SwiftyCapsLockToggler/Sources/SwiftyCapsLockToggler/SwiftyCapsLockToggler.swift +++ b/Packages/HangarRash_SwiftyCapsLockToggler/Sources/SwiftyCapsLockToggler/SwiftyCapsLockToggler.swift @@ -7,30 +7,58 @@ import CapsLockToggler public enum CapsLockToggler { public static func toggle() { - let ioService: io_service_t = IOServiceGetMatchingService(0, IOServiceMatching(kIOHIDSystemClass)) - var ioConnect: io_connect_t = 0 - IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &ioConnect) - var state = false - IOHIDGetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), &state) - state.toggle() - IOHIDSetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), state) - IOServiceClose(ioConnect) + try? IOKit.handleHIDSystemService { ioConnect in + var state = false + IOHIDGetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), &state) + state.toggle() + IOHIDSetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), state) + } } public static var isOn: Bool { - let ioService: io_service_t = IOServiceGetMatchingService(0, IOServiceMatching(kIOHIDSystemClass)) - var ioConnect: io_connect_t = 0 - IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &ioConnect) var state = false - IOHIDGetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), &state) + try? IOKit.handleHIDSystemService { ioConnect in + IOHIDGetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), &state) + } return state } public static func turnOff() { - let ioService: io_service_t = IOServiceGetMatchingService(0, IOServiceMatching(kIOHIDSystemClass)) - var ioConnect: io_connect_t = 0 - IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &ioConnect) - IOHIDSetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), false) - IOServiceClose(ioConnect) + try? IOKit.handleHIDSystemService { ioConnect in + IOHIDSetModifierLockState(ioConnect, Int32(kIOHIDCapsLockState), false) + } + } +} + +// Refactored by Shiki Suen (MIT License) +public enum IOKit { + public static func handleHIDSystemService(_ taskHandler: @escaping (io_connect_t) -> Void) throws { + let ioService: io_service_t = IOServiceGetMatchingService(0, IOServiceMatching(kIOHIDSystemClass)) + var connect: io_connect_t = 0 + let x = IOServiceOpen(ioService, mach_task_self_, UInt32(kIOHIDParamConnectType), &connect) + if let errorOne = Mach.KernReturn(rawValue: x), errorOne != .success { + throw errorOne + } + taskHandler(connect) + let y = IOServiceClose(connect) + if let errorTwo = Mach.KernReturn(rawValue: y), errorTwo != .success { + throw errorTwo + } + } +} + +// Refactored by Shiki Suen (MIT License) +public enum Mach { + public enum KernReturn: Int32, Error { + case success = 0 + case invalidAddress = 1 + case protectionFailure = 2 + case noSpace = 3 + case invalidArgument = 4 + case failure = 5 + case resourceShortage = 6 + case notReceiver = 7 + case noAccess = 8 + case memoryFailure = 9 } }