xref: /openbmc/qemu/ui/spice-input.c (revision de8f580b2360706d644296c690bb187ece6dc4c1)
1 /*
2  * Copyright (C) 2010 Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 or
7  * (at your option) version 3 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include <string.h>
22 
23 #include <spice.h>
24 #include <spice/enums.h>
25 
26 #include "qemu-common.h"
27 #include "ui/qemu-spice.h"
28 #include "ui/console.h"
29 #include "ui/keymaps.h"
30 #include "ui/input.h"
31 
32 /* keyboard bits */
33 
34 typedef struct QemuSpiceKbd {
35     SpiceKbdInstance sin;
36     int ledstate;
37     bool emul0;
38 } QemuSpiceKbd;
39 
40 static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag);
41 static uint8_t kbd_get_leds(SpiceKbdInstance *sin);
42 static void kbd_leds(void *opaque, int l);
43 
44 static const SpiceKbdInterface kbd_interface = {
45     .base.type          = SPICE_INTERFACE_KEYBOARD,
46     .base.description   = "qemu keyboard",
47     .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
48     .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
49     .push_scan_freg     = kbd_push_key,
50     .get_leds           = kbd_get_leds,
51 };
52 
53 static void kbd_push_key(SpiceKbdInstance *sin, uint8_t scancode)
54 {
55     QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
56     int keycode;
57     bool up;
58 
59     if (scancode == SCANCODE_EMUL0) {
60         kbd->emul0 = true;
61         return;
62     }
63     keycode = scancode & ~SCANCODE_UP;
64     up = scancode & SCANCODE_UP;
65     if (kbd->emul0) {
66         kbd->emul0 = false;
67         keycode |= SCANCODE_GREY;
68     }
69 
70     qemu_input_event_send_key_number(NULL, keycode, !up);
71 }
72 
73 static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
74 {
75     QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
76     return kbd->ledstate;
77 }
78 
79 static void kbd_leds(void *opaque, int ledstate)
80 {
81     QemuSpiceKbd *kbd = opaque;
82 
83     kbd->ledstate = 0;
84     if (ledstate & QEMU_SCROLL_LOCK_LED) {
85         kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK;
86     }
87     if (ledstate & QEMU_NUM_LOCK_LED) {
88         kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK;
89     }
90     if (ledstate & QEMU_CAPS_LOCK_LED) {
91         kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK;
92     }
93     spice_server_kbd_leds(&kbd->sin, ledstate);
94 }
95 
96 /* mouse bits */
97 
98 typedef struct QemuSpicePointer {
99     SpiceMouseInstance  mouse;
100     SpiceTabletInstance tablet;
101     int width, height, x, y;
102     Notifier mouse_mode;
103     bool absolute;
104 } QemuSpicePointer;
105 
106 static int map_buttons(int spice_buttons)
107 {
108     int qemu_buttons = 0;
109 
110     /*
111      * Note: SPICE_MOUSE_BUTTON_* specifies the wire protocol but this
112      * isn't what we get passed in via interface callbacks for the
113      * middle and right button ...
114      */
115     if (spice_buttons & SPICE_MOUSE_BUTTON_MASK_LEFT) {
116         qemu_buttons |= MOUSE_EVENT_LBUTTON;
117     }
118     if (spice_buttons & 0x04 /* SPICE_MOUSE_BUTTON_MASK_MIDDLE */) {
119         qemu_buttons |= MOUSE_EVENT_MBUTTON;
120     }
121     if (spice_buttons & 0x02 /* SPICE_MOUSE_BUTTON_MASK_RIGHT */) {
122         qemu_buttons |= MOUSE_EVENT_RBUTTON;
123     }
124     return qemu_buttons;
125 }
126 
127 static void mouse_motion(SpiceMouseInstance *sin, int dx, int dy, int dz,
128                          uint32_t buttons_state)
129 {
130     kbd_mouse_event(dx, dy, dz, map_buttons(buttons_state));
131 }
132 
133 static void mouse_buttons(SpiceMouseInstance *sin, uint32_t buttons_state)
134 {
135     kbd_mouse_event(0, 0, 0, map_buttons(buttons_state));
136 }
137 
138 static const SpiceMouseInterface mouse_interface = {
139     .base.type          = SPICE_INTERFACE_MOUSE,
140     .base.description   = "mouse",
141     .base.major_version = SPICE_INTERFACE_MOUSE_MAJOR,
142     .base.minor_version = SPICE_INTERFACE_MOUSE_MINOR,
143     .motion             = mouse_motion,
144     .buttons            = mouse_buttons,
145 };
146 
147 static void tablet_set_logical_size(SpiceTabletInstance* sin, int width, int height)
148 {
149     QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
150 
151     if (height < 16) {
152         height = 16;
153     }
154     if (width < 16) {
155         width = 16;
156     }
157     pointer->width  = width;
158     pointer->height = height;
159 }
160 
161 static void tablet_position(SpiceTabletInstance* sin, int x, int y,
162                             uint32_t buttons_state)
163 {
164     QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
165 
166     pointer->x = x * 0x7FFF / (pointer->width - 1);
167     pointer->y = y * 0x7FFF / (pointer->height - 1);
168     kbd_mouse_event(pointer->x, pointer->y, 0, map_buttons(buttons_state));
169 }
170 
171 
172 static void tablet_wheel(SpiceTabletInstance* sin, int wheel,
173                          uint32_t buttons_state)
174 {
175     QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
176 
177     kbd_mouse_event(pointer->x, pointer->y, wheel, map_buttons(buttons_state));
178 }
179 
180 static void tablet_buttons(SpiceTabletInstance *sin,
181                            uint32_t buttons_state)
182 {
183     QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
184 
185     kbd_mouse_event(pointer->x, pointer->y, 0, map_buttons(buttons_state));
186 }
187 
188 static const SpiceTabletInterface tablet_interface = {
189     .base.type          = SPICE_INTERFACE_TABLET,
190     .base.description   = "tablet",
191     .base.major_version = SPICE_INTERFACE_TABLET_MAJOR,
192     .base.minor_version = SPICE_INTERFACE_TABLET_MINOR,
193     .set_logical_size   = tablet_set_logical_size,
194     .position           = tablet_position,
195     .wheel              = tablet_wheel,
196     .buttons            = tablet_buttons,
197 };
198 
199 static void mouse_mode_notifier(Notifier *notifier, void *data)
200 {
201     QemuSpicePointer *pointer = container_of(notifier, QemuSpicePointer, mouse_mode);
202     bool is_absolute  = kbd_mouse_is_absolute();
203 
204     if (pointer->absolute == is_absolute) {
205         return;
206     }
207 
208     if (is_absolute) {
209         qemu_spice_add_interface(&pointer->tablet.base);
210     } else {
211         spice_server_remove_interface(&pointer->tablet.base);
212     }
213     pointer->absolute = is_absolute;
214 }
215 
216 void qemu_spice_input_init(void)
217 {
218     QemuSpiceKbd *kbd;
219     QemuSpicePointer *pointer;
220 
221     kbd = g_malloc0(sizeof(*kbd));
222     kbd->sin.base.sif = &kbd_interface.base;
223     qemu_spice_add_interface(&kbd->sin.base);
224     qemu_add_led_event_handler(kbd_leds, kbd);
225 
226     pointer = g_malloc0(sizeof(*pointer));
227     pointer->mouse.base.sif  = &mouse_interface.base;
228     pointer->tablet.base.sif = &tablet_interface.base;
229     qemu_spice_add_interface(&pointer->mouse.base);
230 
231     pointer->absolute = false;
232     pointer->mouse_mode.notify = mouse_mode_notifier;
233     qemu_add_mouse_mode_change_notifier(&pointer->mouse_mode);
234     mouse_mode_notifier(&pointer->mouse_mode, NULL);
235 }
236