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/>.
18 #include "qemu/osdep.h"
21 #include <spice/enums.h>
23 #include "qemu-common.h"
24 #include "ui/qemu-spice.h"
25 #include "ui/console.h"
26 #include "ui/keymaps.h"
31 typedef struct QemuSpiceKbd
{
37 static void kbd_push_key(SpiceKbdInstance
*sin
, uint8_t frag
);
38 static uint8_t kbd_get_leds(SpiceKbdInstance
*sin
);
39 static void kbd_leds(void *opaque
, int l
);
41 static 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
,
50 static void kbd_push_key(SpiceKbdInstance
*sin
, uint8_t scancode
)
52 QemuSpiceKbd
*kbd
= container_of(sin
, QemuSpiceKbd
, sin
);
56 if (scancode
== SCANCODE_EMUL0
) {
60 keycode
= scancode
& ~SCANCODE_UP
;
61 up
= scancode
& SCANCODE_UP
;
64 keycode
|= SCANCODE_GREY
;
67 qemu_input_event_send_key_number(NULL
, keycode
, !up
);
70 static uint8_t kbd_get_leds(SpiceKbdInstance
*sin
)
72 QemuSpiceKbd
*kbd
= container_of(sin
, QemuSpiceKbd
, sin
);
76 static void kbd_leds(void *opaque
, int ledstate
)
78 QemuSpiceKbd
*kbd
= opaque
;
81 if (ledstate
& QEMU_SCROLL_LOCK_LED
) {
82 kbd
->ledstate
|= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK
;
84 if (ledstate
& QEMU_NUM_LOCK_LED
) {
85 kbd
->ledstate
|= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK
;
87 if (ledstate
& QEMU_CAPS_LOCK_LED
) {
88 kbd
->ledstate
|= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK
;
90 spice_server_kbd_leds(&kbd
->sin
, ledstate
);
95 typedef struct QemuSpicePointer
{
96 SpiceMouseInstance mouse
;
97 SpiceTabletInstance tablet
;
104 static void spice_update_buttons(QemuSpicePointer
*pointer
,
105 int wheel
, uint32_t button_mask
)
107 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
108 [INPUT_BUTTON_LEFT
] = 0x01,
109 [INPUT_BUTTON_MIDDLE
] = 0x04,
110 [INPUT_BUTTON_RIGHT
] = 0x02,
111 [INPUT_BUTTON_WHEELUP
] = 0x10,
112 [INPUT_BUTTON_WHEELDOWN
] = 0x20,
122 if (pointer
->last_bmask
== button_mask
) {
125 qemu_input_update_buttons(NULL
, bmap
, pointer
->last_bmask
, button_mask
);
126 pointer
->last_bmask
= button_mask
;
129 static void mouse_motion(SpiceMouseInstance
*sin
, int dx
, int dy
, int dz
,
130 uint32_t buttons_state
)
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();
139 static void mouse_buttons(SpiceMouseInstance
*sin
, uint32_t buttons_state
)
141 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, mouse
);
142 spice_update_buttons(pointer
, 0, buttons_state
);
143 qemu_input_event_sync();
146 static 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
,
155 static void tablet_set_logical_size(SpiceTabletInstance
* sin
, int width
, int height
)
157 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, tablet
);
165 pointer
->width
= width
;
166 pointer
->height
= height
;
169 static void tablet_position(SpiceTabletInstance
* sin
, int x
, int y
,
170 uint32_t buttons_state
)
172 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, tablet
);
174 spice_update_buttons(pointer
, 0, buttons_state
);
175 qemu_input_queue_abs(NULL
, INPUT_AXIS_X
, x
, pointer
->width
);
176 qemu_input_queue_abs(NULL
, INPUT_AXIS_Y
, y
, pointer
->height
);
177 qemu_input_event_sync();
181 static void tablet_wheel(SpiceTabletInstance
* sin
, int wheel
,
182 uint32_t buttons_state
)
184 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, tablet
);
186 spice_update_buttons(pointer
, wheel
, buttons_state
);
187 qemu_input_event_sync();
190 static void tablet_buttons(SpiceTabletInstance
*sin
,
191 uint32_t buttons_state
)
193 QemuSpicePointer
*pointer
= container_of(sin
, QemuSpicePointer
, tablet
);
195 spice_update_buttons(pointer
, 0, buttons_state
);
196 qemu_input_event_sync();
199 static 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
,
210 static void mouse_mode_notifier(Notifier
*notifier
, void *data
)
212 QemuSpicePointer
*pointer
= container_of(notifier
, QemuSpicePointer
, mouse_mode
);
213 bool is_absolute
= qemu_input_is_absolute();
215 if (pointer
->absolute
== is_absolute
) {
220 qemu_spice_add_interface(&pointer
->tablet
.base
);
222 spice_server_remove_interface(&pointer
->tablet
.base
);
224 pointer
->absolute
= is_absolute
;
227 void qemu_spice_input_init(void)
230 QemuSpicePointer
*pointer
;
232 kbd
= g_malloc0(sizeof(*kbd
));
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
);
237 pointer
= g_malloc0(sizeof(*pointer
));
238 pointer
->mouse
.base
.sif
= &mouse_interface
.base
;
239 pointer
->tablet
.base
.sif
= &tablet_interface
.base
;
240 qemu_spice_add_interface(&pointer
->mouse
.base
);
242 pointer
->absolute
= false;
243 pointer
->mouse_mode
.notify
= mouse_mode_notifier
;
244 qemu_add_mouse_mode_change_notifier(&pointer
->mouse_mode
);
245 mouse_mode_notifier(&pointer
->mouse_mode
, NULL
);