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