Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU external Spice client display driver |
| 3 | * |
| 4 | * Copyright (c) 2018 Marc-André Lureau <marcandre.lureau@redhat.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
| 26 | |
| 27 | #include <gio/gio.h> |
| 28 | |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 29 | #include "ui/console.h" |
Akihiko Odaki | 9a6c69d | 2021-07-14 14:57:35 +0900 | [diff] [blame] | 30 | #include "ui/spice-display.h" |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 31 | #include "qemu/config-file.h" |
Thomas Huth | 5feed38 | 2023-02-10 12:19:31 +0100 | [diff] [blame] | 32 | #include "qemu/error-report.h" |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 33 | #include "qemu/option.h" |
| 34 | #include "qemu/cutils.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 35 | #include "qemu/module.h" |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 36 | #include "qapi/error.h" |
| 37 | #include "io/channel-command.h" |
| 38 | #include "chardev/spice.h" |
Philippe Mathieu-Daudé | 32cad1f | 2024-12-03 15:20:13 +0100 | [diff] [blame] | 39 | #include "system/system.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 40 | #include "qom/object.h" |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 41 | |
| 42 | static const char *tmp_dir; |
| 43 | static char *app_dir; |
| 44 | static char *sock_path; |
| 45 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 46 | struct VCChardev { |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 47 | SpiceChardev parent; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 48 | }; |
Gerd Hoffmann | e220cf8 | 2020-10-14 14:11:15 +0200 | [diff] [blame] | 49 | |
| 50 | struct VCChardevClass { |
| 51 | ChardevClass parent; |
| 52 | void (*parent_open)(Chardev *chr, ChardevBackend *backend, |
| 53 | bool *be_opened, Error **errp); |
| 54 | }; |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 55 | |
| 56 | #define TYPE_CHARDEV_VC "chardev-vc" |
Gerd Hoffmann | e220cf8 | 2020-10-14 14:11:15 +0200 | [diff] [blame] | 57 | OBJECT_DECLARE_TYPE(VCChardev, VCChardevClass, CHARDEV_VC) |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 58 | |
| 59 | static ChardevBackend * |
| 60 | chr_spice_backend_new(void) |
| 61 | { |
| 62 | ChardevBackend *be = g_new0(ChardevBackend, 1); |
| 63 | |
| 64 | be->type = CHARDEV_BACKEND_KIND_SPICEPORT; |
| 65 | be->u.spiceport.data = g_new0(ChardevSpicePort, 1); |
| 66 | |
| 67 | return be; |
| 68 | } |
| 69 | |
| 70 | static void vc_chr_open(Chardev *chr, |
| 71 | ChardevBackend *backend, |
| 72 | bool *be_opened, |
| 73 | Error **errp) |
| 74 | { |
Gerd Hoffmann | e220cf8 | 2020-10-14 14:11:15 +0200 | [diff] [blame] | 75 | VCChardevClass *vc = CHARDEV_VC_GET_CLASS(chr); |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 76 | ChardevBackend *be; |
| 77 | const char *fqdn = NULL; |
| 78 | |
| 79 | if (strstart(chr->label, "serial", NULL)) { |
| 80 | fqdn = "org.qemu.console.serial.0"; |
| 81 | } else if (strstart(chr->label, "parallel", NULL)) { |
| 82 | fqdn = "org.qemu.console.parallel.0"; |
| 83 | } else if (strstart(chr->label, "compat_monitor", NULL)) { |
| 84 | fqdn = "org.qemu.monitor.hmp.0"; |
| 85 | } |
| 86 | |
| 87 | be = chr_spice_backend_new(); |
| 88 | be->u.spiceport.data->fqdn = fqdn ? |
| 89 | g_strdup(fqdn) : g_strdup_printf("org.qemu.console.%s", chr->label); |
Gerd Hoffmann | e220cf8 | 2020-10-14 14:11:15 +0200 | [diff] [blame] | 90 | vc->parent_open(chr, be, be_opened, errp); |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 91 | qapi_free_ChardevBackend(be); |
| 92 | } |
| 93 | |
| 94 | static void vc_chr_set_echo(Chardev *chr, bool echo) |
| 95 | { |
| 96 | /* TODO: set echo for frontends QMP and qtest */ |
| 97 | } |
| 98 | |
Marc-André Lureau | 32aa1f8 | 2023-08-30 13:38:17 +0400 | [diff] [blame] | 99 | static void vc_chr_parse(QemuOpts *opts, ChardevBackend *backend, Error **errp) |
| 100 | { |
| 101 | /* fqdn is dealt with in vc_chr_open() */ |
| 102 | } |
| 103 | |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 104 | static void char_vc_class_init(ObjectClass *oc, void *data) |
| 105 | { |
Gerd Hoffmann | e220cf8 | 2020-10-14 14:11:15 +0200 | [diff] [blame] | 106 | VCChardevClass *vc = CHARDEV_VC_CLASS(oc); |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 107 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 108 | |
Gerd Hoffmann | e220cf8 | 2020-10-14 14:11:15 +0200 | [diff] [blame] | 109 | vc->parent_open = cc->open; |
| 110 | |
Marc-André Lureau | 32aa1f8 | 2023-08-30 13:38:17 +0400 | [diff] [blame] | 111 | cc->parse = vc_chr_parse; |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 112 | cc->open = vc_chr_open; |
| 113 | cc->chr_set_echo = vc_chr_set_echo; |
| 114 | } |
| 115 | |
| 116 | static const TypeInfo char_vc_type_info = { |
| 117 | .name = TYPE_CHARDEV_VC, |
| 118 | .parent = TYPE_CHARDEV_SPICEPORT, |
| 119 | .instance_size = sizeof(VCChardev), |
| 120 | .class_init = char_vc_class_init, |
Gerd Hoffmann | e220cf8 | 2020-10-14 14:11:15 +0200 | [diff] [blame] | 121 | .class_size = sizeof(VCChardevClass), |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static void spice_app_atexit(void) |
| 125 | { |
| 126 | if (sock_path) { |
| 127 | unlink(sock_path); |
| 128 | } |
| 129 | if (tmp_dir) { |
| 130 | rmdir(tmp_dir); |
| 131 | } |
| 132 | g_free(sock_path); |
| 133 | g_free(app_dir); |
| 134 | } |
| 135 | |
| 136 | static void spice_app_display_early_init(DisplayOptions *opts) |
| 137 | { |
| 138 | QemuOpts *qopts; |
Bruce Rogers | 122e4ef | 2021-02-12 20:23:18 -0700 | [diff] [blame] | 139 | QemuOptsList *list; |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 140 | GError *err = NULL; |
| 141 | |
| 142 | if (opts->has_full_screen) { |
| 143 | error_report("spice-app full-screen isn't supported yet."); |
| 144 | exit(1); |
| 145 | } |
| 146 | if (opts->has_window_close) { |
| 147 | error_report("spice-app window-close isn't supported yet."); |
| 148 | exit(1); |
| 149 | } |
| 150 | |
| 151 | atexit(spice_app_atexit); |
| 152 | |
| 153 | if (qemu_name) { |
| 154 | app_dir = g_build_filename(g_get_user_runtime_dir(), |
| 155 | "qemu", qemu_name, NULL); |
| 156 | if (g_mkdir_with_parents(app_dir, S_IRWXU) < -1) { |
| 157 | error_report("Failed to create directory %s: %s", |
| 158 | app_dir, strerror(errno)); |
| 159 | exit(1); |
| 160 | } |
| 161 | } else { |
| 162 | app_dir = g_dir_make_tmp(NULL, &err); |
| 163 | tmp_dir = app_dir; |
| 164 | if (err) { |
| 165 | error_report("Failed to create temporary directory: %s", |
| 166 | err->message); |
| 167 | exit(1); |
| 168 | } |
| 169 | } |
Bruce Rogers | 122e4ef | 2021-02-12 20:23:18 -0700 | [diff] [blame] | 170 | list = qemu_find_opts("spice"); |
| 171 | if (list == NULL) { |
| 172 | error_report("spice-app missing spice support"); |
| 173 | exit(1); |
| 174 | } |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 175 | |
Zhao Liu | e5d60d9 | 2024-10-29 16:59:32 +0800 | [diff] [blame] | 176 | type_register_static(&char_vc_type_info); |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 177 | |
| 178 | sock_path = g_strjoin("", app_dir, "/", "spice.sock", NULL); |
Bruce Rogers | 122e4ef | 2021-02-12 20:23:18 -0700 | [diff] [blame] | 179 | qopts = qemu_opts_create(list, NULL, 0, &error_abort); |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 180 | qemu_opt_set(qopts, "disable-ticketing", "on", &error_abort); |
| 181 | qemu_opt_set(qopts, "unix", "on", &error_abort); |
| 182 | qemu_opt_set(qopts, "addr", sock_path, &error_abort); |
| 183 | qemu_opt_set(qopts, "image-compression", "off", &error_abort); |
| 184 | qemu_opt_set(qopts, "streaming-video", "off", &error_abort); |
Akihiko Odaki | 9a6c69d | 2021-07-14 14:57:35 +0900 | [diff] [blame] | 185 | #ifdef HAVE_SPICE_GL |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 186 | qemu_opt_set(qopts, "gl", opts->has_gl ? "on" : "off", &error_abort); |
| 187 | display_opengl = opts->has_gl; |
Marc-André Lureau | cb0efb7 | 2019-05-24 15:09:43 +0200 | [diff] [blame] | 188 | #endif |
Gerd Hoffmann | 93ab584 | 2020-10-14 14:11:17 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void spice_app_display_init(DisplayState *ds, DisplayOptions *opts) |
| 192 | { |
| 193 | ChardevBackend *be = chr_spice_backend_new(); |
| 194 | QemuOpts *qopts; |
| 195 | GError *err = NULL; |
| 196 | gchar *uri; |
| 197 | |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 198 | be->u.spiceport.data->fqdn = g_strdup("org.qemu.monitor.qmp.0"); |
| 199 | qemu_chardev_new("org.qemu.monitor.qmp", TYPE_CHARDEV_SPICEPORT, |
| 200 | be, NULL, &error_abort); |
| 201 | qopts = qemu_opts_create(qemu_find_opts("mon"), |
| 202 | NULL, 0, &error_fatal); |
| 203 | qemu_opt_set(qopts, "chardev", "org.qemu.monitor.qmp", &error_abort); |
| 204 | qemu_opt_set(qopts, "mode", "control", &error_abort); |
| 205 | |
| 206 | qapi_free_ChardevBackend(be); |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 207 | uri = g_strjoin("", "spice+unix://", app_dir, "/", "spice.sock", NULL); |
| 208 | info_report("Launching display with URI: %s", uri); |
| 209 | g_app_info_launch_default_for_uri(uri, NULL, &err); |
| 210 | if (err) { |
| 211 | error_report("Failed to launch %s URI: %s", uri, err->message); |
| 212 | error_report("You need a capable Spice client, " |
| 213 | "such as virt-viewer 8.0"); |
| 214 | exit(1); |
| 215 | } |
| 216 | g_free(uri); |
| 217 | } |
| 218 | |
| 219 | static QemuDisplay qemu_display_spice_app = { |
| 220 | .type = DISPLAY_TYPE_SPICE_APP, |
| 221 | .early_init = spice_app_display_early_init, |
| 222 | .init = spice_app_display_init, |
Marc-André Lureau | b7f1bb3 | 2023-11-17 14:55:07 +0400 | [diff] [blame] | 223 | .vc = "vc", |
Marc-André Lureau | d8aec9d | 2019-02-21 12:07:03 +0100 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | static void register_spice_app(void) |
| 227 | { |
| 228 | qemu_display_register(&qemu_display_spice_app); |
| 229 | } |
| 230 | |
| 231 | type_init(register_spice_app); |
Gerd Hoffmann | b36ae1c | 2021-06-24 12:38:13 +0200 | [diff] [blame] | 232 | |
| 233 | module_dep("ui-spice-core"); |
| 234 | module_dep("chardev-spice"); |