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.

1526 lines
52KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2024 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_WEB_VIEW_HPP_INCLUDED) && !defined(DGL_WEB_VIEW_HPP_INCLUDED)
  17. # error bad include
  18. #endif
  19. #if !defined(WEB_VIEW_DISTRHO_NAMESPACE) && !defined(WEB_VIEW_DGL_NAMESPACE)
  20. # error bad usage
  21. #endif
  22. // #include <gtk/gtk.h>
  23. // #include <gtk/gtkx.h>
  24. // #include <webkit2/webkit2.h>
  25. #define WEB_VIEW_USING_CHOC 0
  26. #ifndef WEB_VIEW_USING_CHOC
  27. # define WEB_VIEW_USING_CHOC 0
  28. #elif WEB_VIEW_USING_CHOC && !(defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS))
  29. # undef WEB_VIEW_USING_CHOC
  30. # define WEB_VIEW_USING_CHOC 0
  31. #endif
  32. #if defined(DISTRHO_OS_MAC) && !WEB_VIEW_USING_CHOC
  33. # undef WEB_VIEW_USING_MACOS_WEBKIT
  34. # define WEB_VIEW_USING_MACOS_WEBKIT 1
  35. #else
  36. # undef WEB_VIEW_USING_MACOS_WEBKIT
  37. # define WEB_VIEW_USING_MACOS_WEBKIT 0
  38. #endif
  39. #if defined(HAVE_X11) && defined(DISTRHO_OS_LINUX)
  40. # undef WEB_VIEW_USING_X11_IPC
  41. # define WEB_VIEW_USING_X11_IPC 1
  42. #else
  43. # undef WEB_VIEW_USING_X11_IPC
  44. # define WEB_VIEW_USING_X11_IPC 0
  45. #endif
  46. #if WEB_VIEW_USING_CHOC
  47. # define WC_ERR_INVALID_CHARS 0
  48. # include "../CHOC/gui/choc_WebView.h"
  49. #elif WEB_VIEW_USING_MACOS_WEBKIT
  50. # include <Cocoa/Cocoa.h>
  51. # include <WebKit/WebKit.h>
  52. #elif WEB_VIEW_USING_X11_IPC
  53. // #define QT_NO_VERSION_TAGGING
  54. // #include <QtCore/QChar>
  55. // #include <QtCore/QPoint>
  56. // #include <QtCore/QSize>
  57. // #undef signals
  58. # include "ChildProcess.hpp"
  59. # include "RingBuffer.hpp"
  60. # include "String.hpp"
  61. # include <clocale>
  62. # include <cstdio>
  63. # include <functional>
  64. # include <dlfcn.h>
  65. # include <fcntl.h>
  66. # include <pthread.h>
  67. # include <unistd.h>
  68. # include <sys/mman.h>
  69. # include <X11/Xlib.h>
  70. # ifdef __linux__
  71. # include <syscall.h>
  72. # include <linux/futex.h>
  73. # include <linux/limits.h>
  74. # else
  75. # include <semaphore.h>
  76. # endif
  77. #endif
  78. // -----------------------------------------------------------------------------------------------------------
  79. #if WEB_VIEW_USING_MACOS_WEBKIT
  80. #define MACRO_NAME2(a, b, c) a ## b ## c
  81. #define MACRO_NAME(a, b, c) MACRO_NAME2(a, b, c)
  82. #define WEB_VIEW_DELEGATE_CLASS_NAME \
  83. MACRO_NAME(WebViewDelegate_, _, DISTRHO_NAMESPACE)
  84. @interface WEB_VIEW_DELEGATE_CLASS_NAME : NSObject<WKNavigationDelegate, WKScriptMessageHandler, WKUIDelegate>
  85. @end
  86. @implementation WEB_VIEW_DELEGATE_CLASS_NAME {
  87. @public
  88. WebViewMessageCallback callback;
  89. void* callbackPtr;
  90. bool loaded;
  91. }
  92. - (void)webView:(WKWebView *)webview
  93. didFinishNavigation:(WKNavigation*)navigation
  94. {
  95. d_stdout("page loaded");
  96. loaded = true;
  97. }
  98. - (void)webView:(WKWebView*)webview
  99. runJavaScriptAlertPanelWithMessage:(NSString*)message
  100. initiatedByFrame:(WKFrameInfo*)frame
  101. completionHandler:(void (^)(void))completionHandler
  102. {
  103. NSAlert* const alert = [[NSAlert alloc] init];
  104. [alert addButtonWithTitle:@"OK"];
  105. [alert setInformativeText:message];
  106. [alert setMessageText:@"Alert"];
  107. dispatch_async(dispatch_get_main_queue(), ^
  108. {
  109. [alert beginSheetModalForWindow:[webview window]
  110. completionHandler:^(NSModalResponse)
  111. {
  112. completionHandler();
  113. [alert release];
  114. }];
  115. });
  116. }
  117. - (void)webView:(WKWebView*)webview
  118. runJavaScriptConfirmPanelWithMessage:(NSString*)message
  119. initiatedByFrame:(WKFrameInfo*)frame
  120. completionHandler:(void (^)(BOOL))completionHandler
  121. {
  122. NSAlert* const alert = [[NSAlert alloc] init];
  123. [alert addButtonWithTitle:@"OK"];
  124. [alert addButtonWithTitle:@"Cancel"];
  125. [alert setInformativeText:message];
  126. [alert setMessageText:@"Confirm"];
  127. dispatch_async(dispatch_get_main_queue(), ^
  128. {
  129. [alert beginSheetModalForWindow:[webview window]
  130. completionHandler:^(NSModalResponse result)
  131. {
  132. completionHandler(result == NSAlertFirstButtonReturn);
  133. [alert release];
  134. }];
  135. });
  136. }
  137. - (void)webView:(WKWebView*)webview
  138. runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt
  139. defaultText:(NSString*)defaultText
  140. initiatedByFrame:(WKFrameInfo*)frame
  141. completionHandler:(void (^)(NSString*))completionHandler
  142. {
  143. NSTextField* const input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 250, 30)];
  144. [input setStringValue:defaultText];
  145. NSAlert* const alert = [[NSAlert alloc] init];
  146. [alert setAccessoryView:input];
  147. [alert addButtonWithTitle:@"OK"];
  148. [alert addButtonWithTitle:@"Cancel"];
  149. [alert setInformativeText:prompt];
  150. [alert setMessageText: @"Prompt"];
  151. dispatch_async(dispatch_get_main_queue(), ^
  152. {
  153. [alert beginSheetModalForWindow:[webview window]
  154. completionHandler:^(NSModalResponse result)
  155. {
  156. [input validateEditing];
  157. completionHandler(result == NSAlertFirstButtonReturn ? [input stringValue] : nil);
  158. [alert release];
  159. }];
  160. });
  161. }
  162. - (void)webView:(WKWebView*)webview
  163. runOpenPanelWithParameters:(WKOpenPanelParameters*)params
  164. initiatedByFrame:(WKFrameInfo*)frame
  165. completionHandler:(void (^)(NSArray<NSURL*>*))completionHandler
  166. {
  167. NSOpenPanel* const panel = [[NSOpenPanel alloc] init];
  168. [panel setAllowsMultipleSelection:[params allowsMultipleSelection]];
  169. // [panel setAllowedFileTypes:(NSArray<NSString*>*)[params _allowedFileExtensions]];
  170. [panel setCanChooseDirectories:[params allowsDirectories]];
  171. [panel setCanChooseFiles:![params allowsDirectories]];
  172. dispatch_async(dispatch_get_main_queue(), ^
  173. {
  174. [panel beginSheetModalForWindow:[webview window]
  175. completionHandler:^(NSModalResponse result)
  176. {
  177. completionHandler(result == NSModalResponseOK ? [panel URLs] : nil);
  178. [panel release];
  179. }];
  180. });
  181. }
  182. - (void)userContentController:(WKUserContentController*)userContentController
  183. didReceiveScriptMessage:(WKScriptMessage*)message
  184. {
  185. NSString* const nsstring = static_cast<NSString*>([message body]);
  186. char* const string = strdup([nsstring UTF8String]);
  187. d_debug("JS call received '%s' %p", string, callback);
  188. if (callback != nullptr)
  189. callback(callbackPtr, string);
  190. std::free(string);
  191. }
  192. @end
  193. #elif WEB_VIEW_USING_X11_IPC
  194. #endif // WEB_VIEW_USING_MACOS_WEBKIT
  195. // -----------------------------------------------------------------------------------------------------------
  196. #ifdef WEB_VIEW_DGL_NAMESPACE
  197. START_NAMESPACE_DGL
  198. using DISTRHO_NAMESPACE::String;
  199. #else
  200. START_NAMESPACE_DISTRHO
  201. #endif
  202. // -----------------------------------------------------------------------------------------------------------
  203. #if WEB_VIEW_USING_X11_IPC
  204. #ifdef __linux__
  205. typedef int32_t ipc_sem_t;
  206. #else
  207. typedef sem_t ipc_sem_t;
  208. #endif
  209. enum WebViewMessageType {
  210. kWebViewMessageNull,
  211. kWebViewMessageInitData,
  212. kWebViewMessageEvaluateJS,
  213. kWebViewMessageCallback,
  214. kWebViewMessageReload
  215. };
  216. struct WebViewSharedBuffer {
  217. static constexpr const uint32_t size = 0x100000;
  218. ipc_sem_t sem;
  219. uint32_t head, tail, wrtn;
  220. bool invalidateCommit;
  221. uint8_t buf[size];
  222. };
  223. struct WebViewRingBuffer {
  224. WebViewSharedBuffer server;
  225. WebViewSharedBuffer client;
  226. bool valid;
  227. };
  228. static void webview_wake(ipc_sem_t* const sem)
  229. {
  230. #ifdef __linux__
  231. if (__sync_bool_compare_and_swap(sem, 0, 1))
  232. syscall(SYS_futex, sem, FUTEX_WAKE, 1, nullptr, nullptr, 0);
  233. #else
  234. sem_post(sem);
  235. #endif
  236. }
  237. static bool webview_timedwait(ipc_sem_t* const sem)
  238. {
  239. #ifdef __linux__
  240. const struct timespec timeout = { 1, 0 };
  241. for (;;)
  242. {
  243. if (__sync_bool_compare_and_swap(sem, 1, 0))
  244. return true;
  245. if (syscall(SYS_futex, sem, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0)
  246. if (errno != EAGAIN && errno != EINTR)
  247. return false;
  248. }
  249. #else
  250. struct timespec timeout;
  251. if (clock_gettime(CLOCK_REALTIME, &timeout) != 0)
  252. return false;
  253. timeout.tv_sec += 1;
  254. for (int r;;)
  255. {
  256. r = sem_timedwait(sem, &timeout);
  257. if (r < 0)
  258. r = errno;
  259. if (r == EINTR)
  260. continue;
  261. return r == 0;
  262. }
  263. #endif
  264. }
  265. #endif
  266. struct WebViewData {
  267. #if WEB_VIEW_USING_CHOC
  268. choc::ui::WebView* const webview;
  269. #elif WEB_VIEW_USING_MACOS_WEBKIT
  270. NSView* view;
  271. WKWebView* webview;
  272. NSURLRequest* urlreq;
  273. WEB_VIEW_DELEGATE_CLASS_NAME* delegate;
  274. #elif WEB_VIEW_USING_X11_IPC
  275. int shmfd = 0;
  276. char shmname[128] = {};
  277. WebViewRingBuffer* shmptr = nullptr;
  278. WebViewMessageCallback callback = nullptr;
  279. void* callbackPtr = nullptr;
  280. ChildProcess p;
  281. RingBufferControl<WebViewSharedBuffer> rbctrl, rbctrl2;
  282. ::Display* display = nullptr;
  283. ::Window childWindow = 0;
  284. ::Window ourWindow = 0;
  285. #endif
  286. WebViewData() {}
  287. DISTRHO_DECLARE_NON_COPYABLE(WebViewData);
  288. };
  289. // -----------------------------------------------------------------------------------------------------------
  290. #if WEB_VIEW_USING_CHOC
  291. static std::optional<choc::ui::WebView::Options::Resource> fetch_resource(const std::string& path)
  292. {
  293. d_stdout("requested path %s", path.c_str());
  294. if (path == "/")
  295. {
  296. const std::string html = R"PREFIX(
  297. <html>
  298. <head>
  299. <style>
  300. html, body { background: black; background-image: url(img.svg); }
  301. </style>
  302. <script>
  303. function parameterChanged(index, value) {
  304. console.log("parameterChanged received", index, value);
  305. }
  306. </script>
  307. </head>
  308. <body>
  309. hello world!
  310. </body>
  311. </html>
  312. )PREFIX";
  313. const std::vector<uint8_t> data(html.begin(), html.end());
  314. return choc::ui::WebView::Options::Resource{ data, "text/html" };
  315. }
  316. if (path == "/img.svg")
  317. {
  318. const std::string html = R"PREFIX(<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  319. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
  320. <!-- based on https://github.com/n0jo/rackwindows/blob/master/res/components/rw_knob_large_dark.svg -->
  321. <svg width="47px" height="47px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;">
  322. <g id="knobLDark">
  323. <path id="path3832" d="M23.521,45.109c-7.674,0 -3.302,3.9 -10.224,0.498c-6.922,-3.403 -1.202,-2.341 -5.997,-8.501c-4.795,-6.159 -5.059,-0.201 -6.763,-7.827c-1.704,-7.625 1.043,-2.42 2.76,-10.046c1.718,-7.626 -2.998,-4.102 1.797,-10.221c4.795,-6.12 2.51,-0.673 9.432,-4.035c6.921,-3.363 1.321,-4.977 8.995,-4.977c7.675,0 2.087,1.574 8.996,4.977c6.909,3.402 4.636,-2.045 9.432,4.035c4.795,6.078 0.079,2.689 1.796,10.26c1.717,7.572 4.465,2.422 2.761,10.048c-1.704,7.625 -1.982,1.708 -6.763,7.827c-4.782,6.119 0.924,5.057 -5.998,8.46c-6.921,3.402 -2.549,-0.498 -10.224,-0.498Z" style="fill:#ccc;fill-rule:nonzero;"/>
  324. </g>
  325. </svg>
  326. )PREFIX";
  327. const std::vector<uint8_t> data(html.begin(), html.end());
  328. return choc::ui::WebView::Options::Resource{ data, "image/svg+xml" };
  329. }
  330. return {};
  331. }
  332. #elif WEB_VIEW_USING_X11_IPC
  333. static void getFilenameFromFunctionPtr(char filename[PATH_MAX], const void* const ptr)
  334. {
  335. Dl_info info = {};
  336. dladdr(ptr, &info);
  337. if (info.dli_fname[0] == '.')
  338. {
  339. getcwd(filename, PATH_MAX - 1);
  340. std::strncat(filename, info.dli_fname + 1, PATH_MAX - 1);
  341. }
  342. else if (info.dli_fname[0] != '/')
  343. {
  344. getcwd(filename, PATH_MAX - 1);
  345. std::strncat(filename, "/", PATH_MAX - 1);
  346. std::strncat(filename, info.dli_fname, PATH_MAX - 1);
  347. }
  348. else
  349. {
  350. std::strncpy(filename, info.dli_fname, PATH_MAX - 1);
  351. }
  352. }
  353. #endif
  354. // -----------------------------------------------------------------------------------------------------------
  355. WebViewHandle webViewCreate(const char* const url,
  356. const uintptr_t windowId,
  357. const uint initialWidth,
  358. const uint initialHeight,
  359. const double scaleFactor,
  360. const WebViewOptions& options)
  361. {
  362. #if WEB_VIEW_USING_CHOC
  363. choc::ui::WebView::Options woptions;
  364. woptions.acceptsFirstMouseClick = true;
  365. woptions.enableDebugMode = true;
  366. woptions.fetchResource = fetch_resource;
  367. std::unique_ptr<choc::ui::WebView> webview = std::make_unique<choc::ui::WebView>(woptions);
  368. DISTRHO_SAFE_ASSERT_RETURN(webview->loadedOK(), nullptr);
  369. void* const handle = webview->getViewHandle();
  370. DISTRHO_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  371. choc::ui::WebView* const www = webview.get();
  372. webview->bind("setParameterValue", [www](const choc::value::ValueView&) -> choc::value::Value {
  373. static int pp = 0;
  374. std::string toeval = "typeof(parameterChanged) === 'function' && parameterChanged(";
  375. toeval += std::to_string(++pp);
  376. toeval += ", 0.1)";
  377. d_stdout("param received | %s", toeval.c_str());
  378. www->evaluateJavascript(toeval);
  379. return {};
  380. });
  381. #ifdef DISTRHO_OS_MAC
  382. NSView* const view = static_cast<NSView*>(handle);
  383. [reinterpret_cast<NSView*>(windowId) addSubview:view];
  384. [view setFrame:NSMakeRect(options.offset.x,
  385. options.offset.y,
  386. DISTRHO_UI_DEFAULT_WIDTH - options.offset.x,
  387. DISTRHO_UI_DEFAULT_HEIGHT - options.offset.y)];
  388. #else
  389. const HWND hwnd = static_cast<HWND>(handle);
  390. LONG_PTR flags = GetWindowLongPtr(hwnd, -16);
  391. flags = (flags & ~WS_POPUP) | WS_CHILD;
  392. SetWindowLongPtr(hwnd, -16, flags);
  393. SetParent(hwnd, reinterpret_cast<HWND>(windowId));
  394. SetWindowPos(hwnd, nullptr,
  395. options.offset.x * scaleFactor,
  396. options.offset.y * scaleFactor,
  397. (initialWidth - options.offset.x) * scaleFactor,
  398. (initialHeight - options.offset.y) * scaleFactor,
  399. SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
  400. ShowWindow(hwnd, SW_SHOW);
  401. #endif
  402. return new WebViewData{options.callback, webview.release()};
  403. #elif WEB_VIEW_USING_MACOS_WEBKIT
  404. NSView* const view = reinterpret_cast<NSView*>(windowId);
  405. WKPreferences* const prefs = [[WKPreferences alloc] init];
  406. [prefs setValue:@YES forKey:@"javaScriptCanAccessClipboard"];
  407. [prefs setValue:@YES forKey:@"DOMPasteAllowed"];
  408. // if (debug)
  409. {
  410. [prefs setValue:@YES forKey:@"developerExtrasEnabled"];
  411. // TODO enable_write_console_messages_to_stdout
  412. }
  413. WKWebViewConfiguration* const config = [[WKWebViewConfiguration alloc] init];
  414. config.limitsNavigationsToAppBoundDomains = false;
  415. config.preferences = prefs;
  416. const CGRect rect = CGRectMake(options.offset.x / scaleFactor,
  417. options.offset.y / scaleFactor,
  418. initialWidth,
  419. initialHeight);
  420. WKWebView* const webview = [[WKWebView alloc] initWithFrame:rect
  421. configuration:config];
  422. [webview setHidden:YES];
  423. [view addSubview:webview];
  424. // TODO webkit_web_view_set_background_color
  425. WEB_VIEW_DELEGATE_CLASS_NAME* const delegate = [[WEB_VIEW_DELEGATE_CLASS_NAME alloc] init];
  426. delegate->callback = options.callback;
  427. delegate->callbackPtr = options.callbackPtr;
  428. delegate->loaded = false;
  429. if (WKUserContentController* const controller = [config userContentController])
  430. {
  431. [controller retain];
  432. [controller addScriptMessageHandler:delegate name:@"external"];
  433. if (options.initialJS != nullptr)
  434. {
  435. NSString* const nsInitialJS = [[NSString alloc] initWithBytes:options.initialJS
  436. length:std::strlen(options.initialJS)
  437. encoding:NSUTF8StringEncoding];
  438. WKUserScript* const script = [[WKUserScript alloc] initWithSource:nsInitialJS
  439. injectionTime:WKUserScriptInjectionTimeAtDocumentStart
  440. forMainFrameOnly:true];
  441. [controller addUserScript:script];
  442. [script release];
  443. [nsInitialJS release];
  444. }
  445. }
  446. [webview setNavigationDelegate:delegate];
  447. [webview setUIDelegate:delegate];
  448. NSString* const nsurl = [[NSString alloc] initWithBytes:url
  449. length:std::strlen(url)
  450. encoding:NSUTF8StringEncoding];
  451. NSURLRequest* const urlreq = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString: nsurl]];
  452. d_stdout("url is '%s'", url);
  453. if (std::strncmp(url, "file://", 7) == 0)
  454. {
  455. const char* const lastsep = std::strrchr(url + 7, '/');
  456. NSString* const urlpath = [[NSString alloc] initWithBytes:url
  457. length:(lastsep - url)
  458. encoding:NSUTF8StringEncoding];
  459. [webview loadFileRequest:urlreq
  460. allowingReadAccessToURL:[NSURL URLWithString:urlpath]];
  461. [urlpath release];
  462. }
  463. else
  464. {
  465. [webview loadRequest:urlreq];
  466. }
  467. d_stdout("waiting for load");
  468. if (! delegate->loaded)
  469. {
  470. NSAutoreleasePool* const pool = [[NSAutoreleasePool alloc] init];
  471. NSDate* const date = [NSDate dateWithTimeIntervalSinceNow:0.05];
  472. NSEvent* event;
  473. while (! delegate->loaded)
  474. {
  475. event = [NSApp
  476. #ifdef __MAC_10_12
  477. nextEventMatchingMask:NSEventMaskAny
  478. #else
  479. nextEventMatchingMask:NSAnyEventMask
  480. #endif
  481. untilDate:date
  482. inMode:NSDefaultRunLoopMode
  483. dequeue:YES];
  484. if (event == nil)
  485. break;
  486. [NSApp sendEvent: event];
  487. }
  488. [pool release];
  489. }
  490. d_stdout("waiting done");
  491. [webview setHidden:NO];
  492. [nsurl release];
  493. [config release];
  494. [prefs release];
  495. WebViewData* const handle = new WebViewData;
  496. handle->view = view;
  497. handle->webview = webview;
  498. handle->urlreq = urlreq;
  499. handle->delegate = delegate;
  500. return handle;
  501. #elif WEB_VIEW_USING_X11_IPC
  502. // get startup paths
  503. char ldlinux[PATH_MAX] = {};
  504. getFilenameFromFunctionPtr(ldlinux, dlsym(nullptr, "_rtld_global"));
  505. char filename[PATH_MAX] = {};
  506. getFilenameFromFunctionPtr(filename, reinterpret_cast<const void*>(webViewCreate));
  507. d_stdout("ld-linux is '%s'", ldlinux);
  508. d_stdout("filename is '%s'", filename);
  509. // setup shared memory
  510. int shmfd;
  511. char shmname[128];
  512. void* shmptr;
  513. for (int i = 0; i < 9999; ++i)
  514. {
  515. snprintf(shmname, sizeof(shmname) - 1, "/dpf-webview-%d", i + 1);
  516. shmfd = shm_open(shmname, O_CREAT|O_EXCL|O_RDWR, 0666);
  517. if (shmfd < 0)
  518. continue;
  519. if (ftruncate(shmfd, sizeof(WebViewRingBuffer)) != 0)
  520. {
  521. close(shmfd);
  522. shm_unlink(shmname);
  523. continue;
  524. }
  525. break;
  526. }
  527. if (shmfd < 0)
  528. {
  529. d_stderr("shm_open failed: %s", strerror(errno));
  530. return nullptr;
  531. }
  532. shmptr = mmap(nullptr, sizeof(WebViewRingBuffer), PROT_READ|PROT_WRITE, MAP_SHARED, shmfd, 0);
  533. if (shmptr == nullptr || shmptr == MAP_FAILED)
  534. {
  535. d_stderr("mmap failed: %s", strerror(errno));
  536. close(shmfd);
  537. shm_unlink(shmname);
  538. return nullptr;
  539. }
  540. #ifndef __linux__
  541. sem_init(&handle->shmptr->client.sem, 1, 0);
  542. sem_init(&handle->shmptr->server.sem, 1, 0);
  543. #endif
  544. ::Display* const display = XOpenDisplay(nullptr);
  545. DISTRHO_SAFE_ASSERT_RETURN(display != nullptr, nullptr);
  546. // set up custom child environment
  547. uint envsize = 0;
  548. while (environ[envsize] != nullptr)
  549. ++envsize;
  550. char** const envp = new char*[envsize + 5];
  551. {
  552. uint e = 0;
  553. for (uint i = 0; i < envsize; ++i)
  554. {
  555. if (std::strncmp(environ[i], "LD_PRELOAD=", 11) == 0)
  556. continue;
  557. if (std::strncmp(environ[i], "LD_LIBRARY_PATH=", 16) == 0)
  558. continue;
  559. envp[e++] = strdup(environ[i]);
  560. }
  561. envp[e++] = strdup("LANG=en_US.UTF-8");
  562. envp[e++] = ("DPF_WEB_VIEW_SCALE_FACTOR=" + String(scaleFactor)).getAndReleaseBuffer();
  563. envp[e++] = ("DPF_WEB_VIEW_WIN_ID=" +String(windowId)).getAndReleaseBuffer();
  564. for (uint i = e; i < envsize + 5; ++i)
  565. envp[e++] = nullptr;
  566. }
  567. WebViewData* const handle = new WebViewData;
  568. handle->callback = options.callback;
  569. handle->callbackPtr = options.callbackPtr;
  570. handle->shmfd = shmfd;
  571. handle->shmptr = static_cast<WebViewRingBuffer*>(shmptr);
  572. handle->display = display;
  573. handle->ourWindow = windowId;
  574. std::memcpy(handle->shmname, shmname, sizeof(shmname));
  575. handle->shmptr->valid = true;
  576. handle->rbctrl.setRingBuffer(&handle->shmptr->client, false);
  577. handle->rbctrl.flush();
  578. handle->rbctrl2.setRingBuffer(&handle->shmptr->server, false);
  579. handle->rbctrl2.flush();
  580. const char* const args[] = { ldlinux, filename, "dpf-ld-linux-webview", shmname, nullptr };
  581. handle->p.start(args, envp);
  582. for (uint i = 0; envp[i] != nullptr; ++i)
  583. std::free(envp[i]);
  584. delete[] envp;
  585. handle->rbctrl.writeUInt(kWebViewMessageInitData) &&
  586. handle->rbctrl.writeULong(windowId) &&
  587. handle->rbctrl.writeUInt(initialWidth) &&
  588. handle->rbctrl.writeUInt(initialHeight) &&
  589. handle->rbctrl.writeDouble(scaleFactor) &&
  590. handle->rbctrl.writeInt(options.offset.x) &&
  591. handle->rbctrl.writeInt(options.offset.y);
  592. handle->rbctrl.commitWrite();
  593. webview_wake(&handle->shmptr->client.sem);
  594. for (int i = 0; i < 5 && handle->p.isRunning(); ++i)
  595. {
  596. if (webview_timedwait(&handle->shmptr->server.sem))
  597. return handle;
  598. }
  599. d_stderr("webview client side failed to start");
  600. webViewDestroy(handle);
  601. return nullptr;
  602. #endif
  603. // maybe unused
  604. (void)windowId;
  605. (void)initialWidth;
  606. (void)initialHeight;
  607. (void)scaleFactor;
  608. (void)options;
  609. return nullptr;
  610. }
  611. void webViewDestroy(const WebViewHandle handle)
  612. {
  613. #if WEB_VIEW_USING_CHOC
  614. delete handle->webview;
  615. #elif WEB_VIEW_USING_MACOS_WEBKIT
  616. [handle->webview setHidden:YES];
  617. [handle->webview removeFromSuperview];
  618. [handle->urlreq release];
  619. // [handle->delegate release];
  620. #elif WEB_VIEW_USING_X11_IPC
  621. munmap(handle->shmptr, sizeof(WebViewRingBuffer));
  622. close(handle->shmfd);
  623. shm_unlink(handle->shmname);
  624. XCloseDisplay(handle->display);
  625. #endif
  626. delete handle;
  627. }
  628. void webViewIdle(const WebViewHandle handle)
  629. {
  630. #if WEB_VIEW_USING_X11_IPC
  631. uint32_t size = 0;
  632. void* buffer = nullptr;
  633. while (handle->rbctrl2.isDataAvailableForReading())
  634. {
  635. switch (handle->rbctrl2.readUInt())
  636. {
  637. case kWebViewMessageCallback:
  638. if (const uint32_t len = handle->rbctrl2.readUInt())
  639. {
  640. if (len > size)
  641. {
  642. size = len;
  643. buffer = std::realloc(buffer, len);
  644. if (buffer == nullptr)
  645. {
  646. d_stderr("server out of memory, abort!");
  647. handle->rbctrl2.flush();
  648. return;
  649. }
  650. }
  651. if (handle->rbctrl2.readCustomData(buffer, len))
  652. {
  653. d_debug("server kWebViewMessageCallback -> '%s'", static_cast<char*>(buffer));
  654. if (handle->callback != nullptr)
  655. handle->callback(handle->callbackPtr, static_cast<char*>(buffer));
  656. continue;
  657. }
  658. }
  659. break;
  660. }
  661. d_stderr("server ringbuffer data race, abort!");
  662. handle->rbctrl2.flush();
  663. return;
  664. }
  665. #else
  666. // unused
  667. (void)handle;
  668. #endif
  669. }
  670. void webViewEvaluateJS(const WebViewHandle handle, const char* const js)
  671. {
  672. #if WEB_VIEW_USING_CHOC
  673. #elif WEB_VIEW_USING_MACOS_WEBKIT
  674. NSString* const nsjs = [[NSString alloc] initWithBytes:js
  675. length:std::strlen(js)
  676. encoding:NSUTF8StringEncoding];
  677. [handle->webview evaluateJavaScript:nsjs completionHandler:nil];
  678. [nsjs release];
  679. #elif WEB_VIEW_USING_X11_IPC
  680. d_debug("evaluateJS '%s'", js);
  681. const size_t len = std::strlen(js) + 1;
  682. handle->rbctrl.writeUInt(kWebViewMessageEvaluateJS) &&
  683. handle->rbctrl.writeUInt(len) &&
  684. handle->rbctrl.writeCustomData(js, len);
  685. if (handle->rbctrl.commitWrite())
  686. webview_wake(&handle->shmptr->client.sem);
  687. #endif
  688. // maybe unused
  689. (void)handle;
  690. (void)js;
  691. }
  692. void webViewReload(const WebViewHandle handle)
  693. {
  694. #if WEB_VIEW_USING_CHOC
  695. #elif WEB_VIEW_USING_MACOS_WEBKIT
  696. [handle->webview loadRequest:handle->urlreq];
  697. #elif WEB_VIEW_USING_X11_IPC
  698. d_stdout("reload");
  699. handle->rbctrl.writeUInt(kWebViewMessageReload);
  700. if (handle->rbctrl.commitWrite())
  701. webview_wake(&handle->shmptr->client.sem);
  702. #endif
  703. // maybe unused
  704. (void)handle;
  705. }
  706. void webViewResize(const WebViewHandle handle, const uint width, const uint height, const double scaleFactor)
  707. {
  708. #if WEB_VIEW_USING_CHOC
  709. #ifdef DISTRHO_OS_MAC
  710. NSView* const view = static_cast<NSView*>(handle->webview->getViewHandle());
  711. [view setFrameSize:NSMakeSize(width / scaleFactor, height / scaleFactor)];
  712. #else
  713. const HWND hwnd = static_cast<HWND>(handle->webview->getViewHandle());
  714. SetWindowPos(hwnd, nullptr, 0, 0,
  715. width, height,
  716. SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
  717. #endif
  718. #elif WEB_VIEW_USING_MACOS_WEBKIT
  719. [handle->webview setFrameSize:NSMakeSize(width / scaleFactor, height / scaleFactor)];
  720. #elif WEB_VIEW_USING_X11_IPC
  721. if (handle->childWindow == 0)
  722. {
  723. ::Window rootWindow, parentWindow;
  724. ::Window* childWindows = nullptr;
  725. uint numChildren = 0;
  726. XFlush(handle->display);
  727. XQueryTree(handle->display, handle->ourWindow, &rootWindow, &parentWindow, &childWindows, &numChildren);
  728. if (numChildren == 0 || childWindows == nullptr)
  729. return;
  730. handle->childWindow = childWindows[0];
  731. XFree(childWindows);
  732. }
  733. XResizeWindow(handle->display, handle->childWindow, width, height);
  734. XFlush(handle->display);
  735. #endif
  736. // maybe unused
  737. (void)handle;
  738. (void)width;
  739. (void)height;
  740. (void)scaleFactor;
  741. }
  742. #if WEB_VIEW_USING_X11_IPC
  743. // -----------------------------------------------------------------------------------------------------------
  744. static std::function<void(const char* js)> evaluateFn;
  745. static std::function<void()> reloadFn;
  746. static std::function<void()> terminateFn;
  747. static std::function<void(WebViewRingBuffer* rb)> wakeFn;
  748. // -----------------------------------------------------------------------------------------------------------
  749. struct GtkContainer;
  750. struct GtkPlug;
  751. struct GtkWidget;
  752. struct GtkWindow;
  753. struct JSCValue;
  754. struct WebKitJavascriptResult;
  755. struct WebKitSettings;
  756. struct WebKitUserContentManager;
  757. struct WebKitWebView;
  758. typedef int gboolean;
  759. #define G_CALLBACK(p) reinterpret_cast<void*>(p)
  760. #define GTK_CONTAINER(p) reinterpret_cast<GtkContainer*>(p)
  761. #define GTK_PLUG(p) reinterpret_cast<GtkPlug*>(p)
  762. #define GTK_WINDOW(p) reinterpret_cast<GtkWindow*>(p)
  763. #define WEBKIT_WEB_VIEW(p) reinterpret_cast<WebKitWebView*>(p)
  764. // struct QApplication;
  765. // struct QUrl;
  766. // struct QWebEngineView;
  767. // struct QWindow;
  768. // -----------------------------------------------------------------------------------------------------------
  769. #define JOIN(A, B) A ## B
  770. #define AUTOSYM(S) \
  771. using JOIN(gtk3_, S) = decltype(&S); \
  772. JOIN(gtk3_, S) S = reinterpret_cast<JOIN(gtk3_, S)>(dlsym(nullptr, #S)); \
  773. DISTRHO_SAFE_ASSERT_RETURN(S != nullptr, false);
  774. #define CSYM(S, NAME) \
  775. S NAME = reinterpret_cast<S>(dlsym(nullptr, #NAME)); \
  776. DISTRHO_SAFE_ASSERT_RETURN(NAME != nullptr, false);
  777. #define CPPSYM(S, NAME, SN) \
  778. S NAME = reinterpret_cast<S>(dlsym(nullptr, #SN)); \
  779. DISTRHO_SAFE_ASSERT_RETURN(NAME != nullptr, false);
  780. // -----------------------------------------------------------------------------------------------------------
  781. // gtk3 variant
  782. static void gtk3_idle(void* const ptr)
  783. {
  784. WebViewRingBuffer* const shmptr = static_cast<WebViewRingBuffer*>(ptr);
  785. RingBufferControl<WebViewSharedBuffer> rbctrl;
  786. rbctrl.setRingBuffer(&shmptr->client, false);
  787. uint32_t size = 0;
  788. void* buffer = nullptr;
  789. while (rbctrl.isDataAvailableForReading())
  790. {
  791. switch (rbctrl.readUInt())
  792. {
  793. case kWebViewMessageEvaluateJS:
  794. if (const uint32_t len = rbctrl.readUInt())
  795. {
  796. if (len > size)
  797. {
  798. size = len;
  799. buffer = realloc(buffer, len);
  800. if (buffer == nullptr)
  801. {
  802. d_stderr("lv2ui client out of memory, abort!");
  803. abort();
  804. }
  805. }
  806. if (rbctrl.readCustomData(buffer, len))
  807. {
  808. d_debug("client kWebViewMessageEvaluateJS -> '%s'", static_cast<char*>(buffer));
  809. evaluateFn(static_cast<char*>(buffer));
  810. continue;
  811. }
  812. }
  813. break;
  814. case kWebViewMessageReload:
  815. d_debug("client kWebViewMessageReload");
  816. reloadFn();
  817. continue;
  818. }
  819. d_stderr("client ringbuffer data race, abort!");
  820. abort();
  821. }
  822. free(buffer);
  823. }
  824. static int gtk3_js_cb(WebKitUserContentManager*, WebKitJavascriptResult* const result, void* const arg)
  825. {
  826. WebViewRingBuffer* const shmptr = static_cast<WebViewRingBuffer*>(arg);
  827. using g_free_t = void (*)(void*);
  828. using jsc_value_to_string_t = char* (*)(JSCValue*);
  829. using webkit_javascript_result_get_js_value_t = JSCValue* (*)(WebKitJavascriptResult*);
  830. CSYM(g_free_t, g_free)
  831. CSYM(jsc_value_to_string_t, jsc_value_to_string)
  832. CSYM(webkit_javascript_result_get_js_value_t, webkit_javascript_result_get_js_value)
  833. JSCValue* const value = webkit_javascript_result_get_js_value(result);
  834. DISTRHO_SAFE_ASSERT_RETURN(value != nullptr, false);
  835. char* const string = jsc_value_to_string(value);
  836. DISTRHO_SAFE_ASSERT_RETURN(string != nullptr, false);
  837. d_debug("js call received with data '%s'", string);
  838. const size_t len = std::strlen(string);
  839. RingBufferControl<WebViewSharedBuffer> rbctrl2;
  840. rbctrl2.setRingBuffer(&shmptr->server, false);
  841. rbctrl2.writeUInt(kWebViewMessageCallback) &&
  842. rbctrl2.writeUInt(len) &&
  843. rbctrl2.writeCustomData(string, len);
  844. rbctrl2.commitWrite();
  845. g_free(string);
  846. return 0;
  847. }
  848. static bool gtk3(Display* const display,
  849. const Window winId,
  850. const int x,
  851. const int y,
  852. const uint width,
  853. const uint height,
  854. double scaleFactor,
  855. const char* const url,
  856. WebViewRingBuffer* const shmptr)
  857. {
  858. void* lib;
  859. if ((lib = dlopen("libwebkit2gtk-4.0.so.37", RTLD_NOW|RTLD_GLOBAL)) == nullptr ||
  860. (lib = dlopen("libwebkit2gtk-4.0.so", RTLD_NOW|RTLD_GLOBAL)) == nullptr)
  861. return false;
  862. using g_main_context_invoke_t = void (*)(void*, void*, void*);
  863. using g_signal_connect_data_t = ulong (*)(void*, const char*, void*, void*, void*, int);
  864. using gdk_set_allowed_backends_t = void (*)(const char*);
  865. using gtk_container_add_t = void (*)(GtkContainer*, GtkWidget*);
  866. using gtk_init_check_t = gboolean (*)(int*, char***);
  867. using gtk_main_t = void (*)();
  868. using gtk_main_quit_t = void (*)();
  869. using gtk_plug_get_id_t = Window (*)(GtkPlug*);
  870. using gtk_plug_new_t = GtkWidget* (*)(Window);
  871. using gtk_widget_show_all_t = void (*)(GtkWidget*);
  872. using gtk_window_move_t = void (*)(GtkWindow*, int, int);
  873. using gtk_window_set_default_size_t = void (*)(GtkWindow*, int, int);
  874. using webkit_settings_new_t = WebKitSettings* (*)();
  875. using webkit_settings_set_enable_developer_extras_t = void (*)(WebKitSettings*, gboolean);
  876. using webkit_settings_set_enable_write_console_messages_to_stdout_t = void (*)(WebKitSettings*, gboolean);
  877. using webkit_settings_set_hardware_acceleration_policy_t = void (*)(WebKitSettings*, int);
  878. using webkit_settings_set_javascript_can_access_clipboard_t = void (*)(WebKitSettings*, gboolean);
  879. using webkit_user_content_manager_register_script_message_handler_t = gboolean (*)(WebKitUserContentManager*, const char*);
  880. using webkit_web_view_evaluate_javascript_t = void* (*)(WebKitWebView*, const char*, ssize_t, const char*, const char*, void*, void*, void*);
  881. using webkit_web_view_get_user_content_manager_t = WebKitUserContentManager* (*)(WebKitWebView*);
  882. using webkit_web_view_load_uri_t = void (*)(WebKitWebView*, const char*);
  883. using webkit_web_view_new_with_settings_t = GtkWidget* (*)(WebKitSettings*);
  884. using webkit_web_view_run_javascript_t = void* (*)(WebKitWebView*, const char*, void*, void*, void*);
  885. using webkit_web_view_set_background_color_t = void (*)(WebKitWebView*, const double*);
  886. CSYM(g_main_context_invoke_t, g_main_context_invoke)
  887. CSYM(g_signal_connect_data_t, g_signal_connect_data)
  888. CSYM(gdk_set_allowed_backends_t, gdk_set_allowed_backends)
  889. CSYM(gtk_container_add_t, gtk_container_add)
  890. CSYM(gtk_init_check_t, gtk_init_check)
  891. CSYM(gtk_main_t, gtk_main)
  892. CSYM(gtk_main_quit_t, gtk_main_quit)
  893. CSYM(gtk_plug_get_id_t, gtk_plug_get_id)
  894. CSYM(gtk_plug_new_t, gtk_plug_new)
  895. CSYM(gtk_widget_show_all_t, gtk_widget_show_all)
  896. CSYM(gtk_window_move_t, gtk_window_move)
  897. CSYM(gtk_window_set_default_size_t, gtk_window_set_default_size)
  898. CSYM(webkit_settings_new_t, webkit_settings_new)
  899. CSYM(webkit_settings_set_enable_developer_extras_t, webkit_settings_set_enable_developer_extras)
  900. CSYM(webkit_settings_set_enable_write_console_messages_to_stdout_t, webkit_settings_set_enable_write_console_messages_to_stdout)
  901. CSYM(webkit_settings_set_hardware_acceleration_policy_t, webkit_settings_set_hardware_acceleration_policy)
  902. CSYM(webkit_settings_set_javascript_can_access_clipboard_t, webkit_settings_set_javascript_can_access_clipboard)
  903. CSYM(webkit_user_content_manager_register_script_message_handler_t, webkit_user_content_manager_register_script_message_handler)
  904. CSYM(webkit_web_view_get_user_content_manager_t, webkit_web_view_get_user_content_manager)
  905. CSYM(webkit_web_view_load_uri_t, webkit_web_view_load_uri)
  906. CSYM(webkit_web_view_new_with_settings_t, webkit_web_view_new_with_settings)
  907. CSYM(webkit_web_view_set_background_color_t, webkit_web_view_set_background_color)
  908. // special case for legacy API handling
  909. webkit_web_view_evaluate_javascript_t webkit_web_view_evaluate_javascript = reinterpret_cast<webkit_web_view_evaluate_javascript_t>(dlsym(nullptr, "webkit_web_view_evaluate_javascript"));
  910. webkit_web_view_run_javascript_t webkit_web_view_run_javascript = reinterpret_cast<webkit_web_view_run_javascript_t>(dlsym(nullptr, "webkit_web_view_run_javascript"));
  911. DISTRHO_SAFE_ASSERT_RETURN(webkit_web_view_evaluate_javascript != nullptr || webkit_web_view_run_javascript != nullptr, false);
  912. const int gdkScale = std::fmod(scaleFactor, 1.0) >= 0.75
  913. ? static_cast<int>(scaleFactor + 0.5)
  914. : static_cast<int>(scaleFactor);
  915. if (gdkScale != 1)
  916. {
  917. char scale[8] = {};
  918. std::snprintf(scale, 7, "%d", gdkScale);
  919. setenv("GDK_SCALE", scale, 1);
  920. std::snprintf(scale, 7, "%.2f", (1.0 / scaleFactor) * 1.2);
  921. setenv("GDK_DPI_SCALE", scale, 1);
  922. }
  923. else if (scaleFactor > 1.0)
  924. {
  925. char scale[8] = {};
  926. std::snprintf(scale, 7, "%.2f", (1.0 / scaleFactor) * 1.4);
  927. setenv("GDK_DPI_SCALE", scale, 1);
  928. }
  929. scaleFactor /= gdkScale;
  930. gdk_set_allowed_backends("x11");
  931. if (! gtk_init_check (nullptr, nullptr))
  932. return false;
  933. GtkWidget* const window = gtk_plug_new(winId);
  934. DISTRHO_SAFE_ASSERT_RETURN(window != nullptr, false);
  935. gtk_window_set_default_size(GTK_WINDOW(window),
  936. (width - x) * scaleFactor,
  937. (height - y) * scaleFactor);
  938. gtk_window_move(GTK_WINDOW(window), x * scaleFactor, y * scaleFactor);
  939. WebKitSettings* const settings = webkit_settings_new();
  940. DISTRHO_SAFE_ASSERT_RETURN(settings != nullptr, false);
  941. // TODO DOMPasteAllowed
  942. webkit_settings_set_javascript_can_access_clipboard(settings, true);
  943. webkit_settings_set_hardware_acceleration_policy(settings, 2 /* WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER */);
  944. // if (debug)
  945. {
  946. webkit_settings_set_enable_developer_extras(settings, true);
  947. webkit_settings_set_enable_write_console_messages_to_stdout(settings, true);
  948. }
  949. GtkWidget* const webview = webkit_web_view_new_with_settings(settings);
  950. DISTRHO_SAFE_ASSERT_RETURN(webview != nullptr, false);
  951. const double color[] = {49.0/255, 54.0/255, 59.0/255, 1};
  952. webkit_web_view_set_background_color(WEBKIT_WEB_VIEW(webview), color);
  953. if (WebKitUserContentManager* const manager = webkit_web_view_get_user_content_manager(WEBKIT_WEB_VIEW(webview)))
  954. {
  955. g_signal_connect_data(manager, "script-message-received::external", G_CALLBACK(gtk3_js_cb), shmptr, nullptr, 0);
  956. webkit_user_content_manager_register_script_message_handler(manager, "external");
  957. }
  958. webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), url);
  959. gtk_container_add(GTK_CONTAINER(window), webview);
  960. gtk_widget_show_all(window);
  961. Window wid = gtk_plug_get_id(GTK_PLUG(window));
  962. XMapWindow(display, wid);
  963. XFlush(display);
  964. evaluateFn = [=](const char* const js){
  965. if (webkit_web_view_evaluate_javascript != nullptr)
  966. webkit_web_view_evaluate_javascript(WEBKIT_WEB_VIEW(webview), js, -1,
  967. nullptr, nullptr, nullptr, nullptr, nullptr);
  968. else
  969. webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(webview), js, nullptr, nullptr, nullptr);
  970. };
  971. reloadFn = [=](){
  972. webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), url);
  973. };
  974. terminateFn = [=](){
  975. d_stdout("terminateFn");
  976. static bool quit = true;
  977. if (quit)
  978. {
  979. quit = false;
  980. gtk_main_quit();
  981. }
  982. };
  983. wakeFn = [=](WebViewRingBuffer* const rb){
  984. g_main_context_invoke(NULL, G_CALLBACK(gtk3_idle), rb);
  985. };
  986. // notify server we started ok
  987. webview_wake(&shmptr->server.sem);
  988. gtk_main();
  989. d_stdout("quit");
  990. dlclose(lib);
  991. return true;
  992. }
  993. #if 0
  994. // -----------------------------------------------------------------------------------------------------------
  995. // qt5webengine variant
  996. static bool qt5webengine(const Window winId, const double scaleFactor, const char* const url)
  997. {
  998. void* lib;
  999. if ((lib = dlopen("libQt5WebEngineWidgets.so.5", RTLD_NOW|RTLD_GLOBAL)) == nullptr ||
  1000. (lib = dlopen("libQt5WebEngineWidgets.so", RTLD_NOW|RTLD_GLOBAL)) == nullptr)
  1001. return false;
  1002. using QApplication__init_t = void (*)(QApplication*, int&, char**, int);
  1003. using QApplication_exec_t = void (*)();
  1004. using QApplication_setAttribute_t = void (*)(Qt::ApplicationAttribute, bool);
  1005. using QString__init_t = void (*)(void*, const QChar*, ptrdiff_t);
  1006. using QUrl__init_t = void (*)(void*, const QString&, int /* QUrl::ParsingMode */);
  1007. using QWebEngineView__init_t = void (*)(QWebEngineView*, void*);
  1008. using QWebEngineView_move_t = void (*)(QWebEngineView*, const QPoint&);
  1009. using QWebEngineView_resize_t = void (*)(QWebEngineView*, const QSize&);
  1010. using QWebEngineView_setUrl_t = void (*)(QWebEngineView*, const QUrl&);
  1011. using QWebEngineView_show_t = void (*)(QWebEngineView*);
  1012. using QWebEngineView_winId_t = ulonglong (*)(QWebEngineView*);
  1013. using QWebEngineView_windowHandle_t = QWindow* (*)(QWebEngineView*);
  1014. using QWindow_fromWinId_t = QWindow* (*)(ulonglong);
  1015. using QWindow_setParent_t = void (*)(QWindow*, void*);
  1016. CPPSYM(QApplication__init_t, QApplication__init, _ZN12QApplicationC1ERiPPci)
  1017. CPPSYM(QApplication_exec_t, QApplication_exec, _ZN15QGuiApplication4execEv)
  1018. CPPSYM(QApplication_setAttribute_t, QApplication_setAttribute, _ZN16QCoreApplication12setAttributeEN2Qt20ApplicationAttributeEb)
  1019. CPPSYM(QString__init_t, QString__init, _ZN7QStringC2EPK5QChari)
  1020. CPPSYM(QUrl__init_t, QUrl__init, _ZN4QUrlC1ERK7QStringNS_11ParsingModeE)
  1021. CPPSYM(QWebEngineView__init_t, QWebEngineView__init, _ZN14QWebEngineViewC1EP7QWidget)
  1022. CPPSYM(QWebEngineView_move_t, QWebEngineView_move, _ZN7QWidget4moveERK6QPoint)
  1023. CPPSYM(QWebEngineView_resize_t, QWebEngineView_resize, _ZN7QWidget6resizeERK5QSize)
  1024. CPPSYM(QWebEngineView_setUrl_t, QWebEngineView_setUrl, _ZN14QWebEngineView6setUrlERK4QUrl)
  1025. CPPSYM(QWebEngineView_show_t, QWebEngineView_show, _ZN7QWidget4showEv)
  1026. CPPSYM(QWebEngineView_winId_t, QWebEngineView_winId, _ZNK7QWidget5winIdEv)
  1027. CPPSYM(QWebEngineView_windowHandle_t, QWebEngineView_windowHandle, _ZNK7QWidget12windowHandleEv)
  1028. CPPSYM(QWindow_fromWinId_t, QWindow_fromWinId, _ZN7QWindow9fromWinIdEy)
  1029. CPPSYM(QWindow_setParent_t, QWindow_setParent, _ZN7QWindow9setParentEPS_)
  1030. unsetenv("QT_FONT_DPI");
  1031. unsetenv("QT_SCREEN_SCALE_FACTORS");
  1032. unsetenv("QT_USE_PHYSICAL_DPI");
  1033. setenv("QT_AUTO_SCREEN_SCALE_FACTOR", "0", 1);
  1034. char scale[8] = {};
  1035. std::snprintf(scale, 7, "%.2f", scaleFactor);
  1036. setenv("QT_SCALE_FACTOR", scale, 1);
  1037. QApplication_setAttribute(Qt::AA_X11InitThreads, true);
  1038. QApplication_setAttribute(Qt::AA_EnableHighDpiScaling, true);
  1039. QApplication_setAttribute(Qt::AA_UseHighDpiPixmaps, true);
  1040. static int argc = 0;
  1041. static char* argv[] = { nullptr };
  1042. uint8_t _app[64]; // sizeof(QApplication) == 16
  1043. QApplication* const app = reinterpret_cast<QApplication*>(_app);
  1044. QApplication__init(app, argc, argv, 0);
  1045. uint8_t _qstrurl[32]; // sizeof(QString) == 8
  1046. QString* const qstrurl(reinterpret_cast<QString*>(_qstrurl));
  1047. {
  1048. const size_t url_len = std::strlen(url);
  1049. QChar* const url_qchar = new QChar[url_len + 1];
  1050. for (size_t i = 0; i < url_len; ++i)
  1051. url_qchar[i] = QChar(url[i]);
  1052. url_qchar[url_len] = 0;
  1053. QString__init(qstrurl, url_qchar, url_len);
  1054. }
  1055. uint8_t _qurl[32]; // sizeof(QUrl) == 8
  1056. QUrl* const qurl(reinterpret_cast<QUrl*>(_qurl));
  1057. QUrl__init(qurl, *qstrurl, 1 /* QUrl::StrictMode */);
  1058. uint8_t _webview[128]; // sizeof(QWebEngineView) == 56
  1059. QWebEngineView* const webview = reinterpret_cast<QWebEngineView*>(_webview);
  1060. QWebEngineView__init(webview, nullptr);
  1061. QWebEngineView_move(webview, QPoint(0, kVerticalOffset));
  1062. QWebEngineView_resize(webview, QSize(DISTRHO_UI_DEFAULT_WIDTH, DISTRHO_UI_DEFAULT_HEIGHT - kVerticalOffset));
  1063. QWebEngineView_winId(webview);
  1064. QWindow_setParent(QWebEngineView_windowHandle(webview), QWindow_fromWinId(winId));
  1065. QWebEngineView_setUrl(webview, *qurl);
  1066. QWebEngineView_show(webview);
  1067. reloadFn = [=](){
  1068. QWebEngineView_setUrl(webview, *qurl);
  1069. };
  1070. terminateFn = [=](){
  1071. // TODO
  1072. };
  1073. QApplication_exec();
  1074. dlclose(lib);
  1075. return true;
  1076. }
  1077. // -----------------------------------------------------------------------------------------------------------
  1078. // qt6webengine variant (same as qt5 but `QString__init_t` has different arguments)
  1079. static bool qt6webengine(const Window winId, const double scaleFactor, const char* const url)
  1080. {
  1081. void* lib;
  1082. if ((lib = dlopen("libQt6WebEngineWidgets.so.6", RTLD_NOW|RTLD_GLOBAL)) == nullptr ||
  1083. (lib = dlopen("libQt6WebEngineWidgets.so", RTLD_NOW|RTLD_GLOBAL)) == nullptr)
  1084. return false;
  1085. using QApplication__init_t = void (*)(QApplication*, int&, char**, int);
  1086. using QApplication_exec_t = void (*)();
  1087. using QApplication_setAttribute_t = void (*)(Qt::ApplicationAttribute, bool);
  1088. using QString__init_t = void (*)(void*, const QChar*, long long);
  1089. using QUrl__init_t = void (*)(void*, const QString&, int /* QUrl::ParsingMode */);
  1090. using QWebEngineView__init_t = void (*)(QWebEngineView*, void*);
  1091. using QWebEngineView_move_t = void (*)(QWebEngineView*, const QPoint&);
  1092. using QWebEngineView_resize_t = void (*)(QWebEngineView*, const QSize&);
  1093. using QWebEngineView_setUrl_t = void (*)(QWebEngineView*, const QUrl&);
  1094. using QWebEngineView_show_t = void (*)(QWebEngineView*);
  1095. using QWebEngineView_winId_t = ulonglong (*)(QWebEngineView*);
  1096. using QWebEngineView_windowHandle_t = QWindow* (*)(QWebEngineView*);
  1097. using QWindow_fromWinId_t = QWindow* (*)(ulonglong);
  1098. using QWindow_setParent_t = void (*)(QWindow*, void*);
  1099. CPPSYM(QApplication__init_t, QApplication__init, _ZN12QApplicationC1ERiPPci)
  1100. CPPSYM(QApplication_exec_t, QApplication_exec, _ZN15QGuiApplication4execEv)
  1101. CPPSYM(QApplication_setAttribute_t, QApplication_setAttribute, _ZN16QCoreApplication12setAttributeEN2Qt20ApplicationAttributeEb)
  1102. CPPSYM(QString__init_t, QString__init, _ZN7QStringC2EPK5QCharx)
  1103. CPPSYM(QUrl__init_t, QUrl__init, _ZN4QUrlC1ERK7QStringNS_11ParsingModeE)
  1104. CPPSYM(QWebEngineView__init_t, QWebEngineView__init, _ZN14QWebEngineViewC1EP7QWidget)
  1105. CPPSYM(QWebEngineView_move_t, QWebEngineView_move, _ZN7QWidget4moveERK6QPoint)
  1106. CPPSYM(QWebEngineView_resize_t, QWebEngineView_resize, _ZN7QWidget6resizeERK5QSize)
  1107. CPPSYM(QWebEngineView_setUrl_t, QWebEngineView_setUrl, _ZN14QWebEngineView6setUrlERK4QUrl)
  1108. CPPSYM(QWebEngineView_show_t, QWebEngineView_show, _ZN7QWidget4showEv)
  1109. CPPSYM(QWebEngineView_winId_t, QWebEngineView_winId, _ZNK7QWidget5winIdEv)
  1110. CPPSYM(QWebEngineView_windowHandle_t, QWebEngineView_windowHandle, _ZNK7QWidget12windowHandleEv)
  1111. CPPSYM(QWindow_fromWinId_t, QWindow_fromWinId, _ZN7QWindow9fromWinIdEy)
  1112. CPPSYM(QWindow_setParent_t, QWindow_setParent, _ZN7QWindow9setParentEPS_)
  1113. unsetenv("QT_FONT_DPI");
  1114. unsetenv("QT_SCREEN_SCALE_FACTORS");
  1115. unsetenv("QT_USE_PHYSICAL_DPI");
  1116. setenv("QT_AUTO_SCREEN_SCALE_FACTOR", "0", 1);
  1117. char scale[8] = {};
  1118. std::snprintf(scale, 7, "%.2f", scaleFactor);
  1119. setenv("QT_SCALE_FACTOR", scale, 1);
  1120. QApplication_setAttribute(Qt::AA_X11InitThreads, true);
  1121. QApplication_setAttribute(Qt::AA_EnableHighDpiScaling, true);
  1122. QApplication_setAttribute(Qt::AA_UseHighDpiPixmaps, true);
  1123. static int argc = 0;
  1124. static char* argv[] = { nullptr };
  1125. uint8_t _app[64]; // sizeof(QApplication) == 16
  1126. QApplication* const app = reinterpret_cast<QApplication*>(_app);
  1127. QApplication__init(app, argc, argv, 0);
  1128. uint8_t _qstrurl[32]; // sizeof(QString) == 8
  1129. QString* const qstrurl(reinterpret_cast<QString*>(_qstrurl));
  1130. {
  1131. const size_t url_len = std::strlen(url);
  1132. QChar* const url_qchar = new QChar[url_len + 1];
  1133. for (size_t i = 0; i < url_len; ++i)
  1134. url_qchar[i] = QChar(url[i]);
  1135. url_qchar[url_len] = 0;
  1136. QString__init(qstrurl, url_qchar, url_len);
  1137. }
  1138. uint8_t _qurl[32]; // sizeof(QUrl) == 8
  1139. QUrl* const qurl(reinterpret_cast<QUrl*>(_qurl));
  1140. QUrl__init(qurl, *qstrurl, 1 /* QUrl::StrictMode */);
  1141. uint8_t _webview[128]; // sizeof(QWebEngineView) == 56
  1142. QWebEngineView* const webview = reinterpret_cast<QWebEngineView*>(_webview);
  1143. QWebEngineView__init(webview, nullptr);
  1144. QWebEngineView_move(webview, QPoint(0, kVerticalOffset));
  1145. QWebEngineView_resize(webview, QSize(DISTRHO_UI_DEFAULT_WIDTH, DISTRHO_UI_DEFAULT_HEIGHT - kVerticalOffset));
  1146. QWebEngineView_winId(webview);
  1147. QWindow_setParent(QWebEngineView_windowHandle(webview), QWindow_fromWinId(winId));
  1148. QWebEngineView_setUrl(webview, *qurl);
  1149. QWebEngineView_show(webview);
  1150. reloadFn = [=](){
  1151. QWebEngineView_setUrl(webview, *qurl);
  1152. };
  1153. terminateFn = [=](){
  1154. // TODO
  1155. };
  1156. QApplication_exec();
  1157. dlclose(lib);
  1158. return true;
  1159. }
  1160. #endif
  1161. // -----------------------------------------------------------------------------------------------------------
  1162. // startup via ld-linux
  1163. static void signalHandler(const int sig)
  1164. {
  1165. switch (sig)
  1166. {
  1167. case SIGTERM:
  1168. terminateFn();
  1169. break;
  1170. }
  1171. }
  1172. static void* threadHandler(void* const ptr)
  1173. {
  1174. WebViewRingBuffer* const shmptr = static_cast<WebViewRingBuffer*>(ptr);
  1175. // TODO wait until page is loaded, or something better
  1176. d_sleep(1);
  1177. while (shmptr->valid)
  1178. {
  1179. if (webview_timedwait(&shmptr->client.sem))
  1180. wakeFn(shmptr);
  1181. }
  1182. return nullptr;
  1183. }
  1184. int dpf_webview_start(const int argc, char* argv[])
  1185. {
  1186. d_stdout("started %d %s", argc, argv[1]);
  1187. if (argc != 3)
  1188. {
  1189. d_stderr("WebView entry point, nothing to see here! ;)");
  1190. return 1;
  1191. }
  1192. uselocale(newlocale(LC_NUMERIC_MASK, "C", nullptr));
  1193. Display* const display = XOpenDisplay(nullptr);
  1194. DISTRHO_SAFE_ASSERT_RETURN(display != nullptr, 1);
  1195. const char* const shmname = argv[2];
  1196. const int shmfd = shm_open(shmname, O_RDWR, 0);
  1197. if (shmfd < 0)
  1198. {
  1199. d_stderr("shm_open failed: %s", std::strerror(errno));
  1200. return 1;
  1201. }
  1202. WebViewRingBuffer* const shmptr = static_cast<WebViewRingBuffer*>(mmap(nullptr,
  1203. sizeof(WebViewRingBuffer),
  1204. PROT_READ|PROT_WRITE,
  1205. MAP_SHARED,
  1206. shmfd, 0));
  1207. if (shmptr == nullptr || shmptr == nullptr)
  1208. {
  1209. d_stderr("mmap failed: %s", std::strerror(errno));
  1210. close(shmfd);
  1211. return 1;
  1212. }
  1213. RingBufferControl<WebViewSharedBuffer> rbctrl;
  1214. rbctrl.setRingBuffer(&shmptr->client, false);
  1215. const char* url = "file:///home/falktx/Source/DISTRHO/DPF/examples/WebMeters/index.html";
  1216. // fetch initial data
  1217. bool hasInitialData = false;
  1218. Window winId = 0;
  1219. uint width = 0, height = 0;
  1220. double scaleFactor = 0;
  1221. int x = 0, y = 0;
  1222. while (shmptr->valid && webview_timedwait(&shmptr->client.sem))
  1223. {
  1224. if (rbctrl.isDataAvailableForReading())
  1225. {
  1226. DISTRHO_SAFE_ASSERT_RETURN(rbctrl.readUInt() == kWebViewMessageInitData, 1);
  1227. hasInitialData = true;
  1228. winId = rbctrl.readULong();
  1229. width = rbctrl.readUInt();
  1230. height = rbctrl.readUInt();
  1231. scaleFactor = rbctrl.readDouble();
  1232. x = rbctrl.readInt();
  1233. y = rbctrl.readInt();
  1234. }
  1235. }
  1236. pthread_t thread;
  1237. if (hasInitialData && pthread_create(&thread, nullptr, threadHandler, shmptr) == 0)
  1238. {
  1239. struct sigaction sig = {};
  1240. sig.sa_handler = signalHandler;
  1241. sig.sa_flags = SA_RESTART;
  1242. sigemptyset(&sig.sa_mask);
  1243. sigaction(SIGTERM, &sig, nullptr);
  1244. // qt5webengine(winId, scaleFactor, url) ||
  1245. // qt6webengine(winId, scaleFactor, url) ||
  1246. gtk3(display, winId, x, y, width, height, scaleFactor, url, shmptr);
  1247. shmptr->valid = false;
  1248. pthread_join(thread, nullptr);
  1249. }
  1250. munmap(shmptr, sizeof(WebViewRingBuffer));
  1251. close(shmfd);
  1252. XCloseDisplay(display);
  1253. return 0;
  1254. }
  1255. // --------------------------------------------------------------------------------------------------------------------
  1256. #endif // WEB_VIEW_USING_X11_IPC
  1257. #ifdef WEB_VIEW_DGL_NAMESPACE
  1258. END_NAMESPACE_DGL
  1259. #else
  1260. END_NAMESPACE_DISTRHO
  1261. #endif
  1262. #undef MACRO_NAME
  1263. #undef MACRO_NAME2
  1264. #undef WEB_VIEW_DISTRHO_NAMESPACE
  1265. #undef WEB_VIEW_DGL_NAMESPACE