2 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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/>.
24 #include <spice/enums.h>
26 #include "qemu-common.h"
27 #include "ui/qemu-spice.h"
28 #include "ui/console.h"
29 #include "ui/keymaps.h"
34 typedef struct QemuSpiceKbd
{
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
);
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
,
53 static void kbd_push_key(SpiceKbdInstance
*sin
, uint8_t scancode
)
55 QemuSpiceKbd
*kbd
= container_of(sin
, QemuSpiceKbd
, sin
);
59 if (scancode
== SCANCODE_EMUL0
) {
63 keycode
= scancode
& ~SCANCODE_UP
;
64 up
= scancode
& SCANCODE_UP
;
67 keycode
|= SCANCODE_GREY
;
70 qemu_input_event_send_key_number(NULL
, keycode
, !up
);
73 static uint8_t kbd_get_leds(SpiceKbdInstance
*sin
)
75 QemuSpiceKbd
*kbd
= container_of(sin
, QemuSpiceKbd
, sin
);
79 static void kbd_leds(void *opaque
, int ledstate
)
81 QemuSpiceKbd
*kbd
= opaque
;
84 if (ledstate
& QEMU_SCROLL_LOCK_LED
) {
85 kbd
->ledstate
|= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK
;
87 if (ledstate
& QEMU_NUM_LOCK_LED
) {
88 kbd
->ledstate
|= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK
;
90 if (ledstate
& QEMU_CAPS_LOCK_LED
) {
91 kbd
->ledstate
|= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK
;
93 spice_server_kbd_leds(&kbd
->sin
, ledstate
);
98 typedef struct QemuSpicePointer
{
99 SpiceMouseInstance mouse
;
100 SpiceTabletInstance tablet
;
107 static void spice_update_buttons(QemuSpicePointer
*pointer
,
108 int wheel
, uint32_t button_mask
)
110 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
111 [INPUT_BUTTON_LEFT
] = 0x01,
112 [INPUT_BUTTON_MIDDLE
] = 0x04,
113 [INPUT_BUTTON_RIGHT
] = 0x02,
114 [INPUT_BUTTON_WHEELUP
] = 0x10,
115 [INPUT_BUTTON_WHEELDOWN
] = 0x20,
125 if (pointer
->last_bmask
== button_mask
) {
128 qemu_input_update_buttons(NULL
, bmap
, pointer
->last_bmask
, button_mask
);
129 pointer
->last_bmask
= button_mask
;
132 static void mouse_motion(SpiceMouseInstance
*sin
, int dx
, int dy
, int dz
,
133 uint32_t buttons_state
)
135 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, mouse
);
136 spice_update_buttons(pointer
, dz
, buttons_state
);
137 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
138 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
139 qemu_input_event_sync();
142 static void mouse_buttons(SpiceMouseInstance
*sin
, uint32_t buttons_state
)
144 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, mouse
);
145 spice_update_buttons(pointer
, 0, buttons_state
);
146 qemu_input_event_sync();
149 static const SpiceMouseInterface mouse_interface
= {
150 .base
.type
= SPICE_INTERFACE_MOUSE
,
151 .base
.description
= "mouse",
152 .base
.major_version
= SPICE_INTERFACE_MOUSE_MAJOR
,
153 .base
.minor_version
= SPICE_INTERFACE_MOUSE_MINOR
,
154 .motion
= mouse_motion
,
155 .buttons
= mouse_buttons
,
158 static void tablet_set_logical_size(SpiceTabletInstance
* sin
, int width
, int height
)
160 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, tablet
);
168 pointer
->width
= width
;
169 pointer
->height
= height
;
172 static void tablet_position(SpiceTabletInstance
* sin
, int x
, int y
,
173 uint32_t buttons_state
)
175 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, tablet
);
177 spice_update_buttons(pointer
, 0, buttons_state
);
178 qemu_input_queue_abs(NULL
, INPUT_AXIS_X
, x
, pointer
->width
);
179 qemu_input_queue_abs(NULL
, INPUT_AXIS_Y
, y
, pointer
->height
);
180 qemu_input_event_sync();
184 static void tablet_wheel(SpiceTabletInstance
* sin
, int wheel
,
185 uint32_t buttons_state
)
187 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, tablet
);
189 spice_update_buttons(pointer
, wheel
, buttons_state
);
190 qemu_input_event_sync();
193 static void tablet_buttons(SpiceTabletInstance
*sin
,
194 uint32_t buttons_state
)
196 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, tablet
);
198 spice_update_buttons(pointer
, 0, buttons_state
);
199 qemu_input_event_sync();
202 static const SpiceTabletInterface tablet_interface
= {
203 .base
.type
= SPICE_INTERFACE_TABLET
,
204 .base
.description
= "tablet",
205 .base
.major_version
= SPICE_INTERFACE_TABLET_MAJOR
,
206 .base
.minor_version
= SPICE_INTERFACE_TABLET_MINOR
,
207 .set_logical_size
= tablet_set_logical_size
,
208 .position
= tablet_position
,
209 .wheel
= tablet_wheel
,
210 .buttons
= tablet_buttons
,
213 static void mouse_mode_notifier(Notifier
*notifier
, void *data
)
215 QemuSpicePointer
*pointer
= container_of(notifier
, QemuSpicePointer
, mouse_mode
);
216 bool is_absolute
= qemu_input_is_absolute();
218 if (pointer
->absolute
== is_absolute
) {
223 qemu_spice_add_interface(&pointer
->tablet
.base
);
225 spice_server_remove_interface(&pointer
->tablet
.base
);
227 pointer
->absolute
= is_absolute
;
230 void qemu_spice_input_init(void)
233 QemuSpicePointer
*pointer
;
235 kbd
= g_malloc0(sizeof(*kbd
));
236 kbd
->sin
.base
.sif
= &kbd_interface
.base
;
237 qemu_spice_add_interface(&kbd
->sin
.base
);
238 qemu_add_led_event_handler(kbd_leds
, kbd
);
240 pointer
= g_malloc0(sizeof(*pointer
));
241 pointer
->mouse
.base
.sif
= &mouse_interface
.base
;
242 pointer
->tablet
.base
.sif
= &tablet_interface
.base
;
243 qemu_spice_add_interface(&pointer
->mouse
.base
);
245 pointer
->absolute
= false;
246 pointer
->mouse_mode
.notify
= mouse_mode_notifier
;
247 qemu_add_mouse_mode_change_notifier(&pointer
->mouse_mode
);
248 mouse_mode_notifier(&pointer
->mouse_mode
, NULL
);