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 
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 
26 class WaylandApp;
27 
28 template<typename T>
30 {
31 public:
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 
47 private:
48  std::unique_ptr<T, void(*)(T*)> proxy;
49 };
50 
52 {
53 public:
55  static void create(wl_callback* callback, std::function<void()>&& func);
56 
57 private:
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 {
72 public:
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 
81 private:
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 {
115 public:
116  WaylandApp();
117  WaylandApp(wl_display* display);
118  virtual ~WaylandApp() = default;
119 
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 
130 protected:
132  virtual void output_ready(WaylandOutput const*) {};
133  virtual void output_changed(WaylandOutput const*) {};
134  virtual void output_gone(WaylandOutput const*) {};
135 
136 private:
138  std::unique_ptr<wl_display, decltype(&wl_display_roundtrip)> display_;
139  WaylandObject<wl_registry> registry_;
140 
141  WaylandObject<wl_compositor> compositor_;
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 
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
friend WaylandOutput
Definition: wayland_app.h:128
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
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
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-2022 Canonical Ltd.
Generated on Tue May 31 01:20:27 UTC 2022
This documentation is licensed under the GPL version 2 or 3.