1 #include "qemu/osdep.h" 2 #include "sysemu/sysemu.h" 3 #include "ui/keymaps.h" 4 #include "ui/input.h" 5 6 static const int qcode_to_number[] = { 7 [Q_KEY_CODE_SHIFT] = 0x2a, 8 [Q_KEY_CODE_SHIFT_R] = 0x36, 9 10 [Q_KEY_CODE_ALT] = 0x38, 11 [Q_KEY_CODE_ALT_R] = 0xb8, 12 [Q_KEY_CODE_ALTGR] = 0x64, 13 [Q_KEY_CODE_ALTGR_R] = 0xe4, 14 [Q_KEY_CODE_CTRL] = 0x1d, 15 [Q_KEY_CODE_CTRL_R] = 0x9d, 16 17 [Q_KEY_CODE_META_L] = 0xdb, 18 [Q_KEY_CODE_META_R] = 0xdc, 19 [Q_KEY_CODE_MENU] = 0xdd, 20 21 [Q_KEY_CODE_ESC] = 0x01, 22 23 [Q_KEY_CODE_1] = 0x02, 24 [Q_KEY_CODE_2] = 0x03, 25 [Q_KEY_CODE_3] = 0x04, 26 [Q_KEY_CODE_4] = 0x05, 27 [Q_KEY_CODE_5] = 0x06, 28 [Q_KEY_CODE_6] = 0x07, 29 [Q_KEY_CODE_7] = 0x08, 30 [Q_KEY_CODE_8] = 0x09, 31 [Q_KEY_CODE_9] = 0x0a, 32 [Q_KEY_CODE_0] = 0x0b, 33 [Q_KEY_CODE_MINUS] = 0x0c, 34 [Q_KEY_CODE_EQUAL] = 0x0d, 35 [Q_KEY_CODE_BACKSPACE] = 0x0e, 36 37 [Q_KEY_CODE_TAB] = 0x0f, 38 [Q_KEY_CODE_Q] = 0x10, 39 [Q_KEY_CODE_W] = 0x11, 40 [Q_KEY_CODE_E] = 0x12, 41 [Q_KEY_CODE_R] = 0x13, 42 [Q_KEY_CODE_T] = 0x14, 43 [Q_KEY_CODE_Y] = 0x15, 44 [Q_KEY_CODE_U] = 0x16, 45 [Q_KEY_CODE_I] = 0x17, 46 [Q_KEY_CODE_O] = 0x18, 47 [Q_KEY_CODE_P] = 0x19, 48 [Q_KEY_CODE_BRACKET_LEFT] = 0x1a, 49 [Q_KEY_CODE_BRACKET_RIGHT] = 0x1b, 50 [Q_KEY_CODE_RET] = 0x1c, 51 52 [Q_KEY_CODE_A] = 0x1e, 53 [Q_KEY_CODE_S] = 0x1f, 54 [Q_KEY_CODE_D] = 0x20, 55 [Q_KEY_CODE_F] = 0x21, 56 [Q_KEY_CODE_G] = 0x22, 57 [Q_KEY_CODE_H] = 0x23, 58 [Q_KEY_CODE_J] = 0x24, 59 [Q_KEY_CODE_K] = 0x25, 60 [Q_KEY_CODE_L] = 0x26, 61 [Q_KEY_CODE_SEMICOLON] = 0x27, 62 [Q_KEY_CODE_APOSTROPHE] = 0x28, 63 [Q_KEY_CODE_GRAVE_ACCENT] = 0x29, 64 65 [Q_KEY_CODE_BACKSLASH] = 0x2b, 66 [Q_KEY_CODE_Z] = 0x2c, 67 [Q_KEY_CODE_X] = 0x2d, 68 [Q_KEY_CODE_C] = 0x2e, 69 [Q_KEY_CODE_V] = 0x2f, 70 [Q_KEY_CODE_B] = 0x30, 71 [Q_KEY_CODE_N] = 0x31, 72 [Q_KEY_CODE_M] = 0x32, 73 [Q_KEY_CODE_COMMA] = 0x33, 74 [Q_KEY_CODE_DOT] = 0x34, 75 [Q_KEY_CODE_SLASH] = 0x35, 76 77 [Q_KEY_CODE_ASTERISK] = 0x37, 78 79 [Q_KEY_CODE_SPC] = 0x39, 80 [Q_KEY_CODE_CAPS_LOCK] = 0x3a, 81 [Q_KEY_CODE_F1] = 0x3b, 82 [Q_KEY_CODE_F2] = 0x3c, 83 [Q_KEY_CODE_F3] = 0x3d, 84 [Q_KEY_CODE_F4] = 0x3e, 85 [Q_KEY_CODE_F5] = 0x3f, 86 [Q_KEY_CODE_F6] = 0x40, 87 [Q_KEY_CODE_F7] = 0x41, 88 [Q_KEY_CODE_F8] = 0x42, 89 [Q_KEY_CODE_F9] = 0x43, 90 [Q_KEY_CODE_F10] = 0x44, 91 [Q_KEY_CODE_NUM_LOCK] = 0x45, 92 [Q_KEY_CODE_SCROLL_LOCK] = 0x46, 93 94 [Q_KEY_CODE_KP_DIVIDE] = 0xb5, 95 [Q_KEY_CODE_KP_MULTIPLY] = 0x37, 96 [Q_KEY_CODE_KP_SUBTRACT] = 0x4a, 97 [Q_KEY_CODE_KP_ADD] = 0x4e, 98 [Q_KEY_CODE_KP_ENTER] = 0x9c, 99 [Q_KEY_CODE_KP_DECIMAL] = 0x53, 100 [Q_KEY_CODE_SYSRQ] = 0x54, 101 102 [Q_KEY_CODE_KP_0] = 0x52, 103 [Q_KEY_CODE_KP_1] = 0x4f, 104 [Q_KEY_CODE_KP_2] = 0x50, 105 [Q_KEY_CODE_KP_3] = 0x51, 106 [Q_KEY_CODE_KP_4] = 0x4b, 107 [Q_KEY_CODE_KP_5] = 0x4c, 108 [Q_KEY_CODE_KP_6] = 0x4d, 109 [Q_KEY_CODE_KP_7] = 0x47, 110 [Q_KEY_CODE_KP_8] = 0x48, 111 [Q_KEY_CODE_KP_9] = 0x49, 112 113 [Q_KEY_CODE_LESS] = 0x56, 114 115 [Q_KEY_CODE_F11] = 0x57, 116 [Q_KEY_CODE_F12] = 0x58, 117 118 [Q_KEY_CODE_PRINT] = 0xb7, 119 120 [Q_KEY_CODE_HOME] = 0xc7, 121 [Q_KEY_CODE_PGUP] = 0xc9, 122 [Q_KEY_CODE_PGDN] = 0xd1, 123 [Q_KEY_CODE_END] = 0xcf, 124 125 [Q_KEY_CODE_LEFT] = 0xcb, 126 [Q_KEY_CODE_UP] = 0xc8, 127 [Q_KEY_CODE_DOWN] = 0xd0, 128 [Q_KEY_CODE_RIGHT] = 0xcd, 129 130 [Q_KEY_CODE_INSERT] = 0xd2, 131 [Q_KEY_CODE_DELETE] = 0xd3, 132 133 [Q_KEY_CODE_RO] = 0x73, 134 [Q_KEY_CODE_HIRAGANA] = 0x70, 135 [Q_KEY_CODE_HENKAN] = 0x79, 136 [Q_KEY_CODE_YEN] = 0x7d, 137 [Q_KEY_CODE_KP_COMMA] = 0x7e, 138 139 [Q_KEY_CODE__MAX] = 0, 140 }; 141 142 static int number_to_qcode[0x100]; 143 144 int qemu_input_key_value_to_number(const KeyValue *value) 145 { 146 if (value->type == KEY_VALUE_KIND_QCODE) { 147 return qcode_to_number[value->u.qcode.data]; 148 } else { 149 assert(value->type == KEY_VALUE_KIND_NUMBER); 150 return value->u.number.data; 151 } 152 } 153 154 int qemu_input_key_number_to_qcode(uint8_t nr) 155 { 156 static int first = true; 157 158 if (first) { 159 int qcode, number; 160 first = false; 161 for (qcode = 0; qcode < Q_KEY_CODE__MAX; qcode++) { 162 number = qcode_to_number[qcode]; 163 assert(number < ARRAY_SIZE(number_to_qcode)); 164 number_to_qcode[number] = qcode; 165 } 166 } 167 168 return number_to_qcode[nr]; 169 } 170 171 int qemu_input_key_value_to_qcode(const KeyValue *value) 172 { 173 if (value->type == KEY_VALUE_KIND_QCODE) { 174 return value->u.qcode.data; 175 } else { 176 assert(value->type == KEY_VALUE_KIND_NUMBER); 177 return qemu_input_key_number_to_qcode(value->u.number.data); 178 } 179 } 180 181 int qemu_input_key_value_to_scancode(const KeyValue *value, bool down, 182 int *codes) 183 { 184 int keycode = qemu_input_key_value_to_number(value); 185 int count = 0; 186 187 if (value->type == KEY_VALUE_KIND_QCODE && 188 value->u.qcode.data == Q_KEY_CODE_PAUSE) { 189 /* specific case */ 190 int v = down ? 0 : 0x80; 191 codes[count++] = 0xe1; 192 codes[count++] = 0x1d | v; 193 codes[count++] = 0x45 | v; 194 return count; 195 } 196 if (keycode & SCANCODE_GREY) { 197 codes[count++] = SCANCODE_EMUL0; 198 keycode &= ~SCANCODE_GREY; 199 } 200 if (!down) { 201 keycode |= SCANCODE_UP; 202 } 203 codes[count++] = keycode; 204 205 return count; 206 } 207