-
Notifications
You must be signed in to change notification settings - Fork 970
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clipboard support to x0vncserver
- Loading branch information
Showing
8 changed files
with
258 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* Copyright (C) 2024 Gaurav Ujjwal. All Rights Reserved. | ||
* | ||
* This is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this software; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | ||
* USA. | ||
*/ | ||
|
||
#include "XSelection.h" | ||
#include <X11/Xatom.h> | ||
#include <rfb/LogWriter.h> | ||
#include <rfb/util.h> | ||
|
||
static rfb::LogWriter vlog("XSelection"); | ||
|
||
XSelection::XSelection(Display* dpy_, XSelectionHandler* handler_) | ||
: TXWindow(dpy_, 1, 1, nullptr), handler(handler_) | ||
{ | ||
xaTransferProperty = XInternAtom(dpy, "SelectionTransferProperty", False); | ||
xaTimestampProperty = XInternAtom(dpy, "TimestampAccessProperty", False); | ||
addEventMask(PropertyChangeMask); // Required for PropertyNotify events | ||
} | ||
|
||
static Bool PropertyEventMatcher(Display* /* dpy */, XEvent* ev, XPointer prop) | ||
{ | ||
if (ev->type == PropertyNotify && ev->xproperty.atom == *((Atom*)prop)) | ||
return True; | ||
else | ||
return False; | ||
} | ||
|
||
Time XSelection::getXServerTime() | ||
{ | ||
XEvent ev; | ||
uint8_t data = 0; | ||
|
||
// Trigger a PropertyNotify event to extract server time | ||
XChangeProperty(dpy, win(), xaTimestampProperty, XA_STRING, 8, | ||
PropModeReplace, &data, sizeof(data)); | ||
XIfEvent(dpy, &ev, &PropertyEventMatcher, (XPointer)&xaTimestampProperty); | ||
return ev.xproperty.time; | ||
} | ||
|
||
// Takes ownership on CLIPBOARD selection, backed by given data. | ||
// data should point to a UTF-8 string. | ||
void XSelection::setClipboardData(const char* data) | ||
{ | ||
Time time = getXServerTime(); | ||
ownSelection(xaCLIPBOARD, time); | ||
|
||
if (selectionOwner(xaCLIPBOARD)) | ||
clipData = data; | ||
else | ||
vlog.error("Unable to own CLIPBOARD selection"); | ||
} | ||
|
||
// We own the selection and another X client has asked for data | ||
bool XSelection::selectionRequest(Window requestor, Atom selection, Atom target, | ||
Atom property) | ||
{ | ||
if (clipData.empty() || selection != xaCLIPBOARD) | ||
return false; | ||
|
||
if (target == XA_STRING) { | ||
std::string latin1 = rfb::utf8ToLatin1(clipData.data(), clipData.length()); | ||
XChangeProperty(dpy, requestor, property, XA_STRING, 8, PropModeReplace, | ||
(unsigned char*)latin1.data(), latin1.length()); | ||
return true; | ||
} | ||
|
||
if (target == xaUTF8_STRING) { | ||
XChangeProperty(dpy, requestor, property, xaUTF8_STRING, 8, PropModeReplace, | ||
(unsigned char*)clipData.data(), clipData.length()); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// Selection-owner change event implies a change in selection data | ||
void XSelection::handleSelectionOwnerChange(Window owner, Atom selection, | ||
Time time) | ||
{ | ||
if (owner == win() || selection != xaCLIPBOARD) | ||
return; | ||
|
||
XConvertSelection(dpy, selection, xaTARGETS, xaTransferProperty, win(), time); | ||
} | ||
|
||
// Some information about selection is received from owner | ||
void XSelection::selectionNotify(XSelectionEvent* ev, Atom type, int format, | ||
int nitems, void* data) | ||
{ | ||
if (!ev || !data || type == None) { | ||
handler->handleXClipboardAvailable(false); | ||
return; | ||
} | ||
|
||
if (ev->target == xaTARGETS) { | ||
if (format != 32 || type != XA_ATOM) | ||
return; | ||
|
||
Atom* targets = (Atom*)data; | ||
bool utf8Supported = false; | ||
bool stringSupported = false; | ||
|
||
for (int i = 0; i < nitems; i++) { | ||
if (targets[i] == xaUTF8_STRING) | ||
utf8Supported = true; | ||
else if (targets[i] == XA_STRING) | ||
stringSupported = true; | ||
} | ||
|
||
// Prefer UTF-8 if available | ||
if (utf8Supported) | ||
XConvertSelection(dpy, ev->selection, xaUTF8_STRING, xaTransferProperty, | ||
win(), ev->time); | ||
else if (stringSupported) | ||
XConvertSelection(dpy, ev->selection, XA_STRING, xaTransferProperty, | ||
win(), ev->time); | ||
} else if (ev->target == xaUTF8_STRING || ev->target == XA_STRING) { | ||
if (format != 8) | ||
return; | ||
|
||
if (type == xaUTF8_STRING) { | ||
clipData = rfb::convertLF((char*)data, nitems); | ||
handler->handleXClipboardAvailable(true); | ||
} else if (type == XA_STRING) { | ||
std::string filtered = rfb::convertLF((char*)data, nitems); | ||
clipData = rfb::latin1ToUTF8(filtered.data(), filtered.length()); | ||
handler->handleXClipboardAvailable(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* Copyright (C) 2024 Gaurav Ujjwal. All Rights Reserved. | ||
* | ||
* This is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this software; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | ||
* USA. | ||
*/ | ||
|
||
#ifndef __XSELECTION_H__ | ||
#define __XSELECTION_H__ | ||
|
||
#include <string> | ||
#include <tx/TXWindow.h> | ||
|
||
class XSelectionHandler | ||
{ | ||
public: | ||
virtual void handleXClipboardAvailable(bool available) = 0; | ||
}; | ||
|
||
|
||
class XSelection : TXWindow | ||
{ | ||
public: | ||
XSelection(Display* dpy_, XSelectionHandler* handler_); | ||
|
||
std::string& getClipboardData() { return clipData; } | ||
void setClipboardData(const char* data); | ||
void handleSelectionOwnerChange(Window owner, Atom selection, Time time); | ||
|
||
private: | ||
std::string clipData; // Always in UTF-8 | ||
Atom xaTransferProperty; | ||
Atom xaTimestampProperty; | ||
XSelectionHandler* handler; | ||
|
||
Time getXServerTime(); | ||
void selectionNotify(XSelectionEvent* ev, Atom type, int format, int nitems, | ||
void* data) override; | ||
bool selectionRequest(Window requestor, Atom selection, Atom target, | ||
Atom property) override; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters