blob: 21990fa9962b20314b7ce244be9737484a6be18d [file] [log] [blame]
Gerd Hoffmann864401c2010-03-11 11:13:28 -03001/*
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
Peter Maydelle16f4c82016-01-29 17:49:51 +000018#include "qemu/osdep.h"
Gerd Hoffmann864401c2010-03-11 11:13:28 -030019
20#include <spice.h>
21#include <spice/enums.h>
22
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010023#include "ui/qemu-spice.h"
24#include "ui/console.h"
Michael S. Tsirkin0041e9a2018-05-03 22:51:00 +030025#include "keymaps.h"
Gerd Hoffmannde8f5802013-12-04 12:23:54 +010026#include "ui/input.h"
Gerd Hoffmann864401c2010-03-11 11:13:28 -030027
28/* keyboard bits */
29
30typedef struct QemuSpiceKbd {
31 SpiceKbdInstance sin;
32 int ledstate;
Gerd Hoffmannde8f5802013-12-04 12:23:54 +010033 bool emul0;
Daniel P. Berrange7c388db2017-07-27 12:32:43 +010034 size_t pauseseq;
Gerd Hoffmann864401c2010-03-11 11:13:28 -030035} QemuSpiceKbd;
36
37static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag);
38static uint8_t kbd_get_leds(SpiceKbdInstance *sin);
Gerd Hoffmann864401c2010-03-11 11:13:28 -030039
40static const SpiceKbdInterface kbd_interface = {
41 .base.type = SPICE_INTERFACE_KEYBOARD,
42 .base.description = "qemu keyboard",
43 .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
44 .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
45 .push_scan_freg = kbd_push_key,
46 .get_leds = kbd_get_leds,
47};
48
Gerd Hoffmannde8f5802013-12-04 12:23:54 +010049static void kbd_push_key(SpiceKbdInstance *sin, uint8_t scancode)
Gerd Hoffmann864401c2010-03-11 11:13:28 -030050{
Daniel P. Berrangee92316a2017-07-27 18:46:40 +010051 static const uint8_t pauseseq[] = { 0xe1, 0x1d, 0x45, 0xe1, 0x9d, 0xc5 };
Gerd Hoffmannde8f5802013-12-04 12:23:54 +010052 QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
53 int keycode;
54 bool up;
55
56 if (scancode == SCANCODE_EMUL0) {
57 kbd->emul0 = true;
58 return;
59 }
Daniel P. Berrangee92316a2017-07-27 18:46:40 +010060
61 if (scancode == pauseseq[kbd->pauseseq]) {
62 kbd->pauseseq++;
63 if (kbd->pauseseq == G_N_ELEMENTS(pauseseq)) {
64 qemu_input_event_send_key_qcode(NULL, Q_KEY_CODE_PAUSE, true);
65 kbd->pauseseq = 0;
66 }
67 return;
68 } else {
69 kbd->pauseseq = 0;
70 }
71
Gerd Hoffmannde8f5802013-12-04 12:23:54 +010072 keycode = scancode & ~SCANCODE_UP;
73 up = scancode & SCANCODE_UP;
74 if (kbd->emul0) {
75 kbd->emul0 = false;
76 keycode |= SCANCODE_GREY;
77 }
78
79 qemu_input_event_send_key_number(NULL, keycode, !up);
Gerd Hoffmann864401c2010-03-11 11:13:28 -030080}
81
82static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
83{
84 QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
85 return kbd->ledstate;
86}
87
88static void kbd_leds(void *opaque, int ledstate)
89{
90 QemuSpiceKbd *kbd = opaque;
91
92 kbd->ledstate = 0;
93 if (ledstate & QEMU_SCROLL_LOCK_LED) {
94 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK;
95 }
96 if (ledstate & QEMU_NUM_LOCK_LED) {
97 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK;
98 }
99 if (ledstate & QEMU_CAPS_LOCK_LED) {
100 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK;
101 }
Jonathon Jongsmabfefa6d2017-05-10 15:20:06 -0500102 spice_server_kbd_leds(&kbd->sin, kbd->ledstate);
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300103}
104
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300105/* mouse bits */
106
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200107typedef struct QemuSpicePointer {
108 SpiceMouseInstance mouse;
109 SpiceTabletInstance tablet;
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100110 int width, height;
111 uint32_t last_bmask;
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200112 Notifier mouse_mode;
113 bool absolute;
114} QemuSpicePointer;
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300115
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100116static void spice_update_buttons(QemuSpicePointer *pointer,
117 int wheel, uint32_t button_mask)
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300118{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700119 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100120 [INPUT_BUTTON_LEFT] = 0x01,
121 [INPUT_BUTTON_MIDDLE] = 0x04,
122 [INPUT_BUTTON_RIGHT] = 0x02,
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100123 [INPUT_BUTTON_WHEEL_UP] = 0x10,
124 [INPUT_BUTTON_WHEEL_DOWN] = 0x20,
Frediano Ziglio5b57a872020-08-20 15:58:51 +0100125 [INPUT_BUTTON_SIDE] = 0x40,
126 [INPUT_BUTTON_EXTRA] = 0x80,
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100127 };
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300128
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100129 if (wheel < 0) {
130 button_mask |= 0x10;
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300131 }
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100132 if (wheel > 0) {
133 button_mask |= 0x20;
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300134 }
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100135
136 if (pointer->last_bmask == button_mask) {
137 return;
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300138 }
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100139 qemu_input_update_buttons(NULL, bmap, pointer->last_bmask, button_mask);
140 pointer->last_bmask = button_mask;
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300141}
142
143static void mouse_motion(SpiceMouseInstance *sin, int dx, int dy, int dz,
144 uint32_t buttons_state)
145{
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100146 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, mouse);
147 spice_update_buttons(pointer, dz, buttons_state);
148 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
149 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
150 qemu_input_event_sync();
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300151}
152
153static void mouse_buttons(SpiceMouseInstance *sin, uint32_t buttons_state)
154{
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100155 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, mouse);
156 spice_update_buttons(pointer, 0, buttons_state);
157 qemu_input_event_sync();
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300158}
159
160static const SpiceMouseInterface mouse_interface = {
161 .base.type = SPICE_INTERFACE_MOUSE,
162 .base.description = "mouse",
163 .base.major_version = SPICE_INTERFACE_MOUSE_MAJOR,
164 .base.minor_version = SPICE_INTERFACE_MOUSE_MINOR,
165 .motion = mouse_motion,
166 .buttons = mouse_buttons,
167};
168
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200169static void tablet_set_logical_size(SpiceTabletInstance* sin, int width, int height)
170{
171 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
172
173 if (height < 16) {
174 height = 16;
175 }
176 if (width < 16) {
177 width = 16;
178 }
179 pointer->width = width;
180 pointer->height = height;
181}
182
183static void tablet_position(SpiceTabletInstance* sin, int x, int y,
184 uint32_t buttons_state)
185{
186 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
187
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100188 spice_update_buttons(pointer, 0, buttons_state);
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200189 qemu_input_queue_abs(NULL, INPUT_AXIS_X, x, 0, pointer->width);
190 qemu_input_queue_abs(NULL, INPUT_AXIS_Y, y, 0, pointer->height);
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100191 qemu_input_event_sync();
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200192}
193
194
195static void tablet_wheel(SpiceTabletInstance* sin, int wheel,
196 uint32_t buttons_state)
197{
198 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
199
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100200 spice_update_buttons(pointer, wheel, buttons_state);
201 qemu_input_event_sync();
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200202}
203
204static void tablet_buttons(SpiceTabletInstance *sin,
205 uint32_t buttons_state)
206{
207 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
208
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100209 spice_update_buttons(pointer, 0, buttons_state);
210 qemu_input_event_sync();
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200211}
212
213static const SpiceTabletInterface tablet_interface = {
214 .base.type = SPICE_INTERFACE_TABLET,
215 .base.description = "tablet",
216 .base.major_version = SPICE_INTERFACE_TABLET_MAJOR,
217 .base.minor_version = SPICE_INTERFACE_TABLET_MINOR,
218 .set_logical_size = tablet_set_logical_size,
219 .position = tablet_position,
220 .wheel = tablet_wheel,
221 .buttons = tablet_buttons,
222};
223
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200224static void mouse_mode_notifier(Notifier *notifier, void *data)
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200225{
226 QemuSpicePointer *pointer = container_of(notifier, QemuSpicePointer, mouse_mode);
Gerd Hoffmannf100db32013-12-04 12:46:34 +0100227 bool is_absolute = qemu_input_is_absolute();
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200228
229 if (pointer->absolute == is_absolute) {
230 return;
231 }
232
233 if (is_absolute) {
234 qemu_spice_add_interface(&pointer->tablet.base);
235 } else {
236 spice_server_remove_interface(&pointer->tablet.base);
237 }
238 pointer->absolute = is_absolute;
239}
240
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300241void qemu_spice_input_init(void)
242{
243 QemuSpiceKbd *kbd;
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200244 QemuSpicePointer *pointer;
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300245
Anthony Liguori7267c092011-08-20 22:09:37 -0500246 kbd = g_malloc0(sizeof(*kbd));
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300247 kbd->sin.base.sif = &kbd_interface.base;
248 qemu_spice_add_interface(&kbd->sin.base);
249 qemu_add_led_event_handler(kbd_leds, kbd);
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300250
Anthony Liguori7267c092011-08-20 22:09:37 -0500251 pointer = g_malloc0(sizeof(*pointer));
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200252 pointer->mouse.base.sif = &mouse_interface.base;
253 pointer->tablet.base.sif = &tablet_interface.base;
254 qemu_spice_add_interface(&pointer->mouse.base);
255
256 pointer->absolute = false;
257 pointer->mouse_mode.notify = mouse_mode_notifier;
258 qemu_add_mouse_mode_change_notifier(&pointer->mouse_mode);
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200259 mouse_mode_notifier(&pointer->mouse_mode, NULL);
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300260}