]> git.proxmox.com Git - mirror_qemu.git/blame - ui/spice-input.c
hmp: fix "dump-quest-memory" segfault
[mirror_qemu.git] / ui / spice-input.c
CommitLineData
864401c2
GH
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
e16f4c87 18#include "qemu/osdep.h"
864401c2
GH
19
20#include <spice.h>
21#include <spice/enums.h>
22
23#include "qemu-common.h"
28ecbaee
PB
24#include "ui/qemu-spice.h"
25#include "ui/console.h"
de8f580b
GH
26#include "ui/keymaps.h"
27#include "ui/input.h"
864401c2
GH
28
29/* keyboard bits */
30
31typedef struct QemuSpiceKbd {
32 SpiceKbdInstance sin;
33 int ledstate;
de8f580b 34 bool emul0;
864401c2
GH
35} QemuSpiceKbd;
36
37static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag);
38static uint8_t kbd_get_leds(SpiceKbdInstance *sin);
39static void kbd_leds(void *opaque, int l);
40
41static const SpiceKbdInterface kbd_interface = {
42 .base.type = SPICE_INTERFACE_KEYBOARD,
43 .base.description = "qemu keyboard",
44 .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
45 .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
46 .push_scan_freg = kbd_push_key,
47 .get_leds = kbd_get_leds,
48};
49
de8f580b 50static void kbd_push_key(SpiceKbdInstance *sin, uint8_t scancode)
864401c2 51{
de8f580b
GH
52 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 }
60 keycode = scancode & ~SCANCODE_UP;
61 up = scancode & SCANCODE_UP;
62 if (kbd->emul0) {
63 kbd->emul0 = false;
64 keycode |= SCANCODE_GREY;
65 }
66
67 qemu_input_event_send_key_number(NULL, keycode, !up);
864401c2
GH
68}
69
70static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
71{
72 QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
73 return kbd->ledstate;
74}
75
76static void kbd_leds(void *opaque, int ledstate)
77{
78 QemuSpiceKbd *kbd = opaque;
79
80 kbd->ledstate = 0;
81 if (ledstate & QEMU_SCROLL_LOCK_LED) {
82 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK;
83 }
84 if (ledstate & QEMU_NUM_LOCK_LED) {
85 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK;
86 }
87 if (ledstate & QEMU_CAPS_LOCK_LED) {
88 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK;
89 }
90 spice_server_kbd_leds(&kbd->sin, ledstate);
91}
92
78dd9ac1
GH
93/* mouse bits */
94
869564a9
GH
95typedef struct QemuSpicePointer {
96 SpiceMouseInstance mouse;
97 SpiceTabletInstance tablet;
f100db38
GH
98 int width, height;
99 uint32_t last_bmask;
869564a9
GH
100 Notifier mouse_mode;
101 bool absolute;
102} QemuSpicePointer;
78dd9ac1 103
f100db38
GH
104static void spice_update_buttons(QemuSpicePointer *pointer,
105 int wheel, uint32_t button_mask)
78dd9ac1 106{
7fb1cf16 107 static uint32_t bmap[INPUT_BUTTON__MAX] = {
f100db38
GH
108 [INPUT_BUTTON_LEFT] = 0x01,
109 [INPUT_BUTTON_MIDDLE] = 0x04,
110 [INPUT_BUTTON_RIGHT] = 0x02,
f22d0af0
GH
111 [INPUT_BUTTON_WHEEL_UP] = 0x10,
112 [INPUT_BUTTON_WHEEL_DOWN] = 0x20,
f100db38
GH
113 };
114
115 if (wheel < 0) {
116 button_mask |= 0x10;
78dd9ac1 117 }
f100db38
GH
118 if (wheel > 0) {
119 button_mask |= 0x20;
78dd9ac1 120 }
f100db38
GH
121
122 if (pointer->last_bmask == button_mask) {
123 return;
78dd9ac1 124 }
f100db38
GH
125 qemu_input_update_buttons(NULL, bmap, pointer->last_bmask, button_mask);
126 pointer->last_bmask = button_mask;
78dd9ac1
GH
127}
128
129static void mouse_motion(SpiceMouseInstance *sin, int dx, int dy, int dz,
130 uint32_t buttons_state)
131{
f100db38
GH
132 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, mouse);
133 spice_update_buttons(pointer, dz, buttons_state);
134 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
135 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
136 qemu_input_event_sync();
78dd9ac1
GH
137}
138
139static void mouse_buttons(SpiceMouseInstance *sin, uint32_t buttons_state)
140{
f100db38
GH
141 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, mouse);
142 spice_update_buttons(pointer, 0, buttons_state);
143 qemu_input_event_sync();
78dd9ac1
GH
144}
145
146static const SpiceMouseInterface mouse_interface = {
147 .base.type = SPICE_INTERFACE_MOUSE,
148 .base.description = "mouse",
149 .base.major_version = SPICE_INTERFACE_MOUSE_MAJOR,
150 .base.minor_version = SPICE_INTERFACE_MOUSE_MINOR,
151 .motion = mouse_motion,
152 .buttons = mouse_buttons,
153};
154
869564a9
GH
155static void tablet_set_logical_size(SpiceTabletInstance* sin, int width, int height)
156{
157 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
158
159 if (height < 16) {
160 height = 16;
161 }
162 if (width < 16) {
163 width = 16;
164 }
165 pointer->width = width;
166 pointer->height = height;
167}
168
169static void tablet_position(SpiceTabletInstance* sin, int x, int y,
170 uint32_t buttons_state)
171{
172 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
173
f100db38
GH
174 spice_update_buttons(pointer, 0, buttons_state);
175 qemu_input_queue_abs(NULL, INPUT_AXIS_X, x, pointer->width);
b2c494c3 176 qemu_input_queue_abs(NULL, INPUT_AXIS_Y, y, pointer->height);
f100db38 177 qemu_input_event_sync();
869564a9
GH
178}
179
180
181static void tablet_wheel(SpiceTabletInstance* sin, int wheel,
182 uint32_t buttons_state)
183{
184 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
185
f100db38
GH
186 spice_update_buttons(pointer, wheel, buttons_state);
187 qemu_input_event_sync();
869564a9
GH
188}
189
190static void tablet_buttons(SpiceTabletInstance *sin,
191 uint32_t buttons_state)
192{
193 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
194
f100db38
GH
195 spice_update_buttons(pointer, 0, buttons_state);
196 qemu_input_event_sync();
869564a9
GH
197}
198
199static const SpiceTabletInterface tablet_interface = {
200 .base.type = SPICE_INTERFACE_TABLET,
201 .base.description = "tablet",
202 .base.major_version = SPICE_INTERFACE_TABLET_MAJOR,
203 .base.minor_version = SPICE_INTERFACE_TABLET_MINOR,
204 .set_logical_size = tablet_set_logical_size,
205 .position = tablet_position,
206 .wheel = tablet_wheel,
207 .buttons = tablet_buttons,
208};
209
9e8dd451 210static void mouse_mode_notifier(Notifier *notifier, void *data)
869564a9
GH
211{
212 QemuSpicePointer *pointer = container_of(notifier, QemuSpicePointer, mouse_mode);
f100db38 213 bool is_absolute = qemu_input_is_absolute();
869564a9
GH
214
215 if (pointer->absolute == is_absolute) {
216 return;
217 }
218
219 if (is_absolute) {
220 qemu_spice_add_interface(&pointer->tablet.base);
221 } else {
222 spice_server_remove_interface(&pointer->tablet.base);
223 }
224 pointer->absolute = is_absolute;
225}
226
864401c2
GH
227void qemu_spice_input_init(void)
228{
229 QemuSpiceKbd *kbd;
869564a9 230 QemuSpicePointer *pointer;
864401c2 231
7267c094 232 kbd = g_malloc0(sizeof(*kbd));
864401c2
GH
233 kbd->sin.base.sif = &kbd_interface.base;
234 qemu_spice_add_interface(&kbd->sin.base);
235 qemu_add_led_event_handler(kbd_leds, kbd);
78dd9ac1 236
7267c094 237 pointer = g_malloc0(sizeof(*pointer));
869564a9
GH
238 pointer->mouse.base.sif = &mouse_interface.base;
239 pointer->tablet.base.sif = &tablet_interface.base;
240 qemu_spice_add_interface(&pointer->mouse.base);
241
242 pointer->absolute = false;
243 pointer->mouse_mode.notify = mouse_mode_notifier;
244 qemu_add_mouse_mode_change_notifier(&pointer->mouse_mode);
9e8dd451 245 mouse_mode_notifier(&pointer->mouse_mode, NULL);
864401c2 246}