SDL_portaldialog.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "./SDL_dialog.h"
  20. #include "../../core/linux/SDL_dbus.h"
  21. #ifdef SDL_USE_LIBDBUS
  22. #include <errno.h>
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include <unistd.h>
  26. #define PORTAL_DESTINATION "org.freedesktop.portal.Desktop"
  27. #define PORTAL_PATH "/org/freedesktop/portal/desktop"
  28. #define PORTAL_INTERFACE "org.freedesktop.portal.FileChooser"
  29. #define SIGNAL_SENDER "org.freedesktop.portal.Desktop"
  30. #define SIGNAL_INTERFACE "org.freedesktop.portal.Request"
  31. #define SIGNAL_NAME "Response"
  32. #define SIGNAL_FILTER "type='signal', sender='"SIGNAL_SENDER"', interface='"SIGNAL_INTERFACE"', member='"SIGNAL_NAME"', path='"
  33. #define HANDLE_LEN 10
  34. #define WAYLAND_HANDLE_PREFIX "wayland:"
  35. #define X11_HANDLE_PREFIX "x11:"
  36. typedef struct {
  37. SDL_DialogFileCallback callback;
  38. void *userdata;
  39. const char *path;
  40. } SignalCallback;
  41. static void DBus_AppendStringOption(SDL_DBusContext *dbus, DBusMessageIter *options, const char *key, const char *value)
  42. {
  43. DBusMessageIter options_pair, options_value;
  44. dbus->message_iter_open_container(options, DBUS_TYPE_DICT_ENTRY, NULL, &options_pair);
  45. dbus->message_iter_append_basic(&options_pair, DBUS_TYPE_STRING, &key);
  46. dbus->message_iter_open_container(&options_pair, DBUS_TYPE_VARIANT, "s", &options_value);
  47. dbus->message_iter_append_basic(&options_value, DBUS_TYPE_STRING, &value);
  48. dbus->message_iter_close_container(&options_pair, &options_value);
  49. dbus->message_iter_close_container(options, &options_pair);
  50. }
  51. static void DBus_AppendBoolOption(SDL_DBusContext *dbus, DBusMessageIter *options, const char *key, int value)
  52. {
  53. DBusMessageIter options_pair, options_value;
  54. dbus->message_iter_open_container(options, DBUS_TYPE_DICT_ENTRY, NULL, &options_pair);
  55. dbus->message_iter_append_basic(&options_pair, DBUS_TYPE_STRING, &key);
  56. dbus->message_iter_open_container(&options_pair, DBUS_TYPE_VARIANT, "b", &options_value);
  57. dbus->message_iter_append_basic(&options_value, DBUS_TYPE_BOOLEAN, &value);
  58. dbus->message_iter_close_container(&options_pair, &options_value);
  59. dbus->message_iter_close_container(options, &options_pair);
  60. }
  61. static void DBus_AppendFilter(SDL_DBusContext *dbus, DBusMessageIter *parent, const SDL_DialogFileFilter *filter)
  62. {
  63. DBusMessageIter filter_entry, filter_array, filter_array_entry;
  64. char *state = NULL, *patterns, *pattern, *glob_pattern;
  65. int zero = 0;
  66. dbus->message_iter_open_container(parent, DBUS_TYPE_STRUCT, NULL, &filter_entry);
  67. dbus->message_iter_append_basic(&filter_entry, DBUS_TYPE_STRING, &filter->name);
  68. dbus->message_iter_open_container(&filter_entry, DBUS_TYPE_ARRAY, "(us)", &filter_array);
  69. patterns = SDL_strdup(filter->pattern);
  70. if (!patterns) {
  71. SDL_OutOfMemory();
  72. goto cleanup;
  73. }
  74. pattern = SDL_strtok_r(patterns, ";", &state);
  75. while (pattern) {
  76. size_t max_len = SDL_strlen(pattern) + 3;
  77. dbus->message_iter_open_container(&filter_array, DBUS_TYPE_STRUCT, NULL, &filter_array_entry);
  78. dbus->message_iter_append_basic(&filter_array_entry, DBUS_TYPE_UINT32, &zero);
  79. glob_pattern = SDL_calloc(sizeof(char), max_len);
  80. if (!glob_pattern) {
  81. SDL_OutOfMemory();
  82. goto cleanup;
  83. }
  84. glob_pattern[0] = '*';
  85. /* Special case: The '*' filter doesn't need to be rewritten */
  86. if (pattern[0] != '*' || pattern[1]) {
  87. glob_pattern[1] = '.';
  88. SDL_strlcat(glob_pattern + 2, pattern, max_len);
  89. }
  90. dbus->message_iter_append_basic(&filter_array_entry, DBUS_TYPE_STRING, &glob_pattern);
  91. SDL_free(glob_pattern);
  92. dbus->message_iter_close_container(&filter_array, &filter_array_entry);
  93. pattern = SDL_strtok_r(NULL, ";", &state);
  94. }
  95. cleanup:
  96. SDL_free(patterns);
  97. dbus->message_iter_close_container(&filter_entry, &filter_array);
  98. dbus->message_iter_close_container(parent, &filter_entry);
  99. }
  100. static void DBus_AppendFilters(SDL_DBusContext *dbus, DBusMessageIter *options, const SDL_DialogFileFilter *filters)
  101. {
  102. DBusMessageIter options_pair, options_value, options_value_array;
  103. static const char *filters_name = "filters";
  104. dbus->message_iter_open_container(options, DBUS_TYPE_DICT_ENTRY, NULL, &options_pair);
  105. dbus->message_iter_append_basic(&options_pair, DBUS_TYPE_STRING, &filters_name);
  106. dbus->message_iter_open_container(&options_pair, DBUS_TYPE_VARIANT, "a(sa(us))", &options_value);
  107. dbus->message_iter_open_container(&options_value, DBUS_TYPE_ARRAY, "(sa(us))", &options_value_array);
  108. for (const SDL_DialogFileFilter *filter = filters; filter && filter->name && filter->pattern; ++filter) {
  109. DBus_AppendFilter(dbus, &options_value_array, filter);
  110. }
  111. dbus->message_iter_close_container(&options_value, &options_value_array);
  112. dbus->message_iter_close_container(&options_pair, &options_value);
  113. dbus->message_iter_close_container(options, &options_pair);
  114. }
  115. static void DBus_AppendByteArray(SDL_DBusContext *dbus, DBusMessageIter *options, const char *key, const char *value)
  116. {
  117. DBusMessageIter options_pair, options_value, options_array;
  118. dbus->message_iter_open_container(options, DBUS_TYPE_DICT_ENTRY, NULL, &options_pair);
  119. dbus->message_iter_append_basic(&options_pair, DBUS_TYPE_STRING, &key);
  120. dbus->message_iter_open_container(&options_pair, DBUS_TYPE_VARIANT, "ay", &options_value);
  121. dbus->message_iter_open_container(&options_value, DBUS_TYPE_ARRAY, "y", &options_array);
  122. do {
  123. dbus->message_iter_append_basic(&options_array, DBUS_TYPE_BYTE, value);
  124. } while (*value++);
  125. dbus->message_iter_close_container(&options_value, &options_array);
  126. dbus->message_iter_close_container(&options_pair, &options_value);
  127. dbus->message_iter_close_container(options, &options_pair);
  128. }
  129. static DBusHandlerResult DBus_MessageFilter(DBusConnection *conn, DBusMessage *msg, void *data) {
  130. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  131. SignalCallback *signal_data = (SignalCallback *)data;
  132. if (dbus->message_is_signal(msg, SIGNAL_INTERFACE, SIGNAL_NAME)
  133. && dbus->message_has_path(msg, signal_data->path)) {
  134. DBusMessageIter signal_iter, result_array, array_entry, value_entry, uri_entry;
  135. uint32_t result;
  136. size_t length = 2, current = 0;
  137. const char **path;
  138. dbus->message_iter_init(msg, &signal_iter);
  139. /* Check if the parameters are what we expect */
  140. if (dbus->message_iter_get_arg_type(&signal_iter) != DBUS_TYPE_UINT32)
  141. goto not_our_signal;
  142. dbus->message_iter_get_basic(&signal_iter, &result);
  143. if (result == 1) {
  144. /* cancelled */
  145. const char *result_data[] = { NULL };
  146. signal_data->callback(signal_data->userdata, result_data, -1); /* TODO: Set this to the last selected filter */
  147. goto handled;
  148. }
  149. else if (result) {
  150. /* some error occurred */
  151. signal_data->callback(signal_data->userdata, NULL, -1);
  152. goto handled;
  153. }
  154. if (!dbus->message_iter_next(&signal_iter))
  155. goto not_our_signal;
  156. if (dbus->message_iter_get_arg_type(&signal_iter) != DBUS_TYPE_ARRAY)
  157. goto not_our_signal;
  158. dbus->message_iter_recurse(&signal_iter, &result_array);
  159. while (dbus->message_iter_get_arg_type(&result_array) == DBUS_TYPE_DICT_ENTRY)
  160. {
  161. const char *method;
  162. dbus->message_iter_recurse(&result_array, &array_entry);
  163. if (dbus->message_iter_get_arg_type(&array_entry) != DBUS_TYPE_STRING)
  164. goto not_our_signal;
  165. dbus->message_iter_get_basic(&array_entry, &method);
  166. if (!SDL_strcmp(method, "uris")) {
  167. /* we only care about the selected file paths */
  168. break;
  169. }
  170. if (!dbus->message_iter_next(&result_array))
  171. goto not_our_signal;
  172. }
  173. if (!dbus->message_iter_next(&array_entry))
  174. goto not_our_signal;
  175. if (dbus->message_iter_get_arg_type(&array_entry) != DBUS_TYPE_VARIANT)
  176. goto not_our_signal;
  177. dbus->message_iter_recurse(&array_entry, &value_entry);
  178. if (dbus->message_iter_get_arg_type(&value_entry) != DBUS_TYPE_ARRAY)
  179. goto not_our_signal;
  180. dbus->message_iter_recurse(&value_entry, &uri_entry);
  181. path = SDL_malloc(sizeof(const char *) * length);
  182. if (!path) {
  183. SDL_OutOfMemory();
  184. signal_data->callback(signal_data->userdata, NULL, -1);
  185. goto cleanup;
  186. }
  187. while (dbus->message_iter_get_arg_type(&uri_entry) == DBUS_TYPE_STRING)
  188. {
  189. if (current >= length - 1) {
  190. ++length;
  191. path = SDL_realloc(path, sizeof(const char *) * length);
  192. if (!path) {
  193. SDL_OutOfMemory();
  194. signal_data->callback(signal_data->userdata, NULL, -1);
  195. goto cleanup;
  196. }
  197. }
  198. dbus->message_iter_get_basic(&uri_entry, path + current);
  199. dbus->message_iter_next(&uri_entry);
  200. ++current;
  201. }
  202. path[length - 1] = NULL;
  203. signal_data->callback(signal_data->userdata, path, -1); /* TODO: Fetch the index of the filter that was used */
  204. cleanup:
  205. dbus->connection_remove_filter(conn, &DBus_MessageFilter, signal_data);
  206. SDL_free(path);
  207. SDL_free((void *)signal_data->path);
  208. SDL_free(signal_data);
  209. handled:
  210. return DBUS_HANDLER_RESULT_HANDLED;
  211. }
  212. not_our_signal:
  213. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  214. }
  215. static void DBus_OpenDialog(const char *method, const char *method_title, SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, const char* default_location, SDL_bool allow_many, int open_folders)
  216. {
  217. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  218. DBusMessage *msg;
  219. DBusMessageIter params, options;
  220. const char *signal_id = NULL;
  221. char *handle_str, *filter;
  222. int filter_len;
  223. static uint32_t handle_id = 0;
  224. static char *default_parent_window = "";
  225. SDL_PropertiesID props = SDL_GetWindowProperties(window);
  226. if (dbus == NULL) {
  227. SDL_SetError("Failed to connect to DBus");
  228. return;
  229. }
  230. msg = dbus->message_new_method_call(PORTAL_DESTINATION, PORTAL_PATH, PORTAL_INTERFACE, method);
  231. if (msg == NULL) {
  232. SDL_SetError("Failed to send message to portal");
  233. return;
  234. }
  235. dbus->message_iter_init_append(msg, &params);
  236. handle_str = default_parent_window;
  237. if (props) {
  238. const char *parent_handle = SDL_GetStringProperty(props, SDL_PROP_WINDOW_WAYLAND_XDG_TOPLEVEL_EXPORT_HANDLE_STRING, NULL);
  239. if (parent_handle) {
  240. size_t len = SDL_strlen(parent_handle);
  241. len += sizeof(WAYLAND_HANDLE_PREFIX) + 1;
  242. handle_str = SDL_malloc(len * sizeof(char));
  243. if (!handle_str) {
  244. return;
  245. }
  246. SDL_snprintf(handle_str, len, "%s%s", WAYLAND_HANDLE_PREFIX, parent_handle);
  247. } else {
  248. const Uint64 xid = (Uint64)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0);
  249. if (xid) {
  250. const size_t len = sizeof(X11_HANDLE_PREFIX) + 24; /* A 64-bit number can be 20 characters max. */
  251. handle_str = SDL_malloc(len * sizeof(char));
  252. if (!handle_str) {
  253. return;
  254. }
  255. /* The portal wants X11 window ID numbers in hex. */
  256. SDL_snprintf(handle_str, len, "%s%" SDL_PRIx64, X11_HANDLE_PREFIX, xid);
  257. }
  258. }
  259. }
  260. dbus->message_iter_append_basic(&params, DBUS_TYPE_STRING, &handle_str);
  261. if (handle_str != default_parent_window) {
  262. SDL_free(handle_str);
  263. }
  264. dbus->message_iter_append_basic(&params, DBUS_TYPE_STRING, &method_title);
  265. dbus->message_iter_open_container(&params, DBUS_TYPE_ARRAY, "{sv}", &options);
  266. handle_str = SDL_malloc(sizeof(char) * (HANDLE_LEN + 1));
  267. if (!handle_str) {
  268. return;
  269. }
  270. SDL_snprintf(handle_str, HANDLE_LEN, "%u", ++handle_id);
  271. DBus_AppendStringOption(dbus, &options, "handle_token", handle_str);
  272. SDL_free(handle_str);
  273. DBus_AppendBoolOption(dbus, &options, "modal", !!window);
  274. if (allow_many == SDL_TRUE) {
  275. DBus_AppendBoolOption(dbus, &options, "multiple", 1);
  276. }
  277. if (open_folders) {
  278. DBus_AppendBoolOption(dbus, &options, "directory", 1);
  279. }
  280. if (filters) {
  281. DBus_AppendFilters(dbus, &options, filters);
  282. }
  283. if (default_location) {
  284. DBus_AppendByteArray(dbus, &options, "current_folder", default_location);
  285. }
  286. dbus->message_iter_close_container(&params, &options);
  287. DBusMessage *reply = dbus->connection_send_with_reply_and_block(dbus->session_conn, msg, DBUS_TIMEOUT_INFINITE, NULL);
  288. if (reply) {
  289. DBusMessageIter reply_iter;
  290. dbus->message_iter_init(reply, &reply_iter);
  291. if (dbus->message_iter_get_arg_type(&reply_iter) == DBUS_TYPE_OBJECT_PATH) {
  292. dbus->message_iter_get_basic(&reply_iter, &signal_id);
  293. }
  294. }
  295. if (!signal_id) {
  296. SDL_SetError("Invalid response received by DBus");
  297. goto incorrect_type;
  298. }
  299. dbus->message_unref(msg);
  300. filter_len = SDL_strlen(SIGNAL_FILTER) + SDL_strlen(signal_id) + 2;
  301. filter = SDL_malloc(sizeof(char) * filter_len);
  302. if (!filter) {
  303. goto incorrect_type;
  304. }
  305. SDL_snprintf(filter, filter_len, SIGNAL_FILTER"%s'", signal_id);
  306. dbus->bus_add_match(dbus->session_conn, filter, NULL);
  307. SDL_free(filter);
  308. SignalCallback *data = SDL_malloc(sizeof(SignalCallback));
  309. if (!data) {
  310. goto incorrect_type;
  311. }
  312. data->callback = callback;
  313. data->userdata = userdata;
  314. data->path = SDL_strdup(signal_id);
  315. if (!data->path) {
  316. SDL_free(data);
  317. goto incorrect_type;
  318. }
  319. /* TODO: This should be registered before opening the portal, or the filter will not catch
  320. the message if it is sent before we register the filter.
  321. */
  322. dbus->connection_add_filter(dbus->session_conn,
  323. &DBus_MessageFilter, data, NULL);
  324. dbus->connection_flush(dbus->session_conn);
  325. incorrect_type:
  326. dbus->message_unref(reply);
  327. }
  328. void SDL_Portal_ShowOpenFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, const char* default_location, SDL_bool allow_many)
  329. {
  330. DBus_OpenDialog("OpenFile", "Open File", callback, userdata, window, filters, default_location, allow_many, 0);
  331. }
  332. void SDL_Portal_ShowSaveFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, const char* default_location)
  333. {
  334. DBus_OpenDialog("SaveFile", "Save File", callback, userdata, window, filters, default_location, 0, 0);
  335. }
  336. void SDL_Portal_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const char* default_location, SDL_bool allow_many)
  337. {
  338. DBus_OpenDialog("OpenFile", "Open Folder", callback, userdata, window, NULL, default_location, allow_many, 1);
  339. }
  340. int SDL_Portal_detect(void)
  341. {
  342. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  343. DBusMessage *msg = NULL, *reply = NULL;
  344. char *reply_str = NULL;
  345. DBusMessageIter reply_iter;
  346. static int portal_present = -1;
  347. /* No need for this if the result is cached. */
  348. if (portal_present != -1) {
  349. return portal_present;
  350. }
  351. portal_present = 0;
  352. if (!dbus) {
  353. SDL_SetError("%s", "Failed to connect to DBus!");
  354. return 0;
  355. }
  356. /* Use introspection to get the available services. */
  357. msg = dbus->message_new_method_call(PORTAL_DESTINATION, PORTAL_PATH, "org.freedesktop.DBus.Introspectable", "Introspect");
  358. if (!msg) {
  359. goto done;
  360. }
  361. reply = dbus->connection_send_with_reply_and_block(dbus->session_conn, msg, DBUS_TIMEOUT_INFINITE, NULL);
  362. dbus->message_unref(msg);
  363. if (!reply) {
  364. goto done;
  365. }
  366. if (!dbus->message_iter_init(reply, &reply_iter)) {
  367. goto done;
  368. }
  369. if (dbus->message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_STRING) {
  370. goto done;
  371. }
  372. /* Introspection gives us a dump of all the services on the destination in XML format, so search the
  373. * giant string for the file chooser protocol.
  374. */
  375. dbus->message_iter_get_basic(&reply_iter, &reply_str);
  376. if (SDL_strstr(reply_str, PORTAL_INTERFACE)) {
  377. portal_present = 1; /* Found it! */
  378. }
  379. done:
  380. if (reply) {
  381. dbus->message_unref(reply);
  382. }
  383. return portal_present;
  384. }
  385. #else
  386. /* Dummy implementation to avoid compilation problems */
  387. void SDL_Portal_ShowOpenFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, const char* default_location, SDL_bool allow_many)
  388. {
  389. SDL_Unsupported();
  390. callback(userdata, NULL, -1);
  391. }
  392. void SDL_Portal_ShowSaveFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, const char* default_location)
  393. {
  394. SDL_Unsupported();
  395. callback(userdata, NULL, -1);
  396. }
  397. void SDL_Portal_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const char* default_location, SDL_bool allow_many)
  398. {
  399. SDL_Unsupported();
  400. callback(userdata, NULL, -1);
  401. }
  402. int SDL_Portal_detect(void)
  403. {
  404. return 0;
  405. }
  406. #endif /* SDL_USE_LIBDBUS */