@factiii/runner 0.27.0 → 0.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DISCLOSURE +174 -0
- package/README.md +36 -16
- package/dist/cli.js +73257 -39541
- package/dist/vault-client.js +876 -0
- package/dist/vault-db.js +858 -0
- package/embedder/app.py +331 -0
- package/embedder/clip_tokenizer.py +137 -0
- package/embedder/convert_clip.py +119 -0
- package/embedder/convert_onnx.py +129 -0
- package/embedder/convert_text.py +148 -0
- package/embedder/onnx_backend.py +48 -0
- package/embedder/overrides-onnx.txt +3 -0
- package/embedder/requirements-onnx.txt +22 -0
- package/embedder/requirements.txt +20 -0
- package/embedder/serve.py +241 -0
- package/embedder/text_tokenizer.py +34 -0
- package/native/simhid.m +7 -0
- package/native/simstream.m +7 -0
- package/package.json +26 -14
- package/plugin/factiii/bin/factiii-board-mcp +11 -3
- package/plugin/factiii/bin/factiii-secrets +88 -21
- package/plugin/factiii/bin/session-context.sh +55 -2
- package/plugin/factiii/skills/board-sessions/SKILL.md +74 -0
- package/plugin/factiii/skills/factiii-secrets/SKILL.md +12 -3
- package/plugin/factiii/skills/runner-onboarding/SKILL.md +67 -0
- package/plugin/factiii/skills/scratchpad/SKILL.md +1 -0
- package/plugin/factiii/skills/share-a-port/SKILL.md +11 -6
- package/index/image-package.json +0 -8
- package/index/index.js +0 -105575
- package/native/screenhost.m +0 -500
- package/vendor/frpc-darwin-amd64 +0 -0
- package/vendor/frpc-darwin-arm64 +0 -0
- package/vendor/frpc-linux-amd64 +0 -0
- package/vendor/frpc-linux-arm64 +0 -0
package/native/screenhost.m
DELETED
|
@@ -1,500 +0,0 @@
|
|
|
1
|
-
// The Mac's own screen, for the runner owner's desktop view
|
|
2
|
-
// (.specs/desktop-view.md). One program does both halves, because macOS grants
|
|
3
|
-
// Screen Recording and Accessibility to a program, and one program is one pair
|
|
4
|
-
// of switches for the owner to turn on:
|
|
5
|
-
//
|
|
6
|
-
// out: each H.264 access unit on stdout as [4-byte BE length][Annex-B
|
|
7
|
-
// payload], the framing simstream.m uses, so the runner's video path
|
|
8
|
-
// does not care which helper is feeding it;
|
|
9
|
-
// in: one JSON command per line on stdin: pointer, scroll, key, text.
|
|
10
|
-
//
|
|
11
|
-
// It is built into an app bundle (FactiiiScreen.app) rather than run as a bare
|
|
12
|
-
// binary. macOS charges a child process's privacy requests to whatever
|
|
13
|
-
// launched it, which for a runner is `node` or the terminal it was started
|
|
14
|
-
// from. The bundle has its own identifier, and this program gives up its
|
|
15
|
-
// parent's responsibility on start (disclaimResponsibility below), so the
|
|
16
|
-
// grant belongs to "Factiii Screen" and survives node and runner upgrades.
|
|
17
|
-
//
|
|
18
|
-
// Public APIs only (ScreenCaptureKit, VideoToolbox, CGEvent). Nothing here
|
|
19
|
-
// moves between Xcode versions the way the simulator helpers' frameworks do.
|
|
20
|
-
#import <AppKit/AppKit.h>
|
|
21
|
-
#import <ApplicationServices/ApplicationServices.h>
|
|
22
|
-
#import <CoreMedia/CoreMedia.h>
|
|
23
|
-
#import <Foundation/Foundation.h>
|
|
24
|
-
#import <ScreenCaptureKit/ScreenCaptureKit.h>
|
|
25
|
-
#import <VideoToolbox/VideoToolbox.h>
|
|
26
|
-
#import <dlfcn.h>
|
|
27
|
-
#import <mach-o/dyld.h>
|
|
28
|
-
#import <os/lock.h>
|
|
29
|
-
#import <spawn.h>
|
|
30
|
-
|
|
31
|
-
extern char **environ;
|
|
32
|
-
|
|
33
|
-
static const char *kDisclaimed = "FACTIII_SCREEN_DISCLAIMED";
|
|
34
|
-
|
|
35
|
-
// Re-exec as our own "responsible process". Without this the Screen Recording
|
|
36
|
-
// and Accessibility prompts name the runner's parent, and a grant made to it
|
|
37
|
-
// is a grant to everything that parent ever starts. The call is the one
|
|
38
|
-
// browsers and IDEs use for the same reason; it is looked up at run time so a
|
|
39
|
-
// macOS without it runs on, charged to the parent as before.
|
|
40
|
-
static void disclaimResponsibility(char *const argv[]) {
|
|
41
|
-
if (getenv(kDisclaimed)) return;
|
|
42
|
-
int (*setdisclaim)(posix_spawnattr_t *, int) =
|
|
43
|
-
dlsym(RTLD_DEFAULT, "responsibility_spawnattrs_setdisclaim");
|
|
44
|
-
if (!setdisclaim) return;
|
|
45
|
-
char path[4096];
|
|
46
|
-
uint32_t size = sizeof(path);
|
|
47
|
-
if (_NSGetExecutablePath(path, &size) != 0) return;
|
|
48
|
-
posix_spawnattr_t attr;
|
|
49
|
-
if (posix_spawnattr_init(&attr) != 0) return;
|
|
50
|
-
// SETEXEC replaces this process, so stdin and stdout stay the runner's pipes.
|
|
51
|
-
if (posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETEXEC) == 0 && setdisclaim(&attr, 1) == 0) {
|
|
52
|
-
setenv(kDisclaimed, "1", 1);
|
|
53
|
-
posix_spawn(NULL, path, NULL, &attr, argv, environ);
|
|
54
|
-
// Only reached when the exec failed: carry on undisclaimed.
|
|
55
|
-
unsetenv(kDisclaimed);
|
|
56
|
-
}
|
|
57
|
-
posix_spawnattr_destroy(&attr);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// ── What the runner is told ──────────────────────────────────────────────
|
|
61
|
-
//
|
|
62
|
-
// One JSON object per line on stderr, each with a "status". The store reads
|
|
63
|
-
// these; anything else on stderr is a log line for a person.
|
|
64
|
-
static void say(NSDictionary *status) {
|
|
65
|
-
NSData *json = [NSJSONSerialization dataWithJSONObject:status options:0 error:nil];
|
|
66
|
-
if (!json) return;
|
|
67
|
-
fwrite(json.bytes, 1, json.length, stderr);
|
|
68
|
-
fputc('\n', stderr);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
static BOOL canCapture(void) { return CGPreflightScreenCaptureAccess(); }
|
|
72
|
-
static BOOL canControl(void) { return AXIsProcessTrusted(); }
|
|
73
|
-
|
|
74
|
-
static NSDictionary *permissions(void) {
|
|
75
|
-
return @{ @"status": @"permissions", @"screen": @(canCapture()), @"control": @(canControl()) };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// ── Encode ───────────────────────────────────────────────────────────────
|
|
79
|
-
|
|
80
|
-
static int gFPS = 30, gWidth = 0, gBitrate = 4000000;
|
|
81
|
-
static VTCompressionSessionRef gSession = NULL;
|
|
82
|
-
static size_t gSourceWidth = 0, gSourceHeight = 0;
|
|
83
|
-
static dispatch_queue_t gWrite;
|
|
84
|
-
static CVPixelBufferRef gLatest = NULL;
|
|
85
|
-
static os_unfair_lock gLock = OS_UNFAIR_LOCK_INIT;
|
|
86
|
-
|
|
87
|
-
static void emit(const void *bytes, size_t len) {
|
|
88
|
-
dispatch_sync(gWrite, ^{
|
|
89
|
-
uint8_t header[4] = { (uint8_t)(len >> 24), (uint8_t)(len >> 16),
|
|
90
|
-
(uint8_t)(len >> 8), (uint8_t)len };
|
|
91
|
-
fwrite(header, 1, 4, stdout);
|
|
92
|
-
fwrite(bytes, 1, len, stdout);
|
|
93
|
-
fflush(stdout);
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// VideoToolbox hands back AVCC (4-byte lengths) with the parameter sets apart;
|
|
98
|
-
// the wire wants Annex-B, with them repeated before every keyframe so a viewer
|
|
99
|
-
// that joins late can start decoding.
|
|
100
|
-
static void appendAnnexB(NSMutableData *out, const uint8_t *nal, size_t len) {
|
|
101
|
-
const uint8_t start[4] = { 0, 0, 0, 1 };
|
|
102
|
-
[out appendBytes:start length:4];
|
|
103
|
-
[out appendBytes:nal length:len];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
static void onEncoded(void *ctx, void *src, OSStatus status, VTEncodeInfoFlags flags,
|
|
107
|
-
CMSampleBufferRef sample) {
|
|
108
|
-
if (status != noErr || !sample || !CMSampleBufferDataIsReady(sample)) return;
|
|
109
|
-
NSMutableData *out = [NSMutableData data];
|
|
110
|
-
CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sample, false);
|
|
111
|
-
BOOL keyframe = YES;
|
|
112
|
-
if (attachments && CFArrayGetCount(attachments)) {
|
|
113
|
-
CFDictionaryRef d = CFArrayGetValueAtIndex(attachments, 0);
|
|
114
|
-
keyframe = !CFDictionaryContainsKey(d, kCMSampleAttachmentKey_NotSync);
|
|
115
|
-
}
|
|
116
|
-
if (keyframe) {
|
|
117
|
-
CMFormatDescriptionRef fmt = CMSampleBufferGetFormatDescription(sample);
|
|
118
|
-
size_t count = 0;
|
|
119
|
-
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(fmt, 0, NULL, NULL, &count, NULL);
|
|
120
|
-
for (size_t i = 0; i < count; i++) {
|
|
121
|
-
const uint8_t *ps = NULL; size_t psLen = 0;
|
|
122
|
-
if (CMVideoFormatDescriptionGetH264ParameterSetAtIndex(fmt, i, &ps, &psLen, NULL, NULL) == noErr)
|
|
123
|
-
appendAnnexB(out, ps, psLen);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample);
|
|
127
|
-
size_t total = 0; char *data = NULL;
|
|
128
|
-
if (CMBlockBufferGetDataPointer(block, 0, NULL, &total, &data) != noErr) return;
|
|
129
|
-
size_t offset = 0;
|
|
130
|
-
while (offset + 4 <= total) {
|
|
131
|
-
uint32_t len = 0;
|
|
132
|
-
memcpy(&len, data + offset, 4);
|
|
133
|
-
len = CFSwapInt32BigToHost(len);
|
|
134
|
-
offset += 4;
|
|
135
|
-
if (len == 0 || offset + len > total) break;
|
|
136
|
-
appendAnnexB(out, (const uint8_t *)(data + offset), len);
|
|
137
|
-
offset += len;
|
|
138
|
-
}
|
|
139
|
-
emit(out.bytes, out.length);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
static void encode(CVPixelBufferRef pixels) {
|
|
143
|
-
size_t w = CVPixelBufferGetWidth(pixels), h = CVPixelBufferGetHeight(pixels);
|
|
144
|
-
// A session only takes the size it was made for, and a display can change
|
|
145
|
-
// under us (a resolution switch, a monitor unplugged).
|
|
146
|
-
if (gSession && (w != gSourceWidth || h != gSourceHeight)) {
|
|
147
|
-
VTCompressionSessionCompleteFrames(gSession, kCMTimeInvalid);
|
|
148
|
-
VTCompressionSessionInvalidate(gSession);
|
|
149
|
-
CFRelease(gSession);
|
|
150
|
-
gSession = NULL;
|
|
151
|
-
}
|
|
152
|
-
if (!gSession) {
|
|
153
|
-
NSDictionary *props = @{ (id)kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder: @YES };
|
|
154
|
-
if (VTCompressionSessionCreate(NULL, (int32_t)w, (int32_t)h, kCMVideoCodecType_H264,
|
|
155
|
-
(__bridge CFDictionaryRef)props, NULL, NULL, onEncoded, NULL, &gSession) != noErr) {
|
|
156
|
-
say(@{ @"status": @"error", @"message": @"The video encoder could not start." });
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
|
|
160
|
-
// B-frames cost a frame of latency, the wrong trade for a remote control.
|
|
161
|
-
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_AllowFrameReordering, kCFBooleanFalse);
|
|
162
|
-
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_High_AutoLevel);
|
|
163
|
-
// A keyframe a second: nothing answers a viewer's request for one, so the
|
|
164
|
-
// interval is a late or lossy viewer's only way back to a picture.
|
|
165
|
-
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_MaxKeyFrameInterval, (__bridge CFTypeRef)@(gFPS));
|
|
166
|
-
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, (__bridge CFTypeRef)@1.0);
|
|
167
|
-
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_ExpectedFrameRate, (__bridge CFTypeRef)@(gFPS));
|
|
168
|
-
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_AverageBitRate, (__bridge CFTypeRef)@(gBitrate));
|
|
169
|
-
VTSessionSetProperty(gSession, kVTCompressionPropertyKey_DataRateLimits,
|
|
170
|
-
(__bridge CFArrayRef)@[@(gBitrate / 8 * 1.5), @1.0]);
|
|
171
|
-
gSourceWidth = w;
|
|
172
|
-
gSourceHeight = h;
|
|
173
|
-
say(@{ @"status": @"streaming", @"width": @(w), @"height": @(h) });
|
|
174
|
-
}
|
|
175
|
-
static int64_t n = 0;
|
|
176
|
-
VTCompressionSessionEncodeFrame(gSession, pixels, CMTimeMake(n++, gFPS), kCMTimeInvalid, NULL, NULL, NULL);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// ── Capture ──────────────────────────────────────────────────────────────
|
|
180
|
-
|
|
181
|
-
@interface Capture : NSObject <SCStreamOutput, SCStreamDelegate>
|
|
182
|
-
@property(nonatomic, strong) SCStream *stream;
|
|
183
|
-
@end
|
|
184
|
-
|
|
185
|
-
@implementation Capture
|
|
186
|
-
- (void)stream:(SCStream *)stream didOutputSampleBuffer:(CMSampleBufferRef)sample ofType:(SCStreamOutputType)type {
|
|
187
|
-
if (type != SCStreamOutputTypeScreen) return;
|
|
188
|
-
// A sample with no image is the stream saying "nothing changed".
|
|
189
|
-
CVPixelBufferRef pixels = CMSampleBufferGetImageBuffer(sample);
|
|
190
|
-
if (!pixels) return;
|
|
191
|
-
CVPixelBufferRetain(pixels);
|
|
192
|
-
os_unfair_lock_lock(&gLock);
|
|
193
|
-
CVPixelBufferRef old = gLatest;
|
|
194
|
-
gLatest = pixels;
|
|
195
|
-
os_unfair_lock_unlock(&gLock);
|
|
196
|
-
if (old) CVPixelBufferRelease(old);
|
|
197
|
-
}
|
|
198
|
-
- (void)stream:(SCStream *)stream didStopWithError:(NSError *)error {
|
|
199
|
-
say(@{ @"status": @"error", @"message": error.localizedDescription ?: @"Screen capture stopped." });
|
|
200
|
-
exit(1);
|
|
201
|
-
}
|
|
202
|
-
@end
|
|
203
|
-
|
|
204
|
-
static Capture *gCapture;
|
|
205
|
-
/** Held for the life of the process: a timer nothing references is released. */
|
|
206
|
-
static dispatch_source_t gTick;
|
|
207
|
-
|
|
208
|
-
static void startCapture(void) {
|
|
209
|
-
[SCShareableContent getShareableContentExcludingDesktopWindows:NO onScreenWindowsOnly:YES
|
|
210
|
-
completionHandler:^(SCShareableContent *content, NSError *error) {
|
|
211
|
-
if (error || content.displays.count == 0) {
|
|
212
|
-
// The one failure with a fix the owner can make: the switch is off.
|
|
213
|
-
say(permissions());
|
|
214
|
-
say(@{ @"status": @"error",
|
|
215
|
-
@"message": error.localizedDescription ?: @"This Mac has no display to show." });
|
|
216
|
-
exit(1);
|
|
217
|
-
}
|
|
218
|
-
SCDisplay *display = content.displays.firstObject;
|
|
219
|
-
for (SCDisplay *d in content.displays)
|
|
220
|
-
if (d.displayID == CGMainDisplayID()) display = d;
|
|
221
|
-
|
|
222
|
-
size_t w = CGDisplayPixelsWide(display.displayID), h = CGDisplayPixelsHigh(display.displayID);
|
|
223
|
-
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display.displayID);
|
|
224
|
-
if (mode) {
|
|
225
|
-
w = CGDisplayModeGetPixelWidth(mode);
|
|
226
|
-
h = CGDisplayModeGetPixelHeight(mode);
|
|
227
|
-
CGDisplayModeRelease(mode);
|
|
228
|
-
}
|
|
229
|
-
// Only ever scaled down, and by the capture itself rather than the encoder.
|
|
230
|
-
if (gWidth > 0 && w > (size_t)gWidth) {
|
|
231
|
-
h = h * gWidth / w;
|
|
232
|
-
w = gWidth;
|
|
233
|
-
}
|
|
234
|
-
// H.264 chroma is subsampled, so both axes must be even.
|
|
235
|
-
w -= w & 1;
|
|
236
|
-
h -= h & 1;
|
|
237
|
-
|
|
238
|
-
SCStreamConfiguration *config = [SCStreamConfiguration new];
|
|
239
|
-
config.width = w;
|
|
240
|
-
config.height = h;
|
|
241
|
-
config.minimumFrameInterval = CMTimeMake(1, gFPS);
|
|
242
|
-
config.pixelFormat = kCVPixelFormatType_32BGRA;
|
|
243
|
-
config.showsCursor = YES;
|
|
244
|
-
config.queueDepth = 3;
|
|
245
|
-
|
|
246
|
-
gCapture = [Capture new];
|
|
247
|
-
SCContentFilter *filter = [[SCContentFilter alloc] initWithDisplay:display excludingWindows:@[]];
|
|
248
|
-
gCapture.stream = [[SCStream alloc] initWithFilter:filter configuration:config delegate:gCapture];
|
|
249
|
-
NSError *addError = nil;
|
|
250
|
-
[gCapture.stream addStreamOutput:gCapture type:SCStreamOutputTypeScreen
|
|
251
|
-
sampleHandlerQueue:dispatch_queue_create("screen.capture", DISPATCH_QUEUE_SERIAL)
|
|
252
|
-
error:&addError];
|
|
253
|
-
[gCapture.stream startCaptureWithCompletionHandler:^(NSError *startError) {
|
|
254
|
-
if (!startError) return;
|
|
255
|
-
say(permissions());
|
|
256
|
-
say(@{ @"status": @"error", @"message": startError.localizedDescription ?: @"Screen capture did not start." });
|
|
257
|
-
exit(1);
|
|
258
|
-
}];
|
|
259
|
-
}];
|
|
260
|
-
|
|
261
|
-
// The stream only delivers a frame when the screen changes, and a still
|
|
262
|
-
// screen would then send no keyframe for a viewer who joins. So the newest
|
|
263
|
-
// frame is encoded on our own clock, changed or not; an unchanged frame
|
|
264
|
-
// costs a few hundred bytes.
|
|
265
|
-
dispatch_source_t tick = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
|
|
266
|
-
dispatch_queue_create("screen.encode", DISPATCH_QUEUE_SERIAL));
|
|
267
|
-
dispatch_source_set_timer(tick, dispatch_time(DISPATCH_TIME_NOW, 0),
|
|
268
|
-
(uint64_t)(NSEC_PER_SEC / gFPS), NSEC_PER_SEC / 100);
|
|
269
|
-
dispatch_source_set_event_handler(tick, ^{
|
|
270
|
-
os_unfair_lock_lock(&gLock);
|
|
271
|
-
CVPixelBufferRef pixels = gLatest ? CVPixelBufferRetain(gLatest) : NULL;
|
|
272
|
-
os_unfair_lock_unlock(&gLock);
|
|
273
|
-
if (!pixels) return;
|
|
274
|
-
encode(pixels);
|
|
275
|
-
CVPixelBufferRelease(pixels);
|
|
276
|
-
});
|
|
277
|
-
dispatch_resume(tick);
|
|
278
|
-
gTick = tick;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// ── Input ────────────────────────────────────────────────────────────────
|
|
282
|
-
|
|
283
|
-
// USB HID keyboard usage -> macOS virtual key code. The app sends usages (the
|
|
284
|
-
// same ones the simulator panel does), because a browser's key codes name
|
|
285
|
-
// physical keys and a usage is exactly that. 0xFF marks "no such key here".
|
|
286
|
-
static const uint8_t kNoKey = 0xFF;
|
|
287
|
-
static uint8_t keyCodeFor(int usage) {
|
|
288
|
-
static const uint8_t letters[26] = { 0, 11, 8, 2, 14, 3, 5, 4, 34, 38, 40, 37, 46,
|
|
289
|
-
45, 31, 35, 12, 15, 1, 17, 32, 9, 13, 7, 16, 6 };
|
|
290
|
-
static const uint8_t digits[10] = { 18, 19, 20, 21, 23, 22, 26, 28, 25, 29 };
|
|
291
|
-
static const uint8_t fkeys[12] = { 122, 120, 99, 118, 96, 97, 98, 100, 101, 109, 103, 111 };
|
|
292
|
-
if (usage >= 4 && usage <= 29) return letters[usage - 4];
|
|
293
|
-
if (usage >= 30 && usage <= 39) return digits[usage - 30];
|
|
294
|
-
if (usage >= 58 && usage <= 69) return fkeys[usage - 58];
|
|
295
|
-
switch (usage) {
|
|
296
|
-
case 40: return 36; // return
|
|
297
|
-
case 41: return 53; // escape
|
|
298
|
-
case 42: return 51; // delete (backspace)
|
|
299
|
-
case 43: return 48; // tab
|
|
300
|
-
case 44: return 49; // space
|
|
301
|
-
case 45: return 27; // -
|
|
302
|
-
case 46: return 24; // =
|
|
303
|
-
case 47: return 33; // [
|
|
304
|
-
case 48: return 30; // ]
|
|
305
|
-
case 49: return 42; // backslash
|
|
306
|
-
case 51: return 41; // ;
|
|
307
|
-
case 52: return 39; // '
|
|
308
|
-
case 53: return 50; // `
|
|
309
|
-
case 54: return 43; // ,
|
|
310
|
-
case 55: return 47; // .
|
|
311
|
-
case 56: return 44; // /
|
|
312
|
-
case 57: return 57; // caps lock
|
|
313
|
-
case 74: return 115; // home
|
|
314
|
-
case 75: return 116; // page up
|
|
315
|
-
case 76: return 117; // forward delete
|
|
316
|
-
case 77: return 119; // end
|
|
317
|
-
case 78: return 121; // page down
|
|
318
|
-
case 79: return 124; // right
|
|
319
|
-
case 80: return 123; // left
|
|
320
|
-
case 81: return 125; // down
|
|
321
|
-
case 82: return 126; // up
|
|
322
|
-
case 224: return 59; // left control
|
|
323
|
-
case 225: return 56; // left shift
|
|
324
|
-
case 226: return 58; // left option
|
|
325
|
-
case 227: return 55; // left command
|
|
326
|
-
case 228: return 62; // right control
|
|
327
|
-
case 229: return 60; // right shift
|
|
328
|
-
case 230: return 61; // right option
|
|
329
|
-
case 231: return 54; // right command
|
|
330
|
-
default: return kNoKey;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
static CGEventFlags flagFor(int usage) {
|
|
335
|
-
switch (usage) {
|
|
336
|
-
case 224: case 228: return kCGEventFlagMaskControl;
|
|
337
|
-
case 225: case 229: return kCGEventFlagMaskShift;
|
|
338
|
-
case 226: case 230: return kCGEventFlagMaskAlternate;
|
|
339
|
-
case 227: case 231: return kCGEventFlagMaskCommand;
|
|
340
|
-
default: return 0;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/** Modifiers held right now, by usage, so each event carries the flags a real
|
|
345
|
-
* keyboard's would and a chord is a chord on the far side. */
|
|
346
|
-
static CGEventFlags gFlags = 0;
|
|
347
|
-
static BOOL gLeftDown = NO, gRightDown = NO;
|
|
348
|
-
static CFAbsoluteTime gLastDown = 0;
|
|
349
|
-
static CGPoint gLastDownAt;
|
|
350
|
-
static int64_t gClicks = 1;
|
|
351
|
-
|
|
352
|
-
static void post(CGEventRef event) {
|
|
353
|
-
if (!event) return;
|
|
354
|
-
CGEventSetFlags(event, gFlags | CGEventGetFlags(event));
|
|
355
|
-
CGEventPost(kCGHIDEventTap, event);
|
|
356
|
-
CFRelease(event);
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
static double ratio(id value) {
|
|
360
|
-
double n = [value isKindOfClass:NSNumber.class] ? [value doubleValue] : 0;
|
|
361
|
-
if (!(n >= 0)) return 0;
|
|
362
|
-
return n > 1 ? 1 : n;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
static CGPoint pointFor(NSDictionary *cmd) {
|
|
366
|
-
CGRect bounds = CGDisplayBounds(CGMainDisplayID());
|
|
367
|
-
return CGPointMake(bounds.origin.x + ratio(cmd[@"x"]) * (bounds.size.width - 1),
|
|
368
|
-
bounds.origin.y + ratio(cmd[@"y"]) * (bounds.size.height - 1));
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
static void handlePointer(NSDictionary *cmd) {
|
|
372
|
-
NSString *phase = cmd[@"phase"];
|
|
373
|
-
BOOL right = [cmd[@"button"] isEqual:@"right"];
|
|
374
|
-
CGPoint at = pointFor(cmd);
|
|
375
|
-
CGMouseButton button = right ? kCGMouseButtonRight : kCGMouseButtonLeft;
|
|
376
|
-
CGEventType type;
|
|
377
|
-
if ([phase isEqual:@"down"]) {
|
|
378
|
-
type = right ? kCGEventRightMouseDown : kCGEventLeftMouseDown;
|
|
379
|
-
// A second press soon after, in the same place, is a double click, and
|
|
380
|
-
// macOS only treats it as one when the event says so.
|
|
381
|
-
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
|
|
382
|
-
BOOL near = fabs(at.x - gLastDownAt.x) < 4 && fabs(at.y - gLastDownAt.y) < 4;
|
|
383
|
-
gClicks = (!right && near && now - gLastDown < NSEvent.doubleClickInterval) ? gClicks + 1 : 1;
|
|
384
|
-
gLastDown = now;
|
|
385
|
-
gLastDownAt = at;
|
|
386
|
-
if (right) gRightDown = YES; else gLeftDown = YES;
|
|
387
|
-
} else if ([phase isEqual:@"up"]) {
|
|
388
|
-
type = right ? kCGEventRightMouseUp : kCGEventLeftMouseUp;
|
|
389
|
-
if (right) gRightDown = NO; else gLeftDown = NO;
|
|
390
|
-
} else if ([phase isEqual:@"move"]) {
|
|
391
|
-
if (gLeftDown) { type = kCGEventLeftMouseDragged; button = kCGMouseButtonLeft; }
|
|
392
|
-
else if (gRightDown) { type = kCGEventRightMouseDragged; button = kCGMouseButtonRight; }
|
|
393
|
-
else type = kCGEventMouseMoved;
|
|
394
|
-
} else {
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
CGEventRef event = CGEventCreateMouseEvent(NULL, type, at, button);
|
|
398
|
-
if (event && type != kCGEventMouseMoved) CGEventSetIntegerValueField(event, kCGMouseEventClickState, gClicks);
|
|
399
|
-
post(event);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
static void handleScroll(NSDictionary *cmd) {
|
|
403
|
-
id dx = cmd[@"dx"], dy = cmd[@"dy"];
|
|
404
|
-
int32_t x = [dx isKindOfClass:NSNumber.class] ? (int32_t)MAX(-2000, MIN(2000, [dx doubleValue])) : 0;
|
|
405
|
-
int32_t y = [dy isKindOfClass:NSNumber.class] ? (int32_t)MAX(-2000, MIN(2000, [dy doubleValue])) : 0;
|
|
406
|
-
// A browser's deltas grow downward and rightward; a scroll event's the other way.
|
|
407
|
-
post(CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, -y, -x));
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
static void handleKey(NSDictionary *cmd) {
|
|
411
|
-
id code = cmd[@"code"], down = cmd[@"down"];
|
|
412
|
-
if (![code isKindOfClass:NSNumber.class] || ![down isKindOfClass:NSNumber.class]) return;
|
|
413
|
-
int usage = [code intValue];
|
|
414
|
-
uint8_t key = keyCodeFor(usage);
|
|
415
|
-
if (key == kNoKey) return;
|
|
416
|
-
BOOL pressed = [down boolValue];
|
|
417
|
-
CGEventFlags flag = flagFor(usage);
|
|
418
|
-
if (flag) gFlags = pressed ? (gFlags | flag) : (gFlags & ~flag);
|
|
419
|
-
post(CGEventCreateKeyboardEvent(NULL, key, pressed));
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/** A paste: typed as the characters themselves, whatever the Mac's layout. */
|
|
423
|
-
static void handleText(NSDictionary *cmd) {
|
|
424
|
-
NSString *text = cmd[@"text"];
|
|
425
|
-
if (![text isKindOfClass:NSString.class]) return;
|
|
426
|
-
CGEventFlags held = gFlags;
|
|
427
|
-
gFlags = 0;
|
|
428
|
-
for (NSUInteger i = 0; i < text.length && i < 2000; i++) {
|
|
429
|
-
unichar ch = [text characterAtIndex:i];
|
|
430
|
-
for (int pressed = 1; pressed >= 0; pressed--) {
|
|
431
|
-
CGEventRef event = CGEventCreateKeyboardEvent(NULL, 0, pressed);
|
|
432
|
-
if (!event) continue;
|
|
433
|
-
CGEventKeyboardSetUnicodeString(event, 1, &ch);
|
|
434
|
-
CGEventPost(kCGHIDEventTap, event);
|
|
435
|
-
CFRelease(event);
|
|
436
|
-
}
|
|
437
|
-
usleep(2000);
|
|
438
|
-
}
|
|
439
|
-
gFlags = held;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
static void readCommands(void) {
|
|
443
|
-
char *line = NULL; size_t cap = 0; ssize_t len;
|
|
444
|
-
while ((len = getline(&line, &cap, stdin)) > 0) {
|
|
445
|
-
@autoreleasepool {
|
|
446
|
-
NSData *data = [NSData dataWithBytes:line length:(NSUInteger)len];
|
|
447
|
-
NSDictionary *cmd = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
|
|
448
|
-
if (![cmd isKindOfClass:NSDictionary.class]) continue;
|
|
449
|
-
// Without Accessibility every event below is dropped by the system with
|
|
450
|
-
// no error, so say so once rather than look broken.
|
|
451
|
-
static BOOL warned = NO;
|
|
452
|
-
if (!canControl()) {
|
|
453
|
-
if (!warned) { warned = YES; say(permissions()); }
|
|
454
|
-
continue;
|
|
455
|
-
}
|
|
456
|
-
NSString *type = cmd[@"type"];
|
|
457
|
-
if ([type isEqual:@"pointer"]) handlePointer(cmd);
|
|
458
|
-
else if ([type isEqual:@"scroll"]) handleScroll(cmd);
|
|
459
|
-
else if ([type isEqual:@"key"]) handleKey(cmd);
|
|
460
|
-
else if ([type isEqual:@"text"]) handleText(cmd);
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
// The runner closed our stdin: it is gone, or done with us.
|
|
464
|
-
exit(0);
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
int main(int argc, char *const argv[]) {
|
|
468
|
-
disclaimResponsibility(argv);
|
|
469
|
-
@autoreleasepool {
|
|
470
|
-
setvbuf(stderr, NULL, _IONBF, 0);
|
|
471
|
-
BOOL statusOnly = NO, request = NO;
|
|
472
|
-
for (int i = 1; i < argc; i++) {
|
|
473
|
-
if (!strcmp(argv[i], "--status")) statusOnly = YES;
|
|
474
|
-
else if (!strcmp(argv[i], "--request")) request = YES;
|
|
475
|
-
else if (!strcmp(argv[i], "--fps") && i + 1 < argc) gFPS = atoi(argv[++i]);
|
|
476
|
-
else if (!strcmp(argv[i], "--width") && i + 1 < argc) gWidth = atoi(argv[++i]);
|
|
477
|
-
else if (!strcmp(argv[i], "--bitrate") && i + 1 < argc) gBitrate = atoi(argv[++i]);
|
|
478
|
-
}
|
|
479
|
-
if (gFPS < 1) gFPS = 1;
|
|
480
|
-
if (gFPS > 60) gFPS = 60;
|
|
481
|
-
|
|
482
|
-
// Asking is what puts "Factiii Screen" in System Settings with a switch to
|
|
483
|
-
// turn on. macOS shows each prompt once; after that the owner uses the
|
|
484
|
-
// switch, and these calls return at once.
|
|
485
|
-
if (request) {
|
|
486
|
-
if (!canCapture()) CGRequestScreenCaptureAccess();
|
|
487
|
-
if (!canControl())
|
|
488
|
-
AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)@{ (__bridge id)kAXTrustedCheckOptionPrompt: @YES });
|
|
489
|
-
}
|
|
490
|
-
say(permissions());
|
|
491
|
-
if (statusOnly) return 0;
|
|
492
|
-
if (!canCapture()) return 3;
|
|
493
|
-
|
|
494
|
-
gWrite = dispatch_queue_create("screen.write", DISPATCH_QUEUE_SERIAL);
|
|
495
|
-
startCapture();
|
|
496
|
-
dispatch_async(dispatch_queue_create("screen.input", DISPATCH_QUEUE_SERIAL), ^{ readCommands(); });
|
|
497
|
-
[[NSRunLoop currentRunLoop] run];
|
|
498
|
-
}
|
|
499
|
-
return 0;
|
|
500
|
-
}
|
package/vendor/frpc-darwin-amd64
DELETED
|
Binary file
|
package/vendor/frpc-darwin-arm64
DELETED
|
Binary file
|
package/vendor/frpc-linux-amd64
DELETED
|
Binary file
|
package/vendor/frpc-linux-arm64
DELETED
|
Binary file
|