input: Add a function to add a keycode to the existing set
[platform/kernel/u-boot.git] / include / input.h
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
9 #ifndef _INPUT_H
10 #define _INPUT_H
11
12 enum {
13         INPUT_MAX_MODIFIERS     = 4,
14         INPUT_BUFFER_LEN        = 16,
15 };
16
17 enum {
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  */
30 struct 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
38 struct input_config {
39         struct udevice *dev;
40         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_...) */
46         uchar leds;             /* active LEDS (INPUT_LED_...) */
47         uchar num_tables;       /* number of modifier tables */
48         int prev_keycodes[INPUT_BUFFER_LEN];    /* keys held last time */
49         int num_prev_keycodes;  /* number of prev keys */
50         struct input_key_xlate table[INPUT_MAX_MODIFIERS];
51
52         /**
53          * Function the input helper calls to scan the keyboard
54          *
55          * @param config        Input state
56          * @return 0 if no keys read, otherwise number of keys read, or 1 if
57          *              unknown
58          */
59         int (*read_keys)(struct input_config *config);
60         unsigned int next_repeat_ms;    /* Next time we repeat a key */
61         unsigned int repeat_delay_ms;   /* Time before autorepeat starts */
62         unsigned int repeat_rate_ms;    /* Autorepeat rate in ms */
63 };
64
65 struct stdio_dev;
66
67 /**
68  * Convert a list of key codes into ASCII and send them
69  *
70  * @param config        Input state
71  * @param keycode       List of key codes to examine
72  * @param num_keycodes  Number of key codes
73  * @return number of ascii characters sent, or 0 if none, or -1 for an
74  *      internal error
75  */
76 int input_send_keycodes(struct input_config *config, int keycode[], int count);
77
78 /**
79  * Add a new keycode to an existing list of keycodes
80  *
81  * This can be used to handle keyboards which do their own scanning. An
82  * internal list of depressed keys is maintained by the input library. Then
83  * this function is called to add a new key to the list (when a 'make code' is
84  * received), or remove a key (when a 'break code' is received).
85  *
86  * This function looks after maintenance of the list of active keys, and calls
87  * input_send_keycodes() with its updated list.
88  *
89  * @param config        Input state
90  * @param new_keycode   New keycode to add/remove
91  * @param release       true if this key was released, false if depressed
92  * @return number of ascii characters sent, or 0 if none, or -1 for an
93  *      internal error
94  */
95 int input_add_keycode(struct input_config *config, int new_keycode,
96                       bool release);
97
98 /**
99  * Add a new key translation table to the input
100  *
101  * @param config        Input state
102  * @param left_keycode  Key to hold to get into this table
103  * @param right_keycode Another key to hold to get into this table
104  * @param xlate         Conversion table from key codes to ASCII
105  * @param num_entries   Number of entries in xlate table
106  */
107 int input_add_table(struct input_config *config, int left_keycode,
108                     int right_keycode, const uchar *xlate, int num_entries);
109
110 /**
111  * Test if keys are available to be read
112  *
113  * @param config        Input state
114  * @return 0 if no keys available, 1 if keys are available
115  */
116 int input_tstc(struct input_config *config);
117
118 /**
119  * Read a key
120  *
121  * TODO: U-Boot wants 0 for no key, but Ctrl-@ is a valid key...
122  *
123  * @param config        Input state
124  * @return key, or 0 if no key, or -1 if error
125  */
126 int input_getc(struct input_config *config);
127
128 /**
129  * Register a new device with stdio and switch to it if wanted
130  *
131  * @param dev   Pointer to device
132  * @return 0 if ok, -1 on error
133  */
134 int input_stdio_register(struct stdio_dev *dev);
135
136 /**
137  * Set up the keyboard autorepeat delays
138  *
139  * @param repeat_delay_ms       Delay before key auto-repeat starts (in ms)
140  * @param repeat_rate_ms        Delay between successive key repeats (in ms)
141  */
142 void input_set_delays(struct input_config *config, int repeat_delay_ms,
143                int repeat_rate_ms);
144
145 /**
146  * Set up the key map tables
147  *
148  * This must be called after input_init() or keycode decoding will not work.
149  *
150  * @param config        Input state
151  * @return 0 if ok, -1 on error
152  */
153 int input_add_tables(struct input_config *config);
154
155 /**
156  * Set up the input handler with basic key maps.
157  *
158  * @param config        Input state
159  * @param leds          Initial LED value (INPUT_LED_ mask), 0 suggested
160  * @return 0 if ok, -1 on error
161  */
162 int input_init(struct input_config *config, int leds);
163
164 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
165 extern int overwrite_console(void);
166 #define OVERWRITE_CONSOLE overwrite_console()
167 #else
168 #define OVERWRITE_CONSOLE 0
169 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
170
171 #endif