99 lines
4.5 KiB
JavaScript
99 lines
4.5 KiB
JavaScript
ObjC.import('AppKit')
|
|
ObjC.import('CoreFoundation')
|
|
ObjC.import('CoreGraphics')
|
|
ObjC.import('Foundation')
|
|
|
|
function overlaps(a, b) {
|
|
return a.X < b.X + b.Width && b.X < a.X + a.Width && a.Y < b.Y + b.Height && b.Y < a.Y + a.Height
|
|
}
|
|
|
|
function activeWindow() {
|
|
// QMP captures the main display. The experimental mode requires a single display.
|
|
if ($.NSScreen.screens.count !== 1) return null
|
|
const display = $.CGDisplayBounds($.CGMainDisplayID())
|
|
const displayWidth = Math.round(display.size.width)
|
|
const displayHeight = Math.round(display.size.height)
|
|
if (!(displayWidth > 0 && displayHeight > 0)) return null
|
|
|
|
const app = $.NSWorkspace.sharedWorkspace.frontmostApplication
|
|
if (!app || app.isNil() || app.activationPolicy !== $.NSApplicationActivationPolicyRegular) return null
|
|
const options = $.kCGWindowListOptionOnScreenOnly | $.kCGWindowListExcludeDesktopElements
|
|
const raw = $.CGWindowListCopyWindowInfo(options, 0)
|
|
if (!raw) return null
|
|
let windows
|
|
try {
|
|
windows = ObjC.deepUnwrap(ObjC.castRefToObject(raw))
|
|
} finally {
|
|
$.CFRelease(raw)
|
|
}
|
|
if (!Array.isArray(windows)) return null
|
|
const appPID = app.processIdentifier
|
|
const foreignAbove = []
|
|
for (const window of windows) {
|
|
const bounds = window && window.kCGWindowBounds
|
|
if (!bounds || !(window.kCGWindowAlpha > 0.01)) continue
|
|
if (window.kCGWindowOwnerPID !== appPID) {
|
|
foreignAbove.push(bounds)
|
|
continue
|
|
}
|
|
if (window.kCGWindowLayer !== 0 || !(bounds.Width >= 32 && bounds.Height >= 32)) continue
|
|
const x = Math.floor(bounds.X - display.origin.x)
|
|
const y = Math.floor(bounds.Y - display.origin.y)
|
|
const width = Math.ceil(bounds.X + bounds.Width - display.origin.x) - x
|
|
const height = Math.ceil(bounds.Y + bounds.Height - display.origin.y) - y
|
|
if (x >= displayWidth || y >= displayHeight || x + width <= 0 || y + height <= 0) continue
|
|
const id = window.kCGWindowNumber
|
|
if (!Number.isInteger(id) || id <= 0 || id > 0xFFFFFFFF) continue
|
|
if (foreignAbove.some(above => overlaps(above, bounds))) return null
|
|
const label = app.localizedName
|
|
const appName = ((label && !label.isNil() ? label.js : window.kCGWindowOwnerName) || '')
|
|
.replace(/[\x00-\x1f\x7f-\x9f]/g, '').trim().slice(0, 64)
|
|
return {id, app_name: appName || 'Mac App', x, y, width, height, display_width: displayWidth,
|
|
display_height: displayHeight}
|
|
}
|
|
return null
|
|
}
|
|
|
|
function postReport(url, token, generation, window) {
|
|
const config = [
|
|
'url = ' + JSON.stringify(url),
|
|
'header = ' + JSON.stringify('Authorization: Bearer ' + token),
|
|
'header = "Content-Type: application/json"',
|
|
'data-raw = ' + JSON.stringify(JSON.stringify({generation, window})),
|
|
]
|
|
const task = $.NSTask.alloc.init
|
|
const input = $.NSPipe.pipe
|
|
const output = $.NSPipe.pipe
|
|
task.launchPath = '/usr/bin/curl'
|
|
task.arguments = $(['-q', '--noproxy', '*', '-sm', '1', '-o', '/dev/null', '-w', '%{http_code}', '-K', '-'])
|
|
task.standardInput = input
|
|
task.standardOutput = output
|
|
task.standardError = $.NSFileHandle.fileHandleWithNullDevice
|
|
task.launch
|
|
try { input.fileHandleForWriting.writeData($(config.join('\n') + '\n').dataUsingEncoding($.NSUTF8StringEncoding)) }
|
|
finally { input.fileHandleForWriting.closeFile }
|
|
const data = output.fileHandleForReading.readDataToEndOfFile
|
|
task.waitUntilExit
|
|
return task.terminationStatus === 0 ? Number($.NSString.alloc.initWithDataEncoding(data, $.NSUTF8StringEncoding).js) : 0
|
|
}
|
|
|
|
function run(args) {
|
|
const generation = Number(args[0])
|
|
if (!Number.isSafeInteger(generation) || generation <= 0) return
|
|
const env = $.NSProcessInfo.processInfo.environment
|
|
const hostValue = env.objectForKey($('REIMS_HOST_URL'))
|
|
const tokenValue = env.objectForKey($('REIMS_TOKEN'))
|
|
const host = hostValue && !hostValue.isNil() ? hostValue.js.replace(/\/+$/, '') : ''
|
|
const token = tokenValue && !tokenValue.isNil() ? tokenValue.js : ''
|
|
if (!/^http:\/\/(?:[a-z0-9.-]+|\[[0-9a-f:]+\]):\d{1,5}$/i.test(host) || !token || /[\r\n\0]/.test(token)) return
|
|
const url = host + '/v1/active-window'
|
|
while (true) {
|
|
let window
|
|
try { window = activeWindow() } catch (_) { window = null }
|
|
let status
|
|
try { status = postReport(url, token, generation, window) } catch (_) { status = 0 }
|
|
if (status === 401 || status === 403 || status === 409) return
|
|
delay(status === 204 ? 0.25 : 1)
|
|
}
|
|
}
|