diff options
author | Michael Olbrich <m.olbrich@pengutronix.de> | 2021-01-20 09:28:02 +0100 |
---|---|---|
committer | Daniel Stone <daniels@collabora.com> | 2023-03-31 12:10:26 +0000 |
commit | 76ada6fe046a876413af78610ed7e49074400f3a (patch) | |
tree | 81a530969d36f330a6f016a4c7cc3fcc03e1b33d | |
parent | 818c1c275cc8a2275fc80f52dd0493a35703cb02 (diff) | |
download | weston-76ada6fe046a876413af78610ed7e49074400f3a.tar.gz |
clients/window: add support for handling tablets
Based on a patch from:
Peter Hutterer <peter.hutterer@who-t.net>
Lyude Paul <thatslyude@gmail.com>
Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
-rw-r--r-- | clients/meson.build | 2 | ||||
-rw-r--r-- | clients/window.c | 506 | ||||
-rw-r--r-- | clients/window.h | 87 |
3 files changed, 595 insertions, 0 deletions
diff --git a/clients/meson.build b/clients/meson.build index 4df4a63c..359dea80 100644 --- a/clients/meson.build +++ b/clients/meson.build @@ -12,6 +12,8 @@ srcs_toytoolkit = [ relative_pointer_unstable_v1_protocol_c, pointer_constraints_unstable_v1_client_protocol_h, pointer_constraints_unstable_v1_protocol_c, + tablet_unstable_v2_client_protocol_h, + tablet_unstable_v2_protocol_c, ivi_application_client_protocol_h, ivi_application_protocol_c, viewporter_client_protocol_h, diff --git a/clients/window.c b/clients/window.c index 1dfa43c6..e5138d54 100644 --- a/clients/window.c +++ b/clients/window.c @@ -59,6 +59,7 @@ #include "text-cursor-position-client-protocol.h" #include "pointer-constraints-unstable-v1-client-protocol.h" #include "relative-pointer-unstable-v1-client-protocol.h" +#include "tablet-unstable-v2-client-protocol.h" #include "shared/os-compatibility.h" #include "shared/string-helpers.h" #include "libweston/matrix.h" @@ -89,6 +90,7 @@ struct display { struct wl_data_device_manager *data_device_manager; struct text_cursor_position *text_cursor_position; struct xdg_wm_base *xdg_shell; + struct zwp_tablet_manager_v2 *tablet_manager; struct zwp_relative_pointer_manager_v1 *relative_pointer_manager; struct zwp_pointer_constraints_v1 *pointer_constraints; uint32_t serial; @@ -128,6 +130,33 @@ struct display { struct wp_viewporter *viewporter; }; +struct tablet { + struct zwp_tablet_v2 *tablet; + char *name; + int32_t vid; + int32_t pid; + + void *user_data; + + struct wl_list link; +}; + +struct tablet_tool { + struct zwp_tablet_tool_v2 *tool; + struct input *input; + void *user_data; + struct wl_list link; + struct tablet *current_tablet; + struct window *focus; + struct widget *focus_widget; + + enum zwp_tablet_tool_v2_type type; + uint64_t serial; + uint64_t hwid; + + double sx, sy; +}; + struct window_output { struct output *output; struct wl_list link; @@ -279,6 +308,19 @@ struct widget { widget_axis_source_handler_t axis_source_handler; widget_axis_stop_handler_t axis_stop_handler; widget_axis_discrete_handler_t axis_discrete_handler; + widget_tablet_tool_motion_handler_t tablet_tool_motion_handler; + widget_tablet_tool_up_handler_t tablet_tool_up_handler; + widget_tablet_tool_down_handler_t tablet_tool_down_handler; + widget_tablet_tool_pressure_handler_t tablet_tool_pressure_handler; + widget_tablet_tool_distance_handler_t tablet_tool_distance_handler; + widget_tablet_tool_tilt_handler_t tablet_tool_tilt_handler; + widget_tablet_tool_rotation_handler_t tablet_tool_rotation_handler; + widget_tablet_tool_slider_handler_t tablet_tool_slider_handler; + widget_tablet_tool_wheel_handler_t tablet_tool_wheel_handler; + widget_tablet_tool_proximity_in_handler_t tablet_tool_prox_in_handler; + widget_tablet_tool_proximity_out_handler_t tablet_tool_prox_out_handler; + widget_tablet_tool_button_handler_t tablet_tool_button_handler; + widget_tablet_tool_frame_handler_t tablet_tool_frame_handler; void *user_data; int opaque; int tooltip_count; @@ -362,6 +404,10 @@ struct input { uint32_t repeat_key; uint32_t repeat_time; int seat_version; + + struct zwp_tablet_seat_v2 *tablet_seat; + struct wl_list tablet_list; + struct wl_list tablet_tool_list; }; struct output { @@ -1758,6 +1804,62 @@ widget_set_axis_handlers(struct widget *widget, widget->axis_discrete_handler = axis_discrete_handler; } +void +widget_set_tablet_tool_axis_handlers(struct widget *widget, + widget_tablet_tool_motion_handler_t motion, + widget_tablet_tool_pressure_handler_t pressure, + widget_tablet_tool_distance_handler_t distance, + widget_tablet_tool_tilt_handler_t tilt, + widget_tablet_tool_rotation_handler_t rotation, + widget_tablet_tool_slider_handler_t slider, + widget_tablet_tool_wheel_handler_t wheel) +{ + widget->tablet_tool_motion_handler = motion; + widget->tablet_tool_pressure_handler = pressure; + widget->tablet_tool_distance_handler = distance; + widget->tablet_tool_tilt_handler = tilt; + widget->tablet_tool_rotation_handler = rotation; + widget->tablet_tool_slider_handler = slider; + widget->tablet_tool_wheel_handler = wheel; +} + +void +widget_set_tablet_tool_up_handler(struct widget *widget, + widget_tablet_tool_up_handler_t handler) +{ + widget->tablet_tool_up_handler = handler; +} + +void +widget_set_tablet_tool_down_handler(struct widget *widget, + widget_tablet_tool_down_handler_t handler) +{ + widget->tablet_tool_down_handler = handler; +} + +void +widget_set_tablet_tool_proximity_handlers(struct widget *widget, + widget_tablet_tool_proximity_in_handler_t in_handler, + widget_tablet_tool_proximity_out_handler_t out_handler) +{ + widget->tablet_tool_prox_in_handler = in_handler; + widget->tablet_tool_prox_out_handler = out_handler; +} + +void +widget_set_tablet_tool_button_handler(struct widget *widget, + widget_tablet_tool_button_handler_t handler) +{ + widget->tablet_tool_button_handler = handler; +} + +void +widget_set_tablet_tool_frame_handler(struct widget *widget, + widget_tablet_tool_frame_handler_t handler) +{ + widget->tablet_tool_frame_handler = handler; +} + static void window_schedule_redraw_task(struct window *window); @@ -5679,6 +5781,9 @@ display_add_input(struct display *d, uint32_t id, int display_seat_version) wl_list_init(&input->touch_point_list); wl_list_insert(d->input_list.prev, &input->link); + wl_list_init(&input->tablet_list); + wl_list_init(&input->tablet_tool_list); + wl_seat_add_listener(input->seat, &seat_listener, input); wl_seat_set_user_data(input->seat, input); @@ -5781,6 +5886,405 @@ static const struct xdg_wm_base_listener wm_base_listener = { }; static void +tablet_handle_name(void *data, struct zwp_tablet_v2 *zwp_tablet_v2, const char *name) +{ + struct tablet *tablet = data; + + tablet->name = xstrdup(name); +} + +static void +tablet_handle_id(void *data, struct zwp_tablet_v2 *zwp_tablet_v2, + uint32_t vid, uint32_t pid) +{ + struct tablet *tablet = data; + + tablet->vid = vid; + tablet->pid = pid; +} + +static void +tablet_handle_path(void *data, struct zwp_tablet_v2 *zwp_tablet_v2, const char *path) +{ +} + +static void +tablet_handle_done(void *data, struct zwp_tablet_v2 *zwp_tablet_v2) +{ +} + +static void +tablet_handle_removed(void *data, struct zwp_tablet_v2 *zwp_tablet_v2) +{ + struct tablet *tablet = data; + + zwp_tablet_v2_destroy(zwp_tablet_v2); + + wl_list_remove(&tablet->link); + free(tablet->name); + free(tablet); +} + +static const struct zwp_tablet_v2_listener tablet_listener = { + tablet_handle_name, + tablet_handle_id, + tablet_handle_path, + tablet_handle_done, + tablet_handle_removed, +}; + +static void +tablet_added(void *data, struct zwp_tablet_seat_v2 *zwp_tablet_seat1, + struct zwp_tablet_v2 *id) +{ + struct input *input = data; + struct tablet *tablet; + + tablet = zalloc(sizeof *tablet); + + zwp_tablet_v2_add_listener(id, &tablet_listener, tablet); + wl_list_insert(&input->tablet_list, &tablet->link); + zwp_tablet_v2_set_user_data(id, tablet); +} + +uint32_t +tablet_tool_get_type(struct tablet_tool *tool) +{ + return tool->type; +} + +uint64_t +tablet_tool_get_serial(struct tablet_tool *tool) +{ + return tool->serial; +} + +uint64_t +tablet_tool_get_hwid(struct tablet_tool *tool) +{ + return tool->hwid; +} + +static void +tablet_tool_handle_type(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t tool_type) +{ + struct tablet_tool *tool = data; + + tool->type = tool_type; +} + +static void +tablet_tool_handle_serialid(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t serial_msb, uint32_t serial_lsb) +{ + struct tablet_tool *tool = data; + + tool->serial = ((uint64_t)serial_msb << 32) | serial_lsb; +} + +static void +tablet_tool_handle_hardware_id_wacom(void *data, + struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t hwid_msb, uint32_t hwid_lsb) +{ + struct tablet_tool *tool = data; + + tool->serial = ((uint64_t)hwid_msb << 32) | hwid_lsb; +} + +static void +tablet_tool_handle_capability(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t capability) +{ +} + +static void +tablet_tool_handle_done(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2) +{ +} + +static void +tablet_tool_handle_removed(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2) +{ + struct tablet_tool *tool = data; + + zwp_tablet_tool_v2_destroy(zwp_tablet_tool_v2); + wl_list_remove(&tool->link); + free(tool); +} + +static void +tablet_tool_set_focus_widget(struct tablet_tool *tool, struct window *window, + wl_fixed_t sx, wl_fixed_t sy) +{ + struct widget *widget, *old; + + if (tool->input->grab) + widget = tool->input->grab; + else + widget = window_find_widget(window, sx, sy); + + if (tool->focus_widget == widget) + return; + + old = tool->focus_widget; + if (old && old->tablet_tool_prox_out_handler) + old->tablet_tool_prox_out_handler(old, tool, + widget_get_user_data(old)); + + if (widget && widget->tablet_tool_prox_in_handler) + widget->tablet_tool_prox_in_handler(widget, tool, + tool->current_tablet, + widget_get_user_data(widget)); + + tool->focus_widget = widget; +} + +static void +tablet_tool_handle_proximity_in(void *data, + struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t serial, + struct zwp_tablet_v2 *zwp_tablet_v2, + struct wl_surface *surface) +{ + struct tablet_tool *tool = data; + struct tablet *tablet = zwp_tablet_v2_get_user_data(zwp_tablet_v2); + struct window *window; + + window = wl_surface_get_user_data(surface); + if (surface != window->main_surface->surface) + return; + + tool->focus = window; + tool->current_tablet = tablet; +} + +static void +tablet_tool_handle_proximity_out(void *data, + struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2) +{ + struct tablet_tool *tool = data; + + tool->focus = NULL; + tool->current_tablet = NULL; +} + +static void +tablet_tool_handle_down(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t serial) +{ + struct tablet_tool *tool = data; + struct widget *focus = tool->focus_widget; + + tool->input->display->serial = serial; + + if (focus && focus->tablet_tool_down_handler) + focus->tablet_tool_down_handler(focus, tool, focus->user_data); +} + +static void +tablet_tool_handle_up(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2) +{ + struct tablet_tool *tool = data; + struct widget *focus = tool->focus_widget; + + if (focus && focus->tablet_tool_up_handler) + focus->tablet_tool_up_handler(focus, tool, focus->user_data); +} + +static void +tablet_tool_handle_motion(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + wl_fixed_t x, wl_fixed_t y) +{ + struct tablet_tool *tool = data; + double sx = wl_fixed_to_double(x); + double sy = wl_fixed_to_double(y); + struct window *window = tool->focus; + struct widget *widget; + + if (!window) + return; + + tool->sx = sx; + tool->sy = sy; + + if (sx > window->main_surface->allocation.width || + sy > window->main_surface->allocation.height) + return; + + tablet_tool_set_focus_widget(tool, window, sx, sy); + widget = tool->focus_widget; + if (widget && widget->tablet_tool_motion_handler) { + widget->tablet_tool_motion_handler(widget, tool, sx, sy, + widget->user_data); + } +} + +static void +tablet_tool_handle_pressure(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t pressure) +{ + struct tablet_tool *tool = data; + struct widget *widget = tool->focus_widget; + + if (widget && widget->tablet_tool_pressure_handler) + widget->tablet_tool_pressure_handler(widget, tool, pressure, + widget->user_data); +} + +static void +tablet_tool_handle_distance(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t distance) +{ + struct tablet_tool *tool = data; + struct widget *widget = tool->focus_widget; + + if (widget && widget->tablet_tool_distance_handler) + widget->tablet_tool_distance_handler(widget, tool, + distance, + widget->user_data); +} + +static void +tablet_tool_handle_tilt(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + int32_t tilt_x, int32_t tilt_y) +{ + struct tablet_tool *tool = data; + struct widget *widget = tool->focus_widget; + + if (widget && widget->tablet_tool_tilt_handler) + widget->tablet_tool_tilt_handler(widget, tool, + tilt_x, tilt_y, + widget->user_data); +} + +static void +tablet_tool_handle_rotation(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + int32_t rotation) +{ + struct tablet_tool *tool = data; + struct widget *widget = tool->focus_widget; + + if (widget && widget->tablet_tool_rotation_handler) + widget->tablet_tool_rotation_handler(widget, tool, + rotation, + widget->user_data); +} + +static void +tablet_tool_handle_slider(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + int32_t slider) +{ + struct tablet_tool *tool = data; + struct widget *widget = tool->focus_widget; + + if (widget && widget->tablet_tool_slider_handler) + widget->tablet_tool_slider_handler(widget, tool, + slider, + widget->user_data); +} + +static void +tablet_tool_handle_wheel(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + wl_fixed_t degrees, int32_t clicks) +{ + struct tablet_tool *tool = data; + struct widget *widget = tool->focus_widget; + + if (widget && widget->tablet_tool_wheel_handler) + widget->tablet_tool_wheel_handler(widget, tool, + degrees, clicks, + widget->user_data); +} + +static void +tablet_tool_handle_button(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t serial, uint32_t button, uint32_t state) +{ + struct tablet_tool *tool = data; + struct widget *focus = tool->focus_widget; + + tool->input->display->serial = serial; + + if (focus && focus->tablet_tool_button_handler) + focus->tablet_tool_button_handler(focus, tool, button, state, + focus->user_data); +} + +static void +tablet_tool_handle_frame(void *data, struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2, + uint32_t time) +{ + struct tablet_tool *tool = data; + struct widget *widget = tool->focus_widget; + + if (widget && widget->tablet_tool_frame_handler) + widget->tablet_tool_frame_handler(widget, tool, time, + widget->user_data); +} + +static const struct zwp_tablet_tool_v2_listener tablet_tool_listener = { + tablet_tool_handle_type, + tablet_tool_handle_serialid, + tablet_tool_handle_hardware_id_wacom, + tablet_tool_handle_capability, + tablet_tool_handle_done, + tablet_tool_handle_removed, + tablet_tool_handle_proximity_in, + tablet_tool_handle_proximity_out, + tablet_tool_handle_down, + tablet_tool_handle_up, + tablet_tool_handle_motion, + tablet_tool_handle_pressure, + tablet_tool_handle_distance, + tablet_tool_handle_tilt, + tablet_tool_handle_rotation, + tablet_tool_handle_slider, + tablet_tool_handle_wheel, + tablet_tool_handle_button, + tablet_tool_handle_frame, +}; + +static void +tablet_tool_added(void *data, struct zwp_tablet_seat_v2 *zwp_tablet_seat1, + struct zwp_tablet_tool_v2 *id) +{ + struct input *input = data; + struct tablet_tool *tool; + + tool = zalloc(sizeof *tool); + zwp_tablet_tool_v2_add_listener(id, &tablet_tool_listener, tool); + wl_list_insert(&input->tablet_tool_list, &tool->link); + + tool->tool = id; + tool->input = input; +} + +static const struct zwp_tablet_seat_v2_listener tablet_seat_listener = { + tablet_added, + tablet_tool_added, +}; + +static void +display_bind_tablets(struct display *d, uint32_t id) +{ + struct input *input; + + d->tablet_manager = wl_registry_bind(d->registry, id, + &zwp_tablet_manager_v2_interface, 1); + + wl_list_for_each(input, &d->input_list, link) { + input->tablet_seat = + zwp_tablet_manager_v2_get_tablet_seat(d->tablet_manager, + input->seat); + zwp_tablet_seat_v2_add_listener(input->tablet_seat, + &tablet_seat_listener, + input); + } +} + +static void global_destroy(struct display *disp, struct global *g) { if (disp->global_handler_remove) { @@ -5846,6 +6350,8 @@ registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, d->viewporter = wl_registry_bind(registry, id, &wp_viewporter_interface, 1); + } else if (strcmp(interface, "zwp_tablet_manager_v2") == 0) { + display_bind_tablets(d, id); } if (d->global_handler) diff --git a/clients/window.h b/clients/window.h index 22fd7e23..535a96f7 100644 --- a/clients/window.h +++ b/clients/window.h @@ -40,6 +40,8 @@ struct widget; struct display; struct input; struct output; +struct tablet; +struct tablet_tool; struct task { void (*run)(struct task *task, uint32_t events); @@ -265,6 +267,57 @@ typedef void (*widget_axis_handler_t)(struct widget *widget, uint32_t axis, wl_fixed_t value, void *data); +typedef void (*widget_tablet_tool_motion_handler_t)(struct widget *widget, + struct tablet_tool *tool, + float x, float y, + void *data); +typedef void (*widget_tablet_tool_down_handler_t)(struct widget *widget, + struct tablet_tool *tool, + void *data); +typedef void (*widget_tablet_tool_up_handler_t)(struct widget *widget, + struct tablet_tool *tool, + void *data); +typedef void (*widget_tablet_tool_pressure_handler_t)(struct widget *widget, + struct tablet_tool *tool, + uint32_t pressure, + void *data); +typedef void (*widget_tablet_tool_distance_handler_t)(struct widget *widget, + struct tablet_tool *tool, + uint32_t distance, + void *data); +typedef void (*widget_tablet_tool_tilt_handler_t)(struct widget *widget, + struct tablet_tool *tool, + int32_t tilt_x, int32_t tilt_y, + void *data); +typedef void (*widget_tablet_tool_rotation_handler_t)(struct widget *widget, + struct tablet_tool *tool, + int32_t rotation, + void *data); +typedef void (*widget_tablet_tool_slider_handler_t)(struct widget *widget, + struct tablet_tool *tool, + int32_t slider, + void *data); +typedef void (*widget_tablet_tool_wheel_handler_t)(struct widget *widget, + struct tablet_tool *tool, + wl_fixed_t degrees, + int32_t clicks, + void *data); +typedef void (*widget_tablet_tool_proximity_in_handler_t)(struct widget *widget, + struct tablet_tool *tool, + struct tablet *tablet, + void *data); +typedef void (*widget_tablet_tool_proximity_out_handler_t)(struct widget *widget, + struct tablet_tool *tool, + void *data); +typedef void (*widget_tablet_tool_button_handler_t)(struct widget *widget, + struct tablet_tool *tool, + uint32_t button, + uint32_t state, + void *data); +typedef void (*widget_tablet_tool_frame_handler_t)(struct widget *widget, + struct tablet_tool *tool, + uint32_t time, + void *data); typedef void (*widget_pointer_frame_handler_t)(struct widget *widget, struct input *input, @@ -578,6 +631,31 @@ widget_set_axis_handlers(struct widget *widget, widget_axis_source_handler_t axis_source_handler, widget_axis_stop_handler_t axis_stop_handler, widget_axis_discrete_handler_t axis_discrete_handler); +void +widget_set_tablet_tool_axis_handlers(struct widget *widget, + widget_tablet_tool_motion_handler_t motion, + widget_tablet_tool_pressure_handler_t pressure, + widget_tablet_tool_distance_handler_t distance, + widget_tablet_tool_tilt_handler_t tilt, + widget_tablet_tool_rotation_handler_t rotation, + widget_tablet_tool_slider_handler_t slider, + widget_tablet_tool_wheel_handler_t wheel); +void +widget_set_tablet_tool_up_handler(struct widget *widget, + widget_tablet_tool_up_handler_t handler); +void +widget_set_tablet_tool_down_handler(struct widget *widget, + widget_tablet_tool_down_handler_t handler); +void +widget_set_tablet_tool_proximity_handlers(struct widget *widget, + widget_tablet_tool_proximity_in_handler_t in_handler, + widget_tablet_tool_proximity_out_handler_t out_handler); +void +widget_set_tablet_tool_button_handler(struct widget *widget, + widget_tablet_tool_button_handler_t handler); +void +widget_set_tablet_tool_frame_handler(struct widget *widget, + widget_tablet_tool_frame_handler_t handler); void window_inhibit_redraw(struct window *window); @@ -701,6 +779,15 @@ xkb_mod_mask_t keysym_modifiers_get_mask(struct wl_array *modifiers_map, const char *name); +uint32_t +tablet_tool_get_type(struct tablet_tool *tool); + +uint64_t +tablet_tool_get_serial(struct tablet_tool *tool); + +uint64_t +tablet_tool_get_hwid(struct tablet_tool *tool); + struct toytimer; typedef void (*toytimer_cb)(struct toytimer *); |