From 6794c5f943abcd975471094ee19d4185d25ffd0e Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Fri, 22 Oct 2021 17:34:21 +0200 Subject: [PATCH] Add cursor handling --- meson.build | 5 ++--- src/main.cpp | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/meson.build b/meson.build index 8efeca4..6fb26d0 100644 --- a/meson.build +++ b/meson.build @@ -2,9 +2,9 @@ project('somebar', ['c', 'cpp'], default_options: ['cpp_std=c++17']) wayland_dep = dependency('wayland-client') +wayland_cursor_dep = dependency('wayland-cursor') qt5 = import('qt5') qt5_dep = dependency('qt5', modules: ['Core', 'Gui']) -#moc = qt5.compile_moc(headers: [ 'src/.hpp', ]) subdir('protocols') @@ -13,5 +13,4 @@ executable('somebar', 'src/shm_buffer.cpp', 'src/bar.cpp', wayland_sources, - #moc, - dependencies: [qt5_dep, wayland_dep]) + dependencies: [qt5_dep, wayland_dep, wayland_cursor_dep]) diff --git a/src/main.cpp b/src/main.cpp index a93ecfe..4f68ceb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "xdg-shell-client-protocol.h" #include "common.hpp" @@ -46,15 +47,20 @@ static const struct xdg_wm_base_listener xdgWmBaseListener = { struct PointerState { wl_pointer *pointer; + wl_surface *cursorSurface; + wl_cursor_image *cursorImage; Bar *focusedBar; int x, y; bool leftButtonClick; }; static PointerState pointerState; static const struct wl_pointer_listener pointerListener = { - .enter = [](void*, wl_pointer*, uint32_t serial, wl_surface*, wl_fixed_t x, wl_fixed_t y) + .enter = [](void*, wl_pointer *pointer, uint32_t serial, + wl_surface*, wl_fixed_t x, wl_fixed_t y) { pointerState.focusedBar = &bar.value(); + wl_pointer_set_cursor(pointer, serial, pointerState.cursorSurface, + pointerState.cursorImage->hotspot_x, pointerState.cursorImage->hotspot_y); }, .leave = [](void*, wl_pointer*, uint32_t serial, wl_surface*) { pointerState.focusedBar = nullptr; @@ -85,8 +91,14 @@ static wl_seat *seat; static const struct wl_seat_listener seatListener = { [](void*, wl_seat*, uint32_t cap) { - if (cap & WL_SEAT_CAPABILITY_POINTER) { - printf("got pointer"); + if (!pointerState.pointer && WL_SEAT_CAPABILITY_POINTER) { + auto cursorTheme = wl_cursor_theme_load(NULL, 24, shm); + auto cursorImage = wl_cursor_theme_get_cursor(cursorTheme, "left_ptr")->images[0]; + pointerState.cursorImage = cursorImage; + pointerState.cursorSurface = wl_compositor_create_surface(compositor); + wl_surface_attach(pointerState.cursorSurface, + wl_cursor_image_get_buffer(cursorImage), 0, 0); + wl_surface_commit(pointerState.cursorSurface); pointerState.pointer = wl_seat_get_pointer(seat); wl_pointer_add_listener(pointerState.pointer, &pointerListener, nullptr); }