Basic cursor handling

This commit is contained in:
buffet 2019-03-07 19:25:38 +01:00
parent 8a2e11ee28
commit 840a40de00
5 changed files with 110 additions and 0 deletions

View file

@ -0,0 +1,24 @@
/* Copyright (c), Charlotte Meyer <dev@buffet.sh>
*
* 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 <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_xcursor_manager.h>
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 */

View file

@ -14,6 +14,8 @@
#include <wlr/types/wlr_data_device.h> #include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_output_layout.h> #include <wlr/types/wlr_output_layout.h>
#include "kiwmi/input/cursor.h"
struct kiwmi_server { struct kiwmi_server {
struct wl_display *wl_display; struct wl_display *wl_display;
struct wlr_backend *backend; struct wlr_backend *backend;
@ -23,6 +25,7 @@ struct kiwmi_server {
const char *socket; const char *socket;
struct wl_list outputs; // struct kiwmi_output::link struct wl_list outputs; // struct kiwmi_output::link
struct wl_listener new_output; struct wl_listener new_output;
struct kiwmi_cursor *cursor;
}; };
bool server_init(struct kiwmi_server *server); bool server_init(struct kiwmi_server *server);

71
kiwmi/input/cursor.c Normal file
View file

@ -0,0 +1,71 @@
/* Copyright (c), Charlotte Meyer <dev@buffet.sh>
*
* 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 <stdlib.h>
#include <wayland-server.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/util/log.h>
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;
}

View file

@ -3,6 +3,7 @@ kiwmi_sources = files(
'server.c', 'server.c',
'output.c', 'output.c',
'desktop/output.c', 'desktop/output.c',
'input/cursor.c',
) )
kiwmi_deps = [ kiwmi_deps = [

View file

@ -13,11 +13,14 @@
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/render/wlr_renderer.h> #include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_compositor.h> #include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_data_device.h> #include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_output_layout.h> #include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "kiwmi/desktop/output.h" #include "kiwmi/desktop/output.h"
#include "kiwmi/input/cursor.h"
bool bool
server_init(struct kiwmi_server *server) server_init(struct kiwmi_server *server)
@ -39,6 +42,14 @@ server_init(struct kiwmi_server *server)
server->data_device_manager = server->data_device_manager =
wlr_data_device_manager_create(server->wl_display); 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(); server->output_layout = wlr_output_layout_create();
wl_list_init(&server->outputs); wl_list_init(&server->outputs);