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:
57  WaylandOutput(WaylandApp* app, wl_output* output);
58  virtual ~WaylandOutput() = default;
59 
60  auto scale() const -> int { return scale_; }
61  auto transform() const -> int { return transform_; }
62  auto operator==(wl_output* other) const -> bool { return wl_ == other; }
63  auto wl() const -> wl_output* { return wl_; }
64 
65 private:
66  static void handle_geometry(
67  void* data,
68  struct wl_output* wl_output,
69  int32_t x,
70  int32_t y,
71  int32_t physical_width,
72  int32_t physical_height,
73  int32_t subpixel,
74  const char *make,
75  const char *model,
76  int32_t transform);
77  static void handle_mode(
78  void *data,
79  struct wl_output* wl_output,
80  uint32_t flags,
81  int32_t width,
82  int32_t height,
83  int32_t refresh);
84  static void handle_done(void* data, struct wl_output* wl_output);
85  static void handle_scale(void* data, struct wl_output* wl_output, int32_t factor);
86 
87  static wl_output_listener const output_listener;
88 
89  WaylandApp* const app;
90  WaylandObject<wl_output> const wl_;
91  bool has_initialized;
92  bool state_dirty;
93  int scale_;
94  int32_t transform_;
95 };
96 
98 {
99 public:
100  WaylandApp();
101  WaylandApp(wl_display* display);
102  virtual ~WaylandApp() = default;
103 
105  void wayland_init(wl_display* display);
106  void roundtrip() const;
107 
108  auto display() const -> wl_display* { return display_.get(); };
109  auto compositor() const -> wl_compositor* { return compositor_; };
110  auto shm() const -> wl_shm* { return shm_; };
111  auto seat() const -> wl_seat* { return seat_; };
112  auto shell() const -> wl_shell* { return shell_; };
113 
114 protected:
116  virtual void output_ready(WaylandOutput const*) {};
117  virtual void output_changed(WaylandOutput const*) {};
118  virtual void output_gone(WaylandOutput const*) {};
119 
120 private:
122  std::unique_ptr<wl_display, decltype(&wl_display_roundtrip)> display_;
123  WaylandObject<wl_registry> registry_;
124 
125  WaylandObject<wl_compositor> compositor_;
129 
130  static void handle_new_global(
131  void* data,
132  struct wl_registry* registry,
133  uint32_t id,
134  char const* interface,
135  uint32_t version);
136  static void handle_global_remove(void* data, struct wl_registry* registry, uint32_t name);
137 
138  static wl_registry_listener const registry_listener;
139 
141  std::map<uint32_t, std::function<void()>> global_remove_handlers;
142 };
143 
144 #endif // MIRAL_WAYLAND_APP_H
Definition: wayland_app.h:98
void roundtrip() const
Definition: wayland_app.cpp:124
virtual ~WaylandApp()=default
auto compositor() const -> wl_compositor *
Definition: wayland_app.h:109
virtual void output_gone(WaylandOutput const *)
Definition: wayland_app.h:118
virtual void output_ready(WaylandOutput const *)
Definition: wayland_app.h:116
WaylandApp()
Definition: wayland_app.cpp:99
friend WaylandOutput
Definition: wayland_app.h:112
auto shell() const -> wl_shell *
Definition: wayland_app.h:112
virtual void output_changed(WaylandOutput const *)
Definition: wayland_app.h:117
auto seat() const -> wl_seat *
Definition: wayland_app.h:111
void wayland_init(wl_display *display)
Needs to be two-step initialized to virtual methods are called.
Definition: wayland_app.cpp:110
auto shm() const -> wl_shm *
Definition: wayland_app.h:110
auto display() const -> wl_display *
Definition: wayland_app.h:108
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:55
auto operator==(wl_output *other) const -> bool
Definition: wayland_app.h:62
auto scale() const -> int
Definition: wayland_app.h:60
WaylandOutput(WaylandApp *app, wl_output *output)
Definition: wayland_app.cpp:31
auto wl() const -> wl_output *
Definition: wayland_app.h:63
virtual ~WaylandOutput()=default
auto transform() const -> int
Definition: wayland_app.h:61

Copyright © 2012-2021 Canonical Ltd.
Generated on Wed Mar 31 15:54:02 UTC 2021
This documentation is licensed under the GPL version 2 or 3.