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