desktop-shell: properly set background widget as opaque
[profile/ivi/weston-ivi-shell.git] / xwayland / dnd.c
1 /*
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
38 #include "cairo-util.h"
39 #include "compositor.h"
40 #include "hash.h"
41
42 static void
43 weston_dnd_start(struct weston_wm *wm, xcb_window_t owner)
44 {
45         uint32_t values[1], version = 4;
46
47         wm->dnd_window = xcb_generate_id(wm->conn);
48         values[0] =
49                 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
50                 XCB_EVENT_MASK_PROPERTY_CHANGE;
51
52         xcb_create_window(wm->conn,
53                           XCB_COPY_FROM_PARENT,
54                           wm->dnd_window,
55                           wm->screen->root,
56                           0, 0,
57                           8192, 8192,
58                           0,
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,
64                             wm->dnd_window,
65                             wm->atom.xdnd_aware,
66                             XCB_ATOM_ATOM,
67                             32, /* format */
68                             1, &version);
69
70         xcb_map_window(wm->conn, wm->dnd_window);
71         wm->dnd_owner = owner;
72 }
73
74 static void
75 weston_dnd_stop(struct weston_wm *wm)
76 {
77         xcb_destroy_window(wm->conn, wm->dnd_window);
78         wm->dnd_window = XCB_WINDOW_NONE;
79 }
80
81 struct dnd_data_source {
82         struct weston_data_source base;
83         struct weston_wm *wm;
84         int version;
85         uint32_t window;
86 };
87
88 static void
89 data_source_accept(struct weston_data_source *base,
90                    uint32_t time, const char *mime_type)
91 {
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;
95
96         weston_log("got accept, mime-type %s\n", mime_type);
97
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. */
102
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;
109         if (mime_type)
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;
114
115         xcb_send_event(wm->conn, 0, wm->dnd_owner,
116                                XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
117                                (char *) &client_message);
118 }
119
120 static void
121 data_source_send(struct weston_data_source *base,
122                  const char *mime_type, int32_t fd)
123 {
124         struct dnd_data_source *source = (struct dnd_data_source *) base;
125         struct weston_wm *wm = source->wm;
126
127         weston_log("got send, %s\n", mime_type);
128
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);
136
137         xcb_flush(wm->conn);
138
139         fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK);
140         wm->data_source_fd = fd;
141 }
142
143 static void
144 data_source_cancel(struct weston_data_source *source)
145 {
146         weston_log("got cancel\n");
147 }
148
149 static void
150 handle_enter(struct weston_wm *wm, xcb_client_message_event_t *client_message)
151 {
152         struct dnd_data_source *source;
153         struct weston_seat *seat = weston_wm_pick_seat(wm);
154         char **p;
155         const char *name;
156         uint32_t *types;
157         int i, length, has_text;
158         xcb_get_property_cookie_t cookie;
159         xcb_get_property_reply_t *reply;
160
161         source = malloc(sizeof *source);
162         if (source == NULL)
163                 return;
164
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;
169         source->wm = wm;
170         source->window = client_message->data.data32[0];
171         source->version = client_message->data.data32[1] >> 24;
172
173         if (client_message->data.data32[1] & 1) {
174                 cookie = xcb_get_property(wm->conn,
175                                           0, /* delete */
176                                           source->window,
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;
182         } else {
183                 reply = NULL;
184                 types = &client_message->data.data32[2];
185                 length = 3;
186         }
187
188         wl_array_init(&source->base.mime_types);
189         has_text = 0;
190         for (i = 0; i < length; i++) {
191                 if (types[i] == XCB_ATOM_NONE)
192                         continue;
193
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) {
198                         if (has_text)
199                                 continue;
200
201                         has_text = 1;
202                         p = wl_array_add(&source->base.mime_types, sizeof *p);
203                         if (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);
207                         if (p)
208                                 *p = strdup(name);
209                 }
210         }
211
212         free(reply);
213         weston_pointer_start_drag(seat->pointer, &source->base, NULL, NULL);
214 }
215
216 int
217 weston_wm_handle_dnd_event(struct weston_wm *wm,
218                            xcb_generic_event_t *event)
219 {
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;
224
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)
228                         return 0;
229
230                 weston_log("XdndSelection owner: %d!\n",
231                            xfixes_selection_notify->owner);
232
233                 if (xfixes_selection_notify->owner != XCB_WINDOW_NONE)
234                         weston_dnd_start(wm, xfixes_selection_notify->owner);
235                 else
236                         weston_dnd_stop(wm);
237
238                 return 1;
239         }
240
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);
245                         return 1;
246                 } else if (client_message->type == wm->atom.xdnd_leave) {
247                         weston_log("got leave!\n");
248                         return 1;
249                 } else if (client_message->type == wm->atom.xdnd_drop) {
250                         weston_log("got drop!\n");
251                         return 1;
252                 } else if (client_message->type == wm->atom.xdnd_drop) {
253                         weston_log("got enter!\n");
254                         return 1;
255                 }
256                 return 0;
257         }
258
259         return 0;
260 }
261
262 void
263 weston_wm_dnd_init(struct weston_wm *wm)
264 {
265         uint32_t mask;
266
267         mask =
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);
273 }