* OF THIS SOFTWARE.
*/
+#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <cairo.h>
-#include <glib.h>
-#include <gdk-pixbuf/gdk-pixbuf.h>
+#include <sys/epoll.h>
-#include "wayland-client.h"
-#include "wayland-glib.h"
+#include <wayland-client.h>
+#include <wayland-cursor.h>
#include "window.h"
+#include "../shared/cairo-util.h"
-static const char socket_name[] = "\0wayland";
+struct dnd_drag;
struct dnd {
struct window *window;
+ struct widget *widget;
struct display *display;
uint32_t key;
struct item *items[16];
+ int self_only;
+ struct dnd_drag *current_drag;
+};
- struct wl_buffer *buffer;
+struct dnd_drag {
+ cairo_surface_t *translucent;
+ cairo_surface_t *opaque;
int hotspot_x, hotspot_y;
- uint32_t tag;
- const char *drag_type;
+ struct dnd *dnd;
+ struct input *input;
+ uint32_t time;
+ struct item *item;
+ int x_offset, y_offset;
+ int width, height;
+ const char *mime_type;
+
+ struct wl_surface *drag_surface;
+ struct wl_data_source *data_source;
};
struct item {
cairo_surface_t *surface;
+ int seed;
int x, y;
};
+struct dnd_flower_message {
+ int seed, x_offset, y_offset;
+};
+
+
static const int item_width = 64;
static const int item_height = 64;
static const int item_padding = 16;
static struct item *
-item_create(struct display *display, int x, int y)
+item_create(struct display *display, int x, int y, int seed)
{
+ struct item *item;
+ struct timeval tv;
+
+ item = malloc(sizeof *item);
+ if (item == NULL)
+ return NULL;
+
+
+ gettimeofday(&tv, NULL);
+ item->seed = seed ? seed : tv.tv_usec;
+ srandom(item->seed);
+
const int petal_count = 3 + random() % 5;
const double r1 = 20 + random() % 10;
const double r2 = 5 + random() % 12;
double t, dt = 2 * M_PI / (petal_count * 2);
double x1, y1, x2, y2, x3, y3;
struct rectangle rect;
- struct item *item;
- item = malloc(sizeof *item);
- if (item == NULL)
- return NULL;
rect.width = item_width;
rect.height = item_height;
- item->surface = display_create_surface(display, &rect);
+ item->surface =
+ display_create_surface(display, NULL, &rect, SURFACE_SHM);
item->x = x;
item->y = y;
cairo_curve_to(cr,
x1 - y1 * u, y1 + x1 * u,
x2 + y2 * v, y2 - x2 * v,
- x2, y2);
+ x2, y2);
cairo_curve_to(cr,
x2 - y2 * v, y2 + x2 * v,
}
static void
-dnd_draw(struct dnd *dnd)
+dnd_redraw_handler(struct widget *widget, void *data)
{
- struct rectangle rectangle;
+ struct dnd *dnd = data;
+ struct rectangle allocation;
cairo_t *cr;
- cairo_surface_t *wsurface, *surface;
- int i;
-
- window_draw(dnd->window);
-
- window_get_child_rectangle(dnd->window, &rectangle);
-
- wsurface = window_get_surface(dnd->window);
- surface = cairo_surface_create_similar(wsurface,
- CAIRO_CONTENT_COLOR_ALPHA,
- rectangle.width,
- rectangle.height);
+ cairo_surface_t *surface;
+ unsigned int i;
+ surface = window_get_surface(dnd->window);
cr = cairo_create(surface);
+ widget_get_allocation(dnd->widget, &allocation);
+ cairo_rectangle(cr, allocation.x, allocation.y,
+ allocation.width, allocation.height);
+
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
- cairo_paint(cr);
+ cairo_fill(cr);
+ cairo_rectangle(cr, allocation.x, allocation.y,
+ allocation.width, allocation.height);
+ cairo_clip(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
if (!dnd->items[i])
continue;
cairo_set_source_surface(cr, dnd->items[i]->surface,
- dnd->items[i]->x, dnd->items[i]->y);
+ dnd->items[i]->x + allocation.x,
+ dnd->items[i]->y + allocation.y);
cairo_paint(cr);
}
cairo_destroy(cr);
-
- window_copy_surface(dnd->window, &rectangle, surface);
- window_commit(dnd->window, dnd->key);
cairo_surface_destroy(surface);
}
static void
-redraw_handler(struct window *window, void *data)
+keyboard_focus_handler(struct window *window,
+ struct input *device, void *data)
{
struct dnd *dnd = data;
- dnd_draw(dnd);
+ window_schedule_redraw(dnd->window);
}
-static void
-keyboard_focus_handler(struct window *window,
- struct input *device, void *data)
+static int
+dnd_add_item(struct dnd *dnd, struct item *item)
{
- struct dnd *dnd = data;
+ unsigned int i;
- window_schedule_redraw(dnd->window);
+ for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
+ if (dnd->items[i] == 0) {
+ dnd->items[i] = item;
+ return i;
+ }
+ }
+ return -1;
}
static struct item *
dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
{
struct item *item;
- int i;
+ struct rectangle allocation;
+ unsigned int i;
+
+ widget_get_allocation(dnd->widget, &allocation);
+
+ x -= allocation.x;
+ y -= allocation.y;
for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
item = dnd->items[i];
}
static void
-drag_handle_device(void *data,
- struct wl_drag *drag, struct wl_input_device *device)
-{
-}
-
-static void
-drag_pointer_focus(void *data,
- struct wl_drag *drag,
- uint32_t time, struct wl_surface *surface,
- int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
+data_source_target(void *data,
+ struct wl_data_source *source, const char *mime_type)
{
- struct dnd *dnd = data;
-
- /* FIXME: We need the offered types before we get the
- * pointer_focus event so we know which one we want and can
- * send the accept request back. */
+ struct dnd_drag *dnd_drag = data;
+ struct dnd *dnd = dnd_drag->dnd;
+ cairo_surface_t *surface;
+ struct wl_buffer *buffer;
- fprintf(stderr, "drag pointer focus %p\n", surface);
+ dnd_drag->mime_type = mime_type;
+ if (mime_type)
+ surface = dnd_drag->opaque;
+ else
+ surface = dnd_drag->translucent;
- if (surface) {
- wl_drag_accept(drag, "text/plain");
- dnd->drag_type = "text/plain";
- } else {
- dnd->drag_type = NULL;
- }
+ buffer = display_get_buffer_for_surface(dnd->display, surface);
+ wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
+ wl_surface_damage(dnd_drag->drag_surface, 0, 0,
+ dnd_drag->width, dnd_drag->height);
+ wl_surface_commit(dnd_drag->drag_surface);
}
static void
-drag_offer(void *data,
- struct wl_drag *drag, const char *type)
+data_source_send(void *data, struct wl_data_source *source,
+ const char *mime_type, int32_t fd)
{
- fprintf(stderr, "drag offer %s\n", type);
-}
+ struct dnd_flower_message dnd_flower_message;
+ struct dnd_drag *dnd_drag = data;
+
+ dnd_flower_message.seed = dnd_drag->item->seed;
+ dnd_flower_message.x_offset = dnd_drag->x_offset;
+ dnd_flower_message.y_offset = dnd_drag->y_offset;
-static void
-drag_motion(void *data,
- struct wl_drag *drag,
- uint32_t time,
- int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
-{
- struct dnd *dnd = data;
+ if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
+ abort();
- /* FIXME: Need to correlate this with the offer event.
- * Problem is, we don't know when we've seen that last offer
- * event, and we might need to look at all of them before we
- * can decide which one to go with. */
- wl_drag_accept(drag, "text/plain");
- dnd->drag_type = "text/plain";
+ close(fd);
}
static void
-drag_target(void *data,
- struct wl_drag *drag, const char *mime_type)
+data_source_cancelled(void *data, struct wl_data_source *source)
{
- struct dnd *dnd = data;
- struct input *input;
- struct wl_input_device *device;
+ struct dnd_drag *dnd_drag = data;
- input = wl_drag_get_user_data(drag);
- device = input_get_input_device(input);
- wl_input_device_attach(device, dnd->buffer,
- dnd->hotspot_x, dnd->hotspot_y);
-}
+ /* The 'cancelled' event means that the source is no longer in
+ * use by the drag (or current selection). We need to clean
+ * up the drag object created and the local state. */
-static gboolean
-drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
-{
- struct dnd *dnd = data;
- char buffer[256];
- int fd;
- unsigned int len;
- GError *err = NULL;
-
- g_io_channel_read_chars(source, buffer, sizeof buffer, &len, &err);
- fprintf(stderr, "read %d bytes: %s\n", len, buffer);
- fd = g_io_channel_unix_get_fd(source);
- close(fd);
- g_source_remove(dnd->tag);
+ wl_data_source_destroy(dnd_drag->data_source);
+
+ /* Destroy the item that has been dragged out */
+ cairo_surface_destroy(dnd_drag->item->surface);
+ free(dnd_drag->item);
- g_io_channel_unref(source);
+ wl_surface_destroy(dnd_drag->drag_surface);
- return TRUE;
+ cairo_surface_destroy(dnd_drag->translucent);
+ cairo_surface_destroy(dnd_drag->opaque);
+ free(dnd_drag);
}
-static void
-drag_drop(void *data, struct wl_drag *drag)
+static const struct wl_data_source_listener data_source_listener = {
+ data_source_target,
+ data_source_send,
+ data_source_cancelled
+};
+
+static cairo_surface_t *
+create_drag_cursor(struct dnd_drag *dnd_drag,
+ struct item *item, int32_t x, int32_t y, double opacity)
{
- struct dnd *dnd = data;
- int p[2];
- GIOChannel *channel;
+ struct dnd *dnd = dnd_drag->dnd;
+ cairo_surface_t *surface;
+ struct wl_cursor_image *pointer;
+ struct rectangle rectangle;
+ cairo_pattern_t *pattern;
+ cairo_t *cr;
- if (!dnd->drag_type) {
- fprintf(stderr, "got 'drop', but no target\n");
- return;
+ pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
+ if (!pointer) {
+ fprintf(stderr, "WARNING: grabbing cursor image not found\n");
+ pointer = display_get_pointer_image(dnd->display,
+ CURSOR_LEFT_PTR);
+ assert(pointer && "no cursor image found");
}
- fprintf(stderr, "got 'drop', sending write end of pipe\n");
+ rectangle.width = item_width + 2 * pointer->width;
+ rectangle.height = item_height + 2 * pointer->height;
+ surface = display_create_surface(dnd->display, NULL, &rectangle,
+ SURFACE_SHM);
- pipe(p);
- wl_drag_receive(drag, p[1]);
- close(p[1]);
+ cr = cairo_create(surface);
+ cairo_translate(cr, pointer->width, pointer->height);
- channel = g_io_channel_unix_new(p[0]);
- dnd->tag = g_io_add_watch(channel, G_IO_IN, drop_io_func, dnd);
-}
+ cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
+ cairo_set_source_rgba(cr, 0, 0, 0, 0);
+ cairo_paint(cr);
-static void
-drag_finish(void *data, struct wl_drag *drag, int fd)
-{
- char text[] = "[drop data]";
+ cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
+ cairo_set_source_surface(cr, item->surface, 0, 0);
+ pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
+ cairo_mask(cr, pattern);
+ cairo_pattern_destroy(pattern);
- fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
+ /* FIXME: more cairo-gl brokeness */
+ surface_flush_device(surface);
+ cairo_destroy(cr);
- write(fd, text, sizeof text);
- close(fd);
-}
+ dnd_drag->hotspot_x = pointer->width + x - item->x;
+ dnd_drag->hotspot_y = pointer->height + y - item->y;
+ dnd_drag->width = rectangle.width;
+ dnd_drag->height = rectangle.height;
-static const struct wl_drag_listener drag_listener = {
- drag_handle_device,
- drag_pointer_focus,
- drag_offer,
- drag_motion,
- drag_target,
- drag_drop,
- drag_finish
-};
+ return surface;
+}
static void
-dnd_button_handler(struct window *window,
+dnd_button_handler(struct widget *widget,
struct input *input, uint32_t time,
- int button, int state, void *data)
+ uint32_t button, enum wl_pointer_button_state state,
+ void *data)
{
struct dnd *dnd = data;
- int32_t x, y, hotspot_x, hotspot_y, pointer_width, pointer_height;
- struct rectangle rectangle;
+ int32_t x, y;
struct item *item;
- cairo_surface_t *surface, *pointer;
- cairo_t *cr;
+ struct rectangle allocation;
+ struct dnd_drag *dnd_drag;
+ struct display *display;
+ struct wl_compositor *compositor;
+ struct wl_buffer *buffer;
+ unsigned int i;
+ uint32_t serial;
+ cairo_surface_t *icon;
+ widget_get_allocation(dnd->widget, &allocation);
input_get_position(input, &x, &y);
- window_get_child_rectangle(dnd->window, &rectangle);
-
- x -= rectangle.x;
- y -= rectangle.y;
item = dnd_get_item(dnd, x, y);
+ x -= allocation.x;
+ y -= allocation.y;
+
+ if (item && state == WL_POINTER_BUTTON_STATE_PRESSED) {
+ dnd_drag = malloc(sizeof *dnd_drag);
+ dnd_drag->dnd = dnd;
+ dnd_drag->input = input;
+ dnd_drag->time = time;
+ dnd_drag->item = item;
+ dnd_drag->x_offset = x - item->x;
+ dnd_drag->y_offset = y - item->y;
+
+ for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
+ if (item == dnd->items[i]){
+ dnd->items[i] = 0;
+ break;
+ }
+ }
+
+ display = window_get_display(dnd->window);
+ compositor = display_get_compositor(display);
+ serial = display_get_serial(display);
+ dnd_drag->drag_surface =
+ wl_compositor_create_surface(compositor);
+
+ input_ungrab(input);
+
+ if (dnd->self_only) {
+ dnd_drag->data_source = NULL;
+ } else {
+ dnd_drag->data_source =
+ display_create_data_source(dnd->display);
+ wl_data_source_add_listener(dnd_drag->data_source,
+ &data_source_listener,
+ dnd_drag);
+ wl_data_source_offer(dnd_drag->data_source,
+ "application/x-wayland-dnd-flower");
+ wl_data_source_offer(dnd_drag->data_source,
+ "text/plain; charset=utf-8");
+ }
+
+ wl_data_device_start_drag(input_get_data_device(input),
+ dnd_drag->data_source,
+ window_get_wl_surface(dnd->window),
+ dnd_drag->drag_surface,
+ serial);
+
+ input_set_pointer_image(input, CURSOR_DRAGGING);
+
+ dnd_drag->opaque =
+ create_drag_cursor(dnd_drag, item, x, y, 1);
+ dnd_drag->translucent =
+ create_drag_cursor(dnd_drag, item, x, y, 0.2);
+
+ if (dnd->self_only)
+ icon = dnd_drag->opaque;
+ else
+ icon = dnd_drag->translucent;
- if (item && state == 1) {
- fprintf(stderr, "start drag, item %p\n", item);
+ buffer = display_get_buffer_for_surface(dnd->display, icon);
+ wl_surface_attach(dnd_drag->drag_surface, buffer,
+ -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
+ wl_surface_damage(dnd_drag->drag_surface, 0, 0,
+ dnd_drag->width, dnd_drag->height);
+ wl_surface_commit(dnd_drag->drag_surface);
- pointer = display_get_pointer_surface(dnd->display,
- POINTER_DRAGGING,
- &pointer_width,
- &pointer_height,
- &hotspot_x,
- &hotspot_y);
+ dnd->current_drag = dnd_drag;
+ window_schedule_redraw(dnd->window);
+ }
+}
- rectangle.width = item_width + 2 * pointer_width;
- rectangle.height = item_height + 2 * pointer_height;
- surface = display_create_surface(dnd->display, &rectangle);
+static int
+lookup_cursor(struct dnd *dnd, int x, int y)
+{
+ struct item *item;
- cr = cairo_create(surface);
- cairo_translate(cr, pointer_width, pointer_height);
+ item = dnd_get_item(dnd, x, y);
+ if (item)
+ return CURSOR_HAND1;
+ else
+ return CURSOR_LEFT_PTR;
+}
- cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
- cairo_set_source_rgba(cr, 0, 0, 0, 0);
- cairo_paint(cr);
+static int
+dnd_enter_handler(struct widget *widget,
+ struct input *input, float x, float y, void *data)
+{
+ struct dnd *dnd = data;
- cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
- cairo_set_source_surface(cr, item->surface, 0, 0);
- cairo_paint(cr);
+ dnd->current_drag = NULL;
- cairo_set_source_surface(cr, pointer,
- x - item->x - hotspot_x,
- y - item->y - hotspot_y);
- cairo_surface_destroy(pointer);
- cairo_paint(cr);
- cairo_destroy(cr);
+ return lookup_cursor(dnd, x, y);
+}
- dnd->buffer = display_get_buffer_for_surface(dnd->display,
- surface);
- dnd->hotspot_x = pointer_width + x - item->x;
- dnd->hotspot_y = pointer_height + y - item->y;
+static int
+dnd_motion_handler(struct widget *widget,
+ struct input *input, uint32_t time,
+ float x, float y, void *data)
+{
+ return lookup_cursor(data, x, y);
+}
- window_start_drag(window, input, time);
+static void
+dnd_data_handler(struct window *window,
+ struct input *input,
+ float x, float y, const char **types, void *data)
+{
+ struct dnd *dnd = data;
+
+ if (!types)
+ return;
- /* FIXME: We leak the surface because we can't free it
- * until the server has referenced it. */
+ if (dnd_get_item(dnd, x, y) || dnd->self_only) {
+ input_accept(input, NULL);
+ } else {
+ input_accept(input, types[0]);
}
}
-static int
-dnd_motion_handler(struct window *window,
- struct input *input, uint32_t time,
- int32_t x, int32_t y,
- int32_t sx, int32_t sy, void *data)
+static void
+dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
{
- struct dnd *dnd = data;
+ struct dnd *dnd = user_data;
+ struct dnd_flower_message *message = data;
struct item *item;
- struct rectangle rectangle;
+ struct rectangle allocation;
- window_get_child_rectangle(dnd->window, &rectangle);
- item = dnd_get_item(dnd, sx - rectangle.x, sy - rectangle.y);
+ if (len == 0) {
+ return;
+ } else if (len != sizeof *message) {
+ fprintf(stderr, "odd message length %zu, expected %zu\n",
+ len, sizeof *message);
+ return;
+ }
+
+ widget_get_allocation(dnd->widget, &allocation);
+ item = item_create(dnd->display,
+ x - message->x_offset - allocation.x,
+ y - message->y_offset - allocation.y,
+ message->seed);
+
+ dnd_add_item(dnd, item);
+ window_schedule_redraw(dnd->window);
+}
- if (item)
- return POINTER_HAND1;
- else
- return POINTER_LEFT_PTR;
+static void
+dnd_drop_handler(struct window *window, struct input *input,
+ int32_t x, int32_t y, void *data)
+{
+ struct dnd *dnd = data;
+ struct dnd_flower_message message;
+
+ if (dnd_get_item(dnd, x, y)) {
+ fprintf(stderr, "got 'drop', but no target\n");
+ return;
+ }
+
+ if (!dnd->self_only) {
+ input_receive_drag_data(input,
+ "application/x-wayland-dnd-flower",
+ dnd_receive_func, dnd);
+ } else if (dnd->current_drag) {
+ message.seed = dnd->current_drag->item->seed;
+ message.x_offset = dnd->current_drag->x_offset;
+ message.y_offset = dnd->current_drag->y_offset;
+ dnd_receive_func(&message, sizeof message, x, y, dnd);
+ dnd->current_drag = NULL;
+ } else {
+ fprintf(stderr, "ignoring drop from another client\n");
+ }
}
static struct dnd *
dnd_create(struct display *display)
{
struct dnd *dnd;
- gchar *title;
- int i, x, y;
- struct rectangle rectangle;
+ int x, y;
+ int32_t width, height;
+ unsigned int i;
dnd = malloc(sizeof *dnd);
if (dnd == NULL)
return dnd;
memset(dnd, 0, sizeof *dnd);
- title = g_strdup_printf("Wayland Drag and Drop Demo");
+ dnd->window = window_create(display);
+ dnd->widget = frame_create(dnd->window, dnd);
+ window_set_title(dnd->window, "Wayland Drag and Drop Demo");
- dnd->window = window_create(display, title, 100, 100, 500, 400);
dnd->display = display;
dnd->key = 100;
x = (i % 4) * (item_width + item_padding) + item_padding;
y = (i / 4) * (item_height + item_padding) + item_padding;
if ((i ^ (i >> 2)) & 1)
- dnd->items[i] = item_create(display, x, y);
+ dnd->items[i] = item_create(display, x, y, 0);
else
dnd->items[i] = NULL;
}
window_set_user_data(dnd->window, dnd);
- window_set_redraw_handler(dnd->window, redraw_handler);
window_set_keyboard_focus_handler(dnd->window,
keyboard_focus_handler);
- window_set_button_handler(dnd->window,
- dnd_button_handler);
-
- window_set_motion_handler(dnd->window,
- dnd_motion_handler);
+ window_set_data_handler(dnd->window, dnd_data_handler);
+ window_set_drop_handler(dnd->window, dnd_drop_handler);
- rectangle.width = 4 * (item_width + item_padding) + item_padding;
- rectangle.height = 4 * (item_height + item_padding) + item_padding;
- window_set_child_size(dnd->window, &rectangle);
+ widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
+ widget_set_enter_handler(dnd->widget, dnd_enter_handler);
+ widget_set_motion_handler(dnd->widget, dnd_motion_handler);
+ widget_set_button_handler(dnd->widget, dnd_button_handler);
- display_add_drag_listener(display, &drag_listener, dnd);
+ width = 4 * (item_width + item_padding) + item_padding;
+ height = 4 * (item_height + item_padding) + item_padding;
- dnd_draw(dnd);
+ frame_set_child_size(dnd->widget, width, height);
return dnd;
}
-static const GOptionEntry option_entries[] = {
- { NULL }
-};
-
int
main(int argc, char *argv[])
{
struct display *d;
struct dnd *dnd;
- struct timeval tv;
+ int i;
- gettimeofday(&tv, NULL);
- srandom(tv.tv_usec);
+ d = display_create(argc, argv);
+ if (d == NULL) {
+ fprintf(stderr, "failed to create display: %m\n");
+ return -1;
+ }
- d = display_create(&argc, &argv, option_entries);
+ dnd = dnd_create(d);
- dnd = dnd_create (d);
+ for (i = 1; i < argc; i++)
+ if (strcmp("--self-only", argv[i]) == 0)
+ dnd->self_only = 1;
display_run(d);