xref: /openbmc/qemu/ui/dbus.c (revision 907b5105)
1 /*
2  * QEMU DBus display
3  *
4  * Copyright (c) 2021 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 #include "qemu/cutils.h"
26 #include "qemu/dbus.h"
27 #include "qemu/main-loop.h"
28 #include "qemu/option.h"
29 #include "qom/object_interfaces.h"
30 #include "sysemu/sysemu.h"
31 #include "ui/dbus-module.h"
32 #include "ui/egl-helpers.h"
33 #include "ui/egl-context.h"
34 #include "audio/audio.h"
35 #include "audio/audio_int.h"
36 #include "qapi/error.h"
37 #include "trace.h"
38 
39 #include "dbus.h"
40 
41 static DBusDisplay *dbus_display;
42 
43 static QEMUGLContext dbus_create_context(DisplayGLCtx *dgc,
44                                          QEMUGLParams *params)
45 {
46     eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
47                    qemu_egl_rn_ctx);
48     return qemu_egl_create_context(dgc, params);
49 }
50 
51 static bool
52 dbus_is_compatible_dcl(DisplayGLCtx *dgc,
53                        DisplayChangeListener *dcl)
54 {
55     return dcl->ops == &dbus_gl_dcl_ops || dcl->ops == &dbus_console_dcl_ops;
56 }
57 
58 static void
59 dbus_create_texture(DisplayGLCtx *ctx, DisplaySurface *surface)
60 {
61     surface_gl_create_texture(ctx->gls, surface);
62 }
63 
64 static void
65 dbus_destroy_texture(DisplayGLCtx *ctx, DisplaySurface *surface)
66 {
67     surface_gl_destroy_texture(ctx->gls, surface);
68 }
69 
70 static void
71 dbus_update_texture(DisplayGLCtx *ctx, DisplaySurface *surface,
72                     int x, int y, int w, int h)
73 {
74     surface_gl_update_texture(ctx->gls, surface, x, y, w, h);
75 }
76 
77 static const DisplayGLCtxOps dbus_gl_ops = {
78     .dpy_gl_ctx_is_compatible_dcl = dbus_is_compatible_dcl,
79     .dpy_gl_ctx_create       = dbus_create_context,
80     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
81     .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
82     .dpy_gl_ctx_create_texture = dbus_create_texture,
83     .dpy_gl_ctx_destroy_texture = dbus_destroy_texture,
84     .dpy_gl_ctx_update_texture = dbus_update_texture,
85 };
86 
87 static NotifierList dbus_display_notifiers =
88     NOTIFIER_LIST_INITIALIZER(dbus_display_notifiers);
89 
90 void
91 dbus_display_notifier_add(Notifier *notifier)
92 {
93     notifier_list_add(&dbus_display_notifiers, notifier);
94 }
95 
96 static void
97 dbus_display_notifier_remove(Notifier *notifier)
98 {
99     notifier_remove(notifier);
100 }
101 
102 void
103 dbus_display_notify(DBusDisplayEvent *event)
104 {
105     notifier_list_notify(&dbus_display_notifiers, event);
106 }
107 
108 static void
109 dbus_display_init(Object *o)
110 {
111     DBusDisplay *dd = DBUS_DISPLAY(o);
112     g_autoptr(GDBusObjectSkeleton) vm = NULL;
113 
114     dd->glctx.ops = &dbus_gl_ops;
115     if (display_opengl) {
116         dd->glctx.gls = qemu_gl_init_shader();
117     }
118     dd->iface = qemu_dbus_display1_vm_skeleton_new();
119     dd->consoles = g_ptr_array_new_with_free_func(g_object_unref);
120 
121     dd->server = g_dbus_object_manager_server_new(DBUS_DISPLAY1_ROOT);
122 
123     vm = g_dbus_object_skeleton_new(DBUS_DISPLAY1_ROOT "/VM");
124     g_dbus_object_skeleton_add_interface(
125         vm, G_DBUS_INTERFACE_SKELETON(dd->iface));
126     g_dbus_object_manager_server_export(dd->server, vm);
127 
128     dbus_clipboard_init(dd);
129     dbus_chardev_init(dd);
130 }
131 
132 static void
133 dbus_display_finalize(Object *o)
134 {
135     DBusDisplay *dd = DBUS_DISPLAY(o);
136 
137     if (dd->notifier.notify) {
138         dbus_display_notifier_remove(&dd->notifier);
139     }
140 
141     qemu_clipboard_peer_unregister(&dd->clipboard_peer);
142     g_clear_object(&dd->clipboard);
143 
144     g_clear_object(&dd->server);
145     g_clear_pointer(&dd->consoles, g_ptr_array_unref);
146     if (dd->add_client_cancellable) {
147         g_cancellable_cancel(dd->add_client_cancellable);
148     }
149     g_clear_object(&dd->add_client_cancellable);
150     g_clear_object(&dd->bus);
151     g_clear_object(&dd->iface);
152     g_free(dd->dbus_addr);
153     g_free(dd->audiodev);
154     g_clear_pointer(&dd->glctx.gls, qemu_gl_fini_shader);
155     dbus_display = NULL;
156 }
157 
158 static bool
159 dbus_display_add_console(DBusDisplay *dd, int idx, Error **errp)
160 {
161     QemuConsole *con;
162     DBusDisplayConsole *dbus_console;
163 
164     con = qemu_console_lookup_by_index(idx);
165     assert(con);
166 
167     if (qemu_console_is_graphic(con) &&
168         dd->gl_mode != DISPLAYGL_MODE_OFF) {
169         qemu_console_set_display_gl_ctx(con, &dd->glctx);
170     }
171 
172     dbus_console = dbus_display_console_new(dd, con);
173     g_ptr_array_insert(dd->consoles, idx, dbus_console);
174     g_dbus_object_manager_server_export(dd->server,
175                                         G_DBUS_OBJECT_SKELETON(dbus_console));
176     return true;
177 }
178 
179 static void
180 dbus_display_complete(UserCreatable *uc, Error **errp)
181 {
182     DBusDisplay *dd = DBUS_DISPLAY(uc);
183     g_autoptr(GError) err = NULL;
184     g_autofree char *uuid = qemu_uuid_unparse_strdup(&qemu_uuid);
185     g_autoptr(GArray) consoles = NULL;
186     GVariant *console_ids;
187     int idx;
188 
189     if (!object_resolve_path_type("", TYPE_DBUS_DISPLAY, NULL)) {
190         error_setg(errp, "There is already an instance of %s",
191                    TYPE_DBUS_DISPLAY);
192         return;
193     }
194 
195     if (dd->p2p) {
196         /* wait for dbus_display_add_client() */
197         dbus_display = dd;
198     } else if (dd->dbus_addr && *dd->dbus_addr) {
199         dd->bus = g_dbus_connection_new_for_address_sync(dd->dbus_addr,
200                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
201                         G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
202                         NULL, NULL, &err);
203     } else {
204         dd->bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &err);
205     }
206     if (err) {
207         error_setg(errp, "failed to connect to DBus: %s", err->message);
208         return;
209     }
210 
211     if (dd->audiodev && *dd->audiodev) {
212         AudioState *audio_state = audio_state_by_name(dd->audiodev);
213         if (!audio_state) {
214             error_setg(errp, "Audiodev '%s' not found", dd->audiodev);
215             return;
216         }
217         if (!g_str_equal(audio_state->drv->name, "dbus")) {
218             error_setg(errp, "Audiodev '%s' is not compatible with DBus",
219                        dd->audiodev);
220             return;
221         }
222         audio_state->drv->set_dbus_server(audio_state, dd->server);
223     }
224 
225     consoles = g_array_new(FALSE, FALSE, sizeof(guint32));
226     for (idx = 0;; idx++) {
227         if (!qemu_console_lookup_by_index(idx)) {
228             break;
229         }
230         if (!dbus_display_add_console(dd, idx, errp)) {
231             return;
232         }
233         g_array_append_val(consoles, idx);
234     }
235 
236     console_ids = g_variant_new_from_data(
237         G_VARIANT_TYPE("au"),
238         consoles->data, consoles->len * sizeof(guint32), TRUE,
239         (GDestroyNotify)g_array_unref, consoles);
240     g_steal_pointer(&consoles);
241     g_object_set(dd->iface,
242                  "name", qemu_name ?: "QEMU " QEMU_VERSION,
243                  "uuid", uuid,
244                  "console-ids", console_ids,
245                  NULL);
246 
247     if (dd->bus) {
248         g_dbus_object_manager_server_set_connection(dd->server, dd->bus);
249         g_bus_own_name_on_connection(dd->bus, "org.qemu",
250                                      G_BUS_NAME_OWNER_FLAGS_NONE,
251                                      NULL, NULL, NULL, NULL);
252     }
253 }
254 
255 static void
256 dbus_display_add_client_ready(GObject *source_object,
257                               GAsyncResult *res,
258                               gpointer user_data)
259 {
260     g_autoptr(GError) err = NULL;
261     g_autoptr(GDBusConnection) conn = NULL;
262 
263     g_clear_object(&dbus_display->add_client_cancellable);
264 
265     conn = g_dbus_connection_new_finish(res, &err);
266     if (!conn) {
267         error_printf("Failed to accept D-Bus client: %s", err->message);
268     }
269 
270     g_dbus_object_manager_server_set_connection(dbus_display->server, conn);
271 }
272 
273 
274 static bool
275 dbus_display_add_client(int csock, Error **errp)
276 {
277     g_autoptr(GError) err = NULL;
278     g_autoptr(GSocket) socket = NULL;
279     g_autoptr(GSocketConnection) conn = NULL;
280     g_autofree char *guid = g_dbus_generate_guid();
281 
282     if (!dbus_display) {
283         error_setg(errp, "p2p connections not accepted in bus mode");
284         return false;
285     }
286 
287     if (dbus_display->add_client_cancellable) {
288         g_cancellable_cancel(dbus_display->add_client_cancellable);
289     }
290 
291     socket = g_socket_new_from_fd(csock, &err);
292     if (!socket) {
293         error_setg(errp, "Failed to setup D-Bus socket: %s", err->message);
294         return false;
295     }
296 
297     conn = g_socket_connection_factory_create_connection(socket);
298 
299     dbus_display->add_client_cancellable = g_cancellable_new();
300 
301     g_dbus_connection_new(G_IO_STREAM(conn),
302                           guid,
303                           G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
304                           NULL,
305                           dbus_display->add_client_cancellable,
306                           dbus_display_add_client_ready,
307                           NULL);
308 
309     return true;
310 }
311 
312 static bool
313 get_dbus_p2p(Object *o, Error **errp)
314 {
315     DBusDisplay *dd = DBUS_DISPLAY(o);
316 
317     return dd->p2p;
318 }
319 
320 static void
321 set_dbus_p2p(Object *o, bool p2p, Error **errp)
322 {
323     DBusDisplay *dd = DBUS_DISPLAY(o);
324 
325     dd->p2p = p2p;
326 }
327 
328 static char *
329 get_dbus_addr(Object *o, Error **errp)
330 {
331     DBusDisplay *dd = DBUS_DISPLAY(o);
332 
333     return g_strdup(dd->dbus_addr);
334 }
335 
336 static void
337 set_dbus_addr(Object *o, const char *str, Error **errp)
338 {
339     DBusDisplay *dd = DBUS_DISPLAY(o);
340 
341     g_free(dd->dbus_addr);
342     dd->dbus_addr = g_strdup(str);
343 }
344 
345 static char *
346 get_audiodev(Object *o, Error **errp)
347 {
348     DBusDisplay *dd = DBUS_DISPLAY(o);
349 
350     return g_strdup(dd->audiodev);
351 }
352 
353 static void
354 set_audiodev(Object *o, const char *str, Error **errp)
355 {
356     DBusDisplay *dd = DBUS_DISPLAY(o);
357 
358     g_free(dd->audiodev);
359     dd->audiodev = g_strdup(str);
360 }
361 
362 
363 static int
364 get_gl_mode(Object *o, Error **errp)
365 {
366     DBusDisplay *dd = DBUS_DISPLAY(o);
367 
368     return dd->gl_mode;
369 }
370 
371 static void
372 set_gl_mode(Object *o, int val, Error **errp)
373 {
374     DBusDisplay *dd = DBUS_DISPLAY(o);
375 
376     dd->gl_mode = val;
377 }
378 
379 static void
380 dbus_display_class_init(ObjectClass *oc, void *data)
381 {
382     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
383 
384     ucc->complete = dbus_display_complete;
385     object_class_property_add_bool(oc, "p2p", get_dbus_p2p, set_dbus_p2p);
386     object_class_property_add_str(oc, "addr", get_dbus_addr, set_dbus_addr);
387     object_class_property_add_str(oc, "audiodev", get_audiodev, set_audiodev);
388     object_class_property_add_enum(oc, "gl-mode",
389                                    "DisplayGLMode", &DisplayGLMode_lookup,
390                                    get_gl_mode, set_gl_mode);
391 }
392 
393 #define TYPE_CHARDEV_VC "chardev-vc"
394 
395 typedef struct DBusVCClass {
396     DBusChardevClass parent_class;
397 
398     void (*parent_parse)(QemuOpts *opts, ChardevBackend *b, Error **errp);
399 } DBusVCClass;
400 
401 DECLARE_CLASS_CHECKERS(DBusVCClass, DBUS_VC,
402                        TYPE_CHARDEV_VC)
403 
404 static void
405 dbus_vc_parse(QemuOpts *opts, ChardevBackend *backend,
406               Error **errp)
407 {
408     DBusVCClass *klass = DBUS_VC_CLASS(object_class_by_name(TYPE_CHARDEV_VC));
409     const char *name = qemu_opt_get(opts, "name");
410     const char *id = qemu_opts_id(opts);
411 
412     if (name == NULL) {
413         if (g_str_has_prefix(id, "compat_monitor")) {
414             name = "org.qemu.monitor.hmp.0";
415         } else if (g_str_has_prefix(id, "serial")) {
416             name = "org.qemu.console.serial.0";
417         } else {
418             name = "";
419         }
420         if (!qemu_opt_set(opts, "name", name, errp)) {
421             return;
422         }
423     }
424 
425     klass->parent_parse(opts, backend, errp);
426 }
427 
428 static void
429 dbus_vc_class_init(ObjectClass *oc, void *data)
430 {
431     DBusVCClass *klass = DBUS_VC_CLASS(oc);
432     ChardevClass *cc = CHARDEV_CLASS(oc);
433 
434     klass->parent_parse = cc->parse;
435     cc->parse = dbus_vc_parse;
436 }
437 
438 static const TypeInfo dbus_vc_type_info = {
439     .name = TYPE_CHARDEV_VC,
440     .parent = TYPE_CHARDEV_DBUS,
441     .class_size = sizeof(DBusVCClass),
442     .class_init = dbus_vc_class_init,
443 };
444 
445 static void
446 early_dbus_init(DisplayOptions *opts)
447 {
448     DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_OFF;
449 
450     if (mode != DISPLAYGL_MODE_OFF) {
451         if (egl_rendernode_init(opts->u.dbus.rendernode, mode) < 0) {
452             error_report("dbus: render node init failed");
453             exit(1);
454         }
455 
456         display_opengl = 1;
457     }
458 
459     type_register(&dbus_vc_type_info);
460 }
461 
462 static void
463 dbus_init(DisplayState *ds, DisplayOptions *opts)
464 {
465     DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_OFF;
466 
467     if (opts->u.dbus.addr && opts->u.dbus.p2p) {
468         error_report("dbus: can't accept both addr=X and p2p=yes options");
469         exit(1);
470     }
471 
472     using_dbus_display = 1;
473 
474     object_new_with_props(TYPE_DBUS_DISPLAY,
475                           object_get_objects_root(),
476                           "dbus-display", &error_fatal,
477                           "addr", opts->u.dbus.addr ?: "",
478                           "audiodev", opts->u.dbus.audiodev ?: "",
479                           "gl-mode", DisplayGLMode_str(mode),
480                           "p2p", yes_no(opts->u.dbus.p2p),
481                           NULL);
482 }
483 
484 static const TypeInfo dbus_display_info = {
485     .name = TYPE_DBUS_DISPLAY,
486     .parent = TYPE_OBJECT,
487     .instance_size = sizeof(DBusDisplay),
488     .instance_init = dbus_display_init,
489     .instance_finalize = dbus_display_finalize,
490     .class_init = dbus_display_class_init,
491     .interfaces = (InterfaceInfo[]) {
492         { TYPE_USER_CREATABLE },
493         { }
494     }
495 };
496 
497 static QemuDisplay qemu_display_dbus = {
498     .type       = DISPLAY_TYPE_DBUS,
499     .early_init = early_dbus_init,
500     .init       = dbus_init,
501 };
502 
503 static void register_dbus(void)
504 {
505     qemu_dbus_display = (struct QemuDBusDisplayOps) {
506         .add_client = dbus_display_add_client,
507     };
508     type_register_static(&dbus_display_info);
509     qemu_display_register(&qemu_display_dbus);
510 }
511 
512 type_init(register_dbus);
513 
514 #ifdef CONFIG_OPENGL
515 module_dep("ui-opengl");
516 #endif
517