portal.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* portal.c
  2. *
  3. * Copyright 2021 Georges Basile Stavracas Neto <[email protected]>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * SPDX-License-Identifier: GPL-2.0-or-later
  19. */
  20. #include "portal.h"
  21. #include "pipewire.h"
  22. static GDBusConnection *connection = NULL;
  23. static GDBusProxy *proxy = NULL;
  24. static void ensure_proxy(void)
  25. {
  26. g_autoptr(GError) error = NULL;
  27. if (!connection) {
  28. connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
  29. if (error) {
  30. blog(LOG_WARNING,
  31. "[portals] Error retrieving D-Bus connection: %s",
  32. error->message);
  33. return;
  34. }
  35. }
  36. if (!proxy) {
  37. proxy = g_dbus_proxy_new_sync(
  38. connection, G_DBUS_PROXY_FLAGS_NONE, NULL,
  39. "org.freedesktop.portal.Desktop",
  40. "/org/freedesktop/portal/desktop",
  41. "org.freedesktop.portal.ScreenCast", NULL, &error);
  42. if (error) {
  43. blog(LOG_WARNING,
  44. "[portals] Error retrieving D-Bus proxy: %s",
  45. error->message);
  46. return;
  47. }
  48. }
  49. }
  50. uint32_t portal_get_available_capture_types(void)
  51. {
  52. g_autoptr(GVariant) cached_source_types = NULL;
  53. uint32_t available_source_types;
  54. ensure_proxy();
  55. if (!proxy)
  56. return 0;
  57. cached_source_types =
  58. g_dbus_proxy_get_cached_property(proxy, "AvailableSourceTypes");
  59. available_source_types =
  60. cached_source_types ? g_variant_get_uint32(cached_source_types)
  61. : 0;
  62. return available_source_types;
  63. }
  64. uint32_t portal_get_screencast_version(void)
  65. {
  66. g_autoptr(GVariant) cached_version = NULL;
  67. uint32_t version;
  68. ensure_proxy();
  69. if (!proxy)
  70. return 0;
  71. cached_version = g_dbus_proxy_get_cached_property(proxy, "version");
  72. version = cached_version ? g_variant_get_uint32(cached_version) : 0;
  73. return version;
  74. }
  75. GDBusConnection *portal_get_dbus_connection(void)
  76. {
  77. ensure_proxy();
  78. return connection;
  79. }
  80. GDBusProxy *portal_get_dbus_proxy(void)
  81. {
  82. ensure_proxy();
  83. return proxy;
  84. }