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
6 #include "qemu/osdep.h"
7 #include "qemu/bitmap.h"
8 #include "ui/console.h"
10 #include "ui/kbd-state.h"
15 DECLARE_BITMAP(keys
, Q_KEY_CODE__MAX
);
16 DECLARE_BITMAP(mods
, QKBD_MOD__MAX
);
19 static void qkbd_state_modifier_update(QKbdState
*kbd
,
20 QKeyCode qcode1
, QKeyCode qcode2
,
23 if (test_bit(qcode1
, kbd
->keys
) || test_bit(qcode2
, kbd
->keys
)) {
24 set_bit(mod
, kbd
->mods
);
26 clear_bit(mod
, kbd
->mods
);
30 bool qkbd_state_modifier_get(QKbdState
*kbd
, QKbdModifier mod
)
32 return test_bit(mod
, kbd
->mods
);
35 bool qkbd_state_key_get(QKbdState
*kbd
, QKeyCode qcode
)
37 return test_bit(qcode
, kbd
->keys
);
40 void qkbd_state_key_event(QKbdState
*kbd
, QKeyCode qcode
, bool down
)
42 bool state
= test_bit(qcode
, kbd
->keys
);
44 if (down
== false /* got key-up event */ &&
45 state
== false /* key is not pressed */) {
47 * Filter out suspicious key-up events.
49 * This allows simply sending along all key-up events, and
50 * this function will filter out everything where the
51 * corresponding key-down event wasn't sent to the guest, for
52 * example due to being a host hotkey.
54 * Note that key-down events on already pressed keys are *not*
55 * suspicious, those are keyboard autorepeat events.
60 /* update key and modifier state */
62 set_bit(qcode
, kbd
->keys
);
64 clear_bit(qcode
, kbd
->keys
);
67 case Q_KEY_CODE_SHIFT
:
68 case Q_KEY_CODE_SHIFT_R
:
69 qkbd_state_modifier_update(kbd
, Q_KEY_CODE_SHIFT
, Q_KEY_CODE_SHIFT_R
,
73 case Q_KEY_CODE_CTRL_R
:
74 qkbd_state_modifier_update(kbd
, Q_KEY_CODE_CTRL
, Q_KEY_CODE_CTRL_R
,
78 qkbd_state_modifier_update(kbd
, Q_KEY_CODE_ALT
, Q_KEY_CODE_ALT
,
81 case Q_KEY_CODE_ALT_R
:
82 qkbd_state_modifier_update(kbd
, Q_KEY_CODE_ALT_R
, Q_KEY_CODE_ALT_R
,
85 case Q_KEY_CODE_CAPS_LOCK
:
87 change_bit(QKBD_MOD_CAPSLOCK
, kbd
->mods
);
90 case Q_KEY_CODE_NUM_LOCK
:
92 change_bit(QKBD_MOD_NUMLOCK
, kbd
->mods
);
101 if (qemu_console_is_graphic(kbd
->con
)) {
102 qemu_input_event_send_key_qcode(kbd
->con
, qcode
, down
);
103 if (kbd
->key_delay_ms
) {
104 qemu_input_event_send_key_delay(kbd
->key_delay_ms
);
109 void qkbd_state_lift_all_keys(QKbdState
*kbd
)
113 for (qcode
= 0; qcode
< Q_KEY_CODE__MAX
; qcode
++) {
114 if (test_bit(qcode
, kbd
->keys
)) {
115 qkbd_state_key_event(kbd
, qcode
, false);
120 void qkbd_state_set_delay(QKbdState
*kbd
, int delay_ms
)
122 kbd
->key_delay_ms
= delay_ms
;
125 void qkbd_state_free(QKbdState
*kbd
)
130 QKbdState
*qkbd_state_init(QemuConsole
*con
)
132 QKbdState
*kbd
= g_new0(QKbdState
, 1);