blob: c9df69906292bbfec150bb9682ea284ce5d02aeb [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
18#include <stdlib.h>
19#include <stdio.h>
Gerd Hoffmann869564a2010-04-13 09:05:03 +020020#include <stdbool.h>
Gerd Hoffmann864401c2010-03-11 11:13:28 -030021#include <string.h>
22
23#include <spice.h>
24#include <spice/enums.h>
25
26#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/qemu-spice.h"
28#include "ui/console.h"
Gerd Hoffmannde8f5802013-12-04 12:23:54 +010029#include "ui/keymaps.h"
30#include "ui/input.h"
Gerd Hoffmann864401c2010-03-11 11:13:28 -030031
32/* keyboard bits */
33
34typedef struct QemuSpiceKbd {
35 SpiceKbdInstance sin;
36 int ledstate;
Gerd Hoffmannde8f5802013-12-04 12:23:54 +010037 bool emul0;
Gerd Hoffmann864401c2010-03-11 11:13:28 -030038} QemuSpiceKbd;
39
40static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag);
41static uint8_t kbd_get_leds(SpiceKbdInstance *sin);
42static void kbd_leds(void *opaque, int l);
43
44static 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,
51};
52
Gerd Hoffmannde8f5802013-12-04 12:23:54 +010053static void kbd_push_key(SpiceKbdInstance *sin, uint8_t scancode)
Gerd Hoffmann864401c2010-03-11 11:13:28 -030054{
Gerd Hoffmannde8f5802013-12-04 12:23:54 +010055 QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
56 int keycode;
57 bool up;
58
59 if (scancode == SCANCODE_EMUL0) {
60 kbd->emul0 = true;
61 return;
62 }
63 keycode = scancode & ~SCANCODE_UP;
64 up = scancode & SCANCODE_UP;
65 if (kbd->emul0) {
66 kbd->emul0 = false;
67 keycode |= SCANCODE_GREY;
68 }
69
70 qemu_input_event_send_key_number(NULL, keycode, !up);
Gerd Hoffmann864401c2010-03-11 11:13:28 -030071}
72
73static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
74{
75 QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin);
76 return kbd->ledstate;
77}
78
79static void kbd_leds(void *opaque, int ledstate)
80{
81 QemuSpiceKbd *kbd = opaque;
82
83 kbd->ledstate = 0;
84 if (ledstate & QEMU_SCROLL_LOCK_LED) {
85 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK;
86 }
87 if (ledstate & QEMU_NUM_LOCK_LED) {
88 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK;
89 }
90 if (ledstate & QEMU_CAPS_LOCK_LED) {
91 kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK;
92 }
93 spice_server_kbd_leds(&kbd->sin, ledstate);
94}
95
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -030096/* mouse bits */
97
Gerd Hoffmann869564a2010-04-13 09:05:03 +020098typedef struct QemuSpicePointer {
99 SpiceMouseInstance mouse;
100 SpiceTabletInstance tablet;
101 int width, height, x, y;
102 Notifier mouse_mode;
103 bool absolute;
104} QemuSpicePointer;
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300105
106static int map_buttons(int spice_buttons)
107{
108 int qemu_buttons = 0;
109
110 /*
111 * Note: SPICE_MOUSE_BUTTON_* specifies the wire protocol but this
112 * isn't what we get passed in via interface callbacks for the
113 * middle and right button ...
114 */
115 if (spice_buttons & SPICE_MOUSE_BUTTON_MASK_LEFT) {
116 qemu_buttons |= MOUSE_EVENT_LBUTTON;
117 }
118 if (spice_buttons & 0x04 /* SPICE_MOUSE_BUTTON_MASK_MIDDLE */) {
119 qemu_buttons |= MOUSE_EVENT_MBUTTON;
120 }
121 if (spice_buttons & 0x02 /* SPICE_MOUSE_BUTTON_MASK_RIGHT */) {
122 qemu_buttons |= MOUSE_EVENT_RBUTTON;
123 }
124 return qemu_buttons;
125}
126
127static void mouse_motion(SpiceMouseInstance *sin, int dx, int dy, int dz,
128 uint32_t buttons_state)
129{
130 kbd_mouse_event(dx, dy, dz, map_buttons(buttons_state));
131}
132
133static void mouse_buttons(SpiceMouseInstance *sin, uint32_t buttons_state)
134{
135 kbd_mouse_event(0, 0, 0, map_buttons(buttons_state));
136}
137
138static const SpiceMouseInterface mouse_interface = {
139 .base.type = SPICE_INTERFACE_MOUSE,
140 .base.description = "mouse",
141 .base.major_version = SPICE_INTERFACE_MOUSE_MAJOR,
142 .base.minor_version = SPICE_INTERFACE_MOUSE_MINOR,
143 .motion = mouse_motion,
144 .buttons = mouse_buttons,
145};
146
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200147static void tablet_set_logical_size(SpiceTabletInstance* sin, int width, int height)
148{
149 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
150
151 if (height < 16) {
152 height = 16;
153 }
154 if (width < 16) {
155 width = 16;
156 }
157 pointer->width = width;
158 pointer->height = height;
159}
160
161static void tablet_position(SpiceTabletInstance* sin, int x, int y,
162 uint32_t buttons_state)
163{
164 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
165
166 pointer->x = x * 0x7FFF / (pointer->width - 1);
167 pointer->y = y * 0x7FFF / (pointer->height - 1);
168 kbd_mouse_event(pointer->x, pointer->y, 0, map_buttons(buttons_state));
169}
170
171
172static void tablet_wheel(SpiceTabletInstance* sin, int wheel,
173 uint32_t buttons_state)
174{
175 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
176
177 kbd_mouse_event(pointer->x, pointer->y, wheel, map_buttons(buttons_state));
178}
179
180static void tablet_buttons(SpiceTabletInstance *sin,
181 uint32_t buttons_state)
182{
183 QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet);
184
185 kbd_mouse_event(pointer->x, pointer->y, 0, map_buttons(buttons_state));
186}
187
188static const SpiceTabletInterface tablet_interface = {
189 .base.type = SPICE_INTERFACE_TABLET,
190 .base.description = "tablet",
191 .base.major_version = SPICE_INTERFACE_TABLET_MAJOR,
192 .base.minor_version = SPICE_INTERFACE_TABLET_MINOR,
193 .set_logical_size = tablet_set_logical_size,
194 .position = tablet_position,
195 .wheel = tablet_wheel,
196 .buttons = tablet_buttons,
197};
198
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200199static void mouse_mode_notifier(Notifier *notifier, void *data)
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200200{
201 QemuSpicePointer *pointer = container_of(notifier, QemuSpicePointer, mouse_mode);
202 bool is_absolute = kbd_mouse_is_absolute();
203
204 if (pointer->absolute == is_absolute) {
205 return;
206 }
207
208 if (is_absolute) {
209 qemu_spice_add_interface(&pointer->tablet.base);
210 } else {
211 spice_server_remove_interface(&pointer->tablet.base);
212 }
213 pointer->absolute = is_absolute;
214}
215
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300216void qemu_spice_input_init(void)
217{
218 QemuSpiceKbd *kbd;
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200219 QemuSpicePointer *pointer;
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300220
Anthony Liguori7267c092011-08-20 22:09:37 -0500221 kbd = g_malloc0(sizeof(*kbd));
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300222 kbd->sin.base.sif = &kbd_interface.base;
223 qemu_spice_add_interface(&kbd->sin.base);
224 qemu_add_led_event_handler(kbd_leds, kbd);
Gerd Hoffmann78dd9ac2010-03-11 11:13:29 -0300225
Anthony Liguori7267c092011-08-20 22:09:37 -0500226 pointer = g_malloc0(sizeof(*pointer));
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200227 pointer->mouse.base.sif = &mouse_interface.base;
228 pointer->tablet.base.sif = &tablet_interface.base;
229 qemu_spice_add_interface(&pointer->mouse.base);
230
231 pointer->absolute = false;
232 pointer->mouse_mode.notify = mouse_mode_notifier;
233 qemu_add_mouse_mode_change_notifier(&pointer->mouse_mode);
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200234 mouse_mode_notifier(&pointer->mouse_mode, NULL);
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300235}