dbus.c 15 KB

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