blob: daeb08dec5cc9b37761c47b9bc41d1292fcf61a5 [file] [log] [blame]
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include "config.h"
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <unistd.h>
33#include <signal.h>
34#include <X11/Xcursor/Xcursor.h>
35
36#include "xwayland.h"
37
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080038#include "cairo-util.h"
39#include "compositor.h"
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -070040#include "xserver-server-protocol.h"
41#include "hash.h"
42
43static void
44weston_dnd_start(struct weston_wm *wm, xcb_window_t owner)
45{
46 uint32_t values[1], version = 4;
47
48 wm->dnd_window = xcb_generate_id(wm->conn);
49 values[0] =
50 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
51 XCB_EVENT_MASK_PROPERTY_CHANGE;
52
53 xcb_create_window(wm->conn,
54 XCB_COPY_FROM_PARENT,
55 wm->dnd_window,
56 wm->screen->root,
57 0, 0,
58 8192, 8192,
59 0,
60 XCB_WINDOW_CLASS_INPUT_ONLY,
61 wm->screen->root_visual,
62 XCB_CW_EVENT_MASK, values);
63 xcb_change_property(wm->conn,
64 XCB_PROP_MODE_REPLACE,
65 wm->dnd_window,
66 wm->atom.xdnd_aware,
67 XCB_ATOM_ATOM,
68 32, /* format */
69 1, &version);
70
71 xcb_map_window(wm->conn, wm->dnd_window);
72 wm->dnd_owner = owner;
73}
74
75static void
76weston_dnd_stop(struct weston_wm *wm)
77{
78 xcb_destroy_window(wm->conn, wm->dnd_window);
79 wm->dnd_window = XCB_WINDOW_NONE;
80}
81
82struct dnd_data_source {
83 struct weston_data_source base;
84 struct weston_wm *wm;
85 int version;
86 uint32_t window;
87};
88
89static void
90data_source_accept(struct weston_data_source *base,
91 uint32_t time, const char *mime_type)
92{
93 struct dnd_data_source *source = (struct dnd_data_source *) base;
94 xcb_client_message_event_t client_message;
95 struct weston_wm *wm = source->wm;
96
97 weston_log("got accept, mime-type %s\n", mime_type);
98
99 /* FIXME: If we rewrote UTF8_STRING to
100 * text/plain;charset=utf-8 and the source doesn't support the
101 * mime-type, we'll have to rewrite the mime-type back to
102 * UTF8_STRING here. */
103
104 client_message.response_type = XCB_CLIENT_MESSAGE;
105 client_message.format = 32;
106 client_message.window = wm->dnd_window;
107 client_message.type = wm->atom.xdnd_status;
108 client_message.data.data32[0] = wm->dnd_window;
109 client_message.data.data32[1] = 2;
110 if (mime_type)
111 client_message.data.data32[1] |= 1;
112 client_message.data.data32[2] = 0;
113 client_message.data.data32[3] = 0;
114 client_message.data.data32[4] = wm->atom.xdnd_action_copy;
115
116 xcb_send_event(wm->conn, 0, wm->dnd_owner,
117 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
118 (char *) &client_message);
119}
120
121static void
122data_source_send(struct weston_data_source *base,
123 const char *mime_type, int32_t fd)
124{
125 struct dnd_data_source *source = (struct dnd_data_source *) base;
126 struct weston_wm *wm = source->wm;
127
128 weston_log("got send, %s\n", mime_type);
129
130 /* Get data for the utf8_string target */
131 xcb_convert_selection(wm->conn,
132 wm->selection_window,
133 wm->atom.xdnd_selection,
134 wm->atom.utf8_string,
135 wm->atom.wl_selection,
136 XCB_TIME_CURRENT_TIME);
137
138 xcb_flush(wm->conn);
139
140 fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
141 wm->data_source_fd = fd;
142}
143
144static void
145data_source_cancel(struct weston_data_source *source)
146{
147 weston_log("got cancel\n");
148}
149
150static void
151handle_enter(struct weston_wm *wm, xcb_client_message_event_t *client_message)
152{
153 struct dnd_data_source *source;
154 struct weston_seat *seat = weston_wm_pick_seat(wm);
155 char **p;
156 const char *name;
157 uint32_t *types;
158 int i, length, has_text;
159 xcb_get_property_cookie_t cookie;
160 xcb_get_property_reply_t *reply;
161
162 source = malloc(sizeof *source);
163 if (source == NULL)
164 return;
165
166 wl_signal_init(&source->base.destroy_signal);
167 source->base.accept = data_source_accept;
168 source->base.send = data_source_send;
169 source->base.cancel = data_source_cancel;
170 source->wm = wm;
171 source->window = client_message->data.data32[0];
172 source->version = client_message->data.data32[1] >> 24;
173
174 if (client_message->data.data32[1] & 1) {
175 cookie = xcb_get_property(wm->conn,
176 0, /* delete */
177 source->window,
178 wm->atom.xdnd_type_list,
179 XCB_ATOM_ANY, 0, 2048);
180 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
181 types = xcb_get_property_value(reply);
182 length = reply->value_len;
183 } else {
184 reply = NULL;
185 types = &client_message->data.data32[2];
186 length = 3;
187 }
188
189 wl_array_init(&source->base.mime_types);
190 has_text = 0;
191 for (i = 0; i < length; i++) {
192 if (types[i] == XCB_ATOM_NONE)
193 continue;
194
195 name = get_atom_name(wm->conn, types[i]);
196 if (types[i] == wm->atom.utf8_string ||
197 types[i] == wm->atom.text_plain_utf8 ||
198 types[i] == wm->atom.text_plain) {
199 if (has_text)
200 continue;
201
202 has_text = 1;
203 p = wl_array_add(&source->base.mime_types, sizeof *p);
204 if (p)
205 *p = strdup("text/plain;charset=utf-8");
206 } else if (strchr(name, '/')) {
207 p = wl_array_add(&source->base.mime_types, sizeof *p);
208 if (p)
209 *p = strdup(name);
210 }
211 }
212
213 free(reply);
Xiong Zhangfd51e7b2013-11-25 18:42:49 +0800214 weston_pointer_start_drag(seat->pointer, &source->base, NULL, NULL);
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -0700215}
216
217int
218weston_wm_handle_dnd_event(struct weston_wm *wm,
219 xcb_generic_event_t *event)
220{
221 xcb_xfixes_selection_notify_event_t *xfixes_selection_notify =
222 (xcb_xfixes_selection_notify_event_t *) event;
223 xcb_client_message_event_t *client_message =
224 (xcb_client_message_event_t *) event;
225
226 switch (event->response_type - wm->xfixes->first_event) {
227 case XCB_XFIXES_SELECTION_NOTIFY:
228 if (xfixes_selection_notify->selection != wm->atom.xdnd_selection)
229 return 0;
230
231 weston_log("XdndSelection owner: %d!\n",
232 xfixes_selection_notify->owner);
233
234 if (xfixes_selection_notify->owner != XCB_WINDOW_NONE)
235 weston_dnd_start(wm, xfixes_selection_notify->owner);
236 else
237 weston_dnd_stop(wm);
238
239 return 1;
240 }
241
242 switch (EVENT_TYPE(event)) {
243 case XCB_CLIENT_MESSAGE:
244 if (client_message->type == wm->atom.xdnd_enter) {
245 handle_enter(wm, client_message);
246 return 1;
247 } else if (client_message->type == wm->atom.xdnd_leave) {
248 weston_log("got leave!\n");
249 return 1;
250 } else if (client_message->type == wm->atom.xdnd_drop) {
251 weston_log("got drop!\n");
252 return 1;
253 } else if (client_message->type == wm->atom.xdnd_drop) {
254 weston_log("got enter!\n");
255 return 1;
256 }
257 return 0;
258 }
259
260 return 0;
261}
262
263void
264weston_wm_dnd_init(struct weston_wm *wm)
265{
266 uint32_t mask;
267
268 mask =
269 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
270 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
271 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
272 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
273 wm->atom.xdnd_selection, mask);
274}