@windowkit/appkit 0.31.0 → 0.33.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.
@@ -11,6 +11,11 @@
11
11
  // thread-safe function
12
12
  // screen-recording CGPreflightScreenCaptureAccess /
13
13
  // CGRequestScreenCaptureAccess
14
+ // system-audio-recording
15
+ // TCC's own TCCAccessPreflight / TCCAccessRequest for
16
+ // kTCCServiceAudioCapture (macOS 14.2: a tap of other
17
+ // processes' sound, systemaudio.mm), which has no
18
+ // public call to read or to ask — found at run time
14
19
  // accessibility AXIsProcessTrusted / AXIsProcessTrustedWithOptions
15
20
  // with the prompt option
16
21
  // input-monitoring IOHIDCheckAccess / IOHIDRequestAccess (listen)
@@ -31,7 +36,9 @@
31
36
  // Every status that crosses to JS is one of 'authorized' | 'denied' |
32
37
  // 'restricted' | 'notDetermined' — plus 'writeOnly', which only calendars
33
38
  // and reminders can answer: macOS 14's partial grant (the app may save items
34
- // it cannot read back). It crosses as its own word rather than as either
39
+ // it cannot read back), and 'unknown', which only system-audio-recording
40
+ // answers, where there is nothing to read it with: before macOS 14.2, or a
41
+ // TCC without the calls. It crosses as its own word rather than as either
35
42
  // side, because whether it counts as granted depends on what the consumer
36
43
  // wanted to do — a writer has what it needs, a reader does not — and that is
37
44
  // the renderer's decision. Screen recording and accessibility only ever
@@ -45,6 +52,7 @@
45
52
  // inside the requesting call — and a pending request holds the event loop
46
53
  // open like pending I/O until the user has answered.
47
54
 
55
+ #include <dlfcn.h>
48
56
  #include <napi.h>
49
57
  #import <Cocoa/Cocoa.h>
50
58
  #import <AVFoundation/AVFoundation.h>
@@ -68,11 +76,13 @@ static const char* const kDenied = "denied";
68
76
  static const char* const kRestricted = "restricted";
69
77
  static const char* const kNotDetermined = "notDetermined";
70
78
  static const char* const kWriteOnly = "writeOnly";
79
+ static const char* const kUnknown = "unknown";
71
80
 
