summaryrefslogtreecommitdiff
path: root/src/xwayland/Dnd.cpp
blob: 963a830e2a9c23b67904c006a1d78cdb03f7eca8 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "Dnd.hpp"
#ifndef NO_XWAYLAND
#include "XWM.hpp"
#include "XWayland.hpp"
#include "Server.hpp"
#endif
#include "../managers/XWaylandManager.hpp"
#include "../desktop/view/WLSurface.hpp"
#include "../protocols/core/Compositor.hpp"

using namespace Hyprutils::OS;

#define PROPERTY_FORMAT_32BIT 32
#define PROPERTY_LENGTH       1
#define PROPERTY_OFFSET       0

#ifndef NO_XWAYLAND
static xcb_atom_t dndActionToAtom(uint32_t actions) {
    if (actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY)
        return HYPRATOMS["XdndActionCopy"];
    else if (actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE)
        return HYPRATOMS["XdndActionMove"];
    else if (actions & WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)
        return HYPRATOMS["XdndActionAsk"];

    return XCB_ATOM_NONE;
}

void CX11DataDevice::sendDndEvent(xcb_window_t targetWindow, xcb_atom_t type, xcb_client_message_data_t& data) {
    xcb_client_message_event_t event = {
        .response_type = XCB_CLIENT_MESSAGE,
        .format        = 32,
        .sequence      = 0,
        .window        = targetWindow,
        .type          = type,
        .data          = data,
    };
    xcb_connection_t* conn = (g_pXWayland->m_wm->getConnection());
    xcb_send_event(conn, 0, targetWindow, XCB_EVENT_MASK_NO_EVENT, rc<const char*>(&event));
    xcb_flush(conn);
}

xcb_window_t CX11DataDevice::getProxyWindow(xcb_window_t window) {
    xcb_window_t              targetWindow = window;
    xcb_get_property_cookie_t proxyCookie =
        xcb_get_property((g_pXWayland->m_wm->getConnection()), PROPERTY_OFFSET, window, HYPRATOMS["XdndProxy"], XCB_ATOM_WINDOW, PROPERTY_OFFSET, PROPERTY_LENGTH);
    xcb_get_property_reply_t* proxyReply = xcb_get_property_reply(g_pXWayland->m_wm->getConnection(), proxyCookie, nullptr);

    const auto                isValidPropertyReply = [](xcb_get_property_reply_t* reply) {
        return reply && reply->type == XCB_ATOM_WINDOW && reply->format == PROPERTY_FORMAT_32BIT && xcb_get_property_value_length(reply) == sizeof(xcb_window_t);
    };

    if (isValidPropertyReply(proxyReply)) {
        xcb_window_t              proxyWindow = *sc<xcb_window_t*>(xcb_get_property_value(proxyReply));

        xcb_get_property_cookie_t proxyVerifyCookie =
            xcb_get_property(g_pXWayland->m_wm->getConnection(), PROPERTY_OFFSET, proxyWindow, HYPRATOMS["XdndProxy"], XCB_ATOM_WINDOW, PROPERTY_OFFSET, PROPERTY_LENGTH);
        xcb_get_property_reply_t* proxyVerifyReply = xcb_get_property_reply(g_pXWayland->m_wm->getConnection(), proxyVerifyCookie, nullptr);

        if (isValidPropertyReply(proxyVerifyReply)) {
            xcb_window_t verifyWindow = *sc<xcb_window_t*>(xcb_get_property_value(proxyVerifyReply));
            if (verifyWindow == proxyWindow) {
                targetWindow = proxyWindow;
                Log::logger->log(Log::DEBUG, "Using XdndProxy window {:x} for window {:x}", proxyWindow, window);
            }
        }
        free(proxyVerifyReply); // NOLINT(cppcoreguidelines-no-malloc)
    }
    free(proxyReply); // NOLINT(cppcoreguidelines-no-malloc)

    return targetWindow;
}
#endif

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

SP<CWLDataOfferResource> CX11DataOffer::getWayland() {
    return nullptr;
}

SP<CX11DataOffer> CX11DataOffer::getX11() {
    return m_self.lock();
}

SP<IDataSource> CX11DataOffer::getSource() {
    return m_source.lock();
}

void CX11DataOffer::markDead() {
#ifndef NO_XWAYLAND
    std::erase(g_pXWayland->m_wm->m_dndDataOffers, m_self);
#endif
}

void CX11DataDevice::sendDataOffer(SP<IDataOffer> offer) {
    ; // no-op, I don't think this has an X equiv
}

void CX11DataDevice::sendEnter(uint32_t serial, SP<CWLSurfaceResource> surf, const Vector2D& local, SP<IDataOffer> offer) {
#ifndef NO_XWAYLAND
    auto XSURF = g_pXWayland->m_wm->windowForWayland(surf);

    if (!XSURF) {
        Log::logger->log(Log::ERR, "CX11DataDevice::sendEnter: No xwayland surface for destination");
        return;
    }

    auto SOURCE = offer->getSource();

    if (!SOURCE) {
        Log::logger->log(Log::ERR, "CX11DataDevice::sendEnter: No source");
        return;
    }

    std::vector<xcb_atom_t> targets;
    // reserve to avoid reallocations
    targets.reserve(SOURCE->mimes().size());
    for (auto const& m : SOURCE->mimes()) {
        targets.push_back(g_pXWayland->m_wm->mimeToAtom(m));
    }

    xcb_change_property(g_pXWayland->m_wm->getConnection(), XCB_PROP_MODE_REPLACE, g_pXWayland->m_wm->m_dndSelection.window, HYPRATOMS["XdndTypeList"], XCB_ATOM_ATOM, 32,
                        targets.size(), targets.data());

    xcb_set_selection_owner(g_pXWayland->m_wm->getConnection(), g_pXWayland->m_wm->m_dndSelection.window, HYPRATOMS["XdndSelection"], XCB_TIME_CURRENT_TIME);
    xcb_flush(g_pXWayland->m_wm->getConnection());

    xcb_window_t              targetWindow = getProxyWindow(XSURF->m_xID);

    xcb_client_message_data_t data = {{0}};
    data.data32[0]                 = g_pXWayland->m_wm->m_dndSelection.window;
    data.data32[1]                 = XDND_VERSION << 24;
    data.data32[1] |= 1;

    sendDndEvent(targetWindow, HYPRATOMS["XdndEnter"], data);

    m_lastSurface = XSURF;
    m_lastOffer   = offer;

    auto hlSurface = XSURF->m_surface.lock();
    if (!hlSurface) {
        Log::logger->log(Log::ERR, "CX11DataDevice::sendEnter: Non desktop x surface?!");
        m_lastSurfaceCoords = {};
        return;
    }

    m_lastSurfaceCoords = g_pXWaylandManager->xwaylandToWaylandCoords(XSURF->m_geometry.pos());
#endif
}

