backend/libinput: guard against new enum entries

When libinput introduces new enum entries, we'd abort or send bogus
events to the compositor. Instead, log a message and ignore the
event.

Keep all enums without a default case so that the compiler warns
when we're missing a case.
This commit is contained in:
Simon Ser 2026-02-12 22:37:41 +01:00 committed by Simon Zeni
parent 25f94c5965
commit 884d29e5f3
7 changed files with 179 additions and 89 deletions

View file

@ -78,27 +78,61 @@ struct wlr_libinput_input_device *device_from_tablet(
return dev;
}
static enum wlr_tablet_tool_type wlr_type_from_libinput_type(
enum libinput_tablet_tool_type value) {
switch (value) {
static bool type_from_libinput(enum libinput_tablet_tool_type type,
enum wlr_tablet_tool_type *out) {
switch (type) {
case LIBINPUT_TABLET_TOOL_TYPE_PEN:
return WLR_TABLET_TOOL_TYPE_PEN;
*out = WLR_TABLET_TOOL_TYPE_PEN;
return true;
case LIBINPUT_TABLET_TOOL_TYPE_ERASER:
return WLR_TABLET_TOOL_TYPE_ERASER;
*out = WLR_TABLET_TOOL_TYPE_ERASER;
return true;
case LIBINPUT_TABLET_TOOL_TYPE_BRUSH:
return WLR_TABLET_TOOL_TYPE_BRUSH;
*out = WLR_TABLET_TOOL_TYPE_BRUSH;
return true;
case LIBINPUT_TABLET_TOOL_TYPE_PENCIL:
return WLR_TABLET_TOOL_TYPE_PENCIL;
*out = WLR_TABLET_TOOL_TYPE_PENCIL;
return true;
case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH:
return WLR_TABLET_TOOL_TYPE_AIRBRUSH;
*out = WLR_TABLET_TOOL_TYPE_AIRBRUSH;
return true;
case LIBINPUT_TABLET_TOOL_TYPE_MOUSE:
return WLR_TABLET_TOOL_TYPE_MOUSE;
*out = WLR_TABLET_TOOL_TYPE_MOUSE;
return true;
case LIBINPUT_TABLET_TOOL_TYPE_LENS:
return WLR_TABLET_TOOL_TYPE_LENS;
*out = WLR_TABLET_TOOL_TYPE_LENS;
return true;
case LIBINPUT_TABLET_TOOL_TYPE_TOTEM:
return WLR_TABLET_TOOL_TYPE_TOTEM;
*out = WLR_TABLET_TOOL_TYPE_TOTEM;
return true;
}
abort(); // unreachable
return false;
}
static bool proximity_state_from_libinput(enum libinput_tablet_tool_proximity_state state,
enum wlr_tablet_tool_proximity_state *out) {
switch (state) {
case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT:
*out = WLR_TABLET_TOOL_PROXIMITY_OUT;
return true;
case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN:
*out = WLR_TABLET_TOOL_PROXIMITY_IN;
return true;
}
return false;
}
static bool tip_state_from_libinput(enum libinput_tablet_tool_tip_state state,
enum wlr_tablet_tool_tip_state *out) {
switch (state) {
case LIBINPUT_TABLET_TOOL_TIP_UP:
*out = WLR_TABLET_TOOL_TIP_UP;
return true;
case LIBINPUT_TABLET_TOOL_TIP_DOWN:
*out = WLR_TABLET_TOOL_TIP_DOWN;
return true;
}
return false;
}
static struct tablet_tool *get_tablet_tool(
@ -110,14 +144,19 @@ static struct tablet_tool *get_tablet_tool(
return tool;
}
enum wlr_tablet_tool_type type;
if (!type_from_libinput(libinput_tablet_tool_get_type(libinput_tool), &type)) {
wlr_log(WLR_DEBUG, "Unhandled libinput tablet tool type");
return NULL;
}
tool = calloc(1, sizeof(*tool));
if (tool == NULL) {
wlr_log_errno(WLR_ERROR, "failed to allocate wlr_libinput_tablet_tool");
return NULL;
}
tool->wlr_tool.type = wlr_type_from_libinput_type(
libinput_tablet_tool_get_type(libinput_tool));
tool->wlr_tool.type = type;
tool->wlr_tool.hardware_serial =
libinput_tablet_tool_get_serial(libinput_tool);
tool->wlr_tool.hardware_wacom =
@ -209,14 +248,12 @@ void handle_tablet_tool_proximity(struct libinput_event *event,
.y = libinput_event_tablet_tool_get_y_transformed(tevent, 1),
};
switch (libinput_event_tablet_tool_get_proximity_state(tevent)) {
case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT:
wlr_event.state = WLR_TABLET_TOOL_PROXIMITY_OUT;
break;
case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN:
wlr_event.state = WLR_TABLET_TOOL_PROXIMITY_IN;
break;
if (!proximity_state_from_libinput(libinput_event_tablet_tool_get_proximity_state(tevent),
&wlr_event.state)) {
wlr_log(WLR_DEBUG, "Unhandled libinput tablet tool proximity state");
return;
}
wl_signal_emit_mutable(&wlr_tablet->events.proximity, &wlr_event);
if (libinput_event_tablet_tool_get_proximity_state(tevent) ==
@ -251,14 +288,11 @@ void handle_tablet_tool_tip(struct libinput_event *event,
.y = libinput_event_tablet_tool_get_y_transformed(tevent, 1),
};
switch (libinput_event_tablet_tool_get_tip_state(tevent)) {
case LIBINPUT_TABLET_TOOL_TIP_UP:
wlr_event.state = WLR_TABLET_TOOL_TIP_UP;
break;
case LIBINPUT_TABLET_TOOL_TIP_DOWN:
wlr_event.state = WLR_TABLET_TOOL_TIP_DOWN;
break;
if (!tip_state_from_libinput(libinput_event_tablet_tool_get_tip_state(tevent), &wlr_event.state)) {
wlr_log(WLR_DEBUG, "Unhandled libinput tablet tool tip state");
return;
}
wl_signal_emit_mutable(&wlr_tablet->events.tip, &wlr_event);
}
@ -277,13 +311,11 @@ void handle_tablet_tool_button(struct libinput_event *event,
.time_msec = usec_to_msec(libinput_event_tablet_tool_get_time_usec(tevent)),
.button = libinput_event_tablet_tool_get_button(tevent),
};
switch (libinput_event_tablet_tool_get_button_state(tevent)) {
case LIBINPUT_BUTTON_STATE_RELEASED:
wlr_event.state = WLR_BUTTON_RELEASED;
break;
case LIBINPUT_BUTTON_STATE_PRESSED:
wlr_event.state = WLR_BUTTON_PRESSED;
break;
if (!button_state_from_libinput(libinput_event_tablet_tool_get_button_state(tevent), &wlr_event.state)) {
wlr_log(WLR_DEBUG, "Unhandled libinput button state");
return;
}
wl_signal_emit_mutable(&wlr_tablet->events.button, &wlr_event);
}