summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorembeddedt <42941056+embeddedt@users.noreply.github.com>2021-02-08 13:18:30 -0500
committerGitHub <noreply@github.com>2021-02-08 19:18:30 +0100
commit6c8f2227cdc01c978ec1cbf08a93925e148175b8 (patch)
tree810401a7745c4b21c25bfa39b71786995ecc272d
parentac2b5e57cf2805eb24370b664380322d62a04eff (diff)
downloadopenttd-6c8f2227cdc01c978ec1cbf08a93925e148175b8.tar.xz
Fix: [Emscripten] open links in browser (#8655)
-rw-r--r--os/emscripten/pre.js28
-rw-r--r--src/os/unix/unix.cpp12
2 files changed, 39 insertions, 1 deletions
diff --git a/os/emscripten/pre.js b/os/emscripten/pre.js
index 5cbd899e0..1563e4f95 100644
--- a/os/emscripten/pre.js
+++ b/os/emscripten/pre.js
@@ -71,6 +71,34 @@ Module.preRun.push(function() {
* add_server("localhost", 3979); */
}
+ var leftButtonDown = false;
+ document.addEventListener("mousedown", e => {
+ if (e.button == 0) {
+ leftButtonDown = true;
+ }
+ });
+ document.addEventListener("mouseup", e => {
+ if (e.button == 0) {
+ leftButtonDown = false;
+ }
+ });
+ window.openttd_open_url = function(url, url_len) {
+ const url_string = UTF8ToString(url, url_len);
+ function openWindow() {
+ document.removeEventListener("mouseup", openWindow);
+ window.open(url_string, '_blank');
+ }
+ /* Trying to open the URL while the mouse is down results in the button getting stuck, so wait for the
+ * mouse to be released before opening it. However, when OpenTTD is lagging, the mouse can get released
+ * before the button click even registers, so check for that, and open the URL immediately if that's the
+ * case. */
+ if (leftButtonDown) {
+ document.addEventListener("mouseup", openWindow);
+ } else {
+ openWindow();
+ }
+ }
+
/* https://github.com/emscripten-core/emscripten/pull/12995 implements this
* properly. Till that time, we use a polyfill. */
SOCKFS.websocket_sock_ops.createPeer_ = SOCKFS.websocket_sock_ops.createPeer;
diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp
index 4b94539b0..9ef2bc386 100644
--- a/src/os/unix/unix.cpp
+++ b/src/os/unix/unix.cpp
@@ -28,6 +28,10 @@
#include <SDL.h>
#endif
+#ifdef __EMSCRIPTEN__
+# include <emscripten.h>
+#endif
+
#ifdef __APPLE__
# include <sys/mount.h>
#elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
@@ -288,7 +292,13 @@ bool GetClipboardContents(char *buffer, const char *last)
#endif
-#ifndef __APPLE__
+#if defined(__EMSCRIPTEN__)
+void OSOpenBrowser(const char *url)
+{
+ /* Implementation in pre.js */
+ EM_ASM({ if(window["openttd_open_url"]) window.openttd_open_url($0, $1) }, url, strlen(url));
+}
+#elif !defined( __APPLE__)
void OSOpenBrowser(const char *url)
{
pid_t child_pid = fork();