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