export-keysyms.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright © 2024 Pierre Le Marre
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #include <locale.h>
  7. #include <stdio.h>
  8. #include <stdbool.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #if HAVE_ICU
  12. #include <unicode/uchar.h>
  13. #endif
  14. #include "xkbcommon/xkbcommon.h"
  15. #include "src/keysym.h"
  16. static void
  17. print_char_name(uint32_t cp)
  18. {
  19. #if HAVE_ICU
  20. char char_name[256];
  21. UErrorCode pErrorCode = 0;
  22. int32_t count = u_charName((UChar32)cp, U_CHAR_NAME_ALIAS, char_name,
  23. sizeof(char_name), &pErrorCode);
  24. if (count <= 0) {
  25. count = u_charName((UChar32)cp, U_EXTENDED_CHAR_NAME,
  26. char_name, sizeof(char_name), &pErrorCode);
  27. }
  28. if (count > 0) {
  29. printf(" %s", char_name);
  30. }
  31. #endif
  32. }
  33. int
  34. main(int argc, char **argv)
  35. {
  36. bool explicit = !(argc > 1 && strcmp("all", argv[1]) == 0);
  37. int idx = 1 + !explicit;
  38. bool char_names = argc > idx && strcmp("names", argv[idx]) == 0;
  39. #if !HAVE_ICU
  40. if (char_names)
  41. fprintf(stderr, "ERROR: names argument requires ICU.\n");
  42. #endif
  43. setlocale(LC_ALL, "");
  44. struct xkb_keysym_iterator *iter = xkb_keysym_iterator_new(explicit);
  45. while (xkb_keysym_iterator_next(iter)) {
  46. xkb_keysym_t ks = xkb_keysym_iterator_get_keysym(iter);
  47. printf("0x%04x:\n", ks);
  48. char name[XKB_KEYSYM_NAME_MAX_SIZE];
  49. xkb_keysym_iterator_get_name(iter, name, sizeof(name));
  50. printf(" name: %s\n", name);
  51. // char utf8[7];
  52. // int count = xkb_keysym_to_utf8(ks, utf8, sizeof(utf8));
  53. uint32_t cp = xkb_keysym_to_utf32(ks);
  54. if (cp) {
  55. printf(" code point: 0x%04X", cp);
  56. if (char_names) {
  57. printf(" #");
  58. print_char_name(cp);
  59. }
  60. printf("\n");
  61. }
  62. xkb_keysym_t ks2;
  63. if ((ks2 = xkb_keysym_to_lower(ks)) != ks) {
  64. xkb_keysym_get_name(ks2, name, sizeof(name));
  65. printf(" lower: 0x%04x # %s", ks2, name);
  66. if (char_names) {
  67. cp = xkb_keysym_to_utf32(ks2);
  68. print_char_name(cp);
  69. }
  70. printf("\n");
  71. }
  72. if ((ks2 = xkb_keysym_to_upper(ks)) != ks) {
  73. xkb_keysym_get_name(ks2, name, sizeof(name));
  74. printf(" upper: 0x%04x # %s", ks2, name);
  75. if (char_names) {
  76. cp = xkb_keysym_to_utf32(ks2);
  77. print_char_name(cp);
  78. }
  79. printf("\n");
  80. }
  81. };
  82. xkb_keysym_iterator_unref(iter);
  83. }