From 87ee3298a3bc1024d6189849acaad3f850985189 Mon Sep 17 00:00:00 2001 From: Spoike Date: Mon, 7 Feb 2022 14:33:44 +0000 Subject: [PATCH] Let the browser port access the system clipboard (where permitted/allowed by browsers). git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@6179 fc73d0e0-1445-4013-8a0c-d673dee63da5 --- engine/web/ftejslib.js | 47 ++++++++++++++++++++++++++++++++++++++++++ engine/web/sys_web.c | 5 ++--- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/engine/web/ftejslib.js b/engine/web/ftejslib.js index 2d1600707..d991594e5 100644 --- a/engine/web/ftejslib.js +++ b/engine/web/ftejslib.js @@ -69,6 +69,7 @@ mergeInto(LibraryManager.library, ctxwarned:0, pointerislocked:0, pointerwantlock:0, + clipboard:"", linebuffer:'', localstorefailure:false, w: -1, @@ -1120,6 +1121,52 @@ mergeInto(LibraryManager.library, }; img.crossorigin = true; img.src = "data:image/png;base64," + encode64(HEAPU8.subarray(dataptr, dataptr+datasize)); + }, + + Sys_Clipboard_PasteText: function(cbt, callback, ctx) + { + if (cbt != 0) + return; //don't do selections. + + let docallback = function(text) + { + FTEC.clipboard = text; + try{ + let stringlen = (text.length*3)+1; + let dataptr = _malloc(stringlen); + stringToUTF8(text, dataptr, stringlen); + {{{makeDynCall('vii')}}}(callback, ctx, dataptr); + _free(dataptr); + }catch(e){ + } + }; + + //try pasting. if it fails then use our internal string. + try + { + navigator.clipboard.readText() + .then(docallback) + .catch((e)=>{docallback(FTEC.clipboard)}); + } + catch(e) + { //clipboard API not supported at all. + console.log(e); //happens in firefox. lets print it so we know WHY its failing. + docallback(FTEC.clipboard); + } + }, + Sys_SaveClipboard: function(cbt, text) + { + if (cbt != 0) + return; //don't do selections. + + FTEC.clipboard = UTF8ToString(text); + + try + { + //try and copy it to the system clipboard too. + navigator.clipboard.writeText(FTEC.clipboard); + } + catch {} } }); diff --git a/engine/web/sys_web.c b/engine/web/sys_web.c index 6f18ec1d9..fdc2c9bd0 100644 --- a/engine/web/sys_web.c +++ b/engine/web/sys_web.c @@ -314,8 +314,7 @@ qboolean Sys_RunInstaller(void) } #endif -#define SYS_CLIPBOARD_SIZE 256 -static char *clipboard_buffer; +/*static char *clipboard_buffer; void Sys_Clipboard_PasteText(clipboardtype_t cbt, void (*callback)(void *cb, const char *utf8), void *ctx) { callback(ctx, clipboard_buffer); @@ -324,7 +323,7 @@ void Sys_SaveClipboard(clipboardtype_t cbt, const char *text) { free(clipboard_buffer); clipboard_buffer = strdup(text); -} +}*/ #ifdef MULTITHREAD /* Thread creation calls */