tools-common.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright © 2012 Intel Corporation
  3. * SPDX-License-Identifier: MIT
  4. *
  5. * Author: Daniel Stone <daniel@fooishbar.org>
  6. */
  7. #pragma once
  8. #include "config.h"
  9. #include "darray.h"
  10. #include <assert.h>
  11. #include <stdbool.h>
  12. /* Don't use compat names in internal code. */
  13. #define _XKBCOMMON_COMPAT_H
  14. #include "xkbcommon/xkbcommon.h"
  15. #include "xkbcommon/xkbcommon-compose.h"
  16. #include "src/keymap.h"
  17. #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
  18. enum {
  19. DEFAULT_KEYMAP_COMPILE_FLAGS = XKB_KEYMAP_COMPILE_NO_FLAGS,
  20. DEFAULT_KEYMAP_SERIALIZE_FLAGS = XKB_KEYMAP_SERIALIZE_PRETTY |
  21. XKB_KEYMAP_SERIALIZE_KEEP_UNUSED
  22. };
  23. /* Fields that are printed in the interactive tools. */
  24. enum print_state_options {
  25. PRINT_LAYOUT = (1u << 0),
  26. PRINT_UNICODE = (1u << 1),
  27. PRINT_ALL_FIELDS = ((PRINT_UNICODE << 1) - 1),
  28. /*
  29. * Fields that can be hidden with the option --short.
  30. * NOTE: If this value is modified, remember to update the documentation of
  31. * the --short option in the corresponding tools.
  32. */
  33. PRINT_VERBOSE_ONE_LINE_FIELDS = (PRINT_LAYOUT | PRINT_UNICODE),
  34. PRINT_VERBOSE = (1u << 2),
  35. PRINT_UNILINE = (1u << 3),
  36. DEFAULT_PRINT_OPTIONS = PRINT_ALL_FIELDS | PRINT_VERBOSE | PRINT_UNILINE
  37. };
  38. void
  39. print_modifiers_encodings(struct xkb_keymap *keymap);
  40. void
  41. print_keys_modmaps(struct xkb_keymap *keymap);
  42. void
  43. tools_enable_verbose_logging(struct xkb_context *ctx);
  44. void
  45. tools_print_keycode_state(const char *prefix,
  46. struct xkb_state *state,
  47. struct xkb_compose_state *compose_state,
  48. xkb_keycode_t keycode,
  49. enum xkb_key_direction direction,
  50. enum xkb_consumed_mode consumed_mode,
  51. enum print_state_options options);
  52. void
  53. tools_print_state_changes(const char *prefix, struct xkb_state *state,
  54. enum xkb_state_component changed,
  55. enum print_state_options options);
  56. void
  57. tools_print_events(const char *prefix, struct xkb_state *state,
  58. struct xkb_events *events,
  59. struct xkb_compose_state *compose_state,
  60. enum xkb_consumed_mode consumed_mode,
  61. enum print_state_options options, bool report_state_changes);
  62. void
  63. tools_disable_stdin_echo(void);
  64. void
  65. tools_enable_stdin_echo(void);
  66. const char *
  67. select_backend(const char *wayland, const char *x11, const char *fallback);
  68. int
  69. tools_exec_command(const char *prefix, int argc, const char **argv);
  70. bool
  71. is_pipe_or_regular_file(int fd);
  72. FILE*
  73. tools_read_stdin(void);
  74. /**
  75. * Specialized bool for CLI arguments optionality, in order to avoid
  76. * boolean blindness
  77. */
  78. enum tools_arg_optionality {
  79. TOOLS_ARG_REQUIRED = 0,
  80. TOOLS_ARG_OPTIONAL = 1
  81. };
  82. /**
  83. * If `optional` is `TOOLS_ARG_OPTIONAL`, then `out` is unchanged.
  84. * Set `out` to the default value before calling this function.
  85. */
  86. bool
  87. tools_parse_bool(const char *s, enum tools_arg_optionality optional, bool *out);
  88. bool
  89. tools_parse_mask(const char *s, enum tools_arg_optionality optional, uint32_t *out);
  90. /** Raw modifier masks: plus-separated list of modifier names */
  91. struct xkb_raw_mod_mask {
  92. darray_char names;
  93. darray(darray_size_t) indices;
  94. };
  95. #define xkb_raw_mod_mask_new() { \
  96. .names = darray_new(), \
  97. .indices = darray_new(), \
  98. }
  99. /** Raw modifier mapping */
  100. struct xkb_machine_mods_raw_mapping {
  101. struct xkb_raw_mod_mask source;
  102. struct xkb_raw_mod_mask target;
  103. };
  104. /** Precursor of xkb_machine_builder, to handle tools args parsing */
  105. struct xkb_machine_options {
  106. struct {
  107. struct {
  108. enum xkb_keyboard_control_flags affect;
  109. enum xkb_keyboard_control_flags flags;
  110. } boolean; /**< Initial boolean controls */
  111. struct {
  112. enum xkb_a11y_flags affect;
  113. enum xkb_a11y_flags flags;
  114. } a11y; /**< Initial A11Y flags */
  115. } controls;
  116. struct {
  117. struct xkb_raw_mod_mask mask;
  118. darray(xkb_layout_index_t) mappings;
  119. } shortcuts; /**< Shortcut tweak */
  120. /** Modifiers tweak */
  121. darray(struct xkb_machine_mods_raw_mapping) modifiers;
  122. };
  123. #define xkb_machine_options_new() { \
  124. .controls = { \
  125. .boolean = {0}, \
  126. .a11y = {0}, \
  127. }, \
  128. .shortcuts = { \
  129. .mask = xkb_raw_mod_mask_new(), \
  130. .mappings = darray_new(), \
  131. }, \
  132. .modifiers = darray_new(), \
  133. }
  134. void
  135. xkb_machine_options_free(struct xkb_machine_options *options);
  136. bool
  137. tools_parse_controls(const char *s, struct xkb_machine_options *options);
  138. bool
  139. tools_parse_modifiers_mappings(const char *raw,
  140. struct xkb_machine_options *options);
  141. bool
  142. tools_parse_shortcuts_mask(const char *raw, struct xkb_machine_options *options);
  143. bool
  144. tools_parse_shortcuts_mappings(const char *raw,
  145. struct xkb_machine_options *options);
  146. struct xkb_machine_builder *
  147. xkb_machine_builder_new_from_options(struct xkb_keymap *keymap,
  148. const struct xkb_machine_options *options);
  149. #ifdef _WIN32
  150. #define setenv(varname, value, overwrite) _putenv_s((varname), (value))
  151. #define unsetenv(varname) _putenv_s(varname, "")
  152. #endif