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