From 9889b14511e95b29855aa265c19e57e9cd86cd1b Mon Sep 17 00:00:00 2001 From: David Eklov Date: Mon, 4 Jul 2016 23:21:11 -0500 Subject: [PATCH 1/3] Check capabilities before using pointer and keyboard --- wayland/registry.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/wayland/registry.c b/wayland/registry.c index 46814bb7..622571f0 100644 --- a/wayland/registry.c +++ b/wayland/registry.c @@ -174,6 +174,35 @@ static const struct wl_keyboard_listener keyboard_listener = { .repeat_info = keyboard_handle_repeat_info }; +static void seat_handle_capabilities(void *data, struct wl_seat *seat, + enum wl_seat_capability caps) { + struct registry *reg = data; + + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !reg->pointer) { + reg->pointer = wl_seat_get_pointer(reg->seat); + } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && reg->pointer) { + wl_pointer_destroy(reg->pointer); + reg->pointer = NULL; + } + + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !reg->keyboard) { + reg->keyboard = wl_seat_get_keyboard(reg->seat); + wl_keyboard_add_listener(reg->keyboard, &keyboard_listener, reg); + } else if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && reg->keyboard) { + wl_keyboard_destroy(reg->keyboard); + reg->keyboard = NULL; + } +} + +static void seat_handle_name(void *data, struct wl_seat *seat, const char *name) { + // this space intentionally left blank +} + +static const struct wl_seat_listener seat_listener = { + .capabilities = seat_handle_capabilities, + .name = seat_handle_name, +}; + static void registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { struct registry *reg = data; @@ -186,11 +215,7 @@ static void registry_global(void *data, struct wl_registry *registry, reg->shell = wl_registry_bind(registry, name, &wl_shell_interface, version); } else if (strcmp(interface, wl_seat_interface.name) == 0) { reg->seat = wl_registry_bind(registry, name, &wl_seat_interface, version); - reg->pointer = wl_seat_get_pointer(reg->seat); - reg->keyboard = wl_seat_get_keyboard(reg->seat); - if (reg->keyboard) { - wl_keyboard_add_listener(reg->keyboard, &keyboard_listener, reg); - } + wl_seat_add_listener(reg->seat, &seat_listener, reg); } else if (strcmp(interface, wl_output_interface.name) == 0) { struct wl_output *output = wl_registry_bind(registry, name, &wl_output_interface, version); struct output_state *ostate = malloc(sizeof(struct output_state)); From c8a64305fd746ab1b33367d3ffcc9fa054221717 Mon Sep 17 00:00:00 2001 From: David Eklov Date: Tue, 5 Jul 2016 01:21:56 -0500 Subject: [PATCH 2/3] Enable windows to register to get notified of pointer button events --- include/client/window.h | 10 ++++++++++ wayland/window.c | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/client/window.h b/include/client/window.h index e48ec4f3..b5cc1880 100644 --- a/include/client/window.h +++ b/include/client/window.h @@ -9,6 +9,8 @@ #include "list.h" #include "client/registry.h" +struct window; + struct buffer { struct wl_buffer *buffer; cairo_surface_t *surface; @@ -25,6 +27,13 @@ struct cursor { struct wl_poitner *pointer; }; +struct pointer_input { + wl_fixed_t last_x; + wl_fixed_t last_y; + + void (*notify)(struct window *window, wl_fixed_t x, wl_fixed_t y, uint32_t button); +}; + struct window { struct registry *registry; struct buffer buffers[2]; @@ -36,6 +45,7 @@ struct window { uint32_t width, height; char *font; cairo_t *cairo; + struct pointer_input pointer_input; }; struct window *window_setup(struct registry *registry, uint32_t width, uint32_t height, bool shell_surface); diff --git a/wayland/window.c b/wayland/window.c index 7ca9e4ec..e055e244 100644 --- a/wayland/window.c +++ b/wayland/window.c @@ -30,10 +30,20 @@ static void pointer_handle_leave(void *data, struct wl_pointer *pointer, static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w) { + struct window *window = data; + + window->pointer_input.last_x = sx_w; + window->pointer_input.last_y = sy_w; } static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state_w) { + struct window *window = data; + struct pointer_input *input = &window->pointer_input; + + if (window->pointer_input.notify) { + window->pointer_input.notify(window, input->last_x, input->last_y, button); + } } static void pointer_handle_axis(void *data, struct wl_pointer *pointer, From b9d8cbabdd42b8b65852fac5d8d8b01bbdabb280 Mon Sep 17 00:00:00 2001 From: David Eklov Date: Wed, 6 Jul 2016 01:08:54 -0500 Subject: [PATCH 3/3] Fix formatting guide violations (spaces instead of tabs) --- include/client/window.h | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/include/client/window.h b/include/client/window.h index b5cc1880..7be4fff3 100644 --- a/include/client/window.h +++ b/include/client/window.h @@ -12,19 +12,19 @@ struct window; struct buffer { - struct wl_buffer *buffer; - cairo_surface_t *surface; - cairo_t *cairo; - PangoContext *pango; - uint32_t width, height; - bool busy; + struct wl_buffer *buffer; + cairo_surface_t *surface; + cairo_t *cairo; + PangoContext *pango; + uint32_t width, height; + bool busy; }; struct cursor { - struct wl_surface *surface; - struct wl_cursor_theme *cursor_theme; - struct wl_cursor *cursor; - struct wl_poitner *pointer; + struct wl_surface *surface; + struct wl_cursor_theme *cursor_theme; + struct wl_cursor *cursor; + struct wl_poitner *pointer; }; struct pointer_input { @@ -35,16 +35,16 @@ struct pointer_input { }; struct window { - struct registry *registry; - struct buffer buffers[2]; - struct buffer *buffer; - struct wl_surface *surface; - struct wl_shell_surface *shell_surface; - struct wl_callback *frame_cb; - struct cursor cursor; - uint32_t width, height; - char *font; - cairo_t *cairo; + struct registry *registry; + struct buffer buffers[2]; + struct buffer *buffer; + struct wl_surface *surface; + struct wl_shell_surface *shell_surface; + struct wl_callback *frame_cb; + struct cursor cursor; + uint32_t width, height; + char *font; + cairo_t *cairo; struct pointer_input pointer_input; };