Changeset View
Changeset View
Standalone View
Standalone View
xwl/transfer.h
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright 2018 Roman Gilg <subdiff@gmail.com> | ||||
6 | | ||||
7 | This program is free software; you can redistribute it and/or modify | ||||
8 | it under the terms of the GNU General Public License as published by | ||||
9 | the Free Software Foundation; either version 2 of the License, or | ||||
10 | (at your option) any later version. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | #ifndef KWIN_XWL_TRANSFER | ||||
21 | #define KWIN_XWL_TRANSFER | ||||
22 | | ||||
23 | #include <QObject> | ||||
24 | #include <QSocketNotifier> | ||||
25 | #include <QVector> | ||||
26 | | ||||
27 | #include <xcb/xcb.h> | ||||
28 | | ||||
29 | namespace KWayland { | ||||
30 | namespace Client { | ||||
31 | class DataDevice; | ||||
32 | class DataSource; | ||||
33 | } | ||||
34 | namespace Server { | ||||
35 | class DataDeviceInterface; | ||||
36 | } | ||||
37 | } | ||||
38 | | ||||
39 | namespace KWin | ||||
40 | { | ||||
41 | namespace Xwl | ||||
42 | { | ||||
43 | | ||||
44 | /* | ||||
45 | * Represents for an arbitrary selection a data transfer between | ||||
46 | * sender and receiver. | ||||
47 | * | ||||
48 | * Lives for the duration of the transfer and must be cleaned up | ||||
49 | * externally afterwards. For that the owner should connect to the | ||||
50 | * @c finished() signal. | ||||
51 | */ | ||||
52 | class Transfer : public QObject | ||||
53 | { | ||||
54 | Q_OBJECT | ||||
55 | public: | ||||
56 | Transfer(xcb_atom_t selection, | ||||
57 | qint32 fd, | ||||
58 | xcb_timestamp_t timestamp, | ||||
59 | QObject *parent = nullptr); | ||||
60 | | ||||
61 | virtual bool handlePropNotify(xcb_property_notify_event_t *event) = 0; | ||||
62 | void timeout(); | ||||
63 | xcb_timestamp_t timestamp() const { | ||||
64 | return m_timestamp; | ||||
65 | } | ||||
66 | | ||||
67 | Q_SIGNALS: | ||||
68 | void finished(); | ||||
69 | | ||||
70 | protected: | ||||
71 | void endTransfer(); | ||||
72 | | ||||
73 | xcb_atom_t atom() const { | ||||
74 | return m_atom; | ||||
75 | } | ||||
76 | qint32 fd() const { | ||||
77 | return m_fd; | ||||
78 | } | ||||
79 | | ||||
80 | void setIncr(bool set) { | ||||
81 | m_incr = set; | ||||
82 | } | ||||
83 | bool incr() const { | ||||
84 | return m_incr; | ||||
85 | } | ||||
86 | void resetTimeout() { | ||||
87 | m_timeout = false; | ||||
88 | } | ||||
89 | void createSocketNotifier(QSocketNotifier::Type type); | ||||
90 | void clearSocketNotifier(); | ||||
91 | QSocketNotifier* socketNotifier() const { | ||||
92 | return m_sn; | ||||
93 | } | ||||
94 | private: | ||||
95 | void closeFd(); | ||||
96 | | ||||
97 | xcb_atom_t m_atom; | ||||
98 | qint32 m_fd; | ||||
99 | xcb_timestamp_t m_timestamp = XCB_CURRENT_TIME; | ||||
100 | | ||||
101 | QSocketNotifier *m_sn = nullptr; | ||||
102 | bool m_incr = false; | ||||
103 | bool m_timeout = false; | ||||
104 | }; | ||||
105 | | ||||
106 | /* | ||||
107 | * Represents a transfer from a Wayland native source to an X window. | ||||
108 | */ | ||||
109 | class TransferWltoX : public Transfer | ||||
110 | { | ||||
111 | Q_OBJECT | ||||
112 | public: | ||||
113 | TransferWltoX(xcb_atom_t selection, | ||||
114 | xcb_selection_request_event_t *request, | ||||
115 | qint32 fd, | ||||
116 | QObject *parent = nullptr); | ||||
117 | ~TransferWltoX(); | ||||
118 | | ||||
119 | void startTransferFromSource(); | ||||
120 | bool handlePropNotify(xcb_property_notify_event_t *event) override; | ||||
121 | | ||||
122 | Q_SIGNALS: | ||||
123 | void selNotify(xcb_selection_request_event_t *event, bool success); | ||||
124 | | ||||
125 | private: | ||||
126 | void startIncr(); | ||||
127 | void readWlSource(); | ||||
128 | int flushSourceData(); | ||||
129 | void handlePropDelete(); | ||||
130 | | ||||
131 | xcb_selection_request_event_t *m_request = nullptr; | ||||
132 | | ||||
133 | /* contains all received data portioned in chunks | ||||
134 | * TODO: explain second QPair component | ||||
135 | */ | ||||
136 | QVector<QPair<QByteArray, int> > chunks; | ||||
137 | | ||||
138 | bool propertyIsSet = false; | ||||
139 | bool flushPropOnDelete = false; | ||||
140 | }; | ||||
141 | | ||||
142 | /* | ||||
143 | * Helper class for X to Wl transfers | ||||
144 | */ | ||||
145 | class DataReceiver | ||||
146 | { | ||||
147 | public: | ||||
148 | virtual ~DataReceiver(); | ||||
149 | | ||||
150 | void transferFromProperty(xcb_get_property_reply_t *reply); | ||||
151 | | ||||
152 | | ||||
153 | virtual void setData(char *value, int length); | ||||
154 | QByteArray data() const; | ||||
155 | | ||||
156 | void partRead(int length); | ||||
157 | | ||||
158 | protected: | ||||
159 | void setDataInternal(QByteArray data) { | ||||
160 | m_data = data; | ||||
161 | } | ||||
162 | | ||||
163 | private: | ||||
164 | xcb_get_property_reply_t *m_propertyReply = nullptr; | ||||
165 | int m_propertyStart = 0; | ||||
166 | QByteArray m_data; | ||||
167 | }; | ||||
168 | | ||||
169 | /* | ||||
170 | * Represents a transfer from an X window to a Wayland native client. | ||||
171 | */ | ||||
172 | class TransferXtoWl : public Transfer | ||||
173 | { | ||||
174 | Q_OBJECT | ||||
175 | public: | ||||
176 | TransferXtoWl(xcb_atom_t selection, | ||||
177 | xcb_atom_t target, | ||||
178 | qint32 fd, | ||||
179 | xcb_timestamp_t timestamp, xcb_window_t parentWindow, | ||||
180 | QObject *parent = nullptr); | ||||
181 | ~TransferXtoWl(); | ||||
182 | | ||||
183 | bool handleSelNotify(xcb_selection_notify_event_t *event); | ||||
184 | bool handlePropNotify(xcb_property_notify_event_t *event) override; | ||||
185 | | ||||
186 | private: | ||||
187 | void dataSourceWrite(); | ||||
188 | void startTransfer(); | ||||
189 | void getIncrChunk(); | ||||
190 | | ||||
191 | xcb_window_t m_window; | ||||
192 | DataReceiver *m_receiver = nullptr; | ||||
193 | }; | ||||
194 | | ||||
195 | } | ||||
196 | } | ||||
197 | | ||||
198 | #endif |