From cd96e1c124f2f4d0002106a2c37363d7e62929e4 Mon Sep 17 00:00:00 2001 From: Keith Bowes Date: Sat, 22 Feb 2020 21:30:44 -0500 Subject: [PATCH] More cleanups; added missing request_cursor functionality --- include/waybox/cursor.h | 4 +++- include/waybox/output.h | 1 - include/waybox/server.h | 2 -- waybox/cursor.c | 29 ++++++++++++++++++++++++++++- waybox/output.c | 2 -- waybox/server.c | 22 ++++++++++++---------- 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/include/waybox/cursor.h b/include/waybox/cursor.h index 5ba3838..ac99f68 100644 --- a/include/waybox/cursor.h +++ b/include/waybox/cursor.h @@ -25,9 +25,11 @@ struct wb_cursor { struct wl_listener cursor_button; struct wl_listener cursor_axis; struct wl_listener cursor_frame; + + struct wl_listener request_cursor; }; -struct wb_cursor *wb_cursor_create(); +struct wb_cursor *wb_cursor_create(struct wb_server *server); void wb_cursor_destroy(struct wb_cursor *cursor); #endif // cursor.h diff --git a/include/waybox/output.h b/include/waybox/output.h index 0a14df8..0d692be 100644 --- a/include/waybox/output.h +++ b/include/waybox/output.h @@ -20,7 +20,6 @@ struct wb_output { struct wlr_output *wlr_output; struct wb_server *server; - struct timespec last_frame; struct wl_listener destroy; struct wl_listener frame; diff --git a/include/waybox/server.h b/include/waybox/server.h index 7636207..88680e0 100644 --- a/include/waybox/server.h +++ b/include/waybox/server.h @@ -6,7 +6,6 @@ #endif #define _POSIX_C_SOURCE 200112L -#include #include #include @@ -28,7 +27,6 @@ struct wb_server { struct wl_display *wl_display; - struct wl_event_loop *wl_event_loop; struct wlr_backend *backend; struct wlr_compositor *compositor; diff --git a/waybox/cursor.c b/waybox/cursor.c index 2e6520b..e040b46 100644 --- a/waybox/cursor.c +++ b/waybox/cursor.c @@ -140,9 +140,30 @@ static void handle_cursor_frame(struct wl_listener *listener, void *data) { wlr_seat_pointer_notify_frame(cursor->server->seat->seat); } -struct wb_cursor *wb_cursor_create() { +static void handle_cursor_request(struct wl_listener *listener, void *data) { + struct wb_cursor *cursor = wl_container_of( + listener, cursor, request_cursor); + /* This event is rasied by the seat when a client provides a cursor image */ + struct wlr_seat_pointer_request_set_cursor_event *event = data; + struct wlr_seat_client *focused_client = + cursor->server->seat->seat->pointer_state.focused_client; + /* This can be sent by any client, so we check to make sure this one is + * actually has pointer focus first. */ + if (focused_client == event->seat_client) { + /* Once we've vetted the client, we can tell the cursor to use the + * provided surface as the cursor image. It will set the hardware cursor + * on the output that it's currently on and continue to do so as the + * cursor moves between outputs. */ + wlr_cursor_set_surface(cursor->cursor, event->surface, + event->hotspot_x, event->hotspot_y); + } +} + +struct wb_cursor *wb_cursor_create(struct wb_server *server) { struct wb_cursor *cursor = malloc(sizeof(struct wb_cursor)); cursor->cursor = wlr_cursor_create(); + cursor->server = server; + cursor->xcursor_manager = wlr_xcursor_manager_create("default", 24); wlr_xcursor_manager_load(cursor->xcursor_manager, 1); @@ -161,6 +182,12 @@ struct wb_cursor *wb_cursor_create() { cursor->cursor_frame.notify = handle_cursor_frame; wl_signal_add(&cursor->cursor->events.frame, &cursor->cursor_frame); + cursor->request_cursor.notify = handle_cursor_request; + wl_signal_add(&server->seat->seat->events.request_set_cursor, + &cursor->request_cursor); + + wlr_cursor_attach_output_layout(cursor->cursor, server->layout); + return cursor; } diff --git a/waybox/output.c b/waybox/output.c index ba71280..dddba71 100644 --- a/waybox/output.c +++ b/waybox/output.c @@ -105,7 +105,6 @@ void output_frame_notify(struct wl_listener *listener, void *data) { wlr_renderer_end(renderer); wlr_output_commit(wlr_output); - output->last_frame = now; } void output_destroy_notify(struct wl_listener *listener, void *data) { @@ -134,7 +133,6 @@ void new_output_notify(struct wl_listener *listener, void *data) { } struct wb_output *output = calloc(1, sizeof(struct wb_output)); - clock_gettime(CLOCK_MONOTONIC, &output->last_frame); output->server = server; output->wlr_output = wlr_output; wl_list_insert(&server->outputs, &output->link); diff --git a/waybox/server.c b/waybox/server.c index 5f089e6..dbba7b7 100644 --- a/waybox/server.c +++ b/waybox/server.c @@ -5,15 +5,13 @@ bool init_wb(struct wb_server* server) { // create display server->wl_display = wl_display_create(); - assert(server->wl_display); - - // event loop stuff - server->wl_event_loop = wl_display_get_event_loop(server->wl_display); - assert(server->wl_event_loop); + if (server->wl_display == NULL) { + fprintf(stderr, "Failed to connect to a Wayland display\n"); + return false; + } // create backend server->backend = wlr_backend_autocreate(server->wl_display, NULL); - assert(server->backend); if (server->backend == NULL) { printf("Failed to create backend\n"); return false; @@ -23,10 +21,8 @@ bool init_wb(struct wb_server* server) { wlr_renderer_init_wl_display(server->renderer, server->wl_display); server->layout = wlr_output_layout_create(); - server->cursor = wb_cursor_create(); - server->cursor->server = server; - wlr_cursor_attach_output_layout(server->cursor->cursor, server->layout); server->seat = wb_seat_create(server); + server->cursor = wb_cursor_create(server); return true; } @@ -38,10 +34,15 @@ bool start_wb(struct wb_server* server) { wl_signal_add(&server->backend->events.new_output, &server->new_output); const char *socket = wl_display_add_socket_auto(server->wl_display); - assert(socket); + if (!socket) + { + wlr_backend_destroy(server->backend); + return false; + } if (!wlr_backend_start(server->backend)) { fprintf(stderr, "Failed to start backend\n"); + wlr_backend_destroy(server->backend); wl_display_destroy(server->wl_display); return false; } @@ -64,6 +65,7 @@ bool start_wb(struct wb_server* server) { } bool terminate_wb(struct wb_server* server) { + wl_display_destroy_clients(server->wl_display); wb_cursor_destroy(server->cursor); wb_seat_destroy(server->seat); wlr_output_layout_destroy(server->layout);