2
0

dbus.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. #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. #ifdef CONFIG_GBM
  48. eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
  49. qemu_egl_rn_ctx);
  50. #endif
  51. return qemu_egl_create_context(dgc, params);
  52. }
  53. static bool
  54. dbus_is_compatible_dcl(DisplayGLCtx *dgc,
  55. DisplayChangeListener *dcl)
  56. {
  57. return
  58. #ifdef CONFIG_GBM
  59. dcl->ops == &dbus_gl_dcl_ops ||
  60. #endif
  61. dcl->ops == &dbus_console_dcl_ops;
  62. }
  63. static void
  64. dbus_create_texture(DisplayGLCtx *ctx, DisplaySurface *surface)
  65. {
  66. surface_gl_create_texture(ctx->gls, surface);
  67. }
  68. static void
  69. dbus_destroy_texture(DisplayGLCtx *ctx, DisplaySurface *surface)
  70. {
  71. surface_gl_destroy_texture(ctx->gls, surface);
  72. }
  73. static void
  74. dbus_update_texture(DisplayGLCtx *ctx, DisplaySurface *surface,
  75. int x, int y, int w, int h)
  76. {
  77. surface_gl_update_texture(ctx->gls, surface, x, y, w, h);
  78. }
  79. static const DisplayGLCtxOps dbus_gl_ops = {
  80. .dpy_gl_ctx_is_compatible_dcl = dbus_is_compatible_dcl,
  81. .dpy_gl_ctx_create = dbus_create_context,
  82. .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
  83. .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
  84. .dpy_gl_ctx_create_texture = dbus_create_texture,
  85. .dpy_gl_ctx_destroy_texture = dbus_destroy_texture,
  86. .dpy_gl_ctx_update_texture = dbus_update_texture,
  87. };
  88. #endif
  89. static NotifierList dbus_display_notifiers =
  90. NOTIFIER_LIST_INITIALIZER(dbus_display_notifiers);
  91. void
  92. dbus_display_notifier_add(Notifier *notifier)
  93. {
  94. notifier_list_add(&dbus_display_notifiers, notifier);
  95. }
  96. static void
  97. dbus_display_notifier_remove(Notifier *notifier)
  98. {
  99. notifier_remove(notifier);
  100. }
  101. void
  102. dbus_display_notify(DBusDisplayEvent *event)
  103. {
  104. notifier_list_notify(&dbus_display_notifiers, event);
  105. }
  106. static void
  107. dbus_display_init(Object *o)
  108. {
  109. DBusDisplay *dd = DBUS_DISPLAY(o);
  110. g_autoptr(GDBusObjectSkeleton) vm = NULL;
  111. #ifdef CONFIG_OPENGL
  112. dd->glctx.ops = &dbus_gl_ops;
  113. if (display_opengl) {
  114. dd->glctx.gls = qemu_gl_init_shader();
  115. }
  116. #endif
  117. dd->iface = qemu_dbus_display1_vm_skeleton_new();
  118. dd->consoles = g_ptr_array_new_with_free_func(g_object_unref);
  119. dd->server = g_dbus_object_manager_server_new(DBUS_DISPLAY1_ROOT);
  120. vm = g_dbus_object_skeleton_new(DBUS_DISPLAY1_ROOT "/VM");
  121. g_dbus_object_skeleton_add_interface(
  122. vm, G_DBUS_INTERFACE_SKELETON(dd->iface));
  123. g_dbus_object_manager_server_export(dd->server, vm);
  124. dbus_clipboard_init(dd);
  125. dbus_chardev_init(dd);
  126. }
  127. static void
  128. dbus_display_finalize(Object *o)
  129. {
  130. DBusDisplay *dd = DBUS_DISPLAY(o);
  131. if (dd->notifier.notify) {
  132. dbus_display_notifier_remove(&dd->notifier);
  133. }
  134. qemu_clipboard_peer_unregister(&dd->clipboard_peer);
  135. g_clear_object(&dd->clipboard);
  136. g_clear_object(&dd->server);
  137. g_clear_pointer(&dd->consoles, g_ptr_array_unref);
  138. if (dd->add_client_cancellable) {
  139. g_cancellable_cancel(dd->add_client_cancellable);
  140. }
  141. g_clear_object(&dd->add_client_cancellable);
  142. g_clear_object(&dd->bus);
  143. g_clear_object(&dd->iface);
  144. g_free(dd->dbus_addr);
  145. g_free(dd->audiodev);
  146. #ifdef CONFIG_OPENGL
  147. g_clear_pointer(&dd->glctx.gls, qemu_gl_fini_shader);
  148. #endif
  149. dbus_display = NULL;
  150. }
  151. static bool
  152. dbus_display_add_console(DBusDisplay *dd, int idx, Error **errp)
  153. {
  154. QemuConsole *con;
  155. DBusDisplayConsole *dbus_console;
  156. con = qemu_console_lookup_by_index(idx);
  157. assert(con);
  158. if (qemu_console_is_graphic(con) &&
  159. dd->gl_mode != DISPLAYGL_MODE_OFF) {
  160. qemu_console_set_display_gl_ctx(con, &dd->glctx);
  161. }
  162. dbus_console = dbus_display_console_new(dd, con);
  163. g_ptr_array_insert(dd->consoles, idx, dbus_console);
  164. g_dbus_object_manager_server_export(dd->server,
  165. G_DBUS_OBJECT_SKELETON(dbus_console));
  166. return true;
  167. }
  168. static void
  169. dbus_display_complete(UserCreatable *uc, Error **errp)
  170. {
  171. DBusDisplay *dd = DBUS_DISPLAY(uc);
  172. g_autoptr(GError) err = NULL;
  173. g_autofree char *uuid = qemu_uuid_unparse_strdup(&qemu_uuid);
  174. g_autoptr(GArray) consoles = NULL;
  175. GVariant *console_ids;
  176. int idx;
  177. if (!object_resolve_path_type("", TYPE_DBUS_DISPLAY, NULL)) {
  178. error_setg(errp, "There is already an instance of %s",
  179. TYPE_DBUS_DISPLAY);
  180. return;
  181. }
  182. if (dd->p2p) {
  183. /* wait for dbus_display_add_client() */
  184. dbus_display = dd;
  185. } else if (dd->dbus_addr && *dd->dbus_addr) {
  186. dd->bus = g_dbus_connection_new_for_address_sync(dd->dbus_addr,
  187. G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
  188. G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
  189. NULL, NULL, &err);
  190. } else {
  191. dd->bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &err);
  192. }
  193. if (err) {
  194. error_setg(errp, "failed to connect to DBus: %s", err->message);
  195. return;
  196. }
  197. if (dd->audiodev && *dd->audiodev) {
  198. AudioState *audio_state = audio_state_by_name(dd->audiodev);
  199. if (!audio_state) {
  200. error_setg(errp, "Audiodev '%s' not found", dd->audiodev);
  201. return;
  202. }
  203. if (!g_str_equal(audio_state->drv->name, "dbus")) {
  204. error_setg(errp, "Audiodev '%s' is not compatible with DBus",
  205. dd->audiodev);
  206. return;
  207. }
  208. audio_state->drv->set_dbus_server(audio_state, dd->server, dd->p2p);
  209. }
  210. consoles = g_array_new(FALSE, FALSE, sizeof(guint32));
  211. for (idx = 0;; idx++) {
  212. if (!qemu_console_lookup_by_index(idx)) {
  213. break;
  214. }
  215. if (!dbus_display_add_console(dd, idx, errp)) {
  216. return;
  217. }
  218. g_array_append_val(consoles, idx);
  219. }
  220. console_ids = g_variant_new_from_data(
  221. G_VARIANT_TYPE("au"),
  222. consoles->data, consoles->len * sizeof(guint32), TRUE,
  223. (GDestroyNotify)g_array_unref, consoles);
  224. g_steal_pointer(&consoles);
  225. g_object_set(dd->iface,
  226. "name", qemu_name ?: "QEMU " QEMU_VERSION,
  227. "uuid", uuid,
  228. "console-ids", console_ids,
  229. NULL);
  230. if (dd->bus) {
  231. g_dbus_object_manager_server_set_connection(dd->server, dd->bus);
  232. g_bus_own_name_on_connection(dd->bus, "org.qemu",
  233. G_BUS_NAME_OWNER_FLAGS_NONE,
  234. NULL, NULL, NULL, NULL);
  235. }
  236. }
  237. static void
  238. dbus_display_add_client_ready(GObject *source_object,
  239. GAsyncResult *res,
  240. gpointer user_data)
  241. {
  242. g_autoptr(GError) err = NULL;
  243. g_autoptr(GDBusConnection) conn = NULL;
  244. g_clear_object(&dbus_display->add_client_cancellable);
  245. conn = g_dbus_connection_new_finish(res, &err);
  246. if (!conn) {
  247. error_printf("Failed to accept D-Bus client: %s", err->message);
  248. }
  249. g_dbus_object_manager_server_set_connection(dbus_display->server, conn);
  250. g_dbus_connection_start_message_processing(conn);
  251. }
  252. static bool
  253. dbus_display_add_client(int csock, Error **errp)
  254. {
  255. g_autoptr(GError) err = NULL;
  256. g_autoptr(GSocket) socket = NULL;
  257. g_autoptr(GSocketConnection) conn = NULL;
  258. g_autofree char *guid = g_dbus_generate_guid();
  259. if (!dbus_display) {
  260. error_setg(errp, "p2p connections not accepted in bus mode");
  261. return false;
  262. }
  263. if (dbus_display->add_client_cancellable) {
  264. g_cancellable_cancel(dbus_display->add_client_cancellable);
  265. }
  266. #ifdef WIN32
  267. socket = g_socket_new_from_fd(_get_osfhandle(csock), &err);
  268. #else
  269. socket = g_socket_new_from_fd(csock, &err);
  270. #endif
  271. if (!socket) {
  272. error_setg(errp, "Failed to setup D-Bus socket: %s", err->message);
  273. close(csock);
  274. return false;
  275. }
  276. #ifdef WIN32
  277. /* socket owns the SOCKET handle now, so release our osf handle */
  278. qemu_close_socket_osfhandle(csock);
  279. #endif
  280. conn = g_socket_connection_factory_create_connection(socket);
  281. dbus_display->add_client_cancellable = g_cancellable_new();
  282. g_dbus_connection_new(G_IO_STREAM(conn),
  283. guid,
  284. G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
  285. G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
  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 : DISPLAYGL_MODE_OFF;
  400. if (mode != DISPLAYGL_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(&dbus_vc_type_info);
  408. }
  409. static void
  410. dbus_init(DisplayState *ds, DisplayOptions *opts)
  411. {
  412. DisplayGLMode mode = opts->has_gl ? opts->gl : DISPLAYGL_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. };
  444. static void register_dbus(void)
  445. {
  446. qemu_dbus_display = (struct QemuDBusDisplayOps) {
  447. .add_client = dbus_display_add_client,
  448. };
  449. type_register_static(&dbus_display_info);
  450. qemu_display_register(&qemu_display_dbus);
  451. }
  452. type_init(register_dbus);
  453. #ifdef CONFIG_OPENGL
  454. module_dep("ui-opengl");
  455. #endif