Mir
wayland_app.h
Go to the documentation of this file.
1/*
2 * Copyright © Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 or 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifndef MIRAL_WAYLAND_APP_H
18#define MIRAL_WAYLAND_APP_H
19
20#include <wayland-client.h>
21#include <memory>
22#include <map>
23#include <functional>
24#include <cstdint>
25
26class WaylandApp;
27
28template<typename T>
30{
31public:
33 : proxy{nullptr, [](auto){}}
34 {
35 }
36
37 WaylandObject(T* proxy, void(*destroy)(T*))
38 : proxy{proxy, destroy}
39 {
40 }
41
42 operator T*() const
43 {
44 return proxy.get();
45 }
46
47private:
48 std::unique_ptr<T, void(*)(T*)> proxy;
49};
50
52{
53public:
54 /// Takes ownership of callback
55 static void create(wl_callback* callback, std::function<void()>&& func);
56
57private:
58 WaylandCallback(WaylandObject<wl_callback> callback, std::function<void()>&& func)
59 : callback{std::move(callback)},
60 func{std::move(func)}
61 {
62 }
63
64 static wl_callback_listener const callback_listener;
65
66 WaylandObject<wl_callback> const callback;
67 std::function<void()> const func;
68};
69
71{
72public:
73 WaylandOutput(WaylandApp* app, wl_output* output);
74 virtual ~WaylandOutput() = default;
75
76 auto scale() const -> int { return scale_; }
77 auto transform() const -> int { return transform_; }
78 auto operator==(wl_output* other) const -> bool { return wl_ == other; }
79 auto wl() const -> wl_output* { return wl_; }
80
81private:
82 static void handle_geometry(
83 void* data,
84 struct wl_output* wl_output,
85 int32_t x,
86 int32_t y,
87 int32_t physical_width,
88 int32_t physical_height,
89 int32_t subpixel,
90 const char *make,
91 const char *model,
92 int32_t transform);
93 static void handle_mode(
94 void *data,
95 struct wl_output* wl_output,
96 uint32_t flags,
97 int32_t width,
98 int32_t height,
99 int32_t refresh);
100 static void handle_done(void* data, struct wl_output* wl_output);
101 static void handle_scale(void* data, struct wl_output* wl_output, int32_t factor);
102
103 static wl_output_listener const output_listener;
104
105 WaylandApp* const app;
106 WaylandObject<wl_output> const wl_;
107 bool has_initialized;
108 bool state_dirty;
109 int scale_;
110 int32_t transform_;
111};
112
114{
115public:
116 WaylandApp();
117 WaylandApp(wl_display* display);
118 virtual ~WaylandApp() = default;
119
120 /// Needs to be two-step initialized to virtual methods are called
121 void wayland_init(wl_display* display);
122 void roundtrip() const;
123
124 auto display() const -> wl_display* { return display_.get(); };
125 auto compositor() const -> wl_compositor* { return compositor_; };
126 auto shm() const -> wl_shm* { return shm_; };
127 auto seat() const -> wl_seat* { return seat_; };
128 auto shell() const -> wl_shell* { return shell_; };
129
130protected:
132 virtual void output_ready(WaylandOutput const*) {};
133 virtual void output_changed(WaylandOutput const*) {};
134 virtual void output_gone(WaylandOutput const*) {};
135
136private:
137 /// Doesn't disconnect the display, instead roundtrips it to make sure everything is cleaned up
138 std::unique_ptr<wl_display, decltype(&wl_display_roundtrip)> display_;
139 WaylandObject<wl_registry> registry_;
140
141 WaylandObject<wl_compositor> compositor_;
142 WaylandObject<wl_shm> shm_;
143 WaylandObject<wl_seat> seat_;
144 WaylandObject<wl_shell> shell_;
145
146 static void handle_new_global(
147 void* data,
148 struct wl_registry* registry,
149 uint32_t id,
150 char const* interface,
151 uint32_t version);
152 static void handle_global_remove(void* data, struct wl_registry* registry, uint32_t name);
153
154 static wl_registry_listener const registry_listener;
155
156 /// Functions to call then drop when globals are removed
157 std::map<uint32_t, std::function<void()>> global_remove_handlers;
158};
159
160#endif // MIRAL_WAYLAND_APP_H
Definition: wayland_app.h:114
void roundtrip() const
Definition: wayland_app.cpp:152
virtual ~WaylandApp()=default
auto compositor() const -> wl_compositor *
Definition: wayland_app.h:125
virtual void output_gone(WaylandOutput const *)
Definition: wayland_app.h:134
virtual void output_ready(WaylandOutput const *)
Definition: wayland_app.h:132
WaylandApp()
Definition: wayland_app.cpp:127
auto shell() const -> wl_shell *
Definition: wayland_app.h:128
virtual void output_changed(WaylandOutput const *)
Definition: wayland_app.h:133
auto seat() const -> wl_seat *
Definition: wayland_app.h:127
void wayland_init(wl_display *display)
Needs to be two-step initialized to virtual methods are called.
Definition: wayland_app.cpp:138
WaylandApp(wl_display *display)
Definition: wayland_app.cpp:132
auto shm() const -> wl_shm *
Definition: wayland_app.h:126
auto display() const -> wl_display *
Definition: wayland_app.h:124
Definition: wayland_app.h:52
static void create(wl_callback *callback, std::function< void()> &&func)
Takes ownership of callback.
Definition: wayland_app.cpp:43
Definition: wayland_app.h:30
WaylandObject(T *proxy, void(*destroy)(T *))
Definition: wayland_app.h:37
operator T*() const
Definition: wayland_app.h:42
WaylandObject()
Definition: wayland_app.h:32
Definition: wayland_app.h:71
auto operator==(wl_output *other) const -> bool
Definition: wayland_app.h:78
auto scale() const -> int
Definition: wayland_app.h:76
WaylandOutput(WaylandApp *app, wl_output *output)
Definition: wayland_app.cpp:59
auto wl() const -> wl_output *
Definition: wayland_app.h:79
virtual ~WaylandOutput()=default
auto transform() const -> int
Definition: wayland_app.h:77

Copyright © 2012-2023 Canonical Ltd.
Generated on Tue 2 May 10:01:24 UTC 2023
This documentation is licensed under the GPL version 2 or 3.