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