diff options
author | Simon Ser <contact@emersion.fr> | 2023-02-28 16:43:05 +0100 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2023-04-14 18:34:54 +0200 |
commit | 08c1946d71039e583696842c3558b337aede1cbf (patch) | |
tree | 3873db2edfb31146bd6cd17dae63f068aef34f05 /sway/input | |
parent | ac8962eb626a8c715241ae91646f2d1bfcb61533 (diff) |
Use format_str() throughout
Diffstat (limited to 'sway/input')
-rw-r--r-- | sway/input/cursor.c | 15 | ||||
-rw-r--r-- | sway/input/input-manager.c | 10 | ||||
-rw-r--r-- | sway/input/keyboard.c | 29 |
3 files changed, 10 insertions, 44 deletions
diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 15687993..75d055cd 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -1273,11 +1273,7 @@ uint32_t get_mouse_bindsym(const char *name, char **error) { // Get event code from name int code = libevdev_event_code_from_name(EV_KEY, name); if (code == -1) { - size_t len = snprintf(NULL, 0, "Unknown event %s", name) + 1; - *error = malloc(len); - if (*error) { - snprintf(*error, len, "Unknown event %s", name); - } + *error = format_str("Unknown event %s", name); return 0; } return code; @@ -1299,13 +1295,8 @@ uint32_t get_mouse_bindcode(const char *name, char **error) { } const char *event = libevdev_event_code_get_name(EV_KEY, code); if (!event || strncmp(event, "BTN_", strlen("BTN_")) != 0) { - size_t len = snprintf(NULL, 0, "Event code %d (%s) is not a button", - code, event ? event : "(null)") + 1; - *error = malloc(len); - if (*error) { - snprintf(*error, len, "Event code %d (%s) is not a button", - code, event ? event : "(null)"); - } + *error = format_str("Event code %d (%s) is not a button", + code, event ? event : "(null)"); return 0; } return code; diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index ea2cc038..1115ba5e 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -80,15 +80,7 @@ char *input_device_get_identifier(struct wlr_input_device *device) { } } - const char *fmt = "%d:%d:%s"; - int len = snprintf(NULL, 0, fmt, vendor, product, name) + 1; - char *identifier = malloc(len); - if (!identifier) { - sway_log(SWAY_ERROR, "Unable to allocate unique input device name"); - return NULL; - } - - snprintf(identifier, len, fmt, vendor, product, name); + char *identifier = format_str("%d:%d:%s", vendor, product, name); free(name); return identifier; } diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index 45a588ec..c3bf4fbb 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -717,23 +717,11 @@ struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, static void handle_xkb_context_log(struct xkb_context *context, enum xkb_log_level level, const char *format, va_list args) { - va_list args_copy; - va_copy(args_copy, args); - size_t length = vsnprintf(NULL, 0, format, args_copy) + 1; - va_end(args_copy); - - char *error = malloc(length); - if (!error) { - sway_log(SWAY_ERROR, "Failed to allocate libxkbcommon log message"); - return; - } + char *error = vformat_str(format, args); - va_copy(args_copy, args); - vsnprintf(error, length, format, args_copy); - va_end(args_copy); - - if (error[length - 2] == '\n') { - error[length - 2] = '\0'; + size_t len = strlen(error); + if (error[len - 1] == '\n') { + error[len - 1] = '\0'; } sway_log_importance_t importance = SWAY_DEBUG; @@ -768,13 +756,8 @@ struct xkb_keymap *sway_keyboard_compile_keymap(struct input_config *ic, if (!keymap_file) { sway_log_errno(SWAY_ERROR, "cannot read xkb file %s", ic->xkb_file); if (error) { - size_t len = snprintf(NULL, 0, "cannot read xkb file %s: %s", - ic->xkb_file, strerror(errno)) + 1; - *error = malloc(len); - if (*error) { - snprintf(*error, len, "cannot read xkb_file %s: %s", - ic->xkb_file, strerror(errno)); - } + *error = format_str("cannot read xkb file %s: %s", + ic->xkb_file, strerror(errno)); } goto cleanup; } |