-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlibtouch.h
240 lines (198 loc) · 6.64 KB
/
libtouch.h
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
#ifndef _LIBTOUCH_H
#define _LIBTOUCH_H
#include <stdint.h>
enum libtouch_action_type {
/**
* Pressing or releasing a finger to or from the touch device.
*/
LIBTOUCH_ACTION_TOUCH,
/**
* A difference in the position of the center of the touch group over time.
*/
LIBTOUCH_ACTION_MOVE,
/**
* The angle of rotation between each finger and the center of the touch
* group changes.
*/
LIBTOUCH_ACTION_ROTATE,
/**
* The average distance between each finger and the center of the touch
* group changes.
*/
LIBTOUCH_ACTION_PINCH,
/**
* No change within the configured thresholds over a certain time frame.
*/
LIBTOUCH_ACTION_DELAY,
};
/**
* Represents a change in the number of touch points in the current touch group.
* DOWN represents pressing a finger against the touch device, and UP represents
* removing the finger from the device. Both represents any kind of change.
*/
enum libtouch_touch_mode {
LIBTOUCH_TOUCH_UP = 1 << 0,
LIBTOUCH_TOUCH_DOWN = 1 << 1,
};
/**
* Represents the directions in which a LIBTOUCH_ACTION_MOVE can occur. Both
* POSITIVE_X and NEGATIVE_X corresponds to movement in either direction along
* the X axis; the same holds for Y.
*/
enum libtouch_move_dir {
LIBTOUCH_MOVE_POSITIVE_X = 1 << 0,
LIBTOUCH_MOVE_POSITIVE_Y = 1 << 1,
LIBTOUCH_MOVE_NEGATIVE_X = 1 << 2,
LIBTOUCH_MOVE_NEGATIVE_Y = 1 << 3,
};
/**
* Represents the direction of rotation in which a LIBTOUCH_ACTION_ROTATE can
* occur. Both CLOCKWISE and ANTICLOCKWISE corresponds to a rotation in either
* direction.
*/
enum libtouch_rotate_dir {
LIBTOUCH_ROTATE_CLOCKWISE = 1 << 0,
LIBTOUCH_ROTATE_ANTICLOCKWISE = 1 << 1,
};
/**
* Represents the direction in which a LIBTOUCH_ACTION_PINCH can occur. Both
* UP and DOWN corresponds to a change of any amount.
*/
enum libtouch_scale_dir {
LIBTOUCH_PINCH_IN = 1 << 0,
LIBTOUCH_PINCH_OUT = 1 << 1,
};
struct libtouch_gesture_progress;
struct libtouch_progress_tracker;
/**
* Represents the internal state. The only holder of state information.
*
*/
struct libtouch_engine;
/**
* Represents a gesture,
*
* a gesture is defined as a sequence of actions
* declarative, no info of state
*/
struct libtouch_gesture;
/**
* Represents a part of a gesture.
* Declarative, no information of state.
*/
struct libtouch_action;
/**
* A region or other delimited area, within which an action listens,
* Declarative, no information of state.
*/
struct libtouch_target;
/**
* Reference to gestures and their progress
*/
struct libtouch_engine *libtouch_engine_create();
struct libtouch_gesture *libtouch_gesture_create(
struct libtouch_engine *engine);
/**
* Set a min movement before it starts counting as movement.
* useful for, for instance long pressing, in case of a not 100% stable finger
* or to ignore possible miss-swipes
*/
void libtouch_action_move_tolerance(struct libtouch_action *action, double min);
struct libtouch_target *libtouch_target_create(
struct libtouch_engine *engine, double x, double y,
double width, double height);
/**
* Informs the touch engine of a touch event.
*
* timestamp: milliseconds from an arbitrary epoch (e.g. CLOCK_MONOTONIC)
* slot: the slot of this event (e.g. which finger the event was caused by)
*/
void libtouch_progress_register_touch(
struct libtouch_progress_tracker *t,
uint32_t timestamp, int slot, enum libtouch_touch_mode mode,
double x, double y);
/**
* Informs the touch engine of a touch movement event.
*
* timestamp: milliseconds from an arbitrary epoch (e.g. CLOCK_MONOTONIC)
* slot: the slot of the event (e.g. which finger)
*/
void libtouch_progress_register_move(
struct libtouch_progress_tracker *t,
uint32_t timestamp, int slot,
double dx, double dy);
struct libtouch_action *libtouch_gesture_add_touch(
struct libtouch_gesture *gesture, uint32_t mode);
struct libtouch_action *libtouch_gesture_add_move(
struct libtouch_gesture *gesture, uint32_t direction);
struct libtouch_action *libtouch_gesture_add_rotate(
struct libtouch_gesture *gesture, uint32_t direction);
struct libtouch_action *libtouch_gesture_add_pinch(
struct libtouch_gesture *gesture, uint32_t direction);
struct libtouch_action *libtouch_gesture_add_delay(
struct libtouch_gesture *gesture, uint32_t duration);
/**
* Sets the threshold of change for an action to be considered complete. The map
* of threshold units to action type is as follows:
*
* - LIBTOUCH_ACTION_TOUCH: number of touch points
* - LIBTOUCH_ACTION_MOVE: positional units (percent of screen)
* - LIBTOUCH_ACTION_ROTATE: degrees
* - LIBTOUCH_ACTION_PINCH: scale (in percent) of original touch
* - LIBTOUCH_ACTION_DELAY: milliseconds (must be positive)
*/
void libtouch_action_set_threshold(
struct libtouch_action *action, int threshold);
/**
* Sets a libtouch_target that the action must reach to be considered complete.
* Valid for LIBTOUCH_ACTION_MOVE,
* where the movement must finish. Cannot be used together with treshold.
*
* For LIBTOUCH_ACTION_TOUCH, target defines where we must press.
*/
void libtouch_action_set_target(
struct libtouch_action *action,
struct libtouch_target *target);
/**
* Sets the minimum duration this action must take place during to be considered
* a match. For instance, if not all n fingers are pressed the same frame,
* we can consider n fingers down within duration_ms to be a n-finger touch.
*/
void libtouch_action_set_duration(
struct libtouch_action *action,
uint32_t duration_ms);
/**
* Gets the current progress of this action between 0 and 1.
*/
double libtouch_action_get_progress(
struct libtouch_progress_tracker *tracker);
void libtouch_gesture_reset_progress(
struct libtouch_gesture_progress *gesture);
/** Returns the active action for this gesture. */
struct libtouch_action *libtouch_gesture_get_current_action(
struct libtouch_gesture_progress *gesture);
/**
* Fills an array of libtouch_gesture_progress pointers
* sorted by progress.
*/
double libtouch_fill_progress_array(
struct libtouch_progress_tracker *tracker,
struct libtouch_gesture_progress **array,
uint32_t count);
/**
* Returns a completed gesture, and resets its progress
* if none exist, return NULL.
*
* Call repeatedly to get all finished gestures.
*/
struct libtouch_gesture *libtouch_handle_finished_gesture(
struct libtouch_progress_tracker *tracker);
struct libtouch_progress_tracker *libtouch_progress_tracker_create(
struct libtouch_engine *engine);
uint32_t libtouch_progress_tracker_n_gestures(
struct libtouch_progress_tracker *t);
struct libtouch_gesture_progress *libtouch_gesture_get_progress(
struct libtouch_progress_tracker *y, uint32_t index);
double libtouch_gesture_progress_get_progress(
struct libtouch_gesture_progress *gesture);
#endif