aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/xwayland/XWM.hpp
blob: 38fdab94cfcfe44eb008110a720409b4626844be (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
#pragma once

#include "../macros.hpp"
#include "XDataSource.hpp"
#include "Dnd.hpp"
#include "../helpers/memory/Memory.hpp"
#include "../helpers/signal/Signal.hpp"

#include <xcb/xcb.h>
#include <xcb/res.h>
#include <xcb/xfixes.h>
#include <xcb/composite.h>
#include <xcb/xcb_errors.h>

struct wl_event_source;
class CXWaylandSurfaceResource;
struct SXSelection;

struct SXTransfer {
    ~SXTransfer();

    SXSelection&                  selection;
    bool                          out = true;

    bool                          incremental   = false;
    bool                          flushOnDelete = false;
    bool                          propertySet   = false;

    int                           wlFD        = -1;
    wl_event_source*              eventSource = nullptr;

    std::vector<uint8_t>          data;

    xcb_selection_request_event_t request;

    int                           propertyStart;
    xcb_get_property_reply_t*     propertyReply;
    xcb_window_t                  incomingWindow;

    bool                          getIncomingSelectionProp(bool erase);
};

struct SXSelection {
    xcb_window_t     window    = 0;
    xcb_window_t     owner     = 0;
    xcb_timestamp_t  timestamp = 0;
    SP<CXDataSource> dataSource;

    void             onSelection();
    bool             sendData(xcb_selection_request_event_t* e, std::string mime);
    int              onRead(int fd, uint32_t mask);

    struct {
        CHyprSignalListener setSelection;
    } listeners;

    std::unique_ptr<SXTransfer> transfer;
};

class CXCBConnection {
  public:
    CXCBConnection(int fd) : connection{xcb_connect_to_fd(fd, nullptr)} {
        ;
    }

    ~CXCBConnection() {
        if (connection)
            xcb_disconnect(connection);
    }

    bool hasError() const {
        return xcb_connection_has_error(connection);
    }

    operator xcb_connection_t*() const {
        return connection;
    }

  private:
    xcb_connection_t* connection = nullptr;
};

class CXCBErrorContext {
  public:
    explicit CXCBErrorContext(xcb_connection_t* connection) {
        if (xcb_errors_context_new(connection, &errors) != 0)
            errors = nullptr;
    }

    ~CXCBErrorContext() {
        if (errors)
            xcb_errors_context_free(errors);
    }

    bool isValid() const {
        return errors != nullptr;
    }

  private:
    xcb_errors_context_t* errors = nullptr;
};

class CXWM {
  public:
    CXWM();
    ~CXWM();

    int                onEvent(int fd, uint32_t mask);
    SP<CX11DataDevice> getDataDevice();
    SP<IDataOffer>     createX11DataOffer(SP<CWLSurfaceResource> surf, SP<IDataSource> source);

  private:
    void                 setCursor(unsigned char* pixData, uint32_t stride, const Vector2D& size, const Vector2D& hotspot);

    void                 gatherResources();
    void                 getVisual();
    void                 getRenderFormat();
    void                 createWMWindow();
    void                 initSelection();

    void                 onNewSurface(SP<CWLSurfaceResource> surf);
    void                 onNewResource(SP<CXWaylandSurfaceResource> resource);

    void                 setActiveWindow(xcb_window_t window);
    void                 sendState(SP<CXWaylandSurface> surf);
    void                 focusWindow(SP<CXWaylandSurface> surf);
    void                 activateSurface(SP<CXWaylandSurface> surf, bool activate);
    bool                 isWMWindow(xcb_window_t w);
    void                 updateOverrideRedirect(SP<CXWaylandSurface> surf, bool overrideRedirect);

    void                 sendWMMessage(SP<CXWaylandSurface> surf, xcb_client_message_data_t* data, uint32_t mask);

    SP<CXWaylandSurface> windowForXID(xcb_window_t wid);
    SP<CXWaylandSurface> windowForWayland(SP<CWLSurfaceResource> surf);

    void                 readWindowData(SP<CXWaylandSurface> surf);
    void                 associate(SP<CXWaylandSurface> surf, SP<CWLSurfaceResource> wlSurf);
    void                 dissociate(SP<CXWaylandSurface> surf);

    void                 updateClientList();

    void                 sendDndEvent(SP<CWLSurfaceResource> destination, xcb_atom_t type, xcb_client_message_data_t& data);

    // event handlers
    void         handleCreate(xcb_create_notify_event_t* e);
    void         handleDestroy(xcb_destroy_notify_event_t* e);
    void         handleConfigure(xcb_configure_request_event_t* e);
    void         handleConfigureNotify(xcb_configure_notify_event_t* e);
    void         handleMapRequest(xcb_map_request_event_t* e);
    void         handleMapNotify(xcb_map_notify_event_t* e);
    void         handleUnmapNotify(xcb_unmap_notify_event_t* e);
    void         handlePropertyNotify(xcb_property_notify_event_t* e);
    void         handleClientMessage(xcb_client_message_event_t* e);
    void         handleFocusIn(xcb_focus_in_event_t* e);
    void         handleFocusOut(xcb_focus_out_event_t* e);
    void         handleError(xcb_value_error_t* e);

    bool         handleSelectionEvent(xcb_generic_event_t* e);
    void         handleSelectionNotify(xcb_selection_notify_event_t* e);
    bool         handleSelectionPropertyNotify(xcb_property_notify_event_t* e);
    void         handleSelectionRequest(xcb_selection_request_event_t* e);
    bool         handleSelectionXFixesNotify(xcb_xfixes_selection_notify_event_t* e);

    void         selectionSendNotify(xcb_selection_request_event_t* e, bool success);
    xcb_atom_t   mimeToAtom(const std::string& mime);
    std::string  mimeFromAtom(xcb_atom_t atom);
    void         setClipboardToWayland(SXSelection& sel);
    void         getTransferData(SXSelection& sel);
    std::string  getAtomName(uint32_t atom);
    void         readProp(SP<CXWaylandSurface> XSURF, uint32_t atom, xcb_get_property_reply_t* reply);

    SXSelection* getSelection(xcb_atom_t atom);

    //
    CXCBConnection                            connection;
    xcb_errors_context_t*                     errors = nullptr;
    xcb_screen_t*                             screen = nullptr;

    xcb_window_t                              wmWindow;

    wl_event_source*                          eventSource = nullptr;

    const xcb_query_extension_reply_t*        xfixes      = nullptr;
    const xcb_query_extension_reply_t*        xres        = nullptr;
    int                                       xfixesMajor = 0;

    xcb_visualid_t                            visual_id;
    xcb_colormap_t                            colormap;
    uint32_t                                  cursorXID = 0;

    xcb_render_pictformat_t                   render_format_id;

    std::vector<WP<CXWaylandSurfaceResource>> shellResources;
    std::vector<SP<CXWaylandSurface>>         surfaces;
    std::vector<WP<CXWaylandSurface>>         mappedSurfaces;         // ordered by map time
    std::vector<WP<CXWaylandSurface>>         mappedSurfacesStacking; // ordered by stacking

    WP<CXWaylandSurface>                      focusedSurface;
    uint64_t                                  lastFocusSeq = 0;

    SXSelection                               clipboard;
    SXSelection                               dndSelection;
    SP<CX11DataDevice>                        dndDataDevice = makeShared<CX11DataDevice>();
    std::vector<SP<CX11DataOffer>>            dndDataOffers;

    struct {
        CHyprSignalListener newWLSurface;
        CHyprSignalListener newXShellSurface;
    } listeners;

    friend class CXWaylandSurface;
    friend class CXWayland;
    friend class CXDataSource;
    friend class CX11DataDevice;
    friend class CX11DataSource;
    friend class CX11DataOffer;
    friend class CWLDataDeviceProtocol;
    friend struct SXSelection;
    friend struct SXTransfer;
};