Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "sysemu.h" |
| 26 | #include "net.h" |
| 27 | #include "monitor.h" |
| 28 | #include "console.h" |
Luiz Capitulino | e235cec | 2011-09-21 15:29:55 -0300 | [diff] [blame] | 29 | #include "error.h" |
| 30 | #include "qmp-commands.h" |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 31 | #include "qapi-types.h" |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 32 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 33 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 34 | static void *qemu_put_kbd_event_opaque; |
Gerd Hoffmann | 03a23a8 | 2010-02-26 17:17:36 +0100 | [diff] [blame] | 35 | static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers); |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 36 | static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers = |
| 37 | QTAILQ_HEAD_INITIALIZER(mouse_handlers); |
Anthony Liguori | 7e581fb | 2010-03-09 13:25:00 -0600 | [diff] [blame] | 38 | static NotifierList mouse_mode_notifiers = |
| 39 | NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers); |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 40 | |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 41 | static const int key_defs[] = { |
Amos Kong | 1048c88 | 2012-08-31 10:56:25 +0800 | [diff] [blame] | 42 | [Q_KEY_CODE_SHIFT] = 0x2a, |
| 43 | [Q_KEY_CODE_SHIFT_R] = 0x36, |
| 44 | |
| 45 | [Q_KEY_CODE_ALT] = 0x38, |
| 46 | [Q_KEY_CODE_ALT_R] = 0xb8, |
| 47 | [Q_KEY_CODE_ALTGR] = 0x64, |
| 48 | [Q_KEY_CODE_ALTGR_R] = 0xe4, |
| 49 | [Q_KEY_CODE_CTRL] = 0x1d, |
| 50 | [Q_KEY_CODE_CTRL_R] = 0x9d, |
| 51 | |
| 52 | [Q_KEY_CODE_MENU] = 0xdd, |
| 53 | |
| 54 | [Q_KEY_CODE_ESC] = 0x01, |
| 55 | |
| 56 | [Q_KEY_CODE_1] = 0x02, |
| 57 | [Q_KEY_CODE_2] = 0x03, |
| 58 | [Q_KEY_CODE_3] = 0x04, |
| 59 | [Q_KEY_CODE_4] = 0x05, |
| 60 | [Q_KEY_CODE_5] = 0x06, |
| 61 | [Q_KEY_CODE_6] = 0x07, |
| 62 | [Q_KEY_CODE_7] = 0x08, |
| 63 | [Q_KEY_CODE_8] = 0x09, |
| 64 | [Q_KEY_CODE_9] = 0x0a, |
| 65 | [Q_KEY_CODE_0] = 0x0b, |
| 66 | [Q_KEY_CODE_MINUS] = 0x0c, |
| 67 | [Q_KEY_CODE_EQUAL] = 0x0d, |
| 68 | [Q_KEY_CODE_BACKSPACE] = 0x0e, |
| 69 | |
| 70 | [Q_KEY_CODE_TAB] = 0x0f, |
| 71 | [Q_KEY_CODE_Q] = 0x10, |
| 72 | [Q_KEY_CODE_W] = 0x11, |
| 73 | [Q_KEY_CODE_E] = 0x12, |
| 74 | [Q_KEY_CODE_R] = 0x13, |
| 75 | [Q_KEY_CODE_T] = 0x14, |
| 76 | [Q_KEY_CODE_Y] = 0x15, |
| 77 | [Q_KEY_CODE_U] = 0x16, |
| 78 | [Q_KEY_CODE_I] = 0x17, |
| 79 | [Q_KEY_CODE_O] = 0x18, |
| 80 | [Q_KEY_CODE_P] = 0x19, |
| 81 | [Q_KEY_CODE_BRACKET_LEFT] = 0x1a, |
| 82 | [Q_KEY_CODE_BRACKET_RIGHT] = 0x1b, |
| 83 | [Q_KEY_CODE_RET] = 0x1c, |
| 84 | |
| 85 | [Q_KEY_CODE_A] = 0x1e, |
| 86 | [Q_KEY_CODE_S] = 0x1f, |
| 87 | [Q_KEY_CODE_D] = 0x20, |
| 88 | [Q_KEY_CODE_F] = 0x21, |
| 89 | [Q_KEY_CODE_G] = 0x22, |
| 90 | [Q_KEY_CODE_H] = 0x23, |
| 91 | [Q_KEY_CODE_J] = 0x24, |
| 92 | [Q_KEY_CODE_K] = 0x25, |
| 93 | [Q_KEY_CODE_L] = 0x26, |
| 94 | [Q_KEY_CODE_SEMICOLON] = 0x27, |
| 95 | [Q_KEY_CODE_APOSTROPHE] = 0x28, |
| 96 | [Q_KEY_CODE_GRAVE_ACCENT] = 0x29, |
| 97 | |
| 98 | [Q_KEY_CODE_BACKSLASH] = 0x2b, |
| 99 | [Q_KEY_CODE_Z] = 0x2c, |
| 100 | [Q_KEY_CODE_X] = 0x2d, |
| 101 | [Q_KEY_CODE_C] = 0x2e, |
| 102 | [Q_KEY_CODE_V] = 0x2f, |
| 103 | [Q_KEY_CODE_B] = 0x30, |
| 104 | [Q_KEY_CODE_N] = 0x31, |
| 105 | [Q_KEY_CODE_M] = 0x32, |
| 106 | [Q_KEY_CODE_COMMA] = 0x33, |
| 107 | [Q_KEY_CODE_DOT] = 0x34, |
| 108 | [Q_KEY_CODE_SLASH] = 0x35, |
| 109 | |
| 110 | [Q_KEY_CODE_ASTERISK] = 0x37, |
| 111 | |
| 112 | [Q_KEY_CODE_SPC] = 0x39, |
| 113 | [Q_KEY_CODE_CAPS_LOCK] = 0x3a, |
| 114 | [Q_KEY_CODE_F1] = 0x3b, |
| 115 | [Q_KEY_CODE_F2] = 0x3c, |
| 116 | [Q_KEY_CODE_F3] = 0x3d, |
| 117 | [Q_KEY_CODE_F4] = 0x3e, |
| 118 | [Q_KEY_CODE_F5] = 0x3f, |
| 119 | [Q_KEY_CODE_F6] = 0x40, |
| 120 | [Q_KEY_CODE_F7] = 0x41, |
| 121 | [Q_KEY_CODE_F8] = 0x42, |
| 122 | [Q_KEY_CODE_F9] = 0x43, |
| 123 | [Q_KEY_CODE_F10] = 0x44, |
| 124 | [Q_KEY_CODE_NUM_LOCK] = 0x45, |
| 125 | [Q_KEY_CODE_SCROLL_LOCK] = 0x46, |
| 126 | |
| 127 | [Q_KEY_CODE_KP_DIVIDE] = 0xb5, |
| 128 | [Q_KEY_CODE_KP_MULTIPLY] = 0x37, |
| 129 | [Q_KEY_CODE_KP_SUBTRACT] = 0x4a, |
| 130 | [Q_KEY_CODE_KP_ADD] = 0x4e, |
| 131 | [Q_KEY_CODE_KP_ENTER] = 0x9c, |
| 132 | [Q_KEY_CODE_KP_DECIMAL] = 0x53, |
| 133 | [Q_KEY_CODE_SYSRQ] = 0x54, |
| 134 | |
| 135 | [Q_KEY_CODE_KP_0] = 0x52, |
| 136 | [Q_KEY_CODE_KP_1] = 0x4f, |
| 137 | [Q_KEY_CODE_KP_2] = 0x50, |
| 138 | [Q_KEY_CODE_KP_3] = 0x51, |
| 139 | [Q_KEY_CODE_KP_4] = 0x4b, |
| 140 | [Q_KEY_CODE_KP_5] = 0x4c, |
| 141 | [Q_KEY_CODE_KP_6] = 0x4d, |
| 142 | [Q_KEY_CODE_KP_7] = 0x47, |
| 143 | [Q_KEY_CODE_KP_8] = 0x48, |
| 144 | [Q_KEY_CODE_KP_9] = 0x49, |
| 145 | |
| 146 | [Q_KEY_CODE_LESS] = 0x56, |
| 147 | |
| 148 | [Q_KEY_CODE_F11] = 0x57, |
| 149 | [Q_KEY_CODE_F12] = 0x58, |
| 150 | |
| 151 | [Q_KEY_CODE_PRINT] = 0xb7, |
| 152 | |
| 153 | [Q_KEY_CODE_HOME] = 0xc7, |
| 154 | [Q_KEY_CODE_PGUP] = 0xc9, |
| 155 | [Q_KEY_CODE_PGDN] = 0xd1, |
| 156 | [Q_KEY_CODE_END] = 0xcf, |
| 157 | |
| 158 | [Q_KEY_CODE_LEFT] = 0xcb, |
| 159 | [Q_KEY_CODE_UP] = 0xc8, |
| 160 | [Q_KEY_CODE_DOWN] = 0xd0, |
| 161 | [Q_KEY_CODE_RIGHT] = 0xcd, |
| 162 | |
| 163 | [Q_KEY_CODE_INSERT] = 0xd2, |
| 164 | [Q_KEY_CODE_DELETE] = 0xd3, |
| 165 | #ifdef NEED_CPU_H |
| 166 | #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64) |
| 167 | [Q_KEY_CODE_STOP] = 0xf0, |
| 168 | [Q_KEY_CODE_AGAIN] = 0xf1, |
| 169 | [Q_KEY_CODE_PROPS] = 0xf2, |
| 170 | [Q_KEY_CODE_UNDO] = 0xf3, |
| 171 | [Q_KEY_CODE_FRONT] = 0xf4, |
| 172 | [Q_KEY_CODE_COPY] = 0xf5, |
| 173 | [Q_KEY_CODE_OPEN] = 0xf6, |
| 174 | [Q_KEY_CODE_PASTE] = 0xf7, |
| 175 | [Q_KEY_CODE_FIND] = 0xf8, |
| 176 | [Q_KEY_CODE_CUT] = 0xf9, |
| 177 | [Q_KEY_CODE_LF] = 0xfa, |
| 178 | [Q_KEY_CODE_HELP] = 0xfb, |
| 179 | [Q_KEY_CODE_META_L] = 0xfc, |
| 180 | [Q_KEY_CODE_META_R] = 0xfd, |
| 181 | [Q_KEY_CODE_COMPOSE] = 0xfe, |
| 182 | #endif |
| 183 | #endif |
| 184 | [Q_KEY_CODE_MAX] = 0, |
| 185 | }; |
| 186 | |
| 187 | int index_from_key(const char *key) |
| 188 | { |
Luiz Capitulino | 9d537c9 | 2012-09-20 14:47:02 -0300 | [diff] [blame] | 189 | int i; |
Amos Kong | 1048c88 | 2012-08-31 10:56:25 +0800 | [diff] [blame] | 190 | |
| 191 | for (i = 0; QKeyCode_lookup[i] != NULL; i++) { |
| 192 | if (!strcmp(key, QKeyCode_lookup[i])) { |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
Amos Kong | 1048c88 | 2012-08-31 10:56:25 +0800 | [diff] [blame] | 197 | /* Return Q_KEY_CODE_MAX if the key is invalid */ |
| 198 | return i; |
| 199 | } |
| 200 | |
| 201 | int index_from_keycode(int code) |
| 202 | { |
| 203 | int i; |
| 204 | |
| 205 | for (i = 0; i < Q_KEY_CODE_MAX; i++) { |
| 206 | if (key_defs[i] == code) { |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* Return Q_KEY_CODE_MAX if the code is invalid */ |
| 212 | return i; |
| 213 | } |
| 214 | |
Luiz Capitulino | 05a3543 | 2012-09-20 13:44:28 -0300 | [diff] [blame] | 215 | static int *keycodes; |
| 216 | static int keycodes_size; |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 217 | static QEMUTimer *key_timer; |
| 218 | |
Luiz Capitulino | 9f32897 | 2012-09-20 14:19:47 -0300 | [diff] [blame] | 219 | static int keycode_from_keyvalue(const KeyValue *value) |
| 220 | { |
| 221 | if (value->kind == KEY_VALUE_KIND_QCODE) { |
| 222 | return key_defs[value->qcode]; |
| 223 | } else { |
| 224 | assert(value->kind == KEY_VALUE_KIND_NUMBER); |
| 225 | return value->number; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | static void free_keycodes(void) |
| 230 | { |
| 231 | g_free(keycodes); |
| 232 | keycodes = NULL; |
| 233 | keycodes_size = 0; |
| 234 | } |
| 235 | |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 236 | static void release_keys(void *opaque) |
| 237 | { |
Luiz Capitulino | 05a3543 | 2012-09-20 13:44:28 -0300 | [diff] [blame] | 238 | int i; |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 239 | |
Luiz Capitulino | 05a3543 | 2012-09-20 13:44:28 -0300 | [diff] [blame] | 240 | for (i = 0; i < keycodes_size; i++) { |
| 241 | if (keycodes[i] & 0x80) { |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 242 | kbd_put_keycode(0xe0); |
| 243 | } |
Luiz Capitulino | 05a3543 | 2012-09-20 13:44:28 -0300 | [diff] [blame] | 244 | kbd_put_keycode(keycodes[i]| 0x80); |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 245 | } |
Luiz Capitulino | 05a3543 | 2012-09-20 13:44:28 -0300 | [diff] [blame] | 246 | |
Luiz Capitulino | 9f32897 | 2012-09-20 14:19:47 -0300 | [diff] [blame] | 247 | free_keycodes(); |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 248 | } |
| 249 | |
Luiz Capitulino | 9f32897 | 2012-09-20 14:19:47 -0300 | [diff] [blame] | 250 | void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time, |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 251 | Error **errp) |
| 252 | { |
| 253 | int keycode; |
Luiz Capitulino | 9f32897 | 2012-09-20 14:19:47 -0300 | [diff] [blame] | 254 | KeyValueList *p; |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 255 | |
| 256 | if (!key_timer) { |
| 257 | key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL); |
| 258 | } |
| 259 | |
| 260 | if (keycodes != NULL) { |
| 261 | qemu_del_timer(key_timer); |
| 262 | release_keys(NULL); |
| 263 | } |
Luiz Capitulino | 05a3543 | 2012-09-20 13:44:28 -0300 | [diff] [blame] | 264 | |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 265 | if (!has_hold_time) { |
| 266 | hold_time = 100; |
| 267 | } |
| 268 | |
| 269 | for (p = keys; p != NULL; p = p->next) { |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 270 | /* key down events */ |
Luiz Capitulino | 9f32897 | 2012-09-20 14:19:47 -0300 | [diff] [blame] | 271 | keycode = keycode_from_keyvalue(p->value); |
| 272 | if (keycode < 0x01 || keycode > 0xff) { |
| 273 | error_setg(errp, "invalid hex keycode 0x%x\n", keycode); |
| 274 | free_keycodes(); |
| 275 | return; |
| 276 | } |
| 277 | |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 278 | if (keycode & 0x80) { |
| 279 | kbd_put_keycode(0xe0); |
| 280 | } |
| 281 | kbd_put_keycode(keycode & 0x7f); |
Luiz Capitulino | 05a3543 | 2012-09-20 13:44:28 -0300 | [diff] [blame] | 282 | |
| 283 | keycodes = g_realloc(keycodes, sizeof(int) * (keycodes_size + 1)); |
| 284 | keycodes[keycodes_size++] = keycode; |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 285 | } |
Amos Kong | e4c8f00 | 2012-08-31 10:56:26 +0800 | [diff] [blame] | 286 | |
| 287 | /* delayed key up events */ |
| 288 | qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) + |
| 289 | muldiv64(get_ticks_per_sec(), hold_time, 1000)); |
| 290 | } |
| 291 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 292 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 293 | { |
| 294 | qemu_put_kbd_event_opaque = opaque; |
| 295 | qemu_put_kbd_event = func; |
| 296 | } |
| 297 | |
Jes Sorensen | 46aaebf | 2010-06-08 15:12:18 +0200 | [diff] [blame] | 298 | void qemu_remove_kbd_event_handler(void) |
| 299 | { |
| 300 | qemu_put_kbd_event_opaque = NULL; |
| 301 | qemu_put_kbd_event = NULL; |
| 302 | } |
| 303 | |
Anthony Liguori | 7e581fb | 2010-03-09 13:25:00 -0600 | [diff] [blame] | 304 | static void check_mode_change(void) |
| 305 | { |
| 306 | static int current_is_absolute, current_has_absolute; |
| 307 | int is_absolute; |
| 308 | int has_absolute; |
| 309 | |
| 310 | is_absolute = kbd_mouse_is_absolute(); |
| 311 | has_absolute = kbd_mouse_has_absolute(); |
| 312 | |
| 313 | if (is_absolute != current_is_absolute || |
| 314 | has_absolute != current_has_absolute) { |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 315 | notifier_list_notify(&mouse_mode_notifiers, NULL); |
Anthony Liguori | 7e581fb | 2010-03-09 13:25:00 -0600 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | current_is_absolute = is_absolute; |
| 319 | current_has_absolute = has_absolute; |
| 320 | } |
| 321 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 322 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 323 | void *opaque, int absolute, |
| 324 | const char *name) |
| 325 | { |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 326 | QEMUPutMouseEntry *s; |
| 327 | static int mouse_index = 0; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 328 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 329 | s = g_malloc0(sizeof(QEMUPutMouseEntry)); |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 330 | |
| 331 | s->qemu_put_mouse_event = func; |
| 332 | s->qemu_put_mouse_event_opaque = opaque; |
| 333 | s->qemu_put_mouse_event_absolute = absolute; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 334 | s->qemu_put_mouse_event_name = g_strdup(name); |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 335 | s->index = mouse_index++; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 336 | |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 337 | QTAILQ_INSERT_TAIL(&mouse_handlers, s, node); |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 338 | |
Anthony Liguori | 7e581fb | 2010-03-09 13:25:00 -0600 | [diff] [blame] | 339 | check_mode_change(); |
| 340 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 341 | return s; |
| 342 | } |
| 343 | |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 344 | void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 345 | { |
| 346 | QTAILQ_REMOVE(&mouse_handlers, entry, node); |
| 347 | QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node); |
| 348 | |
| 349 | check_mode_change(); |
| 350 | } |
| 351 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 352 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 353 | { |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 354 | QTAILQ_REMOVE(&mouse_handlers, entry, node); |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 355 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 356 | g_free(entry->qemu_put_mouse_event_name); |
| 357 | g_free(entry); |
Anthony Liguori | 7e581fb | 2010-03-09 13:25:00 -0600 | [diff] [blame] | 358 | |
| 359 | check_mode_change(); |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Gerd Hoffmann | 03a23a8 | 2010-02-26 17:17:36 +0100 | [diff] [blame] | 362 | QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, |
| 363 | void *opaque) |
| 364 | { |
| 365 | QEMUPutLEDEntry *s; |
| 366 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 367 | s = g_malloc0(sizeof(QEMUPutLEDEntry)); |
Gerd Hoffmann | 03a23a8 | 2010-02-26 17:17:36 +0100 | [diff] [blame] | 368 | |
| 369 | s->put_led = func; |
| 370 | s->opaque = opaque; |
| 371 | QTAILQ_INSERT_TAIL(&led_handlers, s, next); |
| 372 | return s; |
| 373 | } |
| 374 | |
| 375 | void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry) |
| 376 | { |
| 377 | if (entry == NULL) |
| 378 | return; |
| 379 | QTAILQ_REMOVE(&led_handlers, entry, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 380 | g_free(entry); |
Gerd Hoffmann | 03a23a8 | 2010-02-26 17:17:36 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 383 | void kbd_put_keycode(int keycode) |
| 384 | { |
Luiz Capitulino | ad02b96 | 2012-04-27 13:33:36 -0300 | [diff] [blame] | 385 | if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { |
Gerd Hoffmann | 99c7f87 | 2012-02-15 09:15:37 +0100 | [diff] [blame] | 386 | return; |
| 387 | } |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 388 | if (qemu_put_kbd_event) { |
| 389 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 390 | } |
| 391 | } |
| 392 | |
Gerd Hoffmann | 03a23a8 | 2010-02-26 17:17:36 +0100 | [diff] [blame] | 393 | void kbd_put_ledstate(int ledstate) |
| 394 | { |
| 395 | QEMUPutLEDEntry *cursor; |
| 396 | |
| 397 | QTAILQ_FOREACH(cursor, &led_handlers, next) { |
| 398 | cursor->put_led(cursor->opaque, ledstate); |
| 399 | } |
| 400 | } |
| 401 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 402 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 403 | { |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 404 | QEMUPutMouseEntry *entry; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 405 | QEMUPutMouseEvent *mouse_event; |
| 406 | void *mouse_event_opaque; |
Vasily Khoruzhick | 9312805 | 2011-06-17 13:04:36 +0300 | [diff] [blame] | 407 | int width, height; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 408 | |
Luiz Capitulino | ad02b96 | 2012-04-27 13:33:36 -0300 | [diff] [blame] | 409 | if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { |
Gerd Hoffmann | 99c7f87 | 2012-02-15 09:15:37 +0100 | [diff] [blame] | 410 | return; |
| 411 | } |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 412 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 416 | entry = QTAILQ_FIRST(&mouse_handlers); |
| 417 | |
| 418 | mouse_event = entry->qemu_put_mouse_event; |
| 419 | mouse_event_opaque = entry->qemu_put_mouse_event_opaque; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 420 | |
| 421 | if (mouse_event) { |
Vasily Khoruzhick | 9312805 | 2011-06-17 13:04:36 +0300 | [diff] [blame] | 422 | if (entry->qemu_put_mouse_event_absolute) { |
| 423 | width = 0x7fff; |
| 424 | height = 0x7fff; |
Brad Hards | 9769737 | 2011-04-09 12:11:36 +1000 | [diff] [blame] | 425 | } else { |
Vasily Khoruzhick | 9312805 | 2011-06-17 13:04:36 +0300 | [diff] [blame] | 426 | width = graphic_width - 1; |
| 427 | height = graphic_height - 1; |
| 428 | } |
| 429 | |
| 430 | switch (graphic_rotate) { |
| 431 | case 0: |
| 432 | mouse_event(mouse_event_opaque, |
| 433 | dx, dy, dz, buttons_state); |
| 434 | break; |
| 435 | case 90: |
| 436 | mouse_event(mouse_event_opaque, |
| 437 | width - dy, dx, dz, buttons_state); |
| 438 | break; |
| 439 | case 180: |
| 440 | mouse_event(mouse_event_opaque, |
| 441 | width - dx, height - dy, dz, buttons_state); |
| 442 | break; |
| 443 | case 270: |
| 444 | mouse_event(mouse_event_opaque, |
| 445 | dy, height - dx, dz, buttons_state); |
| 446 | break; |
Brad Hards | 9769737 | 2011-04-09 12:11:36 +1000 | [diff] [blame] | 447 | } |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | int kbd_mouse_is_absolute(void) |
| 452 | { |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 453 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 454 | return 0; |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 455 | } |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 456 | |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 457 | return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Anthony Liguori | eb2e259 | 2010-03-09 14:26:40 -0600 | [diff] [blame] | 460 | int kbd_mouse_has_absolute(void) |
| 461 | { |
| 462 | QEMUPutMouseEntry *entry; |
| 463 | |
| 464 | QTAILQ_FOREACH(entry, &mouse_handlers, node) { |
| 465 | if (entry->qemu_put_mouse_event_absolute) { |
| 466 | return 1; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Luiz Capitulino | e235cec | 2011-09-21 15:29:55 -0300 | [diff] [blame] | 473 | MouseInfoList *qmp_query_mice(Error **errp) |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 474 | { |
Luiz Capitulino | e235cec | 2011-09-21 15:29:55 -0300 | [diff] [blame] | 475 | MouseInfoList *mice_list = NULL; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 476 | QEMUPutMouseEntry *cursor; |
Luiz Capitulino | e235cec | 2011-09-21 15:29:55 -0300 | [diff] [blame] | 477 | bool current = true; |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 478 | |
| 479 | QTAILQ_FOREACH(cursor, &mouse_handlers, node) { |
Luiz Capitulino | e235cec | 2011-09-21 15:29:55 -0300 | [diff] [blame] | 480 | MouseInfoList *info = g_malloc0(sizeof(*info)); |
| 481 | info->value = g_malloc0(sizeof(*info->value)); |
| 482 | info->value->name = g_strdup(cursor->qemu_put_mouse_event_name); |
| 483 | info->value->index = cursor->index; |
| 484 | info->value->absolute = !!cursor->qemu_put_mouse_event_absolute; |
| 485 | info->value->current = current; |
| 486 | |
| 487 | current = false; |
| 488 | |
| 489 | info->next = mice_list; |
| 490 | mice_list = info; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 491 | } |
| 492 | |
Luiz Capitulino | e235cec | 2011-09-21 15:29:55 -0300 | [diff] [blame] | 493 | return mice_list; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void do_mouse_set(Monitor *mon, const QDict *qdict) |
| 497 | { |
| 498 | QEMUPutMouseEntry *cursor; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 499 | int index = qdict_get_int(qdict, "index"); |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 500 | int found = 0; |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 501 | |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 502 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 503 | monitor_printf(mon, "No mouse devices connected\n"); |
| 504 | return; |
| 505 | } |
| 506 | |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 507 | QTAILQ_FOREACH(cursor, &mouse_handlers, node) { |
| 508 | if (cursor->index == index) { |
| 509 | found = 1; |
| 510 | qemu_activate_mouse_event_handler(cursor); |
| 511 | break; |
| 512 | } |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 515 | if (!found) { |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 516 | monitor_printf(mon, "Mouse at given index not found\n"); |
Anthony Liguori | 6fef28e | 2010-03-09 20:52:22 -0600 | [diff] [blame] | 517 | } |
Anthony Liguori | 7e581fb | 2010-03-09 13:25:00 -0600 | [diff] [blame] | 518 | |
| 519 | check_mode_change(); |
| 520 | } |
| 521 | |
| 522 | void qemu_add_mouse_mode_change_notifier(Notifier *notify) |
| 523 | { |
| 524 | notifier_list_add(&mouse_mode_notifiers, notify); |
| 525 | } |
| 526 | |
| 527 | void qemu_remove_mouse_mode_change_notifier(Notifier *notify) |
| 528 | { |
Paolo Bonzini | 3155252 | 2012-01-13 17:34:01 +0100 | [diff] [blame] | 529 | notifier_remove(notify); |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 530 | } |