19949b06eSMarkus Armbruster /*
29949b06eSMarkus Armbruster * QMP commands related to UI
39949b06eSMarkus Armbruster *
49949b06eSMarkus Armbruster * Copyright IBM, Corp. 2011
59949b06eSMarkus Armbruster *
69949b06eSMarkus Armbruster * Authors:
79949b06eSMarkus Armbruster * Anthony Liguori <aliguori@us.ibm.com>
89949b06eSMarkus Armbruster *
99949b06eSMarkus Armbruster * This work is licensed under the terms of the GNU GPL, version 2. See
109949b06eSMarkus Armbruster * the COPYING file in the top-level directory.
119949b06eSMarkus Armbruster *
129949b06eSMarkus Armbruster * Contributions after 2012-01-13 are licensed under the terms of the
139949b06eSMarkus Armbruster * GNU GPL, version 2 or (at your option) any later version.
149949b06eSMarkus Armbruster */
159949b06eSMarkus Armbruster
169949b06eSMarkus Armbruster #include "qemu/osdep.h"
174f2c765bSMarc-André Lureau
184f2c765bSMarc-André Lureau #include "io/channel-file.h"
193125af29SMarkus Armbruster #include "monitor/qmp-helpers.h"
209949b06eSMarkus Armbruster #include "qapi/qapi-commands-ui.h"
219949b06eSMarkus Armbruster #include "qapi/qmp/qerror.h"
224f2c765bSMarc-André Lureau #include "qemu/coroutine.h"
239949b06eSMarkus Armbruster #include "qemu/cutils.h"
244f2c765bSMarc-André Lureau #include "trace.h"
259949b06eSMarkus Armbruster #include "ui/console.h"
263125af29SMarkus Armbruster #include "ui/dbus-display.h"
279949b06eSMarkus Armbruster #include "ui/qemu-spice.h"
284f2c765bSMarc-André Lureau #ifdef CONFIG_PNG
294f2c765bSMarc-André Lureau #include <png.h>
304f2c765bSMarc-André Lureau #endif
319949b06eSMarkus Armbruster
qmp_set_password(SetPasswordOptions * opts,Error ** errp)329949b06eSMarkus Armbruster void qmp_set_password(SetPasswordOptions *opts, Error **errp)
339949b06eSMarkus Armbruster {
349949b06eSMarkus Armbruster int rc;
359949b06eSMarkus Armbruster
369949b06eSMarkus Armbruster if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
379949b06eSMarkus Armbruster if (!qemu_using_spice(errp)) {
389949b06eSMarkus Armbruster return;
399949b06eSMarkus Armbruster }
409949b06eSMarkus Armbruster rc = qemu_spice.set_passwd(opts->password,
419949b06eSMarkus Armbruster opts->connected == SET_PASSWORD_ACTION_FAIL,
429949b06eSMarkus Armbruster opts->connected == SET_PASSWORD_ACTION_DISCONNECT);
439949b06eSMarkus Armbruster } else {
449949b06eSMarkus Armbruster assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
459949b06eSMarkus Armbruster if (opts->connected != SET_PASSWORD_ACTION_KEEP) {
469949b06eSMarkus Armbruster /* vnc supports "connected=keep" only */
47*517b0220SMarkus Armbruster error_setg(errp, "parameter 'connected' must be 'keep'"
48*517b0220SMarkus Armbruster " when 'protocol' is 'vnc'");
499949b06eSMarkus Armbruster return;
509949b06eSMarkus Armbruster }
519949b06eSMarkus Armbruster /*
529949b06eSMarkus Armbruster * Note that setting an empty password will not disable login
539949b06eSMarkus Armbruster * through this interface.
549949b06eSMarkus Armbruster */
559949b06eSMarkus Armbruster rc = vnc_display_password(opts->u.vnc.display, opts->password);
569949b06eSMarkus Armbruster }
579949b06eSMarkus Armbruster
589949b06eSMarkus Armbruster if (rc != 0) {
599949b06eSMarkus Armbruster error_setg(errp, "Could not set password");
609949b06eSMarkus Armbruster }
619949b06eSMarkus Armbruster }
629949b06eSMarkus Armbruster
qmp_expire_password(ExpirePasswordOptions * opts,Error ** errp)639949b06eSMarkus Armbruster void qmp_expire_password(ExpirePasswordOptions *opts, Error **errp)
649949b06eSMarkus Armbruster {
659949b06eSMarkus Armbruster time_t when;
669949b06eSMarkus Armbruster int rc;
679949b06eSMarkus Armbruster const char *whenstr = opts->time;
689949b06eSMarkus Armbruster const char *numstr = NULL;
699949b06eSMarkus Armbruster uint64_t num;
709949b06eSMarkus Armbruster
719949b06eSMarkus Armbruster if (strcmp(whenstr, "now") == 0) {
729949b06eSMarkus Armbruster when = 0;
739949b06eSMarkus Armbruster } else if (strcmp(whenstr, "never") == 0) {
749949b06eSMarkus Armbruster when = TIME_MAX;
759949b06eSMarkus Armbruster } else if (whenstr[0] == '+') {
769949b06eSMarkus Armbruster when = time(NULL);
779949b06eSMarkus Armbruster numstr = whenstr + 1;
789949b06eSMarkus Armbruster } else {
799949b06eSMarkus Armbruster when = 0;
809949b06eSMarkus Armbruster numstr = whenstr;
819949b06eSMarkus Armbruster }
829949b06eSMarkus Armbruster
839949b06eSMarkus Armbruster if (numstr) {
849949b06eSMarkus Armbruster if (qemu_strtou64(numstr, NULL, 10, &num) < 0) {
859949b06eSMarkus Armbruster error_setg(errp, "Parameter 'time' doesn't take value '%s'",
869949b06eSMarkus Armbruster whenstr);
879949b06eSMarkus Armbruster return;
889949b06eSMarkus Armbruster }
899949b06eSMarkus Armbruster when += num;
909949b06eSMarkus Armbruster }
919949b06eSMarkus Armbruster
929949b06eSMarkus Armbruster if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
939949b06eSMarkus Armbruster if (!qemu_using_spice(errp)) {
949949b06eSMarkus Armbruster return;
959949b06eSMarkus Armbruster }
969949b06eSMarkus Armbruster rc = qemu_spice.set_pw_expire(when);
979949b06eSMarkus Armbruster } else {
989949b06eSMarkus Armbruster assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
999949b06eSMarkus Armbruster rc = vnc_display_pw_expire(opts->u.vnc.display, when);
1009949b06eSMarkus Armbruster }
1019949b06eSMarkus Armbruster
1029949b06eSMarkus Armbruster if (rc != 0) {
1039949b06eSMarkus Armbruster error_setg(errp, "Could not set password expire time");
1049949b06eSMarkus Armbruster }
1059949b06eSMarkus Armbruster }
1069949b06eSMarkus Armbruster
1079949b06eSMarkus Armbruster #ifdef CONFIG_VNC
qmp_change_vnc_password(const char * password,Error ** errp)1089949b06eSMarkus Armbruster void qmp_change_vnc_password(const char *password, Error **errp)
1099949b06eSMarkus Armbruster {
1109949b06eSMarkus Armbruster if (vnc_display_password(NULL, password) < 0) {
1119949b06eSMarkus Armbruster error_setg(errp, "Could not set password");
1129949b06eSMarkus Armbruster }
1139949b06eSMarkus Armbruster }
1149949b06eSMarkus Armbruster #endif
1159949b06eSMarkus Armbruster
qmp_add_client_spice(int fd,bool has_skipauth,bool skipauth,bool has_tls,bool tls,Error ** errp)1163125af29SMarkus Armbruster bool qmp_add_client_spice(int fd, bool has_skipauth, bool skipauth,
1173125af29SMarkus Armbruster bool has_tls, bool tls, Error **errp)
1183125af29SMarkus Armbruster {
1193125af29SMarkus Armbruster if (!qemu_using_spice(errp)) {
1203125af29SMarkus Armbruster return false;
1213125af29SMarkus Armbruster }
1223125af29SMarkus Armbruster skipauth = has_skipauth ? skipauth : false;
1233125af29SMarkus Armbruster tls = has_tls ? tls : false;
1243125af29SMarkus Armbruster if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
1253125af29SMarkus Armbruster error_setg(errp, "spice failed to add client");
1263125af29SMarkus Armbruster return false;
1273125af29SMarkus Armbruster }
1283125af29SMarkus Armbruster return true;
1293125af29SMarkus Armbruster }
1303125af29SMarkus Armbruster
1313125af29SMarkus Armbruster #ifdef CONFIG_VNC
qmp_add_client_vnc(int fd,bool has_skipauth,bool skipauth,bool has_tls,bool tls,Error ** errp)1323125af29SMarkus Armbruster bool qmp_add_client_vnc(int fd, bool has_skipauth, bool skipauth,
1333125af29SMarkus Armbruster bool has_tls, bool tls, Error **errp)
1343125af29SMarkus Armbruster {
1353125af29SMarkus Armbruster skipauth = has_skipauth ? skipauth : false;
1363125af29SMarkus Armbruster vnc_display_add_client(NULL, fd, skipauth);
1373125af29SMarkus Armbruster return true;
1383125af29SMarkus Armbruster }
1393125af29SMarkus Armbruster #endif
1403125af29SMarkus Armbruster
1413125af29SMarkus Armbruster #ifdef CONFIG_DBUS_DISPLAY
qmp_add_client_dbus_display(int fd,bool has_skipauth,bool skipauth,bool has_tls,bool tls,Error ** errp)1423125af29SMarkus Armbruster bool qmp_add_client_dbus_display(int fd, bool has_skipauth, bool skipauth,
1433125af29SMarkus Armbruster bool has_tls, bool tls, Error **errp)
1443125af29SMarkus Armbruster {
1453125af29SMarkus Armbruster if (!qemu_using_dbus_display(errp)) {
1463125af29SMarkus Armbruster return false;
1473125af29SMarkus Armbruster }
1483125af29SMarkus Armbruster if (!qemu_dbus_display.add_client(fd, errp)) {
1493125af29SMarkus Armbruster return false;
1503125af29SMarkus Armbruster }
1513125af29SMarkus Armbruster return true;
1523125af29SMarkus Armbruster }
1533125af29SMarkus Armbruster #endif
1543125af29SMarkus Armbruster
qmp_display_reload(DisplayReloadOptions * arg,Error ** errp)1559949b06eSMarkus Armbruster void qmp_display_reload(DisplayReloadOptions *arg, Error **errp)
1569949b06eSMarkus Armbruster {
1579949b06eSMarkus Armbruster switch (arg->type) {
1589949b06eSMarkus Armbruster case DISPLAY_RELOAD_TYPE_VNC:
1599949b06eSMarkus Armbruster #ifdef CONFIG_VNC
1609949b06eSMarkus Armbruster if (arg->u.vnc.has_tls_certs && arg->u.vnc.tls_certs) {
1619949b06eSMarkus Armbruster vnc_display_reload_certs(NULL, errp);
1629949b06eSMarkus Armbruster }
1639949b06eSMarkus Armbruster #else
1649949b06eSMarkus Armbruster error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
1659949b06eSMarkus Armbruster #endif
1669949b06eSMarkus Armbruster break;
1679949b06eSMarkus Armbruster default:
1689949b06eSMarkus Armbruster abort();
1699949b06eSMarkus Armbruster }
1709949b06eSMarkus Armbruster }
1719949b06eSMarkus Armbruster
qmp_display_update(DisplayUpdateOptions * arg,Error ** errp)1729949b06eSMarkus Armbruster void qmp_display_update(DisplayUpdateOptions *arg, Error **errp)
1739949b06eSMarkus Armbruster {
1749949b06eSMarkus Armbruster switch (arg->type) {
1759949b06eSMarkus Armbruster case DISPLAY_UPDATE_TYPE_VNC:
1769949b06eSMarkus Armbruster #ifdef CONFIG_VNC
1779949b06eSMarkus Armbruster vnc_display_update(&arg->u.vnc, errp);
1789949b06eSMarkus Armbruster #else
1799949b06eSMarkus Armbruster error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
1809949b06eSMarkus Armbruster #endif
1819949b06eSMarkus Armbruster break;
1829949b06eSMarkus Armbruster default:
1839949b06eSMarkus Armbruster abort();
1849949b06eSMarkus Armbruster }
1859949b06eSMarkus Armbruster }
186f9e1ef74SJuan Quintela
qmp_client_migrate_info(const char * protocol,const char * hostname,bool has_port,int64_t port,bool has_tls_port,int64_t tls_port,const char * cert_subject,Error ** errp)187f9e1ef74SJuan Quintela void qmp_client_migrate_info(const char *protocol, const char *hostname,
188f9e1ef74SJuan Quintela bool has_port, int64_t port,
189f9e1ef74SJuan Quintela bool has_tls_port, int64_t tls_port,
190f9e1ef74SJuan Quintela const char *cert_subject,
191f9e1ef74SJuan Quintela Error **errp)
192f9e1ef74SJuan Quintela {
193f9e1ef74SJuan Quintela if (strcmp(protocol, "spice") == 0) {
194f9e1ef74SJuan Quintela if (!qemu_using_spice(errp)) {
195f9e1ef74SJuan Quintela return;
196f9e1ef74SJuan Quintela }
197f9e1ef74SJuan Quintela
198f9e1ef74SJuan Quintela if (!has_port && !has_tls_port) {
199*517b0220SMarkus Armbruster error_setg(errp, "parameter 'port' or 'tls-port' is required");
200f9e1ef74SJuan Quintela return;
201f9e1ef74SJuan Quintela }
202f9e1ef74SJuan Quintela
203f9e1ef74SJuan Quintela if (qemu_spice.migrate_info(hostname,
204f9e1ef74SJuan Quintela has_port ? port : -1,
205f9e1ef74SJuan Quintela has_tls_port ? tls_port : -1,
206f9e1ef74SJuan Quintela cert_subject)) {
207f9e1ef74SJuan Quintela error_setg(errp, "Could not set up display for migration");
208f9e1ef74SJuan Quintela return;
209f9e1ef74SJuan Quintela }
210f9e1ef74SJuan Quintela return;
211f9e1ef74SJuan Quintela }
212f9e1ef74SJuan Quintela
213f9e1ef74SJuan Quintela error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
214f9e1ef74SJuan Quintela }
2154f2c765bSMarc-André Lureau
216f38aa2c7SMarc-André Lureau #ifdef CONFIG_PIXMAN
2174f2c765bSMarc-André Lureau #ifdef CONFIG_PNG
2184f2c765bSMarc-André Lureau /**
2194f2c765bSMarc-André Lureau * png_save: Take a screenshot as PNG
2204f2c765bSMarc-André Lureau *
2214f2c765bSMarc-André Lureau * Saves screendump as a PNG file
2224f2c765bSMarc-André Lureau *
2234f2c765bSMarc-André Lureau * Returns true for success or false for error.
2244f2c765bSMarc-André Lureau *
2254f2c765bSMarc-André Lureau * @fd: File descriptor for PNG file.
2264f2c765bSMarc-André Lureau * @image: Image data in pixman format.
2274f2c765bSMarc-André Lureau * @errp: Pointer to an error.
2284f2c765bSMarc-André Lureau */
png_save(int fd,pixman_image_t * image,Error ** errp)2294f2c765bSMarc-André Lureau static bool png_save(int fd, pixman_image_t *image, Error **errp)
2304f2c765bSMarc-André Lureau {
2314f2c765bSMarc-André Lureau int width = pixman_image_get_width(image);
2324f2c765bSMarc-André Lureau int height = pixman_image_get_height(image);
2334f2c765bSMarc-André Lureau png_struct *png_ptr;
2344f2c765bSMarc-André Lureau png_info *info_ptr;
2354f2c765bSMarc-André Lureau g_autoptr(pixman_image_t) linebuf =
2364f2c765bSMarc-André Lureau qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
2374f2c765bSMarc-André Lureau uint8_t *buf = (uint8_t *)pixman_image_get_data(linebuf);
2384f2c765bSMarc-André Lureau FILE *f = fdopen(fd, "wb");
2394f2c765bSMarc-André Lureau int y;
2404f2c765bSMarc-André Lureau if (!f) {
2414f2c765bSMarc-André Lureau error_setg_errno(errp, errno,
2424f2c765bSMarc-André Lureau "Failed to create file from file descriptor");
2434f2c765bSMarc-André Lureau return false;
2444f2c765bSMarc-André Lureau }
2454f2c765bSMarc-André Lureau
2464f2c765bSMarc-André Lureau png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
2474f2c765bSMarc-André Lureau NULL, NULL);
2484f2c765bSMarc-André Lureau if (!png_ptr) {
2494f2c765bSMarc-André Lureau error_setg(errp, "PNG creation failed. Unable to write struct");
2504f2c765bSMarc-André Lureau fclose(f);
2514f2c765bSMarc-André Lureau return false;
2524f2c765bSMarc-André Lureau }
2534f2c765bSMarc-André Lureau
2544f2c765bSMarc-André Lureau info_ptr = png_create_info_struct(png_ptr);
2554f2c765bSMarc-André Lureau
2564f2c765bSMarc-André Lureau if (!info_ptr) {
2574f2c765bSMarc-André Lureau error_setg(errp, "PNG creation failed. Unable to write info");
2584f2c765bSMarc-André Lureau fclose(f);
2594f2c765bSMarc-André Lureau png_destroy_write_struct(&png_ptr, &info_ptr);
2604f2c765bSMarc-André Lureau return false;
2614f2c765bSMarc-André Lureau }
2624f2c765bSMarc-André Lureau
2634f2c765bSMarc-André Lureau png_init_io(png_ptr, f);
2644f2c765bSMarc-André Lureau
2654f2c765bSMarc-André Lureau png_set_IHDR(png_ptr, info_ptr, width, height, 8,
2664f2c765bSMarc-André Lureau PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
2674f2c765bSMarc-André Lureau PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2684f2c765bSMarc-André Lureau
2694f2c765bSMarc-André Lureau png_write_info(png_ptr, info_ptr);
2704f2c765bSMarc-André Lureau
2714f2c765bSMarc-André Lureau for (y = 0; y < height; ++y) {
2724f2c765bSMarc-André Lureau qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
2734f2c765bSMarc-André Lureau png_write_row(png_ptr, buf);
2744f2c765bSMarc-André Lureau }
2754f2c765bSMarc-André Lureau
2764f2c765bSMarc-André Lureau png_write_end(png_ptr, NULL);
2774f2c765bSMarc-André Lureau
2784f2c765bSMarc-André Lureau png_destroy_write_struct(&png_ptr, &info_ptr);
2794f2c765bSMarc-André Lureau
2804f2c765bSMarc-André Lureau if (fclose(f) != 0) {
2814f2c765bSMarc-André Lureau error_setg_errno(errp, errno,
2824f2c765bSMarc-André Lureau "PNG creation failed. Unable to close file");
2834f2c765bSMarc-André Lureau return false;
2844f2c765bSMarc-André Lureau }
2854f2c765bSMarc-André Lureau
2864f2c765bSMarc-André Lureau return true;
2874f2c765bSMarc-André Lureau }
2884f2c765bSMarc-André Lureau
2894f2c765bSMarc-André Lureau #else /* no png support */
2904f2c765bSMarc-André Lureau
png_save(int fd,pixman_image_t * image,Error ** errp)2914f2c765bSMarc-André Lureau static bool png_save(int fd, pixman_image_t *image, Error **errp)
2924f2c765bSMarc-André Lureau {
2934f2c765bSMarc-André Lureau error_setg(errp, "Enable PNG support with libpng for screendump");
2944f2c765bSMarc-André Lureau return false;
2954f2c765bSMarc-André Lureau }
2964f2c765bSMarc-André Lureau
2974f2c765bSMarc-André Lureau #endif /* CONFIG_PNG */
2984f2c765bSMarc-André Lureau
ppm_save(int fd,pixman_image_t * image,Error ** errp)2994f2c765bSMarc-André Lureau static bool ppm_save(int fd, pixman_image_t *image, Error **errp)
3004f2c765bSMarc-André Lureau {
3014f2c765bSMarc-André Lureau int width = pixman_image_get_width(image);
3024f2c765bSMarc-André Lureau int height = pixman_image_get_height(image);
3034f2c765bSMarc-André Lureau g_autoptr(Object) ioc = OBJECT(qio_channel_file_new_fd(fd));
3044f2c765bSMarc-André Lureau g_autofree char *header = NULL;
3054f2c765bSMarc-André Lureau g_autoptr(pixman_image_t) linebuf = NULL;
3064f2c765bSMarc-André Lureau int y;
3074f2c765bSMarc-André Lureau
3084f2c765bSMarc-André Lureau trace_ppm_save(fd, image);
3094f2c765bSMarc-André Lureau
3104f2c765bSMarc-André Lureau header = g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255);
3114f2c765bSMarc-André Lureau if (qio_channel_write_all(QIO_CHANNEL(ioc),
3124f2c765bSMarc-André Lureau header, strlen(header), errp) < 0) {
3134f2c765bSMarc-André Lureau return false;
3144f2c765bSMarc-André Lureau }
3154f2c765bSMarc-André Lureau
3164f2c765bSMarc-André Lureau linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
3174f2c765bSMarc-André Lureau for (y = 0; y < height; y++) {
3184f2c765bSMarc-André Lureau qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
3194f2c765bSMarc-André Lureau if (qio_channel_write_all(QIO_CHANNEL(ioc),
3204f2c765bSMarc-André Lureau (char *)pixman_image_get_data(linebuf),
3214f2c765bSMarc-André Lureau pixman_image_get_stride(linebuf), errp) < 0) {
3224f2c765bSMarc-André Lureau return false;
3234f2c765bSMarc-André Lureau }
3244f2c765bSMarc-André Lureau }
3254f2c765bSMarc-André Lureau
3264f2c765bSMarc-André Lureau return true;
3274f2c765bSMarc-André Lureau }
3284f2c765bSMarc-André Lureau
3294f2c765bSMarc-André Lureau /* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
3304f2c765bSMarc-André Lureau void coroutine_fn
qmp_screendump(const char * filename,const char * device,bool has_head,int64_t head,bool has_format,ImageFormat format,Error ** errp)3314f2c765bSMarc-André Lureau qmp_screendump(const char *filename, const char *device,
3324f2c765bSMarc-André Lureau bool has_head, int64_t head,
3334f2c765bSMarc-André Lureau bool has_format, ImageFormat format, Error **errp)
3344f2c765bSMarc-André Lureau {
3354f2c765bSMarc-André Lureau g_autoptr(pixman_image_t) image = NULL;
3364f2c765bSMarc-André Lureau QemuConsole *con;
3374f2c765bSMarc-André Lureau DisplaySurface *surface;
3384f2c765bSMarc-André Lureau int fd;
3394f2c765bSMarc-André Lureau
3404f2c765bSMarc-André Lureau if (device) {
3414f2c765bSMarc-André Lureau con = qemu_console_lookup_by_device_name(device, has_head ? head : 0,
3424f2c765bSMarc-André Lureau errp);
3434f2c765bSMarc-André Lureau if (!con) {
3444f2c765bSMarc-André Lureau return;
3454f2c765bSMarc-André Lureau }
3464f2c765bSMarc-André Lureau } else {
3474f2c765bSMarc-André Lureau if (has_head) {
3484f2c765bSMarc-André Lureau error_setg(errp, "'head' must be specified together with 'device'");
3494f2c765bSMarc-André Lureau return;
3504f2c765bSMarc-André Lureau }
3514f2c765bSMarc-André Lureau con = qemu_console_lookup_by_index(0);
3524f2c765bSMarc-André Lureau if (!con) {
3534f2c765bSMarc-André Lureau error_setg(errp, "There is no console to take a screendump from");
3544f2c765bSMarc-André Lureau return;
3554f2c765bSMarc-André Lureau }
3564f2c765bSMarc-André Lureau }
3574f2c765bSMarc-André Lureau
3584f2c765bSMarc-André Lureau qemu_console_co_wait_update(con);
3594f2c765bSMarc-André Lureau
3604f2c765bSMarc-André Lureau /*
3614f2c765bSMarc-André Lureau * All pending coroutines are woken up, while the BQL is held. No
3624f2c765bSMarc-André Lureau * further graphic update are possible until it is released. Take
3634f2c765bSMarc-André Lureau * an image ref before that.
3644f2c765bSMarc-André Lureau */
3654f2c765bSMarc-André Lureau surface = qemu_console_surface(con);
3664f2c765bSMarc-André Lureau if (!surface) {
3674f2c765bSMarc-André Lureau error_setg(errp, "no surface");
3684f2c765bSMarc-André Lureau return;
3694f2c765bSMarc-André Lureau }
3704f2c765bSMarc-André Lureau image = pixman_image_ref(surface->image);
3714f2c765bSMarc-André Lureau
3724f2c765bSMarc-André Lureau fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
3734f2c765bSMarc-André Lureau if (fd == -1) {
3744f2c765bSMarc-André Lureau error_setg(errp, "failed to open file '%s': %s", filename,
3754f2c765bSMarc-André Lureau strerror(errno));
3764f2c765bSMarc-André Lureau return;
3774f2c765bSMarc-André Lureau }
3784f2c765bSMarc-André Lureau
3794f2c765bSMarc-André Lureau /*
3804f2c765bSMarc-André Lureau * The image content could potentially be updated as the coroutine
3814f2c765bSMarc-André Lureau * yields and releases the BQL. It could produce corrupted dump, but
3824f2c765bSMarc-André Lureau * it should be otherwise safe.
3834f2c765bSMarc-André Lureau */
3844f2c765bSMarc-André Lureau if (has_format && format == IMAGE_FORMAT_PNG) {
3854f2c765bSMarc-André Lureau /* PNG format specified for screendump */
3864f2c765bSMarc-André Lureau if (!png_save(fd, image, errp)) {
3874f2c765bSMarc-André Lureau qemu_unlink(filename);
3884f2c765bSMarc-André Lureau }
3894f2c765bSMarc-André Lureau } else {
3904f2c765bSMarc-André Lureau /* PPM format specified/default for screendump */
3914f2c765bSMarc-André Lureau if (!ppm_save(fd, image, errp)) {
3924f2c765bSMarc-André Lureau qemu_unlink(filename);
3934f2c765bSMarc-André Lureau }
3944f2c765bSMarc-André Lureau }
3954f2c765bSMarc-André Lureau }
396f38aa2c7SMarc-André Lureau #endif /* CONFIG_PIXMAN */
397