@quiesco/native 0.0.0-stage → 0.1.1

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.
@@ -0,0 +1,293 @@
1
+ #import "QSTouchInjector.h"
2
+
3
+ #import <mach/mach_time.h>
4
+
5
+ // Independent implementation of the approach used by KIF (kif-framework/KIF, Apache-2.0):
6
+ // a UITouch with private setters, attached to the application's touches event together with an
7
+ // IOHIDEvent, then delivered with -[UIApplication sendEvent:].
8
+
9
+ #pragma mark - IOKit private declarations
10
+
11
+ typedef struct __IOHIDEvent *IOHIDEventRef;
12
+ typedef double IOHIDFloat;
13
+ typedef UInt32 IOOptionBits;
14
+
15
+ enum {
16
+ kQSTransducerFinger = 2,
17
+ kQSTransducerHand = 3,
18
+ };
19
+ enum {
20
+ kQSDigitizerEventRange = 0x1,
21
+ kQSDigitizerEventTouch = 0x2,
22
+ kQSDigitizerEventPosition = 0x4,
23
+ };
24
+ // (kIOHIDEventTypeDigitizer << 16) + 25
25
+ static const uint32_t kQSFieldDigitizerIsDisplayIntegrated = (11 << 16) + 25;
26
+
27
+ extern IOHIDEventRef IOHIDEventCreateDigitizerEvent(CFAllocatorRef, uint64_t timeStamp, uint32_t type, uint32_t index,
28
+ uint32_t identity, uint32_t eventMask, uint32_t buttonMask,
29
+ IOHIDFloat x, IOHIDFloat y, IOHIDFloat z, IOHIDFloat tipPressure,
30
+ IOHIDFloat barrelPressure, Boolean range, Boolean touch,
31
+ IOOptionBits options);
32
+ extern IOHIDEventRef IOHIDEventCreateDigitizerFingerEventWithQuality(
33
+ CFAllocatorRef, uint64_t timeStamp, uint32_t index, uint32_t identity, uint32_t eventMask, IOHIDFloat x, IOHIDFloat y,
34
+ IOHIDFloat z, IOHIDFloat tipPressure, IOHIDFloat twist, IOHIDFloat minorRadius, IOHIDFloat majorRadius,
35
+ IOHIDFloat quality, IOHIDFloat density, IOHIDFloat irregularity, Boolean range, Boolean touch, IOOptionBits options);
36
+ extern void IOHIDEventAppendEvent(IOHIDEventRef event, IOHIDEventRef child);
37
+ extern void IOHIDEventSetIntegerValue(IOHIDEventRef event, uint32_t field, int value);
38
+
39
+ #pragma mark - UIKit private declarations
40
+
41
+ @interface UITouch (QSPrivate)
42
+ - (void)setWindow:(UIWindow *)window;
43
+ - (void)setView:(UIView *)view;
44
+ - (void)setTapCount:(NSUInteger)tapCount;
45
+ - (void)setPhase:(UITouchPhase)phase;
46
+ - (void)setTimestamp:(NSTimeInterval)timestamp;
47
+ - (void)setGestureView:(UIView *)view;
48
+ - (void)_setLocationInWindow:(CGPoint)location resetPrevious:(BOOL)resetPrevious;
49
+ - (void)_setIsFirstTouchForView:(BOOL)first;
50
+ - (void)_setHidEvent:(IOHIDEventRef)event;
51
+ @end
52
+
53
+ @interface UIEvent (QSPrivate)
54
+ - (void)_clearTouches;
55
+ - (void)_addTouch:(UITouch *)touch forDelayedDelivery:(BOOL)delayed;
56
+ - (void)_setHIDEvent:(IOHIDEventRef)event;
57
+ @end
58
+
59
+ @interface UIApplication (QSPrivate)
60
+ - (UIEvent *)_touchesEvent;
61
+ @end
62
+
63
+ #pragma mark - Implementation
64
+
65
+ static UITouch *qsCurrentTouch;
66
+ /** Multi-finger state: a UITouch or NSNull per finger. */
67
+ static NSMutableArray *qsFingers;
68
+
69
+ static IOHIDEventRef QSCreateHIDEvent(UITouch *touch) {
70
+ uint64_t ts = mach_absolute_time();
71
+ BOOL touching = touch.phase != UITouchPhaseEnded && touch.phase != UITouchPhaseCancelled;
72
+ uint32_t mask = touch.phase == UITouchPhaseMoved ? kQSDigitizerEventPosition
73
+ : (kQSDigitizerEventRange | kQSDigitizerEventTouch);
74
+ IOHIDEventRef hand = IOHIDEventCreateDigitizerEvent(kCFAllocatorDefault, ts, kQSTransducerHand, 0, 0,
75
+ kQSDigitizerEventTouch, 0, 0, 0, 0, 0, 0, false, true, 0);
76
+ IOHIDEventSetIntegerValue(hand, kQSFieldDigitizerIsDisplayIntegrated, 1);
77
+ CGPoint p = [touch locationInView:touch.window];
78
+ IOHIDEventRef finger = IOHIDEventCreateDigitizerFingerEventWithQuality(
79
+ kCFAllocatorDefault, ts, 1, 2, mask, p.x, p.y, 0, 0, 0, 5, 5, 1, 1, 1, touching, touching, 0);
80
+ IOHIDEventSetIntegerValue(finger, kQSFieldDigitizerIsDisplayIntegrated, 1);
81
+ IOHIDEventAppendEvent(hand, finger);
82
+ CFRelease(finger);
83
+ return hand;
84
+ }
85
+
86
+ static void QSSend(UITouch *touch) {
87
+ [touch setTimestamp:NSProcessInfo.processInfo.systemUptime];
88
+ IOHIDEventRef hid = QSCreateHIDEvent(touch);
89
+ [touch _setHidEvent:hid];
90
+ UIEvent *event = [UIApplication.sharedApplication _touchesEvent];
91
+ [event _clearTouches];
92
+ [event _setHIDEvent:hid];
93
+ [event _addTouch:touch forDelayedDelivery:NO];
94
+ CFRelease(hid);
95
+ [UIApplication.sharedApplication sendEvent:event];
96
+ }
97
+
98
+ /**
99
+ * One HID hand event with a finger child per touch (index i+1, identity i+2), like a real multi-finger
100
+ * report: began / ended fingers carry range + touch, moved ones position.
101
+ */
102
+ static IOHIDEventRef QSCreateMultiHIDEvent(NSArray<UITouch *> *touches, NSArray<NSNumber *> *indexes) {
103
+ uint64_t ts = mach_absolute_time();
104
+ BOOL any = NO;
105
+ for (UITouch *t in touches) any = any || (t.phase != UITouchPhaseEnded && t.phase != UITouchPhaseCancelled);
106
+ IOHIDEventRef hand = IOHIDEventCreateDigitizerEvent(kCFAllocatorDefault, ts, kQSTransducerHand, 0, 0,
107
+ kQSDigitizerEventTouch, 0, 0, 0, 0, 0, 0, false, any, 0);
108
+ IOHIDEventSetIntegerValue(hand, kQSFieldDigitizerIsDisplayIntegrated, 1);
109
+ for (NSUInteger k = 0; k < touches.count; k++) {
110
+ UITouch *touch = touches[k];
111
+ uint32_t i = (uint32_t)indexes[k].unsignedIntegerValue;
112
+ BOOL touching = touch.phase != UITouchPhaseEnded && touch.phase != UITouchPhaseCancelled;
113
+ uint32_t mask = touch.phase == UITouchPhaseMoved ? kQSDigitizerEventPosition
114
+ : touch.phase == UITouchPhaseStationary ? 0
115
+ : (kQSDigitizerEventRange | kQSDigitizerEventTouch);
116
+ CGPoint p = [touch locationInView:touch.window];
117
+ IOHIDEventRef finger = IOHIDEventCreateDigitizerFingerEventWithQuality(
118
+ kCFAllocatorDefault, ts, i + 1, i + 2, mask, p.x, p.y, 0, 0, 0, 5, 5, 1, 1, 1, touching, touching, 0);
119
+ IOHIDEventSetIntegerValue(finger, kQSFieldDigitizerIsDisplayIntegrated, 1);
120
+ IOHIDEventAppendEvent(hand, finger);
121
+ CFRelease(finger);
122
+ }
123
+ return hand;
124
+ }
125
+
126
+ static void QSSendMany(NSArray<UITouch *> *touches, NSArray<NSNumber *> *indexes) {
127
+ NSTimeInterval now = NSProcessInfo.processInfo.systemUptime;
128
+ for (UITouch *t in touches) [t setTimestamp:now];
129
+ IOHIDEventRef hid = QSCreateMultiHIDEvent(touches, indexes);
130
+ UIEvent *event = [UIApplication.sharedApplication _touchesEvent];
131
+ [event _clearTouches];
132
+ [event _setHIDEvent:hid];
133
+ for (UITouch *t in touches) {
134
+ [t _setHidEvent:hid];
135
+ [event _addTouch:t forDelayedDelivery:NO];
136
+ }
137
+ CFRelease(hid);
138
+ [UIApplication.sharedApplication sendEvent:event];
139
+ }
140
+
141
+ @implementation QSTouchInjector
142
+
143
+ + (NSString *)missingPrivateAPI {
144
+ UITouch *touch = [UITouch new];
145
+ for (NSString *name in @[ @"setWindow:", @"setView:", @"setTapCount:", @"setPhase:", @"setTimestamp:",
146
+ @"_setLocationInWindow:resetPrevious:", @"_setHidEvent:" ]) {
147
+ if (![touch respondsToSelector:NSSelectorFromString(name)]) return [@"UITouch " stringByAppendingString:name];
148
+ }
149
+ if (![UIApplication.sharedApplication respondsToSelector:@selector(_touchesEvent)]) return @"UIApplication _touchesEvent";
150
+ UIEvent *event = [UIApplication.sharedApplication _touchesEvent];
151
+ for (NSString *name in @[ @"_clearTouches", @"_addTouch:forDelayedDelivery:", @"_setHIDEvent:" ]) {
152
+ if (![event respondsToSelector:NSSelectorFromString(name)]) return [@"UIEvent " stringByAppendingString:name];
153
+ }
154
+ return nil;
155
+ }
156
+
157
+ + (NSArray<UIWindowScene *> *)foregroundScenes {
158
+ NSMutableArray *out = [NSMutableArray array];
159
+ for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
160
+ if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:UIWindowScene.class]) {
161
+ [out addObject:scene];
162
+ }
163
+ }
164
+ return out;
165
+ }
166
+
167
+ + (UIWindow *)keyWindow {
168
+ for (UIWindowScene *ws in [self foregroundScenes]) {
169
+ if (ws.keyWindow) return ws.keyWindow;
170
+ for (UIWindow *w in ws.windows) {
171
+ if (w.isKeyWindow) return w;
172
+ }
173
+ }
174
+ return nil;
175
+ }
176
+
177
+ + (NSArray<UIWindow *> *)windows {
178
+ NSMutableArray<UIWindow *> *out = [NSMutableArray array];
179
+ for (UIWindowScene *ws in [self foregroundScenes]) {
180
+ for (UIWindow *w in ws.windows) {
181
+ if (!w.hidden && w.alpha > 0) [out addObject:w];
182
+ }
183
+ }
184
+ [out sortUsingComparator:^NSComparisonResult(UIWindow *a, UIWindow *b) {
185
+ return a.windowLevel < b.windowLevel ? NSOrderedAscending : a.windowLevel > b.windowLevel ? NSOrderedDescending : NSOrderedSame;
186
+ }];
187
+ return out;
188
+ }
189
+
190
+ + (UIView *)viewWithReactTag:(NSInteger)tag {
191
+ if (tag <= 0) return nil;
192
+ Protocol *componentView = NSProtocolFromString(@"RCTComponentViewProtocol");
193
+ for (UIWindow *w in [[self windows] reverseObjectEnumerator]) {
194
+ UIView *v = [w viewWithTag:tag];
195
+ if (v && (componentView == nil || [v conformsToProtocol:componentView])) return v;
196
+ }
197
+ return nil;
198
+ }
199
+
200
+ + (UIView *)touchDownAt:(CGPoint)point inWindow:(UIWindow *)window tapCount:(NSUInteger)tapCount {
201
+ UIView *hit = [window hitTest:point withEvent:nil];
202
+ UITouch *touch = [[UITouch alloc] init];
203
+ [touch setWindow:window]; // must be first: resets the other fields
204
+ [touch setTapCount:tapCount];
205
+ [touch _setLocationInWindow:point resetPrevious:YES];
206
+ [touch setView:hit];
207
+ [touch setPhase:UITouchPhaseBegan];
208
+ if ([touch respondsToSelector:@selector(setGestureView:)]) [touch setGestureView:hit];
209
+ if ([touch respondsToSelector:@selector(_setIsFirstTouchForView:)]) [touch _setIsFirstTouchForView:YES];
210
+ qsCurrentTouch = touch;
211
+ QSSend(touch);
212
+ return hit;
213
+ }
214
+
215
+ + (BOOL)touchMoveTo:(CGPoint)point {
216
+ UITouch *touch = qsCurrentTouch;
217
+ if (!touch) return NO;
218
+ [touch _setLocationInWindow:point resetPrevious:NO];
219
+ [touch setPhase:UITouchPhaseMoved];
220
+ QSSend(touch);
221
+ return YES;
222
+ }
223
+
224
+ + (BOOL)touchUp {
225
+ UITouch *touch = qsCurrentTouch;
226
+ if (!touch) return NO;
227
+ [touch setPhase:UITouchPhaseEnded];
228
+ QSSend(touch);
229
+ qsCurrentTouch = nil;
230
+ return YES;
231
+ }
232
+
233
+ + (BOOL)sendFingers:(NSArray *)points inWindow:(UIWindow *)window {
234
+ if (qsFingers == nil) qsFingers = [NSMutableArray array];
235
+ while (qsFingers.count < points.count) [qsFingers addObject:NSNull.null];
236
+ NSMutableArray<UITouch *> *touches = [NSMutableArray array];
237
+ NSMutableArray<NSNumber *> *indexes = [NSMutableArray array];
238
+ NSMutableIndexSet *lifted = [NSMutableIndexSet indexSet];
239
+ BOOL changed = NO;
240
+ for (NSUInteger i = 0; i < qsFingers.count; i++) {
241
+ id p = i < points.count ? points[i] : NSNull.null;
242
+ id current = qsFingers[i];
243
+ BOOL down = [p isKindOfClass:NSValue.class];
244
+ if (down && current == NSNull.null) {
245
+ CGPoint point = [(NSValue *)p CGPointValue];
246
+ UIView *hit = [window hitTest:point withEvent:nil];
247
+ UITouch *touch = [[UITouch alloc] init];
248
+ [touch setWindow:window]; // must be first: resets the other fields
249
+ [touch setTapCount:1];
250
+ [touch _setLocationInWindow:point resetPrevious:YES];
251
+ [touch setView:hit];
252
+ [touch setPhase:UITouchPhaseBegan];
253
+ if ([touch respondsToSelector:@selector(setGestureView:)]) [touch setGestureView:hit];
254
+ if ([touch respondsToSelector:@selector(_setIsFirstTouchForView:)]) [touch _setIsFirstTouchForView:YES];
255
+ qsFingers[i] = touch;
256
+ changed = YES;
257
+ } else if (down) {
258
+ UITouch *touch = current;
259
+ CGPoint point = [(NSValue *)p CGPointValue];
260
+ CGPoint before = [touch locationInView:touch.window];
261
+ if (CGPointEqualToPoint(before, point)) {
262
+ [touch setPhase:UITouchPhaseStationary];
263
+ } else {
264
+ [touch _setLocationInWindow:point resetPrevious:NO];
265
+ [touch setPhase:UITouchPhaseMoved];
266
+ changed = YES;
267
+ }
268
+ } else if (current != NSNull.null) {
269
+ [(UITouch *)current setPhase:UITouchPhaseEnded];
270
+ [lifted addIndex:i];
271
+ changed = YES;
272
+ }
273
+ if (qsFingers[i] != NSNull.null) {
274
+ [touches addObject:qsFingers[i]];
275
+ [indexes addObject:@(i)];
276
+ }
277
+ }
278
+ if (changed && touches.count > 0) QSSendMany(touches, indexes);
279
+ [lifted enumerateIndexesUsingBlock:^(NSUInteger i, BOOL *stop) { qsFingers[i] = NSNull.null; }];
280
+ return changed;
281
+ }
282
+
283
+ + (void)liftAllFingers {
284
+ if (qsFingers == nil) return;
285
+ NSMutableArray *none = [NSMutableArray array];
286
+ for (NSUInteger i = 0; i < qsFingers.count; i++) [none addObject:NSNull.null];
287
+ UIWindow *window = nil;
288
+ for (id t in qsFingers) if (t != NSNull.null) window = ((UITouch *)t).window;
289
+ if (window) [self sendFingers:none inWindow:window];
290
+ [qsFingers removeAllObjects];
291
+ }
292
+
293
+ @end
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = 'QuiescoNative'
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.description = package['description']
10
+ s.license = package['license']
11
+ s.author = 'Quiesco'
12
+ s.homepage = 'https://github.com/quiesco/quiesco'
13
+ s.platforms = { :ios => '16.4' }
14
+ s.source = { git: '' }
15
+ s.static_framework = true
16
+
17
+ s.dependency 'ExpoModulesCore'
18
+ # IOHIDEvent* (private C API) lives in IOKit. This pod is only linked into e2e builds:
19
+ # @quiesco/expo-plugin excludes it from autolinking otherwise (docs/spikes/06-prod-strip.md).
20
+ s.frameworks = 'IOKit'
21
+
22
+ s.pod_target_xcconfig = {
23
+ 'DEFINES_MODULE' => 'YES',
24
+ }
25
+
26
+ s.source_files = '**/*.{h,m,swift}'
27
+ end