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