summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorReza Jelveh <[email protected]>2024-04-15 13:39:41 +0800
committerGitHub <[email protected]>2024-04-15 01:39:41 -0400
commitfb86ed6b0588dfdebfb66ce875bc63cfa0a897f6 (patch)
tree29857a1769107adc58696f08d379f608aa4e29a2 /common
parenta5e79676c4bd22fc5902182acf0667907202a465 (diff)
feat: 1.9 merge (#277)
Co-authored-by: William McKinnon <[email protected]> Co-authored-by: Erik Reider <[email protected]>
Diffstat (limited to 'common')
-rw-r--r--common/gesture.c31
-rw-r--r--common/pango.c20
-rw-r--r--common/stringop.c33
3 files changed, 44 insertions, 40 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 @@
const uint8_t GESTURE_FINGERS_ANY = 0;
-// Helper to easily allocate and format string
-static char *strformat(const char *format, ...) {
- va_list args;
- va_start(args, format);
- int length = vsnprintf(NULL, 0, format, args) + 1;
- va_end(args);
-
- char *result = malloc(length);
- if (result) {
- va_start(args, format);
- vsnprintf(result, length, format, args);
- va_end(args);
- }
-
- return result;
-}
-
char *gesture_parse(const char *input, struct gesture *output) {
// Clear output in case of failure
output->type = GESTURE_TYPE_NONE;
@@ -38,7 +21,7 @@ char *gesture_parse(const char *input, struct gesture *output) {
// Split input type, fingers and directions
list_t *split = split_string(input, ":");
if (split->length < 1 || split->length > 3) {
- return strformat(
+ return format_str(
"expected <gesture>[:<fingers>][:direction], got %s",
input);
}
@@ -51,8 +34,8 @@ char *gesture_parse(const char *input, struct gesture *output) {
} else if (strcmp(split->items[0], "swipe") == 0) {
output->type = GESTURE_TYPE_SWIPE;
} else {
- return strformat("expected hold|pinch|swipe, got %s",
- split->items[0]);
+ return format_str("expected hold|pinch|swipe, got %s",
+ (const char *)split->items[0]);
}
// Parse optional arguments
@@ -67,7 +50,7 @@ char *gesture_parse(const char *input, struct gesture *output) {
next = split->length == 3 ? split->items[2] : NULL;
} else if (split->length == 3) {
// Fail here if argument can only be finger count
- return strformat("expected 1-9, got %s", next);
+ return format_str("expected 1-9, got %s", next);
}
// If there is an argument left, try to parse as direction
@@ -95,7 +78,7 @@ char *gesture_parse(const char *input, struct gesture *output) {
} else if (strcmp(item, "counterclockwise") == 0) {
output->directions |= GESTURE_DIRECTION_COUNTERCLOCKWISE;
} else {
- return strformat("expected direction, got %s", item);
+ return format_str("expected direction, got %s", item);
}
}
list_free_items_and_destroy(directions);
@@ -163,7 +146,7 @@ static char *gesture_directions_to_string(uint32_t directions) {
if (!result) {
result = strdup(name);
} else {
- char *new = strformat("%s+%s", result, name);
+ char *new = format_str("%s+%s", result, name);
free(result);
result = new;
}
@@ -179,7 +162,7 @@ static char *gesture_directions_to_string(uint32_t directions) {
char *gesture_to_string(struct gesture *gesture) {
char *directions = gesture_directions_to_string(gesture->directions);
- char *result = strformat("%s:%u:%s",
+ char *result = format_str("%s:%u:%s",
gesture_type_string(gesture->type),
gesture->fingers, directions);
free(directions);
diff --git a/common/pango.c b/common/pango.c
index e04bf80f..288569b3 100644
--- a/common/pango.c
+++ b/common/pango.c
@@ -84,18 +84,11 @@ void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width,
int *baseline, double scale, bool markup, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
- // Add one since vsnprintf excludes null terminator.
- int length = vsnprintf(NULL, 0, fmt, args) + 1;
+ char *buf = vformat_str(fmt, args);
va_end(args);
-
- char *buf = malloc(length);
if (buf == NULL) {
- sway_log(SWAY_ERROR, "Failed to allocate memory");
return;
}
- va_start(args, fmt);
- vsnprintf(buf, length, fmt, args);
- va_end(args);
PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
pango_cairo_update_layout(cairo, layout);
@@ -104,6 +97,7 @@ void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width,
*baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
}
g_object_unref(layout);
+
free(buf);
}
@@ -125,18 +119,11 @@ void render_text(cairo_t *cairo, const PangoFontDescription *desc,
double scale, bool markup, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
- // Add one since vsnprintf excludes null terminator.
- int length = vsnprintf(NULL, 0, fmt, args) + 1;
+ char *buf = vformat_str(fmt, args);
va_end(args);
-
- char *buf = malloc(length);
if (buf == NULL) {
- sway_log(SWAY_ERROR, "Failed to allocate memory");
return;
}
- va_start(args, fmt);
- vsnprintf(buf, length, fmt, args);
- va_end(args);
PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
cairo_font_options_t *fo = cairo_font_options_create();
@@ -146,5 +133,6 @@ void render_text(cairo_t *cairo, const PangoFontDescription *desc,
pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout);
g_object_unref(layout);
+
free(buf);
}
diff --git a/common/stringop.c b/common/stringop.c
index 7fb3fe12..c503143a 100644
--- a/common/stringop.c
+++ b/common/stringop.c
@@ -1,5 +1,6 @@
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
+#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -328,3 +329,35 @@ bool expand_path(char **path) {
wordfree(&p);
return true;
}
+
+char *vformat_str(const char *fmt, va_list args) {
+ char *str = NULL;
+ va_list args_copy;
+ va_copy(args_copy, args);
+
+ int len = vsnprintf(NULL, 0, fmt, args);
+ if (len < 0) {
+ sway_log_errno(SWAY_ERROR, "vsnprintf(\"%s\") failed", fmt);
+ goto out;
+ }
+
+ str = malloc(len + 1);
+ if (str == NULL) {
+ sway_log_errno(SWAY_ERROR, "malloc() failed");
+ goto out;
+ }
+
+ vsnprintf(str, len + 1, fmt, args_copy);
+
+out:
+ va_end(args_copy);
+ return str;
+}
+
+char *format_str(const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ char *str = vformat_str(fmt, args);
+ va_end(args);
+ return str;
+}