72
81
  enum class Kind {
73
82
  Camera,
74
83
  Microphone,
75
84
  ScreenRecording,
85
+ SystemAudioRecording,
76
86
  Accessibility,
77
87
  InputMonitoring,
78
88
  Automation,
@@ -82,13 +92,15 @@ enum class Kind {
82
92
  };
83
93
 
84
94
  static const char* const kKindNames =
85
- "camera, microphone, screen-recording, accessibility, input-monitoring, "
86
- "automation, location, calendars, reminders";
95
+ "camera, microphone, screen-recording, system-audio-recording, "
96
+ "accessibility, input-monitoring, automation, location, calendars, "
97
+ "reminders";
87
98
 
88
99
  static bool ParseKind(const std::string& s, Kind* out) {
89
100
  if (s == "camera") *out = Kind::Camera;
90
101
  else if (s == "microphone") *out = Kind::Microphone;
91
102
  else if (s == "screen-recording") *out = Kind::ScreenRecording;
103
+ else if (s == "system-audio-recording") *out = Kind::SystemAudioRecording;
92
104
  else if (s == "accessibility") *out = Kind::Accessibility;
93
105
  else if (s == "input-monitoring") *out = Kind::InputMonitoring;
94
106
  else if (s == "automation") *out = Kind::Automation;
@@ -254,6 +266,48 @@ EKEventStore* CALEventStore() {
254
266
  // mapping stays here, in the one place that knows it.
255
267
  const char* CALEventsAuthorizationStatus() { return EKStatus(EKEntityTypeEvent); }
256
268
 
269
+ // System Audio Recording has no public call to read it or to ask for it: the
270
+ // first read of a tap raises the prompt, and a refusal is not an error but a
271
+ // tap that delivers silence — a recording of nothing, for as long as anyone
272
+ // lets it run. So it is read and asked through TCC's own TCCAccessPreflight
273
+ // and TCCAccessRequest, private, the pair every recorder of other apps' sound
274
+ // uses, found at run time: nothing links against them, and where they are not
275
+ // there the status is 'unknown' and the tap's own prompt is the only one.
276
+ // Preflight answers 0 for a grant, 1 for a refusal and 2 for not asked yet.
277
+ // Exported for systemaudio.mm, which reads it before it makes a tap.
278
+ static void* TCCSymbol(const char* name) {
279
+ static void* handle =
280
+ dlopen("/System/Library/PrivateFrameworks/TCC.framework/Versions/A/TCC", RTLD_LAZY | RTLD_LOCAL);
281
+ return handle ? dlsym(handle, name) : nullptr;
282
+ }
283
+
284
+ static const CFStringRef kAudioCaptureService = CFSTR("kTCCServiceAudioCapture");
285
+
286
+ const char* CALAudioCaptureStatus() {
287
+ if (@available(macOS 14.2, *)) {
288
+ auto preflight = (int (*)(CFStringRef, CFDictionaryRef))TCCSymbol("TCCAccessPreflight");
289
+ if (!preflight) return kUnknown;
290
+ switch (preflight(kAudioCaptureService, nullptr)) {
291
+ case 0: return kAuthorized;
292
+ case 1: return kDenied;
293
+ case 2: return kNotDetermined;
294
+ default: return kUnknown;
295
+ }
296
+ }
297
+ return kUnknown;
298
+ }
299
+
300
+ // The prompt where nobody has been asked, then `done` on any thread; at once
301
+ // where the status is decided or cannot be read.
302
+ void CALAudioCaptureRequest(void (^done)(void)) {
303
+ auto request = (void (*)(CFStringRef, CFDictionaryRef, void (^)(Boolean)))TCCSymbol("TCCAccessRequest");
304
+ if (!request || CALAudioCaptureStatus() != kNotDetermined) {
305
+ done();
306
+ return;
307
+ }
308
+ request(kAudioCaptureService, nullptr, ^(Boolean) { done(); });
309
+ }
310
+
257
311
  // TCC's answer for sending Apple Events to `bundleId`. With `ask`, the call
258
312
  // blocks while the consent dialog is up. procNotFound means the target is
259
313
  // not running — TCC only answers for a running target, so the caller turns
@@ -379,6 +433,7 @@ static Napi::Value AuthorizationStatus(const Napi::CallbackInfo& info) {
379
433
  case Kind::ScreenRecording:
380
434
  s = CGPreflightScreenCaptureAccess() ? kAuthorized : kDenied;
381
435
  break;
436
+ case Kind::SystemAudioRecording: s = CALAudioCaptureStatus(); break;
382
437
  case Kind::Accessibility:
383
438
  s = AXIsProcessTrusted() ? kAuthorized : kDenied;
384
439
  break;
@@ -436,6 +491,11 @@ static Napi::Value RequestAuthorization(const Napi::CallbackInfo& info) {
436
491
  // returns the current grant either way
437
492
  Answer(tsfn, CGRequestScreenCaptureAccess() ? kAuthorized : kDenied);
438
493
  break;
494
+ case Kind::SystemAudioRecording: {
495
+ // the prompt where nobody was asked, the status after it either way
496
+ CALAudioCaptureRequest(^{ Answer(tsfn, CALAudioCaptureStatus()); });
497
+ break;
498
+ }
439
499
  case Kind::Accessibility: {
440
500
  NSDictionary* opts =
441
501
  @{(__bridge NSString*)kAXTrustedCheckOptionPrompt : @YES};
@@ -519,6 +579,7 @@ static Napi::Value OpenPrivacySettings(const Napi::CallbackInfo& info) {
519
579
  else if (kind == "camera") anchor = "Privacy_Camera";
520
580
  else if (kind == "microphone") anchor = "Privacy_Microphone";
521
581
  else if (kind == "screen-recording") anchor = "Privacy_ScreenCapture";
582
+ else if (kind == "system-audio-recording") anchor = "Privacy_AudioCapture";
522
583
  else if (kind == "accessibility") anchor = "Privacy_Accessibility";
523
584
  else if (kind == "input-monitoring") anchor = "Privacy_ListenEvent";
524
585
  else if (kind == "automation") anchor = "Privacy_Automation";