-
-
Save kr4uzi/eab851a460d80a4491654af53688db12 to your computer and use it in GitHub Desktop.
macos_game_mode
This file contains hidden or 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
| #!/usr/bin/env swift | |
| import Foundation | |
| import AppKit | |
| let handle = dlopen("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics", RTLD_NOW) | |
| guard let symConnection = dlsym(handle, "_CGSDefaultConnection"), | |
| let symSetProp = dlsym(handle, "CGSSetConnectionProperty") else { | |
| print("Failed to load CGS symbols") | |
| exit(1) | |
| } | |
| func hideDock() { | |
| let _ = try? runShell("defaults write com.apple.dock autohide -bool true") | |
| let _ = try? runShell("killall Dock") | |
| let _ = try? runShell("defaults write com.apple.dock autohide-delay -float 1000") | |
| let _ = try? runShell("killall Dock") | |
| let _ = try? runShell("defaults write com.apple.dock no-bouncing -bool TRUE") | |
| let _ = try? runShell("killall Dock") | |
| } | |
| func showDock() { | |
| let _ = try? runShell("defaults write com.apple.dock autohide -bool false") | |
| let _ = try? runShell("killall Dock") | |
| let _ = try? runShell("defaults delete com.apple.dock autohide-delay") | |
| let _ = try? runShell("killall Dock") | |
| let _ = try? runShell("defaults write com.apple.dock no-bouncing -bool FALSE") | |
| let _ = try? runShell("killall Dock") | |
| } | |
| @discardableResult | |
| func runShell(_ command: String) throws -> String { | |
| let task = Process() | |
| task.launchPath = "/bin/bash" | |
| task.arguments = ["-c", command] | |
| let pipe = Pipe() | |
| task.standardOutput = pipe | |
| task.standardError = pipe | |
| try task.run() | |
| task.waitUntilExit() | |
| let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
| return String(data: data, encoding: .utf8) ?? "" | |
| } | |
| func GlobalHideCursor() { | |
| typealias CGSConnectionID = Int32 | |
| typealias CGError = Int32 | |
| typealias CGSDefaultConnectionFunc = @convention(c) () -> CGSConnectionID | |
| typealias CGSSetConnectionPropertyFunc = @convention(c) (CGSConnectionID, CGSConnectionID, CFString, CFTypeRef) -> CGError | |
| let getDefaultConnection = unsafeBitCast(symConnection, to: CGSDefaultConnectionFunc.self) | |
| let setProperty = unsafeBitCast(symSetProp, to: CGSSetConnectionPropertyFunc.self) | |
| let conn = getDefaultConnection() | |
| let kProperty = "SetsCursorInBackground" as CFString | |
| let result = setProperty(conn, conn, kProperty, kCFBooleanTrue) | |
| if result != 0 { | |
| print("failed to activate SetsCursorInBackground") | |
| exit(1) | |
| } | |
| if CGDisplayHideCursor(CGMainDisplayID()) != .success { | |
| print("failed to hide cursor") | |
| exit(1) | |
| } | |
| } | |
| // even with the dock hidden, when the mouse is near on the dock location, it reappears again | |
| // registering global mouse events didn't work either in this "script" mode, so a timer needs to do the job | |
| // CPU time with and without timer was equal, so it doesn't seem to cause too much runtime impact | |
| let interval: TimeInterval = 0.07 | |
| let timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { _ in | |
| GlobalHideCursor() | |
| } | |
| func signalHandler(signal: Int32) -> Void { | |
| showDock() | |
| exit(0) | |
| } | |
| signal(SIGINT, signalHandler) | |
| signal(SIGTERM, signalHandler) | |
| hideDock() | |
| print("Running. Press CTRL+C to stop.") | |
| RunLoop.main.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment