Skip to content

Commit

Permalink
#1844 implement clipboard contents polling so we can detect client-si…
Browse files Browse the repository at this point in the history
…de clipboard changes and update the server

git-svn-id: https://xpra.org/svn/Xpra/trunk@23724 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Sep 6, 2019
1 parent d27f6d1 commit c1cbe4d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/html5/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,9 @@ <h2>Xpra Bug Report</h2>
if (navigator.clipboard && navigator.clipboard.readText) {
navigator.clipboard.readText().then(function(text) {
cdebug("clipboard", "paste event, text=", text);
client.send_clipboard_token(unescape(encodeURIComponent(text)));
var paste_data = unescape(encodeURIComponent(text));
client.clipboard_buffer = paste_data;
client.send_clipboard_token(paste_data);
}, function(err) {
cdebug("clipboard", "paste event failed:", err);
});
Expand All @@ -734,10 +736,10 @@ <h2>Xpra Bug Report</h2>
if (Utilities.isIE()) {
datatype = "Text";
}
paste_data = clipboardData.getData(datatype);
paste_data = unescape(encodeURIComponent(clipboardData.getData(datatype)));
cdebug("clipboard", "paste event, data=", paste_data);
client.clipboard_buffer = paste_data;
client.send_clipboard_token(unescape(encodeURIComponent(paste_data)));
client.send_clipboard_token(paste_data);
}
return false;
});
Expand All @@ -763,12 +765,11 @@ <h2>Xpra Bug Report</h2>
var clipboard_buffer = client.get_clipboard_buffer();
var clipboard_datatype = (client.get_clipboard_datatype() || "").toLowerCase();
var is_text = clipboard_datatype.indexOf("text")>=0 || clipboard_datatype.indexOf("string")>=0;
if (is_text) {
pasteboard.text(clipboard_buffer);
}
else {
pasteboard.text("");
if (!is_text) {
//maybe just abort here instead?
clipboard_buffer = "";
}
pasteboard.text(clipboard_buffer);
pasteboard.select();
cdebug("clipboard", "click event, with pending clipboard datatype=", clipboard_datatype, ", buffer=", clipboard_buffer);
//for IE:
Expand All @@ -794,6 +795,8 @@ <h2>Xpra Bug Report</h2>
//probably no point in trying again?
}
if (success) {
//clipboard_buffer may have been cleared if not set to text:
client.clipboard_buffer = clipboard_buffer;
client.clipboard_pending = false;
}
}
Expand Down
46 changes: 45 additions & 1 deletion src/html5/js/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,7 @@ XpraClient.prototype.do_window_mouse_click = function(e, window, pressed) {
if (this.server_readonly || this.mouse_grabbed || !this.connected) {
return;
}
this._poll_clipboard(e);
var mouse = this.getMouse(e, window),
x = Math.round(mouse.x),
y = Math.round(mouse.y);
Expand Down Expand Up @@ -1405,6 +1406,49 @@ XpraClient.prototype.do_window_mouse_scroll = function(e, window) {
}


XpraClient.prototype._poll_clipboard = function(e) {
//see if the clipboard contents have changed:
this.debug("clipboard", "poll clipboard, navigator.clipboard=", navigator.clipboard);
if (navigator.clipboard && navigator.clipboard.readText) {
var client = this;
//warning: this can take a while,
//so we may send the click before the clipboard contents...
navigator.clipboard.readText().then(function(text) {
client.debug("clipboard", "paste event, text=", text);
var clipboard_buffer = unescape(encodeURIComponent(text));
if (clipboard_buffer!=client.clipboard_buffer) {
client.debug("clipboard", "clipboard contents have changed");
client.clipboard_buffer = clipboard_buffer;
client.send_clipboard_token();
}
}, function(err) {
client.debug("clipboard", "paste event failed:", err);
});
}
else {
var datatype = "text/plain";
var clipboardData = (e.originalEvent || e).clipboardData;
//IE: must use window.clipboardData because the event clipboardData is null!
if (!clipboardData) {
clipboardData = window.clipboardData;
if (!clipboardData) {
return;
}
}
if (Utilities.isIE()) {
datatype = "Text";
}
clipboard_buffer = unescape(encodeURIComponent(clipboardData.getData(datatype)));
this.debug("clipboard", "paste event, data=", clipboard_buffer);
if (clipboard_buffer!=client.clipboard_buffer) {
this.debug("clipboard", "clipboard contents have changed");
this.clipboard_buffer = clipboard_buffer;
this.send_clipboard_token();
}
}
}


/**
* Focus
*/
Expand Down Expand Up @@ -3247,7 +3291,7 @@ XpraClient.prototype.send_clipboard_string = function(request_id, selection, cli
} else {
packet = ["clipboard-contents", request_id, selection, datatype || "UTF8_STRING", 8, "bytes", clipboard_buffer];
}
this.log("send_clipboard_string: packet=", packet);
this.debug("clipboard", "send_clipboard_string: packet=", packet);
this.send(packet);
}

Expand Down

0 comments on commit c1cbe4d

Please sign in to comment.