Skip to content

Commit 41ab439

Browse files
sthibaulLinus Torvalds
authored andcommitted
Console keyboard events and accessibility
Some blind people use a kernel engine called Speakup which uses hardware synthesis to speak what gets displayed on the screen. They use the PC keyboard to control this engine (start/stop, accelerate, ...) and also need to get keyboard feedback (to make sure to know what they are typing, the caps lock status, etc.) Up to now, the way it was done was very ugly. Below is a patch to add a notifier list for permitting a far better implementation, see ChangeLog above for details. You may wonder why this can't be done at the input layer. The problem is that what people want to monitor is the console keyboard, i.e. all input keyboards that got attached to the console, and with the currently active keymap (i.e. keysyms, not only keycodes). This adds a keyboard notifier that such modules can use to get the keyboard events and possibly eat them, at several stages: - keycodes: even before translation into keysym. - unbound keycodes: when no keysym is bound. - unicode: when the keycode would get translated into a unicode character. - keysym: when the keycode would get translated into a keysym. - post_keysym: after the keysym got interpreted, so as to see the result (caps lock, etc.) This also provides access to k_handler so as to permit simulation of keypresses. [akpm@linux-foundation.org: various fixes] Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org> Cc: Jiri Kosina <jkosina@suse.cz> Cc: Dmitry Torokhov <dtor@mail.ru> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c18479f commit 41ab439

3 files changed

Lines changed: 56 additions & 6 deletions

File tree

‎drivers/char/keyboard.c‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <linux/sysrq.h>
4343
#include <linux/input.h>
4444
#include <linux/reboot.h>
45+
#include <linux/notifier.h>
4546

4647
extern void ctrl_alt_del(void);
4748

@@ -81,7 +82,8 @@ void compute_shiftstate(void);
8182
typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value,
8283
char up_flag);
8384
static k_handler_fn K_HANDLERS;
84-
static k_handler_fn *k_handler[16] = { K_HANDLERS };
85+
k_handler_fn *k_handler[16] = { K_HANDLERS };
86+
EXPORT_SYMBOL_GPL(k_handler);
8587

8688
#define FN_HANDLERS\
8789
fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\
@@ -159,6 +161,23 @@ static int sysrq_alt_use;
159161
#endif
160162
static int sysrq_alt;
161163

164+
/*
165+
* Notifier list for console keyboard events
166+
*/
167+
static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list);
168+
169+
int register_keyboard_notifier(struct notifier_block *nb)
170+
{
171+
return atomic_notifier_chain_register(&keyboard_notifier_list, nb);
172+
}
173+
EXPORT_SYMBOL_GPL(register_keyboard_notifier);
174+
175+
int unregister_keyboard_notifier(struct notifier_block *nb)
176+
{
177+
return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb);
178+
}
179+
EXPORT_SYMBOL_GPL(unregister_keyboard_notifier);
180+
162181
/*
163182
* Translation of scancodes to keycodes. We set them on only the first
164183
* keyboard in the list that accepts the scancode and keycode.
@@ -1130,6 +1149,7 @@ static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
11301149
unsigned char type, raw_mode;
11311150
struct tty_struct *tty;
11321151
int shift_final;
1152+
struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
11331153

11341154
tty = vc->vc_tty;
11351155

@@ -1217,10 +1237,11 @@ static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
12171237
return;
12181238
}
12191239

1220-
shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
1240+
param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
12211241
key_map = key_maps[shift_final];
12221242

1223-
if (!key_map) {
1243+
if (atomic_notifier_call_chain(&keyboard_notifier_list, KBD_KEYCODE, &param) == NOTIFY_STOP || !key_map) {
1244+
atomic_notifier_call_chain(&keyboard_notifier_list, KBD_UNBOUND_KEYCODE, &param);
12241245
compute_shiftstate();
12251246
kbd->slockstate = 0;
12261247
return;
@@ -1237,16 +1258,16 @@ static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
12371258
type = KTYP(keysym);
12381259

12391260
if (type < 0xf0) {
1261+
param.value = keysym;
1262+
if (atomic_notifier_call_chain(&keyboard_notifier_list, KBD_UNICODE, &param) == NOTIFY_STOP)
1263+
return;
12401264
if (down && !raw_mode)
12411265
to_utf8(vc, keysym);
12421266
return;
12431267
}
12441268

12451269
type -= 0xf0;
12461270

1247-
if (raw_mode && type != KT_SPEC && type != KT_SHIFT)
1248-
return;
1249-
12501271
if (type == KT_LETTER) {
12511272
type = KT_LATIN;
12521273
if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
@@ -1255,9 +1276,18 @@ static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
12551276
keysym = key_map[keycode];
12561277
}
12571278
}
1279+
param.value = keysym;
1280+
1281+
if (atomic_notifier_call_chain(&keyboard_notifier_list, KBD_KEYSYM, &param) == NOTIFY_STOP)
1282+
return;
1283+
1284+
if (raw_mode && type != KT_SPEC && type != KT_SHIFT)
1285+
return;
12581286

12591287
(*k_handler[type])(vc, keysym & 0xff, !down);
12601288

1289+
atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, &param);
1290+
12611291
if (type != KT_SLOCK)
12621292
kbd->slockstate = 0;
12631293
}

‎include/linux/keyboard.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,21 @@
2323
#define MAX_NR_OF_USER_KEYMAPS 256 /* should be at least 7 */
2424

2525
#ifdef __KERNEL__
26+
struct notifier_block;
2627
extern const int NR_TYPES;
2728
extern const int max_vals[];
2829
extern unsigned short *key_maps[MAX_NR_KEYMAPS];
2930
extern unsigned short plain_map[NR_KEYS];
31+
32+
struct keyboard_notifier_param {
33+
struct vc_data *vc; /* VC on which the keyboard press was done */
34+
int down; /* Pressure of the key? */
35+
int shift; /* Current shift mask */
36+
unsigned int value; /* keycode, unicode value or keysym */
37+
};
38+
39+
extern int register_keyboard_notifier(struct notifier_block *nb);
40+
extern int unregister_keyboard_notifier(struct notifier_block *nb);
3041
#endif
3142

3243
#define MAX_NR_FUNC 256 /* max nr of strings assigned to keys */

‎include/linux/notifier.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,14 @@ static inline int notifier_to_errno(int ret)
231231
#define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
232232
#define PM_POST_SUSPEND 0x0004 /* Suspend finished */
233233

234+
/* Console keyboard events.
235+
* Note: KBD_KEYCODE is always sent before KBD_UNBOUND_KEYCODE, KBD_UNICODE and
236+
* KBD_KEYSYM. */
237+
#define KBD_KEYCODE 0x0001 /* Keyboard keycode, called before any other */
238+
#define KBD_UNBOUND_KEYCODE 0x0002 /* Keyboard keycode which is not bound to any other */
239+
#define KBD_UNICODE 0x0003 /* Keyboard unicode */
240+
#define KBD_KEYSYM 0x0004 /* Keyboard keysym */
241+
#define KBD_POST_KEYSYM 0x0005 /* Called after keyboard keysym interpretation */
242+
234243
#endif /* __KERNEL__ */
235244
#endif /* _LINUX_NOTIFIER_H */

0 commit comments

Comments
 (0)