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