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