1
0

extensions-directories.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright © 2025 Pierre Le Marre <dev@wismill.eu>
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #include "test-config.h"
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "xkbcommon/xkbcommon-keysyms.h"
  11. #include "xkbcommon/xkbcommon.h"
  12. #include "evdev-scancodes.h"
  13. #include "test.h"
  14. #include "context.h"
  15. #include "keymap.h"
  16. #include "utils.h"
  17. #ifdef HAS_XKBREGISTRY
  18. #include "xkbcommon/xkbregistry.h"
  19. #include <libxml/parser.h>
  20. #include <libxml/tree.h>
  21. #endif
  22. #ifdef HAS_XKBREGISTRY
  23. static struct rxkb_layout *
  24. fetch_layout(struct rxkb_context *ctx, const char *layout, const char *variant)
  25. {
  26. struct rxkb_layout *l = rxkb_layout_first(ctx);
  27. while (l) {
  28. const char *v = rxkb_layout_get_variant(l);
  29. if (streq(rxkb_layout_get_name(l), layout) &&
  30. ((v == NULL && variant == NULL) ||
  31. (v != NULL && variant != NULL && streq(v, variant))))
  32. return rxkb_layout_ref(l);
  33. l = rxkb_layout_next(l);
  34. }
  35. return NULL;
  36. }
  37. #endif
  38. static void
  39. test_layouts(const char* xkb_root, bool update_output_files)
  40. {
  41. struct xkb_context *ctx;
  42. struct xkb_keymap *keymap;
  43. const xkb_keysym_t *keysyms;
  44. char *unversioned_extensions_path = test_get_path("extensions/without-rules");
  45. assert(unversioned_extensions_path);
  46. char *versioned_extensions_path = test_get_path("extensions/without-rules-2");
  47. assert(versioned_extensions_path);
  48. setenv("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH", unversioned_extensions_path, 1);
  49. setenv("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH", versioned_extensions_path, 1);
  50. ctx = xkb_context_new(XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
  51. assert(ctx);
  52. char buf[PATH_MAX] = "";
  53. assert(xkb_context_num_include_paths(ctx) == 4);
  54. assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
  55. versioned_extensions_path, "p1"));
  56. assert(strcmp(xkb_context_include_path_get(ctx, 0), buf) == 0);
  57. assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
  58. versioned_extensions_path, "p2"));
  59. assert(strcmp(xkb_context_include_path_get(ctx, 1), buf) == 0);
  60. assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
  61. unversioned_extensions_path, "p3"));
  62. assert(strcmp(xkb_context_include_path_get(ctx, 2), buf) == 0);
  63. assert(strcmp(xkb_context_include_path_get(ctx, 3), xkb_root) == 0);
  64. free(unversioned_extensions_path);
  65. free(versioned_extensions_path);
  66. /* New layouts (example from documentation: “Packaging keyboard layouts”) */
  67. keymap = test_compile_rules(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  68. "evdev", "pc104", "a,b,c", NULL, NULL);
  69. assert(keymap);
  70. assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
  71. 0, 0, &keysyms));
  72. assert(keysyms[0] == XKB_KEY_Greek_alpha); /* from versioned dir */
  73. assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
  74. 1, 0, &keysyms));
  75. assert(keysyms[0] == XKB_KEY_aacute); /* from versioned dir */
  76. assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
  77. 2, 0, &keysyms));
  78. assert(keysyms[0] == XKB_KEY_adiaeresis); /* from unversioned dir */
  79. xkb_keymap_unref(keymap);
  80. xkb_context_unref(ctx);
  81. #ifdef HAS_XKBREGISTRY
  82. struct rxkb_context *rctx = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
  83. assert(rctx);
  84. assert(rxkb_context_parse(rctx, "evdev"));
  85. struct {
  86. const char *layout;
  87. const char *variant;
  88. const char *description;
  89. enum rxkb_popularity popularity;
  90. } registry_tests[] = {
  91. {
  92. .layout = "a",
  93. .variant = NULL,
  94. .description = "A",
  95. .popularity = RXKB_POPULARITY_STANDARD,
  96. },
  97. {
  98. .layout = "b",
  99. .variant = NULL,
  100. .description = "B",
  101. .popularity = RXKB_POPULARITY_EXOTIC,
  102. },
  103. {
  104. .layout = "c",
  105. .variant = NULL,
  106. .description = "C",
  107. .popularity = RXKB_POPULARITY_STANDARD,
  108. },
  109. };
  110. for (unsigned int t = 0; t < ARRAY_SIZE(registry_tests); t++) {
  111. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, t);
  112. struct rxkb_layout * const l = fetch_layout(
  113. rctx, registry_tests[t].layout, registry_tests[t].variant
  114. );
  115. assert(l);
  116. assert(streq_null(rxkb_layout_get_description(l),
  117. registry_tests[t].description));
  118. assert(rxkb_layout_get_popularity(l) == registry_tests[t].popularity);
  119. rxkb_layout_unref(l);
  120. }
  121. rxkb_context_unref(rctx);
  122. #endif
  123. }
  124. #ifdef HAS_XKBREGISTRY
  125. static struct rxkb_option *
  126. fetch_option(struct rxkb_context *ctx, const char *grp, const char *opt)
  127. {
  128. struct rxkb_option_group *g = rxkb_option_group_first(ctx);
  129. while (g) {
  130. if (streq(grp, rxkb_option_group_get_name(g))) {
  131. struct rxkb_option *o = rxkb_option_first(g);
  132. while (o) {
  133. if (streq(opt, rxkb_option_get_name(o)))
  134. return rxkb_option_ref(o);
  135. o = rxkb_option_next(o);
  136. }
  137. }
  138. g = rxkb_option_group_next(g);
  139. }
  140. return NULL;
  141. }
  142. #endif
  143. static void
  144. test_options(const char *xkb_root, bool update_output_files)
  145. {
  146. struct xkb_context *ctx;
  147. struct xkb_keymap *keymap;
  148. const xkb_keysym_t *keysyms;
  149. char *unversioned_extensions_path = test_get_path("extensions/with-rules");
  150. assert(unversioned_extensions_path);
  151. char *versioned_extensions_path = test_get_path("extensions/with-rules-2");
  152. assert(versioned_extensions_path);
  153. setenv("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH", unversioned_extensions_path, 1);
  154. setenv("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH", versioned_extensions_path, 1);
  155. ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
  156. assert(ctx);
  157. char buf[PATH_MAX] = "";
  158. assert(xkb_context_num_include_paths(ctx) == 4);
  159. assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
  160. versioned_extensions_path, "p1"));
  161. assert(strcmp(xkb_context_include_path_get(ctx, 0), buf) == 0);
  162. assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
  163. versioned_extensions_path, "p2"));
  164. assert(strcmp(xkb_context_include_path_get(ctx, 1), buf) == 0);
  165. assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
  166. unversioned_extensions_path, "p3"));
  167. assert(strcmp(xkb_context_include_path_get(ctx, 2), buf) == 0);
  168. assert(strcmp(xkb_context_include_path_get(ctx, 3), xkb_root) == 0);
  169. free(unversioned_extensions_path);
  170. free(versioned_extensions_path);
  171. /* New options */
  172. keymap = test_compile_rules(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
  173. "evdev", "pc104", "cz,ca,de", NULL,
  174. "opt:1,opt:2,opt:3!2");
  175. assert(keymap);
  176. assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
  177. 0, 0, &keysyms));
  178. assert(keysyms[0] == XKB_KEY_Greek_alpha);
  179. assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_S,
  180. 0, 0, &keysyms));
  181. assert(keysyms[0] == XKB_KEY_sacute);
  182. assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
  183. 1, 0, &keysyms));
  184. assert(keysyms[0] == XKB_KEY_adiaeresis);
  185. assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_S,
  186. 1, 0, &keysyms));
  187. assert(keysyms[0] == XKB_KEY_sacute);
  188. assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
  189. 2, 0, &keysyms));
  190. assert(keysyms[0] == XKB_KEY_a);
  191. assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_S,
  192. 2, 0, &keysyms));
  193. assert(keysyms[0] == XKB_KEY_sacute);
  194. xkb_keymap_unref(keymap);
  195. xkb_context_unref(ctx);
  196. #ifdef HAS_XKBREGISTRY
  197. struct rxkb_context *rctx = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
  198. assert(rctx);
  199. assert(rxkb_context_parse(rctx, "evdev"));
  200. struct {
  201. const char *group;
  202. const char *option;
  203. const char *description;
  204. enum rxkb_popularity popularity;
  205. } registry_tests[] = {
  206. {
  207. .group = "opt",
  208. .option = "opt:1",
  209. .description = "1",
  210. .popularity = RXKB_POPULARITY_STANDARD,
  211. },
  212. {
  213. .group = "opt",
  214. .option = "opt:2",
  215. .description = "2",
  216. .popularity = RXKB_POPULARITY_EXOTIC,
  217. },
  218. {
  219. .group = "opt",
  220. .option = "opt:3",
  221. .description = "3",
  222. .popularity = RXKB_POPULARITY_STANDARD,
  223. },
  224. };
  225. for (unsigned int t = 0; t < ARRAY_SIZE(registry_tests); t++) {
  226. fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, t);
  227. struct rxkb_option *const opt = fetch_option(
  228. rctx, registry_tests[t].group, registry_tests[t].option
  229. );
  230. assert(opt);
  231. assert(streq_null(rxkb_option_get_description(opt),
  232. registry_tests[t].description));
  233. assert(rxkb_option_get_popularity(opt) == registry_tests[t].popularity);
  234. rxkb_option_unref(opt);
  235. }
  236. rxkb_context_unref(rctx);
  237. #endif
  238. }
  239. int
  240. main(int argc, char *argv[])
  241. {
  242. test_init();
  243. bool update_output_files = false;
  244. if (argc > 1) {
  245. if (streq(argv[1], "update")) {
  246. /* Update files with *obtained* results */
  247. update_output_files = true;
  248. } else {
  249. fprintf(stderr, "ERROR: unsupported argument: \"%s\".\n", argv[1]);
  250. exit(EXIT_FAILURE);
  251. }
  252. }
  253. unsetenv("HOME");
  254. unsetenv("XDG_CONFIG_HOME");
  255. unsetenv("XDG_CONFIG_DIR");
  256. setenv("XKB_CONFIG_EXTRA_PATH", "¡SKIP!", 1);
  257. char * const xkb_root = test_get_path("");
  258. assert(xkb_root);
  259. setenv("XKB_CONFIG_ROOT", xkb_root, 1);
  260. test_layouts(xkb_root, update_output_files);
  261. test_options(xkb_root, update_output_files);
  262. free(xkb_root);
  263. return EXIT_SUCCESS;
  264. }