1864401c2SGerd Hoffmann /* 2864401c2SGerd Hoffmann * Copyright (C) 2010 Red Hat, Inc. 3864401c2SGerd Hoffmann * 4864401c2SGerd Hoffmann * This program is free software; you can redistribute it and/or 5864401c2SGerd Hoffmann * modify it under the terms of the GNU General Public License as 6864401c2SGerd Hoffmann * published by the Free Software Foundation; either version 2 or 7864401c2SGerd Hoffmann * (at your option) version 3 of the License. 8864401c2SGerd Hoffmann * 9864401c2SGerd Hoffmann * This program is distributed in the hope that it will be useful, 10864401c2SGerd Hoffmann * but WITHOUT ANY WARRANTY; without even the implied warranty of 11864401c2SGerd Hoffmann * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12864401c2SGerd Hoffmann * GNU General Public License for more details. 13864401c2SGerd Hoffmann * 14864401c2SGerd Hoffmann * You should have received a copy of the GNU General Public License 15864401c2SGerd Hoffmann * along with this program; if not, see <http://www.gnu.org/licenses/>. 16864401c2SGerd Hoffmann */ 17864401c2SGerd Hoffmann 18864401c2SGerd Hoffmann #include <stdlib.h> 19864401c2SGerd Hoffmann #include <stdio.h> 20*869564a9SGerd Hoffmann #include <stdbool.h> 21864401c2SGerd Hoffmann #include <string.h> 22864401c2SGerd Hoffmann 23864401c2SGerd Hoffmann #include <spice.h> 24864401c2SGerd Hoffmann #include <spice/enums.h> 25864401c2SGerd Hoffmann 26864401c2SGerd Hoffmann #include "qemu-common.h" 27864401c2SGerd Hoffmann #include "qemu-spice.h" 28864401c2SGerd Hoffmann #include "console.h" 29864401c2SGerd Hoffmann 30864401c2SGerd Hoffmann /* keyboard bits */ 31864401c2SGerd Hoffmann 32864401c2SGerd Hoffmann typedef struct QemuSpiceKbd { 33864401c2SGerd Hoffmann SpiceKbdInstance sin; 34864401c2SGerd Hoffmann int ledstate; 35864401c2SGerd Hoffmann } QemuSpiceKbd; 36864401c2SGerd Hoffmann 37864401c2SGerd Hoffmann static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag); 38864401c2SGerd Hoffmann static uint8_t kbd_get_leds(SpiceKbdInstance *sin); 39864401c2SGerd Hoffmann static void kbd_leds(void *opaque, int l); 40864401c2SGerd Hoffmann 41864401c2SGerd Hoffmann static const SpiceKbdInterface kbd_interface = { 42864401c2SGerd Hoffmann .base.type = SPICE_INTERFACE_KEYBOARD, 43864401c2SGerd Hoffmann .base.description = "qemu keyboard", 44864401c2SGerd Hoffmann .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR, 45864401c2SGerd Hoffmann .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR, 46864401c2SGerd Hoffmann .push_scan_freg = kbd_push_key, 47864401c2SGerd Hoffmann .get_leds = kbd_get_leds, 48864401c2SGerd Hoffmann }; 49864401c2SGerd Hoffmann 50864401c2SGerd Hoffmann static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag) 51864401c2SGerd Hoffmann { 52864401c2SGerd Hoffmann kbd_put_keycode(frag); 53864401c2SGerd Hoffmann } 54864401c2SGerd Hoffmann 55864401c2SGerd Hoffmann static uint8_t kbd_get_leds(SpiceKbdInstance *sin) 56864401c2SGerd Hoffmann { 57864401c2SGerd Hoffmann QemuSpiceKbd *kbd = container_of(sin, QemuSpiceKbd, sin); 58864401c2SGerd Hoffmann return kbd->ledstate; 59864401c2SGerd Hoffmann } 60864401c2SGerd Hoffmann 61864401c2SGerd Hoffmann static void kbd_leds(void *opaque, int ledstate) 62864401c2SGerd Hoffmann { 63864401c2SGerd Hoffmann QemuSpiceKbd *kbd = opaque; 64864401c2SGerd Hoffmann 65864401c2SGerd Hoffmann kbd->ledstate = 0; 66864401c2SGerd Hoffmann if (ledstate & QEMU_SCROLL_LOCK_LED) { 67864401c2SGerd Hoffmann kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_SCROLL_LOCK; 68864401c2SGerd Hoffmann } 69864401c2SGerd Hoffmann if (ledstate & QEMU_NUM_LOCK_LED) { 70864401c2SGerd Hoffmann kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_NUM_LOCK; 71864401c2SGerd Hoffmann } 72864401c2SGerd Hoffmann if (ledstate & QEMU_CAPS_LOCK_LED) { 73864401c2SGerd Hoffmann kbd->ledstate |= SPICE_KEYBOARD_MODIFIER_FLAGS_CAPS_LOCK; 74864401c2SGerd Hoffmann } 75864401c2SGerd Hoffmann spice_server_kbd_leds(&kbd->sin, ledstate); 76864401c2SGerd Hoffmann } 77864401c2SGerd Hoffmann 7878dd9ac1SGerd Hoffmann /* mouse bits */ 7978dd9ac1SGerd Hoffmann 80*869564a9SGerd Hoffmann typedef struct QemuSpicePointer { 81*869564a9SGerd Hoffmann SpiceMouseInstance mouse; 82*869564a9SGerd Hoffmann SpiceTabletInstance tablet; 83*869564a9SGerd Hoffmann int width, height, x, y; 84*869564a9SGerd Hoffmann Notifier mouse_mode; 85*869564a9SGerd Hoffmann bool absolute; 86*869564a9SGerd Hoffmann } QemuSpicePointer; 8778dd9ac1SGerd Hoffmann 8878dd9ac1SGerd Hoffmann static int map_buttons(int spice_buttons) 8978dd9ac1SGerd Hoffmann { 9078dd9ac1SGerd Hoffmann int qemu_buttons = 0; 9178dd9ac1SGerd Hoffmann 9278dd9ac1SGerd Hoffmann /* 9378dd9ac1SGerd Hoffmann * Note: SPICE_MOUSE_BUTTON_* specifies the wire protocol but this 9478dd9ac1SGerd Hoffmann * isn't what we get passed in via interface callbacks for the 9578dd9ac1SGerd Hoffmann * middle and right button ... 9678dd9ac1SGerd Hoffmann */ 9778dd9ac1SGerd Hoffmann if (spice_buttons & SPICE_MOUSE_BUTTON_MASK_LEFT) { 9878dd9ac1SGerd Hoffmann qemu_buttons |= MOUSE_EVENT_LBUTTON; 9978dd9ac1SGerd Hoffmann } 10078dd9ac1SGerd Hoffmann if (spice_buttons & 0x04 /* SPICE_MOUSE_BUTTON_MASK_MIDDLE */) { 10178dd9ac1SGerd Hoffmann qemu_buttons |= MOUSE_EVENT_MBUTTON; 10278dd9ac1SGerd Hoffmann } 10378dd9ac1SGerd Hoffmann if (spice_buttons & 0x02 /* SPICE_MOUSE_BUTTON_MASK_RIGHT */) { 10478dd9ac1SGerd Hoffmann qemu_buttons |= MOUSE_EVENT_RBUTTON; 10578dd9ac1SGerd Hoffmann } 10678dd9ac1SGerd Hoffmann return qemu_buttons; 10778dd9ac1SGerd Hoffmann } 10878dd9ac1SGerd Hoffmann 10978dd9ac1SGerd Hoffmann static void mouse_motion(SpiceMouseInstance *sin, int dx, int dy, int dz, 11078dd9ac1SGerd Hoffmann uint32_t buttons_state) 11178dd9ac1SGerd Hoffmann { 11278dd9ac1SGerd Hoffmann kbd_mouse_event(dx, dy, dz, map_buttons(buttons_state)); 11378dd9ac1SGerd Hoffmann } 11478dd9ac1SGerd Hoffmann 11578dd9ac1SGerd Hoffmann static void mouse_buttons(SpiceMouseInstance *sin, uint32_t buttons_state) 11678dd9ac1SGerd Hoffmann { 11778dd9ac1SGerd Hoffmann kbd_mouse_event(0, 0, 0, map_buttons(buttons_state)); 11878dd9ac1SGerd Hoffmann } 11978dd9ac1SGerd Hoffmann 12078dd9ac1SGerd Hoffmann static const SpiceMouseInterface mouse_interface = { 12178dd9ac1SGerd Hoffmann .base.type = SPICE_INTERFACE_MOUSE, 12278dd9ac1SGerd Hoffmann .base.description = "mouse", 12378dd9ac1SGerd Hoffmann .base.major_version = SPICE_INTERFACE_MOUSE_MAJOR, 12478dd9ac1SGerd Hoffmann .base.minor_version = SPICE_INTERFACE_MOUSE_MINOR, 12578dd9ac1SGerd Hoffmann .motion = mouse_motion, 12678dd9ac1SGerd Hoffmann .buttons = mouse_buttons, 12778dd9ac1SGerd Hoffmann }; 12878dd9ac1SGerd Hoffmann 129*869564a9SGerd Hoffmann static void tablet_set_logical_size(SpiceTabletInstance* sin, int width, int height) 130*869564a9SGerd Hoffmann { 131*869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet); 132*869564a9SGerd Hoffmann 133*869564a9SGerd Hoffmann if (height < 16) { 134*869564a9SGerd Hoffmann height = 16; 135*869564a9SGerd Hoffmann } 136*869564a9SGerd Hoffmann if (width < 16) { 137*869564a9SGerd Hoffmann width = 16; 138*869564a9SGerd Hoffmann } 139*869564a9SGerd Hoffmann pointer->width = width; 140*869564a9SGerd Hoffmann pointer->height = height; 141*869564a9SGerd Hoffmann } 142*869564a9SGerd Hoffmann 143*869564a9SGerd Hoffmann static void tablet_position(SpiceTabletInstance* sin, int x, int y, 144*869564a9SGerd Hoffmann uint32_t buttons_state) 145*869564a9SGerd Hoffmann { 146*869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet); 147*869564a9SGerd Hoffmann 148*869564a9SGerd Hoffmann pointer->x = x * 0x7FFF / (pointer->width - 1); 149*869564a9SGerd Hoffmann pointer->y = y * 0x7FFF / (pointer->height - 1); 150*869564a9SGerd Hoffmann kbd_mouse_event(pointer->x, pointer->y, 0, map_buttons(buttons_state)); 151*869564a9SGerd Hoffmann } 152*869564a9SGerd Hoffmann 153*869564a9SGerd Hoffmann 154*869564a9SGerd Hoffmann static void tablet_wheel(SpiceTabletInstance* sin, int wheel, 155*869564a9SGerd Hoffmann uint32_t buttons_state) 156*869564a9SGerd Hoffmann { 157*869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet); 158*869564a9SGerd Hoffmann 159*869564a9SGerd Hoffmann kbd_mouse_event(pointer->x, pointer->y, wheel, map_buttons(buttons_state)); 160*869564a9SGerd Hoffmann } 161*869564a9SGerd Hoffmann 162*869564a9SGerd Hoffmann static void tablet_buttons(SpiceTabletInstance *sin, 163*869564a9SGerd Hoffmann uint32_t buttons_state) 164*869564a9SGerd Hoffmann { 165*869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(sin, QemuSpicePointer, tablet); 166*869564a9SGerd Hoffmann 167*869564a9SGerd Hoffmann kbd_mouse_event(pointer->x, pointer->y, 0, map_buttons(buttons_state)); 168*869564a9SGerd Hoffmann } 169*869564a9SGerd Hoffmann 170*869564a9SGerd Hoffmann static const SpiceTabletInterface tablet_interface = { 171*869564a9SGerd Hoffmann .base.type = SPICE_INTERFACE_TABLET, 172*869564a9SGerd Hoffmann .base.description = "tablet", 173*869564a9SGerd Hoffmann .base.major_version = SPICE_INTERFACE_TABLET_MAJOR, 174*869564a9SGerd Hoffmann .base.minor_version = SPICE_INTERFACE_TABLET_MINOR, 175*869564a9SGerd Hoffmann .set_logical_size = tablet_set_logical_size, 176*869564a9SGerd Hoffmann .position = tablet_position, 177*869564a9SGerd Hoffmann .wheel = tablet_wheel, 178*869564a9SGerd Hoffmann .buttons = tablet_buttons, 179*869564a9SGerd Hoffmann }; 180*869564a9SGerd Hoffmann 181*869564a9SGerd Hoffmann static void mouse_mode_notifier(Notifier *notifier) 182*869564a9SGerd Hoffmann { 183*869564a9SGerd Hoffmann QemuSpicePointer *pointer = container_of(notifier, QemuSpicePointer, mouse_mode); 184*869564a9SGerd Hoffmann bool is_absolute = kbd_mouse_is_absolute(); 185*869564a9SGerd Hoffmann 186*869564a9SGerd Hoffmann if (pointer->absolute == is_absolute) { 187*869564a9SGerd Hoffmann return; 188*869564a9SGerd Hoffmann } 189*869564a9SGerd Hoffmann 190*869564a9SGerd Hoffmann if (is_absolute) { 191*869564a9SGerd Hoffmann qemu_spice_add_interface(&pointer->tablet.base); 192*869564a9SGerd Hoffmann } else { 193*869564a9SGerd Hoffmann spice_server_remove_interface(&pointer->tablet.base); 194*869564a9SGerd Hoffmann } 195*869564a9SGerd Hoffmann pointer->absolute = is_absolute; 196*869564a9SGerd Hoffmann } 197*869564a9SGerd Hoffmann 198864401c2SGerd Hoffmann void qemu_spice_input_init(void) 199864401c2SGerd Hoffmann { 200864401c2SGerd Hoffmann QemuSpiceKbd *kbd; 201*869564a9SGerd Hoffmann QemuSpicePointer *pointer; 202864401c2SGerd Hoffmann 203864401c2SGerd Hoffmann kbd = qemu_mallocz(sizeof(*kbd)); 204864401c2SGerd Hoffmann kbd->sin.base.sif = &kbd_interface.base; 205864401c2SGerd Hoffmann qemu_spice_add_interface(&kbd->sin.base); 206864401c2SGerd Hoffmann qemu_add_led_event_handler(kbd_leds, kbd); 20778dd9ac1SGerd Hoffmann 208*869564a9SGerd Hoffmann pointer = qemu_mallocz(sizeof(*pointer)); 209*869564a9SGerd Hoffmann pointer->mouse.base.sif = &mouse_interface.base; 210*869564a9SGerd Hoffmann pointer->tablet.base.sif = &tablet_interface.base; 211*869564a9SGerd Hoffmann qemu_spice_add_interface(&pointer->mouse.base); 212*869564a9SGerd Hoffmann 213*869564a9SGerd Hoffmann pointer->absolute = false; 214*869564a9SGerd Hoffmann pointer->mouse_mode.notify = mouse_mode_notifier; 215*869564a9SGerd Hoffmann qemu_add_mouse_mode_change_notifier(&pointer->mouse_mode); 216*869564a9SGerd Hoffmann mouse_mode_notifier(&pointer->mouse_mode); 217864401c2SGerd Hoffmann } 218