master
c 282 lines 8.56 KB
Raw
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 "qemu/osdep.h"
19
20 #include <spice.h>
21 #include <spice/enums.h>
22
23 #include "standard-headers/linux/input-event-codes.h"
24 #include "ui/qemu-spice.h"
25 #include "ui/console.h"
26 #include "keymaps.h"
27 #include "ui/input.h"
28
29 /* keyboard bits */
30
31 typedef struct QemuSpiceKbd {
32 SpiceKbdInstance sin;
33 int ledstate;
34 bool emul0;
35 size_t pauseseq;
36 Notifier led_notifier;
37 } QemuSpiceKbd;
38
39 static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag);
40 static uint8_t kbd_get_leds(SpiceKbdInstance *sin);
41
42 static const SpiceKbdInterface kbd_interface = {
43 .base.type = SPICE_INTERFACE_KEYBOARD,
44 .base.description = "qemu keyboard",
45 .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
46 .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
47 .push_scan_freg = kbd_push_key,
48 .get_leds = kbd_get_leds,
49 };
50
51 static void kbd_push_key(SpiceKbdInstance *sin, uint8_t scancode)
52 {
53 static const uint8_t pauseseq[] = { 0xe1, 0x1d, 0x45, 0xe1, 0x9d, 0xc5 };
54 QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
55 int keycode;
56 bool up;
57
58 if (scancode == SCANCODE_EMUL0) {
59 kbd->emul0 = true;
60 return;
61 }
62
63 if (scancode == pauseseq[kbd->pauseseq]) {
64 kbd->pauseseq++;
65 if (kbd->pauseseq == G_N_ELEMENTS(pauseseq)) {
66 qemu_input_event_send_key_linux(NULL, KEY_PAUSE, true);
67 kbd->pauseseq = 0;
68 }
69 return;
70 } else {
71 kbd->pauseseq = 0;
72 }
73
74 keycode = scancode & ~SCANCODE_UP;
75 up = scancode & SCANCODE_UP;
76 if (kbd->emul0) {
77 kbd->emul0 = false;
78 keycode |= SCANCODE_GREY;
79 }
80
81 qemu_input_event_send_key_number(NULL, keycode, !up);
82 }
83
84 static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
85 {
86 QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
87 return kbd->ledstate;
88 }
89
90 static void kbd_leds(Notifier *notifier, void *data)
91 {
92 QemuSpiceKbd *kbd = container_of(notifier, QemuSpiceKbd, led_notifier);
93 /* spice has no associated console support */
94 uint32_t leds_mask = qemu_input_get_leds_mask(NULL);
95
96 kbd->ledstate = 0;
97 if (leds_mask & QEMU_SCROLL_LOCK_LED) {
98 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK;
99 }
100 if (leds_mask & QEMU_NUM_LOCK_LED) {
101 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK;
102 }
103 if (leds_mask & QEMU_CAPS_LOCK_LED) {
104 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK;
105 }
106 spice_server_kbd_leds(&kbd->sin, kbd->ledstate);
107 }
108
109 /* mouse bits */
110
111 typedef struct QemuSpicePointer {
112 SpiceMouseInstance mouse;
113 SpiceTabletInstance tablet;
114 int width, height;
115 uint32_t last_bmask;
116 Notifier mouse_mode;
117 bool absolute;
118 } QemuSpicePointer;
119
120 static void spice_update_buttons(QemuSpicePointer *pointer,
121 int wheel, uint32_t button_mask)
122 {
123 static uint32_t bmap[INPUT_BUTTON__MAX] = {
124 [INPUT_BUTTON_LEFT] = 0x01,
125 [INPUT_BUTTON_MIDDLE] = 0x04,
126 [INPUT_BUTTON_RIGHT] = 0x02,
127 [INPUT_BUTTON_WHEEL_UP] = 0x10,
128 [INPUT_BUTTON_WHEEL_DOWN] = 0x20,
129 [INPUT_BUTTON_SIDE] = 0x40,
130 [INPUT_BUTTON_EXTRA] = 0x80,
131 };
132
133 if (wheel < 0) {
134 button_mask |= 0x10;
135 }
136 if (wheel > 0) {
137 button_mask |= 0x20;
138 }
139
140 if (pointer->last_bmask == button_mask) {
141 return;
142 }
143 qemu_input_update_buttons(NULL, bmap, pointer->last_bmask, button_mask);
144 pointer->last_bmask = button_mask;
145 }
146
147 static void mouse_motion(SpiceMouseInstance *sin, int dx, int dy, int dz,
148 uint32_t buttons_state)
149 {
150 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, mouse);
151 spice_update_buttons(pointer, dz, buttons_state);
152 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
153 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
154 qemu_input_event_sync();
155 }
156
157 static void mouse_buttons(SpiceMouseInstance *sin, uint32_t buttons_state)
158 {
159 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, mouse);
160 spice_update_buttons(pointer, 0, buttons_state);
161 qemu_input_event_sync();
162 }
163
164 static const SpiceMouseInterface mouse_interface = {
165 .base.type = SPICE_INTERFACE_MOUSE,
166 .base.description = "mouse",
167 .base.major_version = SPICE_INTERFACE_MOUSE_MAJOR,
168 .base.minor_version = SPICE_INTERFACE_MOUSE_MINOR,
169 .motion = mouse_motion,
170 .buttons = mouse_buttons,
171 };
172
173 static void tablet_set_logical_size(SpiceTabletInstance* sin, int width, int height)
174 {
175 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
176
177 if (height < 16) {
178 height = 16;
179 }
180 if (width < 16) {
181 width = 16;
182 }
183 pointer->width = width;
184 pointer->height = height;
185 }
186
187 static void tablet_position(SpiceTabletInstance* sin, int x, int y,
188 uint32_t buttons_state)
189 {
190 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
191
192 spice_update_buttons(pointer, 0, buttons_state);
193 qemu_input_queue_abs(NULL, INPUT_AXIS_X, x, 0, pointer->width);
194 qemu_input_queue_abs(NULL, INPUT_AXIS_Y, y, 0, pointer->height);
195 qemu_input_event_sync();
196 }
197
198
199 static void tablet_wheel(SpiceTabletInstance* sin, int wheel,
200 uint32_t buttons_state)
201 {
202 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
203
204 spice_update_buttons(pointer, wheel, buttons_state);
205 qemu_input_event_sync();
206 }
207
208 static void tablet_buttons(SpiceTabletInstance *sin,
209 uint32_t buttons_state)
210 {
211 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
212
213 spice_update_buttons(pointer, 0, buttons_state);
214 qemu_input_event_sync();
215 }
216
217 static const SpiceTabletInterface tablet_interface = {
218 .base.type = SPICE_INTERFACE_TABLET,
219 .base.description = "tablet",
220 .base.major_version = SPICE_INTERFACE_TABLET_MAJOR,
221 .base.minor_version = SPICE_INTERFACE_TABLET_MINOR,
222 .set_logical_size = tablet_set_logical_size,
223 .position = tablet_position,
224 .wheel = tablet_wheel,
225 .buttons = tablet_buttons,
226 };
227
228 static void mouse_mode_notifier(Notifier *notifier, void *data)
229 {
230 QemuSpicePointer *pointer = container_of(notifier, QemuSpicePointer, mouse_mode);
231 bool is_absolute = qemu_input_is_absolute(NULL);
232
233 if (pointer->absolute == is_absolute) {
234 return;
235 }
236
237 if (is_absolute) {
238 qemu_spice.add_interface(&pointer->tablet.base);
239 } else {
240 spice_server_remove_interface(&pointer->tablet.base);
241 }
242 pointer->absolute = is_absolute;
243 }
244
245 static QemuSpiceKbd *spice_kbd;
246 static QemuSpicePointer *spice_pointer;
247
248 void qemu_spice_input_init(void)
249 {
250 spice_kbd = g_new0(QemuSpiceKbd, 1);
251 spice_kbd->sin.base.sif = &kbd_interface.base;
252 qemu_spice.add_interface(&spice_kbd->sin.base);
253 spice_kbd->led_notifier.notify = kbd_leds;
254 qemu_input_led_notifier_add(&spice_kbd->led_notifier);
255
256 spice_pointer = g_new0(QemuSpicePointer, 1);
257 spice_pointer->mouse.base.sif = &mouse_interface.base;
258 spice_pointer->tablet.base.sif = &tablet_interface.base;
259 qemu_spice.add_interface(&spice_pointer->mouse.base);
260
261 spice_pointer->absolute = false;
262 spice_pointer->mouse_mode.notify = mouse_mode_notifier;
263 qemu_add_mouse_mode_change_notifier(&spice_pointer->mouse_mode);
264 mouse_mode_notifier(&spice_pointer->mouse_mode, NULL);
265 }
266
267 void qemu_spice_input_cleanup(void)
268 {
269 if (spice_pointer) {
270 qemu_remove_mouse_mode_change_notifier(&spice_pointer->mouse_mode);
271 if (spice_pointer->absolute) {
272 spice_server_remove_interface(&spice_pointer->tablet.base);
273 }
274 spice_server_remove_interface(&spice_pointer->mouse.base);
275 g_clear_pointer(&spice_pointer, g_free);
276 }
277 if (spice_kbd) {
278 qemu_input_led_notifier_remove(&spice_kbd->led_notifier);
279 spice_server_remove_interface(&spice_kbd->sin.base);
280 g_clear_pointer(&spice_kbd, g_free);
281 }
282 }