]>
Commit | Line | Data |
---|---|---|
8f0056b7 PB |
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" | |
e235cec3 LC |
29 | #include "error.h" |
30 | #include "qmp-commands.h" | |
8f0056b7 | 31 | |
8f0056b7 PB |
32 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
33 | static void *qemu_put_kbd_event_opaque; | |
03a23a85 | 34 | static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers); |
6fef28ee AL |
35 | static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers = |
36 | QTAILQ_HEAD_INITIALIZER(mouse_handlers); | |
7e581fb3 AL |
37 | static NotifierList mouse_mode_notifiers = |
38 | NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers); | |
8f0056b7 | 39 | |
1048c88f AK |
40 | const int key_defs[] = { |
41 | [Q_KEY_CODE_SHIFT] = 0x2a, | |
42 | [Q_KEY_CODE_SHIFT_R] = 0x36, | |
43 | ||
44 | [Q_KEY_CODE_ALT] = 0x38, | |
45 | [Q_KEY_CODE_ALT_R] = 0xb8, | |
46 | [Q_KEY_CODE_ALTGR] = 0x64, | |
47 | [Q_KEY_CODE_ALTGR_R] = 0xe4, | |
48 | [Q_KEY_CODE_CTRL] = 0x1d, | |
49 | [Q_KEY_CODE_CTRL_R] = 0x9d, | |
50 | ||
51 | [Q_KEY_CODE_MENU] = 0xdd, | |
52 | ||
53 | [Q_KEY_CODE_ESC] = 0x01, | |
54 | ||
55 | [Q_KEY_CODE_1] = 0x02, | |
56 | [Q_KEY_CODE_2] = 0x03, | |
57 | [Q_KEY_CODE_3] = 0x04, | |
58 | [Q_KEY_CODE_4] = 0x05, | |
59 | [Q_KEY_CODE_5] = 0x06, | |
60 | [Q_KEY_CODE_6] = 0x07, | |
61 | [Q_KEY_CODE_7] = 0x08, | |
62 | [Q_KEY_CODE_8] = 0x09, | |
63 | [Q_KEY_CODE_9] = 0x0a, | |
64 | [Q_KEY_CODE_0] = 0x0b, | |
65 | [Q_KEY_CODE_MINUS] = 0x0c, | |
66 | [Q_KEY_CODE_EQUAL] = 0x0d, | |
67 | [Q_KEY_CODE_BACKSPACE] = 0x0e, | |
68 | ||
69 | [Q_KEY_CODE_TAB] = 0x0f, | |
70 | [Q_KEY_CODE_Q] = 0x10, | |
71 | [Q_KEY_CODE_W] = 0x11, | |
72 | [Q_KEY_CODE_E] = 0x12, | |
73 | [Q_KEY_CODE_R] = 0x13, | |
74 | [Q_KEY_CODE_T] = 0x14, | |
75 | [Q_KEY_CODE_Y] = 0x15, | |
76 | [Q_KEY_CODE_U] = 0x16, | |
77 | [Q_KEY_CODE_I] = 0x17, | |
78 | [Q_KEY_CODE_O] = 0x18, | |
79 | [Q_KEY_CODE_P] = 0x19, | |
80 | [Q_KEY_CODE_BRACKET_LEFT] = 0x1a, | |
81 | [Q_KEY_CODE_BRACKET_RIGHT] = 0x1b, | |
82 | [Q_KEY_CODE_RET] = 0x1c, | |
83 | ||
84 | [Q_KEY_CODE_A] = 0x1e, | |
85 | [Q_KEY_CODE_S] = 0x1f, | |
86 | [Q_KEY_CODE_D] = 0x20, | |
87 | [Q_KEY_CODE_F] = 0x21, | |
88 | [Q_KEY_CODE_G] = 0x22, | |
89 | [Q_KEY_CODE_H] = 0x23, | |
90 | [Q_KEY_CODE_J] = 0x24, | |
91 | [Q_KEY_CODE_K] = 0x25, | |
92 | [Q_KEY_CODE_L] = 0x26, | |
93 | [Q_KEY_CODE_SEMICOLON] = 0x27, | |
94 | [Q_KEY_CODE_APOSTROPHE] = 0x28, | |
95 | [Q_KEY_CODE_GRAVE_ACCENT] = 0x29, | |
96 | ||
97 | [Q_KEY_CODE_BACKSLASH] = 0x2b, | |
98 | [Q_KEY_CODE_Z] = 0x2c, | |
99 | [Q_KEY_CODE_X] = 0x2d, | |
100 | [Q_KEY_CODE_C] = 0x2e, | |
101 | [Q_KEY_CODE_V] = 0x2f, | |
102 | [Q_KEY_CODE_B] = 0x30, | |
103 | [Q_KEY_CODE_N] = 0x31, | |
104 | [Q_KEY_CODE_M] = 0x32, | |
105 | [Q_KEY_CODE_COMMA] = 0x33, | |
106 | [Q_KEY_CODE_DOT] = 0x34, | |
107 | [Q_KEY_CODE_SLASH] = 0x35, | |
108 | ||
109 | [Q_KEY_CODE_ASTERISK] = 0x37, | |
110 | ||
111 | [Q_KEY_CODE_SPC] = 0x39, | |
112 | [Q_KEY_CODE_CAPS_LOCK] = 0x3a, | |
113 | [Q_KEY_CODE_F1] = 0x3b, | |
114 | [Q_KEY_CODE_F2] = 0x3c, | |
115 | [Q_KEY_CODE_F3] = 0x3d, | |
116 | [Q_KEY_CODE_F4] = 0x3e, | |
117 | [Q_KEY_CODE_F5] = 0x3f, | |
118 | [Q_KEY_CODE_F6] = 0x40, | |
119 | [Q_KEY_CODE_F7] = 0x41, | |
120 | [Q_KEY_CODE_F8] = 0x42, | |
121 | [Q_KEY_CODE_F9] = 0x43, | |
122 | [Q_KEY_CODE_F10] = 0x44, | |
123 | [Q_KEY_CODE_NUM_LOCK] = 0x45, | |
124 | [Q_KEY_CODE_SCROLL_LOCK] = 0x46, | |
125 | ||
126 | [Q_KEY_CODE_KP_DIVIDE] = 0xb5, | |
127 | [Q_KEY_CODE_KP_MULTIPLY] = 0x37, | |
128 | [Q_KEY_CODE_KP_SUBTRACT] = 0x4a, | |
129 | [Q_KEY_CODE_KP_ADD] = 0x4e, | |
130 | [Q_KEY_CODE_KP_ENTER] = 0x9c, | |
131 | [Q_KEY_CODE_KP_DECIMAL] = 0x53, | |
132 | [Q_KEY_CODE_SYSRQ] = 0x54, | |
133 | ||
134 | [Q_KEY_CODE_KP_0] = 0x52, | |
135 | [Q_KEY_CODE_KP_1] = 0x4f, | |
136 | [Q_KEY_CODE_KP_2] = 0x50, | |
137 | [Q_KEY_CODE_KP_3] = 0x51, | |
138 | [Q_KEY_CODE_KP_4] = 0x4b, | |
139 | [Q_KEY_CODE_KP_5] = 0x4c, | |
140 | [Q_KEY_CODE_KP_6] = 0x4d, | |
141 | [Q_KEY_CODE_KP_7] = 0x47, | |
142 | [Q_KEY_CODE_KP_8] = 0x48, | |
143 | [Q_KEY_CODE_KP_9] = 0x49, | |
144 | ||
145 | [Q_KEY_CODE_LESS] = 0x56, | |
146 | ||
147 | [Q_KEY_CODE_F11] = 0x57, | |
148 | [Q_KEY_CODE_F12] = 0x58, | |
149 | ||
150 | [Q_KEY_CODE_PRINT] = 0xb7, | |
151 | ||
152 | [Q_KEY_CODE_HOME] = 0xc7, | |
153 | [Q_KEY_CODE_PGUP] = 0xc9, | |
154 | [Q_KEY_CODE_PGDN] = 0xd1, | |
155 | [Q_KEY_CODE_END] = 0xcf, | |
156 | ||
157 | [Q_KEY_CODE_LEFT] = 0xcb, | |
158 | [Q_KEY_CODE_UP] = 0xc8, | |
159 | [Q_KEY_CODE_DOWN] = 0xd0, | |
160 | [Q_KEY_CODE_RIGHT] = 0xcd, | |
161 | ||
162 | [Q_KEY_CODE_INSERT] = 0xd2, | |
163 | [Q_KEY_CODE_DELETE] = 0xd3, | |
164 | #ifdef NEED_CPU_H | |
165 | #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64) | |
166 | [Q_KEY_CODE_STOP] = 0xf0, | |
167 | [Q_KEY_CODE_AGAIN] = 0xf1, | |
168 | [Q_KEY_CODE_PROPS] = 0xf2, | |
169 | [Q_KEY_CODE_UNDO] = 0xf3, | |
170 | [Q_KEY_CODE_FRONT] = 0xf4, | |
171 | [Q_KEY_CODE_COPY] = 0xf5, | |
172 | [Q_KEY_CODE_OPEN] = 0xf6, | |
173 | [Q_KEY_CODE_PASTE] = 0xf7, | |
174 | [Q_KEY_CODE_FIND] = 0xf8, | |
175 | [Q_KEY_CODE_CUT] = 0xf9, | |
176 | [Q_KEY_CODE_LF] = 0xfa, | |
177 | [Q_KEY_CODE_HELP] = 0xfb, | |
178 | [Q_KEY_CODE_META_L] = 0xfc, | |
179 | [Q_KEY_CODE_META_R] = 0xfd, | |
180 | [Q_KEY_CODE_COMPOSE] = 0xfe, | |
181 | #endif | |
182 | #endif | |
183 | [Q_KEY_CODE_MAX] = 0, | |
184 | }; | |
185 | ||
186 | int index_from_key(const char *key) | |
187 | { | |
188 | int i, keycode; | |
189 | char *endp; | |
190 | ||
191 | for (i = 0; QKeyCode_lookup[i] != NULL; i++) { | |
192 | if (!strcmp(key, QKeyCode_lookup[i])) { | |
193 | break; | |
194 | } | |
195 | } | |
196 | ||
197 | if (strstart(key, "0x", NULL)) { | |
198 | keycode = strtoul(key, &endp, 0); | |
199 | if (*endp == '\0' && keycode >= 0x01 && keycode <= 0xff) { | |
200 | for (i = 0; i < Q_KEY_CODE_MAX; i++) { | |
201 | if (keycode == key_defs[i]) { | |
202 | break; | |
203 | } | |
204 | } | |
205 | } | |
206 | } | |
207 | ||
208 | /* Return Q_KEY_CODE_MAX if the key is invalid */ | |
209 | return i; | |
210 | } | |
211 | ||
212 | int index_from_keycode(int code) | |
213 | { | |
214 | int i; | |
215 | ||
216 | for (i = 0; i < Q_KEY_CODE_MAX; i++) { | |
217 | if (key_defs[i] == code) { | |
218 | break; | |
219 | } | |
220 | } | |
221 | ||
222 | /* Return Q_KEY_CODE_MAX if the code is invalid */ | |
223 | return i; | |
224 | } | |
225 | ||
8f0056b7 PB |
226 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
227 | { | |
228 | qemu_put_kbd_event_opaque = opaque; | |
229 | qemu_put_kbd_event = func; | |
230 | } | |
231 | ||
46aaebff JS |
232 | void qemu_remove_kbd_event_handler(void) |
233 | { | |
234 | qemu_put_kbd_event_opaque = NULL; | |
235 | qemu_put_kbd_event = NULL; | |
236 | } | |
237 | ||
7e581fb3 AL |
238 | static void check_mode_change(void) |
239 | { | |
240 | static int current_is_absolute, current_has_absolute; | |
241 | int is_absolute; | |
242 | int has_absolute; | |
243 | ||
244 | is_absolute = kbd_mouse_is_absolute(); | |
245 | has_absolute = kbd_mouse_has_absolute(); | |
246 | ||
247 | if (is_absolute != current_is_absolute || | |
248 | has_absolute != current_has_absolute) { | |
9e8dd451 | 249 | notifier_list_notify(&mouse_mode_notifiers, NULL); |
7e581fb3 AL |
250 | } |
251 | ||
252 | current_is_absolute = is_absolute; | |
253 | current_has_absolute = has_absolute; | |
254 | } | |
255 | ||
8f0056b7 PB |
256 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
257 | void *opaque, int absolute, | |
258 | const char *name) | |
259 | { | |
6fef28ee AL |
260 | QEMUPutMouseEntry *s; |
261 | static int mouse_index = 0; | |
8f0056b7 | 262 | |
7267c094 | 263 | s = g_malloc0(sizeof(QEMUPutMouseEntry)); |
8f0056b7 PB |
264 | |
265 | s->qemu_put_mouse_event = func; | |
266 | s->qemu_put_mouse_event_opaque = opaque; | |
267 | s->qemu_put_mouse_event_absolute = absolute; | |
7267c094 | 268 | s->qemu_put_mouse_event_name = g_strdup(name); |
6fef28ee | 269 | s->index = mouse_index++; |
8f0056b7 | 270 | |
6fef28ee | 271 | QTAILQ_INSERT_TAIL(&mouse_handlers, s, node); |
8f0056b7 | 272 | |
7e581fb3 AL |
273 | check_mode_change(); |
274 | ||
8f0056b7 PB |
275 | return s; |
276 | } | |
277 | ||
6fef28ee | 278 | void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry) |
8f0056b7 | 279 | { |
6fef28ee AL |
280 | QTAILQ_REMOVE(&mouse_handlers, entry, node); |
281 | QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node); | |
8f0056b7 | 282 | |
6fef28ee AL |
283 | check_mode_change(); |
284 | } | |
8f0056b7 | 285 | |
6fef28ee AL |
286 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
287 | { | |
288 | QTAILQ_REMOVE(&mouse_handlers, entry, node); | |
8f0056b7 | 289 | |
7267c094 AL |
290 | g_free(entry->qemu_put_mouse_event_name); |
291 | g_free(entry); | |
7e581fb3 AL |
292 | |
293 | check_mode_change(); | |
8f0056b7 PB |
294 | } |
295 | ||
03a23a85 GH |
296 | QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, |
297 | void *opaque) | |
298 | { | |
299 | QEMUPutLEDEntry *s; | |
300 | ||
7267c094 | 301 | s = g_malloc0(sizeof(QEMUPutLEDEntry)); |
03a23a85 GH |
302 | |
303 | s->put_led = func; | |
304 | s->opaque = opaque; | |
305 | QTAILQ_INSERT_TAIL(&led_handlers, s, next); | |
306 | return s; | |
307 | } | |
308 | ||
309 | void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry) | |
310 | { | |
311 | if (entry == NULL) | |
312 | return; | |
313 | QTAILQ_REMOVE(&led_handlers, entry, next); | |
7267c094 | 314 | g_free(entry); |
03a23a85 GH |
315 | } |
316 | ||
8f0056b7 PB |
317 | void kbd_put_keycode(int keycode) |
318 | { | |
ad02b96a | 319 | if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { |
99c7f878 GH |
320 | return; |
321 | } | |
8f0056b7 PB |
322 | if (qemu_put_kbd_event) { |
323 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); | |
324 | } | |
325 | } | |
326 | ||
03a23a85 GH |
327 | void kbd_put_ledstate(int ledstate) |
328 | { | |
329 | QEMUPutLEDEntry *cursor; | |
330 | ||
331 | QTAILQ_FOREACH(cursor, &led_handlers, next) { | |
332 | cursor->put_led(cursor->opaque, ledstate); | |
333 | } | |
334 | } | |
335 | ||
8f0056b7 PB |
336 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
337 | { | |
6fef28ee | 338 | QEMUPutMouseEntry *entry; |
8f0056b7 PB |
339 | QEMUPutMouseEvent *mouse_event; |
340 | void *mouse_event_opaque; | |
9312805d | 341 | int width, height; |
8f0056b7 | 342 | |
ad02b96a | 343 | if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) { |
99c7f878 GH |
344 | return; |
345 | } | |
6fef28ee | 346 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
8f0056b7 PB |
347 | return; |
348 | } | |
349 | ||
6fef28ee AL |
350 | entry = QTAILQ_FIRST(&mouse_handlers); |
351 | ||
352 | mouse_event = entry->qemu_put_mouse_event; | |
353 | mouse_event_opaque = entry->qemu_put_mouse_event_opaque; | |
8f0056b7 PB |
354 | |
355 | if (mouse_event) { | |
9312805d VK |
356 | if (entry->qemu_put_mouse_event_absolute) { |
357 | width = 0x7fff; | |
358 | height = 0x7fff; | |
97697373 | 359 | } else { |
9312805d VK |
360 | width = graphic_width - 1; |
361 | height = graphic_height - 1; | |
362 | } | |
363 | ||
364 | switch (graphic_rotate) { | |
365 | case 0: | |
366 | mouse_event(mouse_event_opaque, | |
367 | dx, dy, dz, buttons_state); | |
368 | break; | |
369 | case 90: | |
370 | mouse_event(mouse_event_opaque, | |
371 | width - dy, dx, dz, buttons_state); | |
372 | break; | |
373 | case 180: | |
374 | mouse_event(mouse_event_opaque, | |
375 | width - dx, height - dy, dz, buttons_state); | |
376 | break; | |
377 | case 270: | |
378 | mouse_event(mouse_event_opaque, | |
379 | dy, height - dx, dz, buttons_state); | |
380 | break; | |
97697373 | 381 | } |
8f0056b7 PB |
382 | } |
383 | } | |
384 | ||
385 | int kbd_mouse_is_absolute(void) | |
386 | { | |
6fef28ee | 387 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
8f0056b7 | 388 | return 0; |
6fef28ee | 389 | } |
8f0056b7 | 390 | |
6fef28ee | 391 | return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute; |
8f0056b7 PB |
392 | } |
393 | ||
eb2e259d AL |
394 | int kbd_mouse_has_absolute(void) |
395 | { | |
396 | QEMUPutMouseEntry *entry; | |
397 | ||
398 | QTAILQ_FOREACH(entry, &mouse_handlers, node) { | |
399 | if (entry->qemu_put_mouse_event_absolute) { | |
400 | return 1; | |
401 | } | |
402 | } | |
403 | ||
404 | return 0; | |
405 | } | |
406 | ||
e235cec3 | 407 | MouseInfoList *qmp_query_mice(Error **errp) |
8f0056b7 | 408 | { |
e235cec3 | 409 | MouseInfoList *mice_list = NULL; |
8f0056b7 | 410 | QEMUPutMouseEntry *cursor; |
e235cec3 | 411 | bool current = true; |
8f0056b7 | 412 | |
e235cec3 LC |
413 | QTAILQ_FOREACH(cursor, &mouse_handlers, node) { |
414 | MouseInfoList *info = g_malloc0(sizeof(*info)); | |
415 | info->value = g_malloc0(sizeof(*info->value)); | |
416 | info->value->name = g_strdup(cursor->qemu_put_mouse_event_name); | |
417 | info->value->index = cursor->index; | |
418 | info->value->absolute = !!cursor->qemu_put_mouse_event_absolute; | |
419 | info->value->current = current; | |
8f0056b7 | 420 | |
e235cec3 | 421 | current = false; |
6fef28ee | 422 | |
e235cec3 LC |
423 | info->next = mice_list; |
424 | mice_list = info; | |
8f0056b7 PB |
425 | } |
426 | ||
e235cec3 | 427 | return mice_list; |
8f0056b7 PB |
428 | } |
429 | ||
430 | void do_mouse_set(Monitor *mon, const QDict *qdict) | |
431 | { | |
432 | QEMUPutMouseEntry *cursor; | |
8f0056b7 | 433 | int index = qdict_get_int(qdict, "index"); |
6fef28ee | 434 | int found = 0; |
8f0056b7 | 435 | |
6fef28ee | 436 | if (QTAILQ_EMPTY(&mouse_handlers)) { |
8f0056b7 PB |
437 | monitor_printf(mon, "No mouse devices connected\n"); |
438 | return; | |
439 | } | |
440 | ||
6fef28ee AL |
441 | QTAILQ_FOREACH(cursor, &mouse_handlers, node) { |
442 | if (cursor->index == index) { | |
443 | found = 1; | |
444 | qemu_activate_mouse_event_handler(cursor); | |
445 | break; | |
446 | } | |
8f0056b7 PB |
447 | } |
448 | ||
6fef28ee | 449 | if (!found) { |
8f0056b7 | 450 | monitor_printf(mon, "Mouse at given index not found\n"); |
6fef28ee | 451 | } |
7e581fb3 AL |
452 | |
453 | check_mode_change(); | |
454 | } | |
455 | ||
456 | void qemu_add_mouse_mode_change_notifier(Notifier *notify) | |
457 | { | |
458 | notifier_list_add(&mouse_mode_notifiers, notify); | |
459 | } | |
460 | ||
461 | void qemu_remove_mouse_mode_change_notifier(Notifier *notify) | |
462 | { | |
31552529 | 463 | notifier_remove(notify); |
8f0056b7 | 464 | } |