aboutsummaryrefslogtreecommitdiffstats
path: root/common/gesture.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/gesture.c')
-rw-r--r--common/gesture.c32
1 files changed, 7 insertions, 25 deletions
diff --git a/common/gesture.c b/common/gesture.c
index 8c2efe99..272aa837 100644
--- a/common/gesture.c
+++ b/common/gesture.c
@@ -1,4 +1,3 @@
1#define _POSIX_C_SOURCE 200809L
2#include "gesture.h" 1#include "gesture.h"
3 2
4#include <math.h> 3#include <math.h>
@@ -12,23 +11,6 @@
12 11
13const uint8_t GESTURE_FINGERS_ANY = 0; 12const uint8_t GESTURE_FINGERS_ANY = 0;
14 13
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) { 14char *gesture_parse(const char *input, struct gesture *output) {
33 // Clear output in case of failure 15 // Clear output in case of failure
34 output->type = GESTURE_TYPE_NONE; 16 output->type = GESTURE_TYPE_NONE;
@@ -38,7 +20,7 @@ char *gesture_parse(const char *input, struct gesture *output) {
38 // Split input type, fingers and directions 20 // Split input type, fingers and directions
39 list_t *split = split_string(input, ":"); 21 list_t *split = split_string(input, ":");
40 if (split->length < 1 || split->length > 3) { 22 if (split->length < 1 || split->length > 3) {
41 return strformat( 23 return format_str(
42 "expected <gesture>[:<fingers>][:direction], got %s", 24 "expected <gesture>[:<fingers>][:direction], got %s",
43 input); 25 input);
44 } 26 }
@@ -51,8 +33,8 @@ char *gesture_parse(const char *input, struct gesture *output) {
51 } else if (strcmp(split->items[0], "swipe") == 0) { 33 } else if (strcmp(split->items[0], "swipe") == 0) {
52 output->type = GESTURE_TYPE_SWIPE; 34 output->type = GESTURE_TYPE_SWIPE;
53 } else { 35 } else {
54 return strformat("expected hold|pinch|swipe, got %s", 36 return format_str("expected hold|pinch|swipe, got %s",
55 split->items[0]); 37 (const char *)split->items[0]);
56 } 38 }
57 39
58 // Parse optional arguments 40 // Parse optional arguments
@@ -67,7 +49,7 @@ char *gesture_parse(const char *input, struct gesture *output) {
67 next = split->length == 3 ? split->items[2] : NULL; 49 next = split->length == 3 ? split->items[2] : NULL;
68 } else if (split->length == 3) { 50 } else if (split->length == 3) {
69 // Fail here if argument can only be finger count 51 // Fail here if argument can only be finger count
70 return strformat("expected 1-9, got %s", next); 52 return format_str("expected 1-9, got %s", next);
71 } 53 }
72 54
73 // If there is an argument left, try to parse as direction 55 // If there is an argument left, try to parse as direction
@@ -95,7 +77,7 @@ char *gesture_parse(const char *input, struct gesture *output) {
95 } else if (strcmp(item, "counterclockwise") == 0) { 77 } else if (strcmp(item, "counterclockwise") == 0) {
96 output->directions |= GESTURE_DIRECTION_COUNTERCLOCKWISE; 78 output->directions |= GESTURE_DIRECTION_COUNTERCLOCKWISE;
97 } else { 79 } else {
98 return strformat("expected direction, got %s", item); 80 return format_str("expected direction, got %s", item);
99 } 81 }
100 } 82 }
101 list_free_items_and_destroy(directions); 83 list_free_items_and_destroy(directions);
@@ -163,7 +145,7 @@ static char *gesture_directions_to_string(uint32_t directions) {
163 if (!result) { 145 if (!result) {
164 result = strdup(name); 146 result = strdup(name);
165 } else { 147 } else {
166 char *new = strformat("%s+%s", result, name); 148 char *new = format_str("%s+%s", result, name);
167 free(result); 149 free(result);
168 result = new; 150 result = new;
169 } 151 }
@@ -179,7 +161,7 @@ static char *gesture_directions_to_string(uint32_t directions) {
179 161
180char *gesture_to_string(struct gesture *gesture) { 162char *gesture_to_string(struct gesture *gesture) {
181 char *directions = gesture_directions_to_string(gesture->directions); 163 char *directions = gesture_directions_to_string(gesture->directions);
182 char *result = strformat("%s:%u:%s", 164 char *result = format_str("%s:%u:%s",
183 gesture_type_string(gesture->type), 165 gesture_type_string(gesture->type),
184 gesture->fingers, directions); 166 gesture->fingers, directions);
185 free(directions); 167 free(directions);