test/avocado/machine_aspeed.py: Move OpenBMC tests
[qemu.git] / ui / dbus.c
blob7a87612379e802566c95a93efd46517e06b0add4
1 /*
2 * QEMU DBus display
4 * Copyright (c) 2021 Marc-André Lureau <marcandre.lureau@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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"
39 #include "dbus.h"
41 static DBusDisplay *dbus_display;
43 static QEMUGLContext dbus_create_context(DisplayGLCtx *dgc,
44 QEMUGLParams *params)
46 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
47 qemu_egl_rn_ctx);
48 return qemu_egl_create_context(dgc, params);
51 static bool
52 dbus_is_compatible_dcl(DisplayGLCtx *dgc,
53 DisplayChangeListener *dcl)
55 return dcl->ops == &dbus_gl_dcl_ops || dcl->ops == &dbus_console_dcl_ops;
58 static void
59 dbus_create_texture(DisplayGLCtx *ctx, DisplaySurface *surface)
61 surface_gl_create_texture(ctx->gls, surface);
64 static void
65 dbus_destroy_texture(DisplayGLCtx *ctx, DisplaySurface *surface)
67 surface_gl_destroy_texture(ctx->gls, surface);
70 static void
71 dbus_update_texture(DisplayGLCtx *ctx, DisplaySurface *surface,
72 int x, int y, int w, int h)
74 surface_gl_update_texture(ctx->gls, surface, x, y, w, h);
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,
87 static NotifierList dbus_display_notifiers =
88 NOTIFIER_LIST_INITIALIZER(dbus_display_notifiers);
90 void
91 dbus_display_notifier_add(Notifier *notifier)
93 notifier_list_add(&dbus_display_notifiers, notifier);
96 static void
97 dbus_display_notifier_remove(Notifier *notifier)
99 notifier_remove(notifier);
102 void
103 dbus_display_notify(DBusDisplayEvent *event)
105 notifier_list_notify(&dbus_display_notifiers, event);
108 static void
109 dbus_display_init(Object *o)
111 DBusDisplay *dd = DBUS_DISPLAY(o);
112 g_autoptr(GDBusObjectSkeleton) vm = NULL;
114 dd->glctx.ops = &dbus_gl_ops;
115 if (display_opengl) {
116 dd->glctx.gls = qemu_gl_init_shader();
118 dd->iface = qemu_dbus_display1_vm_skeleton_new();
119 dd->consoles = g_ptr_array_new_with_free_func(g_object_unref);
121 dd->server = g_dbus_object_manager_server_new(DBUS_DISPLAY1_ROOT);
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);
128 dbus_clipboard_init(dd);
129 dbus_chardev_init(dd);
132 static void
133 dbus_display_finalize(Object *o)
135 DBusDisplay *dd = DBUS_DISPLAY(o);
137 if (dd->notifier.notify) {
138 dbus_display_notifier_remove(&dd->notifier);
141 qemu_clipboard_peer_unregister(&dd->clipboard_peer);
142 g_clear_object(&dd->clipboard);
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);
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;
158 static bool
159 dbus_display_add_console(DBusDisplay *dd, int idx, Error **errp)
161 QemuConsole *con;
162 DBusDisplayConsole *dbus_console;
164 con = qemu_console_lookup_by_index(idx);
165 assert(con);
167 if (qemu_console_is_graphic(con) &&
168 dd->gl_mode != DISPLAYGL_MODE_OFF) {
169 qemu_console_set_display_gl_ctx(con, &dd->glctx);
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;
179 static void
180 dbus_display_complete(UserCreatable *uc, Error **errp)
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;
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;
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);
206 if (err) {
207 error_setg(errp, "failed to connect to DBus: %s", err->message);
208 return;
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;
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;
222 audio_state->drv->set_dbus_server(audio_state, dd->server);
225 consoles = g_array_new(FALSE, FALSE, sizeof(guint32));
226 for (idx = 0;; idx++) {
227 if (!qemu_console_lookup_by_index(idx)) {
228 break;
230 if (!dbus_display_add_console(dd, idx, errp)) {
231 return;
233 g_array_append_val(consoles, idx);
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);
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);
255 static void
256 dbus_display_add_client_ready(GObject *source_object,
257 GAsyncResult *res,
258 gpointer user_data)
260 g_autoptr(GError) err = NULL;
261 g_autoptr(GDBusConnection) conn = NULL;
263 g_clear_object(&dbus_display->add_client_cancellable);
265 conn = g_dbus_connection_new_finish(res, &err);
266 if (!conn) {
267 error_printf("Failed to accept D-Bus client: %s", err->message);
270 g_dbus_object_manager_server_set_connection(dbus_display->server, conn);
274 static bool
275 dbus_display_add_client(int csock, Error **errp)
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();
282 if (!dbus_display) {
283 error_setg(errp, "p2p connections not accepted in bus mode");
284 return false;
287 if (dbus_display->add_client_cancellable) {
288 g_cancellable_cancel(dbus_display->add_client_cancellable);
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;
297 conn = g_socket_connection_factory_create_connection(socket);
299 dbus_display->add_client_cancellable = g_cancellable_new();
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);
309 return true;
312 static bool
313 get_dbus_p2p(Object *o, Error **errp)
315 DBusDisplay *dd = DBUS_DISPLAY(o);
317 return dd->p2p;
320 static void
321 set_dbus_p2p(Object *o, bool p2p, Error **errp)
323 DBusDisplay *dd = DBUS_DISPLAY(o);
325 dd->p2p = p2p;
328 static char *
329 get_dbus_addr(Object *o, Error **errp)
331 DBusDisplay *dd = DBUS_DISPLAY(o);
333 return g_strdup(dd->dbus_addr);
336 static void
337 set_dbus_addr(Object *o, const char *str, Error **errp)
339 DBusDisplay *dd = DBUS_DISPLAY(o);
341 g_free(dd->dbus_addr);
342 dd->dbus_addr = g_strdup(str);
345 static char *
346 get_audiodev(Object *o, Error **errp)
348 DBusDisplay *dd = DBUS_DISPLAY(o);
350 return g_strdup(dd->audiodev);
353 static void
354 set_audiodev(Object *o, const char *str, Error **errp)
356 DBusDisplay *dd = DBUS_DISPLAY(o);
358 g_free(dd->audiodev);
359 dd->audiodev = g_strdup(str);
363 static int
364 get_gl_mode(Object *o, Error **errp)
366 DBusDisplay *dd = DBUS_DISPLAY(o);
368 return dd->gl_mode;
371 static void
372 set_gl_mode(Object *o, int val, Error **errp)
374 DBusDisplay *dd = DBUS_DISPLAY(o);
376 dd->gl_mode = val;
379 static void
380 dbus_display_class_init(ObjectClass *oc, void *data)
382 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
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);
393 #define TYPE_CHARDEV_VC "chardev-vc"
395 typedef struct DBusVCClass {
396 DBusChardevClass parent_class;
398 void (*parent_parse)(QemuOpts *opts, ChardevBackend *b, Error **errp);
399 } DBusVCClass;
401 DECLARE_CLASS_CHECKERS(DBusVCClass, DBUS_VC,
402 TYPE_CHARDEV_VC)
404 static void
405 dbus_vc_parse(QemuOpts *opts, ChardevBackend *backend,
406 Error **errp)
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);
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 = "";
420 if (!qemu_opt_set(opts, "name", name, errp)) {
421 return;
425 klass->parent_parse(opts, backend, errp);
428 static void
429 dbus_vc_class_init(ObjectClass *oc, void *data)
431 DBusVCClass *klass = DBUS_VC_CLASS(oc);
432 ChardevClass *cc = CHARDEV_CLASS(oc);
434 klass->parent_parse = cc->parse;
435 cc->parse = dbus_vc_parse;
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,
445 static void
446 early_dbus_init(DisplayOptions *opts)
448 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_OFF;
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);
456 display_opengl = 1;
459 type_register(&dbus_vc_type_info);
462 static void
463 dbus_init(DisplayState *ds, DisplayOptions *opts)
465 DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_MODE_OFF;
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);
472 using_dbus_display = 1;
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);
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 },
497 static QemuDisplay qemu_display_dbus = {
498 .type = DISPLAY_TYPE_DBUS,
499 .early_init = early_dbus_init,
500 .init = dbus_init,
503 static void register_dbus(void)
505 qemu_dbus_display = (struct QemuDBusDisplayOps) {
506 .add_client = dbus_display_add_client,
508 type_register_static(&dbus_display_info);
509 qemu_display_register(&qemu_display_dbus);
512 type_init(register_dbus);
514 #ifdef CONFIG_OPENGL
515 module_dep("ui-opengl");
516 #endif