~cosme/ubuntu/oneiric/wayland/wayland

« back to all changes in this revision

Viewing changes to clients/dnd.c

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-02-23 18:13:23 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110223181323-xm62w5zi2adkscxq
Tags: 0.1~git20110214.e4762a6a-0ubuntu1
* Update to new git snapshot from 2011-02-14 up to commit e4762a6a. (This
  is the most recent upstream commit that doesn't require moving to a
  newer version of mesa.)
  (LP: #700986)
* copyright: Update to match current codebase, and reformat to follow
  dep5 style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
#include "window.h"
39
39
 
40
 
static const char socket_name[] = "\0wayland";
41
 
 
42
40
struct dnd {
43
41
        struct window *window;
44
42
        struct display *display;
53
51
        struct dnd *dnd;
54
52
        struct input *input;
55
53
        uint32_t time;
 
54
        struct item *item;
 
55
        int x_offset, y_offset;
 
56
        const char *mime_type;
56
57
};
57
58
 
58
59
struct dnd_offer {
 
60
        int refcount;
59
61
        struct dnd *dnd;
60
62
        struct wl_array types;
61
63
        const char *drag_type;
62
64
        uint32_t tag;
 
65
        int x, y;
63
66
};
64
67
 
65
68
struct item {
66
69
        cairo_surface_t *surface;
 
70
        int seed;
67
71
        int x, y;
68
72
};
69
73
 
 
74
struct dnd_flower_message {
 
75
        int seed, x_offset, y_offset;
 
76
};
 
77
 
 
78
 
70
79
static const int item_width = 64;
71
80
static const int item_height = 64;
72
81
static const int item_padding = 16;
73
82
 
74
83
static struct item *
75
 
item_create(struct display *display, int x, int y)
 
84
item_create(struct display *display, int x, int y, int seed)
76
85
{
 
86
        struct item *item;
 
87
        struct timeval tv;
 
88
 
 
89
        item = malloc(sizeof *item);
 
90
        if (item == NULL)
 
91
                return NULL;
 
92
        
 
93
        
 
94
        gettimeofday(&tv, NULL);
 
95
        item->seed = seed ? seed : tv.tv_usec;
 
96
        srandom(item->seed);
 
97
        
77
98
        const int petal_count = 3 + random() % 5;
78
99
        const double r1 = 20 + random() % 10;
79
100
        const double r2 = 5 + random() % 12;
85
106
        double t, dt = 2 * M_PI / (petal_count * 2);
86
107
        double x1, y1, x2, y2, x3, y3;
87
108
        struct rectangle rect;
88
 
        struct item *item;
89
109
 
90
 
        item = malloc(sizeof *item);
91
 
        if (item == NULL)
92
 
                return NULL;
93
110
 
94
111
        rect.width = item_width;
95
112
        rect.height = item_height;
152
169
static void
153
170
dnd_draw(struct dnd *dnd)
154
171
{
155
 
        struct rectangle rectangle;
 
172
        struct rectangle allocation;
156
173
        cairo_t *cr;
157
174
        cairo_surface_t *wsurface, *surface;
158
175
        int i;
159
176
 
160
177
        window_draw(dnd->window);
161
178
 
162
 
        window_get_child_rectangle(dnd->window, &rectangle);
 
179
        window_get_child_allocation(dnd->window, &allocation);
163
180
 
164
181
        wsurface = window_get_surface(dnd->window);
165
182
        surface = cairo_surface_create_similar(wsurface,
166
183
                                               CAIRO_CONTENT_COLOR_ALPHA,
167
 
                                               rectangle.width,
168
 
                                               rectangle.height);
 
184
                                               allocation.width,
 
185
                                               allocation.height);
169
186
        cairo_surface_destroy(wsurface);
170
187
 
171
188
        cr = cairo_create(surface);
184
201
 
185
202
        cairo_destroy(cr);
186
203
 
187
 
        window_copy_surface(dnd->window, &rectangle, surface);
 
204
        window_copy_surface(dnd->window, &allocation, surface);
188
205
        cairo_surface_destroy(surface);
189
206
        window_flush(dnd->window);
190
207
}
206
223
        window_schedule_redraw(dnd->window);
207
224
}
208
225
 
 
226
static void 
 
