import AppKit
|
import CoreGraphics
|
import Foundation
|
|
guard CommandLine.arguments.count == 2,
|
let screenIndex = Int(CommandLine.arguments[1])
|
else {
|
fputs("usage: round1-move-mouse-to-screen-center.swift <screen-index>\n", stderr)
|
exit(2)
|
}
|
|
let screens = NSScreen.screens
|
guard screens.indices.contains(screenIndex) else {
|
fputs("screen index \(screenIndex) is out of range; screen count=\(screens.count)\n", stderr)
|
exit(2)
|
}
|
|
let screen = screens[screenIndex]
|
let displayID = screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? CGDirectDisplayID ?? 0
|
let coreGraphicsFrame = displayID == 0 ? screen.frame : CGDisplayBounds(displayID)
|
let coreGraphicsPoint = CGPoint(x: coreGraphicsFrame.midX, y: coreGraphicsFrame.midY)
|
|
CGWarpMouseCursorPosition(coreGraphicsPoint)
|
CGAssociateMouseAndMouseCursorPosition(boolean_t(1))
|
usleep(100_000)
|
|
print(
|
"screen=\(screenIndex) " +
|
"frame=x:\(Int(screen.frame.minX)),y:\(Int(screen.frame.minY)),w:\(Int(screen.frame.width)),h:\(Int(screen.frame.height)) " +
|
"cgFrame=x:\(Int(coreGraphicsFrame.minX)),y:\(Int(coreGraphicsFrame.minY)),w:\(Int(coreGraphicsFrame.width)),h:\(Int(coreGraphicsFrame.height)) " +
|
"mouse=x:\(Int(NSEvent.mouseLocation.x)),y:\(Int(NSEvent.mouseLocation.y))"
|
)
|