From 4f231567a77fed8248aeac551add209497e3894f Mon Sep 17 00:00:00 2001 From: Veselin Ivanov Date: Thu, 4 Oct 2018 20:56:47 +0300 Subject: [PATCH] Added very basic cursor tracking No interaction with wayland clients yet. Just draws the cursor where the pointer device points. --- include/waybox/cursor.h | 18 ++++++++++++++++++ include/waybox/server.h | 7 ++++++- waybox/cursor.c | 38 ++++++++++++++++++++++++++++++++++++++ waybox/meson.build | 1 + waybox/output.c | 5 +++++ waybox/server.c | 22 ++++++++++++++++++++++ 6 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 include/waybox/cursor.h create mode 100644 waybox/cursor.c diff --git a/include/waybox/cursor.h b/include/waybox/cursor.h new file mode 100644 index 0000000..370c8b5 --- /dev/null +++ b/include/waybox/cursor.h @@ -0,0 +1,18 @@ +#ifndef CURSOR_H +#define CURSOR_H +#include +#include +#include + +struct wb_cursor { + struct wlr_cursor *cursor; + struct wlr_xcursor_manager *xcursor_manager; + + struct wl_listener cursor_motion; + struct wl_listener cursor_motion_absolute; +}; + +struct wb_cursor *wb_cursor_create(); +void wb_cursor_destroy(struct wb_cursor *cursor); + +#endif // cursor.h diff --git a/include/waybox/server.h b/include/waybox/server.h index 613a273..a9d58e1 100644 --- a/include/waybox/server.h +++ b/include/waybox/server.h @@ -19,9 +19,10 @@ #include #include #include +#include #include "waybox/output.h" - +#include "waybox/cursor.h" struct wb_server { struct wl_display *wl_display; @@ -30,7 +31,11 @@ struct wb_server { struct wlr_backend *backend; struct wlr_compositor *compositor; + struct wlr_output_layout *layout; + struct wb_cursor *cursor; + struct wl_listener new_output; + struct wl_listener new_input; struct wl_list outputs; // wb_output::link }; diff --git a/waybox/cursor.c b/waybox/cursor.c new file mode 100644 index 0000000..fb769a4 --- /dev/null +++ b/waybox/cursor.c @@ -0,0 +1,38 @@ +#include +#include "waybox/cursor.h" + +static void handle_cursor_motion(struct wl_listener *listener, void *data) { + struct wb_cursor *cursor = wl_container_of(listener, cursor, cursor_motion); + struct wlr_event_pointer_motion *event = data; + wlr_cursor_move(cursor->cursor, event->device, event->delta_x, event->delta_y); +} + +static void handle_cursor_motion_absolute(struct wl_listener *listener, void *data) { + struct wb_cursor *cursor = wl_container_of(listener, cursor, cursor_motion_absolute); + struct wlr_event_pointer_motion_absolute *event = data; + wlr_cursor_warp_absolute(cursor->cursor, event->device, event->x, event->y); +} + +struct wb_cursor *wb_cursor_create() { + struct wb_cursor *cursor = malloc(sizeof(struct wb_cursor)); + cursor->cursor = wlr_cursor_create(); + cursor->xcursor_manager = wlr_xcursor_manager_create("default", 24); + + cursor->cursor_motion.notify = handle_cursor_motion; + wl_signal_add(&cursor->cursor->events.motion, &cursor->cursor_motion); + + cursor->cursor_motion_absolute.notify = handle_cursor_motion_absolute; + wl_signal_add(&cursor->cursor->events.motion_absolute, &cursor->cursor_motion_absolute); + + return cursor; +} + +void wb_cursor_destroy(struct wb_cursor *cursor) { + if (!cursor) { + return; + } + + wlr_xcursor_manager_destroy(cursor->xcursor_manager); + wlr_cursor_destroy(cursor->cursor); + free(cursor); +} diff --git a/waybox/meson.build b/waybox/meson.build index fba6d92..a44d004 100644 --- a/waybox/meson.build +++ b/waybox/meson.build @@ -1,4 +1,5 @@ wb_src = files( + 'cursor.c', 'main.c', 'output.c', 'server.c', diff --git a/waybox/output.c b/waybox/output.c index 3861ca4..1dbe670 100644 --- a/waybox/output.c +++ b/waybox/output.c @@ -42,6 +42,7 @@ void output_frame_notify(struct wl_listener *listener, void *data) { void output_destroy_notify(struct wl_listener *listener, void *data) { struct wb_output *output = wl_container_of(listener, output, destroy); + wlr_output_layout_remove(output->server->layout, output->wlr_output); wl_list_remove(&output->link); wl_list_remove(&output->destroy.link); wl_list_remove(&output->frame.link); @@ -72,4 +73,8 @@ void new_output_notify(struct wl_listener *listener, void *data) { wl_signal_add(&wlr_output->events.destroy, &output->destroy); output->frame.notify = output_frame_notify; wl_signal_add(&wlr_output->events.frame, &output->frame); + + wlr_output_layout_add_auto(server->layout, output->wlr_output); + wlr_xcursor_manager_load(server->cursor->xcursor_manager, output->wlr_output->scale); + wlr_xcursor_manager_set_cursor_image(server->cursor->xcursor_manager, "left_ptr", server->cursor->cursor); } diff --git a/waybox/server.c b/waybox/server.c index dd34536..36bb805 100644 --- a/waybox/server.c +++ b/waybox/server.c @@ -1,5 +1,17 @@ #include "waybox/server.h" +static void new_input_notify(struct wl_listener *listener, void *data) { + struct wlr_input_device *device = data; + struct wb_server *server = wl_container_of(listener, server, new_input); + switch (device->type) { + case WLR_INPUT_DEVICE_POINTER: + wlr_cursor_attach_input_device(server->cursor->cursor, device); + break; + default: + break; + } +} + bool init_wb(struct wb_server* server) { // create display @@ -21,6 +33,10 @@ bool init_wb(struct wb_server* server) { return false; } + server->layout = wlr_output_layout_create(); + server->cursor = wb_cursor_create(); + wlr_cursor_attach_output_layout(server->cursor->cursor, server->layout); + return true; } @@ -30,6 +46,9 @@ bool start_wb(struct wb_server* server) { server->new_output.notify = new_output_notify; wl_signal_add(&server->backend->events.new_output, &server->new_output); + server->new_input.notify = new_input_notify; + wl_signal_add(&server->backend->events.new_input, &server->new_input); + const char *socket = wl_display_add_socket_auto(server->wl_display); assert(socket); @@ -58,5 +77,8 @@ bool terminate_wb(struct wb_server* server) { printf("Display destroyed.\n"); + wb_cursor_destroy(server->cursor); + wlr_output_layout_destroy(server->layout); + return true; }