-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.swift
More file actions
276 lines (222 loc) · 9.27 KB
/
Copy pathmenu.swift
File metadata and controls
276 lines (222 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import AppKit
import Foundation
import ApplicationServices
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem!
var statusMenuItem: NSMenuItem!
var startMenuItem: NSMenuItem!
var stopMenuItem: NSMenuItem!
var disableWhammyTiltMenuItem: NSMenuItem!
var nodePath: String = "node"
var resourcePath: String = ""
var bridgeScriptPath: String = ""
let logPath = "/tmp/macriff.log"
var disableWhammyTilt: Bool = false
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Request Accessibility permissions with native macOS prompt on launch
let key = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as CFString
let options = [key: true] as CFDictionary
_ = AXIsProcessTrustedWithOptions(options)
// Resolve bundle resource paths
let bundle = Bundle.main
resourcePath = bundle.resourcePath ?? "/Users/cdapayne/Documents/GitHub/MacRiff"
bridgeScriptPath = "\(resourcePath)/bridge.js"
// Find Node.js installation path
nodePath = findNodePath()
print("[MacRiffApp] Using Node path: \(nodePath)")
print("[MacRiffApp] Using Resource path: \(resourcePath)")
// Create system status bar item
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = statusItem.button {
button.title = "🎸 MacRiff"
}
let menu = NSMenu()
statusMenuItem = NSMenuItem(title: "Status: Stopped", action: nil, keyEquivalent: "")
statusMenuItem.isEnabled = false
menu.addItem(statusMenuItem)
menu.addItem(NSMenuItem.separator())
startMenuItem = NSMenuItem(title: "Start Bridge", action: #selector(startBridge), keyEquivalent: "s")
startMenuItem.target = self
menu.addItem(startMenuItem)
stopMenuItem = NSMenuItem(title: "Stop Bridge", action: #selector(stopBridge), keyEquivalent: "t")
stopMenuItem.target = self
stopMenuItem.isEnabled = false
menu.addItem(stopMenuItem)
menu.addItem(NSMenuItem.separator())
disableWhammyTiltMenuItem = NSMenuItem(title: "Disable Whammy & Star Power", action: #selector(toggleDisableWhammyTilt), keyEquivalent: "")
disableWhammyTiltMenuItem.target = self
disableWhammyTiltMenuItem.state = .off
menu.addItem(disableWhammyTiltMenuItem)
menu.addItem(NSMenuItem.separator())
let viewLogsItem = NSMenuItem(title: "View Logs...", action: #selector(viewLogs), keyEquivalent: "l")
viewLogsItem.target = self
menu.addItem(viewLogsItem)
menu.addItem(NSMenuItem.separator())
let quitItem = NSMenuItem(title: "Quit", action: #selector(quitApp), keyEquivalent: "q")
quitItem.target = self
menu.addItem(quitItem)
statusItem.menu = menu
// Poll status every 2 seconds
Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { _ in
self.updateStatus()
}
// Initial status check
updateStatus()
}
func findNodePath() -> String {
// 1. Quick check common locations first (avoids slow login shell spin-up)
let commonPaths = [
"/opt/homebrew/bin/node",
"/usr/local/bin/node",
"/usr/bin/node"
]
for path in commonPaths {
if FileManager.default.fileExists(atPath: path) {
return path
}
}
// 2. Fallback to querying node using a login shell (captures NVM, FNM, etc.)
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/zsh")
process.arguments = ["-l", "-c", "which node"]
let pipe = Pipe()
process.standardOutput = pipe
do {
try process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let path = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines), !path.isEmpty, path.contains("/") {
return path
}
} catch {}
return "node" // final fallback
}
@objc func toggleDisableWhammyTilt() {
disableWhammyTilt.toggle()
disableWhammyTiltMenuItem.state = disableWhammyTilt ? .on : .off
// If the bridge is currently running, restart it with the new arguments
if checkBridgeRunning() {
DispatchQueue.global(qos: .userInitiated).async {
self.stopBridgeSync()
Thread.sleep(forTimeInterval: 0.5)
self.startBridgeSync()
}
}
}
@objc func startBridge() {
// Run in background thread so password prompt doesn't freeze menu bar UI
DispatchQueue.global(qos: .userInitiated).async {
self.startBridgeSync()
}
}
func startBridgeSync() {
let extraArgs = self.disableWhammyTilt ? " --no-special" : ""
let osascript = """
do shell script "cd \\"\(self.resourcePath)\\" && \\"\(self.nodePath)\\" bridge.js\(extraArgs) > \\"\(self.logPath)\\" 2>&1 < /dev/null &" with administrator privileges
"""
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/osascript")
process.arguments = ["-e", osascript]
do {
try process.run()
process.waitUntilExit()
// Wait a moment for process to spin up and update status on the main thread
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.updateStatus()
}
} catch {
print("Failed to run start bridge osascript: \(error)")
}
}
@objc func stopBridge() {
// Run in background thread to keep UI completely responsive
DispatchQueue.global(qos: .userInitiated).async {
self.stopBridgeSync()
}
}
func stopBridgeSync() {
let osascript = """
do shell script "pkill -f bridge.js" with administrator privileges
"""
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/osascript")
process.arguments = ["-e", osascript]
do {
try process.run()
process.waitUntilExit()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.updateStatus()
}
} catch {
print("Failed to run stop bridge osascript: \(error)")
}
}
@objc func viewLogs() {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/open")
process.arguments = [logPath]
try? process.run()
}
@objc func quitApp() {
// Automatically stop the bridge on app termination
let osascript = """
do shell script "pkill -f bridge.js" with administrator privileges
"""
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/osascript")
process.arguments = ["-e", osascript]
try? process.run()
process.waitUntilExit()
NSApplication.shared.terminate(self)
}
func updateStatus() {
// Offload pgrep checking to a background thread to keep UI completely thread-safe
DispatchQueue.global(qos: .background).async {
let isRunning = self.checkBridgeRunning()
DispatchQueue.main.async {
self.updateUI(isRunning: isRunning)
}
}
}
func updateUI(isRunning: Bool) {
if isRunning {
statusMenuItem.title = "Status: Running"
startMenuItem.isEnabled = false
stopMenuItem.isEnabled = true
if let button = statusItem.button {
button.title = "🎸 MacRiff (Active)"
}
} else {
statusMenuItem.title = "Status: Stopped"
startMenuItem.isEnabled = true
stopMenuItem.isEnabled = false
if let button = statusItem.button {
button.title = "🎸 MacRiff"
}
}
}
func checkBridgeRunning() -> Bool {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/sh")
process.arguments = ["-c", "pgrep -f bridge.js"]
let pipe = Pipe()
process.standardOutput = pipe
do {
try process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8), !output.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return true
}
} catch {
return false
}
return false
}
}
// Bootstrap Cocoa application
let app = NSApplication.shared
let delegate = AppDelegate()
app.delegate = delegate
app.setActivationPolicy(.accessory) // Hide App icon from the Dock and run as a status bar agent
app.run()