Skip to content

Commit 59b9226

Browse files
committed
Optimize CLI integration controls
1 parent 98a5c77 commit 59b9226

11 files changed

Lines changed: 635 additions & 415 deletions

File tree

cli/XCWAccessibilityBridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ NS_ASSUME_NONNULL_BEGIN
66

77
+ (nullable NSDictionary *)accessibilitySnapshotForSimulatorUDID:(NSString *)udid
88
atPoint:(nullable NSValue *)pointValue
9+
maxDepth:(NSUInteger)maxDepth
910
error:(NSError * _Nullable * _Nullable)error;
1011

1112
@end

cli/XCWAccessibilityBridge.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ static pid_t XCWAXElementPID(id element) {
301301
return values;
302302
}
303303

304-
static NSMutableDictionary *XCWAXSerializeElement(id element, NSString *token, NSHashTable *visited, NSUInteger depth) {
305-
if (element == nil || depth > XCWAXMaxDepth || [visited containsObject:element]) {
304+
static NSMutableDictionary *XCWAXSerializeElement(id element, NSString *token, NSHashTable *visited, NSUInteger depth, NSUInteger maxDepth) {
305+
if (element == nil || depth > maxDepth || [visited containsObject:element]) {
306306
return nil;
307307
}
308308
[visited addObject:element];
@@ -314,10 +314,10 @@ static pid_t XCWAXElementPID(id element) {
314314

315315
NSMutableDictionary *values = [XCWAXDictionaryForElement(element) mutableCopy];
316316
NSMutableArray *childrenValues = [NSMutableArray array];
317-
id children = XCWAXObject(element, "accessibilityChildren");
317+
id children = depth < maxDepth ? XCWAXObject(element, "accessibilityChildren") : nil;
318318
if ([children isKindOfClass:NSArray.class]) {
319319
for (id child in (NSArray *)children) {
320-
NSMutableDictionary *childValues = XCWAXSerializeElement(child, token, visited, depth + 1);
320+
NSMutableDictionary *childValues = XCWAXSerializeElement(child, token, visited, depth + 1, maxDepth);
321321
if (childValues != nil) {
322322
[childrenValues addObject:childValues];
323323
}
@@ -331,6 +331,7 @@ @implementation XCWAccessibilityBridge
331331

332332
+ (nullable NSDictionary *)accessibilitySnapshotForSimulatorUDID:(NSString *)udid
333333
atPoint:(nullable NSValue *)pointValue
334+
maxDepth:(NSUInteger)maxDepth
334335
error:(NSError * _Nullable __autoreleasing *)error {
335336
if (![self.class loadAndValidate:error]) {
336337
return nil;
@@ -413,7 +414,7 @@ + (nullable NSDictionary *)accessibilitySnapshotForSimulatorUDID:(NSString *)udi
413414
}
414415

415416
NSHashTable *visited = [NSHashTable hashTableWithOptions:NSPointerFunctionsObjectPointerPersonality];
416-
NSMutableDictionary *root = XCWAXSerializeElement(element, token, visited, 0);
417+
NSMutableDictionary *root = XCWAXSerializeElement(element, token, visited, 0, MIN(maxDepth, XCWAXMaxDepth));
417418
NSArray *roots = root != nil ? @[root] : @[];
418419
return @{
419420
@"roots": roots,

cli/XCWSimctl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
1616
- (BOOL)uninstallBundleID:(NSString *)bundleID simulatorUDID:(NSString *)udid error:(NSError * _Nullable * _Nullable)error;
1717
- (BOOL)setPasteboardText:(NSString *)text simulatorUDID:(NSString *)udid error:(NSError * _Nullable * _Nullable)error;
1818
- (nullable NSString *)pasteboardTextForSimulatorUDID:(NSString *)udid error:(NSError * _Nullable * _Nullable)error;
19-
- (nullable NSArray<NSDictionary *> *)recentLogEntriesForSimulatorUDID:(NSString *)udid seconds:(NSTimeInterval)seconds error:(NSError * _Nullable * _Nullable)error;
19+
- (nullable NSArray<NSDictionary *> *)recentLogEntriesForSimulatorUDID:(NSString *)udid seconds:(NSTimeInterval)seconds limit:(NSUInteger)limit error:(NSError * _Nullable * _Nullable)error;
2020
- (nullable NSDictionary *)simulatorWithUDID:(NSString *)udid error:(NSError * _Nullable * _Nullable)error;
2121

2222
@end

cli/XCWSimctl.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ - (nullable NSString *)pasteboardTextForSimulatorUDID:(NSString *)udid error:(NS
380380

381381
- (nullable NSArray<NSDictionary *> *)recentLogEntriesForSimulatorUDID:(NSString *)udid
382382
seconds:(NSTimeInterval)seconds
383+
limit:(NSUInteger)limit
383384
error:(NSError * _Nullable __autoreleasing *)error {
384385
NSUInteger boundedSeconds = MIN(MAX((NSUInteger)ceil(seconds), 1), 1800);
385386
XCWProcessResult *result = [self.class runSimctl:@[
@@ -404,7 +405,8 @@ - (nullable NSString *)pasteboardTextForSimulatorUDID:(NSString *)udid error:(NS
404405
return nil;
405406
}
406407

407-
NSMutableArray<NSDictionary *> *entries = [NSMutableArray array];
408+
NSUInteger boundedLimit = limit == 0 ? NSUIntegerMax : limit;
409+
NSMutableArray<NSDictionary *> *entries = [NSMutableArray arrayWithCapacity:MIN(boundedLimit, 256)];
408410
NSArray<NSString *> *lines = [result.stdoutString componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet];
409411
for (NSString *line in lines) {
410412
NSString *trimmed = [line stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
@@ -432,6 +434,9 @@ - (nullable NSString *)pasteboardTextForSimulatorUDID:(NSString *)udid error:(NS
432434
@"category": payload[@"category"] ?: @"",
433435
@"message": payload[@"eventMessage"] ?: payload[@"formatString"] ?: @"",
434436
}];
437+
if (entries.count > boundedLimit) {
438+
[entries removeObjectAtIndex:0];
439+
}
435440
}
436441

437442
return entries;

cli/native/XCWNativeBridge.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ char * _Nullable xcw_native_get_chrome_profile(const char * _Nonnull udid, char
4343
xcw_native_owned_bytes xcw_native_render_chrome_png(const char * _Nonnull udid, char * _Nullable * _Nullable error_message);
4444
xcw_native_owned_bytes xcw_native_screenshot_png(const char * _Nonnull udid, char * _Nullable * _Nullable error_message);
4545
char * _Nullable xcw_native_recent_logs(const char * _Nonnull udid, double seconds, size_t limit, char * _Nullable * _Nullable error_message);
46-
char * _Nullable xcw_native_accessibility_snapshot(const char * _Nonnull udid, bool has_point, double x, double y, char * _Nullable * _Nullable error_message);
46+
char * _Nullable xcw_native_accessibility_snapshot(const char * _Nonnull udid, bool has_point, double x, double y, size_t max_depth, char * _Nullable * _Nullable error_message);
4747
bool xcw_native_send_touch(const char * _Nonnull udid, double x, double y, const char * _Nonnull phase, char * _Nullable * _Nullable error_message);
4848
bool xcw_native_send_key(const char * _Nonnull udid, uint16_t key_code, uint32_t modifiers, char * _Nullable * _Nullable error_message);
4949
bool xcw_native_send_key_event(const char * _Nonnull udid, uint16_t key_code, bool down, char * _Nullable * _Nullable error_message);
@@ -62,6 +62,8 @@ void xcw_native_input_destroy(void * _Nullable handle);
6262
bool xcw_native_input_display_size(void * _Nonnull handle, double * _Nullable width, double * _Nullable height);
6363
bool xcw_native_input_send_touch(void * _Nonnull handle, double x, double y, const char * _Nonnull phase, char * _Nullable * _Nullable error_message);
6464
bool xcw_native_input_send_multitouch(void * _Nonnull handle, double x1, double y1, double x2, double y2, const char * _Nonnull phase, char * _Nullable * _Nullable error_message);
65+
bool xcw_native_input_send_key(void * _Nonnull handle, uint16_t key_code, uint32_t modifiers, char * _Nullable * _Nullable error_message);
66+
bool xcw_native_input_send_key_event(void * _Nonnull handle, uint16_t key_code, bool down, char * _Nullable * _Nullable error_message);
6567

6668
void * _Nullable xcw_native_session_create(const char * _Nonnull udid, char * _Nullable * _Nullable error_message);
6769
void xcw_native_session_destroy(void * _Nullable handle);

cli/native/XCWNativeBridge.m

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ xcw_native_owned_bytes xcw_native_screenshot_png(const char *udid, char **error_
216216

217217
char *xcw_native_recent_logs(const char *udid, double seconds, size_t limit, char **error_message) {
218218
@autoreleasepool {
219-
(void)limit;
220219
XCWSimctl *simctl = [[XCWSimctl alloc] init];
221220
NSError *error = nil;
222221
NSArray<NSDictionary *> *entries = [simctl recentLogEntriesForSimulatorUDID:XCWStringFromCString(udid)
223222
seconds:seconds
223+
limit:limit
224224
error:&error];
225225
if (entries == nil) {
226226
XCWSetErrorMessage(error_message, error);
@@ -231,12 +231,13 @@ xcw_native_owned_bytes xcw_native_screenshot_png(const char *udid, char **error_
231231
}
232232
}
233233

234-
char *xcw_native_accessibility_snapshot(const char *udid, bool has_point, double x, double y, char **error_message) {
234+
char *xcw_native_accessibility_snapshot(const char *udid, bool has_point, double x, double y, size_t max_depth, char **error_message) {
235235
@autoreleasepool {
236236
NSError *error = nil;
237237
NSValue *pointValue = has_point ? [NSValue valueWithPoint:NSMakePoint(x, y)] : nil;
238238
NSDictionary *snapshot = [XCWAccessibilityBridge accessibilitySnapshotForSimulatorUDID:XCWStringFromCString(udid)
239239
atPoint:pointValue
240+
maxDepth:max_depth
240241
error:&error];
241242
if (snapshot == nil) {
242243
XCWSetErrorMessage(error_message, error);
@@ -395,6 +396,44 @@ bool xcw_native_input_send_multitouch(void *handle, double x1, double y1, double
395396
}
396397
}
397398

399+
bool xcw_native_input_send_key(void *handle, uint16_t key_code, uint32_t modifiers, char **error_message) {
400+
@autoreleasepool {
401+
if (handle == NULL) {
402+
XCWSetErrorMessage(error_message, [NSError errorWithDomain:@"SimDeck.NativeInput"
403+
code:1
404+
userInfo:@{NSLocalizedDescriptionKey: @"Native input handle is null."}]);
405+
return false;
406+
}
407+
NSError *error = nil;
408+
BOOL ok = [(__bridge DFPrivateSimulatorDisplayBridge *)handle sendKeyCode:key_code
409+
modifiers:modifiers
410+
error:&error];
411+
if (!ok) {
412+
XCWSetErrorMessage(error_message, error);
413+
}
414+
return ok;
415+
}
416+
}
417+
418+
bool xcw_native_input_send_key_event(void *handle, uint16_t key_code, bool down, char **error_message) {
419+
@autoreleasepool {
420+
if (handle == NULL) {
421+
XCWSetErrorMessage(error_message, [NSError errorWithDomain:@"SimDeck.NativeInput"
422+
code:1
423+
userInfo:@{NSLocalizedDescriptionKey: @"Native input handle is null."}]);
424+
return false;
425+
}
426+
NSError *error = nil;
427+
BOOL ok = [(__bridge DFPrivateSimulatorDisplayBridge *)handle sendKeyCode:key_code
428+
down:down
429+
error:&error];
430+
if (!ok) {
431+
XCWSetErrorMessage(error_message, error);
432+
}
433+
return ok;
434+
}
435+
}
436+
398437
bool xcw_native_send_key(const char *udid, uint16_t key_code, uint32_t modifiers, char **error_message) {
399438
@autoreleasepool {
400439
DFPrivateSimulatorDisplayBridge *bridge = XCWInputBridgeForUDID(udid, error_message);

0 commit comments

Comments
 (0)