• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ClipboardX11.h"
17 #include <iostream>
18 #include <cstring>
19 #include <X11/Xlib.h>
20 using namespace std;
21 
SetClipboardData(const string & str)22 void ClipboardX11::SetClipboardData(const string& str)
23 {
24     display = XOpenDisplay(0);
25     int ret = DefaultScreen(display);
26     window = XCreateSimpleWindow(display, RootWindow(display, ret), 0, 0, 1, 1, 0,
27         BlackPixel(display, ret), WhitePixel(display, ret));
28     targets_atom = XInternAtom(display, "TARGETS", 0);
29     text_atom = XInternAtom(display, "TEXT", 0);
30     UTF8 = XInternAtom(display, "UTF8_STRING", 1);
31     if (UTF8 == None) UTF8 = XA_STRING;
32     Atom selection = XInternAtom(display, "CLIPBOARD", 0);
33     SetCopyData(selection, str, strlen(str.c_str()));
34 }
35 
GetClipboardData()36 const string ClipboardX11::GetClipboardData()
37 {
38     display = XOpenDisplay(0);
39     int ret = DefaultScreen(display);
40     window = XCreateSimpleWindow(display, RootWindow(display, ret), 0, 0, 1, 1, 0,
41                                  BlackPixel(display, ret), WhitePixel(display, ret));
42     string retStr; // NOLINT
43     UTF8 = XInternAtom(display, "UTF8_STRING", True);
44     if (UTF8 != None) retStr = GetPasteType(UTF8);
45     if (retStr.empty()) retStr = GetPasteType(XA_STRING);
46     return retStr;
47 }
48 
SetCopyData(const Atom & selection,const string & text,const int size)49 void ClipboardX11::SetCopyData(const Atom& selection, const string& text, const int size)
50 {
51     XEvent event;
52     XSetSelectionOwner (display, selection, window, 0);
53     if (XGetSelectionOwner (display, selection) != window) {
54         return;
55     }
56     while (1) {
57         XNextEvent (display, &event);
58         if (event.type == SelectionRequest) {
59             if (event.xselectionrequest.selection != selection) {
60                 break;
61             }
62             XSelectionRequestEvent* xsr = &event.xselectionrequest;
63             XSelectionEvent ev = {0};
64             int ret = 0;
65             ev.type = SelectionNotify, ev.display = xsr->display, ev.requestor = xsr->requestor,
66             ev.selection = xsr->selection, ev.time = xsr->time, ev.target = xsr->target,
67             ev.property = xsr->property;
68             if (ev.target == targets_atom) {
69                 ret = XChangeProperty(ev.display, ev.requestor, ev.property,
70                                       XA_ATOM, nFormateProp32, PropModeReplace,
71                                       (unsigned char*)&UTF8, 1);
72             } else if (ev.target == XA_STRING || ev.target == text_atom) {
73                 ret = XChangeProperty(ev.display, ev.requestor, ev.property, XA_STRING,
74                                       nFormateProp8, PropModeReplace, (unsigned char*)text.c_str(), size);
75             } else if (ev.target == UTF8) {
76                 ret = XChangeProperty(ev.display, ev.requestor, ev.property, UTF8,
77                                       nFormateProp8, PropModeReplace, (unsigned char*)text.c_str(), size);
78             } else {
79                 ev.property = None;
80             }
81             if ((ret & nEvenNumber) == 0) {
82                 XSendEvent (display, ev.requestor, 0, 0, (XEvent *)&ev);
83             }
84         }
85     }
86 }
87 
GetPasteType(const Atom & atom)88 string ClipboardX11::GetPasteType(const Atom& atom)
89 {
90     XEvent event;
91     int format;
92     unsigned long num, size;
93     char* data = nullptr;
94     string retStr;
95     Atom target, CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0), XSEL_DATA = XInternAtom(display, "XSEL_DATA", 0);
96     XConvertSelection(display, CLIPBOARD, atom, XSEL_DATA, window, CurrentTime);
97     XSync(display, 0);
98     XNextEvent(display, &event);
99     if (event.type == SelectionNotify) {
100         if (event.xselection.selection != CLIPBOARD) {
101             return retStr;
102         }
103         if (event.xselection.property) {
104             XGetWindowProperty(event.xselection.display, event.xselection.requestor, event.xselection.property, 0L,
105                                (~0L), 0, AnyPropertyType, &target, &format, &size, &num, (unsigned char**)&data);
106             if (target == UTF8 || target == XA_STRING) {
107                 string str(data);
108                 retStr = str;
109             }
110             XFree(data);
111             XDeleteProperty(event.xselection.display, event.xselection.requestor, event.xselection.property);
112         }
113     }
114     return retStr;
115 }