diff --git a/include/kiwmi/input/cursor.h b/include/kiwmi/input/cursor.h new file mode 100644 index 0000000..100f0b8 --- /dev/null +++ b/include/kiwmi/input/cursor.h @@ -0,0 +1,24 @@ +/* Copyright (c), Charlotte Meyer + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#ifndef KIWMI_INPUT_CURSOR_H +#define KIWMI_INPUT_CURSOR_H + +#include +#include +#include + +struct kiwmi_cursor { + struct wlr_cursor *cursor; + struct wlr_xcursor_manager *xcursor_manager; + struct wl_listener cursor_motion; + struct wl_listener cursor_motion_absolute; +}; + +struct kiwmi_cursor *cursor_create(struct wlr_output_layout *output_layout); + +#endif /* KIWMI_INPUT_CURSOR_H */ diff --git a/include/kiwmi/server.h b/include/kiwmi/server.h index b39e5b3..5cb8b6c 100644 --- a/include/kiwmi/server.h +++ b/include/kiwmi/server.h @@ -14,6 +14,8 @@ #include #include +#include "kiwmi/input/cursor.h" + struct kiwmi_server { struct wl_display *wl_display; struct wlr_backend *backend; @@ -23,6 +25,7 @@ struct kiwmi_server { const char *socket; struct wl_list outputs; // struct kiwmi_output::link struct wl_listener new_output; + struct kiwmi_cursor *cursor; }; bool server_init(struct kiwmi_server *server); diff --git a/kiwmi/input/cursor.c b/kiwmi/input/cursor.c new file mode 100644 index 0000000..0cd6ec7 --- /dev/null +++ b/kiwmi/input/cursor.c @@ -0,0 +1,71 @@ +/* Copyright (c), Charlotte Meyer + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "kiwmi/input/cursor.h" + +#include + +#include +#include +#include +#include +#include + +static void +cursor_motion_notify(struct wl_listener *listener, void *data) +{ + struct kiwmi_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 +cursor_motion_absolute_notify(struct wl_listener *listener, void *data) +{ + struct kiwmi_cursor *cursor = + wl_container_of(listener, cursor, cursor_motion); + struct wlr_event_pointer_motion_absolute *event = data; + + wlr_cursor_warp_absolute(cursor->cursor, event->device, event->x, event->y); +} + +struct kiwmi_cursor * +cursor_create(struct wlr_output_layout *output_layout) +{ + wlr_log(WLR_DEBUG, "Creating cursor"); + + struct kiwmi_cursor *cursor = malloc(sizeof(*cursor)); + if (!cursor) { + wlr_log(WLR_ERROR, "Failed to allocate kiwmi_cursor"); + return NULL; + } + + cursor->cursor = wlr_cursor_create(); + if (!cursor->cursor) { + wlr_log(WLR_ERROR, "Failed to create cursor"); + free(cursor); + return NULL; + } + + wlr_cursor_attach_output_layout(cursor->cursor, output_layout); + + cursor->xcursor_manager = wlr_xcursor_manager_create(NULL, 24); + wlr_xcursor_manager_load(cursor->xcursor_manager, 1); + + cursor->cursor_motion.notify = cursor_motion_notify; + wl_signal_add(&cursor->cursor->events.motion, &cursor->cursor_motion); + + cursor->cursor_motion_absolute.notify = cursor_motion_absolute_notify; + wl_signal_add( + &cursor->cursor->events.motion_absolute, + &cursor->cursor_motion_absolute); + + return cursor; +} diff --git a/kiwmi/meson.build b/kiwmi/meson.build index 72efcab..320792c 100644 --- a/kiwmi/meson.build +++ b/kiwmi/meson.build @@ -3,6 +3,7 @@ kiwmi_sources = files( 'server.c', 'output.c', 'desktop/output.c', + 'input/cursor.c', ) kiwmi_deps = [ diff --git a/kiwmi/server.c b/kiwmi/server.c index bcbf1d2..fe19a84 100644 --- a/kiwmi/server.c +++ b/kiwmi/server.c @@ -13,11 +13,14 @@ #include #include #include +#include #include #include +#include #include #include "kiwmi/desktop/output.h" +#include "kiwmi/input/cursor.h" bool server_init(struct kiwmi_server *server) @@ -39,6 +42,14 @@ server_init(struct kiwmi_server *server) server->data_device_manager = wlr_data_device_manager_create(server->wl_display); + server->cursor = cursor_create(server->output_layout); + if (!server->cursor) { + wlr_log(WLR_ERROR, "Failed to create cursor"); + wlr_backend_destroy(server->backend); + wl_display_destroy(server->wl_display); + return false; + } + server->output_layout = wlr_output_layout_create(); wl_list_init(&server->outputs);