227
dnd_offer_destroy(struct dnd_offer *dnd_offer)
 
228
{
 
229
        dnd_offer->refcount--;
 
230
        if (dnd_offer->refcount == 0) {
 
231
                wl_array_release(&dnd_offer->types);
 
232
                free(dnd_offer);
 
233
        }
 
234
}
 
235
 
 
236
static int
 
237
dnd_add_item(struct dnd *dnd, struct item *item)
 
238
{
 
239
        int i;
 
240
 
 
241
        for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
 
242
                if (dnd->items[i] == 0) {
 
243
                        dnd->items[i] = item;
 
244
                        return i;
 
245
                }
 
246
        }
 
247
        return -1;
 
248
}
 
249
 
209
250
static struct item *
210
251
dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
211
252
{
212
253
        struct item *item;
213
 
        struct rectangle rectangle;
 
254
        struct rectangle allocation;
214
255
        int i;
215
256
 
216
 
        window_get_child_rectangle(dnd->window, &rectangle);
 
257
        window_get_child_allocation(dnd->window, &allocation);
217
258
 
218
 
        x -= rectangle.x;
219
 
        y -= rectangle.y;
 
259
        x -= allocation.x;
 
260
        y -= allocation.y;
220
261
 
221
262
        for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
222
263
                item = dnd->items[i];
241
282
 
242
283
        fprintf(stderr, "target %s\n", mime_type);
243
284
        device = input_get_input_device(dnd_drag->input);
 
285
        dnd_drag->mime_type = mime_type;
244
286
        if (mime_type)
245
287
                surface = dnd_drag->opaque;
246
288
        else
255
297
drag_finish(void *data, struct wl_drag *drag, int fd)
256
298
{
257
299
        struct dnd_drag *dnd_drag = data;
258
 
        char text[] = "[drop data]";
259
 
 
260
 
        fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
261
 
 
262
 
        write(fd, text, sizeof text);
 
300
 
 
301
        if (!dnd_drag->mime_type) {
 
302
                dnd_add_item(dnd_drag->dnd, dnd_drag->item);
 
303
                window_schedule_redraw(dnd_drag->dnd->window);
 
304
                return;
 
305
        }
 
306
        
 
307
        struct dnd_flower_message dnd_flower_message;   
 
308
        
 
309
        
 
310
        dnd_flower_message.seed = dnd_drag->item->seed;
 
311
 
 
312
        dnd_flower_message.x_offset = dnd_drag->x_offset;
 
313
        dnd_flower_message.y_offset = dnd_drag->y_offset;
 
314
 
 
315
        fprintf(stderr, "got 'finish', fd %d, sending dnd_flower_message\n", fd);
 
316
 
 
317
        write(fd, &dnd_flower_message, sizeof dnd_flower_message);
263
318
        close(fd);
264
319
 
265
320
        /* The 'finish' event marks the end of the session on the drag
266
321
         * source side and we need to clean up the drag object created
267
322
         * and the local state. */
268
323
        wl_drag_destroy(drag);
 
324
        
 
325
        /* Destroy the item that has been dragged out */
 
326
        cairo_surface_destroy(dnd_drag->item->surface);
 
327
        free(dnd_drag->item);
 
328
        
269
329
        cairo_surface_destroy(dnd_drag->translucent);
270
330
        cairo_surface_destroy(dnd_drag->opaque);
271
331
        free(dnd_drag);
272
332
}
273
333
 
 
334
static void
 
335
drag_reject(void *data, struct wl_drag *drag)
 
336
{
 
337
        struct dnd_drag *dnd_drag = data;
 
338
 
 
339
        dnd_add_item(dnd_drag->dnd, dnd_drag->item);
 
340
        window_schedule_redraw(dnd_drag->dnd->window);
 
341
}
 
342
 
274
343
static const struct wl_drag_listener drag_listener = {
275
344
        drag_target,
276
 
        drag_finish
 
345
        drag_finish,
 
346
        drag_reject
277
347
};
278
348
 
279
349
static void
305
375
         * allocated. */
306
376
        if (!surface) {
307
377
                fprintf(stderr, "pointer focus NULL, session over\n");
308
 
                wl_array_release(&dnd_offer->types);
309
 
                free(dnd_offer);
310
378
                wl_drag_offer_destroy(offer);
 
379
                dnd_offer_destroy(dnd_offer);
311
380
                return;
312
381
        }
313
382
 
321
390
        dnd_offer->dnd = window_get_user_data(window);
322
391
 
323
392
        if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
324
 
                wl_drag_offer_accept(offer, time, "text/plain");
325
 
                dnd_offer->drag_type = "text/plain";
 
393
                wl_drag_offer_accept(offer, time, "application/x-wayland-dnd-flower");
 
394
                dnd_offer->drag_type = "application/x-wayland-dnd-flower";
 
395
                dnd_offer->x = surface_x;
 
396
                dnd_offer->y = surface_y;
326
397
        } else {
327
398
                wl_drag_offer_accept(offer, time, NULL);
328
399
                dnd_offer->drag_type = NULL;
335
406
                  int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
336
407
{
337
408
        struct dnd_offer *dnd_offer = data;
338
 
        struct dnd *dnd = dnd_offer->dnd;
339
409
 
340
 
        if (!dnd_get_item(dnd, surface_x, surface_y)) {
 
410
        if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
341
411
                fprintf(stderr, "drag offer motion %d, %d, accepting\n",
342
412
                        surface_x, surface_y);
343
 
                wl_drag_offer_accept(offer, time, "text/plain");
344
 
                dnd_offer->drag_type = "text/plain";
 
413
                wl_drag_offer_accept(offer, time, "application/x-wayland-dnd-flower");
 
414
                dnd_offer->drag_type = "application/x-wayland-dnd-flower";
 
415
                dnd_offer->x = surface_x;
 
416
                dnd_offer->y = surface_y;
345
417
        } else {
346
418
                fprintf(stderr, "drag offer motion %d, %d, declining\n",
347
419
                        surface_x, surface_y);
354
426
drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
355
427
{
356
428
        struct dnd_offer *dnd_offer = data;
357
 
        char buffer[256];
 
429
        struct dnd *dnd = dnd_offer->dnd;
 
430
        struct dnd_flower_message dnd_flower_message;
358
431
        int fd;
359
432
        unsigned int len;
360
 
        GError *err = NULL;
 
433
        struct item *item;
361
434
 
362
 
        g_io_channel_read_chars(source, buffer, sizeof buffer, &len, &err);
363
 
        fprintf(stderr, "read %d bytes: %s\n", len, buffer);
364
435
        fd = g_io_channel_unix_get_fd(source);
 
436
        len = read(fd, &dnd_flower_message, sizeof dnd_flower_message);
 
437
        fprintf(stderr, "read %d bytes\n", len);
 
438
 
365
439
        close(fd);
366
440
        g_source_remove(dnd_offer->tag);
367
441
 
368
442
        g_io_channel_unref(source);
369
443
 
 
444
        item = item_create(dnd->display,
 
445
                           dnd_offer->x - dnd_flower_message.x_offset - 26,
 
446
                           dnd_offer->y - dnd_flower_message.y_offset - 66,
 
447
                           dnd_flower_message.seed);
 
448
 
 
449
        dnd_add_item(dnd, item);
 
450
        window_schedule_redraw(dnd->window);
 
451
 
 
452
        dnd_offer_destroy(dnd_offer);
 
453
 
370
454
        return TRUE;
371
455
}
372
456
 
379
463
 
380
464
        if (!dnd_offer->drag_type) {
381
465
                fprintf(stderr, "got 'drop', but no target\n");
382
 
                /* FIXME: Should send response so compositor and
383
 
                 * source knows it's over. Can't send -1 to indicate
384
 
                 * 'no target' though becauses of the way fd passing
385
 
                 * currently works.  */
 
466
                wl_drag_offer_reject(offer);
386
467
                return;
387
468
        }
388
469
 
389
470
        fprintf(stderr, "got 'drop', sending write end of pipe\n");
390
471
 
 
472
        dnd_offer->refcount++;
391
473
        pipe(p);
392
474
        wl_drag_offer_receive(offer, p[1]);
393
475
        close(p[1]);
412
494
        dnd_offer = malloc(sizeof *dnd_offer);
413
495
        if (dnd_offer == NULL)
414
496
                return;
 
497
        
 
498
        dnd_offer->refcount = 1;
415
499
 
416
500
        wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
417
501
        wl_array_init(&dnd_offer->types);
475
559
        struct dnd *dnd = data;
476
560
        int32_t x, y;
477
561
        struct item *item;
478
 
        struct rectangle rectangle;
 
562
        struct rectangle allocation;
479
563
        struct dnd_drag *dnd_drag;
 
564
        struct wl_drag *drag;
 
565
        int i;
480
566
 
481
 
        window_get_child_rectangle(dnd->window, &rectangle);
 
567
        window_get_child_allocation(dnd->window, &allocation);
482
568
        input_get_position(input, &x, &y);
483
569
        item = dnd_get_item(dnd, x, y);
484
 
        x -= rectangle.x;
485
 
        y -= rectangle.y;
 
570
        x -= allocation.x;
 
571
        y -= allocation.y;
486
572
 
487
573
        if (item && state == 1) {
488
574
                fprintf(stderr, "start drag, item %p\n", item);
491
577
                dnd_drag->dnd = dnd;
492
578
                dnd_drag->input = input;
493
579
                dnd_drag->time = time;
 
580
                dnd_drag->item = item;
 
581
                dnd_drag->x_offset = x - item->x;
 
582
                dnd_drag->y_offset = y - item->y;
 
583
 
 
584
                for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
 
585
                        if (item == dnd->items[i]){
 
586
                                dnd->items[i] = 0;
 
587
                                break;
 
588
                        }
 
589
                }
494
590
 
495
591
                dnd_drag->opaque =
496
592
                        create_drag_cursor(dnd_drag, item, x, y, 1);
497
593
                dnd_drag->translucent =
498
594
                        create_drag_cursor(dnd_drag, item, x, y, 0.2);
499
595
 
500
 
                window_start_drag(window, input, time,
501
 
                                  &drag_listener, dnd_drag);
 
596
                drag = window_create_drag(window);
 
597
                wl_drag_offer(drag, "application/x-wayland-dnd-flower");
 
598
                window_activate_drag(drag, window, input, time);
 
599
                wl_drag_add_listener(drag, &drag_listener, dnd_drag);
 
600
                window_schedule_redraw(dnd->window);
502
601
        }
503
602
}
504
603
 
525
624
        struct dnd *dnd;
526
625
        gchar *title;
527
626
        int i, x, y;
528
 
        struct rectangle rectangle;
 
627
        int32_t width, height;
529
628
 
530
629
        dnd = malloc(sizeof *dnd);
531
630
        if (dnd == NULL)
534
633
 
535
634
        title = g_strdup_printf("Wayland Drag and Drop Demo");
536
635
 
537
 
        dnd->window = window_create(display, title, 100, 100, 500, 400);
 
636
        dnd->window = window_create(display, title, 500, 400);
538
637
        dnd->display = display;
539
638
        dnd->key = 100;
540
639
 
542
641
                x = (i % 4) * (item_width + item_padding) + item_padding;
543
642
                y = (i / 4) * (item_height + item_padding) + item_padding;
544
643
                if ((i ^ (i >> 2)) & 1)
545
 
                        dnd->items[i] = item_create(display, x, y);
 
644
                        dnd->items[i] = item_create(display, x, y, 0);
546
645
                else
547
646
                        dnd->items[i] = NULL;
548
647
        }
557
656
        window_set_motion_handler(dnd->window,
558
657
                                  dnd_motion_handler);
559
658
 
560
 
        rectangle.width = 4 * (item_width + item_padding) + item_padding;
561
 
        rectangle.height = 4 * (item_height + item_padding) + item_padding;
562
 
        window_set_child_size(dnd->window, &rectangle);
 
659
        width = 4 * (item_width + item_padding) + item_padding;
 
660
        height = 4 * (item_height + item_padding) + item_padding;
 
661
        window_set_child_size(dnd->window, width, height);
563
662
 
564
663
        dnd_draw(dnd);
565
664
 
575
674
{
576
675
        struct display *d;
577
676
        struct dnd *dnd;
578
 
        struct timeval tv;
579
 
 
580
 
        gettimeofday(&tv, NULL);
581
 
        srandom(tv.tv_usec);
582
677
 
583
678
        d = display_create(&argc, &argv, option_entries);
584
679
        if (d == NULL) {