KeyBind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. Copyright 1985, 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. */
  20. uint32_t KeycodeColumnToKeysym(KeyCode keycode,
  21. int column,
  22. const GetKeyboardMappingReply& keyboard_mapping,
  23. uint8_t min_keycode,
  24. uint8_t max_keycode);
  25. // Ported from ResetModMap:
  26. // https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/2b7598221d87049d03e9a95fcb541c37c8728184/src/KeyBind.c#L160
  27. void UpdateMappingImpl(Connection* connection,
  28. GetKeyboardMappingReply* keyboard_mapping,
  29. uint16_t* lock_meaning,
  30. uint8_t* mode_switch,
  31. uint8_t* num_lock) {
  32. auto min_keycode = static_cast<uint8_t>(connection->setup().min_keycode);
  33. auto max_keycode = static_cast<uint8_t>(connection->setup().max_keycode);
  34. uint8_t count = max_keycode - min_keycode + 1;
  35. auto keyboard_future =
  36. connection->GetKeyboardMapping({connection->setup().min_keycode, count});
  37. auto modifier_future = connection->GetModifierMapping();
  38. GetModifierMappingReply modifier_mapping;
  39. connection->Flush();
  40. if (auto reply = keyboard_future.Sync())
  41. *keyboard_mapping = std::move(*reply.reply);
  42. if (auto reply = modifier_future.Sync())
  43. modifier_mapping = std::move(*reply.reply);
  44. for (uint8_t i = 0; i < modifier_mapping.keycodes_per_modifier; i++) {
  45. // Lock modifiers are in the second row of the matrix
  46. size_t index = 2 * modifier_mapping.keycodes_per_modifier + i;
  47. for (uint8_t j = 0; j < keyboard_mapping->keysyms_per_keycode; j++) {
  48. auto sym =
  49. KeycodeColumnToKeysym(modifier_mapping.keycodes[index], j,
  50. *keyboard_mapping, min_keycode, max_keycode);
  51. if (sym == XK_Caps_Lock || sym == XK_ISO_Lock) {
  52. *lock_meaning = XK_Caps_Lock;
  53. break;
  54. }
  55. if (sym == XK_Shift_Lock)
  56. *lock_meaning = XK_Shift_Lock;
  57. }
  58. }
  59. // Mod<n> is at row (n + 2) of the matrix. This iterates from Mod1 to Mod5.
  60. for (int mod = 3; mod < 8; mod++) {
  61. for (size_t i = 0; i < modifier_mapping.keycodes_per_modifier; i++) {
  62. size_t index = mod * modifier_mapping.keycodes_per_modifier + i;
  63. for (uint8_t j = 0; j < keyboard_mapping->keysyms_per_keycode; j++) {
  64. auto sym =
  65. KeycodeColumnToKeysym(modifier_mapping.keycodes[index], j,
  66. *keyboard_mapping, min_keycode, max_keycode);
  67. if (sym == XK_Mode_switch)
  68. *mode_switch |= 1 << mod;
  69. if (sym == XK_Num_Lock)
  70. *num_lock |= 1 << mod;
  71. }
  72. }
  73. }
  74. }
  75. // Ported from XConvertCase:
  76. // https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/2b7598221d87049d03e9a95fcb541c37c8728184/src/KeyBind.c#L645
  77. void ConvertCaseImpl(uint32_t sym, uint32_t* lower, uint32_t* upper) {
  78. // Unicode keysym
  79. if ((sym & 0xff000000) == 0x01000000) {
  80. std::u16string string({static_cast<char16_t>(sym)});
  81. auto lower_string = base::i18n::ToLower(string);
  82. auto upper_string = base::i18n::ToUpper(string);
  83. *lower = lower_string[0] | 0x01000000;
  84. *upper = upper_string[0] | 0x01000000;
  85. return;
  86. }
  87. *lower = sym;
  88. *upper = sym;
  89. switch (sym >> 8) {
  90. // Latin 1
  91. case 0:
  92. if ((sym >= XK_A) && (sym <= XK_Z))
  93. *lower += (XK_a - XK_A);
  94. else if ((sym >= XK_a) && (sym <= XK_z))
  95. *upper -= (XK_a - XK_A);
  96. else if ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis))
  97. *lower += (XK_agrave - XK_Agrave);
  98. else if ((sym >= XK_agrave) && (sym <= XK_odiaeresis))
  99. *upper -= (XK_agrave - XK_Agrave);
  100. else if ((sym >= XK_Ooblique) && (sym <= XK_Thorn))
  101. *lower += (XK_oslash - XK_Ooblique);
  102. else if ((sym >= XK_oslash) && (sym <= XK_thorn))
  103. *upper -= (XK_oslash - XK_Ooblique);
  104. break;
  105. // Latin 2
  106. case 1:
  107. if (sym == XK_Aogonek)
  108. *lower = XK_aogonek;
  109. else if (sym >= XK_Lstroke && sym <= XK_Sacute)
  110. *lower += (XK_lstroke - XK_Lstroke);
  111. else if (sym >= XK_Scaron && sym <= XK_Zacute)
  112. *lower += (XK_scaron - XK_Scaron);
  113. else if (sym >= XK_Zcaron && sym <= XK_Zabovedot)
  114. *lower += (XK_zcaron - XK_Zcaron);
  115. else if (sym == XK_aogonek)
  116. *upper = XK_Aogonek;
  117. else if (sym >= XK_lstroke && sym <= XK_sacute)
  118. *upper -= (XK_lstroke - XK_Lstroke);
  119. else if (sym >= XK_scaron && sym <= XK_zacute)
  120. *upper -= (XK_scaron - XK_Scaron);
  121. else if (sym >= XK_zcaron && sym <= XK_zabovedot)
  122. *upper -= (XK_zcaron - XK_Zcaron);
  123. else if (sym >= XK_Racute && sym <= XK_Tcedilla)
  124. *lower += (XK_racute - XK_Racute);
  125. else if (sym >= XK_racute && sym <= XK_tcedilla)
  126. *upper -= (XK_racute - XK_Racute);
  127. break;
  128. // Latin 3
  129. case 2:
  130. if (sym >= XK_Hstroke && sym <= XK_Hcircumflex)
  131. *lower += (XK_hstroke - XK_Hstroke);
  132. else if (sym >= XK_Gbreve && sym <= XK_Jcircumflex)
  133. *lower += (XK_gbreve - XK_Gbreve);
  134. else if (sym >= XK_hstroke && sym <= XK_hcircumflex)
  135. *upper -= (XK_hstroke - XK_Hstroke);
  136. else if (sym >= XK_gbreve && sym <= XK_jcircumflex)
  137. *upper -= (XK_gbreve - XK_Gbreve);
  138. else if (sym >= XK_Cabovedot && sym <= XK_Scircumflex)
  139. *lower += (XK_cabovedot - XK_Cabovedot);
  140. else if (sym >= XK_cabovedot && sym <= XK_scircumflex)
  141. *upper -= (XK_cabovedot - XK_Cabovedot);
  142. break;
  143. // Latin 4
  144. case 3:
  145. if (sym >= XK_Rcedilla && sym <= XK_Tslash)
  146. *lower += (XK_rcedilla - XK_Rcedilla);
  147. else if (sym >= XK_rcedilla && sym <= XK_tslash)
  148. *upper -= (XK_rcedilla - XK_Rcedilla);
  149. else if (sym == XK_ENG)
  150. *lower = XK_eng;
  151. else if (sym == XK_eng)
  152. *upper = XK_ENG;
  153. else if (sym >= XK_Amacron && sym <= XK_Umacron)
  154. *lower += (XK_amacron - XK_Amacron);
  155. else if (sym >= XK_amacron && sym <= XK_umacron)
  156. *upper -= (XK_amacron - XK_Amacron);
  157. break;
  158. // Cyrillic
  159. case 6:
  160. if (sym >= XK_Serbian_DJE && sym <= XK_Serbian_DZE)
  161. *lower -= (XK_Serbian_DJE - XK_Serbian_dje);
  162. else if (sym >= XK_Serbian_dje && sym <= XK_Serbian_dze)
  163. *upper += (XK_Serbian_DJE - XK_Serbian_dje);
  164. else if (sym >= XK_Cyrillic_YU && sym <= XK_Cyrillic_HARDSIGN)
  165. *lower -= (XK_Cyrillic_YU - XK_Cyrillic_yu);
  166. else if (sym >= XK_Cyrillic_yu && sym <= XK_Cyrillic_hardsign)
  167. *upper += (XK_Cyrillic_YU - XK_Cyrillic_yu);
  168. break;
  169. // Greek
  170. case 7:
  171. if (sym >= XK_Greek_ALPHAaccent && sym <= XK_Greek_OMEGAaccent)
  172. *lower += (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
  173. else if (sym >= XK_Greek_alphaaccent && sym <= XK_Greek_omegaaccent &&
  174. sym != XK_Greek_iotaaccentdieresis &&
  175. sym != XK_Greek_upsilonaccentdieresis)
  176. *upper -= (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
  177. else if (sym >= XK_Greek_ALPHA && sym <= XK_Greek_OMEGA)
  178. *lower += (XK_Greek_alpha - XK_Greek_ALPHA);
  179. else if (sym >= XK_Greek_alpha && sym <= XK_Greek_omega &&
  180. sym != XK_Greek_finalsmallsigma)
  181. *upper -= (XK_Greek_alpha - XK_Greek_ALPHA);
  182. break;
  183. // Latin 9
  184. case 0x13:
  185. if (sym == XK_OE)
  186. *lower = XK_oe;
  187. else if (sym == XK_oe)
  188. *upper = XK_OE;
  189. else if (sym == XK_Ydiaeresis)
  190. *lower = XK_ydiaeresis;
  191. break;
  192. }
  193. }
  194. // Ported from _XTranslateKey:
  195. // https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/2b7598221d87049d03e9a95fcb541c37c8728184/src/KeyBind.c#L761
  196. KeySym KeycodeToKeysymCoreImpl(KeyCode key,
  197. uint32_t modifiers,
  198. Connection* connection,
  199. const GetKeyboardMappingReply& keyboard_mapping,
  200. uint16_t lock_meaning,
  201. uint8_t mode_switch,
  202. uint8_t num_lock) {
  203. constexpr auto kShiftMask = static_cast<unsigned int>(x11::ModMask::Shift);
  204. constexpr auto kLockMask = static_cast<unsigned int>(x11::ModMask::Lock);
  205. uint8_t keycode = static_cast<uint8_t>(key);
  206. uint8_t min_key = static_cast<uint8_t>(connection->setup().min_keycode);
  207. uint8_t max_key = static_cast<uint8_t>(connection->setup().max_keycode);
  208. if (keycode < min_key || keycode > max_key)
  209. return kNoSymbol;
  210. uint8_t n_keysyms = keyboard_mapping.keysyms_per_keycode;
  211. if (!n_keysyms)
  212. return {};
  213. const auto* syms = &keyboard_mapping.keysyms[(keycode - min_key) * n_keysyms];
  214. while ((n_keysyms > 2) && (syms[n_keysyms - 1] == kNoSymbol))
  215. n_keysyms--;
  216. if ((n_keysyms > 2) && (modifiers & mode_switch)) {
  217. syms += 2;
  218. n_keysyms -= 2;
  219. }
  220. if ((modifiers & num_lock) &&
  221. (n_keysyms > 1 && (IsPublicOrPrivateKeypadKey(syms[1])))) {
  222. if ((modifiers & kShiftMask) ||
  223. ((modifiers & kLockMask) && (lock_meaning == XK_Shift_Lock))) {
  224. return syms[0];
  225. }
  226. return syms[1];
  227. }
  228. KeySym lower;
  229. KeySym upper;
  230. if (!(modifiers & kShiftMask) &&
  231. (!(modifiers & kLockMask) ||
  232. (static_cast<x11::KeySym>(lock_meaning) == kNoSymbol))) {
  233. if ((n_keysyms == 1) || (syms[1] == kNoSymbol)) {
  234. ConvertCase(syms[0], &lower, &upper);
  235. return lower;
  236. }
  237. return syms[0];
  238. }
  239. if (!(modifiers & kLockMask) || (lock_meaning != XK_Caps_Lock)) {
  240. if ((n_keysyms == 1) || ((upper = syms[1]) == kNoSymbol))
  241. ConvertCase(syms[0], &lower, &upper);
  242. return upper;
  243. }
  244. KeySym sym;
  245. if ((n_keysyms == 1) || ((sym = syms[1]) == kNoSymbol))
  246. sym = syms[0];
  247. ConvertCase(sym, &lower, &upper);
  248. if (!(modifiers & kShiftMask) && (sym != syms[0]) &&
  249. ((sym != upper) || (lower == upper)))
  250. ConvertCase(syms[0], &lower, &upper);
  251. return upper;
  252. }