input.h 6.1 KB

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