xen-bus: allow AioContext to be specified for each event channel
[qemu.git] / ui / kbd-state.c
blob1668d17ddabca766629499eb54700b30e194c675
1 /*
2 * This work is licensed under the terms of the GNU GPL, version 2 or
3 * (at your option) any later version. See the COPYING file in the
4 * top-level directory.
5 */
6 #include "qemu/osdep.h"
7 #include "qemu/bitmap.h"
8 #include "qemu/queue.h"
9 #include "ui/console.h"
10 #include "ui/input.h"
11 #include "ui/kbd-state.h"
13 struct QKbdState {
14 QemuConsole *con;
15 int key_delay_ms;
16 DECLARE_BITMAP(keys, Q_KEY_CODE__MAX);
17 DECLARE_BITMAP(mods, QKBD_MOD__MAX);
20 static void qkbd_state_modifier_update(QKbdState *kbd,
21 QKeyCode qcode1, QKeyCode qcode2,
22 QKbdModifier mod)
24 if (test_bit(qcode1, kbd->keys) || test_bit(qcode2, kbd->keys)) {
25 set_bit(mod, kbd->mods);
26 } else {
27 clear_bit(mod, kbd->mods);
31 bool qkbd_state_modifier_get(QKbdState *kbd, QKbdModifier mod)
33 return test_bit(mod, kbd->mods);
36 bool qkbd_state_key_get(QKbdState *kbd, QKeyCode qcode)
38 return test_bit(qcode, kbd->keys);
41 void qkbd_state_key_event(QKbdState *kbd, QKeyCode qcode, bool down)
43 bool state = test_bit(qcode, kbd->keys);
45 if (down == false /* got key-up event */ &&
46 state == false /* key is not pressed */) {
48 * Filter out suspicious key-up events.
50 * This allows simply sending along all key-up events, and
51 * this function will filter out everything where the
52 * corresponding key-down event wasn't sent to the guest, for
53 * example due to being a host hotkey.
55 * Note that key-down events on already pressed keys are *not*
56 * suspicious, those are keyboard autorepeat events.
58 return;
61 /* update key and modifier state */
62 if (down) {
63 set_bit(qcode, kbd->keys);
64 } else {
65 clear_bit(qcode, kbd->keys);
67 switch (qcode) {
68 case Q_KEY_CODE_SHIFT:
69 case Q_KEY_CODE_SHIFT_R:
70 qkbd_state_modifier_update(kbd, Q_KEY_CODE_SHIFT, Q_KEY_CODE_SHIFT_R,
71 QKBD_MOD_SHIFT);
72 break;
73 case Q_KEY_CODE_CTRL:
74 case Q_KEY_CODE_CTRL_R:
75 qkbd_state_modifier_update(kbd, Q_KEY_CODE_CTRL, Q_KEY_CODE_CTRL_R,
76 QKBD_MOD_CTRL);
77 break;
78 case Q_KEY_CODE_ALT:
79 qkbd_state_modifier_update(kbd, Q_KEY_CODE_ALT, Q_KEY_CODE_ALT,
80 QKBD_MOD_ALT);
81 break;
82 case Q_KEY_CODE_ALT_R:
83 qkbd_state_modifier_update(kbd, Q_KEY_CODE_ALT_R, Q_KEY_CODE_ALT_R,
84 QKBD_MOD_ALTGR);
85 break;
86 case Q_KEY_CODE_CAPS_LOCK:
87 if (down) {
88 change_bit(QKBD_MOD_CAPSLOCK, kbd->mods);
90 break;
91 case Q_KEY_CODE_NUM_LOCK:
92 if (down) {
93 change_bit(QKBD_MOD_NUMLOCK, kbd->mods);
95 break;
96 default:
97 /* keep gcc happy */
98 break;
101 /* send to guest */
102 if (qemu_console_is_graphic(kbd->con)) {
103 qemu_input_event_send_key_qcode(kbd->con, qcode, down);
104 if (kbd->key_delay_ms) {
105 qemu_input_event_send_key_delay(kbd->key_delay_ms);
110 void qkbd_state_lift_all_keys(QKbdState *kbd)
112 int qcode;
114 for (qcode = 0; qcode < Q_KEY_CODE__MAX; qcode++) {
115 if (test_bit(qcode, kbd->keys)) {
116 qkbd_state_key_event(kbd, qcode, false);
121 void qkbd_state_set_delay(QKbdState *kbd, int delay_ms)
123 kbd->key_delay_ms = delay_ms;
126 void qkbd_state_free(QKbdState *kbd)
128 g_free(kbd);
131 QKbdState *qkbd_state_init(QemuConsole *con)
133 QKbdState *kbd = g_new0(QKbdState, 1);
135 kbd->con = con;
137 return kbd;