| 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 |
#pragma once |
| 10 |
|
| 11 |
#include "input/keyboard/XBMC_keysym.h" |
| 12 |
|
| 13 |
#include <cstdint> |
| 14 |
#include <memory> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
#include <xkbcommon/xkbcommon-compose.h> |
| 18 |
#include <xkbcommon/xkbcommon.h> |
| 19 |
#ifdef TARGET_WEBOS |
| 20 |
#include <xkbcommon/xkbcommon-webos-keysyms.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
namespace KODI |
| 24 |
{ |
| 25 |
namespace WINDOWING |
| 26 |
{ |
| 27 |
namespace WAYLAND |
| 28 |
{ |
| 29 |
|
| 30 |
/** |
| 31 |
* A wrapper class around an xkbcommon keymap and state tracker. |
| 32 |
* |
| 33 |
* This class knows about some common modifier combinations and keeps |
| 34 |
* track of the currently pressed keys and modifiers. It also has |
| 35 |
* some utility functions to transform hardware keycodes into |
| 36 |
* a common representation. |
| 37 |
* |
| 38 |
* Since this class is keeping track of all the pressed and depressed |
| 39 |
* modifiers, IT MUST ALWAYS BE KEPT UP TO DATE WITH ANY NEWLY |
| 40 |
* PRESSED MODIFIERS. Undefined behaviour will result if it is not |
| 41 |
* kept up to date. |
| 42 |
* |
| 43 |
* Instances can be easily created from keymap strings with \ref CXkbcommonContext |
| 44 |
*/ |
| 45 |
|
| 46 |
/** |
| 47 |
* Abstracts the key composer key state |
| 48 |
*/ |
| 49 |
enum class KeyComposerState |
| 50 |
{ |
| 51 |
/*! Key composer is idle */ |
| 52 |
IDLE, |
| 53 |
/*! Key composer is busy composing a key */ |
| 54 |
COMPOSING, |
| 55 |
/*! Key composer finished composing a key, it has a valid key in the buffer */ |
| 56 |
FINISHED, |
| 57 |
/*! Key composer was cancelled */ |
| 58 |
CANCELLED |
| 59 |
}; |
| 60 |
|
| 61 |
/** |
| 62 |
* Container for the key composer status |
| 63 |
*/ |
| 64 |
struct KeyComposerStatus |
| 65 |
{ |
| 66 |
/*! Key composer state */ |
| 67 |
KeyComposerState state{KeyComposerState::IDLE}; |
| 68 |
/*! The unicode key symbol that was pressed */ |
| 69 |
std::uint32_t keysym{0}; |
| 70 |
}; |
| 71 |
|
| 72 |
class CXkbcommonKeymap |
| 73 |
{ |
| 74 |
public: |
| 75 |
struct XkbKeymapDeleter |
| 76 |
{ |
| 77 |
void operator()(xkb_keymap* keymap) const; |
| 78 |
}; |
| 79 |
struct XkbComposeTableDeleter |
| 80 |
{ |
| 81 |
void operator()(xkb_compose_table* composeTable) const; |
| 82 |
}; |
| 83 |
|
| 84 |
/** |
| 85 |
* Construct for known xkb_keymap |
| 86 |
*/ |
| 87 |
explicit CXkbcommonKeymap( |
| 88 |
std::unique_ptr<xkb_keymap, XkbKeymapDeleter> keymap, |
| 89 |
std::unique_ptr<xkb_compose_table, XkbComposeTableDeleter> composeTable); |
| 90 |
|
| 91 |
/** |
| 92 |
* Get xkb keysym for keycode - only a single keysym is supported |
| 93 |
*/ |
| 94 |
xkb_keysym_t KeysymForKeycode(xkb_keycode_t code) const; |
| 95 |
/** |
| 96 |
* Updates the currently depressed, latched, locked and group |
| 97 |
* modifiers for a keyboard being tracked. |
| 98 |
* |
| 99 |
* This function must be called whenever modifiers change, or the state will |
| 100 |
* be wrong and keysym translation will be off. |
| 101 |
*/ |
| 102 |
void UpdateMask(xkb_mod_mask_t depressed, |
| 103 |
xkb_mod_mask_t latched, |
| 104 |
xkb_mod_mask_t locked, |
| 105 |
xkb_mod_mask_t group); |
| 106 |
/** |
| 107 |
* Gets the currently depressed, latched and locked modifiers |
| 108 |
* for the keyboard |
| 109 |
*/ |
| 110 |
xkb_mod_mask_t CurrentModifiers() const; |
| 111 |
/** |
| 112 |
* Get XBMCKey for provided keycode |
| 113 |
*/ |
| 114 |
XBMCKey XBMCKeyForKeycode(xkb_keycode_t code) const; |
| 115 |
/** |
| 116 |
* \ref CurrentModifiers with XBMC flags |
| 117 |
*/ |
| 118 |
XBMCMod ActiveXBMCModifiers() const; |
| 119 |
/** |
| 120 |
* Get Unicode codepoint/UTF32 code for provided keycode |
| 121 |
*/ |
| 122 |
std::uint32_t UnicodeCodepointForKeycode(xkb_keycode_t code) const; |
| 123 |
/** |
| 124 |
* Check whether a given keycode should have key repeat |
| 125 |
*/ |
| 126 |
bool ShouldKeycodeRepeat(xkb_keycode_t code) const; |
| 127 |
/** |
| 128 |
* Check if the system supports key composition |
| 129 |
* \return true if composition is supported, false otherwise |
| 130 |
*/ |
| 131 |
bool SupportsKeyComposition() const; |
| 132 |
/** |
| 133 |
* Feed a given keycode to the key composer |
| 134 |
* \param code - the keycode |
| 135 |
* \return the status of the operation (both the state and the equivalent unicode dead key char resulting from the dead key press) |
| 136 |
*/ |
| 137 |
KeyComposerStatus KeyComposerFeed(xkb_keycode_t code); |
| 138 |
/** |
| 139 |
* Reset the composer state |
| 140 |
*/ |
| 141 |
void KeyComposerFlush(); |
| 142 |
|
| 143 |
static XBMCKey XBMCKeyForKeysym(xkb_keysym_t sym); |
| 144 |
|
| 145 |
private: |
| 146 |
struct XkbStateDeleter |
| 147 |
{ |
| 148 |
void operator()(xkb_state* state) const; |
| 149 |
}; |
| 150 |
struct XkbComposeStateDeleter |
| 151 |
{ |
| 152 |
void operator()(xkb_compose_state* state) const; |
| 153 |
}; |
| 154 |
static std::unique_ptr<xkb_state, XkbStateDeleter> CreateXkbStateFromKeymap(xkb_keymap* keymap); |
| 155 |
static std::unique_ptr<xkb_compose_state, XkbComposeStateDeleter> |
| 156 |
CreateXkbComposedStateStateFromTable(xkb_compose_table* composeTable); |
| 157 |
|
| 158 |
std::unique_ptr<xkb_keymap, XkbKeymapDeleter> m_keymap; |
| 159 |
std::unique_ptr<xkb_state, XkbStateDeleter> m_state; |
| 160 |
std::unique_ptr<xkb_compose_state, XkbComposeStateDeleter> m_composeState; |
| 161 |
|
| 162 |
struct ModifierMapping |
| 163 |
{ |
| 164 |
xkb_mod_index_t xkb; |
| 165 |
XBMCMod xbmc; |
| 166 |
ModifierMapping(xkb_mod_index_t xkb, XBMCMod xbmc) |
| 167 |
: xkb{xkb}, xbmc{xbmc} |
| 168 |
{} |
| 169 |
}; |
| 170 |
std::vector<ModifierMapping> m_modifierMappings; |
| 171 |
}; |
| 172 |
|
| 173 |
class CXkbcommonContext |
| 174 |
{ |
| 175 |
public: |
| 176 |
explicit CXkbcommonContext(xkb_context_flags flags = XKB_CONTEXT_NO_FLAGS); |
| 177 |
|
| 178 |
/** |
| 179 |
* Opens a shared memory region and parses the data in it to an |
| 180 |
* xkbcommon keymap. |
| 181 |
* |
| 182 |
* This function does not own the file descriptor. It must not be closed |
| 183 |
* from this function. |
| 184 |
*/ |
| 185 |
std::unique_ptr<CXkbcommonKeymap> LocalizedKeymapFromString(const std::string& keymap, |
| 186 |
const std::string& locale); |
| 187 |
|
| 188 |
private: |
| 189 |
struct XkbContextDeleter |
| 190 |
{ |
| 191 |
void operator()(xkb_context* ctx) const; |
| 192 |
}; |
| 193 |
std::unique_ptr<xkb_context, XkbContextDeleter> m_context; |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
} |
| 198 |
} |
| 199 |
} |