Track the input handled state, and dispose it on unrealize.

Also free some allocated fields during unrealize.

Signed-off-by: Marc-André Lureau <[email protected]>
---
 include/hw/input/stellaris_gamepad.h |  2 ++
 hw/input/stellaris_gamepad.c         | 12 +++++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/include/hw/input/stellaris_gamepad.h 
b/include/hw/input/stellaris_gamepad.h
index e011482646f..c1069735010 100644
--- a/include/hw/input/stellaris_gamepad.h
+++ b/include/hw/input/stellaris_gamepad.h
@@ -13,6 +13,7 @@
 
 #include "hw/core/sysbus.h"
 #include "qom/object.h"
+#include "ui/input.h"
 
 /*
  * QEMU interface:
@@ -28,6 +29,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(StellarisGamepad, 
STELLARIS_GAMEPAD)
 struct StellarisGamepad {
     SysBusDevice parent_obj;
 
+    QemuInputHandlerState *hs;
     uint32_t num_buttons;
     qemu_irq *irqs;
     uint32_t *keycodes;
diff --git a/hw/input/stellaris_gamepad.c b/hw/input/stellaris_gamepad.c
index 25769fc2616..376d91abad7 100644
--- a/hw/input/stellaris_gamepad.c
+++ b/hw/input/stellaris_gamepad.c
@@ -59,7 +59,16 @@ static void stellaris_gamepad_realize(DeviceState *dev, 
Error **errp)
     s->irqs = g_new0(qemu_irq, s->num_buttons);
     s->pressed = g_new0(uint8_t, s->num_buttons);
     qdev_init_gpio_out(dev, s->irqs, s->num_buttons);
-    qemu_input_handler_register(dev, &stellaris_gamepad_handler);
+    s->hs = qemu_input_handler_register(dev, &stellaris_gamepad_handler);
+}
+
+static void stellaris_gamepad_unrealize(DeviceState *dev)
+{
+    StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
+
+    g_clear_pointer(&s->irqs, g_free);
+    g_clear_pointer(&s->pressed, g_free);
+    g_clear_pointer(&s->hs, qemu_input_handler_unregister);
 }
 
 static void stellaris_gamepad_reset_enter(Object *obj, ResetType type)
@@ -81,6 +90,7 @@ static void stellaris_gamepad_class_init(ObjectClass *klass, 
const void *data)
 
     rc->phases.enter = stellaris_gamepad_reset_enter;
     dc->realize = stellaris_gamepad_realize;
+    dc->unrealize = stellaris_gamepad_unrealize;
     dc->vmsd = &vmstate_stellaris_gamepad;
     device_class_set_props(dc, stellaris_gamepad_properties);
 }

-- 
2.54.0


Reply via email to