12#include <boost/locale.hpp>
13#include <libevdev/libevdev.h>
22 const std::multimap<short, short> key_mappings = {
23 {KEY_BACKSPACE, 0x08},
26 {KEY_LEFTSHIFT, 0x10},
80 {KEY_RIGHTMETA, 0x5C},
91 {KEY_KPASTERISK, 0x6A},
109 {KEY_SCROLLLOCK, 0x91},
110 {KEY_LEFTSHIFT, 0xA0},
111 {KEY_RIGHTSHIFT, 0xA1},
112 {KEY_LEFTCTRL, 0xA2},
113 {KEY_RIGHTCTRL, 0xA3},
115 {KEY_RIGHTALT, 0xA5},
116 {KEY_SEMICOLON, 0xBA},
123 {KEY_LEFTBRACE, 0xDB},
124 {KEY_BACKSLASH, 0xDC},
125 {KEY_RIGHTBRACE, 0xDD},
126 {KEY_APOSTROPHE, 0xDE},
132 input_raw_t::input_raw_t(std::shared_ptr<kwin::connection_t> private_kwin, std::string input_session_tag):
133 session_tag(std::move(input_session_tag)),
134 kwin(std::move(private_kwin)),
135 runtime(session_tag.empty() ? nullptr : gamepad::runtime_for(session_tag)),
136 gamepads(MAX_GAMEPADS) {
137 if (!session_tag.empty() && !runtime) {
138 BOOST_LOG(error) <<
"[libvirtualhid] Failed to create the detached desktop's gamepad runtime";
147 std::mutex kept_kwin_input_mutex;
148 std::map<std::string, std::shared_ptr<kwin::connection_t>> kept_kwin_input;
151 input_t input_private_kwin(
const std::string &wayland_socket,
const std::string &session_tag) {
152 std::lock_guard lock {kept_kwin_input_mutex};
155 if (connection && !connection->dispatch(std::chrono::milliseconds(0))) {
162 kept_kwin_input.erase(wayland_socket);
163 throw std::runtime_error(
"Private KWin input: " +
error);
169 void forget_private_kwin_input(
const std::string &wayland_socket) {
170 std::shared_ptr<kwin::connection_t> connection;
172 std::lock_guard lock {kept_kwin_input_mutex};
173 const auto found = kept_kwin_input.find(wayland_socket);
174 if (found == kept_kwin_input.end()) {
177 connection = std::move(found->second);
178 kept_kwin_input.erase(found);
183namespace platf::kwin_input {
184 bool active(
const input_raw_t *raw) {
185 return raw && raw->kwin;
188 void move(input_raw_t *raw,
int delta_x,
int delta_y) {
189 const auto &o = raw->kwin->output();
190 raw->kwin->pointer_motion(
double(delta_x) / o.scale,
double(delta_y) / o.scale);
193 void move_abs(input_raw_t *raw,
float x,
float y) {
194 const auto &o = raw->kwin->output();
195 raw->kwin->pointer_absolute(o.x +
double(x) / o.scale, o.y +
double(y) / o.scale);
198 void button(input_raw_t *raw,
int button,
bool release) {
219 raw->kwin->button(code, !release);
222 void scroll(input_raw_t *raw,
int high_resolution_distance,
bool horizontal) {
224 raw->kwin->axis(horizontal, (horizontal ? 1.0 : -1.0) *
double(high_resolution_distance) / 8.0);
227 void key(input_raw_t *raw, std::uint16_t key_code,
bool release) {
228 const auto key = std::find_if(key_mappings.begin(), key_mappings.end(), [key_code](
const auto &entry) {
229 return entry.second == key_code;
231 if (key != key_mappings.end()) {
232 raw->kwin->key(key->first, !release);
236 void unicode(input_raw_t *raw,
const char *utf8,
int size) {
237 if (!utf8 || size <= 0) {
241 const auto text = boost::locale::conv::utf_to_utf<char32_t>(utf8, utf8 + size);
242 for (
char32_t character : text) {
243 const std::uint32_t symbol = character <= 0xff ? character : (0x01000000u | character);
244 if (!raw->kwin->keysym(symbol,
true) || !raw->kwin->keysym(symbol,
false)) {
245 BOOST_LOG(warning) <<
"Private KWin Unicode input failed";
249 }
catch (
const std::exception &error) {
250 BOOST_LOG(warning) <<
"Private KWin text input: " << error.what();
254 void touch(client_input_raw_t *raw,
const touch_input_t &touch) {
255 auto &
kwin = *raw->global->kwin;
256 const auto &o =
kwin.output();
259 const double x = o.x + touch.x * o.width / o.scale;
260 const double y = o.y + touch.y * o.height / o.scale;
261 switch (touch.eventType) {
262 case LI_TOUCH_EVENT_DOWN:
263 case LI_TOUCH_EVENT_MOVE:
264 if (raw->kwin_touches.insert(touch.pointerId).second) {
265 kwin.touch_down(touch.pointerId, x, y);
267 kwin.touch_motion(touch.pointerId, x, y);
270 case LI_TOUCH_EVENT_UP:
271 case LI_TOUCH_EVENT_CANCEL:
272 if (raw->kwin_touches.erase(touch.pointerId)) {
273 kwin.touch_up(touch.pointerId);
276 case LI_TOUCH_EVENT_CANCEL_ALL:
277 for (
const auto id : raw->kwin_touches) {
280 raw->kwin_touches.clear();
Explicit, generation-bound connections to a private KWin session.
Declarations for logging related functions.
std::optional< std::string > wayland_socket(const std::string &client_uuid)
The name of the Wayland socket the session's compositor created.
Gamepad translation for the Linux libvirtualhid backend.