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
21void ClipboardX11::SetClipboardData(const std::string& str)
22{
23    display = XOpenDisplay(0);
24    int ret = DefaultScreen(display);
25    window = XCreateSimpleWindow(display, RootWindow(display, ret), 0, 0, 1, 1, 0,
26        BlackPixel(display, ret), WhitePixel(display, ret));
27    targets_atom = XInternAtom(display, "TARGETS", 0);
28    text_atom = XInternAtom(display, "TEXT", 0);
29    UTF8 = XInternAtom(display, "UTF8_STRING", 1);
30    if (UTF8 == None) UTF8 = XA_STRING;
31    Atom selection = XInternAtom(display, "CLIPBOARD", 0);
32    SetCopyData(selection, str, strlen(str.c_str()));
33}
34
35const std::string ClipboardX11::GetClipboardData()
36{
37    display = XOpenDisplay(0);
38    int ret = DefaultScreen(display);
39    window = XCreateSimpleWindow(display, RootWindow(display, ret), 0, 0, 1, 1, 0,
40                                 BlackPixel(display, ret), WhitePixel(display, ret));
41    std::string retStr; // NOLINT
42    UTF8 = XInternAtom(display, "UTF8_STRING", True);
43    if (UTF8 != None) retStr = GetPasteType(UTF8);
44    if (retStr.empty()) retStr = GetPasteType(XA_STRING);
45    return retStr;
46}
47
48void ClipboardX11::SetCopyData(const Atom& selection, const std::string& text, const int size)
49{
50    XEvent event;
51    XSetSelectionOwner (display, selection, window, 0);
52    if (XGetSelectionOwner (display, selection) != window) {
53        return;
54    }
55    while (1) {
56        XNextEvent (display, &event);
57        if (event.type == SelectionRequest) {
58            if (event.xselectionrequest.selection != selection) {
59                break;
60            }
61            XSelectionRequestEvent* xsr = &event.xselectionrequest;
62            XSelectionEvent ev = {0};
63            int ret = 0;
64            ev.type = SelectionNotify, ev.display = xsr->display, ev.requestor = xsr->requestor,
65            ev.selection = xsr->selection, ev.time = xsr->time, ev.target = xsr->target,
66            ev.property = xsr->property;
67            if (ev.target == targets_atom) {
68                ret = XChangeProperty(ev.display, ev.requestor, ev.property,
69                                      XA_ATOM, nFormateProp32, PropModeReplace,
70                                      (unsigned char*)&UTF8, 1);
71            } else if (ev.target == XA_STRING || ev.target == text_atom) {
72                ret = XChangeProperty(ev.display, ev.requestor, ev.property, XA_STRING,
73                                      nFormateProp8, PropModeReplace, (unsigned char*)text.c_str(), size);
74            } else if (ev.target == UTF8) {
75                ret = XChangeProperty(ev.display, ev.requestor, ev.property, UTF8,
76                                      nFormateProp8, PropModeReplace, (unsigned char*)text.c_str(), size);
77            } else {
78                ev.property = None;
79            }
80            if ((ret & nEvenNumber) == 0) {
81                XSendEvent (display, ev.requestor, 0, 0, (XEvent *)&ev);
82            }
83        }
84    }
85}
86
87std::string ClipboardX11::GetPasteType(const Atom& atom)
88{
89    XEvent event;
90    int format;
91    unsigned long num, size;
92    char* data = nullptr;
93    std::string retStr;
94    Atom target, CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0), XSEL_DATA = XInternAtom(display, "XSEL_DATA", 0);
95    XConvertSelection(display, CLIPBOARD, atom, XSEL_DATA, window, CurrentTime);
96    XSync(display, 0);
97    XNextEvent(display, &event);
98    if (event.type == SelectionNotify) {
99        if (event.xselection.selection != CLIPBOARD) {
100            return retStr;
101        }
102        if (event.xselection.property) {
103            XGetWindowProperty(event.xselection.display, event.xselection.requestor, event.xselection.property, 0L,
104                               (~0L), 0, AnyPropertyType, &target, &format, &size, &num, (unsigned char**)&data);
105            if (target == UTF8 || target == XA_STRING) {
106                std::string str(data);
107                retStr = str;
108            }
109            XFree(data);
110            XDeleteProperty(event.xselection.display, event.xselection.requestor, event.xselection.property);
111        }
112    }
113    return retStr;
114}