aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/xwayland/XDataSource.cpp
blob: 54b23937dc06620a33849673f296316365e0114c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef NO_XWAYLAND

#include "XWayland.hpp"
#include "../defines.hpp"
#include "XDataSource.hpp"

#include <fcntl.h>

CXDataSource::CXDataSource(SXSelection& sel_) : selection(sel_) {
    xcb_get_property_cookie_t cookie = xcb_get_property(g_pXWayland->pWM->connection,
                                                        1, // delete
                                                        selection.window, HYPRATOMS["_WL_SELECTION"], XCB_GET_PROPERTY_TYPE_ANY, 0, 4096);

    xcb_get_property_reply_t* reply = xcb_get_property_reply(g_pXWayland->pWM->connection, cookie, nullptr);
    if (!reply)
        return;

    if (reply->type != XCB_ATOM_ATOM) {
        free(reply);
        return;
    }

    auto value = (xcb_atom_t*)xcb_get_property_value(reply);
    for (uint32_t i = 0; i < reply->value_len; i++) {
        if (value[i] == HYPRATOMS["UTF8_STRING"])
            mimeTypes.emplace_back("text/plain;charset=utf-8");
        else if (value[i] == HYPRATOMS["TEXT"])
            mimeTypes.emplace_back("text/plain");
        else if (value[i] != HYPRATOMS["TARGETS"] && value[i] != HYPRATOMS["TIMESTAMP"]) {

            auto type = g_pXWayland->pWM->mimeFromAtom(value[i]);

            if (type == "INVALID")
                continue;

            mimeTypes.push_back(type);
        }

        mimeAtoms.push_back(value[i]);
    }

    free(reply);
}

std::vector<std::string> CXDataSource::mimes() {
    return mimeTypes;
}

void CXDataSource::send(const std::string& mime, uint32_t fd) {
    xcb_atom_t mimeAtom = 0;

    if (mime == "text/plain")
        mimeAtom = HYPRATOMS["TEXT"];
    else if (mime == "text/plain;charset=utf-8")
        mimeAtom = HYPRATOMS["UTF8_STRING"];
    else {
        for (size_t i = 0; i < mimeTypes.size(); ++i) {
            if (mimeTypes.at(i) == mime) {
                mimeAtom = mimeAtoms.at(i);
                break;
            }
        }
    }

    if (!mimeAtom) {
        Debug::log(ERR, "[XDataSource] mime atom not found");
        close(fd);
        return;
    }

    Debug::log(LOG, "[XDataSource] send with mime {} to fd {}", mime, fd);

    selection.transfer                 = std::make_unique<SXTransfer>(selection);
    selection.transfer->incomingWindow = xcb_generate_id(g_pXWayland->pWM->connection);
    const uint32_t MASK                = XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE;
    xcb_create_window(g_pXWayland->pWM->connection, XCB_COPY_FROM_PARENT, selection.transfer->incomingWindow, g_pXWayland->pWM->screen->root, 0, 0, 10, 10, 0,
                      XCB_WINDOW_CLASS_INPUT_OUTPUT, g_pXWayland->pWM->screen->root_visual, XCB_CW_EVENT_MASK, &MASK);

    xcb_convert_selection(g_pXWayland->pWM->connection, selection.transfer->incomingWindow, HYPRATOMS["CLIPBOARD"], mimeAtom, HYPRATOMS["_WL_SELECTION"], XCB_TIME_CURRENT_TIME);

    xcb_flush(g_pXWayland->pWM->connection);

    fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
    selection.transfer->wlFD = fd;
}

void CXDataSource::accepted(const std::string& mime) {
    Debug::log(LOG, "[XDataSource] accepted is a stub");
}

void CXDataSource::cancelled() {
    Debug::log(LOG, "[XDataSource] cancelled is a stub");
}

void CXDataSource::error(uint32_t code, const std::string& msg) {
    Debug::log(LOG, "[XDataSource] error is a stub: err {}: {}", code, msg);
}

eDataSourceType CXDataSource::type() {
    return DATA_SOURCE_TYPE_X11;
}

#endif