forked from asmagill/hs._asm.axuielement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer.m
548 lines (487 loc) · 26.2 KB
/
observer.m
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#import "common.h"
/// === hs._asm.axuielement.observer ===
///
/// This submodule allows you to create observers for accessibility elements and be notified when they trigger notifications. Not all notifications are supported by all elements and not all elements support notifications, so some trial and error will be necessary, but for compliant applications, this can allow your code to be notified when an application's user interface changes in some way.
///
/// This is very much a work in progress, so bugs and comments are welcome.
#define DEBUGGING_METHODS
static int refTable = LUA_NOREF ;
static CFMutableDictionaryRef observerDetails = NULL ;
static CFStringRef keySelfRefCount = CFSTR("selfRefCount") ;
static CFStringRef keyCallbackRef = CFSTR("callbackRef") ;
static CFStringRef keyIsRunning = CFSTR("isRunning") ;
static CFStringRef keyWatching = CFSTR("watching") ;
#pragma mark - Support Functions
int pushAXObserver(lua_State *L, AXObserverRef observer) {
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
if (!details) {
details = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) ;
CFDictionarySetValue(details, keySelfRefCount, (__bridge CFNumberRef)@(0)) ;
CFDictionarySetValue(details, keyCallbackRef, (__bridge CFNumberRef)@(LUA_NOREF)) ;
CFDictionarySetValue(details, keyIsRunning, kCFBooleanFalse) ;
CFDictionarySetValue(details, keyWatching, CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)) ;
CFDictionarySetValue(observerDetails, observer, details) ;
}
int selfRefCount = [(__bridge NSNumber *)CFDictionaryGetValue(details, keySelfRefCount) intValue] ;
selfRefCount++ ;
CFDictionarySetValue(details, keySelfRefCount, (__bridge CFNumberRef)@(selfRefCount)) ;
AXObserverRef* thePtr = lua_newuserdata(L, sizeof(AXObserverRef)) ;
*thePtr = CFRetain(observer) ;
luaL_getmetatable(L, OBSERVER_TAG) ;
lua_setmetatable(L, -2) ;
return 1 ;
}
// reduce duplication in meta_gc and userdata_gc
static void purgeWatchers(const void *key, const void *value, void *context) {
AXUIElementRef element = key ;
CFMutableArrayRef notifications = value ;
AXObserverRef observer = context ;
for (CFIndex i = 0 ; i < CFArrayGetCount(notifications) ; i++) {
CFStringRef what = CFArrayGetValueAtIndex(notifications, i) ;
AXObserverRemoveNotification(observer, element, what) ;
}
CFArrayRemoveAllValues(notifications) ;
}
static void cleanupAXObserver(AXObserverRef observer, CFMutableDictionaryRef details) {
LuaSkin *skin = [LuaSkin shared] ;
int callbackRef = [(__bridge NSNumber *)CFDictionaryGetValue(details, keyCallbackRef) intValue] ;
callbackRef = [skin luaUnref:refTable ref:callbackRef] ;
CFDictionarySetValue(details, keyCallbackRef, (__bridge CFNumberRef)@(callbackRef)) ;
Boolean isRunning = CFBooleanGetValue(CFDictionaryGetValue(details, keyIsRunning)) ;
if (isRunning) {
CFRunLoopRemoveSource(CFRunLoopGetMain(), AXObserverGetRunLoopSource(observer), kCFRunLoopCommonModes) ;
CFDictionarySetValue(details, keyIsRunning, kCFBooleanFalse) ;
}
CFMutableDictionaryRef watching = CFDictionaryGetValue(details, keyWatching) ;
CFDictionaryApplyFunction(watching, purgeWatchers, observer) ;
CFDictionaryRemoveAllValues(watching) ;
}
static void observerCallback(AXObserverRef observer, AXUIElementRef element, CFStringRef notification, CFDictionaryRef info, __unused void *refcon) {
LuaSkin *skin = [LuaSkin shared] ;
lua_State *L = skin.L ;
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
if (!details) {
[skin logWarn:[NSString stringWithFormat:@"%s:callback triggered for unregistered observer", OBSERVER_TAG]] ;
} else {
int callbackRef = [(__bridge NSNumber *)CFDictionaryGetValue(details, keyCallbackRef) intValue] ;
if (callbackRef != LUA_NOREF) {
[skin pushLuaRef:refTable ref:callbackRef] ;
pushAXObserver(L, observer) ;
pushAXUIElement(L, element) ;
[skin pushNSObject:(__bridge NSString *)notification] ;
if (info) {
pushCFTypeToLua(L, info, refTable) ;
} else {
lua_newtable(L) ;
}
if (![skin protectedCallAndTraceback:4 nresults:0]) {
[skin logError:[NSString stringWithFormat:@"%s:callback error:%s", OBSERVER_TAG, lua_tostring(L, -1)]] ;
lua_pop(L, 1) ;
}
}
}
}
#pragma mark - Module Functions
/// hs._asm.axuielement.observer.new(pid) -> observerObject
/// Constructor
/// Creates a new observer object for the application with the specified process ID.
///
/// Parameters:
/// * `pid` - the process ID of the application.
///
/// Returns:
/// * a new observerObject; generates an error if the pid does not exist or if the object cannot be created.
///
/// Notes:
/// * If you already have the `hs.application` object for an application, you can get its process ID with `hs.application:pid()`
/// * If you already have an `hs._asm.axuielement` from the application you wish to observe (it doesn't have to be the application axuielement object, just one belonging to the application), you can get the process ID with `hs._asm.axuielement:pid()`.
static int observer_new(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
[skin checkArgs:LS_TNUMBER | LS_TINTEGER, LS_TBREAK] ;
pid_t appPid = (pid_t)lua_tointeger(L, 1) ;
AXObserverRef observer = NULL ;
AXError err = AXObserverCreateWithInfoCallback(appPid, observerCallback, &observer) ;
if (err != kAXErrorSuccess) return luaL_error(L, AXErrorAsString(err)) ;
pushAXObserver(L, observer) ;
return 1 ;
}
#pragma mark - Module Methods
/// hs._asm.axuielement.observer:start() -> observerObject
/// Method
/// Start observing the application and trigger callbacks for the elements and notifications assigned.
///
/// Parameters:
/// * None
///
/// Returns:
/// * the observerObject
///
/// Notes:
/// * This method does nothing if the observer is already running
static int observer_start(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
[skin checkArgs:LS_TUSERDATA, OBSERVER_TAG, LS_TBREAK] ;
AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
Boolean isRunning = CFBooleanGetValue(CFDictionaryGetValue(details, keyIsRunning)) ;
if (!isRunning) {
CFRunLoopAddSource(CFRunLoopGetMain(), AXObserverGetRunLoopSource(observer), kCFRunLoopCommonModes) ;
CFDictionarySetValue(details, keyIsRunning, kCFBooleanTrue) ;
}
lua_pushvalue(L, 1) ;
return 1 ;
}
/// hs._asm.axuielement.observer:stop() -> observerObject
/// Method
/// Stop observing the application; no further callbacks will be generated.
///
/// Parameters:
/// * None
///
/// Returns:
/// * the observerObject
///
/// Notes:
/// * This method does nothing if the observer is not currently running
static int observer_stop(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
[skin checkArgs:LS_TUSERDATA, OBSERVER_TAG, LS_TBREAK] ;
AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
Boolean isRunning = CFBooleanGetValue(CFDictionaryGetValue(details, keyIsRunning)) ;
if (isRunning) {
CFRunLoopRemoveSource(CFRunLoopGetMain(), AXObserverGetRunLoopSource(observer), kCFRunLoopCommonModes) ;
CFDictionarySetValue(details, keyIsRunning, kCFBooleanFalse) ;
}
lua_pushvalue(L, 1) ;
return 1 ;
}
/// hs._asm.axuielement.observer:isRunning() -> boolean
/// Method
/// Returns true or false indicating whether the observer is currently watching for notifications and generating callbacks.
///
/// Parameters:
/// * None
///
/// Returns:
/// * a boolean value indicating whether or not the observer is currently active.
static int observer_isRunning(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
[skin checkArgs:LS_TUSERDATA, OBSERVER_TAG, LS_TBREAK] ;
AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
lua_pushboolean(L, CFBooleanGetValue(CFDictionaryGetValue(details, keyIsRunning))) ;
return 1 ;
}
/// hs._asm.axuielement.observer:callback([fn | nil]) -> observerObject | fn | nil
/// Method
/// Get or set the callback for the observer.
///
/// Parameters:
/// * `fn` - a function, or an explicit nil to remove, specifying the callback to the observer will invoke when the assigned elements generate notifications.
///
/// Returns:
/// * If an argument is provided, the observerObject; otherwise the current value.
///
/// Notes:
/// * the callback should expect 4 arguments and return none. The arguments passed to the callback will be as follows:
/// * the observerObject itself
/// * the `hs._asm.axuielement` object for the accessibility element which generated the notification
/// * a string specifying the specific notification which was received
/// * a table containing key-value pairs with more information about the notification, if the element and notification type provide it. Commonly this will be an empty table indicating that no additional detail was provided.
static int observer_callback(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
[skin checkArgs:LS_TUSERDATA, OBSERVER_TAG, LS_TFUNCTION | LS_TNIL | LS_TOPTIONAL, LS_TBREAK] ;
AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
int callbackRef = [(__bridge NSNumber *)CFDictionaryGetValue(details, keyCallbackRef) intValue] ;
if (lua_gettop(L) == 2) {
callbackRef = [skin luaUnref:refTable ref:callbackRef] ;
CFDictionarySetValue(details, keyCallbackRef, (__bridge CFNumberRef)@(callbackRef)) ;
if (lua_type(L, 2) != LUA_TNIL) {
lua_pushvalue(L, 2) ;
callbackRef = [skin luaRef:refTable] ;
CFDictionarySetValue(details, keyCallbackRef, (__bridge CFNumberRef)@(callbackRef)) ;
lua_pushvalue(L, 1) ;
}
} else {
if (callbackRef != LUA_NOREF) {
[skin pushLuaRef:refTable ref:callbackRef] ;
} else {
lua_pushnil(L) ;
}
}
return 1 ;
}
/// hs._asm.axuielement.observer:addWatcher(element, notification) -> observerObject
/// Method
/// Registers the specified notification for the specified accesibility element with the observer.
///
/// Parameters:
/// * `element` - the `hs._asm.axuielement` representing an accessibility element of the application the observer was created for.
/// * `notification` - a string specifying the notification.
///
/// Returns:
/// * the observerObject; generates an error if watcher cannot be registered
///
/// Notes:
/// * multiple notifications for the same accessibility element can be registered by invoking this method multiple times with the same element but different notification strings.
/// * if the specified element and notification string are already registered, this method does nothing.
/// * the notification string is application dependent and can be any string that the application developers choose; some common ones are found in `hs._asm.axuielement.observer.notifications`, but the list is not exhaustive nor is an application or element required to provide them.
static int observer_addWatchedElement(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
[skin checkArgs:LS_TUSERDATA, OBSERVER_TAG,
LS_TUSERDATA, USERDATA_TAG,
LS_TSTRING,
LS_TBREAK] ;
AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
AXUIElementRef element = get_axuielementref(L, 2, USERDATA_TAG) ;
NSString *what = [skin toNSObjectAtIndex:3] ;
CFMutableDictionaryRef watching = CFDictionaryGetValue(details, keyWatching) ;
CFMutableArrayRef notifications = CFDictionaryGetValue(watching, element) ;
Boolean exists = false ;
if (notifications) {
exists = CFArrayContainsValue(notifications, CFRangeMake(0, CFArrayGetCount(notifications)), (__bridge CFStringRef)what) ;
} else {
notifications = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks) ;
CFDictionarySetValue(watching, element, notifications) ;
}
if (!exists) {
AXError err = AXObserverAddNotification(observer, element, (__bridge CFStringRef)what, NULL) ;
if (err != kAXErrorSuccess) return luaL_error(L, AXErrorAsString(err)) ;
CFArrayAppendValue(notifications, (__bridge CFStringRef)what) ;
}
lua_pushvalue(L, 1) ;
return 1 ;
}
/// hs._asm.axuielement.observer:removeWatcher(element, notification) -> observerObject
/// Method
/// Unregisters the specified notification for the specified accessibility element from the observer.
///
/// Parameters:
/// * `element` - the `hs._asm.axuielement` representing an accessibility element of the application the observer was created for.
/// * `notification` - a string specifying the notification.
///
/// Returns:
/// * the observerObject; generates an error if watcher cannot be unregistered
///
/// Notes:
/// * if the specified element and notification string are not currently registered with the observer, this method does nothing.
static int observer_removeWatchedElement(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
[skin checkArgs:LS_TUSERDATA, OBSERVER_TAG,
LS_TUSERDATA, USERDATA_TAG,
LS_TSTRING,
LS_TBREAK] ;
AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
AXUIElementRef element = get_axuielementref(L, 2, USERDATA_TAG) ;
NSString *what = [skin toNSObjectAtIndex:3] ;
CFMutableDictionaryRef watching = CFDictionaryGetValue(details, keyWatching) ;
CFMutableArrayRef notifications = CFDictionaryGetValue(watching, element) ;
CFIndex exists = -1 ;
if (notifications) {
exists = CFArrayGetFirstIndexOfValue(notifications, CFRangeMake(0, CFArrayGetCount(notifications)), (__bridge CFStringRef)what) ;
} else {
notifications = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks) ;
CFDictionarySetValue(watching, element, notifications) ;
}
if (exists > -1) {
AXError err = AXObserverRemoveNotification(observer, element, (__bridge CFStringRef)what) ;
if (err != kAXErrorSuccess) return luaL_error(L, AXErrorAsString(err)) ;
CFArrayRemoveValueAtIndex(notifications, exists) ;
}
lua_pushvalue(L, 1) ;
return 1 ;
}
/// hs._asm.axuielement.observer:watching([element]) -> table
/// Method
/// Returns a table of the notifications currently registered with the observer.
///
/// Parameters:
/// * `element` - an optional `hs._asm.axuielement` to return a list of registered notifications for.
///
/// Returns:
/// * a table containing the currently registered notifications
///
/// Notes:
/// * If an element is specified, then the table returned will contain a list of strings specifying the specific notifications that the observer is watching that element for.
/// * If no argument is specified, then the table will contain key-value pairs in which each key will be an `hs._asm.axuielement` that is being observed and the corresponding value will be a table containing a list of strings specifying the specific notifications that the observer is watching for from from that element.
static int observer_watchedElements(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
[skin checkArgs:LS_TUSERDATA, OBSERVER_TAG, LS_TBREAK | LS_TVARARG] ;
AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
AXUIElementRef element = NULL ;
if (lua_gettop(L) > 1) {
[skin checkArgs:LS_TUSERDATA, OBSERVER_TAG, LS_TUSERDATA, USERDATA_TAG, LS_TBREAK] ;
element = get_axuielementref(L, 2, USERDATA_TAG) ;
}
CFMutableDictionaryRef watching = CFDictionaryGetValue(details, keyWatching) ;
if (element) {
CFMutableArrayRef notifications = CFDictionaryGetValue(watching, element) ;
if (notifications) {
pushCFTypeToLua(L, notifications, refTable) ;
} else {
lua_newtable(L) ;
}
} else {
pushCFTypeToLua(L, watching, refTable) ;
}
return 1 ;
}
#ifdef DEBUGGING_METHODS
static int observer_internalDetails(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
CFMutableDictionaryRef details = observerDetails ;
if (lua_gettop(L) > 0) {
[skin checkArgs:LS_TUSERDATA, OBSERVER_TAG, LS_TBREAK] ;
AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
details = CFDictionaryGetValue(observerDetails, observer) ;
}
pushCFTypeToLua(L, details, refTable) ;
return 1 ;
}
#endif
#pragma mark - Module Constants
/// hs._asm.axuielement.observer.notifications[]
/// Constant
/// A table of common accessibility object notification names, provided for reference.
///
/// Notes:
/// * Notifications are application dependent and can be any string that the application developers choose; this list provides the suggested notification names found within the macOS Framework headers, but the list is not exhaustive nor is an application or element required to provide them.
static int pushNotificationsTable(lua_State *L) {
LuaSkin *skin = [LuaSkin shared] ;
lua_newtable(L) ;
// Focus notifications
[skin pushNSObject:(__bridge NSString *)kAXMainWindowChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXFocusedWindowChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXFocusedUIElementChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
// Application notifications
[skin pushNSObject:(__bridge NSString *)kAXApplicationActivatedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXApplicationDeactivatedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXApplicationHiddenNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXApplicationShownNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
// Window notifications
[skin pushNSObject:(__bridge NSString *)kAXWindowCreatedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXWindowMovedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXWindowResizedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXWindowMiniaturizedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXWindowDeminiaturizedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
// New drawer, sheet, and help tag notifications
[skin pushNSObject:(__bridge NSString *)kAXDrawerCreatedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXSheetCreatedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXHelpTagCreatedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
// Element notifications
[skin pushNSObject:(__bridge NSString *)kAXValueChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXUIElementDestroyedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXElementBusyChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
// Menu notifications
[skin pushNSObject:(__bridge NSString *)kAXMenuOpenedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXMenuClosedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXMenuItemSelectedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
// Table and outline view notifications
[skin pushNSObject:(__bridge NSString *)kAXRowCountChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXRowCollapsedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXRowExpandedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
// Miscellaneous notifications
[skin pushNSObject:(__bridge NSString *)kAXSelectedChildrenChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXResizedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXMovedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXCreatedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXAnnouncementRequestedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXLayoutChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXSelectedCellsChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXSelectedChildrenMovedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXSelectedColumnsChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXSelectedRowsChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXSelectedTextChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXTitleChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
[skin pushNSObject:(__bridge NSString *)kAXUnitsChangedNotification] ; lua_rawseti(L, -2, luaL_len(L, -2) + 1) ;
return 1 ;
}
#pragma mark - Hammerspoon/Lua Infrastructure
static int userdata_tostring(lua_State* L) {
LuaSkin *skin = [LuaSkin shared] ;
// AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
[skin pushNSObject:[NSString stringWithFormat:@"%s: (%p)", OBSERVER_TAG, lua_topointer(L, 1)]] ;
return 1 ;
}
static int userdata_gc(lua_State* L) {
LuaSkin *skin = [LuaSkin shared] ;
AXObserverRef observer = get_axobserverref(L, 1, OBSERVER_TAG) ;
CFMutableDictionaryRef details = CFDictionaryGetValue(observerDetails, observer) ;
if (!details) {
[skin logWarn:[NSString stringWithFormat:@"%s:__gc triggered for unregistered observer", OBSERVER_TAG]] ;
} else {
int selfRefCount = [(__bridge NSNumber *)CFDictionaryGetValue(details, keySelfRefCount) intValue] ;
selfRefCount-- ;
CFDictionarySetValue(details, keySelfRefCount, (__bridge CFNumberRef)@(selfRefCount)) ;
if (selfRefCount == 0) {
cleanupAXObserver(observer, details) ;
CFDictionaryRemoveValue(observerDetails, observer) ;
CFRelease(observer) ;
}
}
lua_pushnil(L) ;
lua_setmetatable(L, 1) ;
return 0 ;
}
static int userdata_eq(lua_State* L) {
AXObserverRef observer1 = get_axobserverref(L, 1, OBSERVER_TAG) ;
AXObserverRef observer2 = get_axobserverref(L, 2, OBSERVER_TAG) ;
lua_pushboolean(L, CFEqual(observer1, observer2)) ;
return 1 ;
}
static void purgeObserver(const void *key, const void *value, __unused void *context) {
AXObserverRef observer = key ;
CFMutableDictionaryRef details = value ;
cleanupAXObserver(observer, details) ;
}
static int meta_gc(lua_State* __unused L) {
CFDictionaryApplyFunction(observerDetails, purgeObserver, NULL) ;
CFDictionaryRemoveAllValues(observerDetails) ;
observerDetails = NULL ;
return 0 ;
}
// Metatable for userdata objects
static const luaL_Reg userdata_metaLib[] = {
{"start", observer_start},
{"stop", observer_stop},
{"isRunning", observer_isRunning},
{"callback", observer_callback},
{"addWatcher", observer_addWatchedElement},
{"removeWatcher", observer_removeWatchedElement},
{"watching", observer_watchedElements},
#ifdef DEBUGGING_METHODS
{"_internals", observer_internalDetails},
#endif
{"__tostring", userdata_tostring},
{"__eq", userdata_eq},
{"__gc", userdata_gc},
{NULL, NULL}
} ;
// Functions for returned object when module loads
static luaL_Reg moduleLib[] = {
{"new", observer_new},
#ifdef DEBUGGING_METHODS
{"_internals", observer_internalDetails},
#endif
{NULL, NULL}
} ;
// Metatable for module, if needed
static const luaL_Reg module_metaLib[] = {
{"__gc", meta_gc},
{NULL, NULL}
} ;
int luaopen_hs__asm_axuielement_observer(lua_State* L) {
LuaSkin *skin = [LuaSkin shared] ;
refTable = [skin registerLibraryWithObject:OBSERVER_TAG
functions:moduleLib
metaFunctions:module_metaLib
objectFunctions:userdata_metaLib] ;
observerDetails = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) ;
pushNotificationsTable(L) ; lua_setfield(L, -2, "notifications") ;
return 1 ;
}