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