input.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Keyboard input helper functions (too small to be called a layer)
  4. *
  5. * Copyright (c) 2011 The Chromium OS Authors.
  6. */
  7. #ifndef _INPUT_H
  8. #define _INPUT_H
  9. enum {
  10. INPUT_MAX_MODIFIERS = 4,
  11. INPUT_BUFFER_LEN = 16,
  12. };
  13. enum {
  14. /* Keyboard LEDs */
  15. INPUT_LED_SCROLL = 1 << 0,
  16. INPUT_LED_NUM = 1 << 1,
  17. INPUT_LED_CAPS = 1 << 2,
  18. };
  19. /*
  20. * This table translates key codes to ASCII. Most of the entries are ASCII
  21. * codes, but entries after KEY_FIRST_MOD indicate that this key is a
  22. * modifier key, like shift, ctrl. KEY_FIRST_MOD + MOD_SHIFT is the shift
  23. * key, for example.
  24. */
  25. struct input_key_xlate {
  26. /* keycode of the modifiers which select this table, -1 if none */
  27. int left_keycode;
  28. int right_keycode;
  29. const uchar *xlate; /* keycode to ASCII table */
  30. int num_entries; /* number of entries in this table */
  31. };
  32. struct input_config {
  33. struct udevice *dev;
  34. uchar fifo[INPUT_BUFFER_LEN];
  35. int fifo_in, fifo_out;
  36. /* Which modifiers are active (1 bit for each MOD_... value) */
  37. uchar modifiers;
  38. uchar flags; /* active state keys (FLAGS_...) */
  39. uchar leds; /* active LEDs (INPUT_LED_...) */
  40. uchar leds_changed; /* LEDs that just changed */
  41. uchar num_tables; /* number of modifier tables */
  42. int prev_keycodes[INPUT_BUFFER_LEN]; /* keys held last time */
  43. int num_prev_keycodes; /* number of prev keys */
  44. struct input_key_xlate table[INPUT_MAX_MODIFIERS];
  45. /**
  46. * Function the input helper calls to scan the keyboard
  47. *
  48. * @param config Input state
  49. * @return 0 if no keys read, otherwise number of keys read, or 1 if
  50. * unknown
  51. */
  52. int (*read_keys)(struct input_config *config);
  53. bool allow_repeats; /* Don't filter out repeats */
  54. unsigned int next_repeat_ms; /* Next time we repeat a key */
  55. unsigned int repeat_delay_ms; /* Time before autorepeat starts */
  56. unsigned int repeat_rate_ms; /* Autorepeat rate in ms */
  57. };
  58. struct stdio_dev;
  59. /**
  60. * Convert a list of key codes into ASCII and send them
  61. *
  62. * @param config Input state
  63. * @param keycode List of key codes to examine
  64. * @param num_keycodes Number of key codes
  65. * @return number of ascii characters sent, or 0 if none, or -1 for an
  66. * internal error
  67. */
  68. int input_send_keycodes(struct input_config *config, int keycode[], int count);
  69. /**
  70. * Add a new keycode to an existing list of keycodes
  71. *
  72. * This can be used to handle keyboards which do their own scanning. An
  73. * internal list of depressed keys is maintained by the input library. Then
  74. * this function is called to add a new key to the list (when a 'make code' is
  75. * received), or remove a key (when a 'break code' is received).
  76. *
  77. * This function looks after maintenance of the list of active keys, and calls
  78. * input_send_keycodes() with its updated list.
  79. *
  80. * @param config Input state
  81. * @param new_keycode New keycode to add/remove
  82. * @param release true if this key was released, false if depressed
  83. * @return number of ascii characters sent, or 0 if none, or -1 for an
  84. * internal error
  85. */
  86. int input_add_keycode(struct input_config *config, int new_keycode,
  87. bool release);
  88. /**
  89. * Add a new key translation table to the input
  90. *
  91. * @param config Input state
  92. * @param left_keycode Key to hold to get into this table
  93. * @param right_keycode Another key to hold to get into this table
  94. * @param xlate Conversion table from key codes to ASCII
  95. * @param num_entries Number of entries in xlate table
  96. */
  97. int input_add_table(struct input_config *config, int left_keycode,
  98. int right_keycode, const uchar *xlate, int num_entries);
  99. /**
  100. * Test if keys are available to be read
  101. *
  102. * @param config Input state
  103. * @return 0 if no keys available, 1 if keys are available
  104. */
  105. int input_tstc(struct input_config *config);
  106. /**
  107. * Read a key
  108. *
  109. * TODO: U-Boot wants 0 for no key, but Ctrl-@ is a valid key...
  110. *
  111. * @param config Input state
  112. * @return key, or 0 if no key, or -1 if error
  113. */
  114. int input_getc(struct input_config *config);
  115. /**
  116. * Register a new device with stdio and switch to it if wanted
  117. *
  118. * @param dev Pointer to device
  119. * @return 0 if ok, -1 on error
  120. */
  121. int input_stdio_register(struct stdio_dev *dev);
  122. /**
  123. * Set up the keyboard autorepeat delays
  124. *
  125. * @param repeat_delay_ms Delay before key auto-repeat starts (in ms)
  126. * @param repeat_rate_ms Delay between successive key repeats (in ms)
  127. */
  128. void input_set_delays(struct input_config *config, int repeat_delay_ms,
  129. int repeat_rate_ms);
  130. /**
  131. * Tell the input layer whether to allow the caller to determine repeats
  132. *
  133. * Generally the input library handles processing of a list of scanned keys.
  134. * Repeated keys need to be generated based on a timer in this case, since all
  135. * that is provided is a list of keys current depressed.
  136. *
  137. * Keyboards which do their own scanning will resend codes when they want to
  138. * inject a repeating key. This function can be called at start-up to select
  139. * this behaviour.
  140. *
  141. * @param config Input state
  142. * @param allow_repeats true to repeat depressed keys every time
  143. * input_send_keycodes() is called, false to do normal
  144. * keyboard repeat processing with a timer.
  145. */
  146. void input_allow_repeats(struct input_config *config, bool allow_repeats);
  147. /**
  148. * Check if keyboard LEDs need to be updated
  149. *
  150. * This can be called after input_tstc() to see if keyboard LEDs need
  151. * updating.
  152. *
  153. * @param config Input state
  154. * @return -1 if no LEDs need updating, other value if they do
  155. */
  156. int input_leds_changed(struct input_config *config);
  157. /**
  158. * Set up the key map tables
  159. *
  160. * This must be called after input_init() or keycode decoding will not work.
  161. *
  162. * @param config Input state
  163. * @param german true to use German keyboard layout, false for US
  164. * @return 0 if ok, -1 on error
  165. */
  166. int input_add_tables(struct input_config *config, bool german);
  167. /**
  168. * Set up the input handler with basic key maps.
  169. *
  170. * @param config Input state
  171. * @param leds Initial LED value (INPUT_LED_ mask), 0 suggested
  172. * @return 0 if ok, -1 on error
  173. */
  174. int input_init(struct input_config *config, int leds);
  175. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  176. extern int overwrite_console(void);
  177. #define OVERWRITE_CONSOLE overwrite_console()
  178. #else
  179. #define OVERWRITE_CONSOLE 0
  180. #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
  181. #endif