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
18e16f4c87SPeter Maydell #include "qemu/osdep.h"
19864401c2SGerd Hoffmann
20864401c2SGerd Hoffmann #include <spice.h>
21864401c2SGerd Hoffmann #include <spice/enums.h>
22864401c2SGerd Hoffmann
2328ecbaeeSPaolo Bonzini #include "ui/qemu-spice.h"
2428ecbaeeSPaolo Bonzini #include "ui/console.h"
250041e9a0SMichael S. Tsirkin #include "keymaps.h"
26de8f580bSGerd Hoffmann #include "ui/input.h"
27864401c2SGerd Hoffmann
28864401c2SGerd Hoffmann /* keyboard bits */
29864401c2SGerd Hoffmann
30864401c2SGerd Hoffmann typedef struct QemuSpiceKbd {
31864401c2SGerd Hoffmann SpiceKbdInstance sin;
32864401c2SGerd Hoffmann int ledstate;
33de8f580bSGerd Hoffmann bool emul0;
347c388dbdSDaniel P. Berrange size_t pauseseq;
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
40864401c2SGerd Hoffmann static const SpiceKbdInterface kbd_interface = {
41864401c2SGerd Hoffmann .base.type = SPICE_INTERFACE_KEYBOARD,
42864401c2SGerd Hoffmann .base.description = "qemu keyboard",
43864401c2SGerd Hoffmann .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
44864401c2SGerd Hoffmann .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
45864401c2SGerd Hoffmann .push_scan_freg = kbd_push_key,
46864401c2SGerd Hoffmann .get_leds = kbd_get_leds,
47864401c2SGerd Hoffmann };
48864401c2SGerd Hoffmann
kbd_push_key(SpiceKbdInstance * sin,uint8_t scancode)49de8f580bSGerd Hoffmann static void kbd_push_key(SpiceKbdInstance *sin, uint8_t scancode)
50864401c2SGerd Hoffmann {
51e92316adSDaniel P. Berrange static const uint8_t pauseseq[] = { 0xe1, 0x1d, 0x45, 0xe1, 0x9d, 0xc5 };
52de8f580bSGerd Hoffmann QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
53de8f580bSGerd Hoffmann int keycode;
54de8f580bSGerd Hoffmann bool up;
55de8f580bSGerd Hoffmann
56de8f580bSGerd Hoffmann if (scancode == SCANCODE_EMUL0) {
57de8f580bSGerd Hoffmann kbd->emul0 = true;
58de8f580bSGerd Hoffmann return;
59de8f580bSGerd Hoffmann }
60e92316adSDaniel P. Berrange
61e92316adSDaniel P. Berrange if (scancode == pauseseq[kbd->pauseseq]) {
62e92316adSDaniel P. Berrange kbd->pauseseq++;
63e92316adSDaniel P. Berrange if (kbd->pauseseq == G_N_ELEMENTS(pauseseq)) {
64e92316adSDaniel P. Berrange qemu_input_event_send_key_qcode(NULL, Q_KEY_CODE_PAUSE, true);
65e92316adSDaniel P. Berrange kbd->pauseseq = 0;
66e92316adSDaniel P. Berrange }
67e92316adSDaniel P. Berrange return;
68e92316adSDaniel P. Berrange } else {
69e92316adSDaniel P. Berrange kbd->pauseseq = 0;
70e92316adSDaniel P. Berrange }
71e92316adSDaniel P. Berrange
72de8f580bSGerd Hoffmann keycode = scancode & ~SCANCODE_UP;
73de8f580bSGerd Hoffmann up = scancode & SCANCODE_UP;
74de8f580bSGerd Hoffmann if (kbd->emul0) {
75de8f580bSGerd Hoffmann kbd->emul0 = false;
76de8f580bSGerd Hoffmann keycode |= SCANCODE_GREY;
77de8f580bSGerd Hoffmann }
78de8f580bSGerd Hoffmann
79de8f580bSGerd Hoffmann qemu_input_event_send_key_number(NULL, keycode, !up);
80864401c2SGerd Hoffmann }
81864401c2SGerd Hoffmann
kbd_get_leds(SpiceKbdInstance * sin)82864401c2SGerd Hoffmann static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
83864401c2SGerd Hoffmann {
84864401c2SGerd Hoffmann QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
85864401c2SGerd Hoffmann return kbd->ledstate;
86864401c2SGerd Hoffmann }
87864401c2SGerd Hoffmann
kbd_leds(void * opaque,int ledstate)88864401c2SGerd Hoffmann static void kbd_leds(void *opaque, int ledstate)
89864401c2SGerd Hoffmann {
90864401c2SGerd Hoffmann QemuSpiceKbd *kbd = opaque;
91864401c2SGerd Hoffmann
92864401c2SGerd Hoffmann kbd->ledstate = 0;
93864401c2SGerd Hoffmann if (ledstate & QEMU_SCROLL_LOCK_LED) {
94864401c2SGerd Hoffmann kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK;
95864401c2SGerd Hoffmann }
96864401c2SGerd Hoffmann if (ledstate & QEMU_NUM_LOCK_LED) {
97864401c2SGerd Hoffmann kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK;
98864401c2SGerd Hoffmann }
99864401c2SGerd Hoffmann if (ledstate & QEMU_CAPS_LOCK_LED) {
100864401c2SGerd Hoffmann kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK;
101864401c2SGerd Hoffmann }
102bfefa6d7SJonathon Jongsma spice_server_kbd_leds(&kbd->sin, kbd->ledstate);
103864401c2SGerd Hoffmann }
104864401c2SGerd Hoffmann
10578dd9ac1SGerd Hoffmann /* mouse bits */
10678dd9ac1SGerd Hoffmann
107869564a9SGerd Hoffmann typedef struct QemuSpicePointer {
108869564a9SGerd Hoffmann SpiceMouseInstance mouse;
109869564a9SGerd Hoffmann SpiceTabletInstance tablet;
110f100db38SGerd Hoffmann int width, height;
111f100db38SGerd Hoffmann uint32_t last_bmask;
112869564a9SGerd Hoffmann Notifier mouse_mode;
113869564a9SGerd Hoffmann bool absolute;
114869564a9SGerd Hoffmann } QemuSpicePointer;
11578dd9ac1SGerd Hoffmann
spice_update_buttons(QemuSpicePointer * pointer,int wheel,uint32_t button_mask)116f100db38SGerd Hoffmann static void spice_update_buttons(QemuSpicePointer *pointer,
117f100db38SGerd Hoffmann int wheel, uint32_t button_mask)
11878dd9ac1SGerd Hoffmann {
1197fb1cf16SEric Blake static uint32_t bmap[INPUT_BUTTON__MAX] = {
120f100db38SGerd Hoffmann [INPUT_BUTTON_LEFT] = 0x01,
121f100db38SGerd Hoffmann [INPUT_BUTTON_MIDDLE] = 0x04,
122f100db38SGerd Hoffmann [INPUT_BUTTON_RIGHT] = 0x02,
123f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_UP] = 0x10,
124f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_DOWN] = 0x20,
1255b57a87eSFrediano Ziglio [INPUT_BUTTON_SIDE] = 0x40,
1265b57a87eSFrediano Ziglio [INPUT_BUTTON_EXTRA] = 0x80,
127f100db38SGerd Hoffmann };
12878dd9ac1SGerd Hoffmann
129f100db38SGerd Hoffmann if (wheel < 0) {
130f100db38SGerd Hoffmann button_mask |= 0x10;
13178dd9ac1SGerd Hoffmann }
132f100db38SGerd Hoffmann if (wheel > 0) {
133f100db38SGerd Hoffmann button_mask |= 0x20;
13478dd9ac1SGerd Hoffmann }
135f100db38SGerd Hoffmann
136f100db38SGerd Hoffmann if (pointer->last_bmask == button_mask) {
137f100db38SGerd Hoffmann return;
13878dd9ac1SGerd Hoffmann }
139f100db38SGerd Hoffmann qemu_input_update_buttons(NULL, bmap, pointer->last_bmask, button_mask);
140f100db38SGerd Hoffmann pointer->last_bmask = button_mask;
14178dd9ac1SGerd Hoffmann }
14278dd9ac1SGerd Hoffmann
mouse_motion(SpiceMouseInstance * sin,int dx,int dy,int dz,uint32_t buttons_state)14378dd9ac1SGerd Hoffmann static void mouse_motion(SpiceMouseInstance *sin, int dx, int dy, int dz,
14478dd9ac1SGerd Hoffmann uint32_t buttons_state)
14578dd9ac1SGerd Hoffmann {
146f100db38SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, mouse);
147f100db38SGerd Hoffmann spice_update_buttons(pointer, dz, buttons_state);
148f100db38SGerd Hoffmann qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
149f100db38SGerd Hoffmann qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
150f100db38SGerd Hoffmann qemu_input_event_sync();
15178dd9ac1SGerd Hoffmann }
15278dd9ac1SGerd Hoffmann
mouse_buttons(SpiceMouseInstance * sin,uint32_t buttons_state)15378dd9ac1SGerd Hoffmann static void mouse_buttons(SpiceMouseInstance *sin, uint32_t buttons_state)
15478dd9ac1SGerd Hoffmann {
155f100db38SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, mouse);
156f100db38SGerd Hoffmann spice_update_buttons(pointer, 0, buttons_state);
157f100db38SGerd Hoffmann qemu_input_event_sync();
15878dd9ac1SGerd Hoffmann }
15978dd9ac1SGerd Hoffmann
16078dd9ac1SGerd Hoffmann static const SpiceMouseInterface mouse_interface = {
16178dd9ac1SGerd Hoffmann .base.type = SPICE_INTERFACE_MOUSE,
16278dd9ac1SGerd Hoffmann .base.description = "mouse",
16378dd9ac1SGerd Hoffmann .base.major_version = SPICE_INTERFACE_MOUSE_MAJOR,
16478dd9ac1SGerd Hoffmann .base.minor_version = SPICE_INTERFACE_MOUSE_MINOR,
16578dd9ac1SGerd Hoffmann .motion = mouse_motion,
16678dd9ac1SGerd Hoffmann .buttons = mouse_buttons,
16778dd9ac1SGerd Hoffmann };
16878dd9ac1SGerd Hoffmann
tablet_set_logical_size(SpiceTabletInstance * sin,int width,int height)169869564a9SGerd Hoffmann static void tablet_set_logical_size(SpiceTabletInstance* sin, int width, int height)
170869564a9SGerd Hoffmann {
171869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
172869564a9SGerd Hoffmann
173869564a9SGerd Hoffmann if (height < 16) {
174869564a9SGerd Hoffmann height = 16;
175869564a9SGerd Hoffmann }
176869564a9SGerd Hoffmann if (width < 16) {
177869564a9SGerd Hoffmann width = 16;
178869564a9SGerd Hoffmann }
179869564a9SGerd Hoffmann pointer->width = width;
180869564a9SGerd Hoffmann pointer->height = height;
181869564a9SGerd Hoffmann }
182869564a9SGerd Hoffmann
tablet_position(SpiceTabletInstance * sin,int x,int y,uint32_t buttons_state)183869564a9SGerd Hoffmann static void tablet_position(SpiceTabletInstance* sin, int x, int y,
184869564a9SGerd Hoffmann uint32_t buttons_state)
185869564a9SGerd Hoffmann {
186869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
187869564a9SGerd Hoffmann
188f100db38SGerd Hoffmann spice_update_buttons(pointer, 0, buttons_state);
1899cfa7ab9SPhilippe Voinov qemu_input_queue_abs(NULL, INPUT_AXIS_X, x, 0, pointer->width);
1909cfa7ab9SPhilippe Voinov qemu_input_queue_abs(NULL, INPUT_AXIS_Y, y, 0, pointer->height);
191f100db38SGerd Hoffmann qemu_input_event_sync();
192869564a9SGerd Hoffmann }
193869564a9SGerd Hoffmann
194869564a9SGerd Hoffmann
tablet_wheel(SpiceTabletInstance * sin,int wheel,uint32_t buttons_state)195869564a9SGerd Hoffmann static void tablet_wheel(SpiceTabletInstance* sin, int wheel,
196869564a9SGerd Hoffmann uint32_t buttons_state)
197869564a9SGerd Hoffmann {
198869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
199869564a9SGerd Hoffmann
200f100db38SGerd Hoffmann spice_update_buttons(pointer, wheel, buttons_state);
201f100db38SGerd Hoffmann qemu_input_event_sync();
202869564a9SGerd Hoffmann }
203869564a9SGerd Hoffmann
tablet_buttons(SpiceTabletInstance * sin,uint32_t buttons_state)204869564a9SGerd Hoffmann static void tablet_buttons(SpiceTabletInstance *sin,
205869564a9SGerd Hoffmann uint32_t buttons_state)
206869564a9SGerd Hoffmann {
207869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
208869564a9SGerd Hoffmann
209f100db38SGerd Hoffmann spice_update_buttons(pointer, 0, buttons_state);
210f100db38SGerd Hoffmann qemu_input_event_sync();
211869564a9SGerd Hoffmann }
212869564a9SGerd Hoffmann
213869564a9SGerd Hoffmann static const SpiceTabletInterface tablet_interface = {
214869564a9SGerd Hoffmann .base.type = SPICE_INTERFACE_TABLET,
215869564a9SGerd Hoffmann .base.description = "tablet",
216869564a9SGerd Hoffmann .base.major_version = SPICE_INTERFACE_TABLET_MAJOR,
217869564a9SGerd Hoffmann .base.minor_version = SPICE_INTERFACE_TABLET_MINOR,
218869564a9SGerd Hoffmann .set_logical_size = tablet_set_logical_size,
219869564a9SGerd Hoffmann .position = tablet_position,
220869564a9SGerd Hoffmann .wheel = tablet_wheel,
221869564a9SGerd Hoffmann .buttons = tablet_buttons,
222869564a9SGerd Hoffmann };
223869564a9SGerd Hoffmann
mouse_mode_notifier(Notifier * notifier,void * data)2249e8dd451SJan Kiszka static void mouse_mode_notifier(Notifier *notifier, void *data)
225869564a9SGerd Hoffmann {
226869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(notifier, QemuSpicePointer, mouse_mode);
227*0337e412SAkihiko Odaki bool is_absolute = qemu_input_is_absolute(NULL);
228869564a9SGerd Hoffmann
229869564a9SGerd Hoffmann if (pointer->absolute == is_absolute) {
230869564a9SGerd Hoffmann return;
231869564a9SGerd Hoffmann }
232869564a9SGerd Hoffmann
233869564a9SGerd Hoffmann if (is_absolute) {
23405b53636SGerd Hoffmann qemu_spice.add_interface(&pointer->tablet.base);
235869564a9SGerd Hoffmann } else {
236869564a9SGerd Hoffmann spice_server_remove_interface(&pointer->tablet.base);
237869564a9SGerd Hoffmann }
238869564a9SGerd Hoffmann pointer->absolute = is_absolute;
239869564a9SGerd Hoffmann }
240869564a9SGerd Hoffmann
qemu_spice_input_init(void)241864401c2SGerd Hoffmann void qemu_spice_input_init(void)
242864401c2SGerd Hoffmann {
243864401c2SGerd Hoffmann QemuSpiceKbd *kbd;
244869564a9SGerd Hoffmann QemuSpicePointer *pointer;
245864401c2SGerd Hoffmann
2467267c094SAnthony Liguori kbd = g_malloc0(sizeof(*kbd));
247864401c2SGerd Hoffmann kbd->sin.base.sif = &kbd_interface.base;
24805b53636SGerd Hoffmann qemu_spice.add_interface(&kbd->sin.base);
249864401c2SGerd Hoffmann qemu_add_led_event_handler(kbd_leds, kbd);
25078dd9ac1SGerd Hoffmann
2517267c094SAnthony Liguori pointer = g_malloc0(sizeof(*pointer));
252869564a9SGerd Hoffmann pointer->mouse.base.sif = &mouse_interface.base;
253869564a9SGerd Hoffmann pointer->tablet.base.sif = &tablet_interface.base;
25405b53636SGerd Hoffmann qemu_spice.add_interface(&pointer->mouse.base);
255869564a9SGerd Hoffmann
256869564a9SGerd Hoffmann pointer->absolute = false;
257869564a9SGerd Hoffmann pointer->mouse_mode.notify = mouse_mode_notifier;
258869564a9SGerd Hoffmann qemu_add_mouse_mode_change_notifier(&pointer->mouse_mode);
2599e8dd451SJan Kiszka mouse_mode_notifier(&pointer->mouse_mode, NULL);
260864401c2SGerd Hoffmann }
261