aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Microsoft serial mouse emulation |
| 3 | * |
| 4 | * Copyright (c) 2008 Lubomir Rintel |
| 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 | */ |
Peter Maydell | 9c05833 | 2016-01-29 17:49:54 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 25 | #include "qemu-common.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 26 | #include "sysemu/char.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 27 | #include "ui/console.h" |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 28 | #include "ui/input.h" |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 29 | |
| 30 | #define MSMOUSE_LO6(n) ((n) & 0x3f) |
| 31 | #define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6) |
| 32 | |
Gerd Hoffmann | cde8dcb | 2016-07-04 11:42:52 +0200 | [diff] [blame] | 33 | typedef struct { |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 34 | Chardev parent; |
Marc-André Lureau | 41ac54b | 2016-10-21 23:44:44 +0300 | [diff] [blame] | 35 | |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 36 | QemuInputHandlerState *hs; |
| 37 | int axis[INPUT_AXIS__MAX]; |
| 38 | bool btns[INPUT_BUTTON__MAX]; |
Gerd Hoffmann | d7b7f526 | 2016-07-04 11:42:55 +0200 | [diff] [blame] | 39 | bool btnc[INPUT_BUTTON__MAX]; |
Gerd Hoffmann | 57a4e3b | 2016-07-04 11:42:53 +0200 | [diff] [blame] | 40 | uint8_t outbuf[32]; |
| 41 | int outlen; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 42 | } MouseChardev; |
Gerd Hoffmann | cde8dcb | 2016-07-04 11:42:52 +0200 | [diff] [blame] | 43 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 44 | #define TYPE_CHARDEV_MSMOUSE "chardev-msmouse" |
| 45 | #define MOUSE_CHARDEV(obj) \ |
| 46 | OBJECT_CHECK(MouseChardev, (obj), TYPE_CHARDEV_MSMOUSE) |
| 47 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 48 | static void msmouse_chr_accept_input(Chardev *chr) |
Gerd Hoffmann | 57a4e3b | 2016-07-04 11:42:53 +0200 | [diff] [blame] | 49 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 50 | MouseChardev *mouse = MOUSE_CHARDEV(chr); |
Gerd Hoffmann | 57a4e3b | 2016-07-04 11:42:53 +0200 | [diff] [blame] | 51 | int len; |
| 52 | |
| 53 | len = qemu_chr_be_can_write(chr); |
| 54 | if (len > mouse->outlen) { |
| 55 | len = mouse->outlen; |
| 56 | } |
| 57 | if (!len) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | qemu_chr_be_write(chr, mouse->outbuf, len); |
| 62 | mouse->outlen -= len; |
| 63 | if (mouse->outlen) { |
| 64 | memmove(mouse->outbuf, mouse->outbuf + len, mouse->outlen); |
| 65 | } |
| 66 | } |
| 67 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 68 | static void msmouse_queue_event(MouseChardev *mouse) |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 69 | { |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 70 | unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 }; |
Gerd Hoffmann | d7b7f526 | 2016-07-04 11:42:55 +0200 | [diff] [blame] | 71 | int dx, dy, count = 3; |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 72 | |
| 73 | dx = mouse->axis[INPUT_AXIS_X]; |
| 74 | mouse->axis[INPUT_AXIS_X] = 0; |
| 75 | |
| 76 | dy = mouse->axis[INPUT_AXIS_Y]; |
| 77 | mouse->axis[INPUT_AXIS_Y] = 0; |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 78 | |
| 79 | /* Movement deltas */ |
| 80 | bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx); |
| 81 | bytes[1] |= MSMOUSE_LO6(dx); |
| 82 | bytes[2] |= MSMOUSE_LO6(dy); |
| 83 | |
| 84 | /* Buttons */ |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 85 | bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00); |
| 86 | bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00); |
Gerd Hoffmann | d7b7f526 | 2016-07-04 11:42:55 +0200 | [diff] [blame] | 87 | if (mouse->btns[INPUT_BUTTON_MIDDLE] || |
| 88 | mouse->btnc[INPUT_BUTTON_MIDDLE]) { |
| 89 | bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00); |
| 90 | mouse->btnc[INPUT_BUTTON_MIDDLE] = false; |
| 91 | count = 4; |
| 92 | } |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 93 | |
Gerd Hoffmann | d7b7f526 | 2016-07-04 11:42:55 +0200 | [diff] [blame] | 94 | if (mouse->outlen <= sizeof(mouse->outbuf) - count) { |
| 95 | memcpy(mouse->outbuf + mouse->outlen, bytes, count); |
| 96 | mouse->outlen += count; |
Gerd Hoffmann | 57a4e3b | 2016-07-04 11:42:53 +0200 | [diff] [blame] | 97 | } else { |
| 98 | /* queue full -> drop event */ |
| 99 | } |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 100 | } |
Gerd Hoffmann | 57a4e3b | 2016-07-04 11:42:53 +0200 | [diff] [blame] | 101 | |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 102 | static void msmouse_input_event(DeviceState *dev, QemuConsole *src, |
| 103 | InputEvent *evt) |
| 104 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 105 | MouseChardev *mouse = MOUSE_CHARDEV(dev); |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 106 | InputMoveEvent *move; |
| 107 | InputBtnEvent *btn; |
| 108 | |
| 109 | switch (evt->type) { |
| 110 | case INPUT_EVENT_KIND_REL: |
| 111 | move = evt->u.rel.data; |
| 112 | mouse->axis[move->axis] += move->value; |
| 113 | break; |
| 114 | |
| 115 | case INPUT_EVENT_KIND_BTN: |
| 116 | btn = evt->u.btn.data; |
| 117 | mouse->btns[btn->button] = btn->down; |
Gerd Hoffmann | d7b7f526 | 2016-07-04 11:42:55 +0200 | [diff] [blame] | 118 | mouse->btnc[btn->button] = true; |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 119 | break; |
| 120 | |
| 121 | default: |
| 122 | /* keep gcc happy */ |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static void msmouse_input_sync(DeviceState *dev) |
| 128 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 129 | MouseChardev *mouse = MOUSE_CHARDEV(dev); |
| 130 | Chardev *chr = CHARDEV(dev); |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 131 | |
| 132 | msmouse_queue_event(mouse); |
Marc-André Lureau | 41ac54b | 2016-10-21 23:44:44 +0300 | [diff] [blame] | 133 | msmouse_chr_accept_input(chr); |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 136 | static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len) |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 137 | { |
| 138 | /* Ignore writes to mouse port */ |
| 139 | return len; |
| 140 | } |
| 141 | |
Marc-André Lureau | 8955e89 | 2016-12-08 16:34:44 +0300 | [diff] [blame] | 142 | static void char_msmouse_finalize(Object *obj) |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 143 | { |
Marc-André Lureau | 8955e89 | 2016-12-08 16:34:44 +0300 | [diff] [blame] | 144 | MouseChardev *mouse = MOUSE_CHARDEV(obj); |
Gerd Hoffmann | cde8dcb | 2016-07-04 11:42:52 +0200 | [diff] [blame] | 145 | |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 146 | qemu_input_handler_unregister(mouse->hs); |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 149 | static QemuInputHandler msmouse_handler = { |
| 150 | .name = "QEMU Microsoft Mouse", |
| 151 | .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL, |
| 152 | .event = msmouse_input_event, |
| 153 | .sync = msmouse_input_sync, |
| 154 | }; |
| 155 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 156 | static void msmouse_chr_open(Chardev *chr, |
| 157 | ChardevBackend *backend, |
| 158 | bool *be_opened, |
| 159 | Error **errp) |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 160 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 161 | MouseChardev *mouse = MOUSE_CHARDEV(chr); |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 162 | |
Marc-André Lureau | 82878da | 2016-10-22 13:09:43 +0300 | [diff] [blame] | 163 | *be_opened = false; |
Gerd Hoffmann | 96d7c07 | 2016-07-04 11:42:54 +0200 | [diff] [blame] | 164 | mouse->hs = qemu_input_handler_register((DeviceState *)mouse, |
| 165 | &msmouse_handler); |
aurel32 | aa71cf8 | 2009-02-08 15:53:20 +0000 | [diff] [blame] | 166 | } |
Anthony Liguori | 5ab8211 | 2013-03-05 23:21:31 +0530 | [diff] [blame] | 167 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 168 | static void char_msmouse_class_init(ObjectClass *oc, void *data) |
| 169 | { |
| 170 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 171 | |
| 172 | cc->open = msmouse_chr_open; |
| 173 | cc->chr_write = msmouse_chr_write; |
| 174 | cc->chr_accept_input = msmouse_chr_accept_input; |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static const TypeInfo char_msmouse_type_info = { |
| 178 | .name = TYPE_CHARDEV_MSMOUSE, |
| 179 | .parent = TYPE_CHARDEV, |
| 180 | .instance_size = sizeof(MouseChardev), |
Marc-André Lureau | 8955e89 | 2016-12-08 16:34:44 +0300 | [diff] [blame] | 181 | .instance_finalize = char_msmouse_finalize, |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 182 | .class_init = char_msmouse_class_init, |
| 183 | }; |
| 184 | |
Anthony Liguori | 5ab8211 | 2013-03-05 23:21:31 +0530 | [diff] [blame] | 185 | static void register_types(void) |
| 186 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 187 | type_register_static(&char_msmouse_type_info); |
Anthony Liguori | 5ab8211 | 2013-03-05 23:21:31 +0530 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | type_init(register_types); |