Set pointer image only in response to 'target' event
[profile/ivi/weston.git] / clients / dnd.c
1 /*
2  * Copyright © 2010 Kristian Høgsberg
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <math.h>
30 #include <sys/time.h>
31 #include <cairo.h>
32 #include <glib.h>
33 #include <gdk-pixbuf/gdk-pixbuf.h>
34
35 #include "wayland-client.h"
36 #include "wayland-glib.h"
37
38 #include "window.h"
39
40 static const char socket_name[] = "\0wayland";
41
42 struct dnd {
43         struct window *window;
44         struct display *display;
45         uint32_t key;
46         struct item *items[16];
47
48         struct wl_buffer *buffer;
49         int hotspot_x, hotspot_y;
50 };
51
52 struct item {
53         cairo_surface_t *surface;
54         int x, y;
55 };
56
57 static const int item_width = 64;
58 static const int item_height = 64;
59 static const int item_padding = 16;
60
61 static struct item *
62 item_create(struct display *display, int x, int y)
63 {
64         const int petal_count = 3 + random() % 5;
65         const double r1 = 20 + random() % 10;
66         const double r2 = 5 + random() % 12;
67         const double u = (10 + random() % 90) / 100.0;
68         const double v = (random() % 90) / 100.0;
69
70         cairo_t *cr;
71         int i;
72         double t, dt = 2 * M_PI / (petal_count * 2);
73         double x1, y1, x2, y2, x3, y3;
74         struct rectangle rect;
75         struct item *item;
76
77         item = malloc(sizeof *item);
78         if (item == NULL)
79                 return NULL;
80
81         rect.width = item_width;
82         rect.height = item_height;
83         item->surface = display_create_surface(display, &rect);
84
85         item->x = x;
86         item->y = y;
87
88         cr = cairo_create(item->surface);
89         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
90         cairo_set_source_rgba(cr, 0, 0, 0, 0);
91         cairo_paint(cr);
92
93         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
94         cairo_translate(cr, item_width / 2, item_height / 2);
95         t = random();
96         cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
97         for (i = 0; i < petal_count; i++, t += dt * 2) {
98                 x1 = cos(t) * r1;
99                 y1 = sin(t) * r1;
100                 x2 = cos(t + dt) * r2;
101                 y2 = sin(t + dt) * r2;
102                 x3 = cos(t + 2 * dt) * r1;
103                 y3 = sin(t + 2 * dt) * r1;
104
105                 cairo_curve_to(cr,
106                                x1 - y1 * u, y1 + x1 * u,
107                                x2 + y2 * v, y2 - x2 * v,
108                                x2, y2);                        
109
110                 cairo_curve_to(cr,
111                                x2 - y2 * v, y2 + x2 * v,
112                                x3 + y3 * u, y3 - x3 * u,
113                                x3, y3);
114         }
115
116         cairo_close_path(cr);
117
118         cairo_set_source_rgba(cr,
119                               0.5 + (random() % 50) / 49.0,
120                               0.5 + (random() % 50) / 49.0,
121                               0.5 + (random() % 50) / 49.0,
122                               0.5 + (random() % 100) / 99.0);
123
124         cairo_fill_preserve(cr);
125
126         cairo_set_line_width(cr, 1);
127         cairo_set_source_rgba(cr,
128                               0.5 + (random() % 50) / 49.0,
129                               0.5 + (random() % 50) / 49.0,
130                               0.5 + (random() % 50) / 49.0,
131                               0.5 + (random() % 100) / 99.0);
132         cairo_stroke(cr);
133
134         cairo_destroy(cr);
135
136         return item;
137 }
138
139 static void
140 dnd_draw(struct dnd *dnd)
141 {
142         struct rectangle rectangle;
143         cairo_t *cr;
144         cairo_surface_t *wsurface, *surface;
145         int i;
146
147         window_draw(dnd->window);
148
149         window_get_child_rectangle(dnd->window, &rectangle);
150
151         wsurface = window_get_surface(dnd->window);
152         surface = cairo_surface_create_similar(wsurface,
153                                                CAIRO_CONTENT_COLOR_ALPHA,
154                                                rectangle.width,
155                                                rectangle.height);
156
157         cr = cairo_create(surface);
158         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
159         cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
160         cairo_paint(cr);
161
162         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
163         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
164                 if (!dnd->items[i])
165                         continue;
166                 cairo_set_source_surface(cr, dnd->items[i]->surface,
167                                          dnd->items[i]->x, dnd->items[i]->y);
168                 cairo_paint(cr);
169         }
170
171         cairo_destroy(cr);
172
173         window_copy_surface(dnd->window, &rectangle, surface);
174         window_commit(dnd->window, dnd->key);
175         cairo_surface_destroy(surface);
176 }
177
178 static void
179 redraw_handler(struct window *window, void *data)
180 {
181         struct dnd *dnd = data;
182
183         dnd_draw(dnd);
184 }
185
186 static void
187 keyboard_focus_handler(struct window *window,
188                        struct input *device, void *data)
189 {
190         struct dnd *dnd = data;
191
192         window_schedule_redraw(dnd->window);
193 }
194
195 static struct item *
196 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
197 {
198         struct item *item;
199         int i;
200
201         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
202                 item = dnd->items[i];
203                 if (item &&
204                     item->x <= x && x < item->x + item_width &&
205                     item->y <= y && y < item->y + item_height)
206                         return item;
207         }
208
209         return NULL;
210 }
211
212 static void
213 drag_handle_device(void *data,
214                    struct wl_drag *drag, struct wl_input_device *device)
215 {
216 }
217
218 static void
219 drag_pointer_focus(void *data,
220                    struct wl_drag *drag,
221                    uint32_t time, struct wl_surface *surface,
222                    int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
223 {
224         fprintf(stderr, "drag pointer focus %p\n", surface);
225
226         wl_drag_accept(drag, "text/plain");
227 }
228
229 static void
230 drag_offer(void *data,
231            struct wl_drag *drag, const char *type)
232 {
233         fprintf(stderr, "drag offer %s\n", type);
234 }
235
236 static void
237 drag_motion(void *data,
238             struct wl_drag *drag,
239             uint32_t time,
240             int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
241 {
242         fprintf(stderr, "drag motion %d,%d\n", surface_x, surface_y);
243
244         /* FIXME: Need to correlate this with the offer event.
245          * Problem is, we don't know when we've seen that last offer
246          * event, and we might need to look at all of them before we
247          * can decide which one to go with. */
248         wl_drag_accept(drag, "text/plain");
249 }
250
251 static void
252 drag_target(void *data,
253             struct wl_drag *drag, const char *mime_type)
254 {
255         struct dnd *dnd = data;
256         struct input *input;
257         struct wl_input_device *device;
258
259         fprintf(stderr, "target %s\n", mime_type);
260
261         input = wl_drag_get_user_data(drag);
262         device = input_get_input_device(input);
263         wl_input_device_attach(device, dnd->buffer,
264                                dnd->hotspot_x, dnd->hotspot_y);
265 }
266
267 static void
268 drag_finish(void *data, struct wl_drag *drag)
269 {
270         fprintf(stderr, "drag finish\n");
271         struct wl_array a;
272         char text[] = "[drop data]";
273
274         a.data = text;
275         a.size = sizeof text;
276
277         wl_drag_send(drag, &a);
278 }
279
280 static void
281 drag_data(void *data,
282           struct wl_drag *drag, struct wl_array *contents)
283 {
284         fprintf(stderr, "drag drop, data %s\n", (char *) contents->data);
285 }
286
287 static const struct wl_drag_listener drag_listener = {
288         drag_handle_device,
289         drag_pointer_focus,
290         drag_offer,
291         drag_motion,
292         drag_target,
293         drag_finish,
294         drag_data
295 };
296
297 static void
298 dnd_button_handler(struct window *window,
299                    struct input *input, uint32_t time,
300                    int button, int state, void *data)
301 {
302         struct dnd *dnd = data;
303         int32_t x, y, hotspot_x, hotspot_y, pointer_width, pointer_height;
304         struct rectangle rectangle;
305         struct item *item;
306         cairo_surface_t *surface, *pointer;
307         cairo_t *cr;
308
309         input_get_position(input, &x, &y);
310         window_get_child_rectangle(dnd->window, &rectangle);
311
312         x -= rectangle.x;
313         y -= rectangle.y;
314         item = dnd_get_item(dnd, x, y);
315
316         if (item && state == 1) {
317                 fprintf(stderr, "start drag, item %p\n", item);
318
319                 pointer = display_get_pointer_surface(dnd->display,
320                                                       POINTER_DRAGGING,
321                                                       &pointer_width,
322                                                       &pointer_height,
323                                                       &hotspot_x,
324                                                       &hotspot_y);
325
326                 rectangle.width = item_width + 2 * pointer_width;
327                 rectangle.height = item_height + 2 * pointer_height;
328                 surface = display_create_surface(dnd->display, &rectangle);
329
330                 cr = cairo_create(surface);
331                 cairo_translate(cr, pointer_width, pointer_height);
332
333                 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
334                 cairo_set_source_rgba(cr, 0, 0, 0, 0);
335                 cairo_paint(cr);
336
337                 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
338                 cairo_set_source_surface(cr, item->surface, 0, 0);
339                 cairo_paint(cr);
340
341                 cairo_set_source_surface(cr, pointer,
342                                          x - item->x - hotspot_x,
343                                          y - item->y - hotspot_y);
344                 cairo_surface_destroy(pointer);
345                 cairo_paint(cr);
346                 cairo_destroy(cr);
347
348                 dnd->buffer = display_get_buffer_for_surface(dnd->display,
349                                                              surface);
350                 dnd->hotspot_x = pointer_width + x - item->x;
351                 dnd->hotspot_y = pointer_height + y - item->y;
352
353                 window_start_drag(window, input, time);
354
355                 /* FIXME: We leak the surface because we can't free it
356                  * until the server has referenced it. */
357         }
358 }
359
360 static int
361 dnd_motion_handler(struct window *window,
362                    struct input *input, uint32_t time,
363                    int32_t x, int32_t y,
364                    int32_t sx, int32_t sy, void *data)
365 {
366         struct dnd *dnd = data;
367         struct item *item;
368         struct rectangle rectangle;
369
370         window_get_child_rectangle(dnd->window, &rectangle);
371         item = dnd_get_item(dnd, sx - rectangle.x, sy - rectangle.y);
372
373         if (item)
374                 return POINTER_HAND1;
375         else
376                 return POINTER_LEFT_PTR;
377 }
378
379 static struct dnd *
380 dnd_create(struct display *display)
381 {
382         struct dnd *dnd;
383         gchar *title;
384         int i, x, y;
385         struct rectangle rectangle;
386
387         dnd = malloc(sizeof *dnd);
388         if (dnd == NULL)
389                 return dnd;
390         memset(dnd, 0, sizeof *dnd);
391
392         title = g_strdup_printf("Wayland Drag and Drop Demo");
393
394         dnd->window = window_create(display, title, 100, 100, 500, 400);
395         dnd->display = display;
396         dnd->key = 100;
397
398         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
399                 x = (i % 4) * (item_width + item_padding) + item_padding;
400                 y = (i / 4) * (item_height + item_padding) + item_padding;
401                 if ((i ^ (i >> 2)) & 1)
402                         dnd->items[i] = item_create(display, x, y);
403                 else
404                         dnd->items[i] = NULL;
405         }
406
407         window_set_user_data(dnd->window, dnd);
408         window_set_redraw_handler(dnd->window, redraw_handler);
409         window_set_keyboard_focus_handler(dnd->window,
410                                           keyboard_focus_handler);
411         window_set_button_handler(dnd->window,
412                                   dnd_button_handler);
413
414         window_set_motion_handler(dnd->window,
415                                   dnd_motion_handler);
416
417         rectangle.width = 4 * (item_width + item_padding) + item_padding;
418         rectangle.height = 4 * (item_height + item_padding) + item_padding;
419         window_set_child_size(dnd->window, &rectangle);
420
421         display_add_drag_listener(display, &drag_listener, dnd);
422
423         dnd_draw(dnd);
424
425         return dnd;
426 }
427
428 static const GOptionEntry option_entries[] = {
429         { NULL }
430 };
431
432 int
433 main(int argc, char *argv[])
434 {
435         struct display *d;
436         struct dnd *dnd;
437         struct timeval tv;
438
439         gettimeofday(&tv, NULL);
440         srandom(tv.tv_usec);
441
442         d = display_create(&argc, &argv, option_entries);
443
444         dnd = dnd_create (d);
445
446         display_run(d);
447
448         return 0;
449 }