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/sysemu.h" 26 #include "monitor/monitor.h" 27 #include "ui/console.h" 28 #include "qapi/error.h" 29 #include "qmp-commands.h" 30 #include "qapi-types.h" 31 #include "ui/keymaps.h" 32 #include "ui/input.h" 33 34 struct QEMUPutMouseEntry { 35 QEMUPutMouseEvent *qemu_put_mouse_event; 36 void *qemu_put_mouse_event_opaque; 37 int qemu_put_mouse_event_absolute; 38 39 /* new input core */ 40 QemuInputHandler h; 41 QemuInputHandlerState *s; 42 int axis[INPUT_AXIS_MAX]; 43 int buttons; 44 }; 45 46 struct QEMUPutKbdEntry { 47 QEMUPutKBDEvent *put_kbd; 48 void *opaque; 49 QemuInputHandlerState *s; 50 }; 51 52 struct QEMUPutLEDEntry { 53 QEMUPutLEDEvent *put_led; 54 void *opaque; 55 QTAILQ_ENTRY(QEMUPutLEDEntry) next; 56 }; 57 58 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = 59 QTAILQ_HEAD_INITIALIZER(led_handlers); 60 static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers = 61 QTAILQ_HEAD_INITIALIZER(mouse_handlers); 62 63 int index_from_key(const char *key) 64 { 65 int i; 66 67 for (i = 0; QKeyCode_lookup[i] != NULL; i++) { 68 if (!strcmp(key, QKeyCode_lookup[i])) { 69 break; 70 } 71 } 72 73 /* Return Q_KEY_CODE_MAX if the key is invalid */ 74 return i; 75 } 76 77 static KeyValue *copy_key_value(KeyValue *src) 78 { 79 KeyValue *dst = g_new(KeyValue, 1); 80 memcpy(dst, src, sizeof(*src)); 81 return dst; 82 } 83 84 void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time, 85 Error **errp) 86 { 87 KeyValueList *p; 88 89 if (!has_hold_time) { 90 hold_time = 0; /* use default */ 91 } 92 93 for (p = keys; p != NULL; p = p->next) { 94 qemu_input_event_send_key(NULL, copy_key_value(p->value), true); 95 qemu_input_event_send_key_delay(hold_time); 96 } 97 for (p = keys; p != NULL; p = p->next) { 98 qemu_input_event_send_key(NULL, copy_key_value(p->value), false); 99 qemu_input_event_send_key_delay(hold_time); 100 } 101 } 102 103 static void legacy_kbd_event(DeviceState *dev, QemuConsole *src, 104 InputEvent *evt) 105 { 106 QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev; 107 int scancodes[3], i, count; 108 109 if (!entry || !entry->put_kbd) { 110 return; 111 } 112 count = qemu_input_key_value_to_scancode(evt->key->key, 113 evt->key->down, 114 scancodes); 115 for (i = 0; i < count; i++) { 116 entry->put_kbd(entry->opaque, scancodes[i]); 117 } 118 } 119 120 static QemuInputHandler legacy_kbd_handler = { 121 .name = "legacy-kbd", 122 .mask = INPUT_EVENT_MASK_KEY, 123 .event = legacy_kbd_event, 124 }; 125 126 QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) 127 { 128 QEMUPutKbdEntry *entry; 129 130 entry = g_new0(QEMUPutKbdEntry, 1); 131 entry->put_kbd = func; 132 entry->opaque = opaque; 133 entry->s = qemu_input_handler_register((DeviceState *)entry, 134 &legacy_kbd_handler); 135 qemu_input_handler_activate(entry->s); 136 return entry; 137 } 138 139 void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry) 140 { 141 qemu_input_handler_unregister(entry->s); 142 g_free(entry); 143 } 144 145 static void legacy_mouse_event(DeviceState *dev, QemuConsole *src, 146 InputEvent *evt) 147 { 148 static const int bmap[INPUT_BUTTON_MAX] = { 149 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, 150 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, 151 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, 152 }; 153 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev; 154 155 switch (evt->kind) { 156 case INPUT_EVENT_KIND_BTN: 157 if (evt->btn->down) { 158 s->buttons |= bmap[evt->btn->button]; 159 } else { 160 s->buttons &= ~bmap[evt->btn->button]; 161 } 162 if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_UP) { 163 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque, 164 s->axis[INPUT_AXIS_X], 165 s->axis[INPUT_AXIS_Y], 166 -1, 167 s->buttons); 168 } 169 if (evt->btn->down && evt->btn->button == INPUT_BUTTON_WHEEL_DOWN) { 170 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque, 171 s->axis[INPUT_AXIS_X], 172 s->axis[INPUT_AXIS_Y], 173 1, 174 s->buttons); 175 } 176 break; 177 case INPUT_EVENT_KIND_ABS: 178 s->axis[evt->abs->axis] = evt->abs->value; 179 break; 180 case INPUT_EVENT_KIND_REL: 181 s->axis[evt->rel->axis] += evt->rel->value; 182 break; 183 default: 184 break; 185 } 186 } 187 188 static void legacy_mouse_sync(DeviceState *dev) 189 { 190 QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev; 191 192 s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque, 193 s->axis[INPUT_AXIS_X], 194 s->axis[INPUT_AXIS_Y], 195 0, 196 s->buttons); 197 198 if (!s->qemu_put_mouse_event_absolute) { 199 s->axis[INPUT_AXIS_X] = 0; 200 s->axis[INPUT_AXIS_Y] = 0; 201 } 202 } 203 204 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, 205 void *opaque, int absolute, 206 const char *name) 207 { 208 QEMUPutMouseEntry *s; 209 210 s = g_malloc0(sizeof(QEMUPutMouseEntry)); 211 212 s->qemu_put_mouse_event = func; 213 s->qemu_put_mouse_event_opaque = opaque; 214 s->qemu_put_mouse_event_absolute = absolute; 215 216 s->h.name = name; 217 s->h.mask = INPUT_EVENT_MASK_BTN | 218 (absolute ? INPUT_EVENT_MASK_ABS : INPUT_EVENT_MASK_REL); 219 s->h.event = legacy_mouse_event; 220 s->h.sync = legacy_mouse_sync; 221 s->s = qemu_input_handler_register((DeviceState *)s, 222 &s->h); 223 224 return s; 225 } 226 227 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry) 228 { 229 qemu_input_handler_activate(entry->s); 230 } 231 232 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) 233 { 234 qemu_input_handler_unregister(entry->s); 235 236 g_free(entry); 237 } 238 239 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func, 240 void *opaque) 241 { 242 QEMUPutLEDEntry *s; 243 244 s = g_malloc0(sizeof(QEMUPutLEDEntry)); 245 246 s->put_led = func; 247 s->opaque = opaque; 248 QTAILQ_INSERT_TAIL(&led_handlers, s, next); 249 return s; 250 } 251 252 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry) 253 { 254 if (entry == NULL) 255 return; 256 QTAILQ_REMOVE(&led_handlers, entry, next); 257 g_free(entry); 258 } 259 260 void kbd_put_ledstate(int ledstate) 261 { 262 QEMUPutLEDEntry *cursor; 263 264 QTAILQ_FOREACH(cursor, &led_handlers, next) { 265 cursor->put_led(cursor->opaque, ledstate); 266 } 267 } 268