diff --git a/README.md b/README.md index 9179cdc..a0438cc 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,12 @@ _Note: waypp is a work in progress and is not ABI stable_ waypp is a modern and efficient C++ library for Wayland. This library aims to provide a smooth and easy-to-use interface for developers to build Wayland-based applications using C++. +## Compatible Compositors + +* Mutter +* Weston +* Wlroots + ## Installation ### Debian diff --git a/examples/simple-egl.cc b/examples/simple-egl.cc index f9d6ae8..994a9f6 100644 --- a/examples/simple-egl.cc +++ b/examples/simple-egl.cc @@ -189,8 +189,8 @@ struct weston_matrix { * 3 7 11 15 */ -void weston_matrix_init(struct weston_matrix *matrix) { - static const struct weston_matrix identity = { +void weston_matrix_init(weston_matrix *matrix) { + static const weston_matrix identity = { .d = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, .type = 0, }; @@ -199,8 +199,8 @@ void weston_matrix_init(struct weston_matrix *matrix) { } /* m <- n * m, that is, m is multiplied on the LEFT. */ -void weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n) { - struct weston_matrix tmp{}; +void weston_matrix_multiply(weston_matrix *m, const weston_matrix *n) { + weston_matrix tmp{}; const float *row, *column; int i, j, k; @@ -217,8 +217,8 @@ void weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix memcpy(m, &tmp, sizeof tmp); } -void weston_matrix_scale(struct weston_matrix *matrix, float x, float y, float z) { - struct weston_matrix scale = { +void weston_matrix_scale(weston_matrix *matrix, float x, float y, float z) { + weston_matrix scale = { .d = {x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1}, .type = WESTON_MATRIX_TRANSFORM_SCALE, }; @@ -226,8 +226,8 @@ void weston_matrix_scale(struct weston_matrix *matrix, float x, float y, float z weston_matrix_multiply(matrix, &scale); } -void weston_matrix_rotate_xy(struct weston_matrix *matrix, float cos, float sin) { - struct weston_matrix translate = { +void weston_matrix_rotate_xy(weston_matrix *matrix, float cos, float sin) { + weston_matrix translate = { .d = {cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, .type = WESTON_MATRIX_TRANSFORM_ROTATE, }; @@ -303,7 +303,7 @@ static void draw_frame(void *userdata, uint32_t /* time */) { window->update_buffer_geometry(); - struct timeval tv{}; + timeval tv{}; gettimeofday(&tv, nullptr); auto time = static_cast(tv.tv_sec * 1000 + tv.tv_usec / 1000); if (frames == 0) { @@ -325,7 +325,7 @@ static void draw_frame(void *userdata, uint32_t /* time */) { angle = static_cast(((time - initial_frame_time) / speed_div) % 360 * M_PI / 180.0); } - struct weston_matrix rotation{}; + weston_matrix rotation{}; weston_matrix_init(&rotation); rotation.d[0] = cos(angle); rotation.d[2] = sin(angle); @@ -383,7 +383,7 @@ static void draw_frame(void *userdata, uint32_t /* time */) { class KeyboardHandler : public SeatObserver, public KeyboardObserver { public: - void notify_seat_capabilities(Seat *seat, struct wl_seat * /* seat */, uint32_t /* caps */) override { + void notify_seat_capabilities(Seat *seat, wl_seat * /* seat */, uint32_t /* caps */) override { if (seat) { auto keyboard = seat->get_keyboard(); if (keyboard.has_value()) { @@ -392,27 +392,27 @@ class KeyboardHandler : public SeatObserver, public KeyboardObserver { } } - void notify_seat_name(Seat * /* seat */, struct wl_seat * /* seat */, const char *name) override { + void notify_seat_name(Seat * /* seat */, wl_seat * /* seat */, const char *name) override { spdlog::info("Seat: {}", name); } void notify_keyboard_enter(Keyboard * /* keyboard */, - struct wl_keyboard * /* wl_keyboard */, + wl_keyboard * /* wl_keyboard */, uint32_t serial, - struct wl_surface *surface, - struct wl_array * /* keys */) override { + wl_surface *surface, + wl_array * /* keys */) override { spdlog::info("Keyboard Enter: serial: {}, surface: {}", serial, fmt::ptr(surface)); } void notify_keyboard_leave(Keyboard * /* keyboard */, - struct wl_keyboard * /* wl_keyboard */, + wl_keyboard * /* wl_keyboard */, uint32_t serial, - struct wl_surface *surface) override { + wl_surface *surface) override { spdlog::info("Keyboard Leave: serial: {}, surface: {}", serial, fmt::ptr(surface)); } void notify_keyboard_keymap(Keyboard * /* keyboard */, - struct wl_keyboard * /* wl_keyboard */, + wl_keyboard * /* wl_keyboard */, uint32_t format, int32_t fd, uint32_t size) override { @@ -420,7 +420,7 @@ class KeyboardHandler : public SeatObserver, public KeyboardObserver { } void notify_keyboard_key(Keyboard * /* keyboard */, - struct wl_keyboard * /* wl_keyboard */, + wl_keyboard * /* wl_keyboard */, uint32_t serial, uint32_t time, uint32_t xkb_scancode, diff --git a/examples/simple-shm.cc b/examples/simple-shm.cc index e524df7..33d3bd4 100644 --- a/examples/simple-shm.cc +++ b/examples/simple-shm.cc @@ -164,7 +164,7 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver return (top_level_->is_valid() && wm_->display_dispatch() != -1); } - void notify_seat_capabilities(Seat *seat, struct wl_seat * /* seat */, uint32_t /* caps */) override { + void notify_seat_capabilities(Seat *seat, wl_seat * /* seat */, uint32_t /* caps */) override { if (seat) { auto keyboard = seat->get_keyboard(); if (keyboard.has_value()) { @@ -178,27 +178,27 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver } } - void notify_seat_name(Seat * /* seat */, struct wl_seat * /* seat */, const char *name) override { + void notify_seat_name(Seat * /* seat */, wl_seat * /* seat */, const char *name) override { spdlog::info("Seat: {}", name); } void notify_keyboard_enter(Keyboard * /* keyboard */, - struct wl_keyboard * /* wl_keyboard */, + wl_keyboard * /* wl_keyboard */, uint32_t serial, - struct wl_surface *surface, - struct wl_array * /* keys */) override { + wl_surface *surface, + wl_array * /* keys */) override { spdlog::info("Keyboard Enter: serial: {}, surface: {}", serial, fmt::ptr(surface)); } void notify_keyboard_leave(Keyboard * /* keyboard */, - struct wl_keyboard * /* wl_keyboard */, + wl_keyboard * /* wl_keyboard */, uint32_t serial, - struct wl_surface *surface) override { + wl_surface *surface) override { spdlog::info("Keyboard Leave: serial: {}, surface: {}", serial, fmt::ptr(surface)); } void notify_keyboard_keymap(Keyboard * /* keyboard */, - struct wl_keyboard * /* wl_keyboard */, + wl_keyboard * /* wl_keyboard */, uint32_t format, int32_t fd, uint32_t size) override { @@ -206,7 +206,7 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver } void notify_keyboard_key(Keyboard * /* keyboard */, - struct wl_keyboard * /* wl_keyboard */, + wl_keyboard * /* wl_keyboard */, uint32_t serial, uint32_t time, uint32_t xkb_scancode, @@ -221,24 +221,24 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver } void notify_pointer_enter(Pointer *pointer, - struct wl_pointer * /* pointer */, + wl_pointer * /* pointer */, uint32_t serial, - struct wl_surface *surface, + wl_surface *surface, double sx, double sy) override { spdlog::info("Pointer Enter: serial: {}, surface: {}, x: {}, y: {}", serial, fmt::ptr(surface), sx, sy); - pointer->set_cursor(serial); + pointer->set_cursor(serial, "crosshair"); } void notify_pointer_leave(Pointer * /* pointer */, - struct wl_pointer * /* pointer */, + wl_pointer * /* pointer */, uint32_t serial, - struct wl_surface *surface) override { + wl_surface *surface) override { spdlog::info("Pointer Leave: serial: {}, surface: {}", serial, fmt::ptr(surface)); } void notify_pointer_motion(Pointer * /* pointer */, - struct wl_pointer * /* pointer */, + wl_pointer * /* pointer */, uint32_t time, double sx, double sy) override { @@ -246,7 +246,7 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver } void notify_pointer_button(Pointer * /* pointer */, - struct wl_pointer * /* pointer */, + wl_pointer * /* pointer */, uint32_t serial, uint32_t time, uint32_t button, @@ -255,32 +255,32 @@ class App : public PointerObserver, public KeyboardObserver, public SeatObserver } void notify_pointer_axis(Pointer * /* pointer */, - struct wl_pointer * /* pointer */, + wl_pointer * /* pointer */, uint32_t time, uint32_t axis, wl_fixed_t value) override { spdlog::info("Pointer Axis: time: {}, axis: {}, value: {}", time, axis, value); } - void notify_pointer_frame(Pointer * /* pointer */, struct wl_pointer * /* pointer */) override { + void notify_pointer_frame(Pointer * /* pointer */, wl_pointer * /* pointer */) override { spdlog::info("Pointer Frame"); }; void notify_pointer_axis_source(Pointer * /* pointer */, - struct wl_pointer * /* pointer */, + wl_pointer * /* pointer */, uint32_t axis_source) override { spdlog::info("Pointer Axis Source: axis_source: {}", axis_source); }; void notify_pointer_axis_stop(Pointer * /* pointer */, - struct wl_pointer * /* pointer */, + wl_pointer * /* pointer */, uint32_t /* time */, uint32_t axis) override { spdlog::info("Pointer Axis Stop: axis: {}", axis); }; void notify_pointer_axis_discrete(Pointer * /* pointer */, - struct wl_pointer * /*pointer */, + wl_pointer * /*pointer */, uint32_t axis, int32_t discrete) override { spdlog::info("Pointer Axis Discrete: axis: {}, discrete: {}", axis, discrete); diff --git a/src/seat/pointer.cc b/src/seat/pointer.cc index da81127..89540b1 100644 --- a/src/seat/pointer.cc +++ b/src/seat/pointer.cc @@ -31,9 +31,7 @@ Pointer::Pointer(wl_pointer *pointer, struct wl_compositor *wl_compositor, const wl_pointer_(pointer), wl_shm_(wl_shm), disable_cursor_(disable_cursor), size_(size) { SPDLOG_DEBUG("Pointer"); wl_pointer_add_listener(pointer, &pointer_listener_, this); - if (!disable_cursor_) { - wl_surface_ = wl_compositor_create_surface(wl_compositor); - } + wl_surface_cursor_ = wl_compositor_create_surface(wl_compositor); } /** @@ -51,8 +49,8 @@ Pointer::~Pointer() { if (theme_) { wl_cursor_theme_destroy(theme_); } - if (wl_surface_) { - wl_surface_destroy(wl_surface_); + if (wl_surface_cursor_) { + wl_surface_destroy(wl_surface_cursor_); } if (wl_pointer_) { wl_pointer_release(wl_pointer_); @@ -298,7 +296,15 @@ void Pointer::handle_axis_discrete(void *data, } void Pointer::set_cursor(uint32_t serial, const char *name) { - if (disable_cursor_ || !wl_shm_.has_value()) { + if (disable_cursor_) { + wl_pointer_set_cursor(wl_pointer_, serial, + wl_surface_cursor_, 0, 0); + wl_surface_damage(wl_surface_cursor_, 0, 0, 0, 0); + wl_surface_commit(wl_surface_cursor_); + return; + } + + if (!wl_shm_.has_value()) { return; } @@ -321,11 +327,11 @@ void Pointer::set_cursor(uint32_t serial, const char *name) { return; } wl_pointer_set_cursor(wl_pointer_, serial, - wl_surface_, + wl_surface_cursor_, static_cast(image->hotspot_x), static_cast(image->hotspot_y)); - wl_surface_attach(wl_surface_, buffer, 0, 0); - wl_surface_damage(wl_surface_, 0, 0, + wl_surface_attach(wl_surface_cursor_, buffer, 0, 0); + wl_surface_damage(wl_surface_cursor_, 0, 0, static_cast(image->width), static_cast(image->height)); - wl_surface_commit(wl_surface_); + wl_surface_commit(wl_surface_cursor_); } diff --git a/src/seat/pointer.h b/src/seat/pointer.h index 793ca0a..b7a0b56 100644 --- a/src/seat/pointer.h +++ b/src/seat/pointer.h @@ -96,6 +96,8 @@ class Pointer { void set_cursor(uint32_t serial, const char *name = "right_ptr"); + [[nodiscard]] bool is_cursor_enabled() const { return !disable_cursor_; } + // Disallow copy and assign. Pointer(const Pointer &) = delete; @@ -104,7 +106,7 @@ class Pointer { private: struct wl_pointer *wl_pointer_; std::list observers_{}; - struct wl_surface *wl_surface_; + struct wl_surface *wl_surface_cursor_; struct wl_cursor_theme *theme_{}; const std::optional &wl_shm_; bool disable_cursor_; diff --git a/src/window/window.cc b/src/window/window.cc index 605fcb7..22ea899 100644 --- a/src/window/window.cc +++ b/src/window/window.cc @@ -201,10 +201,13 @@ void Window::update_buffer_geometry() { } } - if (fractional_buffer_scale_ > 0.0) - wp_viewport_set_destination(viewport_, - new_viewport_dest_size.width, - new_viewport_dest_size.height); + if (fractional_buffer_scale_ > 0.0) { + if (viewport_) { + wp_viewport_set_destination(viewport_, + new_viewport_dest_size.width, + new_viewport_dest_size.height); + } + } needs_buffer_geometry_update_ = false; } diff --git a/src/window/window.h b/src/window/window.h index fa9f44f..c010284 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -185,7 +185,6 @@ class Window { int height; } logical_size_; - bool init_buffers_; bool needs_buffer_geometry_update_; static void handle_surface_enter(void *data, @@ -217,9 +216,9 @@ class Window { #endif }; - struct wp_viewport *viewport_; + struct wp_viewport *viewport_{}; - struct wp_fractional_scale_v1 *fractional_scale_; + struct wp_fractional_scale_v1 *fractional_scale_{}; static void handle_preferred_scale(void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1, diff --git a/src/window/xdg_toplevel.cc b/src/window/xdg_toplevel.cc index e5122a9..5e4eca8 100644 --- a/src/window/xdg_toplevel.cc +++ b/src/window/xdg_toplevel.cc @@ -61,7 +61,6 @@ XdgTopLevel::XdgTopLevel(WindowManager *wm, const char *title, const char *app_i } wait_for_configure_ = true; - //wl_surface_damage(wl_surface_, 0, 0, width, height); wl_surface_commit(wl_surface_); // this makes the start-up from the beginning with the correct dimensions