xref: /openbmc/qemu/ui/spice-input.c (revision 28ecbaeecb139a214f019207402a35d7b58aec0f)
1864401c2SGerd Hoffmann /*
2864401c2SGerd Hoffmann  * Copyright (C) 2010 Red Hat, Inc.
3864401c2SGerd Hoffmann  *
4864401c2SGerd Hoffmann  * This program is free software; you can redistribute it and/or
5864401c2SGerd Hoffmann  * modify it under the terms of the GNU General Public License as
6864401c2SGerd Hoffmann  * published by the Free Software Foundation; either version 2 or
7864401c2SGerd Hoffmann  * (at your option) version 3 of the License.
8864401c2SGerd Hoffmann  *
9864401c2SGerd Hoffmann  * This program is distributed in the hope that it will be useful,
10864401c2SGerd Hoffmann  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11864401c2SGerd Hoffmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12864401c2SGerd Hoffmann  * GNU General Public License for more details.
13864401c2SGerd Hoffmann  *
14864401c2SGerd Hoffmann  * You should have received a copy of the GNU General Public License
15864401c2SGerd Hoffmann  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16864401c2SGerd Hoffmann  */
17864401c2SGerd Hoffmann 
18864401c2SGerd Hoffmann #include <stdlib.h>
19864401c2SGerd Hoffmann #include <stdio.h>
20869564a9SGerd Hoffmann #include <stdbool.h>
21864401c2SGerd Hoffmann #include <string.h>
22864401c2SGerd Hoffmann 
23864401c2SGerd Hoffmann #include <spice.h>
24864401c2SGerd Hoffmann #include <spice/enums.h>
25864401c2SGerd Hoffmann 
26864401c2SGerd Hoffmann #include "qemu-common.h"
27*28ecbaeeSPaolo Bonzini #include "ui/qemu-spice.h"
28*28ecbaeeSPaolo Bonzini #include "ui/console.h"
29864401c2SGerd Hoffmann 
30864401c2SGerd Hoffmann /* keyboard bits */
31864401c2SGerd Hoffmann 
32864401c2SGerd Hoffmann typedef struct QemuSpiceKbd {
33864401c2SGerd Hoffmann     SpiceKbdInstance sin;
34864401c2SGerd Hoffmann     int ledstate;
35864401c2SGerd Hoffmann } QemuSpiceKbd;
36864401c2SGerd Hoffmann 
37864401c2SGerd Hoffmann static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag);
38864401c2SGerd Hoffmann static uint8_t kbd_get_leds(SpiceKbdInstance *sin);
39864401c2SGerd Hoffmann static void kbd_leds(void *opaque, int l);
40864401c2SGerd Hoffmann 
41864401c2SGerd Hoffmann static const SpiceKbdInterface kbd_interface = {
42864401c2SGerd Hoffmann     .base.type          = SPICE_INTERFACE_KEYBOARD,
43864401c2SGerd Hoffmann     .base.description   = "qemu keyboard",
44864401c2SGerd Hoffmann     .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
45864401c2SGerd Hoffmann     .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
46864401c2SGerd Hoffmann     .push_scan_freg     = kbd_push_key,
47864401c2SGerd Hoffmann     .get_leds           = kbd_get_leds,
48864401c2SGerd Hoffmann };
49864401c2SGerd Hoffmann 
50864401c2SGerd Hoffmann static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag)
51864401c2SGerd Hoffmann {
52864401c2SGerd Hoffmann     kbd_put_keycode(frag);
53864401c2SGerd Hoffmann }
54864401c2SGerd Hoffmann 
55864401c2SGerd Hoffmann static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
56864401c2SGerd Hoffmann {
57864401c2SGerd Hoffmann     QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
58864401c2SGerd Hoffmann     return kbd->ledstate;
59864401c2SGerd Hoffmann }
60864401c2SGerd Hoffmann 
61864401c2SGerd Hoffmann static void kbd_leds(void *opaque, int ledstate)
62864401c2SGerd Hoffmann {
63864401c2SGerd Hoffmann     QemuSpiceKbd *kbd = opaque;
64864401c2SGerd Hoffmann 
65864401c2SGerd Hoffmann     kbd->ledstate = 0;
66864401c2SGerd Hoffmann     if (ledstate & QEMU_SCROLL_LOCK_LED) {
67864401c2SGerd Hoffmann         kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK;
68864401c2SGerd Hoffmann     }
69864401c2SGerd Hoffmann     if (ledstate & QEMU_NUM_LOCK_LED) {
70864401c2SGerd Hoffmann         kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK;
71864401c2SGerd Hoffmann     }
72864401c2SGerd Hoffmann     if (ledstate & QEMU_CAPS_LOCK_LED) {
73864401c2SGerd Hoffmann         kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK;
74864401c2SGerd Hoffmann     }
75864401c2SGerd Hoffmann     spice_server_kbd_leds(&kbd->sin, ledstate);
76864401c2SGerd Hoffmann }
77864401c2SGerd Hoffmann 
7878dd9ac1SGerd Hoffmann /* mouse bits */
7978dd9ac1SGerd Hoffmann 
80869564a9SGerd Hoffmann typedef struct QemuSpicePointer {
81869564a9SGerd Hoffmann     SpiceMouseInstance  mouse;
82869564a9SGerd Hoffmann     SpiceTabletInstance tablet;
83869564a9SGerd Hoffmann     int width, height, x, y;
84869564a9SGerd Hoffmann     Notifier mouse_mode;
85869564a9SGerd Hoffmann     bool absolute;
86869564a9SGerd Hoffmann } QemuSpicePointer;
8778dd9ac1SGerd Hoffmann 
8878dd9ac1SGerd Hoffmann static int map_buttons(int spice_buttons)
8978dd9ac1SGerd Hoffmann {
9078dd9ac1SGerd Hoffmann     int qemu_buttons = 0;
9178dd9ac1SGerd Hoffmann 
9278dd9ac1SGerd Hoffmann     /*
9378dd9ac1SGerd Hoffmann      * Note: SPICE_MOUSE_BUTTON_* specifies the wire protocol but this
9478dd9ac1SGerd Hoffmann      * isn't what we get passed in via interface callbacks for the
9578dd9ac1SGerd Hoffmann      * middle and right button ...
9678dd9ac1SGerd Hoffmann      */
9778dd9ac1SGerd Hoffmann     if (spice_buttons & SPICE_MOUSE_BUTTON_MASK_LEFT) {
9878dd9ac1SGerd Hoffmann         qemu_buttons |= MOUSE_EVENT_LBUTTON;
9978dd9ac1SGerd Hoffmann     }
10078dd9ac1SGerd Hoffmann     if (spice_buttons & 0x04 /* SPICE_MOUSE_BUTTON_MASK_MIDDLE */) {
10178dd9ac1SGerd Hoffmann         qemu_buttons |= MOUSE_EVENT_MBUTTON;
10278dd9ac1SGerd Hoffmann     }
10378dd9ac1SGerd Hoffmann     if (spice_buttons & 0x02 /* SPICE_MOUSE_BUTTON_MASK_RIGHT */) {
10478dd9ac1SGerd Hoffmann         qemu_buttons |= MOUSE_EVENT_RBUTTON;
10578dd9ac1SGerd Hoffmann     }
10678dd9ac1SGerd Hoffmann     return qemu_buttons;
10778dd9ac1SGerd Hoffmann }
10878dd9ac1SGerd Hoffmann 
10978dd9ac1SGerd Hoffmann static void mouse_motion(SpiceMouseInstance *sin, int dx, int dy, int dz,
11078dd9ac1SGerd Hoffmann                          uint32_t buttons_state)
11178dd9ac1SGerd Hoffmann {
11278dd9ac1SGerd Hoffmann     kbd_mouse_event(dx, dy, dz, map_buttons(buttons_state));
11378dd9ac1SGerd Hoffmann }
11478dd9ac1SGerd Hoffmann 
11578dd9ac1SGerd Hoffmann static void mouse_buttons(SpiceMouseInstance *sin, uint32_t buttons_state)
11678dd9ac1SGerd Hoffmann {
11778dd9ac1SGerd Hoffmann     kbd_mouse_event(0, 0, 0, map_buttons(buttons_state));
11878dd9ac1SGerd Hoffmann }
11978dd9ac1SGerd Hoffmann 
12078dd9ac1SGerd Hoffmann static const SpiceMouseInterface mouse_interface = {
12178dd9ac1SGerd Hoffmann     .base.type          = SPICE_INTERFACE_MOUSE,
12278dd9ac1SGerd Hoffmann     .base.description   = "mouse",
12378dd9ac1SGerd Hoffmann     .base.major_version = SPICE_INTERFACE_MOUSE_MAJOR,
12478dd9ac1SGerd Hoffmann     .base.minor_version = SPICE_INTERFACE_MOUSE_MINOR,
12578dd9ac1SGerd Hoffmann     .motion             = mouse_motion,
12678dd9ac1SGerd Hoffmann     .buttons            = mouse_buttons,
12778dd9ac1SGerd Hoffmann };
12878dd9ac1SGerd Hoffmann 
129869564a9SGerd Hoffmann static void tablet_set_logical_size(SpiceTabletInstance* sin, int width, int height)
130869564a9SGerd Hoffmann {
131869564a9SGerd Hoffmann     QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
132869564a9SGerd Hoffmann 
133869564a9SGerd Hoffmann     if (height < 16) {
134869564a9SGerd Hoffmann         height = 16;
135869564a9SGerd Hoffmann     }
136869564a9SGerd Hoffmann     if (width < 16) {
137869564a9SGerd Hoffmann         width = 16;
138869564a9SGerd Hoffmann     }
139869564a9SGerd Hoffmann     pointer->width  = width;
140869564a9SGerd Hoffmann     pointer->height = height;
141869564a9SGerd Hoffmann }
142869564a9SGerd Hoffmann 
143869564a9SGerd Hoffmann static void tablet_position(SpiceTabletInstance* sin, int x, int y,
144869564a9SGerd Hoffmann                             uint32_t buttons_state)
145869564a9SGerd Hoffmann {
146869564a9SGerd Hoffmann     QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
147869564a9SGerd Hoffmann 
148869564a9SGerd Hoffmann     pointer->x = x * 0x7FFF / (pointer->width - 1);
149869564a9SGerd Hoffmann     pointer->y = y * 0x7FFF / (pointer->height - 1);
150869564a9SGerd Hoffmann     kbd_mouse_event(pointer->x, pointer->y, 0, map_buttons(buttons_state));
151869564a9SGerd Hoffmann }
152869564a9SGerd Hoffmann 
153869564a9SGerd Hoffmann 
154869564a9SGerd Hoffmann static void tablet_wheel(SpiceTabletInstance* sin, int wheel,
155869564a9SGerd Hoffmann                          uint32_t buttons_state)
156869564a9SGerd Hoffmann {
157869564a9SGerd Hoffmann     QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
158869564a9SGerd Hoffmann 
159869564a9SGerd Hoffmann     kbd_mouse_event(pointer->x, pointer->y, wheel, map_buttons(buttons_state));
160869564a9SGerd Hoffmann }
161869564a9SGerd Hoffmann 
162869564a9SGerd Hoffmann static void tablet_buttons(SpiceTabletInstance *sin,
163869564a9SGerd Hoffmann                            uint32_t buttons_state)
164869564a9SGerd Hoffmann {
165869564a9SGerd Hoffmann     QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
166869564a9SGerd Hoffmann 
167869564a9SGerd Hoffmann     kbd_mouse_event(pointer->x, pointer->y, 0, map_buttons(buttons_state));
168869564a9SGerd Hoffmann }
169869564a9SGerd Hoffmann 
170869564a9SGerd Hoffmann static const SpiceTabletInterface tablet_interface = {
171869564a9SGerd Hoffmann     .base.type          = SPICE_INTERFACE_TABLET,
172869564a9SGerd Hoffmann     .base.description   = "tablet",
173869564a9SGerd Hoffmann     .base.major_version = SPICE_INTERFACE_TABLET_MAJOR,
174869564a9SGerd Hoffmann     .base.minor_version = SPICE_INTERFACE_TABLET_MINOR,
175869564a9SGerd Hoffmann     .set_logical_size   = tablet_set_logical_size,
176869564a9SGerd Hoffmann     .position           = tablet_position,
177869564a9SGerd Hoffmann     .wheel              = tablet_wheel,
178869564a9SGerd Hoffmann     .buttons            = tablet_buttons,
179869564a9SGerd Hoffmann };
180869564a9SGerd Hoffmann 
1819e8dd451SJan Kiszka static void mouse_mode_notifier(Notifier *notifier, void *data)
182869564a9SGerd Hoffmann {
183869564a9SGerd Hoffmann     QemuSpicePointer *pointer = container_of(notifier, QemuSpicePointer, mouse_mode);
184869564a9SGerd Hoffmann     bool is_absolute  = kbd_mouse_is_absolute();
185869564a9SGerd Hoffmann 
186869564a9SGerd Hoffmann     if (pointer->absolute == is_absolute) {
187869564a9SGerd Hoffmann         return;
188869564a9SGerd Hoffmann     }
189869564a9SGerd Hoffmann 
190869564a9SGerd Hoffmann     if (is_absolute) {
191869564a9SGerd Hoffmann         qemu_spice_add_interface(&pointer->tablet.base);
192869564a9SGerd Hoffmann     } else {
193869564a9SGerd Hoffmann         spice_server_remove_interface(&pointer->tablet.base);
194869564a9SGerd Hoffmann     }
195869564a9SGerd Hoffmann     pointer->absolute = is_absolute;
196869564a9SGerd Hoffmann }
197869564a9SGerd Hoffmann 
198864401c2SGerd Hoffmann void qemu_spice_input_init(void)
199864401c2SGerd Hoffmann {
200864401c2SGerd Hoffmann     QemuSpiceKbd *kbd;
201869564a9SGerd Hoffmann     QemuSpicePointer *pointer;
202864401c2SGerd Hoffmann 
2037267c094SAnthony Liguori     kbd = g_malloc0(sizeof(*kbd));
204864401c2SGerd Hoffmann     kbd->sin.base.sif = &kbd_interface.base;
205864401c2SGerd Hoffmann     qemu_spice_add_interface(&kbd->sin.base);
206864401c2SGerd Hoffmann     qemu_add_led_event_handler(kbd_leds, kbd);
20778dd9ac1SGerd Hoffmann 
2087267c094SAnthony Liguori     pointer = g_malloc0(sizeof(*pointer));
209869564a9SGerd Hoffmann     pointer->mouse.base.sif  = &mouse_interface.base;
210869564a9SGerd Hoffmann     pointer->tablet.base.sif = &tablet_interface.base;
211869564a9SGerd Hoffmann     qemu_spice_add_interface(&pointer->mouse.base);
212869564a9SGerd Hoffmann 
213869564a9SGerd Hoffmann     pointer->absolute = false;
214869564a9SGerd Hoffmann     pointer->mouse_mode.notify = mouse_mode_notifier;
215869564a9SGerd Hoffmann     qemu_add_mouse_mode_change_notifier(&pointer->mouse_mode);
2169e8dd451SJan Kiszka     mouse_mode_notifier(&pointer->mouse_mode, NULL);
217864401c2SGerd Hoffmann }
218