Re: [Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-24 Thread Anthony Liguori

On 02/23/2012 08:08 AM, Markus Armbruster wrote:

Luiz Capitulinolcapitul...@redhat.com  writes:


On Thu, 23 Feb 2012 08:50:08 +0100
Markus Armbrusterarm...@redhat.com  wrote:


Kevin Wolfkw...@redhat.com  writes:


Am 17.02.2012 20:21, schrieb Luiz Capitulino:

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

Signed-off-by: Luiz Capitulinolcapitul...@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 06cb404..9286af5 100644
--- a/QMP/qmp-events.txt
+++ b/QMP/qmp-events.txt
@@ -26,6 +26,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)


For me, a device name is something related to qdev. 'device' is a
misnomer consistently used in all QMP commands so far and we can't fix
it any more, but at least the documentation should clarify what is meant
(that's for a follow-up patch).


We can fix it if we really want to: rename, then add the old name as
alias for backward compatibility.  Pick your favourite flavor of cruft.


I like it, new events won't have the cruft.


If we reserve device for device models, we need sensible names for
device backends.  One each for block, net and char.  There's some
precedence for blockdev, netdev, chardev, but they contain dev,
so there's still some overloading of the name device.  Better ideas?


For 1.2 (when QOM is considered stable), this should become pathname.  Given a 
path, a client can determine what the type of the object is (by reading the type 
property).


In fact, I'd like to see all events have a pathname of origin.  This would 
probably become part of the QMP protocol itself.


This gives us a mechanism to subscribe to events from specific objects.

Regards,

Anthony Liguori



Re: [Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-24 Thread Luiz Capitulino
On Fri, 24 Feb 2012 10:01:00 -0600
Anthony Liguori anth...@codemonkey.ws wrote:

 On 02/23/2012 08:08 AM, Markus Armbruster wrote:
  Luiz Capitulinolcapitul...@redhat.com  writes:
 
  On Thu, 23 Feb 2012 08:50:08 +0100
  Markus Armbrusterarm...@redhat.com  wrote:
 
  Kevin Wolfkw...@redhat.com  writes:
 
  Am 17.02.2012 20:21, schrieb Luiz Capitulino:
  It's emitted whenever the tray is moved by the guest or by HMP/QMP
  commands.
 
  Signed-off-by: Luiz Capitulinolcapitul...@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 06cb404..9286af5 100644
  --- a/QMP/qmp-events.txt
  +++ b/QMP/qmp-events.txt
  @@ -26,6 +26,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)
 
  For me, a device name is something related to qdev. 'device' is a
  misnomer consistently used in all QMP commands so far and we can't fix
  it any more, but at least the documentation should clarify what is meant
  (that's for a follow-up patch).
 
  We can fix it if we really want to: rename, then add the old name as
  alias for backward compatibility.  Pick your favourite flavor of cruft.
 
  I like it, new events won't have the cruft.
 
  If we reserve device for device models, we need sensible names for
  device backends.  One each for block, net and char.  There's some
  precedence for blockdev, netdev, chardev, but they contain dev,
  so there's still some overloading of the name device.  Better ideas?
 
 For 1.2 (when QOM is considered stable), this should become pathname.  Given 
 a 
 path, a client can determine what the type of the object is (by reading the 
 type 
 property).
 
 In fact, I'd like to see all events have a pathname of origin.  This would 
 probably become part of the QMP protocol itself.
 
 This gives us a mechanism to subscribe to events from specific objects.

This is 1.2 material, right?

I'm asking because the conversion of events to the qapi is not too far away,
but I think that using QOM will somewhat deprecate the code you have in the
glib branch (besides having to wait for 1.2)?



Re: [Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-24 Thread Anthony Liguori

On 02/24/2012 10:39 AM, Luiz Capitulino wrote:

On Fri, 24 Feb 2012 10:01:00 -0600
Anthony Liguorianth...@codemonkey.ws  wrote:


On 02/23/2012 08:08 AM, Markus Armbruster wrote:

Luiz Capitulinolcapitul...@redhat.com   writes:


On Thu, 23 Feb 2012 08:50:08 +0100
Markus Armbrusterarm...@redhat.com   wrote:


Kevin Wolfkw...@redhat.com   writes:


Am 17.02.2012 20:21, schrieb Luiz Capitulino:

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

Signed-off-by: Luiz Capitulinolcapitul...@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 06cb404..9286af5 100644
--- a/QMP/qmp-events.txt
+++ b/QMP/qmp-events.txt
@@ -26,6 +26,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)


For me, a device name is something related to qdev. 'device' is a
misnomer consistently used in all QMP commands so far and we can't fix
it any more, but at least the documentation should clarify what is meant
(that's for a follow-up patch).


We can fix it if we really want to: rename, then add the old name as
alias for backward compatibility.  Pick your favourite flavor of cruft.


I like it, new events won't have the cruft.


If we reserve device for device models, we need sensible names for
device backends.  One each for block, net and char.  There's some
precedence for blockdev, netdev, chardev, but they contain dev,
so there's still some overloading of the name device.  Better ideas?


For 1.2 (when QOM is considered stable), this should become pathname.  Given a
path, a client can determine what the type of the object is (by reading the type
property).

In fact, I'd like to see all events have a pathname of origin.  This would
probably become part of the QMP protocol itself.

This gives us a mechanism to subscribe to events from specific objects.


This is 1.2 material, right?


Yes.


I'm asking because the conversion of events to the qapi is not too far away,
but I think that using QOM will somewhat deprecate the code you have in the
glib branch (besides having to wait for 1.2)?


I have some vague ideas about what to do here.  One thought would be to have a 
standard notifier mechanism in Object that was advertised as a property type. 
We could then provide an interface via QMP to [un]subscribe to a notifier property.


I won't get to this until the 1.2 time frame though.  My goals for 1.1 are to 
get qbus conversions merged and refactor IRQs/MemoryRegions to use QOM.  If time 
permits, also refactor the PC to better use QOM.


If someone wants to tackle events in QOM, I'd be happy to provide some 
suggestions on where to start.


Regards,

Anthony Liguori

Regards,

Anthony Liguori



Re: [Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-24 Thread Luiz Capitulino
On Fri, 24 Feb 2012 10:44:11 -0600
Anthony Liguori anth...@codemonkey.ws wrote:

  I'm asking because the conversion of events to the qapi is not too far away,
  but I think that using QOM will somewhat deprecate the code you have in the
  glib branch (besides having to wait for 1.2)?
 
 I have some vague ideas about what to do here.  One thought would be to have 
 a 
 standard notifier mechanism in Object that was advertised as a property type. 
 We could then provide an interface via QMP to [un]subscribe to a notifier 
 property.

This seems to be a good match with your previous ideas, implemented in the glib
branch. But then subsystems/devices emitting events will have to be converted
to QOM first...

 I won't get to this until the 1.2 time frame though.  My goals for 1.1 are to 
 get qbus conversions merged and refactor IRQs/MemoryRegions to use QOM.  If 
 time 
 permits, also refactor the PC to better use QOM.
 
 If someone wants to tackle events in QOM, I'd be happy to provide some 
 suggestions on where to start.

I'd like to hear about it, but I'm not sure when I'll start working on it.



Re: [Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-23 Thread Luiz Capitulino
On Thu, 23 Feb 2012 08:50:08 +0100
Markus Armbruster arm...@redhat.com wrote:

 Kevin Wolf kw...@redhat.com writes:
 
  Am 17.02.2012 20:21, schrieb Luiz Capitulino:
  It's emitted whenever the tray is moved by the guest or by HMP/QMP
  commands.
  
  Signed-off-by: Luiz Capitulino lcapitul...@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 06cb404..9286af5 100644
  --- a/QMP/qmp-events.txt
  +++ b/QMP/qmp-events.txt
  @@ -26,6 +26,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)
 
  For me, a device name is something related to qdev. 'device' is a
  misnomer consistently used in all QMP commands so far and we can't fix
  it any more, but at least the documentation should clarify what is meant
  (that's for a follow-up patch).
 
 We can fix it if we really want to: rename, then add the old name as
 alias for backward compatibility.  Pick your favourite flavor of cruft.

I like it, new events won't have the cruft.



Re: [Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-23 Thread Markus Armbruster
Luiz Capitulino lcapitul...@redhat.com writes:

 On Thu, 23 Feb 2012 08:50:08 +0100
 Markus Armbruster arm...@redhat.com wrote:

 Kevin Wolf kw...@redhat.com writes:
 
  Am 17.02.2012 20:21, schrieb Luiz Capitulino:
  It's emitted whenever the tray is moved by the guest or by HMP/QMP
  commands.
  
  Signed-off-by: Luiz Capitulino lcapitul...@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 06cb404..9286af5 100644
  --- a/QMP/qmp-events.txt
  +++ b/QMP/qmp-events.txt
  @@ -26,6 +26,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)
 
  For me, a device name is something related to qdev. 'device' is a
  misnomer consistently used in all QMP commands so far and we can't fix
  it any more, but at least the documentation should clarify what is meant
  (that's for a follow-up patch).
 
 We can fix it if we really want to: rename, then add the old name as
 alias for backward compatibility.  Pick your favourite flavor of cruft.

 I like it, new events won't have the cruft.

If we reserve device for device models, we need sensible names for
device backends.  One each for block, net and char.  There's some
precedence for blockdev, netdev, chardev, but they contain dev,
so there's still some overloading of the name device.  Better ideas?



[Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-23 Thread Luiz Capitulino
It's emitted whenever the tray is moved by the guest or by HMP/QMP
commands.

Signed-off-by: Luiz Capitulino lcapitul...@redhat.com
Reviewed-by: Markus Armbruster arm...@redhat.com
Acked-by: Kevin Wolf kw...@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 06cb404..9286af5 100644
--- a/QMP/qmp-events.txt
+++ b/QMP/qmp-events.txt
@@ -26,6 +26,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 bfb0dec..e27d528 100644
--- a/block.c
+++ b/block.c
@@ -972,10 +972,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);
+}
 }
 }
 
@@ -3616,6 +3636,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 0d4daad..03beab3 100644
--- a/monitor.c
+++ b/monitor.c
@@ -485,6 +485,9 @@ void monitor_protocol_event(MonitorEvent event, QObject 
*data)
 case QEVENT_BLOCK_JOB_CANCELLED:
 event_name = BLOCK_JOB_CANCELLED;
 break;
+case QEVENT_DEVICE_TRAY_MOVED:
+ event_name = DEVICE_TRAY_MOVED;
+break;
 default:
 abort();
 break;
diff --git a/monitor.h b/monitor.h
index b72ea07..b6f700f 100644
--- a/monitor.h
+++ b/monitor.h
@@ -38,6 +38,7 @@ typedef enum MonitorEvent {
 QEVENT_SPICE_DISCONNECTED,
 QEVENT_BLOCK_JOB_COMPLETED,
 QEVENT_BLOCK_JOB_CANCELLED,
+QEVENT_DEVICE_TRAY_MOVED,
 QEVENT_MAX,
 } MonitorEvent;
 
-- 
1.7.9.111.gf3fb0.dirty




Re: [Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-22 Thread Kevin Wolf
Am 17.02.2012 20:21, schrieb Luiz Capitulino:
 It's emitted whenever the tray is moved by the guest or by HMP/QMP
 commands.
 
 Signed-off-by: Luiz Capitulino lcapitul...@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 06cb404..9286af5 100644
 --- a/QMP/qmp-events.txt
 +++ b/QMP/qmp-events.txt
 @@ -26,6 +26,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)

For me, a device name is something related to qdev. 'device' is a
misnomer consistently used in all QMP commands so far and we can't fix
it any more, but at least the documentation should clarify what is meant
(that's for a follow-up patch).

Kevin



Re: [Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-22 Thread Markus Armbruster
Kevin Wolf kw...@redhat.com writes:

 Am 17.02.2012 20:21, schrieb Luiz Capitulino:
 It's emitted whenever the tray is moved by the guest or by HMP/QMP
 commands.
 
 Signed-off-by: Luiz Capitulino lcapitul...@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 06cb404..9286af5 100644
 --- a/QMP/qmp-events.txt
 +++ b/QMP/qmp-events.txt
 @@ -26,6 +26,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)

 For me, a device name is something related to qdev. 'device' is a
 misnomer consistently used in all QMP commands so far and we can't fix
 it any more, but at least the documentation should clarify what is meant
 (that's for a follow-up patch).

We can fix it if we really want to: rename, then add the old name as
alias for backward compatibility.  Pick your favourite flavor of cruft.



Re: [Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-20 Thread Markus Armbruster
Luiz Capitulino lcapitul...@redhat.com writes:

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

 Signed-off-by: Luiz Capitulino lcapitul...@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 06cb404..9286af5 100644
 --- a/QMP/qmp-events.txt
 +++ b/QMP/qmp-events.txt
 @@ -26,6 +26,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 bfb0dec..e27d528 100644
 --- a/block.c
 +++ b/block.c
 @@ -972,10 +972,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);
 +}
  }
  }
  

This one's subtle.  I discussed why it works in my review of v2, but
I'll be hanged if I remember now.  I'd ask for a comment, if I hadn't
already asked for replacing the BlockDevOps method by one that's easier
to use, in a followup patch.

 @@ -3616,6 +3636,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);
 +}

