input: keyboard cleanups continued.
PATCH 4/5 input: switch ps/2 mouse to new input api
In reply to Gerd Hoffmann--- hw/input/ps2.c | 67 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 15 deletions(-)
hw/input/ps2.c +52 -15
@@ -368,28 +368,55 @@ static void ps2_mouse_send_packet(PS2MouseState *s) s->mouse_dz -= dz1; } -static void ps2_mouse_event (void *opaque, -int dx,int dy,int dz,int buttons_state) +static void ps2_mouse_event (DeviceState *dev,QemuConsole *src, +InputEvent *evt) { -PS2MouseState *s = opaque; +static const int bmap[INPUT_BUTTON_MAX ] = { + [INPUT_BUTTON_LEFT ] =MOUSE_EVENT_LBUTTON , + [INPUT_BUTTON_MIDDLE ] =MOUSE_EVENT_MBUTTON , + [INPUT_BUTTON_RIGHT ] =MOUSE_EVENT_RBUTTON , + }; +PS2MouseState *s = (PS2MouseState *)dev;/* check if deltas are recorded when disabled */ if (!(s->mouse_status &MOUSE_STATUS_ENABLED ))return ; - s->mouse_dx += dx; - s->mouse_dy -= dy; - s->mouse_dz += dz; -/* XXX: SDL sometimes generates nul events: we delete them */ -if (s->mouse_dx ==0 && s->mouse_dy ==0 && s->mouse_dz ==0 && - s->mouse_buttons == buttons_state) -return ; - s->mouse_buttons = buttons_state; +switch (evt->kind) { +case INPUT_EVENT_KIND_REL : +if (evt->rel->axis ==INPUT_AXIS_X ) { + s->mouse_dx += evt->rel->value; + }else if (evt->rel->axis ==INPUT_AXIS_Y ) { + s->mouse_dy -= evt->rel->value; + } +break ; -if (buttons_state) { -qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); +case INPUT_EVENT_KIND_BTN : +if (evt->btn->down) { + s->mouse_buttons |= bmap[evt->btn->button]; +if (evt->btn->button ==INPUT_BUTTON_WHEEL_UP ) { + s->mouse_dz--; + }else if (evt->btn->button ==INPUT_BUTTON_WHEEL_DOWN ) { + s->mouse_dz++; + } + }else { + s->mouse_buttons &= ~bmap[evt->btn->button]; + } +break ; + +default : +/* keep gcc happy */ +break ; } +} +static void ps2_mouse_sync (DeviceState *dev) +{ +PS2MouseState *s = (PS2MouseState *)dev; + +if (s->mouse_buttons) { +qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); + }if (!(s->mouse_status &MOUSE_STATUS_REMOTE ) && (s->common.queue.count < (PS2_QUEUE_SIZE -16 ))) {for (;;) {
@@ -404,7 +431,9 @@ static void ps2_mouse_event(void *opaque,void ps2_mouse_fake_event (void *opaque) { -ps2_mouse_event (opaque,1 ,0 ,0 ,0 ); +PS2MouseState *s = opaque; + s->mouse_dx++; +ps2_mouse_sync (opaque); }void ps2_write_mouse (void *opaque,int val)
@@ -686,6 +715,13 @@ void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)return s; } +static QemuInputHandler ps2_mouse_handler = { + .name ="QEMU PS/2 Mouse" , + .mask =INPUT_EVENT_MASK_BTN |INPUT_EVENT_MASK_REL , + .event = ps2_mouse_event, + .sync = ps2_mouse_sync, +}; +void *ps2_mouse_init (void (*update_irq)(void *,int ),void *update_arg) {PS2MouseState *s = (PS2MouseState *)g_malloc0 (sizeof (PS2MouseState));
@@ -693,7 +729,8 @@ void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg) s->common.update_irq = update_irq; s->common.update_arg = update_arg;vmstate_register (NULL ,0 , &vmstate_ps2_mouse, s); -qemu_add_mouse_event_handler (ps2_mouse_event, s,0 ,"QEMU PS/2 Mouse" ); +qemu_input_handler_register ((DeviceState *)s, + &ps2_mouse_handler);qemu_register_reset (ps2_mouse_reset, s);return s; }
Signature · 1 line
1.8.3.1
cover input: keyboard cleanups continued.
Hi,
Little patch series to continue input cleanups, for the most part keyboard related.
First patch adds helper functions to translate InputKeyEvents into other representations used in qemu today, to simplify switching over keyboard emulation backends to the new input API. With the new API things should be easier because the need to handle SCANCODE_EMUL0 & SCANCODE_UP in the backends goes away.
Third patch converts the ps/2 keyboard to the new API. Isn't that a great example as the emulation actually must pass scancodes to the guest so there isn't much of a simplification.
A much better example is patch #5 which implements a virtio keyboard (also mouse & tablet).
I've also tried to convert the hid code (used by usb-kbd). Nasty thing there is that it stores scancodes (the values it gets from old qemu input api, i.e. qemu implementation details) instead of hid keycodes (the values it passes to the guest) in the event queue. The event queue is vmstate data. Which makes the switch to the new API a bit difficuilt :(
cheers,
Gerd
Gerd Hoffmann (5):
input: key mapping helpers
input: add qemu_input_handler_deactivate
input: switch ps/2 kbd to new input api
input: switch ps/2 mouse to new input api
[RfC] virtio-input
docs/specs/virtio-input.txt | 50 ++++ hw/input/Makefile.objs | 4 + hw/input/ps2.c | 92 +++++-- hw/input/virtio-input.c | 559 +++++++++++++++++++++++++++++++++++++++ hw/virtio/virtio-pci.c | 88 ++++++ hw/virtio/virtio-pci.h | 18 ++ include/hw/pci/pci.h | 1 + include/hw/virtio/virtio-input.h | 71 +++++ include/ui/input.h | 5 + ui/Makefile.objs | 3 +- ui/input-keymap.c | 210 +++++++++++++++ ui/input-legacy.c | 181 +------------ ui/input.c | 7 + 13 files changed, 1098 insertions(+), 191 deletions(-) create mode 100644 docs/specs/virtio-input.txt create mode 100644 hw/input/virtio-input.c create mode 100644 include/hw/virtio/virtio-input.h create mode 100644 ui/input-keymap.c
Signature · 1 line
1.8.3.1
PATCH 2/5 input: add qemu_input_handler_deactivate
--- include/ui/input.h | 1 + ui/input.c | 7 +++++++ 2 files changed, 8 insertions(+)
include/ui/input.h +1 -0
@@ -27,6 +27,7 @@ struct QemuInputHandler {QemuInputHandlerState *qemu_input_handler_register (DeviceState *dev,QemuInputHandler *handler);void qemu_input_handler_activate (QemuInputHandlerState *s); +void qemu_input_handler_deactivate (QemuInputHandlerState *s);void qemu_input_handler_unregister (QemuInputHandlerState *s);void qemu_input_event_send (QemuConsole *src,InputEvent *evt);void qemu_input_event_sync (void );
ui/input.c +7 -0
@@ -39,6 +39,13 @@ void qemu_input_handler_activate(QemuInputHandlerState *s)qemu_input_check_mode_change (); } +void qemu_input_handler_deactivate (QemuInputHandlerState *s) +{ +QTAILQ_REMOVE (&handlers, s, node); +QTAILQ_INSERT_TAIL (&handlers, s, node); +qemu_input_check_mode_change (); +} +void qemu_input_handler_unregister (QemuInputHandlerState *s) {QTAILQ_REMOVE (&handlers, s, node);
Signature · 1 line
1.8.3.1
PATCH 3/5 input: switch ps/2 kbd to new input api
In reply to Gerd Hoffmann--- hw/input/ps2.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-)
hw/input/ps2.c +24 -1
@@ -24,6 +24,7 @@#include "hw/hw.h" #include "hw/input/ps2.h" #include "ui/console.h" +#include "ui/input.h" #include "sysemu/sysemu.h" /* debug PC keyboard */
@@ -170,6 +171,21 @@ static void ps2_put_keycode(void *opaque, int keycode)ps2_queue (&s->common, keycode); } +static void ps2_keyboard_event (DeviceState *dev,QemuConsole *src, +InputEvent *evt) +{ +PS2KbdState *s = (PS2KbdState *)dev; +int scancodes[3 ], i, count; + +qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); + count =qemu_input_key_value_to_scancode (evt->key->key, + evt->key->down, + scancodes); +for (i =0 ; i < count; i++) { +ps2_put_keycode (s, scancodes[i]); + } +} +uint32_t ps2_read_data (void *opaque) {PS2State *s = (PS2State *)opaque;
@@ -650,6 +666,12 @@ static const VMStateDescription vmstate_ps2_mouse = { } }; +static QemuInputHandler ps2_keyboard_handler = { + .name ="QEMU PS/2 Keyboard" , + .mask =INPUT_EVENT_MASK_KEY , + .event = ps2_keyboard_event, +}; +void *ps2_kbd_init (void (*update_irq)(void *,int ),void *update_arg) {PS2KbdState *s = (PS2KbdState *)g_malloc0 (sizeof (PS2KbdState));
@@ -658,7 +680,8 @@ void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg) s->common.update_arg = update_arg; s->scancode_set =2 ;vmstate_register (NULL ,0 , &vmstate_ps2_keyboard, s); -qemu_add_kbd_event_handler (ps2_put_keycode, s); +qemu_input_handler_register ((DeviceState *)s, + &ps2_keyboard_handler);qemu_register_reset (ps2_kbd_reset, s);return s; }
Signature · 1 line
1.8.3.1
RFC PATCH 5/5 virtio-input
In reply to Gerd HoffmannExperimental virtio protocol implementation for input devices.
See docs/specs/virtio-input.txt (+code) for more info. Detailed specs are to be written.
Guest bits: http://www.kraxel.org/cgit/linux/log/?h=virtio-input
Config isn't exactly small, so I tend to make that a pure virtio 1.0 device where virtio config space can live in mmio and doesn't waste io address space.
--- docs/specs/virtio-input.txt | 50 ++++ hw/input/Makefile.objs | 4 + hw/input/virtio-input.c | 559 +++++++++++++++++++++++++++++++++++++++ hw/virtio/virtio-pci.c | 88 ++++++ hw/virtio/virtio-pci.h | 18 ++ include/hw/pci/pci.h | 1 + include/hw/virtio/virtio-input.h | 71 +++++ 7 files changed, 791 insertions(+) create mode 100644 docs/specs/virtio-input.txt create mode 100644 hw/input/virtio-input.c create mode 100644 include/hw/virtio/virtio-input.h
docs/specs/virtio-input.txt new file +50 -0
@@ -0,0 +1,50 @@ + +virtio-input protocol +===================== + +TL;DR It is basically the linux input layer event API in virtio. +ioctls to query device capabilities map to config space. Events +travel via virtqueues. + + +config space +------------ + +Device config space (see include/hw/virtio/virtio-input.h, struct +virtio_input_config) has a four byte header. + + * First byte (select) can be written by the guest to pick the piece of + information it wants query. + + * Third byte (size) is read-only and returns the size of the + information. If there isn't any (for example when checking mouse + axis information on a virtual keyboard) size will be zero. + + * Second and fourth byte are reserved and should not be written by the + guest. + + +After the header the actual information is stored: + +VIRTIO_INPUT_CFG_ID_NAME -- u.id_name + name of the device + +VIRTIO_INPUT_CFG_FL_REPEAT -- u.flag + true if autorepeat should be enabled for the device + +VIRTIO_INPUT_CFG_EV_* -- u.ev_bits + bitmap for the supported event codes + +VIRTIO_INPUT_CFG_ABS_BASE -- u.abs + struct virtio_input_absinfo for each absolute axis. + select = VIRTIO_INPUT_CFG_ABS_BASE + ABS_$axis + + +virtqueues +========== + +One queue (#0) for input events (KEY, ABS, REL + friends). +One queue (#1) for status reports (LED). + +Both carry virtio_input_event structs. +#0 is host->guest, #1 is guest->host.
hw/input/Makefile.objs +4 -0
@@ -8,6 +8,10 @@ common-obj-$(CONFIG_STELLARIS_INPUT) += stellaris_input.o common-obj-$(CONFIG_TSC2005) += tsc2005.o common-obj-$(CONFIG_VMMOUSE) += vmmouse.o +ifeq ($(CONFIG_LINUX),y) +common-obj-$(CONFIG_VIRTIO) += virtio-input.o +endif + obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o obj-$(CONFIG_PXA2XX) += pxa2xx_keypad.o obj-$(CONFIG_TSC210X) += tsc210x.o
hw/input/virtio-input.c new file +559 -0
559 lines not on this page · raw message
hw/virtio/virtio-pci.c +88 -0
106 lines not on this page · raw message
hw/virtio/virtio-pci.h +18 -0
@@ -24,6 +24,7 @@#include "hw/virtio/virtio-balloon.h" #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-9p.h" +#include "hw/virtio/virtio-input.h" #ifdef CONFIG_VIRTFS #include "hw/9pfs/virtio-9p.h" #endif
@@ -39,6 +40,7 @@ typedef struct VirtIOSerialPCI VirtIOSerialPCI;typedef struct VirtIONetPCI VirtIONetPCI ;typedef struct VHostSCSIPCI VHostSCSIPCI ;typedef struct VirtIORngPCI VirtIORngPCI ; +typedef struct VirtIOInputPCI VirtIOInputPCI ;/* virtio-pci-bus */
@@ -199,6 +201,22 @@ struct VirtIORngPCI {VirtIORNG vdev; }; +/* +* virtio-keyboard-pci: This extends VirtioPCIProxy. +*/ +#define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci" +#define VIRTIO_INPUT_PCI (obj) \ + OBJECT_CHECK(VirtIOInputPCI, (obj), TYPE_VIRTIO_INPUT_PCI) + +#define TYPE_VIRTIO_KEYBOARD_PCI "virtio-keyboard-pci" +#define TYPE_VIRTIO_MOUSE_PCI "virtio-mouse-pci" +#define TYPE_VIRTIO_TABLET_PCI "virtio-tablet-pci" + +struct VirtIOInputPCI { +VirtIOPCIProxy parent_obj; +VirtIOInput vdev; +}; +/* Virtio ABI version, if we increment this, we break the guest driver. */ #define VIRTIO_PCI_ABI_VERSION 0
include/hw/pci/pci.h +1 -0
@@ -80,6 +80,7 @@#define PCI_DEVICE_ID_VIRTIO_SCSI 0x1004#define PCI_DEVICE_ID_VIRTIO_RNG 0x1005#define PCI_DEVICE_ID_VIRTIO_9P 0x1009 +#define PCI_DEVICE_ID_VIRTIO_INPUT 0x1021#define PCI_VENDOR_ID_REDHAT 0x1b36#define PCI_DEVICE_ID_REDHAT_BRIDGE 0x0001
include/hw/virtio/virtio-input.h new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 1/5 input: key mapping helpers
In reply to Gerd HoffmannAdd helper functions to translate KeyValue (qapi key representation) into other representations: traditional qemu key numbers, qapi key codes (Q_KEY_CODE_*) and scancode sequences.
--- include/ui/input.h | 4 + ui/Makefile.objs | 3 +- ui/input-keymap.c | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ui/input-legacy.c | 181 ++------------------------------------------- 4 files changed, 223 insertions(+), 175 deletions(-) create mode 100644 ui/input-keymap.c
include/ui/input.h +4 -0
@@ -35,6 +35,10 @@ InputEvent *qemu_input_event_new_key(KeyValue *key, bool down);void qemu_input_event_send_key (QemuConsole *src,KeyValue *key,bool down);void qemu_input_event_send_key_number (QemuConsole *src,int num,bool down);void qemu_input_event_send_key_qcode (QemuConsole *src,QKeyCode q,bool down); +int qemu_input_key_value_to_number (const KeyValue *value); +int qemu_input_key_value_to_qcode (const KeyValue *value); +int qemu_input_key_value_to_scancode (const KeyValue *value,bool down, +int *codes);InputEvent *qemu_input_event_new_btn (InputButton btn,bool down);void qemu_input_queue_btn (QemuConsole *src,InputButton btn,bool down);
ui/Makefile.objs +2 -1
@@ -7,7 +7,8 @@ vnc-obj-$(CONFIG_VNC_SASL) += vnc-auth-sasl.o vnc-obj-$(CONFIG_VNC_WS) += vnc-ws.o vnc-obj-y += vnc-jobs.o -common-obj-y += keymaps.o console.o cursor.o input.o input-legacy.o qemu-pixman.o +common-obj-y += keymaps.o console.o cursor.o qemu-pixman.o +common-obj-y += input.o input-keymap.o input-legacy.o common-obj-$(CONFIG_SPICE) += spice-core.o spice-input.o spice-display.o common-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o sdl2.o common-obj-$(CONFIG_COCOA) += cocoa.o
ui/input-keymap.c new file +210 -0
210 lines not on this page · raw message
ui/input-legacy.c +7 -174
210 lines not on this page · raw message
Signature · 1 line
1.8.3.1