DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

695 lines
24KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #if !defined(DISTRHO_FILE_BROWSER_DIALOG_HPP_INCLUDED) && !defined(DGL_FILE_BROWSER_DIALOG_HPP_INCLUDED)
  17. # error bad include
  18. #endif
  19. #if !defined(FILE_BROWSER_DIALOG_DISTRHO_NAMESPACE) && !defined(FILE_BROWSER_DIALOG_DGL_NAMESPACE)
  20. # error bad usage
  21. #endif
  22. #include "ScopedPointer.hpp"
  23. #include "String.hpp"
  24. #ifdef DISTRHO_OS_MAC
  25. # import <Cocoa/Cocoa.h>
  26. #endif
  27. #ifdef DISTRHO_OS_WINDOWS
  28. # include <direct.h>
  29. # include <process.h>
  30. # include <winsock2.h>
  31. # include <windows.h>
  32. # include <commdlg.h>
  33. # include <vector>
  34. #else
  35. # include <unistd.h>
  36. #endif
  37. #ifdef HAVE_DBUS
  38. # include <dbus/dbus.h>
  39. #endif
  40. #ifdef HAVE_X11
  41. # define DBLCLKTME 400
  42. # include "sofd/libsofd.h"
  43. # include "sofd/libsofd.c"
  44. #endif
  45. #ifdef FILE_BROWSER_DIALOG_DGL_NAMESPACE
  46. START_NAMESPACE_DGL
  47. using DISTRHO_NAMESPACE::ScopedPointer;
  48. using DISTRHO_NAMESPACE::String;
  49. #else
  50. START_NAMESPACE_DISTRHO
  51. #endif
  52. // --------------------------------------------------------------------------------------------------------------------
  53. // static pointer used for signal null/none action taken
  54. static const char* const kSelectedFileCancelled = "__dpf_cancelled__";
  55. #ifdef HAVE_DBUS
  56. static constexpr bool isHexChar(const char c) noexcept
  57. {
  58. return c >= '0' && c <= 'f' && (c <= '9' || (c >= 'A' && c <= 'F') || c >= 'a');
  59. }
  60. static constexpr int toHexChar(const char c) noexcept
  61. {
  62. return c >= '0' && c <= '9' ? c - '0' : (c >= 'A' && c <= 'F' ? c - 'A' : c - 'a') + 10;
  63. }
  64. #endif
  65. struct FileBrowserData {
  66. const char* selectedFile;
  67. #ifdef DISTRHO_OS_MAC
  68. NSSavePanel* nsBasePanel;
  69. NSOpenPanel* nsOpenPanel;
  70. #endif
  71. #ifdef HAVE_DBUS
  72. DBusConnection* dbuscon;
  73. #endif
  74. #ifdef HAVE_X11
  75. Display* x11display;
  76. #endif
  77. #ifdef DISTRHO_OS_WINDOWS
  78. OPENFILENAMEW ofn;
  79. volatile bool threadCancelled;
  80. uintptr_t threadHandle;
  81. std::vector<WCHAR> fileNameW;
  82. std::vector<WCHAR> startDirW;
  83. std::vector<WCHAR> titleW;
  84. const bool saving;
  85. bool isEmbed;
  86. FileBrowserData(const bool save)
  87. : selectedFile(nullptr),
  88. threadCancelled(false),
  89. threadHandle(0),
  90. fileNameW(32768),
  91. saving(save),
  92. isEmbed(false)
  93. {
  94. std::memset(&ofn, 0, sizeof(ofn));
  95. ofn.lStructSize = sizeof(ofn);
  96. ofn.lpstrFile = fileNameW.data();
  97. ofn.nMaxFile = (DWORD)fileNameW.size();
  98. }
  99. ~FileBrowserData()
  100. {
  101. if (cancelAndStop())
  102. free();
  103. }
  104. void setupAndStart(const bool embed,
  105. const char* const startDir,
  106. const char* const windowTitle,
  107. const uintptr_t winId,
  108. const FileBrowserOptions options)
  109. {
  110. isEmbed = embed;
  111. ofn.hwndOwner = (HWND)winId;
  112. ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
  113. if (options.buttons.showHidden == FileBrowserOptions::kButtonVisibleChecked)
  114. ofn.Flags |= OFN_FORCESHOWHIDDEN;
  115. ofn.FlagsEx = 0x0;
  116. if (options.buttons.showPlaces == FileBrowserOptions::kButtonInvisible)
  117. ofn.FlagsEx |= OFN_EX_NOPLACESBAR;
  118. startDirW.resize(std::strlen(startDir) + 1);
  119. if (MultiByteToWideChar(CP_UTF8, 0, startDir, -1, startDirW.data(), static_cast<int>(startDirW.size())))
  120. ofn.lpstrInitialDir = startDirW.data();
  121. titleW.resize(std::strlen(windowTitle) + 1);
  122. if (MultiByteToWideChar(CP_UTF8, 0, windowTitle, -1, titleW.data(), static_cast<int>(titleW.size())))
  123. ofn.lpstrTitle = titleW.data();
  124. uint threadId;
  125. threadCancelled = false;
  126. threadHandle = _beginthreadex(nullptr, 0, _run, this, 0, &threadId);
  127. }
  128. bool cancelAndStop()
  129. {
  130. threadCancelled = true;
  131. if (threadHandle == 0)
  132. return true;
  133. // if previous dialog running, carefully close its window
  134. const HWND owner = isEmbed ? GetParent(ofn.hwndOwner) : ofn.hwndOwner;
  135. if (owner != nullptr && owner != INVALID_HANDLE_VALUE)
  136. {
  137. const HWND window = GetWindow(owner, GW_HWNDFIRST);
  138. if (window != nullptr && window != INVALID_HANDLE_VALUE)
  139. {
  140. SendMessage(window, WM_SYSCOMMAND, SC_CLOSE, 0);
  141. SendMessage(window, WM_CLOSE, 0, 0);
  142. WaitForSingleObject((HANDLE)threadHandle, 5000);
  143. }
  144. }
  145. if (threadHandle == 0)
  146. return true;
  147. // not good if thread still running, but let's close the handle anyway
  148. CloseHandle((HANDLE)threadHandle);
  149. threadHandle = 0;
  150. return false;
  151. }
  152. void run()
  153. {
  154. const char* nextFile = nullptr;
  155. if (saving ? GetSaveFileNameW(&ofn) : GetOpenFileNameW(&ofn))
  156. {
  157. if (threadCancelled)
  158. {
  159. threadHandle = 0;
  160. return;
  161. }
  162. // back to UTF-8
  163. std::vector<char> fileNameA(4 * 32768);
  164. if (WideCharToMultiByte(CP_UTF8, 0, fileNameW.data(), -1,
  165. fileNameA.data(), (int)fileNameA.size(),
  166. nullptr, nullptr))
  167. {
  168. nextFile = strdup(fileNameA.data());
  169. }
  170. }
  171. if (threadCancelled)
  172. {
  173. threadHandle = 0;
  174. return;
  175. }
  176. if (nextFile == nullptr)
  177. nextFile = kSelectedFileCancelled;
  178. selectedFile = nextFile;
  179. threadHandle = 0;
  180. }
  181. static unsigned __stdcall _run(void* const arg)
  182. {
  183. // CoInitializeEx(nullptr, COINIT_MULTITHREADED);
  184. static_cast<FileBrowserData*>(arg)->run();
  185. // CoUninitialize();
  186. _endthreadex(0);
  187. return 0;
  188. }
  189. #else // DISTRHO_OS_WINDOWS
  190. FileBrowserData(const bool saving)
  191. : selectedFile(nullptr)
  192. {
  193. #ifdef DISTRHO_OS_MAC
  194. if (saving)
  195. {
  196. nsOpenPanel = nullptr;
  197. nsBasePanel = [[NSSavePanel savePanel]retain];
  198. }
  199. else
  200. {
  201. nsOpenPanel = [[NSOpenPanel openPanel]retain];
  202. nsBasePanel = nsOpenPanel;
  203. }
  204. #endif
  205. #ifdef HAVE_DBUS
  206. if ((dbuscon = dbus_bus_get(DBUS_BUS_SESSION, nullptr)) != nullptr)
  207. dbus_connection_set_exit_on_disconnect(dbuscon, false);
  208. #endif
  209. #ifdef HAVE_X11
  210. x11display = XOpenDisplay(nullptr);
  211. #endif
  212. // maybe unused
  213. return; (void)saving;
  214. }
  215. ~FileBrowserData()
  216. {
  217. #ifdef DISTRHO_OS_MAC
  218. [nsBasePanel release];
  219. #endif
  220. #ifdef HAVE_DBUS
  221. if (dbuscon != nullptr)
  222. dbus_connection_unref(dbuscon);
  223. #endif
  224. #ifdef HAVE_X11
  225. if (x11display != nullptr)
  226. XCloseDisplay(x11display);
  227. #endif
  228. free();
  229. }
  230. #endif
  231. void free()
  232. {
  233. if (selectedFile == nullptr)
  234. return;
  235. if (selectedFile == kSelectedFileCancelled || std::strcmp(selectedFile, kSelectedFileCancelled) == 0)
  236. {
  237. selectedFile = nullptr;
  238. return;
  239. }
  240. std::free(const_cast<char*>(selectedFile));
  241. selectedFile = nullptr;
  242. }
  243. };
  244. // --------------------------------------------------------------------------------------------------------------------
  245. FileBrowserHandle fileBrowserCreate(const bool isEmbed,
  246. const uintptr_t windowId,
  247. const double scaleFactor,
  248. const FileBrowserOptions& options)
  249. {
  250. String startDir(options.startDir);
  251. if (startDir.isEmpty())
  252. {
  253. #ifdef DISTRHO_OS_WINDOWS
  254. if (char* const cwd = _getcwd(nullptr, 0))
  255. {
  256. startDir = cwd;
  257. std::free(cwd);
  258. }
  259. #else
  260. if (char* const cwd = getcwd(nullptr, 0))
  261. {
  262. startDir = cwd;
  263. std::free(cwd);
  264. }
  265. #endif
  266. }
  267. DISTRHO_SAFE_ASSERT_RETURN(startDir.isNotEmpty(), nullptr);
  268. if (! startDir.endsWith(DISTRHO_OS_SEP))
  269. startDir += DISTRHO_OS_SEP_STR;
  270. String windowTitle(options.title);
  271. if (windowTitle.isEmpty())
  272. windowTitle = "FileBrowser";
  273. ScopedPointer<FileBrowserData> handle(new FileBrowserData(options.saving));
  274. #ifdef DISTRHO_OS_MAC
  275. # if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_8
  276. // unsupported
  277. return nullptr;
  278. # else
  279. NSSavePanel* const nsBasePanel = handle->nsBasePanel;
  280. DISTRHO_SAFE_ASSERT_RETURN(nsBasePanel != nullptr, nullptr);
  281. if (! options.saving)
  282. {
  283. NSOpenPanel* const nsOpenPanel = handle->nsOpenPanel;
  284. DISTRHO_SAFE_ASSERT_RETURN(nsOpenPanel != nullptr, nullptr);
  285. [nsOpenPanel setAllowsMultipleSelection:NO];
  286. [nsOpenPanel setCanChooseDirectories:NO];
  287. [nsOpenPanel setCanChooseFiles:YES];
  288. }
  289. [nsBasePanel setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:startDir]]];
  290. // TODO file filter using allowedContentTypes: [UTType]
  291. if (options.buttons.listAllFiles == FileBrowserOptions::kButtonVisibleChecked)
  292. [nsBasePanel setAllowsOtherFileTypes:YES];
  293. if (options.buttons.showHidden == FileBrowserOptions::kButtonVisibleChecked)
  294. [nsBasePanel setShowsHiddenFiles:YES];
  295. NSString* const titleString = [[NSString alloc]
  296. initWithBytes:windowTitle
  297. length:strlen(windowTitle)
  298. encoding:NSUTF8StringEncoding];
  299. [nsBasePanel setTitle:titleString];
  300. FileBrowserData* const handleptr = handle.get();
  301. dispatch_async(dispatch_get_main_queue(), ^
  302. {
  303. [nsBasePanel beginSheetModalForWindow:[(NSView*)windowId window]
  304. completionHandler:^(NSModalResponse result)
  305. {
  306. if (result == NSModalResponseOK && [[nsBasePanel URL] isFileURL])
  307. {
  308. NSString* const path = [[nsBasePanel URL] path];
  309. handleptr->selectedFile = strdup([path UTF8String]);
  310. }
  311. else
  312. {
  313. handleptr->selectedFile = kSelectedFileCancelled;
  314. }
  315. }];
  316. });
  317. # endif
  318. #endif
  319. #ifdef DISTRHO_OS_WINDOWS
  320. handle->setupAndStart(isEmbed, startDir, windowTitle, windowId, options);
  321. #endif
  322. #ifdef HAVE_DBUS
  323. // optional, can be null
  324. DBusConnection* const dbuscon = handle->dbuscon;
  325. // https://flatpak.github.io/xdg-desktop-portal/portal-docs.html#gdbus-org.freedesktop.portal.FileChooser
  326. if (dbuscon != nullptr)
  327. {
  328. // if this is the first time we are calling into DBus, check if things are working
  329. static bool checkAvailable = !dbus_bus_name_has_owner(dbuscon, "org.freedesktop.portal.Desktop", nullptr);
  330. if (checkAvailable)
  331. {
  332. checkAvailable = false;
  333. if (DBusMessage* const msg = dbus_message_new_method_call("org.freedesktop.portal.Desktop",
  334. "/org/freedesktop/portal/desktop",
  335. "org.freedesktop.portal.FileChooser",
  336. "version"))
  337. {
  338. if (DBusMessage* const reply = dbus_connection_send_with_reply_and_block(dbuscon, msg, 250, nullptr))
  339. dbus_message_unref(reply);
  340. dbus_message_unref(msg);
  341. }
  342. }
  343. // Any subsquent calls should have this DBus service active
  344. if (dbus_bus_name_has_owner(dbuscon, "org.freedesktop.portal.Desktop", nullptr))
  345. {
  346. if (DBusMessage* const msg = dbus_message_new_method_call("org.freedesktop.portal.Desktop",
  347. "/org/freedesktop/portal/desktop",
  348. "org.freedesktop.portal.FileChooser",
  349. options.saving ? "SaveFile" : "OpenFile"))
  350. {
  351. #ifdef HAVE_X11
  352. char windowIdStr[32];
  353. memset(windowIdStr, 0, sizeof(windowIdStr));
  354. snprintf(windowIdStr, sizeof(windowIdStr)-1, "x11:%llx", (ulonglong)windowId);
  355. const char* windowIdStrPtr = windowIdStr;
  356. #endif
  357. dbus_message_append_args(msg,
  358. #ifdef HAVE_X11
  359. DBUS_TYPE_STRING, &windowIdStrPtr,
  360. #endif
  361. DBUS_TYPE_STRING, &windowTitle,
  362. DBUS_TYPE_INVALID);
  363. DBusMessageIter iter, array;
  364. dbus_message_iter_init_append(msg, &iter);
  365. dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &array);
  366. {
  367. DBusMessageIter dict, variant, variantArray;
  368. const char* const current_folder_key = "current_folder";
  369. const char* const current_folder_val = startDir.buffer();
  370. dbus_message_iter_open_container(&array, DBUS_TYPE_DICT_ENTRY, nullptr, &dict);
  371. dbus_message_iter_append_basic(&dict, DBUS_TYPE_STRING, &current_folder_key);
  372. dbus_message_iter_open_container(&dict, DBUS_TYPE_VARIANT, "ay", &variant);
  373. dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY, "y", &variantArray);
  374. dbus_message_iter_append_fixed_array(&variantArray, DBUS_TYPE_BYTE,
  375. &current_folder_val, startDir.length()+1);
  376. dbus_message_iter_close_container(&variant, &variantArray);
  377. dbus_message_iter_close_container(&dict, &variant);
  378. dbus_message_iter_close_container(&array, &dict);
  379. }
  380. dbus_message_iter_close_container(&iter, &array);
  381. dbus_connection_send(dbuscon, msg, nullptr);
  382. dbus_message_unref(msg);
  383. return handle.release();
  384. }
  385. }
  386. }
  387. #endif
  388. #ifdef HAVE_X11
  389. Display* const x11display = handle->x11display;
  390. DISTRHO_SAFE_ASSERT_RETURN(x11display != nullptr, nullptr);
  391. // unsupported at the moment
  392. if (options.saving)
  393. return nullptr;
  394. DISTRHO_SAFE_ASSERT_RETURN(x_fib_configure(0, startDir) == 0, nullptr);
  395. DISTRHO_SAFE_ASSERT_RETURN(x_fib_configure(1, windowTitle) == 0, nullptr);
  396. const int button1 = options.buttons.showHidden == FileBrowserOptions::kButtonVisibleChecked ? 1
  397. : options.buttons.showHidden == FileBrowserOptions::kButtonVisibleUnchecked ? 0 : -1;
  398. const int button2 = options.buttons.showPlaces == FileBrowserOptions::kButtonVisibleChecked ? 1
  399. : options.buttons.showPlaces == FileBrowserOptions::kButtonVisibleUnchecked ? 0 : -1;
  400. const int button3 = options.buttons.listAllFiles == FileBrowserOptions::kButtonVisibleChecked ? 1
  401. : options.buttons.listAllFiles == FileBrowserOptions::kButtonVisibleUnchecked ? 0 : -1;
  402. x_fib_cfg_buttons(1, button1);
  403. x_fib_cfg_buttons(2, button2);
  404. x_fib_cfg_buttons(3, button3);
  405. if (x_fib_show(x11display, windowId, 0, 0, scaleFactor + 0.5) != 0)
  406. return nullptr;
  407. #endif
  408. return handle.release();
  409. // might be unused
  410. (void)isEmbed;
  411. (void)windowId;
  412. (void)scaleFactor;
  413. }
  414. // --------------------------------------------------------------------------------------------------------------------
  415. // returns true if dialog was closed (with or without a file selection)
  416. bool fileBrowserIdle(const FileBrowserHandle handle)
  417. {
  418. #ifdef HAVE_DBUS
  419. if (DBusConnection* dbuscon = handle->dbuscon)
  420. {
  421. while (dbus_connection_dispatch(dbuscon) == DBUS_DISPATCH_DATA_REMAINS) {}
  422. dbus_connection_read_write_dispatch(dbuscon, 0);
  423. if (DBusMessage* const message = dbus_connection_pop_message(dbuscon))
  424. {
  425. const char* const interface = dbus_message_get_interface(message);
  426. const char* const member = dbus_message_get_member(message);
  427. if (interface != nullptr && std::strcmp(interface, "org.freedesktop.portal.Request") == 0
  428. && member != nullptr && std::strcmp(member, "Response") == 0)
  429. {
  430. do {
  431. DBusMessageIter iter;
  432. dbus_message_iter_init(message, &iter);
  433. // starts with uint32 for return/exit code
  434. DISTRHO_SAFE_ASSERT_BREAK(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32);
  435. uint32_t ret = 1;
  436. dbus_message_iter_get_basic(&iter, &ret);
  437. if (ret != 0)
  438. break;
  439. // next must be array
  440. dbus_message_iter_next(&iter);
  441. DISTRHO_SAFE_ASSERT_BREAK(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_ARRAY);
  442. // open dict array
  443. DBusMessageIter dictArray;
  444. dbus_message_iter_recurse(&iter, &dictArray);
  445. DISTRHO_SAFE_ASSERT_BREAK(dbus_message_iter_get_arg_type(&dictArray) == DBUS_TYPE_DICT_ENTRY);
  446. // open containing dict
  447. DBusMessageIter dict;
  448. dbus_message_iter_recurse(&dictArray, &dict);
  449. // look for dict with string "uris"
  450. DISTRHO_SAFE_ASSERT_BREAK(dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRING);
  451. const char* key = nullptr;
  452. dbus_message_iter_get_basic(&dict, &key);
  453. DISTRHO_SAFE_ASSERT_BREAK(key != nullptr);
  454. // keep going until we find it
  455. while (std::strcmp(key, "uris") != 0)
  456. {
  457. key = nullptr;
  458. dbus_message_iter_next(&dictArray);
  459. DISTRHO_SAFE_ASSERT_BREAK(dbus_message_iter_get_arg_type(&dictArray) == DBUS_TYPE_DICT_ENTRY);
  460. dbus_message_iter_recurse(&dictArray, &dict);
  461. DISTRHO_SAFE_ASSERT_BREAK(dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRING);
  462. dbus_message_iter_get_basic(&dict, &key);
  463. DISTRHO_SAFE_ASSERT_BREAK(key != nullptr);
  464. }
  465. if (key == nullptr)
  466. break;
  467. // then comes variant
  468. dbus_message_iter_next(&dict);
  469. DISTRHO_SAFE_ASSERT_BREAK(dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_VARIANT);
  470. DBusMessageIter variant;
  471. dbus_message_iter_recurse(&dict, &variant);
  472. DISTRHO_SAFE_ASSERT_BREAK(dbus_message_iter_get_arg_type(&variant) == DBUS_TYPE_ARRAY);
  473. // open variant array (variant type is string)
  474. DBusMessageIter variantArray;
  475. dbus_message_iter_recurse(&variant, &variantArray);
  476. DISTRHO_SAFE_ASSERT_BREAK(dbus_message_iter_get_arg_type(&variantArray) == DBUS_TYPE_STRING);
  477. const char* value = nullptr;
  478. dbus_message_iter_get_basic(&variantArray, &value);
  479. // and finally we have our dear value, just make sure it is local
  480. DISTRHO_SAFE_ASSERT_BREAK(value != nullptr);
  481. if (const char* const localvalue = std::strstr(value, "file:///"))
  482. {
  483. if (char* const decodedvalue = strdup(localvalue + 7))
  484. {
  485. for (char* s = decodedvalue; (s = std::strchr(s, '%')) != nullptr; ++s)
  486. {
  487. if (! isHexChar(s[1]) || ! isHexChar(s[2]))
  488. continue;
  489. const int decodedNum = toHexChar(s[1]) * 0x10 + toHexChar(s[2]);
  490. char replacementChar;
  491. switch (decodedNum)
  492. {
  493. case 0x20: replacementChar = ' '; break;
  494. case 0x22: replacementChar = '\"'; break;
  495. case 0x23: replacementChar = '#'; break;
  496. case 0x25: replacementChar = '%'; break;
  497. case 0x3c: replacementChar = '<'; break;
  498. case 0x3e: replacementChar = '>'; break;
  499. case 0x5b: replacementChar = '['; break;
  500. case 0x5c: replacementChar = '\\'; break;
  501. case 0x5d: replacementChar = ']'; break;
  502. case 0x5e: replacementChar = '^'; break;
  503. case 0x60: replacementChar = '`'; break;
  504. case 0x7b: replacementChar = '{'; break;
  505. case 0x7c: replacementChar = '|'; break;
  506. case 0x7d: replacementChar = '}'; break;
  507. case 0x7e: replacementChar = '~'; break;
  508. default: continue;
  509. }
  510. s[0] = replacementChar;
  511. std::memmove(s + 1, s + 3, std::strlen(s) - 2);
  512. }
  513. handle->selectedFile = decodedvalue;
  514. }
  515. }
  516. } while(false);
  517. if (handle->selectedFile == nullptr)
  518. handle->selectedFile = kSelectedFileCancelled;
  519. }
  520. }
  521. }
  522. #endif
  523. #ifdef HAVE_X11
  524. Display* const x11display = handle->x11display;
  525. if (x11display == nullptr)
  526. return false;
  527. XEvent event;
  528. while (XPending(x11display) > 0)
  529. {
  530. XNextEvent(x11display, &event);
  531. if (x_fib_handle_events(x11display, &event) == 0)
  532. continue;
  533. if (x_fib_status() > 0)
  534. handle->selectedFile = x_fib_filename();
  535. else
  536. handle->selectedFile = kSelectedFileCancelled;
  537. x_fib_close(x11display);
  538. XCloseDisplay(x11display);
  539. handle->x11display = nullptr;
  540. break;
  541. }
  542. #endif
  543. return handle->selectedFile != nullptr;
  544. }
  545. // --------------------------------------------------------------------------------------------------------------------
  546. // close sofd file dialog
  547. void fileBrowserClose(const FileBrowserHandle handle)
  548. {
  549. #ifdef HAVE_X11
  550. if (Display* const x11display = handle->x11display)
  551. x_fib_close(x11display);
  552. #endif
  553. delete handle;
  554. }
  555. // --------------------------------------------------------------------------------------------------------------------
  556. // get path chosen via sofd file dialog
  557. const char* fileBrowserGetPath(const FileBrowserHandle handle)
  558. {
  559. if (const char* const selectedFile = handle->selectedFile)
  560. if (selectedFile != kSelectedFileCancelled && std::strcmp(selectedFile, kSelectedFileCancelled) != 0)
  561. return selectedFile;
  562. return nullptr;
  563. }
  564. // --------------------------------------------------------------------------------------------------------------------
  565. #ifdef FILE_BROWSER_DIALOG_DGL_NAMESPACE
  566. END_NAMESPACE_DGL
  567. #else
  568. END_NAMESPACE_DISTRHO
  569. #endif
  570. #undef FILE_BROWSER_DIALOG_DISTRHO_NAMESPACE
  571. #undef FILE_BROWSER_DIALOG_DGL_NAMESPACE