]> git.proxmox.com Git - mirror_qemu.git/blame - backends/msmouse.c
msmouse: fix buffer handling
[mirror_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"
aa71cf80
AJ
28
29#define MSMOUSE_LO6(n) ((n) & 0x3f)
30#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
31
cde8dcbc
GH
32typedef struct {
33 CharDriverState *chr;
34 QEMUPutMouseEntry *entry;
57a4e3b9
GH
35 uint8_t outbuf[32];
36 int outlen;
cde8dcbc
GH
37} MouseState;
38
57a4e3b9
GH
39static void msmouse_chr_accept_input(CharDriverState *chr)
40{
41 MouseState *mouse = chr->opaque;
42 int len;
43
44 len = qemu_chr_be_can_write(chr);
45 if (len > mouse->outlen) {
46 len = mouse->outlen;
47 }
48 if (!len) {
49 return;
50 }
51
52 qemu_chr_be_write(chr, mouse->outbuf, len);
53 mouse->outlen -= len;
54 if (mouse->outlen) {
55 memmove(mouse->outbuf, mouse->outbuf + len, mouse->outlen);
56 }
57}
58
aa71cf80
AJ
59static void msmouse_event(void *opaque,
60 int dx, int dy, int dz, int buttons_state)
61{
62 CharDriverState *chr = (CharDriverState *)opaque;
57a4e3b9 63 MouseState *mouse = chr->opaque;
aa71cf80
AJ
64 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
65
66 /* Movement deltas */
67 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
68 bytes[1] |= MSMOUSE_LO6(dx);
69 bytes[2] |= MSMOUSE_LO6(dy);
70
71 /* Buttons */
72 bytes[0] |= (buttons_state & 0x01 ? 0x20 : 0x00);
73 bytes[0] |= (buttons_state & 0x02 ? 0x10 : 0x00);
74 bytes[3] |= (buttons_state & 0x04 ? 0x20 : 0x00);
75
57a4e3b9
GH
76 if (mouse->outlen <= sizeof(mouse->outbuf) - 4) {
77 /* We always send the packet of, so that we do not have to keep track
78 of previous state of the middle button. This can potentially confuse
79 some very old drivers for two button mice though. */
80 memcpy(mouse->outbuf + mouse->outlen, bytes, 4);
81 mouse->outlen += 4;
82 } else {
83 /* queue full -> drop event */
84 }
85
86 msmouse_chr_accept_input(chr);
aa71cf80
AJ
87}
88
89static int msmouse_chr_write (struct CharDriverState *s, const uint8_t *buf, int len)
90{
91 /* Ignore writes to mouse port */
92 return len;
93}
94
95static void msmouse_chr_close (struct CharDriverState *chr)
96{
cde8dcbc
GH
97 MouseState *mouse = chr->opaque;
98
99 qemu_remove_mouse_event_handler(mouse->entry);
100 g_free(mouse);
101 g_free(chr);
aa71cf80
AJ
102}
103
96d885b9
PB
104static CharDriverState *qemu_chr_open_msmouse(const char *id,
105 ChardevBackend *backend,
106 ChardevReturn *ret,
107 Error **errp)
aa71cf80 108{
32bafa8f 109 ChardevCommon *common = backend->u.msmouse.data;
cde8dcbc 110 MouseState *mouse;
aa71cf80
AJ
111 CharDriverState *chr;
112
d0d7708b 113 chr = qemu_chr_alloc(common, errp);
aa71cf80
AJ
114 chr->chr_write = msmouse_chr_write;
115 chr->chr_close = msmouse_chr_close;
57a4e3b9 116 chr->chr_accept_input = msmouse_chr_accept_input;
bd5c51ee 117 chr->explicit_be_open = true;
aa71cf80 118
cde8dcbc
GH
119 mouse = g_new0(MouseState, 1);
120 mouse->entry = qemu_add_mouse_event_handler(msmouse_event, chr, 0,
121 "QEMU Microsoft Mouse");
122
123 mouse->chr = chr;
124 chr->opaque = mouse;
aa71cf80 125
1f51470d 126 return chr;
aa71cf80 127}
5ab8211b
AL
128
129static void register_types(void)
130{
4ca17281 131 register_char_driver("msmouse", CHARDEV_BACKEND_KIND_MSMOUSE, NULL,
96d885b9 132 qemu_chr_open_msmouse);
5ab8211b
AL
133}
134
135type_init(register_types);