Mir
wayland_helpers.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2018 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  */
18 
19 #ifndef MIRAL_WAYLAND_HELPERS_H
20 #define MIRAL_WAYLAND_HELPERS_H
21 
22 #include <wayland-client.h>
23 #include <memory>
24 #include <vector>
25 #include <functional>
26 #include <unordered_map>
27 
28 template<typename Type>
29 auto make_scoped(Type* owned, void(*deleter)(Type*)) -> std::unique_ptr<Type, void(*)(Type*)>
30 {
31  return {owned, deleter};
32 }
33 
34 wl_shm_pool* make_shm_pool(struct wl_shm* shm, int size, void **data);
35 
36 class Output
37 {
38 public:
39  Output(
40  wl_output* output,
41  std::function<void(Output const&)> on_constructed,
42  std::function<void(Output const&)> on_change);
43  ~Output();
44 
45  Output(Output const&) = delete;
46  Output(Output&&) = delete;
47 
48  Output& operator=(Output const&) = delete;
49  Output& operator=(Output&&) = delete;
50 
51  int32_t transform;
52  int scale{1};
53  wl_output* output;
54 private:
55  static void output_done(void* data, wl_output* output);
56 
57  static wl_output_listener const output_listener;
58 
59  std::function<void(Output const&)> on_constructed;
60  std::function<void(Output const&)> on_change;
61 };
62 
63 class Globals
64 {
65 public:
66  Globals(
67  std::function<void(Output const&)> on_new_output,
68  std::function<void(Output const&)> on_output_changed,
69  std::function<void(Output const&)> on_output_gone);
70 
71  wl_compositor* compositor = nullptr;
72  wl_shm* shm = nullptr;
73  wl_seat* seat = nullptr;
74  wl_shell* shell = nullptr;
75 
76  void init(struct wl_display* display);
77  void teardown();
78 
79 private:
80  static void new_global(
81  void* data,
82  struct wl_registry* registry,
83  uint32_t id,
84  char const* interface,
85  uint32_t version);
86 
87  static void global_remove(
88  void* data,
89  struct wl_registry* registry,
90  uint32_t name);
91 
92  wl_registry_listener const registry_listener = {
93  new_global,
94  global_remove
95  };
96 
97  std::unique_ptr<wl_registry, decltype(&wl_registry_destroy)> registry;
98 
99  std::unordered_map<uint32_t, std::unique_ptr<Output>> bound_outputs;
100 
101  std::function<void(Output const&)> const on_new_output;
102  std::function<void(Output const&)> const on_output_changed;
103  std::function<void(Output const&)> const on_output_gone;
104 };
105 
106 #endif //MIRAL_WAYLAND_HELPERS_H
make_scoped
auto make_scoped(Type *owned, void(*deleter)(Type *)) -> std::unique_ptr< Type, void(*)(Type *)>
Definition: wayland_helpers.h:29
Output::scale
int scale
Definition: wayland_helpers.h:52
Output::~Output
~Output()
Definition: wayland_helpers.cpp:160
make_shm_pool
wl_shm_pool * make_shm_pool(struct wl_shm *shm, int size, void **data)
Definition: wayland_helpers.cpp:31
Output::Output
Output(Output &&)=delete
Output::Output
Output(wl_output *output, std::function< void(Output const &)> on_constructed, std::function< void(Output const &)> on_change)
Definition: wayland_helpers.cpp:149
Globals::compositor
wl_compositor * compositor
Definition: wayland_helpers.h:71
Output::transform
int32_t transform
Definition: wayland_helpers.h:51
Globals
Definition: wayland_helpers.h:64
Globals::teardown
void teardown()
Definition: wayland_helpers.cpp:245
Output::operator=
Output & operator=(Output &&)=delete
Output::output
wl_output * output
Definition: wayland_helpers.h:53
Globals::shm
wl_shm * shm
Definition: wayland_helpers.h:72
Globals::Globals
Globals(std::function< void(Output const &)> on_new_output, std::function< void(Output const &)> on_output_changed, std::function< void(Output const &)> on_output_gone)
Definition: wayland_helpers.cpp:166
Output::Output
Output(Output const &)=delete
Globals::shell
wl_shell * shell
Definition: wayland_helpers.h:74
Output::operator=
Output & operator=(Output const &)=delete
Output
Definition: wayland_helpers.h:37
Globals::seat
wl_seat * seat
Definition: wayland_helpers.h:73
Globals::init
void init(struct wl_display *display)
Definition: wayland_helpers.cpp:236

Copyright © 2012-2020 Canonical Ltd.
Generated on Thu Dec 17 06:53:45 UTC 2020
This documentation is licensed under the GPL version 2 or 3.