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