Debian Codegraph @

xbmc/windowing/wayland/SeatSelection.cpp

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 #include "SeatSelection.h"
10
11 #include "Connection.h"
12 #include "Registry.h"
13 #include "WinEventsWayland.h"
14 #include "utils/StringUtils.h"
15 #include "utils/log.h"
16
17 #include "platform/posix/utils/FileHandle.h"
18
19 #include <cerrno>
20 #include <chrono>
21 #include <cstring>
22 #include <mutex>
23 #include <system_error>
24 #include <utility>
25
26 #include <poll.h>
27 #include <unistd.h>
28
29 using namespace KODI::UTILS::POSIX;
30 using namespace KODI::WINDOWING::WAYLAND;
31
32 namespace
33 {
34
35 const std::vector<std::string> MIME_TYPES_PREFERENCE =
36 {
37 "text/plain;charset=utf-8",
38 "text/plain;charset=iso-8859-1",
39 "text/plain;charset=us-ascii",
40 "text/plain"
41 };
42
43 }
44
45 CSeatSelection::CSeatSelection(CConnection& connection, wayland::seat_t const& seat)
46 {
47 wayland::data_device_manager_t manager;
48 {
49 CRegistry registry{connection};
50 registry.RequestSingleton(manager, 1, 3, false);
51 registry.Bind();
52 }
53
54 if (!manager)
55 {
56 CLog::Log(LOGWARNING, "No data device manager announced by compositor, clipboard will not be available");
57 return;
58 }
59
60 m_dataDevice = manager.get_data_device(seat);
61
62 // Class is created in response to seat add events - so no events can get lost
63 m_dataDevice.on_data_offer() = [this](wayland::data_offer_t offer)
64 {
65 // We don't know yet whether this is drag-and-drop or selection, so collect
66 // MIME types in either case
67 m_currentOffer = std::move(offer);
68 m_mimeTypeOffers.clear();
69 m_currentOffer.on_offer() = [this](std::string mime)
70 {
71 m_mimeTypeOffers.push_back(std::move(mime));
72 };
73 };
74 m_dataDevice.on_selection() = [this](const wayland::data_offer_t& offer)
75 {
76 std::unique_lock<CCriticalSection> lock(m_currentSelectionMutex);
77 m_matchedMimeType.clear();
78
79 if (offer != m_currentOffer)
80 {
81 // Selection was not previously introduced by offer (could be NULL for example)
82 m_currentSelection.proxy_release();
83 }
84 else
85 {
86 m_currentSelection = offer;
87 std::string offers = StringUtils::Join(m_mimeTypeOffers, ", ");
88
89 // Match MIME type by priority: Find first preferred MIME type that is in the
90 // set of offered types
91 // Charset is not case-sensitive in MIME type spec, so match case-insensitively
92 auto mimeIt = std::find_first_of(MIME_TYPES_PREFERENCE.cbegin(), MIME_TYPES_PREFERENCE.cend(),
93 m_mimeTypeOffers.cbegin(), m_mimeTypeOffers.cend(),
94 // static_cast needed for overload resolution
95 static_cast<bool (*)(std::string const&, std::string const&)> (&StringUtils::EqualsNoCase));
96 if (mimeIt != MIME_TYPES_PREFERENCE.cend())
97 {
98 m_matchedMimeType = *mimeIt;
99 CLog::Log(LOGDEBUG, "Chose selection MIME type {} out of offered {}", m_matchedMimeType,
100 offers);
101 }
102 else
103 {
104 CLog::Log(LOGDEBUG, "Could not find compatible MIME type for selection data (offered: {})",
105 offers);
106 }
107 }
108 };
109 }
110
111 std::string CSeatSelection::GetSelectionText() const
112 {
113 std::unique_lock<CCriticalSection> lock(m_currentSelectionMutex);
114 if (!m_currentSelection || m_matchedMimeType.empty())
115 {
116 return "";
117 }
118
119 std::array<int, 2> fds;
120 if (pipe(fds.data()) != 0)
121 {
122 CLog::LogF(LOGERROR, "Could not open pipe for selection data transfer: {}",
123 std::strerror(errno));
124 return "";
125 }
126
127 CFileHandle readFd{fds[0]};
128 CFileHandle writeFd{fds[1]};
129
130 m_currentSelection.receive(m_matchedMimeType, writeFd);
131 lock.unlock();
132 // Make sure the other party gets the request as soon as possible
133 CWinEventsWayland::Flush();
134 // Fd now gets sent to the other party -> make sure our write end is closed
135 // so we get POLLHUP when the other party closes its write fd
136 writeFd.reset();
137
138 pollfd fd =
139 {
140 .fd = readFd,
141 .events = POLLIN,
142 .revents = 0
143 };
144
145 // UI will block in this function when Ctrl+V is pressed, so timeout should be
146 // rather short!
147 const std::chrono::seconds TIMEOUT{1};
148 const std::size_t MAX_SIZE{4096};
149 std::array<char, MAX_SIZE> buffer;
150
151 auto start = std::chrono::steady_clock::now();
152 std::size_t totalBytesRead{0};
153
154 do
155 {
156 auto now = std::chrono::steady_clock::now();
157 // Do not permit negative timeouts (would cause infinitely long poll)
158 auto remainingTimeout = std::max(std::chrono::milliseconds(0), std::chrono::duration_cast<std::chrono::milliseconds> (TIMEOUT - (now - start))).count();
159 // poll() for changes until poll signals POLLHUP and the remaining data was read
160 int ret{poll(&fd, 1, remainingTimeout)};
161 if (ret == 0)
162 {
163 // Timeout
164 CLog::LogF(LOGERROR, "Reading from selection data pipe timed out");
165 return "";
166 }
167 else if (ret < 0 && errno == EINTR)
168 {
169 continue;
170 }
171 else if (ret < 0)
172 {
173 throw std::system_error(errno, std::generic_category(), "Error polling selection pipe");
174 }
175 else if (fd.revents & POLLNVAL || fd.revents & POLLERR)
176 {
177 CLog::LogF(LOGERROR, "poll() indicated error on selection pipe");
178 return "";
179 }
180 else if (fd.revents & POLLIN)
181 {
182 if (totalBytesRead >= buffer.size())
183 {
184 CLog::LogF(LOGERROR, "Selection data is too big, aborting read");
185 return "";
186 }
187 ssize_t readBytes{read(fd.fd, buffer.data() + totalBytesRead, buffer.size() - totalBytesRead)};
188 if (readBytes < 0)
189 {
190 CLog::LogF(LOGERROR, "read() from selection pipe failed: {}", std::strerror(errno));
191 return "";
192 }
193 totalBytesRead += readBytes;
194 }
195 }
196 while (!(fd.revents & POLLHUP));
197
198 return std::string(buffer.data(), totalBytesRead);
199 }