| 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 | */ |
| 24 | |
| 25 | #include "qemu/osdep.h" |
| 26 | |
| 27 | #include <gio/gio.h> |
| 28 | |
| 29 | #include "ui/console.h" |
| 30 | #include "ui/spice-display.h" |
| 31 | #include "qemu/config-file.h" |
| 32 | #include "qemu/error-report.h" |
| 33 | #include "qemu/option.h" |
| 34 | #include "qemu/cutils.h" |
| 35 | #include "qemu/module.h" |
| 36 | #include "qapi/error.h" |
| 37 | #include "io/channel-command.h" |
| 38 | #include "chardev/spice.h" |
| 39 | #include "system/system.h" |
| 40 | #include "qom/object.h" |
| 41 | |
| 42 | static const char *tmp_dir; |
| 43 | static char *app_dir; |
| 44 | static char *sock_path; |
| 45 | |
| 46 | struct VCChardev { |
| 47 | SpiceChardev parent; |
| 48 | }; |
| 49 | |
| 50 | struct VCChardevClass { |
| 51 | ChardevClass parent; |
| 52 | bool (*parent_init)(Chardev *chr, ChardevBackend *backend, Error **errp); |
| 53 | }; |
| 54 | |
| 55 | #define TYPE_CHARDEV_VC "chardev-vc" |
| 56 | OBJECT_DECLARE_TYPE(VCChardev, VCChardevClass, CHARDEV_VC) |
| 57 | |
| 58 | static ChardevBackend * |
| 59 | chr_spice_backend_new(void) |
| 60 | { |
| 61 | ChardevBackend *be = g_new0(ChardevBackend, 1); |
| 62 | |
| 63 | be->type = CHARDEV_BACKEND_KIND_SPICEPORT; |
| 64 | be->u.spiceport.data = g_new0(ChardevSpicePort, 1); |
| 65 | |
| 66 | return be; |
| 67 | } |
| 68 | |
| 69 | static bool vc_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) |
| 70 | { |
| 71 | VCChardevClass *vc = CHARDEV_VC_GET_CLASS(chr); |
| 72 | ChardevBackend *be; |
| 73 | const char *fqdn = NULL; |
| 74 | bool ok; |
| 75 | |
| 76 | if (strstart(chr->label, "serial", NULL)) { |
| 77 | fqdn = "org.qemu.console.serial.0"; |
| 78 | } else if (strstart(chr->label, "parallel", NULL)) { |
| 79 | fqdn = "org.qemu.console.parallel.0"; |
| 80 | } else if (strstart(chr->label, "compat_monitor", NULL)) { |
| 81 | fqdn = "org.qemu.monitor.hmp.0"; |
| 82 | } |
| 83 | |
| 84 | be = chr_spice_backend_new(); |
| 85 | be->u.spiceport.data->fqdn = fqdn ? |
| 86 | g_strdup(fqdn) : g_strdup_printf("org.qemu.console.%s", chr->label); |
| 87 | ok = vc->parent_init(chr, be, errp); |
| 88 | qapi_free_ChardevBackend(be); |
| 89 | return ok; |
| 90 | } |
| 91 | |
| 92 | static void vc_chr_set_echo(Chardev *chr, bool echo) |
| 93 | { |
| 94 | /* TODO: set echo for frontends QMP and qtest */ |
| 95 | } |
| 96 | |
| 97 | static void vc_chr_parse(QemuOpts *opts, ChardevBackend *backend, Error **errp) |
| 98 | { |
| 99 | /* fqdn is dealt with in vc_chr_open() */ |
| 100 | } |
| 101 | |
| 102 | static void char_vc_class_init(ObjectClass *oc, const void *data) |
| 103 | { |
| 104 | VCChardevClass *vc = CHARDEV_VC_CLASS(oc); |
| 105 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 106 | |
| 107 | vc->parent_init = cc->chr_open; |
| 108 | |
| 109 | cc->chr_parse = vc_chr_parse; |
| 110 | cc->chr_open = vc_chr_open; |
| 111 | cc->chr_set_echo = vc_chr_set_echo; |
| 112 | } |
| 113 | |
| 114 | static const TypeInfo char_vc_type_info = { |
| 115 | .name = TYPE_CHARDEV_VC, |
| 116 | .parent = TYPE_CHARDEV_SPICEPORT, |
| 117 | .instance_size = sizeof(VCChardev), |
| 118 | .class_init = char_vc_class_init, |
| 119 | .class_size = sizeof(VCChardevClass), |
| 120 | }; |
| 121 | |
| 122 | static void spice_app_cleanup(void) |
| 123 | { |
| 124 | if (sock_path) { |
| 125 | unlink(sock_path); |
| 126 | g_clear_pointer(&sock_path, g_free); |
| 127 | } |
| 128 | if (tmp_dir) { |
| 129 | rmdir(tmp_dir); |
| 130 | tmp_dir = NULL; |
| 131 | } |
| 132 | g_clear_pointer(&app_dir, g_free); |
| 133 | } |
| 134 | |
| 135 | static void spice_app_display_early_init(DisplayOptions *opts) |
| 136 | { |
| 137 | QemuOpts *qopts; |
| 138 | QemuOptsList *list; |
| 139 | GError *err = NULL; |
| 140 | |
| 141 | if (opts->has_full_screen) { |
| 142 | error_report("spice-app full-screen isn't supported yet."); |
| 143 | exit(1); |
| 144 | } |
| 145 | if (opts->has_window_close) { |
| 146 | error_report("spice-app window-close isn't supported yet."); |
| 147 | exit(1); |
| 148 | } |
| 149 | |
| 150 | if (qemu_name) { |
| 151 | app_dir = g_build_filename(g_get_user_runtime_dir(), |
| 152 | "qemu", qemu_name, NULL); |
| 153 | if (g_mkdir_with_parents(app_dir, S_IRWXU) < 0) { |
| 154 | error_report("Failed to create directory %s: %s", |
| 155 | app_dir, strerror(errno)); |
| 156 | exit(1); |
| 157 | } |
| 158 | } else { |
| 159 | app_dir = g_dir_make_tmp(NULL, &err); |
| 160 | tmp_dir = app_dir; |
| 161 | if (err) { |
| 162 | error_report("Failed to create temporary directory: %s", |
| 163 | err->message); |
| 164 | exit(1); |
| 165 | } |
| 166 | } |
| 167 | list = qemu_find_opts("spice"); |
| 168 | if (list == NULL) { |
| 169 | error_report("spice-app missing spice support"); |
| 170 | exit(1); |
| 171 | } |
| 172 | |
| 173 | type_register_static(&char_vc_type_info); |
| 174 | |
| 175 | sock_path = g_strjoin("", app_dir, "/", "spice.sock", NULL); |
| 176 | qopts = qemu_opts_create(list, NULL, 0, &error_abort); |
| 177 | qemu_opt_set(qopts, "disable-ticketing", "on", &error_abort); |
| 178 | qemu_opt_set(qopts, "unix", "on", &error_abort); |
| 179 | qemu_opt_set(qopts, "addr", sock_path, &error_abort); |
| 180 | qemu_opt_set(qopts, "image-compression", "off", &error_abort); |
| 181 | qemu_opt_set(qopts, "streaming-video", "off", &error_abort); |
| 182 | #ifdef HAVE_SPICE_GL |
| 183 | qemu_opt_set(qopts, "gl", opts->has_gl ? "on" : "off", &error_abort); |
| 184 | display_opengl = opts->has_gl; |
| 185 | #endif |
| 186 | } |
| 187 | |
| 188 | static void spice_app_display_init(DisplayState *ds, DisplayOptions *opts) |
| 189 | { |
| 190 | ChardevBackend *be = chr_spice_backend_new(); |
| 191 | QemuOpts *qopts; |
| 192 | GError *err = NULL; |
| 193 | gchar *uri; |
| 194 | |
| 195 | be->u.spiceport.data->fqdn = g_strdup("org.qemu.monitor.qmp.0"); |
| 196 | qemu_chardev_new("org.qemu.monitor.qmp", TYPE_CHARDEV_SPICEPORT, |
| 197 | be, NULL, &error_abort); |
| 198 | qopts = qemu_opts_create(qemu_find_opts("mon"), |
| 199 | NULL, 0, &error_fatal); |
| 200 | qemu_opt_set(qopts, "chardev", "org.qemu.monitor.qmp", &error_abort); |
| 201 | qemu_opt_set(qopts, "mode", "control", &error_abort); |
| 202 | |
| 203 | qapi_free_ChardevBackend(be); |
| 204 | uri = g_strjoin("", "spice+unix://", app_dir, "/", "spice.sock", NULL); |
| 205 | info_report("Launching display with URI: %s", uri); |
| 206 | g_app_info_launch_default_for_uri(uri, NULL, &err); |
| 207 | if (err) { |
| 208 | error_report("Failed to launch %s URI: %s", uri, err->message); |
| 209 | error_report("You need a capable Spice client, " |
| 210 | "such as virt-viewer 8.0"); |
| 211 | exit(1); |
| 212 | } |
| 213 | g_free(uri); |
| 214 | } |
| 215 | |
| 216 | static QemuDisplay qemu_display_spice_app = { |
| 217 | .type = DISPLAY_TYPE_SPICE_APP, |
| 218 | .early_init = spice_app_display_early_init, |
| 219 | .init = spice_app_display_init, |
| 220 | .cleanup = spice_app_cleanup, |
| 221 | .vc = "vc", |
| 222 | }; |
| 223 | |
| 224 | static void register_spice_app(void) |
| 225 | { |
| 226 | qemu_display_register(&qemu_display_spice_app); |
| 227 | } |
| 228 | |
| 229 | type_init(register_spice_app); |
| 230 | |
| 231 | module_dep("ui-spice-core"); |
| 232 | module_dep("chardev-spice"); |