blob: c0dbfcdd6bf2c87c0d0d0983748cb7a9d774b11d [file] [log] [blame]
aurel32aa71cf82009-02-08 15:53:20 +00001/*
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 */
24#include <stdlib.h>
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010025#include "qemu-common.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020026#include "sysemu/char.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/console.h"
aurel32aa71cf82009-02-08 15:53:20 +000028
29#define MSMOUSE_LO6(n) ((n) & 0x3f)
30#define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
31
32static void msmouse_event(void *opaque,
33 int dx, int dy, int dz, int buttons_state)
34{
35 CharDriverState *chr = (CharDriverState *)opaque;
36
37 unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
38
39 /* Movement deltas */
40 bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
41 bytes[1] |= MSMOUSE_LO6(dx);
42 bytes[2] |= MSMOUSE_LO6(dy);
43
44 /* Buttons */
45 bytes[0] |= (buttons_state & 0x01 ? 0x20 : 0x00);
46 bytes[0] |= (buttons_state & 0x02 ? 0x10 : 0x00);
47 bytes[3] |= (buttons_state & 0x04 ? 0x20 : 0x00);
48
49 /* We always send the packet of, so that we do not have to keep track
50 of previous state of the middle button. This can potentially confuse
51 some very old drivers for two button mice though. */
Anthony Liguorifa5efcc2011-08-15 11:17:30 -050052 qemu_chr_be_write(chr, bytes, 4);
aurel32aa71cf82009-02-08 15:53:20 +000053}
54
55static int msmouse_chr_write (struct CharDriverState *s, const uint8_t *buf, int len)
56{
57 /* Ignore writes to mouse port */
58 return len;
59}
60
61static void msmouse_chr_close (struct CharDriverState *chr)
62{
Anthony Liguori7267c092011-08-20 22:09:37 -050063 g_free (chr);
aurel32aa71cf82009-02-08 15:53:20 +000064}
65
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +010066CharDriverState *qemu_chr_open_msmouse(void)
aurel32aa71cf82009-02-08 15:53:20 +000067{
68 CharDriverState *chr;
69
Anthony Liguori7267c092011-08-20 22:09:37 -050070 chr = g_malloc0(sizeof(CharDriverState));
aurel32aa71cf82009-02-08 15:53:20 +000071 chr->chr_write = msmouse_chr_write;
72 chr->chr_close = msmouse_chr_close;
Michael Rothbd5c51e2013-06-07 15:19:53 -050073 chr->explicit_be_open = true;
aurel32aa71cf82009-02-08 15:53:20 +000074
75 qemu_add_mouse_event_handler(msmouse_event, chr, 0, "QEMU Microsoft Mouse");
76
Markus Armbruster1f514702012-02-07 15:09:08 +010077 return chr;
aurel32aa71cf82009-02-08 15:53:20 +000078}
Anthony Liguori5ab82112013-03-05 23:21:31 +053079
80static void register_types(void)
81{
Gerd Hoffmannf5a51ca2013-02-21 11:58:44 +010082 register_char_driver_qapi("msmouse", CHARDEV_BACKEND_KIND_MSMOUSE, NULL);
Anthony Liguori5ab82112013-03-05 23:21:31 +053083}
84
85type_init(register_types);