void CX11DataDevice::cleanupState() {
    m_lastSurface.reset();
    m_lastOffer.reset();
    m_lastSurfaceCoords = {};
    m_lastTime          = 0;
}

void CX11DataDevice::sendLeave() {
#ifndef NO_XWAYLAND
    if (!m_lastSurface)
        return;

    xcb_window_t              targetWindow = getProxyWindow(m_lastSurface->m_xID);

    xcb_client_message_data_t data = {{0}};
    data.data32[0]                 = g_pXWayland->m_wm->m_dndSelection.window;

    sendDndEvent(targetWindow, HYPRATOMS["XdndLeave"], data);

    cleanupState();
#endif
}

void CX11DataDevice::sendMotion(uint32_t timeMs, const Vector2D& local) {
#ifndef NO_XWAYLAND
    if (!m_lastSurface || !m_lastOffer || !m_lastOffer->getSource())
        return;

    xcb_window_t              targetWindow = getProxyWindow(m_lastSurface->m_xID);

    const auto                XCOORDS = g_pXWaylandManager->waylandToXWaylandCoords(m_lastSurfaceCoords + local);
    const uint32_t            coords  = (sc<uint32_t>(XCOORDS.x) << 16) | sc<uint32_t>(XCOORDS.y);

    xcb_client_message_data_t data = {{0}};
    data.data32[0]                 = g_pXWayland->m_wm->m_dndSelection.window;
    data.data32[2]                 = coords;
    data.data32[3]                 = timeMs;
    data.data32[4]                 = dndActionToAtom(m_lastOffer->getSource()->actions());

    sendDndEvent(targetWindow, HYPRATOMS["XdndPosition"], data);

    m_lastTime = timeMs;
#endif
}

void CX11DataDevice::sendDrop() {
#ifndef NO_XWAYLAND
    if (!m_lastSurface || !m_lastOffer) {
        Log::logger->log(Log::ERR, "CX11DataDevice::sendDrop: No surface or offer");
        return;
    }

    xcb_window_t              targetWindow = getProxyWindow(m_lastSurface->m_xID);

    xcb_client_message_data_t data = {{0}};
    data.data32[0]                 = g_pXWayland->m_wm->m_dndSelection.window;
    data.data32[2]                 = m_lastTime;

    sendDndEvent(targetWindow, HYPRATOMS["XdndDrop"], data);

    cleanupState();
#endif
}

void CX11DataDevice::sendSelection(SP<IDataOffer> offer) {
    ; // no-op. Selection is done separately.
}

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

SP<CWLDataDeviceResource> CX11DataDevice::getWayland() {
    return nullptr;
}

SP<CX11DataDevice> CX11DataDevice::getX11() {
    return m_self.lock();
}

std::vector<std::string> CX11DataSource::mimes() {
    return m_mimeTypes;
}

void CX11DataSource::send(const std::string& mime, CFileDescriptor fd) {
    ; // no-op
}

void CX11DataSource::accepted(const std::string& mime) {
    ; // no-op
}

void CX11DataSource::cancelled() {
    m_dndSuccess = false;
    m_dropped    = false;
}

bool CX11DataSource::hasDnd() {
    return m_dnd;
}

bool CX11DataSource::dndDone() {
    return m_dropped;
}

void CX11DataSource::error(uint32_t code, const std::string& msg) {
    Log::logger->log(Log::ERR, "CX11DataSource error: code {} msg {}", code, msg);
    m_dndSuccess = false;
    m_dropped    = false;
}

void CX11DataSource::sendDndFinished() {
    m_dndSuccess = true;
}

uint32_t CX11DataSource::actions() {
    return m_supportedActions;
}

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

void CX11DataSource::sendDndDropPerformed() {
    m_dropped = true;
}

void CX11DataSource::sendDndAction(wl_data_device_manager_dnd_action a) {
    ; // no-op
}

void CX11DataDevice::forceCleanupDnd() {
#ifndef NO_XWAYLAND
    if (m_lastOffer) {
        auto source = m_lastOffer->getSource();
        if (source) {
            source->sendDndFinished();
            source->cancelled();
        }
    }

    xcb_set_selection_owner(g_pXWayland->m_wm->getConnection(), XCB_ATOM_NONE, HYPRATOMS["XdndSelection"], XCB_TIME_CURRENT_TIME);
    xcb_flush(g_pXWayland->m_wm->getConnection());

    cleanupState();

    g_pSeatManager->setPointerFocus(nullptr, {});
    g_pInputManager->simulateMouseMovement();
#endif
}