Mir
wayland_app.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2018, 2021 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  * Authored by: Alan Griffiths <alan@octopull.co.uk>
17  * William Wold <william.wold@canonical.com>
18  */
19 
20 #ifndef MIRAL_WAYLAND_APP_H
21 #define MIRAL_WAYLAND_APP_H
22 
23 #include <wayland-client.h>
24 #include <memory>
25 #include <map>
26 #include <functional>
27 #include <cstdint>
28 
29 class WaylandApp;
30 
31 template<typename T>
33 {
34 public:
36  : proxy{nullptr, [](auto){}}
37  {
38  }
39 
40  WaylandObject(T* proxy, void(*destroy)(T*))
41  : proxy{proxy, destroy}
42  {
43  }
44 
45  operator T*() const
46  {
47  return proxy.get();
48  }
49 
50 private:
51  std::unique_ptr<T, void(*)(T*)> proxy;
52 };
53 
55 {
56 public:
58  static void create(wl_callback* callback, std::function<void()>&& func);
59 
60 private:
61  WaylandCallback(WaylandObject<wl_callback> callback, std::function<void()>&& func)
62  : callback{std::move(callback)},
63  func{std::move(func)}
64  {
65  }
66 
67  static wl_callback_listener const callback_listener;
68 
69  WaylandObject<wl_callback> const callback;
70  std::function<void()> const func;
71 };
72 
74 {
75 public:
76  WaylandOutput(WaylandApp* app, wl_output* output);
77  virtual ~WaylandOutput() = default;
78 
79  auto scale() const -> int { return scale_; }
80  auto transform() const -> int { return transform_; }
81  auto operator==(wl_output* other) const -> bool { return wl_ == other; }
82  auto wl() const -> wl_output* { return wl_; }
83 
84 private:
85  static void handle_geometry(
86  void* data,
87  struct wl_output* wl_output,
88  int32_t x,
89  int32_t y,
90  int32_t physical_width,
91  int32_t physical_height,
92  int32_t subpixel,
93  const char *make,
94  const char *model,
95  int32_t transform);
96  static void handle_mode(
97  void *data,
98  struct wl_output* wl_output,
99  uint32_t flags,
100  int32_t width,
101  int32_t height,
102  int32_t refresh);
103  static void handle_done(void* data, struct wl_output* wl_output);
104  static void handle_scale(void* data, struct wl_output* wl_output, int32_t factor);
105 
106  static wl_output_listener const output_listener;
107 
108  WaylandApp* const app;
109  WaylandObject<wl_output> const wl_;
110  bool has_initialized;
111  bool state_dirty;
112  int scale_;
113  int32_t transform_;
114 };
115 
117 {
118 public:
119  WaylandApp();
120  WaylandApp(wl_display* display);
121  virtual ~WaylandApp() = default;
122 
124  void wayland_init(wl_display* display);
125  void roundtrip() const;
126 
127  auto display() const -> wl_display* { return display_.get(); };
128  auto compositor() const -> wl_compositor* { return compositor_; };
129  auto shm() const -> wl_shm* { return shm_; };
130  auto seat() const -> wl_seat* { return seat_; };
131  auto shell() const -> wl_shell* { return shell_; };
132 
133 protected:
135  virtual void output_ready(WaylandOutput const*) {};
136  virtual void output_changed(WaylandOutput const*) {};
137  virtual void output_gone(WaylandOutput const*) {};
138 
139 private:
141  std::unique_ptr<wl_display, decltype(&wl_display_roundtrip)> display_;
142  WaylandObject<wl_registry> registry_;
143 
144  WaylandObject<wl_compositor> compositor_;
148 
149  static void handle_new_global(
150  void* data,
151  struct wl_registry* registry,
152  uint32_t id,
153  char const* interface,
154  uint32_t version);
155  static void handle_global_remove(void* data, struct wl_registry* registry, uint32_t name);
156 
157  static wl_registry_listener const registry_listener;
158 
160  std::map<uint32_t, std::function<void()>> global_remove_handlers;
161 };
162 
163 #endif // MIRAL_WAYLAND_APP_H
Definition: wayland_app.h:117
void roundtrip() const
Definition: wayland_app.cpp:154
virtual ~WaylandApp()=default
auto compositor() const -> wl_compositor *
Definition: wayland_app.h:128
virtual void output_gone(WaylandOutput const *)
Definition: wayland_app.h:137
virtual void output_ready(WaylandOutput const *)
Definition: wayland_app.h:135
WaylandApp()
Definition: wayland_app.cpp:129
friend WaylandOutput
Definition: wayland_app.h:131
auto shell() const -> wl_shell *
Definition: wayland_app.h:131
virtual void output_changed(WaylandOutput const *)
Definition: wayland_app.h:136
auto seat() const -> wl_seat *
Definition: wayland_app.h:130
void wayland_init(wl_display *display)
Needs to be two-step initialized to virtual methods are called.
Definition: wayland_app.cpp:140
auto shm() const -> wl_shm *
Definition: wayland_app.h:129
auto display() const -> wl_display *
Definition: wayland_app.h:127
Definition: wayland_app.h:55
static void create(wl_callback *callback, std::function< void()> &&func)
Takes ownership of callback.
Definition: wayland_app.cpp:45
Definition: wayland_app.h:33
WaylandObject(T *proxy, void(*destroy)(T *))
Definition: wayland_app.h:40
WaylandObject()
Definition: wayland_app.h:35
Definition: wayland_app.h:74
auto operator==(wl_output *other) const -> bool
Definition: wayland_app.h:81
auto scale() const -> int
Definition: wayland_app.h:79
WaylandOutput(WaylandApp *app, wl_output *output)
Definition: wayland_app.cpp:61
auto wl() const -> wl_output *
Definition: wayland_app.h:82
virtual ~WaylandOutput()=default
auto transform() const -> int
Definition: wayland_app.h:80

Copyright © 2012-2022 Canonical Ltd.
Generated on Fri Feb 25 18:01:37 UTC 2022
This documentation is licensed under the GPL version 2 or 3.