Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions apps/desktop/native/macos-push-to-talk-helper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import ApplicationServices
import CoreGraphics
import Foundation

private var isPushToTalkActive = false
private var eventTapPort: CFMachPort?

private func writeLine(_ value: String) {
if let data = "\(value)\n".data(using: .utf8) {
FileHandle.standardOutput.write(data)
}
fflush(stdout)
}

private func hasPushToTalkModifiers(_ flags: CGEventFlags) -> Bool {
flags.contains(.maskControl)
&& flags.contains(.maskAlternate)
&& !flags.contains(.maskCommand)
}

private func syncPushToTalkState(_ flags: CGEventFlags) {
let nextActive = hasPushToTalkModifiers(flags)
if nextActive == isPushToTalkActive {
return
}

isPushToTalkActive = nextActive
writeLine(nextActive ? "start" : "stop")
}

private let eventCallback: CGEventTapCallBack = { proxy, type, event, refcon in
if type == .tapDisabledByTimeout || type == .tapDisabledByUserInput {
if let eventTapPort {
CGEvent.tapEnable(tap: eventTapPort, enable: true)
}
return Unmanaged.passUnretained(event)
}

if type == .flagsChanged {
syncPushToTalkState(event.flags)
}

return Unmanaged.passUnretained(event)
}

let accessibilityOptions = [
kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true
] as CFDictionary

if !AXIsProcessTrustedWithOptions(accessibilityOptions) {
writeLine("permission-required")
for _ in 0..<120 {
Thread.sleep(forTimeInterval: 1)
if AXIsProcessTrusted() {
writeLine("permission-granted")
break
}
}
}

if !AXIsProcessTrusted() {
writeLine("permission-timeout")
exit(2)
}

let eventMask = (1 << CGEventType.flagsChanged.rawValue)
let tap = CGEvent.tapCreate(
tap: .cgSessionEventTap,
place: .headInsertEventTap,
options: .listenOnly,
eventsOfInterest: CGEventMask(eventMask),
callback: eventCallback,
userInfo: nil
)

guard let eventTap = tap else {
writeLine("tap-unavailable")
exit(3)
}

eventTapPort = eventTap
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, .commonModes)
CGEvent.tapEnable(tap: eventTap, enable: true)
writeLine("ready")
CFRunLoopRun()
5 changes: 3 additions & 2 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"main": "dist-electron/main.cjs",
"scripts": {
"dev": "bun run --parallel dev:bundle dev:electron",
"dev:bundle": "tsdown --watch",
"dev:bundle": "bun run build:native && tsdown --watch",
"dev:electron": "node scripts/dev-electron.mjs",
"build": "tsdown",
"build": "bun run build:native && tsdown",
"build:native": "node scripts/build-native-helpers.mjs",
"start": "node scripts/start-electron.mjs",
"typecheck": "tsc --noEmit",
"test": "vitest run --passWithNoTests",
Expand Down
Binary file not shown.
25 changes: 25 additions & 0 deletions apps/desktop/scripts/build-native-helpers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { mkdirSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { spawnSync } from "node:child_process";

const __dirname = dirname(fileURLToPath(import.meta.url));
const desktopDir = resolve(__dirname, "..");

if (process.platform !== "darwin") {
process.exit(0);
}

const sourcePath = resolve(desktopDir, "native/macos-push-to-talk-helper.swift");
const outputPath = resolve(desktopDir, "resources/native/t3code-push-to-talk-helper");

mkdirSync(dirname(outputPath), { recursive: true });

const result = spawnSync("xcrun", ["swiftc", sourcePath, "-O", "-o", outputPath], {
cwd: desktopDir,
stdio: "inherit",
});

if (result.status !== 0) {
process.exit(result.status ?? 1);
}
Loading
Loading