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