Gerd Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 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 | |
| 18 | #include <stdlib.h> |
| 19 | #include <stdio.h> |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 20 | #include <stdbool.h> |
Gerd Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
| 23 | #include <spice.h> |
| 24 | #include <spice/enums.h> |
| 25 | |
| 26 | #include "qemu-common.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 27 | #include "ui/qemu-spice.h" |
| 28 | #include "ui/console.h" |
Gerd Hoffmann | de8f580 | 2013-12-04 12:23:54 +0100 | [diff] [blame] | 29 | #include "ui/keymaps.h" |
| 30 | #include "ui/input.h" |
Gerd Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 31 | |
| 32 | /* keyboard bits */ |
| 33 | |
| 34 | typedef struct QemuSpiceKbd { |
| 35 | SpiceKbdInstance sin; |
| 36 | int ledstate; |
Gerd Hoffmann | de8f580 | 2013-12-04 12:23:54 +0100 | [diff] [blame] | 37 | bool emul0; |
Gerd Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 38 | } QemuSpiceKbd; |
| 39 | |
| 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); |
| 43 | |
| 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, |
| 51 | }; |
| 52 | |
Gerd Hoffmann | de8f580 | 2013-12-04 12:23:54 +0100 | [diff] [blame] | 53 | static void kbd_push_key(SpiceKbdInstance *sin, uint8_t scancode) |
Gerd Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 54 | { |
Gerd Hoffmann | de8f580 | 2013-12-04 12:23:54 +0100 | [diff] [blame] | 55 | 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 Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static uint8_t kbd_get_leds(SpiceKbdInstance *sin) |
| 74 | { |
| 75 | QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin); |
| 76 | return kbd->ledstate; |
| 77 | } |
| 78 | |
| 79 | static 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 Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 96 | /* mouse bits */ |
| 97 | |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 98 | typedef struct QemuSpicePointer { |
| 99 | SpiceMouseInstance mouse; |
| 100 | SpiceTabletInstance tablet; |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 101 | int width, height; |
| 102 | uint32_t last_bmask; |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 103 | Notifier mouse_mode; |
| 104 | bool absolute; |
| 105 | } QemuSpicePointer; |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 106 | |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 107 | static void spice_update_buttons(QemuSpicePointer *pointer, |
| 108 | int wheel, uint32_t button_mask) |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 109 | { |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 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_WHEEL_UP] = 0x10, |
| 115 | [INPUT_BUTTON_WHEEL_DOWN] = 0x20, |
| 116 | }; |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 117 | |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 118 | if (wheel < 0) { |
| 119 | button_mask |= 0x10; |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 120 | } |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 121 | if (wheel > 0) { |
| 122 | button_mask |= 0x20; |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 123 | } |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 124 | |
| 125 | if (pointer->last_bmask == button_mask) { |
| 126 | return; |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 127 | } |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 128 | qemu_input_update_buttons(NULL, bmap, pointer->last_bmask, button_mask); |
| 129 | pointer->last_bmask = button_mask; |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void mouse_motion(SpiceMouseInstance *sin, int dx, int dy, int dz, |
| 133 | uint32_t buttons_state) |
| 134 | { |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 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(); |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void mouse_buttons(SpiceMouseInstance *sin, uint32_t buttons_state) |
| 143 | { |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 144 | QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, mouse); |
| 145 | spice_update_buttons(pointer, 0, buttons_state); |
| 146 | qemu_input_event_sync(); |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 147 | } |
| 148 | |
| 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, |
| 156 | }; |
| 157 | |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 158 | static void tablet_set_logical_size(SpiceTabletInstance* sin, int width, int height) |
| 159 | { |
| 160 | QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet); |
| 161 | |
| 162 | if (height < 16) { |
| 163 | height = 16; |
| 164 | } |
| 165 | if (width < 16) { |
| 166 | width = 16; |
| 167 | } |
| 168 | pointer->width = width; |
| 169 | pointer->height = height; |
| 170 | } |
| 171 | |
| 172 | static void tablet_position(SpiceTabletInstance* sin, int x, int y, |
| 173 | uint32_t buttons_state) |
| 174 | { |
| 175 | QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet); |
| 176 | |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 177 | spice_update_buttons(pointer, 0, buttons_state); |
| 178 | qemu_input_queue_abs(NULL, INPUT_AXIS_X, x, pointer->width); |
Cole Robinson | b2c494c | 2014-03-21 14:28:49 -0400 | [diff] [blame] | 179 | qemu_input_queue_abs(NULL, INPUT_AXIS_Y, y, pointer->height); |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 180 | qemu_input_event_sync(); |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
| 184 | static void tablet_wheel(SpiceTabletInstance* sin, int wheel, |
| 185 | uint32_t buttons_state) |
| 186 | { |
| 187 | QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet); |
| 188 | |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 189 | spice_update_buttons(pointer, wheel, buttons_state); |
| 190 | qemu_input_event_sync(); |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void tablet_buttons(SpiceTabletInstance *sin, |
| 194 | uint32_t buttons_state) |
| 195 | { |
| 196 | QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet); |
| 197 | |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 198 | spice_update_buttons(pointer, 0, buttons_state); |
| 199 | qemu_input_event_sync(); |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 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, |
| 211 | }; |
| 212 | |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 213 | static void mouse_mode_notifier(Notifier *notifier, void *data) |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 214 | { |
| 215 | QemuSpicePointer *pointer = container_of(notifier, QemuSpicePointer, mouse_mode); |
Gerd Hoffmann | f100db3 | 2013-12-04 12:46:34 +0100 | [diff] [blame] | 216 | bool is_absolute = qemu_input_is_absolute(); |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 217 | |
| 218 | if (pointer->absolute == is_absolute) { |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | if (is_absolute) { |
| 223 | qemu_spice_add_interface(&pointer->tablet.base); |
| 224 | } else { |
| 225 | spice_server_remove_interface(&pointer->tablet.base); |
| 226 | } |
| 227 | pointer->absolute = is_absolute; |
| 228 | } |
| 229 | |
Gerd Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 230 | void qemu_spice_input_init(void) |
| 231 | { |
| 232 | QemuSpiceKbd *kbd; |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 233 | QemuSpicePointer *pointer; |
Gerd Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 234 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 235 | kbd = g_malloc0(sizeof(*kbd)); |
Gerd Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 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); |
Gerd Hoffmann | 78dd9ac | 2010-03-11 11:13:29 -0300 | [diff] [blame] | 239 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 240 | pointer = g_malloc0(sizeof(*pointer)); |
Gerd Hoffmann | 869564a | 2010-04-13 09:05:03 +0200 | [diff] [blame] | 241 | pointer->mouse.base.sif = &mouse_interface.base; |
| 242 | pointer->tablet.base.sif = &tablet_interface.base; |
| 243 | qemu_spice_add_interface(&pointer->mouse.base); |
| 244 | |
| 245 | pointer->absolute = false; |
| 246 | pointer->mouse_mode.notify = mouse_mode_notifier; |
| 247 | qemu_add_mouse_mode_change_notifier(&pointer->mouse_mode); |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 248 | mouse_mode_notifier(&pointer->mouse_mode, NULL); |
Gerd Hoffmann | 864401c | 2010-03-11 11:13:28 -0300 | [diff] [blame] | 249 | } |