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
|
/* Exercise the real AppKit adapter without a window or a physical trackpad.
* Only SDL's event queue and window lookup are replaced with test doubles. */
#import <Cocoa/Cocoa.h>
#include "../src/gui/macos_trackpad.m"
#include <assert.h>
static id native_window;
static SDL_Event queued[32];
static unsigned count;
SDL_PropertiesID SDL_GetWindowProperties(SDL_Window *window) { (void)window; return 1; }
void *SDL_GetPointerProperty(SDL_PropertiesID props, const char *name, void *fallback)
{ (void)props; (void)name; (void)fallback; return (__bridge void *)native_window; }
Uint32 SDL_RegisterEvents(int n) { (void)n; return SDL_EVENT_USER; }
Uint64 SDL_GetTicksNS(void) { return 100; }
SDL_WindowID SDL_GetWindowID(SDL_Window *window) { (void)window; return 42; }
bool SDL_PushEvent(SDL_Event *event) { assert(count < 32); queued[count++] = *event; return true; }
@interface FakeNativeWindow : NSObject
@property NSView *contentView;
@end
@implementation FakeNativeWindow
@end
@interface HapticSpy : NSObject <NSHapticFeedbackPerformer>
@property NSUInteger pulses;
@end
@implementation HapticSpy
- (void)performFeedbackPattern:(NSHapticFeedbackPattern)pattern performanceTime:(NSHapticFeedbackPerformanceTime)time
{
assert(pattern == NSHapticFeedbackPatternAlignment);
assert(time == NSHapticFeedbackPerformanceTimeNow);
self.pulses++;
}
@end
static HapticSpy *haptic_spy;
static id fake_performer(id receiver, SEL selector)
{ (void)receiver; (void)selector; return haptic_spy; }
static BOOL swipe_enabled(id receiver, SEL selector)
{ (void)receiver; (void)selector; return YES; }
@interface TrackpadEvent : NSEvent
@property NSUInteger fingerCount;
@property NSEventPhase testPhase;
@property BOOL precise;
@property BOOL horizontal, cancelled;
@property CGFloat progress, discreteDirection;
@property NSArray<NSNumber *> *progressSequence;
@end
@implementation TrackpadEvent
- (NSPoint)locationInWindow { return NSMakePoint(25, 40); }
- (NSEventPhase)phase { return self.testPhase; }
- (BOOL)hasPreciseScrollingDeltas { return self.precise; }
- (NSEventPhase)momentumPhase { return NSEventPhaseNone; }
- (NSEventModifierFlags)modifierFlags { return 0; }
- (CGFloat)deltaX { return self.discreteDirection; }
- (CGFloat)scrollingDeltaX { return self.horizontal ? 20 : 3; }
- (CGFloat)scrollingDeltaY { return self.horizontal ? 1 : 20; }
- (void)trackSwipeEventWithOptions:(NSEventSwipeTrackingOptions)options
dampenAmountThresholdMin:(CGFloat)min max:(CGFloat)max
usingHandler:(void (^)(CGFloat, NSEventPhase, BOOL, BOOL *))handler
{
assert(options == 0 && min == -100 && max == 100);
BOOL stop = NO;
NSArray<NSNumber *> *samples = self.progressSequence ?: @[@0.13, @0.18];
for (NSNumber *sample in samples) {
handler(sample.doubleValue, NSEventPhaseChanged, NO, &stop);
assert(!stop); // a jump must not end physical tracking
}
if (self.cancelled) handler(self.progress, NSEventPhaseCancelled, NO, &stop);
else if (!self.progressSequence) handler(self.progress, NSEventPhaseChanged, NO, &stop);
if (!stop) handler(0, NSEventPhaseEnded, NO, &stop);
assert(stop); // no native completion animation can cause another jump
}
- (NSInteger)stage { return 2; }
- (float)rotation { return 12; }
- (CGFloat)magnification { return 0.25; }
- (NSSet<NSTouch *> *)touchesMatchingPhase:(NSTouchPhase)phase inView:(NSView *)view
{
(void)phase; (void)view;
NSMutableSet *result = [NSMutableSet set];
for (NSUInteger i = 0; i < self.fingerCount; ++i) [result addObject:[NSObject new]];
return (NSSet<NSTouch *> *)result;
}
@end
int main(void)
{
@autoreleasepool {
haptic_spy = [HapticSpy new];
Method performer = class_getClassMethod([NSHapticFeedbackManager class], @selector(defaultPerformer));
IMP original = method_setImplementation(performer, (IMP)fake_performer);
FakeNativeWindow *window = [FakeNativeWindow new];
NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 400, 300)];
window.contentView = view;
native_window = window;
ui_macos_trackpad_attach((SDL_Window *)1);
assert([NSStringFromClass(view.class) isEqualToString:@"PardesSDLTrackpadView"]);
assert(view.wantsRestingTouches);
TrackpadEvent *event = [TrackpadEvent new];
event.fingerCount = 3;
[view touchesBeganWithEvent:event];
UITrackpadEvent *packet = (UITrackpadEvent *)&queued[0];
assert(packet->kind == UI_TRACKPAD_FINGERS && packet->fingers == 3);
assert(packet->windowID == 42 && packet->x == 25 && packet->y == 260);
event.precise = YES;
event.testPhase = NSEventPhaseBegan;
[view scrollWheel:event];
packet = (UITrackpadEvent *)&queued[1];
assert(packet->kind == UI_TRACKPAD_SCROLL && packet->a == 3 && packet->b == 20 && packet->phase == 1);
// System momentum has no finger phase. It must keep reaching SDL.
event.testPhase = NSEventPhaseNone;
[view scrollWheel:event];
assert(((UITrackpadEvent *)&queued[2])->kind == UI_TRACKPAD_SCROLL);
[view pressureChangeWithEvent:event];
assert(((UITrackpadEvent *)&queued[3])->kind == UI_TRACKPAD_PRESSURE);
assert(((UITrackpadEvent *)&queued[3])->a == 2);
[view rotateWithEvent:event];
assert(((UITrackpadEvent *)&queued[4])->kind == UI_TRACKPAD_ROTATE);
assert(((UITrackpadEvent *)&queued[4])->a == 12);
[view magnifyWithEvent:event];
assert(((UITrackpadEvent *)&queued[5])->kind == UI_TRACKPAD_PINCH);
assert(((UITrackpadEvent *)&queued[5])->a == 1.25);
event.discreteDirection = -1;
[view swipeWithEvent:event];
assert(((UITrackpadEvent *)&queued[6])->kind == UI_TRACKPAD_HISTORY);
assert(((UITrackpadEvent *)&queued[6])->a == -1);
Method preference = class_getClassMethod([NSEvent class], @selector(isSwipeTrackingFromScrollEventsEnabled));
IMP old_preference = method_setImplementation(preference, (IMP)swipe_enabled);
event.horizontal = YES;
event.progress = 0.25;
event.testPhase = NSEventPhaseBegan;
[view scrollWheel:event];
assert(count == 8);
assert(haptic_spy.pulses == 0); // command dispatch owns the haptic, never progress
assert(((UITrackpadEvent *)&queued[7])->kind == UI_TRACKPAD_HISTORY);
assert(((UITrackpadEvent *)&queued[7])->a == 1);
event.cancelled = YES;
event.progress = 1; // even later completion cannot undo cancellation
[view scrollWheel:event];
assert(count == 8); // a cancelled swipe never navigates
event.cancelled = NO;
event.progress = 0.20;
[view scrollWheel:event];
assert(count == 8); // below threshold never commits on release/animation
event.horizontal = NO;
[view scrollWheel:event];
event.horizontal = YES;
event.testPhase = NSEventPhaseChanged;
[view scrollWheel:event];
assert(((UITrackpadEvent *)&queued[9])->kind == UI_TRACKPAD_SCROLL);
event.testPhase = NSEventPhaseBegan;
event.progress = -0.25;
[view scrollWheel:event];
assert(count == 11);
assert(((UITrackpadEvent *)&queued[10])->a == -1);
event.testPhase = NSEventPhaseChanged;
[view scrollWheel:event];
event.testPhase = NSEventPhaseNone;
[view scrollWheel:event];
assert(count == 11); // remaining movement/momentum cannot jump twice
event.testPhase = NSEventPhaseBegan;
event.progressSequence = @[@0.13, @0.25, @0.25, @0.26, @0.38, @0.50,
@0.50, @0.37, @0.24, @0.12, @-0.01];
unsigned continuous_start = count;
[view scrollWheel:event];
assert(count == continuous_start + 7);
const int directions[] = {1, 1, 1, -1, -1, -1, -1};
for (unsigned i = 0; i < 7; ++i)
assert(((UITrackpadEvent *)&queued[continuous_start + i])->a == directions[i]);
// Lift resets the larger initial threshold for the next gesture.
event.progressSequence = @[@0.13, @0.20];
[view scrollWheel:event];
assert(count == continuous_start + 7);
event.progressSequence = @[@-0.13, @-0.25, @-0.25, @-0.26, @-0.38, @-0.50,
@-0.50, @-0.37, @-0.24, @-0.12, @0.01];
[view scrollWheel:event];
assert(count == continuous_start + 14);
for (unsigned i = 0; i < 7; ++i)
assert(((UITrackpadEvent *)&queued[continuous_start + 7 + i])->a == -directions[i]);
method_setImplementation(preference, old_preference);
assert(haptic_spy.pulses == 0); // no preview pulses, even across repeated/reversed swipes
NSUInteger before = haptic_spy.pulses;
ui_macos_trackpad_haptic();
method_setImplementation(performer, original);
assert(haptic_spy.pulses == before + 1);
puts("PASS native SDL trackpad adapter: touches, momentum, pressure, rotation, pinch, history/cancellation, haptics");
}
return 0;
}
|