Condition needed when we stack raw on top of another driver.
raw_eject() uses bdrv_eject() to pass down the call.

Only the topmost driver (raw in this case) has a non-empty
device_name.

Perhaps worth a comment.

  }
  
  /**
 diff --git a/monitor.c b/monitor.c
 index aadbdcb..38d876e 100644
 --- a/monitor.c
 +++ b/monitor.c
 @@ -485,6 +485,9 @@ void monitor_protocol_event(MonitorEvent event, QObject 
 *data)
  case QEVENT_BLOCK_JOB_CANCELLED:
  event_name = BLOCK_JOB_CANCELLED;
  break;
 +case QEVENT_DEVICE_TRAY_MOVED:
 + event_name = DEVICE_TRAY_MOVED;
 +break;
  default:
  abort();
  break;
 diff --git a/monitor.h b/monitor.h
 index b72ea07..b6f700f 100644
 --- a/monitor.h
 +++ b/monitor.h
 @@ -38,6 +38,7 @@ typedef enum MonitorEvent {
  QEVENT_SPICE_DISCONNECTED,
  QEVENT_BLOCK_JOB_COMPLETED,
  QEVENT_BLOCK_JOB_CANCELLED,
 +QEVENT_DEVICE_TRAY_MOVED,
  QEVENT_MAX,
  } MonitorEvent;



[Qemu-devel] [PATCH 5/5] qmp: add DEVICE_TRAY_MOVED event

2012-02-17 Thread Luiz Capitulino
It's emitted whenever the tray is moved by the guest or by HMP/QMP
commands.

Signed-off-by: Luiz Capitulino lcapitul...@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 06cb404..9286af5 100644
--- a/QMP/qmp-events.txt
+++ b/QMP/qmp-events.txt
@@ -26,6 +26,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 bfb0dec..e27d528 100644
--- a/block.c
+++ b/block.c
@@ -972,10 +972,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);
+}
 }
 }
 
@@ -3616,6 +3636,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 aadbdcb..38d876e 100644
--- a/monitor.c
+++ b/monitor.c
@@ -485,6 +485,9 @@ void monitor_protocol_event(MonitorEvent event, QObject 
*data)
 case QEVENT_BLOCK_JOB_CANCELLED:
 event_name = BLOCK_JOB_CANCELLED;
 break;
+case QEVENT_DEVICE_TRAY_MOVED:
+ event_name = DEVICE_TRAY_MOVED;
+break;
 default:
 abort();
 break;
diff --git a/monitor.h b/monitor.h
index b72ea07..b6f700f 100644
--- a/monitor.h
+++ b/monitor.h
@@ -38,6 +38,7 @@ typedef enum MonitorEvent {
 QEVENT_SPICE_DISCONNECTED,
 QEVENT_BLOCK_JOB_COMPLETED,
 QEVENT_BLOCK_JOB_CANCELLED,
+QEVENT_DEVICE_TRAY_MOVED,
 QEVENT_MAX,
 } MonitorEvent;
 
-- 
1.7.9.111.gf3fb0.dirty