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" 12 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); 18 }; 19 20 static void qkbd_state_modifier_update(QKbdState *kbd, 21 QKeyCode qcode1, QKeyCode qcode2, 22 QKbdModifier mod) 23 { 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); 28 } 29 } 30 31 bool qkbd_state_modifier_get(QKbdState *kbd, QKbdModifier mod) 32 { 33 return test_bit(mod, kbd->mods); 34 } 35 36 bool qkbd_state_key_get(QKbdState *kbd, QKeyCode qcode) 37 { 38 return test_bit(qcode, kbd->keys); 39 } 40 41 void qkbd_state_key_event(QKbdState *kbd, QKeyCode qcode, bool down) 42 { 43 bool state = test_bit(qcode, kbd->keys); 44 45 if (state == down) { 46 /* 47 * Filter out events which don't change the keyboard state. 48 * 49 * Most notably this allows to simply send along all key-up 50 * events, and this function will filter out everything where 51 * the corresponding key-down event wasn't send to the guest, 52 * for example due to being a host hotkey. 53 */ 54 return; 55 } 56 57 /* update key and modifier state */ 58 change_bit(qcode, kbd->keys); 59 switch (qcode) { 60 case Q_KEY_CODE_SHIFT: 61 case Q_KEY_CODE_SHIFT_R: 62 qkbd_state_modifier_update(kbd, Q_KEY_CODE_SHIFT, Q_KEY_CODE_SHIFT_R, 63 QKBD_MOD_SHIFT); 64 break; 65 case Q_KEY_CODE_CTRL: 66 case Q_KEY_CODE_CTRL_R: 67 qkbd_state_modifier_update(kbd, Q_KEY_CODE_CTRL, Q_KEY_CODE_CTRL_R, 68 QKBD_MOD_CTRL); 69 break; 70 case Q_KEY_CODE_ALT: 71 qkbd_state_modifier_update(kbd, Q_KEY_CODE_ALT, Q_KEY_CODE_ALT, 72 QKBD_MOD_ALT); 73 break; 74 case Q_KEY_CODE_ALT_R: 75 qkbd_state_modifier_update(kbd, Q_KEY_CODE_ALT_R, Q_KEY_CODE_ALT_R, 76 QKBD_MOD_ALTGR); 77 break; 78 case Q_KEY_CODE_CAPS_LOCK: 79 if (down) { 80 change_bit(QKBD_MOD_CAPSLOCK, kbd->mods); 81 } 82 break; 83 case Q_KEY_CODE_NUM_LOCK: 84 if (down) { 85 change_bit(QKBD_MOD_NUMLOCK, kbd->mods); 86 } 87 break; 88 default: 89 /* keep gcc happy */ 90 break; 91 } 92 93 /* send to guest */ 94 if (qemu_console_is_graphic(kbd->con)) { 95 qemu_input_event_send_key_qcode(kbd->con, qcode, down); 96 if (kbd->key_delay_ms) { 97 qemu_input_event_send_key_delay(kbd->key_delay_ms); 98 } 99 } 100 } 101 102 void qkbd_state_lift_all_keys(QKbdState *kbd) 103 { 104 int qcode; 105 106 for (qcode = 0; qcode < Q_KEY_CODE__MAX; qcode++) { 107 if (test_bit(qcode, kbd->keys)) { 108 qkbd_state_key_event(kbd, qcode, false); 109 } 110 } 111 } 112 113 void qkbd_state_set_delay(QKbdState *kbd, int delay_ms) 114 { 115 kbd->key_delay_ms = delay_ms; 116 } 117 118 void qkbd_state_free(QKbdState *kbd) 119 { 120 g_free(kbd); 121 } 122 123 QKbdState *qkbd_state_init(QemuConsole *con) 124 { 125 QKbdState *kbd = g_new0(QKbdState, 1); 126 127 kbd->con = con; 128 129 return kbd; 130 } 131