| 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 "WinEventsWayland.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "application/AppInboundProtocol.h" |
| 13 |
#include "threads/CriticalSection.h" |
| 14 |
#include "threads/Thread.h" |
| 15 |
#include "utils/log.h" |
| 16 |
|
| 17 |
#include "platform/posix/utils/FileHandle.h" |
| 18 |
|
| 19 |
#include <exception> |
| 20 |
#include <memory> |
| 21 |
#include <mutex> |
| 22 |
#include <system_error> |
| 23 |
|
| 24 |
#include <sys/poll.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <wayland-client.hpp> |
| 27 |
|
| 28 |
using namespace KODI::UTILS::POSIX; |
| 29 |
using namespace KODI::WINDOWING::WAYLAND; |
| 30 |
|
| 31 |
namespace |
| 32 |
{ |
| 33 |
/** |
| 34 |
* Thread for processing Wayland events |
| 35 |
* |
| 36 |
* While not strictly needed, reading from the Wayland display file descriptor |
| 37 |
* and dispatching the resulting events is done in an extra thread here. |
| 38 |
* Sometime in the future, MessagePump() might be gone and then the |
| 39 |
* transition will be easier since this extra thread is already here. |
| 40 |
*/ |
| 41 |
class CWinEventsWaylandThread : CThread |
| 42 |
{ |
| 43 |
wayland::display_t& m_display; |
| 44 |
// Pipe used for cancelling poll() on shutdown |
| 45 |
CFileHandle m_pipeRead; |
| 46 |
CFileHandle m_pipeWrite; |
| 47 |
|
| 48 |
CCriticalSection m_roundtripQueueMutex; |
| 49 |
std::atomic<wayland::event_queue_t*> m_roundtripQueue{nullptr}; |
| 50 |
CEvent m_roundtripQueueEvent; |
| 51 |
|
| 52 |
public: |
| 53 |
CWinEventsWaylandThread(wayland::display_t& display) |
| 54 |
: CThread("Wayland message pump"), m_display{display} |
| 55 |
{ |
| 56 |
std::array<int, 2> fds; |
| 57 |
if (pipe(fds.data()) < 0) |
| 58 |
{ |
| 59 |
throw std::system_error(errno, std::generic_category(), "Error creating pipe for Wayland message pump cancellation"); |
| 60 |
} |
| 61 |
m_pipeRead.attach(fds[0]); |
| 62 |
m_pipeWrite.attach(fds[1]); |
| 63 |
Create(); |
| 64 |
} |
| 65 |
|
| 66 |
~CWinEventsWaylandThread() override |
| 67 |
{ |
| 68 |
Stop(); |
| 69 |
// Wait for roundtrip invocation to finish |
| 70 |
std::unique_lock<CCriticalSection> lock(m_roundtripQueueMutex); |
| 71 |
} |
| 72 |
|
| 73 |
void Stop() |
| 74 |
{ |
| 75 |
CLog::Log(LOGDEBUG, "Stopping Wayland message pump"); |
| 76 |
// Set m_bStop |
| 77 |
StopThread(false); |
| 78 |
InterruptPoll(); |
| 79 |
// Now wait for actual exit |
| 80 |
StopThread(true); |
| 81 |
} |
| 82 |
|
| 83 |
void RoundtripQueue(wayland::event_queue_t const& queue) |
| 84 |
{ |
| 85 |
wayland::event_queue_t queueCopy{queue}; |
| 86 |
|
| 87 |
// Serialize invocations of this function - it's used very rarely and usually |
| 88 |
// not in parallel anyway, and doing it avoids lots of complications |
| 89 |
std::unique_lock<CCriticalSection> lock(m_roundtripQueueMutex); |
| 90 |
|
| 91 |
m_roundtripQueueEvent.Reset(); |
| 92 |
// We can just set the value here since there is no other writer in parallel |
| 93 |
m_roundtripQueue.store(&queueCopy); |
| 94 |
// Dispatching can happen now |
| 95 |
|
| 96 |
// Make sure we don't wait for an event to happen on the socket |
| 97 |
InterruptPoll(); |
| 98 |
|
| 99 |
if (m_bStop) |
| 100 |
return; |
| 101 |
|
| 102 |
m_roundtripQueueEvent.Wait(); |
| 103 |
} |
| 104 |
|
| 105 |
wayland::display_t& GetDisplay() |
| 106 |
{ |
| 107 |
return m_display; |
| 108 |
} |
| 109 |
|
| 110 |
private: |
| 111 |
void InterruptPoll() |
| 112 |
{ |
| 113 |
char c = 0; |
| 114 |
if (write(m_pipeWrite, &c, 1) != 1) |
| 115 |
throw std::runtime_error("Failed to write to wayland message pipe"); |
| 116 |
} |
| 117 |
|
| 118 |
void Process() override |
| 119 |
{ |
| 120 |
try |
| 121 |
{ |
| 122 |
std::array<pollfd, 2> pollFds; |
| 123 |
pollfd& waylandPoll = pollFds[0]; |
| 124 |
pollfd& cancelPoll = pollFds[1]; |
| 125 |
// Wayland filedescriptor |
| 126 |
waylandPoll.fd = m_display.get_fd(); |
| 127 |
waylandPoll.events = POLLIN; |
| 128 |
waylandPoll.revents = 0; |
| 129 |
// Read end of the cancellation pipe |
| 130 |
cancelPoll.fd = m_pipeRead; |
| 131 |
cancelPoll.events = POLLIN; |
| 132 |
cancelPoll.revents = 0; |
| 133 |
|
| 134 |
CLog::Log(LOGDEBUG, "Starting Wayland message pump"); |
| 135 |
|
| 136 |
// Run until cancelled or error |
| 137 |
while (!m_bStop) |
| 138 |
{ |
| 139 |
// dispatch() provides no way to cancel a blocked read from the socket |
| 140 |
// wl_display_disconnect would just close the socket, leading to problems |
| 141 |
// with the poll() that dispatch() uses internally - so we have to implement |
| 142 |
// cancellation ourselves here |
| 143 |
|
| 144 |
// Acquire global read intent |
| 145 |
wayland::read_intent readIntent = m_display.obtain_read_intent(); |
| 146 |
m_display.flush(); |
| 147 |
|
| 148 |
if (poll(pollFds.data(), pollFds.size(), -1) < 0) |
| 149 |
{ |
| 150 |
if (errno == EINTR) |
| 151 |
{ |
| 152 |
continue; |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
throw std::system_error(errno, std::generic_category(), "Error polling on Wayland socket"); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
if (cancelPoll.revents & POLLERR || cancelPoll.revents & POLLHUP || cancelPoll.revents & POLLNVAL) |
| 161 |
{ |
| 162 |
throw std::runtime_error("poll() signalled error condition on poll interruption socket"); |
| 163 |
} |
| 164 |
|
| 165 |
if (waylandPoll.revents & POLLERR || waylandPoll.revents & POLLHUP || waylandPoll.revents & POLLNVAL) |
| 166 |
{ |
| 167 |
throw std::runtime_error("poll() signalled error condition on Wayland socket"); |
| 168 |
} |
| 169 |
|
| 170 |
// Read events and release intent; this does not block |
| 171 |
readIntent.read(); |
| 172 |
// Dispatch default event queue |
| 173 |
m_display.dispatch_pending(); |
| 174 |
|
| 175 |
if (auto* roundtripQueue = m_roundtripQueue.exchange(nullptr)) |
| 176 |
{ |
| 177 |
m_display.roundtrip_queue(*roundtripQueue); |
| 178 |
m_roundtripQueueEvent.Set(); |
| 179 |
} |
| 180 |
if (cancelPoll.revents & POLLIN) |
| 181 |
{ |
| 182 |
// Read away the char so we don't get another notification |
| 183 |
// Indepentent from m_roundtripQueue so there are no races |
| 184 |
char c; |
| 185 |
if (read(m_pipeRead, &c, 1) != 1) |
| 186 |
throw std::runtime_error("Error reading from wayland message pipe"); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
CLog::Log(LOGDEBUG, "Wayland message pump stopped"); |
| 191 |
} |
| 192 |
catch (std::exception const& e) |
| 193 |
{ |
| 194 |
// FIXME CThread::OnException is very badly named and should probably go away |
| 195 |
// FIXME Thread exception handling is seriously broken: |
| 196 |
// Exceptions will be swallowed and do not terminate the program. |
| 197 |
// Even XbmcCommons::UncheckedException which claims to be there for just this |
| 198 |
// purpose does not cause termination, the log message will just be slightly different. |
| 199 |
|
| 200 |
// But here, going on would be meaningless, so do a hard exit |
| 201 |
CLog::Log(LOGFATAL, "Exception in Wayland message pump, exiting: {}", e.what()); |
| 202 |
std::terminate(); |
| 203 |
} |
| 204 |
|
| 205 |
// Wake up if someone is still waiting for roundtrip, won't happen anytime soon... |
| 206 |
m_roundtripQueueEvent.Set(); |
| 207 |
} |
| 208 |
}; |
| 209 |
|
| 210 |
std::unique_ptr<CWinEventsWaylandThread> g_WlMessagePump{nullptr}; |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
void CWinEventsWayland::SetDisplay(wayland::display_t* display) |
| 215 |
{ |
| 216 |
if (display && !g_WlMessagePump) |
| 217 |
{ |
| 218 |
// Start message processing as soon as we have a display |
| 219 |
g_WlMessagePump = std::make_unique<CWinEventsWaylandThread>(*display); |
| 220 |
} |
| 221 |
else if (g_WlMessagePump) |
| 222 |
{ |
| 223 |
// Stop if display is set to nullptr |
| 224 |
g_WlMessagePump.reset(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
void CWinEventsWayland::Flush() |
| 229 |
{ |
| 230 |
if (g_WlMessagePump) |
| 231 |
{ |
| 232 |
g_WlMessagePump->GetDisplay().flush(); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
void CWinEventsWayland::RoundtripQueue(const wayland::event_queue_t& queue) |
| 237 |
{ |
| 238 |
if (g_WlMessagePump) |
| 239 |
{ |
| 240 |
g_WlMessagePump->RoundtripQueue(queue); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
bool CWinEventsWayland::MessagePump() |
| 245 |
{ |
| 246 |
std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort(); |
| 247 |
// Forward any events that may have been pushed to our queue |
| 248 |
while (true) |
| 249 |
{ |
| 250 |
XBMC_Event event; |
| 251 |
{ |
| 252 |
// Scoped lock for reentrancy |
| 253 |
std::unique_lock<CCriticalSection> lock(m_queueMutex); |
| 254 |
|
| 255 |
if (m_queue.empty()) |
| 256 |
{ |
| 257 |
break; |
| 258 |
} |
| 259 |
|
| 260 |
// First get event and remove it from the queue, then pass it on - be aware that this |
| 261 |
// function must be reentrant |
| 262 |
event = m_queue.front(); |
| 263 |
m_queue.pop(); |
| 264 |
} |
| 265 |
|
| 266 |
if (appPort) |
| 267 |
appPort->OnEvent(event); |
| 268 |
} |
| 269 |
|
| 270 |
return true; |
| 271 |
} |
| 272 |
|
| 273 |
void CWinEventsWayland::MessagePush(XBMC_Event* ev) |
| 274 |
{ |
| 275 |
std::unique_lock<CCriticalSection> lock(m_queueMutex); |
| 276 |
m_queue.emplace(*ev); |
| 277 |
} |