| 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 "InputProcessorKeyboard.h" |
| 10 |
|
| 11 |
#include "LangInfo.h" |
| 12 |
#include "utils/log.h" |
| 13 |
|
| 14 |
#include <cassert> |
| 15 |
#include <limits> |
| 16 |
#include <memory> |
| 17 |
|
| 18 |
using namespace KODI::WINDOWING::WAYLAND; |
| 19 |
|
| 20 |
namespace |
| 21 |
{ |
| 22 |
// Offset between keyboard codes of Wayland (effectively evdev) and xkb_keycode_t |
| 23 |
constexpr int WL_KEYBOARD_XKB_CODE_OFFSET{8}; |
| 24 |
} |
| 25 |
|
| 26 |
CInputProcessorKeyboard::CInputProcessorKeyboard(IInputHandlerKeyboard& handler) |
| 27 |
: m_handler{handler}, m_keyRepeatTimer{std::bind(&CInputProcessorKeyboard::KeyRepeatTimeout, this)} |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
void CInputProcessorKeyboard::OnKeyboardKeymap(CSeat* seat, wayland::keyboard_keymap_format format, std::string const &keymap) |
| 32 |
{ |
| 33 |
if (format != wayland::keyboard_keymap_format::xkb_v1) |
| 34 |
{ |
| 35 |
CLog::Log(LOGWARNING, |
| 36 |
"Wayland compositor sent keymap in format {}, but we only understand xkbv1 - " |
| 37 |
"keyboard input will not work", |
| 38 |
static_cast<unsigned int>(format)); |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
m_keyRepeatTimer.Stop(); |
| 43 |
|
| 44 |
try |
| 45 |
{ |
| 46 |
if (!m_xkbContext) |
| 47 |
{ |
| 48 |
// Lazily initialize XkbcommonContext |
| 49 |
m_xkbContext = std::make_unique<CXkbcommonContext>(); |
| 50 |
} |
| 51 |
|
| 52 |
m_keymap = m_xkbContext->LocalizedKeymapFromString(keymap, g_langInfo.GetSystemLocale().name()); |
| 53 |
} |
| 54 |
catch(std::exception const& e) |
| 55 |
{ |
| 56 |
CLog::Log(LOGERROR, "Could not parse keymap from compositor: {} - continuing without keymap", |
| 57 |
e.what()); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void CInputProcessorKeyboard::OnKeyboardEnter(CSeat* seat, |
| 62 |
std::uint32_t serial, |
| 63 |
const wayland::surface_t& surface, |
| 64 |
const wayland::array_t& keys) |
| 65 |
{ |
| 66 |
m_handler.OnKeyboardEnter(); |
| 67 |
} |
| 68 |
|
| 69 |
void CInputProcessorKeyboard::OnKeyboardLeave(CSeat* seat, |
| 70 |
std::uint32_t serial, |
| 71 |
const wayland::surface_t& surface) |
| 72 |
{ |
| 73 |
m_keyRepeatTimer.Stop(); |
| 74 |
m_handler.OnKeyboardLeave(); |
| 75 |
} |
| 76 |
|
| 77 |
void CInputProcessorKeyboard::OnKeyboardKey(CSeat* seat, std::uint32_t serial, std::uint32_t time, std::uint32_t key, wayland::keyboard_key_state state) |
| 78 |
{ |
| 79 |
if (!m_keymap) |
| 80 |
{ |
| 81 |
CLog::Log(LOGWARNING, "Key event for code {} without valid keymap, ignoring", key); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
ConvertAndSendKey(key, state == wayland::keyboard_key_state::pressed); |
| 86 |
} |
| 87 |
|
| 88 |
void CInputProcessorKeyboard::OnKeyboardModifiers(CSeat* seat, std::uint32_t serial, std::uint32_t modsDepressed, std::uint32_t modsLatched, std::uint32_t modsLocked, std::uint32_t group) |
| 89 |
{ |
| 90 |
if (!m_keymap) |
| 91 |
{ |
| 92 |
CLog::Log(LOGWARNING, "Modifier event without valid keymap, ignoring"); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
m_keyRepeatTimer.Stop(); |
| 97 |
m_keymap->UpdateMask(modsDepressed, modsLatched, modsLocked, group); |
| 98 |
} |
| 99 |
|
| 100 |
void CInputProcessorKeyboard::OnKeyboardRepeatInfo(CSeat* seat, std::int32_t rate, std::int32_t delay) |
| 101 |
{ |
| 102 |
CLog::Log(LOGDEBUG, "Key repeat rate: {} cps, delay {} ms", rate, delay); |
| 103 |
// rate is in characters per second, so convert to msec interval |
| 104 |
m_keyRepeatInterval = (rate != 0) ? static_cast<int> (1000.0f / rate) : 0; |
| 105 |
m_keyRepeatDelay = delay; |
| 106 |
} |
| 107 |
|
| 108 |
void CInputProcessorKeyboard::ConvertAndSendKey(std::uint32_t scancode, bool pressed) |
| 109 |
{ |
| 110 |
const std::uint32_t xkbCode{scancode + WL_KEYBOARD_XKB_CODE_OFFSET}; |
| 111 |
|
| 112 |
bool flushComposer{false}; |
| 113 |
if (pressed && m_keymap->SupportsKeyComposition()) |
| 114 |
{ |
| 115 |
// feed it first to the key composer if from a press |
| 116 |
const KeyComposerStatus feedResult = m_keymap->KeyComposerFeed(xkbCode); |
| 117 |
if (feedResult.state == KeyComposerState::COMPOSING) |
| 118 |
{ |
| 119 |
// let the outside world (e.g. GUI controls) know we're composing a key |
| 120 |
NotifyKeyComposingEvent(XBMC_KEYCOMPOSING_COMPOSING, feedResult.keysym); |
| 121 |
return; |
| 122 |
} |
| 123 |
else if (feedResult.state == KeyComposerState::FINISHED) |
| 124 |
{ |
| 125 |
flushComposer = true; |
| 126 |
// let the outside world (e.g. GUI Controls) know we're back to normal input |
| 127 |
NotifyKeyComposingEvent(XBMC_KEYCOMPOSING_FINISHED, XBMCK_UNKNOWN); |
| 128 |
} |
| 129 |
else if (feedResult.state == KeyComposerState::CANCELLED) |
| 130 |
{ |
| 131 |
// composed sequence was cancelled, we're back to normal input |
| 132 |
// let the outside world know what key lead to the cancellation (replay sequence) |
| 133 |
const std::uint32_t unicodeCodePointCancellationKey{ |
| 134 |
m_keymap->UnicodeCodepointForKeycode(xkbCode)}; |
| 135 |
NotifyKeyComposingEvent(XBMC_KEYCOMPOSING_CANCELLED, unicodeCodePointCancellationKey); |
| 136 |
m_keymap->KeyComposerFlush(); |
| 137 |
// do not allow key fallthrough if we are simply cancelling with a backspace (we are cancelling the |
| 138 |
// composition behavior not really removing any character) |
| 139 |
if (unicodeCodePointCancellationKey == XBMCK_BACKSPACE) |
| 140 |
{ |
| 141 |
return; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
std::uint32_t utf32{m_keymap->UnicodeCodepointForKeycode(xkbCode)}; |
| 147 |
if (utf32 > std::numeric_limits<std::uint16_t>::max()) |
| 148 |
{ |
| 149 |
// Kodi event system only supports UTF16, so ignore the codepoint if |
| 150 |
// it does not fit |
| 151 |
utf32 = 0; |
| 152 |
} |
| 153 |
|
| 154 |
// flush composer if set (after a finished sequence) |
| 155 |
if (flushComposer) |
| 156 |
{ |
| 157 |
m_keymap->KeyComposerFlush(); |
| 158 |
} |
| 159 |
|
| 160 |
const XBMCKey xbmcKey{m_keymap->XBMCKeyForKeycode(xkbCode)}; |
| 161 |
if (xbmcKey == XBMCKey::XBMCK_UNKNOWN && utf32 == 0) |
| 162 |
{ |
| 163 |
// Such an event would carry no useful information in it and thus can be safely dropped here |
| 164 |
// This prevents starting an unnecessary timer for key presses |
| 165 |
CLog::Log(LOGDEBUG, "Unknown key event ignored (scancode: {})", scancode); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
XBMC_Event event{SendKey(scancode, xbmcKey, static_cast<std::uint16_t> (utf32), pressed)}; |
| 170 |
|
| 171 |
if (pressed && m_keymap->ShouldKeycodeRepeat(xkbCode) && m_keyRepeatInterval > 0) |
| 172 |
{ |
| 173 |
// Can't modify keyToRepeat until we're sure the thread isn't accessing it |
| 174 |
m_keyRepeatTimer.Stop(true); |
| 175 |
// Update/Set key |
| 176 |
m_keyToRepeat = event; |
| 177 |
// Start timer with initial delay |
| 178 |
m_keyRepeatTimer.Start(std::chrono::milliseconds(m_keyRepeatDelay), false); |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
m_keyRepeatTimer.Stop(); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
XBMC_Event CInputProcessorKeyboard::SendKey(uint32_t scancode, |
| 187 |
XBMCKey key, |
| 188 |
std::uint16_t unicodeCodepoint, |
| 189 |
bool pressed) |
| 190 |
{ |
| 191 |
assert(m_keymap); |
| 192 |
|
| 193 |
XBMC_Event event{}; |
| 194 |
event.type = pressed ? XBMC_KEYDOWN : XBMC_KEYUP; |
| 195 |
event.key.keysym = |
| 196 |
{ |
| 197 |
.scancode = scancode, |
| 198 |
.sym = key, |
| 199 |
.mod = m_keymap->ActiveXBMCModifiers(), |
| 200 |
.unicode = unicodeCodepoint |
| 201 |
}; |
| 202 |
m_handler.OnKeyboardEvent(event); |
| 203 |
// Return created event for convenience (key repeat) |
| 204 |
return event; |
| 205 |
} |
| 206 |
|
| 207 |
void CInputProcessorKeyboard::KeyRepeatTimeout() |
| 208 |
{ |
| 209 |
// Reset ourselves |
| 210 |
m_keyRepeatTimer.RestartAsync(std::chrono::milliseconds(m_keyRepeatInterval)); |
| 211 |
// Simulate repeat: Key up and down |
| 212 |
XBMC_Event event = m_keyToRepeat; |
| 213 |
event.type = XBMC_KEYUP; |
| 214 |
m_handler.OnKeyboardEvent(event); |
| 215 |
event.type = XBMC_KEYDOWN; |
| 216 |
m_handler.OnKeyboardEvent(event); |
| 217 |
} |
| 218 |
|
| 219 |
void CInputProcessorKeyboard::NotifyKeyComposingEvent(uint8_t eventType, |
| 220 |
std::uint16_t unicodeCodepoint) |
| 221 |
{ |
| 222 |
XBMC_Event event{}; |
| 223 |
event.type = eventType; |
| 224 |
event.key.keysym.unicode = unicodeCodepoint; |
| 225 |
m_handler.OnKeyboardEvent(event); |
| 226 |
} |