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