Debian Codegraph @

xbmc/windowing/wayland/Registry.h

raw
1 /*
2 * Copyright (C) 2017-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #pragma once
10
11 #include "Connection.h"
12
13 #include <cstdint>
14 #include <functional>
15 #include <map>
16 #include <utility>
17
18 #include <wayland-client-protocol.hpp>
19
20 namespace KODI
21 {
22 namespace WINDOWING
23 {
24 namespace WAYLAND
25 {
26
27 /**
28 * Handle Wayland globals
29 *
30 * Request singletons (bound once) with \ref RequestSingleton, non-singletons
31 * such as wl_output with \ref Request, then call \ref Bind once.
32 *
33 * Make sure to destroy all registries before destroying the \ref CConnection.
34 */
35 class CRegistry
36 {
37 public:
38 explicit CRegistry(CConnection& connection);
39
40 /**
41 * Request a static singleton global to be bound to a proxy
42 *
43 * You should only use this if the singleton is announced at registry bind time
44 * (not dynamically) and you do not need to catch events that are sent immediately
45 * in response to the bind. Use \ref Request in that case, even if there is
46 * ever only one instance of the object at maximum.
47 *
48 * Cannot be called after \ref Bind has been called.
49 *
50 * \param target target of waylandpp proxy type
51 * \param minVersion minimum version to bind
52 * \param maxVersion maximum version to bind
53 * \param required whether to throw an exception when the bind cannot be satisfied
54 * by the compositor - exception is thrown in \ref Bind
55 */
56 template<typename T>
57 void RequestSingleton(T& target, std::uint32_t minVersion, std::uint32_t maxVersion, bool required = true)
58 {
59 RequestSingletonInternal(target, T::interface_name, minVersion, maxVersion, required);
60 }
61 using AddHandler = std::function<void(std::uint32_t /* name */, wayland::proxy_t&& /* object */)>;
62 using RemoveHandler = std::function<void(std::uint32_t) /* name */>;
63 /**
64 * Request a callback when a dynamic global appears or disappears
65 *
66 * The callbacks may be called from the thread that calls \ref Bind or the
67 * global Wayland message pump thread during \ref Bind (but never at the same
68 * time) and only from the global thread after \ref Bind returns.
69 *
70 * Events that occur immediately upon binding are only delivered reliably
71 * if \ref Bind is called from the Wayland message pump thread.
72 *
73 * Cannot be called after \ref Bind has been called.
74 *
75 * \param minVersion minimum version to bind
76 * \param maxVersion maximum version to bind
77 * \param addHandler function that is called when a global of the requested
78 * type is added
79 * \param removeHandler function that is called when a global of the requested
80 * type is removed
81 */
82 template<typename T>
83 void Request(std::uint32_t minVersion,
84 std::uint32_t maxVersion,
85 const AddHandler& addHandler,
86 const RemoveHandler& removeHandler)
87 {
88 RequestInternal([]{ return T(); }, T::interface_name, minVersion, maxVersion, addHandler, removeHandler);
89 }
90
91 /**
92 * Create a registry object at the compositor and do an roundtrip to bind
93 * objects
94 *
95 * This function blocks until the initial roundtrip is complete. All statically
96 * requested singletons that were available will be bound then.
97 *
98 * Neither statically nor dynamically requested proxies will be bound before this
99 * function is called.
100 *
101 * May throw std::runtime_error if a required global was not found.
102 *
103 * Can only be called once for the same \ref CRegistry object.
104 */
105 void Bind();
106 /**
107 * Unbind all singletons requested with \ref RequestSingleton
108 */
109 void UnbindSingletons();
110
111 private:
112 CRegistry(CRegistry const& other) = delete;
113 CRegistry& operator=(CRegistry const& other) = delete;
114
115 void RequestSingletonInternal(wayland::proxy_t& target, std::string const& interfaceName, std::uint32_t minVersion, std::uint32_t maxVersion, bool required);
116 void RequestInternal(std::function<wayland::proxy_t()> constructor, std::string const& interfaceName, std::uint32_t minVersion, std::uint32_t maxVersion, AddHandler addHandler, RemoveHandler removeHandler);
117 void CheckRequired();
118
119 CConnection& m_connection;
120 wayland::registry_t m_registry;
121
122 struct SingletonBindInfo
123 {
124 wayland::proxy_t& target;
125 // Throw exception if trying to bind below this version and required
126 std::uint32_t minVersion;
127 // Limit bind version to the minimum of this and compositor version
128 std::uint32_t maxVersion;
129 bool required;
130 SingletonBindInfo(wayland::proxy_t& target, std::uint32_t minVersion, std::uint32_t maxVersion, bool required)
131 : target{target}, minVersion{minVersion}, maxVersion{maxVersion}, required{required}
132 {}
133 };
134 std::map<std::string, SingletonBindInfo> m_singletonBinds;
135
136 struct BindInfo
137 {
138 std::function<wayland::proxy_t()> constructor;
139 std::uint32_t minVersion;
140 std::uint32_t maxVersion;
141 AddHandler addHandler;
142 RemoveHandler removeHandler;
143 BindInfo(std::function<wayland::proxy_t()> constructor,
144 std::uint32_t minVersion,
145 std::uint32_t maxVersion,
146 AddHandler addHandler,
147 RemoveHandler removeHandler)
148 : constructor{std::move(constructor)},
149 minVersion{minVersion},
150 maxVersion{maxVersion},
151 addHandler{std::move(addHandler)},
152 removeHandler{std::move(removeHandler)}
153 {}
154 };
155 std::map<std::string, BindInfo> m_binds;
156
157 std::map<std::uint32_t, std::reference_wrapper<BindInfo>> m_boundNames;
158 };
159
160 }
161 }
162 }