aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorLibravatar Florian Franzen <Florian.Franzen@gmail.com>2022-04-23 10:27:47 +0200
committerLibravatar Simon Ser <contact@emersion.fr>2022-05-30 12:20:43 +0200
commitcab2189aa64d04ba79dc2cbf19400435b47cdbd2 (patch)
tree450ac51fbc75c73ed1dc6728bc05b08366ace785 /common
parentAdd a Hindi (हिन्दी) translation to the README (diff)
downloadsway-cab2189aa64d04ba79dc2cbf19400435b47cdbd2.tar.gz
sway-cab2189aa64d04ba79dc2cbf19400435b47cdbd2.tar.zst
sway-cab2189aa64d04ba79dc2cbf19400435b47cdbd2.zip
sway: add bindgesture command
Co-authored-by: Michael Weiser <michael.weiser@gmx.de>
Diffstat (limited to 'common')
-rw-r--r--common/gesture.c350
-rw-r--r--common/meson.build1
2 files changed, 351 insertions, 0 deletions
diff --git a/common/gesture.c b/common/gesture.c
new file mode 100644
index 00000000..8c2efe99
--- /dev/null
+++ b/common/gesture.c
@@ -0,0 +1,350 @@
1#define _POSIX_C_SOURCE 200809L
2#include "gesture.h"
3
4#include <math.h>
5#include <stdarg.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include "list.h"
10#include "log.h"
11#include "stringop.h"
12
13const uint8_t GESTURE_FINGERS_ANY = 0;
14
15// Helper to easily allocate and format string
16static char *strformat(const char *format, ...) {
17 va_list args;
18 va_start(args, format);
19 int length = vsnprintf(NULL, 0, format, args) + 1;
20 va_end(args);
21
22 char *result = malloc(length);
23 if (result) {
24 va_start(args, format);
25 vsnprintf(result, length, format, args);
26 va_end(args);
27 }
28
29 return result;
30}
31
32char *gesture_parse(const char *input, struct gesture *output) {
33 // Clear output in case of failure
34 output->type = GESTURE_TYPE_NONE;
35 output->fingers = GESTURE_FINGERS_ANY;
36 output->directions = GESTURE_DIRECTION_NONE;
37
38 // Split input type, fingers and directions
39 list_t *split = split_string(input, ":");
40 if (split->length < 1 || split->length > 3) {
41 return strformat(
42 "expected <gesture>[:<fingers>][:direction], got %s",
43 input);
44 }
45
46 // Parse gesture type
47 if (strcmp(split->items[0], "hold") == 0) {
48 output->type = GESTURE_TYPE_HOLD;
49 } else if (strcmp(split->items[0], "pinch") == 0) {
50 output->type = GESTURE_TYPE_PINCH;
51 } else if (strcmp(split->items[0], "swipe") == 0) {
52 output->type = GESTURE_TYPE_SWIPE;
53 } else {
54 return strformat("expected hold|pinch|swipe, got %s",
55 split->items[0]);
56 }
57
58 // Parse optional arguments
59 if (split->length > 1) {
60 char *next = split->items[1];
61
62 // Try to parse as finger count (1-9)
63 if (strlen(next) == 1 && '1' <= next[0] && next[0] <= '9') {
64 output->fingers = atoi(next);
65
66 // Move to next if available
67 next = split->length == 3 ? split->items[2] : NULL;
68 } else if (split->length == 3) {
69 // Fail here if argument can only be finger count
70 return strformat("expected 1-9, got %s", next);
71 }
72
73 // If there is an argument left, try to parse as direction
74 if (next) {
75 list_t *directions = split_string(next, "+");
76
77 for (int i = 0; i < directions->length; ++i) {
78 const char *item = directions->items[i];
79 if (strcmp(item, "any") == 0) {
80 continue;
81 } else if (strcmp(item, "up") == 0) {
82 output->directions |= GESTURE_DIRECTION_UP;
83 } else if (strcmp(item, "down") == 0) {
84 output->directions |= GESTURE_DIRECTION_DOWN;
85 } else if (strcmp(item, "left") == 0) {
86 output->directions |= GESTURE_DIRECTION_LEFT;
87 } else if (strcmp(item, "right") == 0) {
88 output->directions |= GESTURE_DIRECTION_RIGHT;
89 } else if (strcmp(item, "inward") == 0) {
90 output->directions |= GESTURE_DIRECTION_INWARD;
91 } else if (strcmp(item, "outward") == 0) {
92 output->directions |= GESTURE_DIRECTION_OUTWARD;
93 } else if (strcmp(item, "clockwise") == 0) {
94 output->directions |= GESTURE_DIRECTION_CLOCKWISE;
95 } else if (strcmp(item, "counterclockwise") == 0) {
96 output->directions |= GESTURE_DIRECTION_COUNTERCLOCKWISE;
97 } else {
98 return strformat("expected direction, got %s", item);
99 }
100 }
101 list_free_items_and_destroy(directions);
102 }
103 } // if optional args
104
105 list_free_items_and_destroy(split);
106
107 return NULL;
108}
109
110const char *gesture_type_string(enum gesture_type type) {
111 switch (type) {
112 case GESTURE_TYPE_NONE:
113 return "none";
114 case GESTURE_TYPE_HOLD:
115 return "hold";
116 case GESTURE_TYPE_PINCH:
117 return "pinch";
118 case GESTURE_TYPE_SWIPE:
119 return "swipe";
120 }
121
122 return NULL;
123}
124
125const char *gesture_direction_string(enum gesture_direction direction) {
126 switch (direction) {
127 case GESTURE_DIRECTION_NONE:
128 return "none";
129 case GESTURE_DIRECTION_UP:
130 return "up";
131 case GESTURE_DIRECTION_DOWN:
132 return "down";
133 case GESTURE_DIRECTION_LEFT:
134 return "left";
135 case GESTURE_DIRECTION_RIGHT:
136 return "right";
137 case GESTURE_DIRECTION_INWARD:
138 return "inward";
139 case GESTURE_DIRECTION_OUTWARD:
140 return "outward";
141 case GESTURE_DIRECTION_CLOCKWISE:
142 return "clockwise";
143 case GESTURE_DIRECTION_COUNTERCLOCKWISE:
144 return "counterclockwise";
145 }
146
147 return NULL;
148}
149
150// Helper to turn combination of directions flags into string representation.
151static char *gesture_directions_to_string(uint32_t directions) {
152 char *result = NULL;
153
154 for (uint8_t bit = 0; bit < 32; bit++) {
155 uint32_t masked = directions & (1 << bit);
156 if (masked) {
157 const char *name = gesture_direction_string(masked);
158
159 if (!name) {
160 name = "unknown";
161 }
162
163 if (!result) {
164 result = strdup(name);
165 } else {
166 char *new = strformat("%s+%s", result, name);
167 free(result);
168 result = new;
169 }
170 }
171 }
172
173 if(!result) {
174 return strdup("any");
175 }
176
177 return result;
178}
179
180char *gesture_to_string(struct gesture *gesture) {
181 char *directions = gesture_directions_to_string(gesture->directions);
182 char *result = strformat("%s:%u:%s",
183 gesture_type_string(gesture->type),
184 gesture->fingers, directions);
185 free(directions);
186 return result;
187}
188
189bool gesture_check(struct gesture *target, enum gesture_type type, uint8_t fingers) {
190 // Check that gesture type matches
191 if (target->type != type) {
192 return false;
193 }
194
195 // Check that finger count matches
196 if (target->fingers != GESTURE_FINGERS_ANY && target->fingers != fingers) {
197 return false;
198 }
199
200 return true;
201}
202
203bool gesture_match(struct gesture *target, struct gesture *to_match, bool exact) {
204 // Check type and fingers
205 if (!gesture_check(target, to_match->type, to_match->fingers)) {
206 return false;
207 }
208
209 // Enforce exact matches ...
210 if (exact && target->directions != to_match->directions) {
211 return false;
212 }
213
214 // ... or ensure all target directions are matched
215 return (target->directions & to_match->directions) == target->directions;
216}
217
218bool gesture_equal(struct gesture *a, struct gesture *b) {
219 return a->type == b->type
220 && a->fingers == b->fingers
221 && a->directions == b->directions;
222}
223
224// Return count of set bits in directions bit field.
225static uint8_t gesture_directions_count(uint32_t directions) {
226 uint8_t count = 0;
227 for (; directions; directions >>= 1) {
228 count += directions & 1;
229 }
230 return count;
231}
232
233// Compare direction bit count of two direction.
234static int8_t gesture_directions_compare(uint32_t a, uint32_t b) {
235 return gesture_directions_count(a) - gesture_directions_count(b);
236}
237
238// Compare two direction based on their direction bit count
239int8_t gesture_compare(struct gesture *a, struct gesture *b) {
240 return gesture_directions_compare(a->directions, b->directions);
241}
242
243void gesture_tracker_begin(struct gesture_tracker *tracker,
244 enum gesture_type type, uint8_t fingers) {
245 tracker->type = type;
246 tracker->fingers = fingers;
247
248 tracker->dx = 0.0;
249 tracker->dy = 0.0;
250 tracker->scale = 1.0;
251 tracker->rotation = 0.0;
252
253 sway_log(SWAY_DEBUG, "begin tracking %s:%u:? gesture",
254 gesture_type_string(type), fingers);
255}
256
257bool gesture_tracker_check(struct gesture_tracker *tracker, enum gesture_type type) {
258 return tracker->type == type;
259}
260
261void gesture_tracker_update(struct gesture_tracker *tracker,
262 double dx, double dy, double scale, double rotation) {
263 if (tracker->type == GESTURE_TYPE_HOLD) {
264 sway_assert(false, "hold does not update.");
265 return;
266 }
267
268 tracker->dx += dx;
269 tracker->dy += dy;
270
271 if (tracker->type == GESTURE_TYPE_PINCH) {
272 tracker->scale = scale;
273 tracker->rotation += rotation;
274 }
275
276 sway_log(SWAY_DEBUG, "update tracking %s:%u:? gesture: %f %f %f %f",
277 gesture_type_string(tracker->type),
278 tracker->fingers,
279 tracker->dx, tracker->dy,
280 tracker->scale, tracker->rotation);
281}
282
283void gesture_tracker_cancel(struct gesture_tracker *tracker) {
284 sway_log(SWAY_DEBUG, "cancel tracking %s:%u:? gesture",
285 gesture_type_string(tracker->type), tracker->fingers);
286
287 tracker->type = GESTURE_TYPE_NONE;
288}
289
290struct gesture *gesture_tracker_end(struct gesture_tracker *tracker) {
291 struct gesture *result = calloc(1, sizeof(struct gesture));
292
293 // Ignore gesture under some threshold
294 // TODO: Make configurable
295 const double min_rotation = 5;
296 const double min_scale_delta = 0.1;
297
298 // Determine direction
299 switch(tracker->type) {
300 // Gestures with scale and rotation
301 case GESTURE_TYPE_PINCH:
302 if (tracker->rotation > min_rotation) {
303 result->directions |= GESTURE_DIRECTION_CLOCKWISE;
304 }
305 if (tracker->rotation < -min_rotation) {
306 result->directions |= GESTURE_DIRECTION_COUNTERCLOCKWISE;
307 }
308
309 if (tracker->scale > (1.0 + min_scale_delta)) {
310 result->directions |= GESTURE_DIRECTION_OUTWARD;
311 }
312 if (tracker->scale < (1.0 - min_scale_delta)) {
313 result->directions |= GESTURE_DIRECTION_INWARD;
314 }
315 __attribute__ ((fallthrough));
316 // Gestures with dx and dy
317 case GESTURE_TYPE_SWIPE:
318 if (fabs(tracker->dx) > fabs(tracker->dy)) {
319 if (tracker->dx > 0) {
320 result->directions |= GESTURE_DIRECTION_RIGHT;
321 } else {
322 result->directions |= GESTURE_DIRECTION_LEFT;
323 }
324 } else {
325 if (tracker->dy > 0) {
326 result->directions |= GESTURE_DIRECTION_DOWN;
327 } else {
328 result->directions |= GESTURE_DIRECTION_UP;
329 }
330 }
331 // Gesture without any direction
332 case GESTURE_TYPE_HOLD:
333 break;
334 // Not tracking any gesture
335 case GESTURE_TYPE_NONE:
336 sway_assert(false, "Not tracking any gesture.");
337 return result;
338 }
339
340 result->type = tracker->type;
341 result->fingers = tracker->fingers;
342
343 char *description = gesture_to_string(result);
344 sway_log(SWAY_DEBUG, "end tracking gesture: %s", description);
345 free(description);
346
347 tracker->type = GESTURE_TYPE_NONE;
348
349 return result;
350}
diff --git a/common/meson.build b/common/meson.build
index c653dd72..3756075a 100644
--- a/common/meson.build
+++ b/common/meson.build
@@ -3,6 +3,7 @@ lib_sway_common = static_library(
3 files( 3 files(
4 'background-image.c', 4 'background-image.c',
5 'cairo.c', 5 'cairo.c',
6 'gesture.c',
6 'ipc-client.c', 7 'ipc-client.c',
7 'log.c', 8 'log.c',
8 'loop.c', 9 'loop.c',