| 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 |
#include "Registry.h" |
| 10 |
|
| 11 |
#include "WinEventsWayland.h" |
| 12 |
#include "utils/log.h" |
| 13 |
|
| 14 |
#include <wayland-client-protocol.h> |
| 15 |
|
| 16 |
using namespace KODI::WINDOWING::WAYLAND; |
| 17 |
|
| 18 |
namespace |
| 19 |
{ |
| 20 |
|
| 21 |
void TryBind(wayland::registry_t& registry, wayland::proxy_t& target, std::uint32_t name, std::string const& interface, std::uint32_t minVersion, std::uint32_t maxVersion, std::uint32_t offeredVersion) |
| 22 |
{ |
| 23 |
if (offeredVersion < minVersion) |
| 24 |
{ |
| 25 |
CLog::Log(LOGWARNING, |
| 26 |
"Not binding Wayland protocol {} because server has only version {} (we need at " |
| 27 |
"least version {})", |
| 28 |
interface, offeredVersion, minVersion); |
| 29 |
} |
| 30 |
else |
| 31 |
{ |
| 32 |
// Binding below the offered version is OK |
| 33 |
auto bindVersion = std::min(maxVersion, offeredVersion); |
| 34 |
CLog::Log(LOGDEBUG, "Binding Wayland protocol {} version {} (server has version {})", interface, |
| 35 |
bindVersion, offeredVersion); |
| 36 |
registry.bind(name, target, bindVersion); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
CRegistry::CRegistry(CConnection& connection) |
| 43 |
: m_connection{connection} |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
void CRegistry::RequestSingletonInternal(wayland::proxy_t& target, std::string const& interfaceName, std::uint32_t minVersion, std::uint32_t maxVersion, bool required) |
| 48 |
{ |
| 49 |
if (m_registry) |
| 50 |
{ |
| 51 |
throw std::logic_error("Cannot request more binds from registry after binding has started"); |
| 52 |
} |
| 53 |
m_singletonBinds.emplace(std::piecewise_construct, std::forward_as_tuple(interfaceName), std::forward_as_tuple(target, minVersion, maxVersion, required)); |
| 54 |
} |
| 55 |
|
| 56 |
void CRegistry::RequestInternal(std::function<wayland::proxy_t()> constructor, const std::string& interfaceName, std::uint32_t minVersion, std::uint32_t maxVersion, AddHandler addHandler, RemoveHandler removeHandler) |
| 57 |
{ |
| 58 |
if (m_registry) |
| 59 |
{ |
| 60 |
throw std::logic_error("Cannot request more binds from registry after binding has started"); |
| 61 |
} |
| 62 |
m_binds.emplace(std::piecewise_construct, std::forward_as_tuple(interfaceName), std::forward_as_tuple(constructor, minVersion, maxVersion, addHandler, removeHandler)); |
| 63 |
} |
| 64 |
|
| 65 |
void CRegistry::Bind() |
| 66 |
{ |
| 67 |
if (m_registry) |
| 68 |
{ |
| 69 |
throw std::logic_error("Cannot start binding on registry twice"); |
| 70 |
} |
| 71 |
|
| 72 |
// We want to block in this function until we have received the global interfaces |
| 73 |
// from the compositor - no matter whether the global event pump is running |
| 74 |
// or not. |
| 75 |
// If it is running, we have to take special precautions not to drop events between |
| 76 |
// the creation of the registry and attaching event handlers, so we create |
| 77 |
// an extra queue and use that to dispatch the singleton globals. Then |
| 78 |
// we switch back to the global queue for further dispatch of interfaces |
| 79 |
// added/removed dynamically. |
| 80 |
|
| 81 |
auto registryRoundtripQueue = m_connection.GetDisplay().create_queue(); |
| 82 |
|
| 83 |
auto displayProxy = m_connection.GetDisplay().proxy_create_wrapper(); |
| 84 |
displayProxy.set_queue(registryRoundtripQueue); |
| 85 |
|
| 86 |
m_registry = displayProxy.get_registry(); |
| 87 |
|
| 88 |
m_registry.on_global() = [this](std::uint32_t name, const std::string& interface, |
| 89 |
std::uint32_t version) { |
| 90 |
{ |
| 91 |
auto it = m_singletonBinds.find(interface); |
| 92 |
if (it != m_singletonBinds.end()) |
| 93 |
{ |
| 94 |
auto& bind = it->second; |
| 95 |
auto registryProxy = m_registry.proxy_create_wrapper(); |
| 96 |
// Events on the bound global should always go to the main queue |
| 97 |
registryProxy.set_queue(wayland::event_queue_t()); |
| 98 |
TryBind(registryProxy, bind.target, name, interface, bind.minVersion, bind.maxVersion, version); |
| 99 |
return; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
{ |
| 104 |
auto it = m_binds.find(interface); |
| 105 |
if (it != m_binds.end()) |
| 106 |
{ |
| 107 |
auto& bind = it->second; |
| 108 |
wayland::proxy_t target{bind.constructor()}; |
| 109 |
auto registryProxy = m_registry.proxy_create_wrapper(); |
| 110 |
// Events on the bound global should always go to the main queue |
| 111 |
registryProxy.set_queue(wayland::event_queue_t()); |
| 112 |
TryBind(registryProxy, target, name, interface, bind.minVersion, bind.maxVersion, version); |
| 113 |
if (target) |
| 114 |
{ |
| 115 |
m_boundNames.emplace(name, bind); |
| 116 |
bind.addHandler(name, std::move(target)); |
| 117 |
} |
| 118 |
return; |
| 119 |
} |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
m_registry.on_global_remove() = [this] (std::uint32_t name) |
| 124 |
{ |
| 125 |
auto it = m_boundNames.find(name); |
| 126 |
if (it != m_boundNames.end()) |
| 127 |
{ |
| 128 |
it->second.get().removeHandler(name); |
| 129 |
m_boundNames.erase(it); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
CLog::Log(LOGDEBUG, "Wayland connection: Waiting for global interfaces"); |
| 134 |
m_connection.GetDisplay().roundtrip_queue(registryRoundtripQueue); |
| 135 |
CLog::Log(LOGDEBUG, "Wayland connection: Roundtrip complete"); |
| 136 |
|
| 137 |
CheckRequired(); |
| 138 |
|
| 139 |
// Now switch it to the global queue for further runtime binds |
| 140 |
m_registry.set_queue(wayland::event_queue_t()); |
| 141 |
// Roundtrip extra queue one last time in case something got queued up there. |
| 142 |
// Do it on the event thread so it does not race/run in parallel with the |
| 143 |
// dispatch of newly arrived registry messages in the default queue. |
| 144 |
CWinEventsWayland::RoundtripQueue(registryRoundtripQueue); |
| 145 |
} |
| 146 |
|
| 147 |
void CRegistry::UnbindSingletons() |
| 148 |
{ |
| 149 |
for (auto& bind : m_singletonBinds) |
| 150 |
{ |
| 151 |
bind.second.target.proxy_release(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void CRegistry::CheckRequired() |
| 156 |
{ |
| 157 |
for (auto const& bind : m_singletonBinds) |
| 158 |
{ |
| 159 |
if (bind.second.required && !bind.second.target) |
| 160 |
{ |
| 161 |
throw std::runtime_error(std::string("Missing required ") + bind.first + " protocol"); |
| 162 |
} |
| 163 |
} |
| 164 |
} |