forked from Pyroh/Fluor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] Get rid of legacy, deprecated objc-c code
- Loading branch information
Showing
8 changed files
with
109 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// FKeyManager.swift | ||
// Fluor | ||
// | ||
// | ||
|
||
enum FKeyManager { | ||
typealias FKeyManagerResult = Result<FKeyMode, FKeyManagerError> | ||
|
||
enum FKeyManagerError: Error { | ||
case cannotCreateMasterPort | ||
case cannotOpenService | ||
case cannotSetParameter | ||
case cannotGetParameter | ||
|
||
case otherError | ||
} | ||
|
||
private static func getIORegistry() throws -> io_registry_entry_t { | ||
var masterPort: mach_port_t = .zero | ||
guard IOMasterPort(bootstrap_port, &masterPort) == KERN_SUCCESS else { throw FKeyManagerError.cannotCreateMasterPort } | ||
|
||
return IORegistryEntryFromPath(masterPort, "IOService:/IOResources/IOHIDSystem") | ||
} | ||
|
||
private static func getIOHandle() throws -> io_service_t { | ||
try self.getIORegistry() as io_service_t | ||
} | ||
|
||
private static func getServiceConnect() throws -> io_connect_t { | ||
var service: io_connect_t = .zero | ||
let handle = try self.getIOHandle() | ||
defer { IOObjectRelease(handle) } | ||
|
||
guard IOServiceOpen(handle, mach_task_self_, UInt32(kIOHIDParamConnectType), &service) == KERN_SUCCESS else { | ||
throw FKeyManagerError.cannotOpenService | ||
} | ||
|
||
return service | ||
} | ||
|
||
static func setCurrentFKeyMode(_ mode: FKeyMode) -> FKeyManagerResult { | ||
do { | ||
let connect = try FKeyManager.getServiceConnect() | ||
let value = mode.rawValue as CFNumber | ||
|
||
guard IOHIDSetCFTypeParameter(connect, kIOHIDFKeyModeKey as CFString, value) == KERN_SUCCESS else { | ||
throw FKeyManagerError.cannotSetParameter | ||
} | ||
|
||
IOServiceClose(connect) | ||
return .success(mode) | ||
} catch let error as FKeyManagerError { | ||
return .failure(error) | ||
} catch { | ||
return .failure(.otherError) | ||
} | ||
} | ||
|
||
static func getCurrentFKeyMode() -> FKeyManagerResult { | ||
do { | ||
let ri = try self.getIORegistry() | ||
defer { IOObjectRelease(ri) } | ||
|
||
guard let entry = IORegistryEntryCreateCFProperty(ri, "HIDParameters" as CFString, kCFAllocatorDefault, 0), let dict = entry.takeUnretainedValue() as? NSDictionary, let mode = dict.value(forKey: "HIDFKeyMode") as? Int, let currentMode = FKeyMode(rawValue: mode) else { | ||
throw FKeyManagerError.cannotGetParameter | ||
} | ||
return .success(currentMode) | ||
} catch let error as FKeyManagerError { | ||
return .failure(error) | ||
} catch { | ||
return .failure(.otherError) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.