portal.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <util/dstr.h>
  23. #define REQUEST_PATH "/org/freedesktop/portal/desktop/request/%s/obs%u"
  24. #define SESSION_PATH "/org/freedesktop/portal/desktop/session/%s/obs%u"
  25. static GDBusConnection *connection = NULL;
  26. static void ensure_connection(void)
  27. {
  28. g_autoptr(GError) error = NULL;
  29. if (!connection) {
  30. connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
  31. if (error) {
  32. blog(LOG_WARNING,
  33. "[portals] Error retrieving D-Bus connection: %s",
  34. error->message);
  35. return;
  36. }
  37. }
  38. }
  39. char *get_sender_name(void)
  40. {
  41. char *sender_name;
  42. char *aux;
  43. ensure_connection();
  44. sender_name =
  45. bstrdup(g_dbus_connection_get_unique_name(connection) + 1);
  46. /* Replace dots by underscores */
  47. while ((aux = strstr(sender_name, ".")) != NULL)
  48. *aux = '_';
  49. return sender_name;
  50. }
  51. GDBusConnection *portal_get_dbus_connection(void)
  52. {
  53. ensure_connection();
  54. return connection;
  55. }
  56. void portal_create_request_path(char **out_path, char **out_token)
  57. {
  58. static uint32_t request_token_count = 0;
  59. request_token_count++;
  60. if (out_token) {
  61. struct dstr str;
  62. dstr_init(&str);
  63. dstr_printf(&str, "obs%u", request_token_count);
  64. *out_token = str.array;
  65. }
  66. if (out_path) {
  67. char *sender_name;
  68. struct dstr str;
  69. sender_name = get_sender_name();
  70. dstr_init(&str);
  71. dstr_printf(&str, REQUEST_PATH, sender_name,
  72. request_token_count);
  73. *out_path = str.array;
  74. bfree(sender_name);
  75. }
  76. }
  77. void portal_create_session_path(char **out_path, char **out_token)
  78. {
  79. static uint32_t session_token_count = 0;
  80. session_token_count++;
  81. if (out_token) {
  82. struct dstr str;
  83. dstr_init(&str);
  84. dstr_printf(&str, "obs%u", session_token_count);
  85. *out_token = str.array;
  86. }
  87. if (out_path) {
  88. char *sender_name;
  89. struct dstr str;
  90. sender_name = get_sender_name();
  91. dstr_init(&str);
  92. dstr_printf(&str, SESSION_PATH, sender_name,
  93. session_token_count);
  94. *out_path = str.array;
  95. bfree(sender_name);
  96. }
  97. }