keysyms.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright © 2008 Ian Osgood <iano@quirkster.com>
  3. * Copyright © 2008 Jamey Sharp <jamey@minilop.net>
  4. * Copyright © 2008 Josh Triplett <josh@freedesktop.org>
  5. * Copyright © 2008 Ulrich Eckhardt <doomster@knuut.de>
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use, copy,
  11. * modify, merge, publish, distribute, sublicense, and/or sell copies
  12. * of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  23. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Except as contained in this notice, the names of the authors or
  27. * their institutions shall not be used in advertising or otherwise to
  28. * promote the sale, use or other dealings in this Software without
  29. * prior written authorization from the authors.
  30. */
  31. // Ported from xcb_key_symbols_get_keysym
  32. // https://gitlab.freedesktop.org/xorg/lib/libxcb-keysyms/-/blob/691515491a4a3c119adc6c769c29de264b3f3806/keysyms/keysyms.c#L189
  33. uint32_t KeycodeColumnToKeysym(KeyCode keycode,
  34. int column,
  35. const GetKeyboardMappingReply& keyboard_mapping,
  36. uint8_t min_keycode,
  37. uint8_t max_keycode) {
  38. uint8_t key = static_cast<uint8_t>(keycode);
  39. uint8_t n_keysyms = keyboard_mapping.keysyms_per_keycode;
  40. if (column < 0 || (column >= n_keysyms && column > 3) || key < min_keycode ||
  41. key > max_keycode) {
  42. return 0;
  43. }
  44. const auto* syms = &keyboard_mapping.keysyms[(key - min_keycode) * n_keysyms];
  45. if (column < 4) {
  46. if (column > 1) {
  47. while ((n_keysyms > 2) && (syms[n_keysyms - 1] == kNoSymbol))
  48. n_keysyms--;
  49. if (n_keysyms < 3)
  50. column -= 2;
  51. }
  52. if ((n_keysyms <= (column | 1)) || (syms[column | 1] == kNoSymbol)) {
  53. KeySym lsym, usym;
  54. ConvertCase(syms[column & ~1], &lsym, &usym);
  55. if (!(column & 1))
  56. return static_cast<uint32_t>(lsym);
  57. if (usym == lsym)
  58. return 0;
  59. return static_cast<uint32_t>(usym);
  60. }
  61. }
  62. return static_cast<uint32_t>(syms[column]);
  63. }