From 38dd06aab7989f138d22cbb4f6ea03e6c18b60ae Mon Sep 17 00:00:00 2001
From: Luiz Capitulino <lcapitulino@redhat.com>
Date: Tue, 6 Mar 2012 00:40:09 +0100
Subject: [PATCH 11/11] qmp: add DEVICE_TRAY_MOVED event

RH-Author: Luiz Capitulino <lcapitulino@redhat.com>
Message-id: <20120305214009.550f70cb@doriath.home>
Patchwork-id: 38229
O-Subject: [PATCH v2 5/5] qmp: add DEVICE_TRAY_MOVED event
Bugzilla: 575159
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>

It's emitted whenever the tray is moved by the guest or by HMP/QMP
commands.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Acked-by: Kevin Wolf <kwolf@redhat.com>
(cherry picked from commit 6f382ed226f3a408825d882e6c4b47499baaa15b)

Conflicts:

	monitor.c
	monitor.h

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---

This is a rebase of 5/5 only, this is done on top of rhel6/master so that
the series is not held by Anthony's 50+ patch series.

Michal, please supersede the patch contents of 5/5. The other patches
don't change.

 QMP/qmp-events.txt |   18 ++++++++++++++++++
 block.c            |   24 ++++++++++++++++++++++++
 monitor.c          |    3 +++
 monitor.h          |    1 +
 4 files changed, 46 insertions(+)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 QMP/qmp-events.txt |   18 ++++++++++++++++++
 block.c            |   24 ++++++++++++++++++++++++
 monitor.c          |    3 +++
 monitor.h          |    1 +
 4 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/QMP/qmp-events.txt b/QMP/qmp-events.txt
index 4f78efe..42f6992 100644
--- a/QMP/qmp-events.txt
+++ b/QMP/qmp-events.txt
@@ -41,6 +41,24 @@ Example:
 Note: If action is "stop", a STOP event will eventually follow the
 BLOCK_IO_ERROR event.
 
+DEVICE_TRAY_MOVED
+-----------------
+
+It's emitted whenever the tray of a removable device is moved by the guest
+or by HMP/QMP commands.
+
+Data:
+
+- "device": device name (json-string)
+- "tray-open": true if the tray has been opened or false if it has been closed
+               (json-bool)
+
+{ "event": "DEVICE_TRAY_MOVED",
+  "data": { "device": "ide1-cd0",
+            "tray-open": true
+  },
+  "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
+
 RESET
 -----
 
diff --git a/block.c b/block.c
index 3d1d9f1..899b026 100644
--- a/block.c
+++ b/block.c
@@ -835,10 +835,30 @@ void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
     qobject_decref(data);
 }
 
+static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected)
+{
+    QObject *data;
+
+    data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }",
+                              bdrv_get_device_name(bs), ejected);
+    monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data);
+
+    qobject_decref(data);
+}
+
 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
 {
     if (bs->dev_ops && bs->dev_ops->change_media_cb) {
+        bool tray_was_closed = !bdrv_dev_is_tray_open(bs);
         bs->dev_ops->change_media_cb(bs->dev_opaque, load);
+        if (tray_was_closed) {
+            /* tray open */
+            bdrv_emit_qmp_eject_event(bs, true);
+        }
+        if (load) {
+            /* tray close */
+            bdrv_emit_qmp_eject_event(bs, false);
+        }
     }
 }
 
@@ -2909,6 +2929,10 @@ void bdrv_eject(BlockDriverState *bs, bool eject_flag)
     if (drv && drv->bdrv_eject) {
         drv->bdrv_eject(bs, eject_flag);
     }
+
+    if (bs->device_name[0] != '\0') {
+        bdrv_emit_qmp_eject_event(bs, eject_flag);
+    }
 }
 
 /**
diff --git a/monitor.c b/monitor.c
index 8b9d3b8..49a395a 100644
--- a/monitor.c
+++ b/monitor.c
@@ -465,6 +465,9 @@ void monitor_protocol_event(MonitorEvent event, QObject *data)
         case QEVENT_SPICE_DISCONNECTED:
             event_name = "SPICE_DISCONNECTED";
             break;
+        case QEVENT_DEVICE_TRAY_MOVED:
+             event_name = "DEVICE_TRAY_MOVED";
+            break;
         case QEVENT_RH_SPICE_INITIALIZED:
             event_name = RFQDN_REDHAT "SPICE_INITIALIZED";
             break;
diff --git a/monitor.h b/monitor.h
index 8d414ef..8598a4a 100644
--- a/monitor.h
+++ b/monitor.h
@@ -34,6 +34,7 @@ typedef enum MonitorEvent {
     QEVENT_SPICE_CONNECTED,
     QEVENT_SPICE_INITIALIZED,
     QEVENT_SPICE_DISCONNECTED,
+    QEVENT_DEVICE_TRAY_MOVED,
     QEVENT_RH_SPICE_INITIALIZED,
     QEVENT_RH_SPICE_DISCONNECTED,
     QEVENT_BLOCK_JOB_COMPLETED,
-- 
1.7.7.6