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
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"
41 static DBusDisplay
*dbus_display
;
43 static QEMUGLContext
dbus_create_context(DisplayGLCtx
*dgc
,
46 eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
,
48 return qemu_egl_create_context(dgc
, params
);
52 dbus_is_compatible_dcl(DisplayGLCtx
*dgc
,
53 DisplayChangeListener
*dcl
)
55 return dcl
->ops
== &dbus_gl_dcl_ops
|| dcl
->ops
== &dbus_console_dcl_ops
;
59 dbus_create_texture(DisplayGLCtx
*ctx
, DisplaySurface
*surface
)
61 surface_gl_create_texture(ctx
->gls
, surface
);
65 dbus_destroy_texture(DisplayGLCtx
*ctx
, DisplaySurface
*surface
)
67 surface_gl_destroy_texture(ctx
->gls
, surface
);
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
);
91 dbus_display_notifier_add(Notifier
*notifier
)
93 notifier_list_add(&dbus_display_notifiers
, notifier
);
97 dbus_display_notifier_remove(Notifier
*notifier
)
99 notifier_remove(notifier
);
103 dbus_display_notify(DBusDisplayEvent
*event
)
105 notifier_list_notify(&dbus_display_notifiers
, event
);
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
);
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
);
159 dbus_display_add_console(DBusDisplay
*dd
, int idx
, Error
**errp
)
162 DBusDisplayConsole
*dbus_console
;
164 con
= qemu_console_lookup_by_index(idx
);
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
));
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
;
189 if (!object_resolve_path_type("", TYPE_DBUS_DISPLAY
, NULL
)) {
190 error_setg(errp
, "There is already an instance of %s",
196 /* wait for dbus_display_add_client() */
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
,
204 dd
->bus
= g_bus_get_sync(G_BUS_TYPE_SESSION
, NULL
, &err
);
207 error_setg(errp
, "failed to connect to DBus: %s", err
->message
);
211 if (dd
->audiodev
&& *dd
->audiodev
) {
212 AudioState
*audio_state
= audio_state_by_name(dd
->audiodev
);
214 error_setg(errp
, "Audiodev '%s' not found", dd
->audiodev
);
217 if (!g_str_equal(audio_state
->drv
->name
, "dbus")) {
218 error_setg(errp
, "Audiodev '%s' is not compatible with DBus",
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
)) {
230 if (!dbus_display_add_console(dd
, idx
, errp
)) {
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
,
244 "console-ids", console_ids
,
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
);
256 dbus_display_add_client_ready(GObject
*source_object
,
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
);
267 error_printf("Failed to accept D-Bus client: %s", err
->message
);
270 g_dbus_object_manager_server_set_connection(dbus_display
->server
, conn
);
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();
283 error_setg(errp
, "p2p connections not accepted in bus mode");
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
);
293 error_setg(errp
, "Failed to setup D-Bus socket: %s", err
->message
);
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
),
303 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
,
305 dbus_display
->add_client_cancellable
,
306 dbus_display_add_client_ready
,
313 get_dbus_p2p(Object
*o
, Error
**errp
)
315 DBusDisplay
*dd
= DBUS_DISPLAY(o
);
321 set_dbus_p2p(Object
*o
, bool p2p
, Error
**errp
)
323 DBusDisplay
*dd
= DBUS_DISPLAY(o
);
329 get_dbus_addr(Object
*o
, Error
**errp
)
331 DBusDisplay
*dd
= DBUS_DISPLAY(o
);
333 return g_strdup(dd
->dbus_addr
);
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
);
346 get_audiodev(Object
*o
, Error
**errp
)
348 DBusDisplay
*dd
= DBUS_DISPLAY(o
);
350 return g_strdup(dd
->audiodev
);
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
);
364 get_gl_mode(Object
*o
, Error
**errp
)
366 DBusDisplay
*dd
= DBUS_DISPLAY(o
);
372 set_gl_mode(Object
*o
, int val
, Error
**errp
)
374 DBusDisplay
*dd
= DBUS_DISPLAY(o
);
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
);
401 DECLARE_CLASS_CHECKERS(DBusVCClass
, DBUS_VC
,
405 dbus_vc_parse(QemuOpts
*opts
, ChardevBackend
*backend
,
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
);
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";
420 if (!qemu_opt_set(opts
, "name", name
, errp
)) {
425 klass
->parent_parse(opts
, backend
, errp
);
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
,
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");
459 type_register(&dbus_vc_type_info
);
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");
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
),
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
,
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
);
515 module_dep("ui-opengl");