summaryrefslogtreecommitdiff
path: root/src/gui/Trackpad.zig
blob: ef28d21184d236eb86ca2b9accc70eb4eb03ceae (plain) (blame)
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
//! Mac trackpad policy, after AppKit decoding. Buttons use SDL's 1/2/3.
const std = @import("std");
const Trackpad = @This();

fingers: usize = 0,
stream: ?u8 = null,
button: ?u8 = null,
forced: bool = false,
rotation: f32 = 0,
rotation_total: f32 = 0,
gesture: enum { none, pinch, rotate } = .none,
pinch_base: f32 = 0,
pinch_total: f32 = 1,

pub fn countFingers(t: *Trackpad, count: usize) void {
    if (count < 2 or t.fingers < 2) {
        t.gesture = .none;
        t.rotation_total = 0;
        t.pinch_base = 0;
        t.pinch_total = 1;
    }
    t.fingers = count;
}

pub fn press(t: *Trackpad, stream: u8) u8 {
    if (t.fingers == 0 or stream > 3) return stream;
    t.stream = stream;
    t.button = if (t.fingers >= 2) 2 else stream;
    t.forced = false;
    return t.button.?;
}

pub fn release(t: *Trackpad, stream: u8) ?u8 {
    if (t.stream != stream) return stream;
    defer {
        t.stream = null;
        t.button = null;
        t.forced = false;
    }
    return if (t.forced) null else t.button;
}

/// The caller releases this button before issuing one Look click. The
/// physical release afterwards is swallowed, so it cannot become a chord.
pub fn force(t: *Trackpad, stage: f32) ?u8 {
    if (stage < 2 or t.forced or t.button == null or t.button == 3) return null;
    t.forced = true;
    return t.button;
}

pub fn rotate(t: *Trackpad, degrees: f32, phase: u32) i32 {
    if (phase == 1 or phase == 3) {
        t.rotation = 0;
        t.rotation_total = 0;
    }
    if (phase == 3 or !std.math.isFinite(degrees)) return 0;
    if (t.gesture == .pinch) return 0;
    t.rotation_total += std.math.clamp(degrees, -640, 640);
    if (@abs(t.rotation_total) >= 3) t.gesture = .rotate;
    const next = t.rotation + std.math.clamp(degrees, -640, 640);
    const ticks: i32 = @intFromFloat(@trunc(next / 10));
    t.rotation = next - @as(f32, @floatFromInt(ticks)) * 10;
    return ticks;
}

/// Ignore small magnification noise, especially while twisting. Once a
/// deliberate pinch wins, half the logarithmic gain makes fine zoom easier.
pub fn magnify(t: *Trackpad, size: f32, scale: f32, phase: u32) ?f32 {
    if (phase == 1 or t.pinch_base == 0) {
        t.pinch_base = size;
        t.pinch_total = 1;
    }
    if (phase == 3) {
        t.pinch_base = 0;
        if (t.gesture == .pinch) t.gesture = .none;
        return null;
    }
    if (t.gesture == .rotate or !std.math.isFinite(scale) or scale <= 0) return null;
    t.pinch_total = std.math.clamp(t.pinch_total * scale, 0.01, 100);
    if (t.gesture == .none) {
        if (t.pinch_total > 1 / @as(f32, 1.1) and t.pinch_total < 1.1) return null;
        t.gesture = .pinch;
    }
    return std.math.clamp(t.pinch_base * @sqrt(t.pinch_total), 8, 72);
}

pub fn pinch(size: f32, scale: f32) f32 {
    if (!std.math.isFinite(scale) or scale <= 0) return size;
    return std.math.clamp(size * scale, 8, 72);
}

test "Mac trackpad multi-finger buttons stay latched after the fingers lift" {
    var t: Trackpad = .{};
    try std.testing.expectEqual(@as(u8, 3), t.press(3)); // real mouse
    try std.testing.expectEqual(@as(?u8, 3), t.release(3));
    for ([_]usize{ 2, 3 }) |n| {
        t.fingers = n;
        try std.testing.expectEqual(@as(u8, 2), t.press(3));
        t.fingers = 0;
        try std.testing.expectEqual(@as(?u8, 2), t.release(3));
    }
}

test "Mac trackpad force click upgrades Exec once without a paste chord or stray release" {
    var t: Trackpad = .{ .fingers = 2 };
    try std.testing.expectEqual(@as(u8, 2), t.press(3));
    try std.testing.expect(t.force(1) == null);
    try std.testing.expectEqual(@as(?u8, 2), t.force(2));
    try std.testing.expect(t.force(2) == null);
    try std.testing.expect(t.release(3) == null);
    t.fingers = 1;
    try std.testing.expectEqual(@as(u8, 1), t.press(1));
    try std.testing.expectEqual(@as(?u8, 1), t.release(1));
}

test "Mac trackpad rotate and pinch reject invalid input and reset cancelled gestures" {
    var t: Trackpad = .{};
    try std.testing.expectEqual(@as(i32, 0), t.rotate(7, 1));
    try std.testing.expectEqual(@as(i32, 1), t.rotate(5, 0));
    try std.testing.expectEqual(@as(i32, -1), t.rotate(-12, 0));
    try std.testing.expectEqual(@as(i32, 0), t.rotate(9, 3));
    try std.testing.expectEqual(@as(f32, 0), t.rotation);
    try std.testing.expectEqual(@as(f32, 18), pinch(18, 1));
    try std.testing.expectEqual(@as(f32, 36), pinch(18, 2));
    try std.testing.expectEqual(@as(f32, 72), pinch(18, 100));
    try std.testing.expectEqual(@as(f32, 18), pinch(18, std.math.nan(f32)));
}

test "trackpad pinch ignores twist noise and uses gentler deliberate zoom" {
    var t: Trackpad = .{};
    t.countFingers(2);
    try std.testing.expect(t.magnify(18, 1, 1) == null);
    for (0..5) |_| try std.testing.expect(t.magnify(18, 1.01, 0) == null);
    _ = t.rotate(7, 1);
    try std.testing.expect(t.magnify(18, 1.3, 0) == null);
    t.countFingers(0);
    t.countFingers(2);
    const zoom = t.magnify(18, 1.2, 1) orelse return error.PinchNotRecognized;
    try std.testing.expect(zoom > 18 and zoom < 20.5);
    try std.testing.expectEqual(@as(i32, 0), t.rotate(30, 1));
    try std.testing.expect(t.magnify(zoom, 1, 3) == null);
}