kodi
Registry.h
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 
35 class CRegistry
36 {
37 public:
38  explicit CRegistry(CConnection& connection);
39 
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 */>;
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 
105  void Bind();
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 }
Handle Wayland globals.
Definition: Registry.h:35
Connection to Wayland compositor.
Definition: Connection.h:25
void Bind()
Create a registry object at the compositor and do an roundtrip to bind objects.
Definition: Registry.cpp:65
void UnbindSingletons()
Unbind all singletons requested with RequestSingleton.
Definition: Registry.cpp:147
Definition: AudioDecoder.h:18
void Request(std::uint32_t minVersion, std::uint32_t maxVersion, const AddHandler &addHandler, const RemoveHandler &removeHandler)
Request a callback when a dynamic global appears or disappears.
Definition: Registry.h:83
void RequestSingleton(T &target, std::uint32_t minVersion, std::uint32_t maxVersion, bool required=true)
Request a static singleton global to be bound to a proxy.
Definition: Registry.h:57