From: Marc-André Lureau <marcandre.lur...@redhat.com>

Learn a new async command to make a correct screendump.

Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=1230527

Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com>
---
 qapi-schema.json | 21 +++++++++++++++++++-
 ui/console.c     | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+), 1 deletion(-)

diff --git a/qapi-schema.json b/qapi-schema.json
index 99c986d..cf2bd3a 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -3899,7 +3899,7 @@
 ##
 # @screendump:
 #
-# Write a PPM of the VGA screen to a file.
+# Write a PPM of the first console to a file.
 #
 # @filename: the path of a new PPM file to store the image
 #
@@ -3916,6 +3916,25 @@
 { 'command': 'screendump', 'data': {'filename': 'str'} }
 
 ##
+# @screendump-async:
+#
+# Write a PPM of the first console to a file.
+#
+# @filename: the path of a new PPM file to store the image
+#
+# Returns: Nothing on success
+#
+# Since: 2.5
+#
+# Example:
+#
+# -> { "execute": "screendump-async", "arguments": { "filename": "/tmp/image" 
}, id: X }
+# <- { "return": {}, id: X }
+#
+##
+{ 'command': 'screendump-async', 'data': {'filename': 'str'}, 'async': true }
+
+##
 # @ChardevFile:
 #
 # Configuration info for file chardevs.
diff --git a/ui/console.c b/ui/console.c
index 1f800ef..5b4d1e8 100644
--- a/ui/console.c
+++ b/ui/console.c
@@ -113,6 +113,12 @@ typedef enum {
     TEXT_CONSOLE_FIXED_SIZE
 } console_type_t;
 
+struct qmp_screendump {
+    gchar *filename;
+    QmpReturn *ret;
+    QLIST_ENTRY(qmp_screendump) link;
+};
+
 struct QemuConsole {
     Object parent;
 
@@ -159,6 +165,8 @@ struct QemuConsole {
     QEMUFIFO out_fifo;
     uint8_t out_fifo_buf[16];
     QEMUTimer *kbd_timer;
+
+    QLIST_HEAD(, qmp_screendump) qmp_screendumps;
 };
 
 struct DisplayState {
@@ -184,6 +192,8 @@ static void dpy_refresh(DisplayState *s);
 static DisplayState *get_alloc_displaystate(void);
 static void text_console_update_cursor_timer(void);
 static void text_console_update_cursor(void *opaque);
+static void ppm_save(const char *filename, DisplaySurface *ds,
+                     Error **errp);
 
 static void gui_update(void *opaque)
 {
@@ -250,8 +260,33 @@ static void gui_setup_refresh(DisplayState *ds)
     ds->have_text = have_text;
 }
 
+static void qmp_screendump_return(QemuConsole *con, const char *filename,
+                                  QmpReturn *ret)
+{
+    Error *err = NULL;
+    DisplaySurface *surface;
+
+    surface = qemu_console_surface(con);
+
+    ppm_save(filename, surface, &err);
+
+    if (err) {
+        qmp_return_error(ret, err);
+    } else {
+        qmp_screendump_async_return(ret);
+    }
+}
+
 void graphic_hw_update_done(QemuConsole *con)
 {
+    struct qmp_screendump *dump, *next;
+
+    QLIST_FOREACH_SAFE(dump, &con->qmp_screendumps, link, next) {
+        qmp_screendump_return(con, dump->filename, dump->ret);
+        g_free(dump->filename);
+        QLIST_REMOVE(dump, link);
+        g_free(dump);
+    }
 }
 
 bool graphic_hw_update(QemuConsole *con)
@@ -347,6 +382,29 @@ void qmp_screendump(const char *filename, Error **errp)
     ppm_save(filename, surface, errp);
 }
 
+void qmp_screendump_async(const char *filename, QmpReturn *qret)
+{
+    QemuConsole *con = qemu_console_lookup_by_index(0);
+    bool async;
+    Error *err;
+
+    if (con == NULL) {
+        error_setg(&err, "There is no QemuConsole I can screendump from.");
+        qmp_return_error(qret, err);
+        return;
+    }
+
+    async = graphic_hw_update(con);
+    if (async) {
+        struct qmp_screendump *dump = g_new(struct qmp_screendump, 1);
+        dump->filename = g_strdup(filename);
+        dump->ret = qret;
+        QLIST_INSERT_HEAD(&con->qmp_screendumps, dump, link);
+    } else {
+        qmp_screendump_return(con, filename, qret);
+    }
+}
+
 void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
 {
     if (!con) {
@@ -1206,6 +1264,7 @@ static QemuConsole *new_console(DisplayState *ds, 
console_type_t console_type,
     obj = object_new(TYPE_QEMU_CONSOLE);
     s = QEMU_CONSOLE(obj);
     s->head = head;
+    QLIST_INIT(&s->qmp_screendumps);
     object_property_add_link(obj, "device", TYPE_DEVICE,
                              (Object **)&s->device,
                              object_property_allow_set_link,
-- 
2.4.3


Reply via email to