]> git.ipfire.org Git - thirdparty/qemu.git/blame - backends/msmouse.c
char: rename CharDriverState Chardev
[thirdparty/qemu.git] / backends / msmouse.c
CommitLineData
aa71cf80
AJ
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 */
9c058332 24#include "qemu/osdep.h"
28ecbaee 25#include "qemu-common.h"
dccfcd0e 26#include "sysemu/char.h"
28ecbaee 27#include "ui/console.h"
96d7c072 28#include "ui/input.h"
aa71cf80
AJ
29
30#define MSMOUSE_LO6(n) ((n) & 0x3f)
31#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
32
cde8dcbc 33typedef struct {
0ec7b3e7 34 Chardev parent;
41ac54b2 35
96d7c072
GH
36 QemuInputHandlerState *hs;
37 int axis[INPUT_AXIS__MAX];
38 bool btns[INPUT_BUTTON__MAX];
d7b7f526 39 bool btnc[INPUT_BUTTON__MAX];
57a4e3b9
GH
40 uint8_t outbuf[32];
41 int outlen;
0ec7b3e7 42} MouseChardev;
cde8dcbc 43
0ec7b3e7 44static void msmouse_chr_accept_input(Chardev *chr)
57a4e3b9 45{
0ec7b3e7 46 MouseChardev *mouse = (MouseChardev *)chr;
57a4e3b9
GH
47 int len;
48
49 len = qemu_chr_be_can_write(chr);
50 if (len > mouse->outlen) {
51 len = mouse->outlen;
52 }
53 if (!len) {
54 return;
55 }
56
57 qemu_chr_be_write(chr, mouse->outbuf, len);
58 mouse->outlen -= len;
59 if (mouse->outlen) {
60 memmove(mouse->outbuf, mouse->outbuf + len, mouse->outlen);
61 }
62}
63
0ec7b3e7 64static void msmouse_queue_event(MouseChardev *mouse)
aa71cf80 65{
aa71cf80 66 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
d7b7f526 67 int dx, dy, count = 3;
96d7c072
GH
68
69 dx = mouse->axis[INPUT_AXIS_X];
70 mouse->axis[INPUT_AXIS_X] = 0;
71
72 dy = mouse->axis[INPUT_AXIS_Y];
73 mouse->axis[INPUT_AXIS_Y] = 0;
aa71cf80
AJ
74
75 /* Movement deltas */
76 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
77 bytes[1] |= MSMOUSE_LO6(dx);
78 bytes[2] |= MSMOUSE_LO6(dy);
79
80 /* Buttons */
96d7c072
GH
81 bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
82 bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
d7b7f526
GH
83 if (mouse->btns[INPUT_BUTTON_MIDDLE] ||
84 mouse->btnc[INPUT_BUTTON_MIDDLE]) {
85 bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00);
86 mouse->btnc[INPUT_BUTTON_MIDDLE] = false;
87 count = 4;
88 }
89
90 if (mouse->outlen <= sizeof(mouse->outbuf) - count) {
91 memcpy(mouse->outbuf + mouse->outlen, bytes, count);
92 mouse->outlen += count;
57a4e3b9
GH
93 } else {
94 /* queue full -> drop event */
95 }
96d7c072 96}
57a4e3b9 97
96d7c072
GH
98static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
99 InputEvent *evt)
100{
0ec7b3e7 101 MouseChardev *mouse = (MouseChardev *)dev;
96d7c072
GH
102 InputMoveEvent *move;
103 InputBtnEvent *btn;
104
105 switch (evt->type) {
106 case INPUT_EVENT_KIND_REL:
107 move = evt->u.rel.data;
108 mouse->axis[move->axis] += move->value;
109 break;
110
111 case INPUT_EVENT_KIND_BTN:
112 btn = evt->u.btn.data;
113 mouse->btns[btn->button] = btn->down;
d7b7f526 114 mouse->btnc[btn->button] = true;
96d7c072
GH
115 break;
116
117 default:
118 /* keep gcc happy */
119 break;
120 }
121}
122
123static void msmouse_input_sync(DeviceState *dev)
124{
0ec7b3e7
MAL
125 MouseChardev *mouse = (MouseChardev *)dev;
126 Chardev *chr = (Chardev *)dev;
96d7c072
GH
127
128 msmouse_queue_event(mouse);
41ac54b2 129 msmouse_chr_accept_input(chr);
aa71cf80
AJ
130}
131
0ec7b3e7 132static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
aa71cf80
AJ
133{
134 /* Ignore writes to mouse port */
135 return len;
136}
137
0ec7b3e7 138static void msmouse_chr_free(struct Chardev *chr)
aa71cf80 139{
0ec7b3e7 140 MouseChardev *mouse = (MouseChardev *)chr;
cde8dcbc 141
96d7c072 142 qemu_input_handler_unregister(mouse->hs);
aa71cf80
AJ
143}
144
96d7c072
GH
145static QemuInputHandler msmouse_handler = {
146 .name = "QEMU Microsoft Mouse",
147 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
148 .event = msmouse_input_event,
149 .sync = msmouse_input_sync,
150};
151
0ec7b3e7
MAL
152static Chardev *qemu_chr_open_msmouse(const CharDriver *driver,
153 const char *id,
154 ChardevBackend *backend,
155 ChardevReturn *ret,
156 bool *be_opened,
157 Error **errp)
aa71cf80 158{
32bafa8f 159 ChardevCommon *common = backend->u.msmouse.data;
0ec7b3e7
MAL
160 MouseChardev *mouse;
161 Chardev *chr;
aa71cf80 162
b68e956a 163 chr = qemu_chr_alloc(driver, common, errp);
71200fb9
LM
164 if (!chr) {
165 return NULL;
166 }
82878dac 167 *be_opened = false;
aa71cf80 168
0ec7b3e7 169 mouse = (MouseChardev *)chr;
96d7c072
GH
170 mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
171 &msmouse_handler);
cde8dcbc 172
aa71cf80 173
1f51470d 174 return chr;
aa71cf80 175}
5ab8211b
AL
176
177static void register_types(void)
178{
0b812f31 179 static const CharDriver driver = {
0ec7b3e7 180 .instance_size = sizeof(MouseChardev),
0b812f31
MAL
181 .kind = CHARDEV_BACKEND_KIND_MSMOUSE,
182 .create = qemu_chr_open_msmouse,
b68e956a
MAL
183 .chr_write = msmouse_chr_write,
184 .chr_accept_input = msmouse_chr_accept_input,
185 .chr_free = msmouse_chr_free,
0b812f31
MAL
186 };
187 register_char_driver(&driver);
5ab8211b
AL
188}
189
190type_init(register_types);