2 * Copyright © 2013 Intel Corporation
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.
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.
28 #include <sys/socket.h>
34 #include <X11/Xcursor/Xcursor.h>
38 #include "cairo-util.h"
39 #include "compositor.h"
43 weston_dnd_start(struct weston_wm *wm, xcb_window_t owner)
45 uint32_t values[1], version = 4;
47 wm->dnd_window = xcb_generate_id(wm->conn);
49 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
50 XCB_EVENT_MASK_PROPERTY_CHANGE;
52 xcb_create_window(wm->conn,
59 XCB_WINDOW_CLASS_INPUT_ONLY,
60 wm->screen->root_visual,
61 XCB_CW_EVENT_MASK, values);
62 xcb_change_property(wm->conn,
63 XCB_PROP_MODE_REPLACE,
70 xcb_map_window(wm->conn, wm->dnd_window);
71 wm->dnd_owner = owner;
75 weston_dnd_stop(struct weston_wm *wm)
77 xcb_destroy_window(wm->conn, wm->dnd_window);
78 wm->dnd_window = XCB_WINDOW_NONE;
81 struct dnd_data_source {
82 struct weston_data_source base;
89 data_source_accept(struct weston_data_source *base,
90 uint32_t time, const char *mime_type)
92 struct dnd_data_source *source = (struct dnd_data_source *) base;
93 xcb_client_message_event_t client_message;
94 struct weston_wm *wm = source->wm;
96 weston_log("got accept, mime-type %s\n", mime_type);
98 /* FIXME: If we rewrote UTF8_STRING to
99 * text/plain;charset=utf-8 and the source doesn't support the
100 * mime-type, we'll have to rewrite the mime-type back to
101 * UTF8_STRING here. */
103 client_message.response_type = XCB_CLIENT_MESSAGE;
104 client_message.format = 32;
105 client_message.window = wm->dnd_window;
106 client_message.type = wm->atom.xdnd_status;
107 client_message.data.data32[0] = wm->dnd_window;
108 client_message.data.data32[1] = 2;
110 client_message.data.data32[1] |= 1;
111 client_message.data.data32[2] = 0;
112 client_message.data.data32[3] = 0;
113 client_message.data.data32[4] = wm->atom.xdnd_action_copy;
115 xcb_send_event(wm->conn, 0, wm->dnd_owner,
116 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
117 (char *) &client_message);
121 data_source_send(struct weston_data_source *base,
122 const char *mime_type, int32_t fd)
124 struct dnd_data_source *source = (struct dnd_data_source *) base;
125 struct weston_wm *wm = source->wm;
127 weston_log("got send, %s\n", mime_type);
129 /* Get data for the utf8_string target */
130 xcb_convert_selection(wm->conn,
131 wm->selection_window,
132 wm->atom.xdnd_selection,
133 wm->atom.utf8_string,
134 wm->atom.wl_selection,
135 XCB_TIME_CURRENT_TIME);
139 fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
140 wm->data_source_fd = fd;
144 data_source_cancel(struct weston_data_source *source)
146 weston_log("got cancel\n");
150 handle_enter(struct weston_wm *wm, xcb_client_message_event_t *client_message)
152 struct dnd_data_source *source;
153 struct weston_seat *seat = weston_wm_pick_seat(wm);
157 int i, length, has_text;
158 xcb_get_property_cookie_t cookie;
159 xcb_get_property_reply_t *reply;
161 source = malloc(sizeof *source);
165 wl_signal_init(&source->base.destroy_signal);
166 source->base.accept = data_source_accept;
167 source->base.send = data_source_send;
168 source->base.cancel = data_source_cancel;
170 source->window = client_message->data.data32[0];
171 source->version = client_message->data.data32[1] >> 24;
173 if (client_message->data.data32[1] & 1) {
174 cookie = xcb_get_property(wm->conn,
177 wm->atom.xdnd_type_list,
178 XCB_ATOM_ANY, 0, 2048);
179 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
180 types = xcb_get_property_value(reply);
181 length = reply->value_len;
184 types = &client_message->data.data32[2];
188 wl_array_init(&source->base.mime_types);
190 for (i = 0; i < length; i++) {
191 if (types[i] == XCB_ATOM_NONE)
194 name = get_atom_name(wm->conn, types[i]);
195 if (types[i] == wm->atom.utf8_string ||
196 types[i] == wm->atom.text_plain_utf8 ||
197 types[i] == wm->atom.text_plain) {
202 p = wl_array_add(&source->base.mime_types, sizeof *p);
204 *p = strdup("text/plain;charset=utf-8");
205 } else if (strchr(name, '/')) {
206 p = wl_array_add(&source->base.mime_types, sizeof *p);
213 weston_pointer_start_drag(seat->pointer, &source->base, NULL, NULL);
217 weston_wm_handle_dnd_event(struct weston_wm *wm,
218 xcb_generic_event_t *event)
220 xcb_xfixes_selection_notify_event_t *xfixes_selection_notify =
221 (xcb_xfixes_selection_notify_event_t *) event;
222 xcb_client_message_event_t *client_message =
223 (xcb_client_message_event_t *) event;
225 switch (event->response_type - wm->xfixes->first_event) {
226 case XCB_XFIXES_SELECTION_NOTIFY:
227 if (xfixes_selection_notify->selection != wm->atom.xdnd_selection)
230 weston_log("XdndSelection owner: %d!\n",
231 xfixes_selection_notify->owner);
233 if (xfixes_selection_notify->owner != XCB_WINDOW_NONE)
234 weston_dnd_start(wm, xfixes_selection_notify->owner);
241 switch (EVENT_TYPE(event)) {
242 case XCB_CLIENT_MESSAGE:
243 if (client_message->type == wm->atom.xdnd_enter) {
244 handle_enter(wm, client_message);
246 } else if (client_message->type == wm->atom.xdnd_leave) {
247 weston_log("got leave!\n");
249 } else if (client_message->type == wm->atom.xdnd_drop) {
250 weston_log("got drop!\n");
252 } else if (client_message->type == wm->atom.xdnd_drop) {
253 weston_log("got enter!\n");
263 weston_wm_dnd_init(struct weston_wm *wm)
268 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
269 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
270 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
271 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
272 wm->atom.xdnd_selection, mask);