]> git.proxmox.com Git - mirror_qemu.git/blame - chardev/msmouse.c
Use DECLARE_*CHECKER* macros
[mirror_qemu.git] / chardev / 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 */
0b8fa32f 24
9c058332 25#include "qemu/osdep.h"
0b8fa32f 26#include "qemu/module.h"
8228e353 27#include "chardev/char.h"
28ecbaee 28#include "ui/console.h"
96d7c072 29#include "ui/input.h"
db1015e9 30#include "qom/object.h"
aa71cf80
AJ
31
32#define MSMOUSE_LO6(n) ((n) & 0x3f)
33#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
34
db1015e9 35struct MouseChardev {
0ec7b3e7 36 Chardev parent;
41ac54b2 37
96d7c072
GH
38 QemuInputHandlerState *hs;
39 int axis[INPUT_AXIS__MAX];
40 bool btns[INPUT_BUTTON__MAX];
d7b7f526 41 bool btnc[INPUT_BUTTON__MAX];
57a4e3b9
GH
42 uint8_t outbuf[32];
43 int outlen;
db1015e9
EH
44};
45typedef struct MouseChardev MouseChardev;
cde8dcbc 46
777357d7 47#define TYPE_CHARDEV_MSMOUSE "chardev-msmouse"
8110fa1d
EH
48DECLARE_INSTANCE_CHECKER(MouseChardev, MOUSE_CHARDEV,
49 TYPE_CHARDEV_MSMOUSE)
777357d7 50
0ec7b3e7 51static void msmouse_chr_accept_input(Chardev *chr)
57a4e3b9 52{
777357d7 53 MouseChardev *mouse = MOUSE_CHARDEV(chr);
57a4e3b9
GH
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
0ec7b3e7 71static void msmouse_queue_event(MouseChardev *mouse)
aa71cf80 72{
aa71cf80 73 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
d7b7f526 74 int dx, dy, count = 3;
96d7c072
GH
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;
aa71cf80
AJ
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 */
96d7c072
GH
88 bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
89 bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
d7b7f526
GH
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 }
96
97 if (mouse->outlen <= sizeof(mouse->outbuf) - count) {
98 memcpy(mouse->outbuf + mouse->outlen, bytes, count);
99 mouse->outlen += count;
57a4e3b9
GH
100 } else {
101 /* queue full -> drop event */
102 }
96d7c072 103}
57a4e3b9 104
96d7c072
GH
105static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
106 InputEvent *evt)
107{
777357d7 108 MouseChardev *mouse = MOUSE_CHARDEV(dev);
96d7c072
GH
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;
d7b7f526 121 mouse->btnc[btn->button] = true;
96d7c072
GH
122 break;
123
124 default:
125 /* keep gcc happy */
126 break;
127 }
128}
129
130static void msmouse_input_sync(DeviceState *dev)
131{
777357d7
MAL
132 MouseChardev *mouse = MOUSE_CHARDEV(dev);
133 Chardev *chr = CHARDEV(dev);
96d7c072
GH
134
135 msmouse_queue_event(mouse);
41ac54b2 136 msmouse_chr_accept_input(chr);
aa71cf80
AJ
137}
138
0ec7b3e7 139static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
aa71cf80
AJ
140{
141 /* Ignore writes to mouse port */
142 return len;
143}
144
8955e891 145static void char_msmouse_finalize(Object *obj)
aa71cf80 146{
8955e891 147 MouseChardev *mouse = MOUSE_CHARDEV(obj);
cde8dcbc 148
96d7c072 149 qemu_input_handler_unregister(mouse->hs);
aa71cf80
AJ
150}
151
96d7c072
GH
152static 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
777357d7
MAL
159static void msmouse_chr_open(Chardev *chr,
160 ChardevBackend *backend,
161 bool *be_opened,
162 Error **errp)
aa71cf80 163{
777357d7 164 MouseChardev *mouse = MOUSE_CHARDEV(chr);
aa71cf80 165
82878dac 166 *be_opened = false;
96d7c072
GH
167 mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
168 &msmouse_handler);
777357d7 169}
cde8dcbc 170
777357d7
MAL
171static void char_msmouse_class_init(ObjectClass *oc, void *data)
172{
173 ChardevClass *cc = CHARDEV_CLASS(oc);
aa71cf80 174
777357d7
MAL
175 cc->open = msmouse_chr_open;
176 cc->chr_write = msmouse_chr_write;
177 cc->chr_accept_input = msmouse_chr_accept_input;
aa71cf80 178}
5ab8211b 179
777357d7
MAL
180static const TypeInfo char_msmouse_type_info = {
181 .name = TYPE_CHARDEV_MSMOUSE,
182 .parent = TYPE_CHARDEV,
183 .instance_size = sizeof(MouseChardev),
8955e891 184 .instance_finalize = char_msmouse_finalize,
777357d7
MAL
185 .class_init = char_msmouse_class_init,
186};
187
5ab8211b
AL
188static void register_types(void)
189{
777357d7 190 type_register_static(&char_msmouse_type_info);
5ab8211b
AL
191}
192
193type_init(register_types);