Add C++20 RAII wrapper around wlroots 0.20 + scenefx
Wraps wlroots' C ABI behind owning types (Display, Backend, Compositor, Output, XdgShell/Toplevel, Seat/Cursor/Keyboard, Scene/Node/Tree) with wl_signal -> std::function callbacks. Links the scenefx effect engine and ships a wlr::fx animation helper. Includes a runnable tinywl-style compositor and a scene/blur demo. License: AGPL-3.0-only.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "wlr/display.hpp"
|
||||
#include "wlr/signals.hpp"
|
||||
#include "wlr/compositor.hpp"
|
||||
|
||||
namespace wlr {
|
||||
|
||||
class Backend {
|
||||
public:
|
||||
explicit Backend(Display& display, bool headless = false) {
|
||||
m_backend = wlr_backend_autocreate(display.event_loop(), nullptr);
|
||||
}
|
||||
|
||||
~Backend() {
|
||||
m_newOutput.disconnect();
|
||||
m_newInput.disconnect();
|
||||
if (m_backend)
|
||||
wlr_backend_destroy(m_backend);
|
||||
}
|
||||
|
||||
Backend(const Backend&) = delete;
|
||||
Backend& operator=(const Backend&) = delete;
|
||||
|
||||
struct wlr_backend* raw() const { return m_backend; }
|
||||
explicit operator bool() const { return m_backend != nullptr; }
|
||||
bool start() { return wlr_backend_start(m_backend); }
|
||||
|
||||
// cb receives struct wlr_output*
|
||||
void on_new_output(std::function<void(void*)> cb) {
|
||||
m_newOutput.connect(&m_backend->events.new_output, std::move(cb));
|
||||
}
|
||||
// cb receives struct wlr_input_device*
|
||||
void on_new_input(std::function<void(void*)> cb) {
|
||||
m_newInput.connect(&m_backend->events.new_input, std::move(cb));
|
||||
}
|
||||
|
||||
private:
|
||||
struct wlr_backend* m_backend;
|
||||
SignalSink m_newOutput;
|
||||
SignalSink m_newInput;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include "wlr/display.hpp"
|
||||
#include "wlr/signals.hpp"
|
||||
#include "wlr/scene.hpp"
|
||||
#include "wlr/node.hpp"
|
||||
#include "wlr/output.hpp"
|
||||
|
||||
namespace wlr {
|
||||
|
||||
class Renderer {
|
||||
public:
|
||||
Renderer(struct wlr_renderer* r) : m_renderer(r) {}
|
||||
struct wlr_renderer* raw() const { return m_renderer; }
|
||||
explicit operator bool() const { return m_renderer != nullptr; }
|
||||
bool init_wl_display(Display& d) { return wlr_renderer_init_wl_display(m_renderer, d.raw()); }
|
||||
void destroy() { if (m_renderer) { wlr_renderer_destroy(m_renderer); m_renderer = nullptr; } }
|
||||
~Renderer() { destroy(); }
|
||||
|
||||
private:
|
||||
struct wlr_renderer* m_renderer;
|
||||
};
|
||||
|
||||
class Allocator {
|
||||
public:
|
||||
Allocator(struct wlr_allocator* a) : m_allocator(a) {}
|
||||
struct wlr_allocator* raw() const { return m_allocator; }
|
||||
explicit operator bool() const { return m_allocator != nullptr; }
|
||||
void destroy() { if (m_allocator) { wlr_allocator_destroy(m_allocator); m_allocator = nullptr; } }
|
||||
~Allocator() { destroy(); }
|
||||
|
||||
private:
|
||||
struct wlr_allocator* m_allocator;
|
||||
};
|
||||
|
||||
class DataDeviceManager {
|
||||
public:
|
||||
DataDeviceManager(struct wlr_data_device_manager* m) : m_manager(m) {}
|
||||
struct wlr_data_device_manager* raw() const { return m_manager; }
|
||||
|
||||
private:
|
||||
struct wlr_data_device_manager* m_manager;
|
||||
};
|
||||
|
||||
class Compositor {
|
||||
public:
|
||||
Compositor(Display& display, Renderer& renderer)
|
||||
: m_compositor(wlr_compositor_create(display.raw(), 6, renderer.raw())) {}
|
||||
struct wlr_compositor* raw() const { return m_compositor; }
|
||||
explicit operator bool() const { return m_compositor != nullptr; }
|
||||
|
||||
private:
|
||||
struct wlr_compositor* m_compositor;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <wayland-server-core.h>
|
||||
|
||||
#include "wlr/wlr.hpp"
|
||||
|
||||
namespace wlr {
|
||||
|
||||
class Display {
|
||||
public:
|
||||
Display() : m_display(wl_display_create()) {}
|
||||
~Display() { if (m_display) wl_display_destroy(m_display); }
|
||||
|
||||
Display(const Display&) = delete;
|
||||
Display& operator=(const Display&) = delete;
|
||||
Display(Display&&) = delete;
|
||||
Display& operator=(Display&&) = delete;
|
||||
|
||||
struct wl_display* raw() const { return m_display; }
|
||||
explicit operator bool() const { return m_display != nullptr; }
|
||||
struct wl_event_loop* event_loop() const { return wl_display_get_event_loop(m_display); }
|
||||
|
||||
void set_terminate() { wl_display_terminate(m_display); }
|
||||
void run() { wl_display_run(m_display); }
|
||||
bool init_shm() { return wl_display_init_shm(m_display); }
|
||||
const char* add_socket_auto() {
|
||||
const char* name = wl_display_add_socket_auto(m_display);
|
||||
return name;
|
||||
}
|
||||
void flush_clients() { wl_display_flush_clients(m_display); }
|
||||
|
||||
private:
|
||||
struct wl_display* m_display;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "wlr/wlr.hpp"
|
||||
#include "wlr/scene.hpp"
|
||||
|
||||
namespace wlr {
|
||||
|
||||
class Node {
|
||||
public:
|
||||
Node(struct wlr_scene_node* node) : m_node(node) {}
|
||||
struct wlr_scene_node* raw() const { return m_node; }
|
||||
explicit operator bool() const { return m_node != nullptr; }
|
||||
|
||||
void set_position(int x, int y) { wlr_scene_node_set_position(m_node, x, y); }
|
||||
std::pair<int, int> position() const { return {m_node->x, m_node->y}; }
|
||||
|
||||
void set_enabled(bool enabled) { wlr_scene_node_set_enabled(m_node, enabled); }
|
||||
bool enabled() const { return m_node->enabled; }
|
||||
|
||||
void raise_to_top() { wlr_scene_node_raise_to_top(m_node); }
|
||||
void lower_to_bottom() { wlr_scene_node_lower_to_bottom(m_node); }
|
||||
void place_above(Node& sibling) { wlr_scene_node_place_above(m_node, sibling.raw()); }
|
||||
void place_below(Node& sibling) { wlr_scene_node_place_below(m_node, sibling.raw()); }
|
||||
|
||||
enum wlr_scene_node_type type() const { return m_node->type; }
|
||||
struct wlr_scene_tree* parent() const { return m_node->parent; }
|
||||
void*& data() { return m_node->data; }
|
||||
void* data() const { return m_node->data; }
|
||||
|
||||
template <typename T> T as() const;
|
||||
|
||||
protected:
|
||||
struct wlr_scene_node* m_node;
|
||||
};
|
||||
|
||||
class Tree : public Node {
|
||||
public:
|
||||
Tree(struct wlr_scene_tree* tree) : Node(&tree->node), m_tree(tree) {}
|
||||
struct wlr_scene_tree* raw() const { return m_tree; }
|
||||
void reparent(Node& n) { wlr_scene_node_reparent(n.raw(), m_tree); }
|
||||
|
||||
private:
|
||||
struct wlr_scene_tree* m_tree;
|
||||
};
|
||||
|
||||
class Rect : public Node {
|
||||
public:
|
||||
Rect(struct wlr_scene_rect* rect) : Node(&rect->node), m_rect(rect) {}
|
||||
struct wlr_scene_rect* raw() const { return m_rect; }
|
||||
|
||||
void set_size(int w, int h) { wlr_scene_rect_set_size(m_rect, w, h); }
|
||||
void set_color(const Color& c) { c.apply(m_rect->color); }
|
||||
void set_corner_radius(int radius) { wlr_scene_rect_set_corner_radius(m_rect, radius); }
|
||||
void set_corner_radii(const CornerRadii& r) { wlr_scene_rect_set_corner_radii(m_rect, r.raw()); }
|
||||
|
||||
private:
|
||||
struct wlr_scene_rect* m_rect;
|
||||
};
|
||||
|
||||
class Shadow : public Node {
|
||||
public:
|
||||
Shadow(struct wlr_scene_shadow* shadow) : Node(&shadow->node), m_shadow(shadow) {}
|
||||
struct wlr_scene_shadow* raw() const { return m_shadow; }
|
||||
|
||||
void set_size(int w, int h) { wlr_scene_shadow_set_size(m_shadow, w, h); }
|
||||
void set_corner_radius(int radius) { wlr_scene_shadow_set_corner_radius(m_shadow, radius); }
|
||||
void set_blur_sigma(float sigma) { wlr_scene_shadow_set_blur_sigma(m_shadow, sigma); }
|
||||
void set_color(const Color& c) { c.apply(m_shadow->color); }
|
||||
|
||||
private:
|
||||
struct wlr_scene_shadow* m_shadow;
|
||||
};
|
||||
|
||||
class Blur : public Node {
|
||||
public:
|
||||
Blur(struct wlr_scene_blur* blur) : Node(&blur->node), m_blur(blur) {}
|
||||
struct wlr_scene_blur* raw() const { return m_blur; }
|
||||
|
||||
void set_size(int w, int h) { wlr_scene_blur_set_size(m_blur, w, h); }
|
||||
void set_corner_radius(int radius) { wlr_scene_blur_set_corner_radius(m_blur, radius); }
|
||||
void set_corner_radii(const CornerRadii& r) { wlr_scene_blur_set_corner_radii(m_blur, r.raw()); }
|
||||
void set_alpha(float alpha) { wlr_scene_blur_set_alpha(m_blur, alpha); }
|
||||
void set_strength(float strength) { wlr_scene_blur_set_strength(m_blur, strength); }
|
||||
void set_should_only_blur_bottom_layer(bool v) { wlr_scene_blur_set_should_only_blur_bottom_layer(m_blur, v); }
|
||||
|
||||
private:
|
||||
struct wlr_scene_blur* m_blur;
|
||||
};
|
||||
|
||||
class OptimizedBlur : public Node {
|
||||
public:
|
||||
OptimizedBlur(struct wlr_scene_optimized_blur* b) : Node(&b->node), m_blur(b) {}
|
||||
struct wlr_scene_optimized_blur* raw() const { return m_blur; }
|
||||
|
||||
void set_size(int w, int h) { wlr_scene_optimized_blur_set_size(m_blur, w, h); }
|
||||
void mark_dirty() { wlr_scene_optimized_blur_mark_dirty(m_blur); }
|
||||
|
||||
private:
|
||||
struct wlr_scene_optimized_blur* m_blur;
|
||||
};
|
||||
|
||||
class Buffer : public Node {
|
||||
public:
|
||||
Buffer(struct wlr_scene_buffer* buf) : Node(&buf->node), m_buffer(buf) {}
|
||||
struct wlr_scene_buffer* raw() const { return m_buffer; }
|
||||
|
||||
void set_buffer(struct wlr_buffer* b) { wlr_scene_buffer_set_buffer(m_buffer, b); }
|
||||
void set_opacity(float o) { wlr_scene_buffer_set_opacity(m_buffer, o); }
|
||||
float opacity() const { return m_buffer->opacity; }
|
||||
void set_dest_size(int w, int h) { wlr_scene_buffer_set_dest_size(m_buffer, w, h); }
|
||||
void set_corner_radius(int radius) { wlr_scene_buffer_set_corner_radius(m_buffer, radius); }
|
||||
void set_corner_radii(const CornerRadii& r) { wlr_scene_buffer_set_corner_radii(m_buffer, r.raw()); }
|
||||
|
||||
private:
|
||||
struct wlr_scene_buffer* m_buffer;
|
||||
};
|
||||
|
||||
class Surface : public Buffer {
|
||||
public:
|
||||
Surface(struct wlr_scene_surface* s) : Buffer(s ? s->buffer : nullptr), m_surface(s) {}
|
||||
struct wlr_scene_surface* raw() const { return m_surface; }
|
||||
|
||||
private:
|
||||
struct wlr_scene_surface* m_surface;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "wlr/wlr.hpp"
|
||||
#include "wlr/signals.hpp"
|
||||
#include "wlr/display.hpp"
|
||||
#include "wlr/scene.hpp"
|
||||
|
||||
namespace wlr {
|
||||
|
||||
class Output {
|
||||
public:
|
||||
Output(struct wlr_output* o) : m_output(o) {}
|
||||
struct wlr_output* raw() const { return m_output; }
|
||||
explicit operator bool() const { return m_output != nullptr; }
|
||||
const char* name() const { return m_output->name; }
|
||||
const char* make() const { return m_output->make; }
|
||||
const char* model() const { return m_output->model; }
|
||||
|
||||
struct wlr_output_mode* preferred_mode() const {
|
||||
return wlr_output_preferred_mode(m_output);
|
||||
}
|
||||
bool set_preferred_mode() {
|
||||
struct wlr_output_mode* mode = wlr_output_preferred_mode(m_output);
|
||||
if (!mode) return false;
|
||||
struct wlr_output_state state;
|
||||
wlr_output_state_init(&state);
|
||||
wlr_output_state_set_mode(&state, mode);
|
||||
bool ok = wlr_output_commit_state(m_output, &state);
|
||||
wlr_output_state_finish(&state);
|
||||
return ok;
|
||||
}
|
||||
|
||||
private:
|
||||
struct wlr_output* m_output;
|
||||
};
|
||||
|
||||
class SceneOutput {
|
||||
public:
|
||||
SceneOutput(struct wlr_scene_output* so) : m_so(so) {}
|
||||
struct wlr_scene_output* raw() const { return m_so; }
|
||||
explicit operator bool() const { return m_so != nullptr; }
|
||||
struct wlr_output* output() const { return m_so->output; }
|
||||
void set_position(int x, int y) { wlr_scene_output_set_position(m_so, x, y); }
|
||||
bool commit() { return wlr_scene_output_commit(m_so, nullptr); }
|
||||
bool needs_frame() { return wlr_scene_output_needs_frame(m_so); }
|
||||
void send_frame_done(struct timespec* when) { wlr_scene_output_send_frame_done(m_so, when); }
|
||||
|
||||
private:
|
||||
struct wlr_scene_output* m_so;
|
||||
};
|
||||
|
||||
class OutputLayout {
|
||||
public:
|
||||
OutputLayout(Display& display) : m_layout(wlr_output_layout_create(display.raw())) {}
|
||||
~OutputLayout() { if (m_layout) wlr_output_layout_destroy(m_layout); }
|
||||
OutputLayout(const OutputLayout&) = delete;
|
||||
OutputLayout& operator=(const OutputLayout&) = delete;
|
||||
struct wlr_output_layout* raw() const { return m_layout; }
|
||||
explicit operator bool() const { return m_layout != nullptr; }
|
||||
struct wlr_output_layout_output* add_auto_output(struct wlr_output* o) {
|
||||
return wlr_output_layout_add_auto(m_layout, o);
|
||||
}
|
||||
struct wlr_output_layout_output* add_output(struct wlr_output* o, int x, int y) {
|
||||
return wlr_output_layout_add(m_layout, o, x, y);
|
||||
}
|
||||
struct wlr_box box() const {
|
||||
struct wlr_box b;
|
||||
wlr_output_layout_get_box(m_layout, nullptr, &b);
|
||||
return b;
|
||||
}
|
||||
|
||||
private:
|
||||
struct wlr_output_layout* m_layout;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <pixman.h>
|
||||
#include <string>
|
||||
|
||||
#include "wlr/wlr.hpp"
|
||||
|
||||
namespace wlr {
|
||||
|
||||
std::string version();
|
||||
|
||||
class Scene {
|
||||
public:
|
||||
Scene() : m_scene(wlr_scene_create()) {}
|
||||
~Scene() { if (m_scene) wlr_scene_node_destroy(&m_scene->tree.node); }
|
||||
|
||||
Scene(const Scene&) = delete;
|
||||
Scene& operator=(const Scene&) = delete;
|
||||
Scene(Scene&&) = delete;
|
||||
Scene& operator=(Scene&&) = delete;
|
||||
|
||||
struct wlr_scene* raw() const { return m_scene; }
|
||||
explicit operator bool() const { return m_scene != nullptr; }
|
||||
|
||||
void set_blur_data(int num_passes, int radius, float noise,
|
||||
float brightness, float contrast, float saturation) {
|
||||
wlr_scene_set_blur_data(m_scene, num_passes, radius,
|
||||
noise, brightness, contrast, saturation);
|
||||
}
|
||||
void set_blur_num_passes(int num_passes) { wlr_scene_set_blur_num_passes(m_scene, num_passes); }
|
||||
void set_blur_radius(int radius) { wlr_scene_set_blur_radius(m_scene, radius); }
|
||||
void set_blur_noise(float noise) { wlr_scene_set_blur_noise(m_scene, noise); }
|
||||
void set_blur_brightness(float brightness) { wlr_scene_set_blur_brightness(m_scene, brightness); }
|
||||
void set_blur_contrast(float contrast) { wlr_scene_set_blur_contrast(m_scene, contrast); }
|
||||
void set_blur_saturation(float saturation) { wlr_scene_set_blur_saturation(m_scene, saturation); }
|
||||
struct wlr_scene_tree* root() { return &m_scene->tree; }
|
||||
|
||||
private:
|
||||
struct wlr_scene* m_scene;
|
||||
};
|
||||
|
||||
struct Box {
|
||||
int x = 0, y = 0, width = 0, height = 0;
|
||||
pixman_box32 to_box() const { return {x, y, x + width, y + height}; }
|
||||
static Box from_box(pixman_box32 b) {
|
||||
return {b.x1, b.y1, b.x2 - b.x1, b.y2 - b.y1};
|
||||
}
|
||||
};
|
||||
|
||||
struct Color {
|
||||
float r = 1.0f, g = 1.0f, b = 1.0f, a = 1.0f;
|
||||
void apply(float out[4]) const {
|
||||
out[0] = r; out[1] = g; out[2] = b; out[3] = a;
|
||||
}
|
||||
};
|
||||
|
||||
struct CornerRadii {
|
||||
uint16_t top_left = 0, top_right = 0, bottom_right = 0, bottom_left = 0;
|
||||
|
||||
CornerRadii() = default;
|
||||
CornerRadii(uint16_t all) : top_left(all), top_right(all), bottom_right(all), bottom_left(all) {}
|
||||
CornerRadii(uint16_t tl, uint16_t tr, uint16_t br, uint16_t bl)
|
||||
: top_left(tl), top_right(tr), bottom_right(br), bottom_left(bl) {}
|
||||
|
||||
fx_corner_radii raw() const {
|
||||
return fx_corner_radii{top_left, top_right, bottom_right, bottom_left};
|
||||
}
|
||||
static CornerRadii from_radii(fx_corner_radii r) {
|
||||
return {r.top_left, r.top_right, r.bottom_right, r.bottom_left};
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
namespace wlr::fx {
|
||||
|
||||
enum class Easing {
|
||||
Linear,
|
||||
InQuad, OutQuad, InOutQuad,
|
||||
InCubic, OutCubic, InOutCubic,
|
||||
InElastic, OutElastic, InOutElastic,
|
||||
};
|
||||
|
||||
inline float ease(Easing e, float t) {
|
||||
t = std::clamp(t, 0.0f, 1.0f);
|
||||
switch (e) {
|
||||
case Easing::Linear: return t;
|
||||
case Easing::InQuad: return t * t;
|
||||
case Easing::OutQuad: return t * (2.0f - t);
|
||||
case Easing::InCubic: return t * t * t;
|
||||
case Easing::OutCubic: { float u = t - 1.0f; return u * u * u + 1.0f; }
|
||||
case Easing::InOutQuad: return t < 0.5f ? 2.0f * t * t : -1.0f + (4.0f - 2.0f * t) * t;
|
||||
case Easing::InOutCubic: return t < 0.5f ? 4.0f * t * t * t : (t - 1.0f) * (2.0f * t - 2.0f) * (2.0f * t - 2.0f) + 1.0f;
|
||||
case Easing::InElastic: {
|
||||
const float c4 = (2.0f * M_PI) / 3.0f;
|
||||
return t <= 0.0f ? 0.0f : t >= 1.0f ? 1.0f
|
||||
: -std::pow(2.0f, 10.0f * t - 10.0f) * std::sin((t * 10.0f - 10.75f) * c4);
|
||||
}
|
||||
case Easing::OutElastic: {
|
||||
const float c4 = (2.0f * M_PI) / 3.0f;
|
||||
return t <= 0.0f ? 0.0f : t >= 1.0f ? 1.0f
|
||||
: std::pow(2.0f, -10.0f * t) * std::sin((t * 10.0f - 0.75f) * c4) + 1.0f;
|
||||
}
|
||||
case Easing::InOutElastic: {
|
||||
const float c5 = (2.0f * M_PI) / 4.5f;
|
||||
return t <= 0.0f ? 0.0f : t >= 1.0f ? 1.0f
|
||||
: t < 0.5f
|
||||
? -(std::pow(2.0f, 20.0f * t - 10.0f) * std::sin((20.0f * t - 11.125f) * c5)) / 2.0f
|
||||
: (std::pow(2.0f, -20.0f * t + 10.0f) * std::sin((20.0f * t - 11.125f) * c5)) / 2.0f + 1.0f;
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
template <typename Clock = std::uint64_t>
|
||||
class Animated {
|
||||
public:
|
||||
Animated() = default;
|
||||
Animated(float value) : m_value(value), m_target(value), m_equal(true) {}
|
||||
|
||||
void set(float value) {
|
||||
if (value == m_target && !m_equal) { jump_to(value); return; }
|
||||
m_target = value;
|
||||
if (m_duration == 0) { jump_to(value); return; }
|
||||
m_startValue = m_value;
|
||||
m_startTime = now();
|
||||
m_elapsed = 0;
|
||||
m_equal = false;
|
||||
}
|
||||
|
||||
void jump_to(float value) {
|
||||
m_value = value; m_target = value; m_equal = true; m_startTime = now();
|
||||
}
|
||||
|
||||
void on_frame(Clock time) {
|
||||
if (m_equal) return;
|
||||
m_elapsed = time - m_startTime;
|
||||
if (m_elapsed >= m_duration) { m_value = m_target; m_equal = true; return; }
|
||||
const float t = ease(m_easing, static_cast<float>(m_elapsed) / static_cast<float>(m_duration));
|
||||
m_value = m_startValue + (m_target - m_startValue) * t;
|
||||
}
|
||||
|
||||
void set_duration(Clock d) { m_duration = d > 0 ? d : 1; }
|
||||
void set_easing(Easing e) { m_easing = e; }
|
||||
void set_clock_source(std::function<Clock()> src) { m_clock = std::move(src); }
|
||||
|
||||
float value() const { return m_value; }
|
||||
float target() const { return m_target; }
|
||||
bool animating() const { return !m_equal; }
|
||||
Clock remaining() const { return m_equal ? 0 : m_duration - m_elapsed; }
|
||||
|
||||
private:
|
||||
Clock now() { return m_clock ? m_clock() : Clock(0); }
|
||||
float m_value = 0.0f;
|
||||
float m_startValue = 0.0f;
|
||||
float m_target = 0.0f;
|
||||
Clock m_duration = 1;
|
||||
Clock m_startTime = 0;
|
||||
Clock m_elapsed = 0;
|
||||
bool m_equal = true;
|
||||
Easing m_easing = Easing::OutCubic;
|
||||
std::function<Clock()> m_clock;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
#include "wlr/scenefx/anim.hpp"
|
||||
#include "wlr/scene.hpp"
|
||||
#include "wlr/node.hpp"
|
||||
|
||||
namespace wlr::fx {
|
||||
|
||||
class Effect {
|
||||
public:
|
||||
Animated<>& position_x() { return m_x; }
|
||||
Animated<>& position_y() { return m_y; }
|
||||
Animated<>& scale() { return m_scale; }
|
||||
Animated<>& opacity() { return m_opacity; }
|
||||
|
||||
void on_frame(std::uint64_t time) {
|
||||
m_x.on_frame(time);
|
||||
m_y.on_frame(time);
|
||||
m_scale.on_frame(time);
|
||||
m_opacity.on_frame(time);
|
||||
}
|
||||
|
||||
void apply_to(Node& node) {
|
||||
node.set_position((int)m_x.value(), (int)m_y.value());
|
||||
if (node.type() != WLR_SCENE_NODE_BUFFER)
|
||||
return;
|
||||
Buffer b = node.as<Buffer>();
|
||||
b.set_opacity(m_opacity.value());
|
||||
const float s = m_scale.value();
|
||||
const int dw = b.raw()->dst_width;
|
||||
const int dh = b.raw()->dst_height;
|
||||
if (s != 1.0f && dw > 0 && dh > 0)
|
||||
b.set_dest_size((int)(s * dw), (int)(s * dh));
|
||||
}
|
||||
|
||||
bool animating() const {
|
||||
return m_x.animating() || m_y.animating() ||
|
||||
m_scale.animating() || m_opacity.animating();
|
||||
}
|
||||
|
||||
private:
|
||||
Animated<> m_x;
|
||||
Animated<> m_y;
|
||||
Animated<> m_scale{1.0f};
|
||||
Animated<> m_opacity{1.0f};
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
|
||||
#include "wlr/wlr.hpp"
|
||||
#include "wlr/display.hpp"
|
||||
#include "wlr/signals.hpp"
|
||||
#include "wlr/output.hpp"
|
||||
namespace wlr {
|
||||
|
||||
class Cursor {
|
||||
public:
|
||||
Cursor() : m_cursor(wlr_cursor_create()) {}
|
||||
~Cursor() { if (m_cursor) wlr_cursor_destroy(m_cursor); }
|
||||
Cursor(const Cursor&) = delete;
|
||||
Cursor& operator=(const Cursor&) = delete;
|
||||
struct wlr_cursor* raw() const { return m_cursor; }
|
||||
explicit operator bool() const { return m_cursor != nullptr; }
|
||||
|
||||
void attach_output_layout(struct wlr_output_layout* layout) {
|
||||
wlr_cursor_attach_output_layout(m_cursor, layout);
|
||||
}
|
||||
void attach_input_device(struct wlr_input_device* dev) {
|
||||
wlr_cursor_attach_input_device(m_cursor, dev);
|
||||
}
|
||||
void warp(double x, double y) { wlr_cursor_warp(m_cursor, nullptr, x, y); }
|
||||
|
||||
private:
|
||||
struct wlr_cursor* m_cursor;
|
||||
};
|
||||
|
||||
class Keyboard {
|
||||
public:
|
||||
Keyboard(struct wlr_keyboard* kb) : m_keyboard(kb) {}
|
||||
struct wlr_keyboard* raw() const { return m_keyboard; }
|
||||
explicit operator bool() const { return m_keyboard != nullptr; }
|
||||
|
||||
void set_keymap(struct xkb_keymap* keymap) { wlr_keyboard_set_keymap(m_keyboard, keymap); }
|
||||
void set_default_keymap() {
|
||||
struct xkb_context* ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
||||
if (!ctx) return;
|
||||
struct xkb_keymap* km = xkb_keymap_new_from_names(ctx, nullptr, XKB_KEYMAP_COMPILE_NO_FLAGS);
|
||||
xkb_context_unref(ctx);
|
||||
if (km) {
|
||||
wlr_keyboard_set_keymap(m_keyboard, km);
|
||||
xkb_keymap_unref(km);
|
||||
}
|
||||
}
|
||||
void on_key(std::function<void(void*)> cb) { m_keys.connect(&m_keyboard->events.key, std::move(cb)); }
|
||||
|
||||
private:
|
||||
struct wlr_keyboard* m_keyboard;
|
||||
SignalSink m_keys;
|
||||
};
|
||||
|
||||
class Seat {
|
||||
public:
|
||||
Seat(Display& display, const char* name)
|
||||
: m_seat(wlr_seat_create(display.raw(), name)) {}
|
||||
~Seat() { if (m_seat) wlr_seat_destroy(m_seat); }
|
||||
Seat(const Seat&) = delete;
|
||||
Seat& operator=(const Seat&) = delete;
|
||||
|
||||
struct wlr_seat* raw() const { return m_seat; }
|
||||
explicit operator bool() const { return m_seat != nullptr; }
|
||||
|
||||
void set_capabilities(uint32_t caps) { wlr_seat_set_capabilities(m_seat, caps); }
|
||||
void set_keyboard(Keyboard& kb) { wlr_seat_set_keyboard(m_seat, kb.raw()); }
|
||||
void keyboard_notify_key(uint32_t time, uint32_t keycode, enum wl_keyboard_key_state state) {
|
||||
wlr_seat_keyboard_notify_key(m_seat, time, keycode, state);
|
||||
}
|
||||
void pointer_notify_motion(uint32_t time, double sx, double sy) {
|
||||
wlr_seat_pointer_notify_motion(m_seat, time, sx, sy);
|
||||
}
|
||||
uint32_t pointer_notify_button(uint32_t time, uint32_t button, enum wl_pointer_button_state state) {
|
||||
return wlr_seat_pointer_notify_button(m_seat, time, button, state);
|
||||
}
|
||||
void pointer_notify_axis(uint32_t time, enum wl_pointer_axis axis, double value, int value_discrete, enum wl_pointer_axis_source source) {
|
||||
wlr_seat_pointer_notify_axis(m_seat, time, axis, value, value_discrete, source,
|
||||
WL_POINTER_AXIS_RELATIVE_DIRECTION_IDENTICAL);
|
||||
}
|
||||
void pointer_notify_frame() { wlr_seat_pointer_notify_frame(m_seat); }
|
||||
|
||||
private:
|
||||
struct wlr_seat* m_seat;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
||||
#include <wayland-server-core.h>
|
||||
|
||||
namespace wlr {
|
||||
|
||||
class SignalSink {
|
||||
public:
|
||||
SignalSink() = default;
|
||||
SignalSink(const SignalSink&) = delete;
|
||||
SignalSink& operator=(const SignalSink&) = delete;
|
||||
|
||||
void connect(struct wl_signal* sig, std::function<void(void*)> fn) {
|
||||
if (m_listener && m_listener->attached)
|
||||
return;
|
||||
if (!m_listener)
|
||||
m_listener = new Listener;
|
||||
m_listener->attached = true;
|
||||
m_listener->fn = std::move(fn);
|
||||
m_listener->listener.notify = &SignalSink::trampoline;
|
||||
wl_signal_add(sig, &m_listener->listener);
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
if (m_listener && m_listener->attached) {
|
||||
wl_list_remove(&m_listener->listener.link);
|
||||
m_listener->attached = false;
|
||||
}
|
||||
}
|
||||
|
||||
~SignalSink() {
|
||||
disconnect();
|
||||
delete m_listener;
|
||||
}
|
||||
|
||||
private:
|
||||
struct Listener {
|
||||
struct wl_listener listener{};
|
||||
std::function<void(void*)> fn;
|
||||
bool attached = false;
|
||||
};
|
||||
|
||||
static void trampoline(struct wl_listener* l, void* data) {
|
||||
auto* self = wl_container_of(l, static_cast<Listener*>(nullptr), listener);
|
||||
if (self->fn)
|
||||
self->fn(data);
|
||||
}
|
||||
|
||||
Listener* m_listener = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#define static
|
||||
extern "C" {
|
||||
#include <scenefx/types/wlr_scene.h>
|
||||
#include <wlr/types/wlr_compositor.h>
|
||||
#include <wlr/types/wlr_output.h>
|
||||
#include <wlr/types/wlr_output_layout.h>
|
||||
#include <wlr/types/wlr_seat.h>
|
||||
#include <wlr/types/wlr_keyboard.h>
|
||||
#include <wlr/types/wlr_pointer.h>
|
||||
#include <wlr/types/wlr_cursor.h>
|
||||
#include <wlr/types/wlr_xdg_shell.h>
|
||||
#include <wlr/types/wlr_data_device.h>
|
||||
#include <wlr/backend.h>
|
||||
#include <wlr/render/wlr_renderer.h>
|
||||
#include <wlr/render/allocator.h>
|
||||
#include <wlr/version.h>
|
||||
}
|
||||
#undef static
|
||||
@@ -0,0 +1,124 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "wlr/wlr.hpp"
|
||||
#include "wlr/display.hpp"
|
||||
#include "wlr/signals.hpp"
|
||||
#include "wlr/node.hpp"
|
||||
|
||||
namespace wlr {
|
||||
|
||||
class XdgSurface;
|
||||
|
||||
class Toplevel {
|
||||
public:
|
||||
Toplevel(struct wlr_xdg_toplevel* t) : m_toplevel(t) {}
|
||||
struct wlr_xdg_toplevel* raw() const { return m_toplevel; }
|
||||
explicit operator bool() const { return m_toplevel != nullptr; }
|
||||
|
||||
XdgSurface base() const;
|
||||
struct wlr_surface* surface() const;
|
||||
|
||||
std::string title() const { return m_toplevel->title ? m_toplevel->title : ""; }
|
||||
std::string app_id() const { return m_toplevel->app_id ? m_toplevel->app_id : ""; }
|
||||
int width() const { return m_toplevel->current.width; }
|
||||
int height() const { return m_toplevel->current.height; }
|
||||
bool maximized() const { return m_toplevel->current.maximized; }
|
||||
bool activated() const { return m_toplevel->current.activated; }
|
||||
struct wlr_xdg_toplevel* parent() const { return m_toplevel->parent; }
|
||||
|
||||
uint32_t set_size(int w, int h) { return wlr_xdg_toplevel_set_size(m_toplevel, w, h); }
|
||||
uint32_t set_activated(bool activated) { return wlr_xdg_toplevel_set_activated(m_toplevel, activated); }
|
||||
uint32_t set_maximized(bool maximized) { return wlr_xdg_toplevel_set_maximized(m_toplevel, maximized); }
|
||||
|
||||
void on_request_move(std::function<void(void*)> cb) {
|
||||
m_requestMove.connect(&m_toplevel->events.request_move, std::move(cb));
|
||||
}
|
||||
void on_request_resize(std::function<void(void*)> cb) {
|
||||
m_requestResize.connect(&m_toplevel->events.request_resize, std::move(cb));
|
||||
}
|
||||
void on_request_maximize(std::function<void(void*)> cb) {
|
||||
m_requestMaximize.connect(&m_toplevel->events.request_maximize, std::move(cb));
|
||||
}
|
||||
void on_request_minimize(std::function<void(void*)> cb) {
|
||||
m_requestMinimize.connect(&m_toplevel->events.request_minimize, std::move(cb));
|
||||
}
|
||||
void on_request_fullscreen(std::function<void(void*)> cb) {
|
||||
m_requestFullscreen.connect(&m_toplevel->events.request_fullscreen, std::move(cb));
|
||||
}
|
||||
void on_set_title(std::function<void(void*)> cb) {
|
||||
m_setTitle.connect(&m_toplevel->events.set_title, std::move(cb));
|
||||
}
|
||||
void on_set_app_id(std::function<void(void*)> cb) {
|
||||
m_setAppId.connect(&m_toplevel->events.set_app_id, std::move(cb));
|
||||
}
|
||||
|
||||
public:
|
||||
struct wlr_xdg_toplevel* m_toplevel;
|
||||
SignalSink m_requestMove;
|
||||
SignalSink m_requestResize;
|
||||
SignalSink m_requestMaximize;
|
||||
SignalSink m_requestMinimize;
|
||||
SignalSink m_requestFullscreen;
|
||||
SignalSink m_setTitle;
|
||||
SignalSink m_setAppId;
|
||||
};
|
||||
|
||||
class XdgSurface {
|
||||
public:
|
||||
XdgSurface(struct wlr_xdg_surface* s) : m_surface(s) {}
|
||||
struct wlr_xdg_surface* raw() const { return m_surface; }
|
||||
explicit operator bool() const { return m_surface != nullptr; }
|
||||
bool toplevel_role() const { return m_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL && m_surface->toplevel; }
|
||||
bool popup_role() const { return m_surface->role == WLR_XDG_SURFACE_ROLE_POPUP && m_surface->popup; }
|
||||
struct wlr_surface* surface() const { return m_surface->surface; }
|
||||
uint32_t geometry_width() const { return m_surface->geometry.width; }
|
||||
uint32_t geometry_height() const { return m_surface->geometry.height; }
|
||||
Toplevel toplevel() const { return Toplevel{m_surface->toplevel}; }
|
||||
struct wlr_box geometry() const {
|
||||
return m_surface->geometry;
|
||||
}
|
||||
|
||||
void on_map(std::function<void(void*)> cb) {
|
||||
m_map.connect(&m_surface->surface->events.map, std::move(cb));
|
||||
}
|
||||
void on_unmap(std::function<void(void*)> cb) {
|
||||
m_unmap.connect(&m_surface->surface->events.unmap, std::move(cb));
|
||||
}
|
||||
void on_destroy(std::function<void(void*)> cb) {
|
||||
m_destroy.connect(&m_surface->events.destroy, std::move(cb));
|
||||
}
|
||||
|
||||
public:
|
||||
struct wlr_xdg_surface* m_surface;
|
||||
SignalSink m_map;
|
||||
SignalSink m_unmap;
|
||||
SignalSink m_destroy;
|
||||
};
|
||||
|
||||
inline XdgSurface Toplevel::base() const { return XdgSurface{m_toplevel->base}; }
|
||||
inline struct wlr_surface* Toplevel::surface() const { return m_toplevel->base->surface; }
|
||||
|
||||
class XdgShell {
|
||||
public:
|
||||
explicit XdgShell(Display& display, uint32_t version = 5)
|
||||
: m_shell(wlr_xdg_shell_create(display.raw(), version)) {}
|
||||
struct wlr_xdg_shell* raw() const { return m_shell; }
|
||||
explicit operator bool() const { return m_shell != nullptr; }
|
||||
|
||||
void on_new_toplevel(std::function<void(void*)> cb) {
|
||||
m_newToplevel.connect(&m_shell->events.new_toplevel, std::move(cb));
|
||||
}
|
||||
void on_new_surface(std::function<void(void*)> cb) {
|
||||
m_newSurface.connect(&m_shell->events.new_surface, std::move(cb));
|
||||
}
|
||||
|
||||
private:
|
||||
struct wlr_xdg_shell* m_shell;
|
||||
SignalSink m_newToplevel;
|
||||
SignalSink m_newSurface;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user