util: use cleanup in the event printing helpers

Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1184>
This commit is contained in:
Peter Hutterer 2025-04-01 12:19:23 +10:00
parent bbbe757a26
commit d5e2bb1267

View file

@ -27,6 +27,7 @@
#include "libevdev/libevdev.h" #include "libevdev/libevdev.h"
#include "util-mem.h"
#include "util-strings.h" #include "util-strings.h"
#include "util-libinput.h" #include "util-libinput.h"
@ -176,12 +177,12 @@ static inline char *
print_device_options(struct libinput_device *dev) print_device_options(struct libinput_device *dev)
{ {
uint32_t scroll_methods, click_methods; uint32_t scroll_methods, click_methods;
char *tap = NULL, _autofree_ char *tap = NULL;
*scroll = NULL, _autofree_ char *scroll = NULL;
*clickm = NULL, _autofree_ char *clickm = NULL;
*dwt = NULL, _autofree_ char *dwt = NULL;
*dwtp = NULL, _autofree_ char *dwtp = NULL;
*pad = NULL; _autofree_ char *pad = NULL;
if (libinput_device_config_tap_get_finger_count(dev)) { if (libinput_device_config_tap_get_finger_count(dev)) {
tap = strdup_printf(" tap (dl %s)", tap = strdup_printf(" tap (dl %s)",
@ -222,7 +223,7 @@ print_device_options(struct libinput_device *dev)
libinput_device_tablet_pad_get_num_mode_groups(dev)); libinput_device_tablet_pad_get_num_mode_groups(dev));
} }
char *str = strdup_printf("%s%s%s%s%s%s%s%s%s", return strdup_printf("%s%s%s%s%s%s%s%s%s",
tap ? tap : "", tap ? tap : "",
libinput_device_config_left_handed_is_available(dev) ? " left" : "", libinput_device_config_left_handed_is_available(dev) ? " left" : "",
libinput_device_config_scroll_has_natural_scroll(dev) ? " scroll-nat" : "", libinput_device_config_scroll_has_natural_scroll(dev) ? " scroll-nat" : "",
@ -232,14 +233,6 @@ print_device_options(struct libinput_device *dev)
dwt ? dwt : "", dwt ? dwt : "",
dwtp ? dwtp : "", dwtp ? dwtp : "",
pad ? pad : ""); pad ? pad : "");
free(tap);
free(scroll);
free(clickm);
free(dwt);
free(dwtp);
free(pad);
return str;
} }
static char * static char *
@ -251,9 +244,9 @@ print_device_notify(struct libinput_event *ev)
double w, h; double w, h;
static int next_group_id = 0; static int next_group_id = 0;
intptr_t group_id; intptr_t group_id;
char *size = NULL, _autofree_ char *size = NULL;
*ntouches = NULL, _autofree_ char *ntouches = NULL;
*options = NULL; _autofree_ char *options = NULL;
group = libinput_device_get_device_group(dev); group = libinput_device_get_device_group(dev);
group_id = (intptr_t)libinput_device_group_get_user_data(group); group_id = (intptr_t)libinput_device_group_get_user_data(group);
@ -272,7 +265,7 @@ print_device_notify(struct libinput_event *ev)
if (libinput_event_get_type(ev) == LIBINPUT_EVENT_DEVICE_ADDED) if (libinput_event_get_type(ev) == LIBINPUT_EVENT_DEVICE_ADDED)
options = print_device_options(dev); options = print_device_options(dev);
char *str = strdup_printf("%-33s %5s %7s group%-2d cap:%s%s%s%s%s%s%s%s%s%s", return strdup_printf("%-33s %5s %7s group%-2d cap:%s%s%s%s%s%s%s%s%s%s",
libinput_device_get_name(dev), libinput_device_get_name(dev),
libinput_seat_get_physical_name(seat), libinput_seat_get_physical_name(seat),
libinput_seat_get_logical_name(seat), libinput_seat_get_logical_name(seat),
@ -287,11 +280,6 @@ print_device_notify(struct libinput_event *ev)
size ? size : "", size ? size : "",
ntouches ? ntouches : "", ntouches ? ntouches : "",
options ? options : ""); options ? options : "");
free(size);
free(ntouches);
free(options);
return str;
} }
static char * static char *
@ -378,12 +366,12 @@ print_tablet_axes(struct libinput_event_tablet_tool *t)
{ {
struct libinput_tablet_tool *tool = libinput_event_tablet_tool_get_tool(t); struct libinput_tablet_tool *tool = libinput_event_tablet_tool_get_tool(t);
double x, y; double x, y;
char *tilt = NULL, _autofree_ char *tilt = NULL;
*distance = NULL, _autofree_ char *distance = NULL;
*rot = NULL, _autofree_ char *rot = NULL;
*whl = NULL, _autofree_ char *whl = NULL;
*sld = NULL, _autofree_ char *sld = NULL;
*size = NULL; _autofree_ char *size = NULL;
#define changed_sym(ev, ax) \ #define changed_sym(ev, ax) \
(libinput_event_tablet_tool_##ax##_has_changed(ev) ? "*" : "") (libinput_event_tablet_tool_##ax##_has_changed(ev) ? "*" : "")
@ -438,7 +426,7 @@ print_tablet_axes(struct libinput_event_tablet_tool *t)
x = libinput_event_tablet_tool_get_x(t); x = libinput_event_tablet_tool_get_x(t);
y = libinput_event_tablet_tool_get_y(t); y = libinput_event_tablet_tool_get_y(t);
char *str = strdup_printf("\t%.2f%s/%.2f%s%s%s%s%s%s%s", return strdup_printf("\t%.2f%s/%.2f%s%s%s%s%s%s%s",
x, changed_sym(t, x), x, changed_sym(t, x),
y, changed_sym(t, y), y, changed_sym(t, y),
tilt ? tilt : "", tilt ? tilt : "",
@ -447,14 +435,6 @@ print_tablet_axes(struct libinput_event_tablet_tool *t)
whl ? whl : "", whl ? whl : "",
sld ? sld : "", sld ? sld : "",
size ? size : ""); size ? size : "");
free(tilt);
free(distance);
free(rot);
free(whl);
free(sld);
free(size);
return str;
} }
static char * static char *
@ -466,16 +446,13 @@ print_tablet_tip_event(struct libinput_event *ev, const struct libinput_print_op
print_event_time(time, opts->start_time, libinput_event_tablet_tool_get_time(t)); print_event_time(time, opts->start_time, libinput_event_tablet_tool_get_time(t));
char *axes = print_tablet_axes(t); _autofree_ char *axes = print_tablet_axes(t);
state = libinput_event_tablet_tool_get_tip_state(t); state = libinput_event_tablet_tool_get_tip_state(t);
char *str = strdup_printf("%s\t%s %s", return strdup_printf("%s\t%s %s",
time, time,
axes, axes,
state == LIBINPUT_TABLET_TOOL_TIP_DOWN ? "down" : "up"); state == LIBINPUT_TABLET_TOOL_TIP_DOWN ? "down" : "up");
free(axes);
return str;
} }
static char * static char *
@ -559,11 +536,9 @@ print_tablet_axis_event(struct libinput_event *ev, const struct libinput_print_o
char time[16]; char time[16];
print_event_time(time, opts->start_time, libinput_event_tablet_tool_get_time(t)); print_event_time(time, opts->start_time, libinput_event_tablet_tool_get_time(t));
char *axes = print_tablet_axes(t); _autofree_ char *axes = print_tablet_axes(t);
char *str = strdup_printf("%s\t%s", time, axes); return strdup_printf("%s\t%s", time, axes);
free(axes);
return str;
} }
static char * static char *
@ -575,8 +550,8 @@ print_proximity_event(struct libinput_event *ev, const struct libinput_print_opt
const char *tool_str, const char *tool_str,
*state_str; *state_str;
char time[16]; char time[16];
char *axes = NULL, _autofree_ char *axes = NULL;
*proxin = NULL; _autofree_ char *proxin = NULL;
switch (libinput_tablet_tool_get_type(tool)) { switch (libinput_tablet_tool_get_type(tool)) {
case LIBINPUT_TABLET_TOOL_TYPE_PEN: case LIBINPUT_TABLET_TOOL_TYPE_PEN:
@ -641,7 +616,7 @@ print_proximity_event(struct libinput_event *ev, const struct libinput_print_opt
libinput_tablet_tool_has_button(tool, BTN_0) ? "0" : ""); libinput_tablet_tool_has_button(tool, BTN_0) ? "0" : "");
} }
char *str = strdup_printf("%s\t%s\t%-8s (%#" PRIx64 ", id %#" PRIx64 ") %s%s", return strdup_printf("%s\t%s\t%-8s (%#" PRIx64 ", id %#" PRIx64 ") %s%s",
time, time,
axes ? axes : "", axes ? axes : "",
tool_str, tool_str,
@ -649,10 +624,6 @@ print_proximity_event(struct libinput_event *ev, const struct libinput_print_opt
libinput_tablet_tool_get_tool_id(tool), libinput_tablet_tool_get_tool_id(tool),
state_str, state_str,
proxin ? proxin : ""); proxin ? proxin : "");
free(axes);
free(proxin);
return str;
} }
static char * static char *
@ -661,8 +632,8 @@ print_touch_event(struct libinput_event *ev, const struct libinput_print_options
struct libinput_event_touch *t = libinput_event_get_touch_event(ev); struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
enum libinput_event_type type = libinput_event_get_type(ev); enum libinput_event_type type = libinput_event_get_type(ev);
char time[16]; char time[16];
char *slot = NULL, _autofree_ char *slot = NULL;
*pos = NULL; _autofree_ char *pos = NULL;
print_event_time(time, opts->start_time, libinput_event_touch_get_time(t)); print_event_time(time, opts->start_time, libinput_event_touch_get_time(t));
@ -682,13 +653,7 @@ print_touch_event(struct libinput_event *ev, const struct libinput_print_options
pos = strdup_printf(" %5.2f/%5.2f (%5.2f/%5.2fmm)", x, y, xmm, ymm); pos = strdup_printf(" %5.2f/%5.2f (%5.2f/%5.2fmm)", x, y, xmm, ymm);
} }
char *str = strdup_printf("%s\t%s%s", return strdup_printf("%s\t%s%s", time, slot ? slot : "", pos ? pos : "");
time,
slot ? slot : "",
pos ? pos : "");
free(slot);
free(pos);
return str;
} }
static char * static char *
@ -720,7 +685,7 @@ print_gesture_event_with_coords(struct libinput_event *ev, const struct libinput
double dx_unaccel = libinput_event_gesture_get_dx_unaccelerated(t); double dx_unaccel = libinput_event_gesture_get_dx_unaccelerated(t);
double dy_unaccel = libinput_event_gesture_get_dy_unaccelerated(t); double dy_unaccel = libinput_event_gesture_get_dy_unaccelerated(t);
char time[16]; char time[16];
char *pinch = NULL; _autofree_ char *pinch = NULL;
print_event_time(time, opts->start_time, libinput_event_gesture_get_time(t)); print_event_time(time, opts->start_time, libinput_event_gesture_get_time(t));
@ -732,13 +697,11 @@ print_gesture_event_with_coords(struct libinput_event *ev, const struct libinput
pinch = strdup_printf(" %5.2f @ %5.2f", scale, angle); pinch = strdup_printf(" %5.2f @ %5.2f", scale, angle);
} }
char *str = strdup_printf("%s\t%d %5.2f/%5.2f (%5.2f/%5.2f unaccelerated)%s", return strdup_printf("%s\t%d %5.2f/%5.2f (%5.2f/%5.2f unaccelerated)%s",
time, time,
libinput_event_gesture_get_finger_count(t), libinput_event_gesture_get_finger_count(t),
dx, dy, dx_unaccel, dy_unaccel, dx, dy, dx_unaccel, dy_unaccel,
pinch ? pinch : ""); pinch ? pinch : "");
free(pinch);
return str;
} }
static char * static char *
@ -749,7 +712,7 @@ print_tablet_pad_button_event(struct libinput_event *ev, const struct libinput_p
enum libinput_button_state state; enum libinput_button_state state;
unsigned int button, mode; unsigned int button, mode;
char time[16]; char time[16];
char *toggle = NULL; const char *toggle = NULL;
print_event_time(time, opts->start_time, libinput_event_tablet_pad_get_time(p)); print_event_time(time, opts->start_time, libinput_event_tablet_pad_get_time(p));
@ -900,8 +863,8 @@ libinput_event_to_str(struct libinput_event *ev,
const struct libinput_print_options *options) const struct libinput_print_options *options)
{ {
enum libinput_event_type type = libinput_event_get_type(ev); enum libinput_event_type type = libinput_event_get_type(ev);
char *event_header = print_event_header(ev, event_repeat_count); _autofree_ char *event_header = print_event_header(ev, event_repeat_count);
char *event_str = NULL; _autofree_ char *event_str = NULL;
struct libinput_print_options opts = { struct libinput_print_options opts = {
.start_time = options ? options->start_time : 0, .start_time = options ? options->start_time : 0,
@ -1005,8 +968,5 @@ libinput_event_to_str(struct libinput_event *ev,
break; break;
} }
char *str = strdup_printf("%s %s", event_header, event_str); return strdup_printf("%s %s", event_header, event_str);
free(event_header);
free(event_str);
return str;
} }