struct item *items[16];
};
+struct dnd_drag {
+ cairo_surface_t *translucent;
+ cairo_surface_t *opaque;
+ int hotspot_x, hotspot_y;
+ struct dnd *dnd;
+ struct input *input;
+ uint32_t time;
+};
+
+struct dnd_offer {
+ struct dnd *dnd;
+ struct wl_array types;
+ const char *drag_type;
+ uint32_t tag;
+};
+
struct item {
cairo_surface_t *surface;
int x, 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,
cairo_destroy(cr);
window_copy_surface(dnd->window, &rectangle, surface);
- window_commit(dnd->window, dnd->key);
cairo_surface_destroy(surface);
+ window_flush(dnd->window);
}
static void
dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
{
struct item *item;
+ struct rectangle rectangle;
int i;
+ window_get_child_rectangle(dnd->window, &rectangle);
+
+ x -= rectangle.x;
+ y -= rectangle.y;
+
for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
item = dnd->items[i];
if (item &&
}
static void
-drag_handle_device(void *data,
- struct wl_drag *drag, struct wl_input_device *device)
+drag_target(void *data,
+ struct wl_drag *drag, const char *mime_type)
{
+ struct dnd_drag *dnd_drag = data;
+ struct dnd *dnd = dnd_drag->dnd;
+ struct wl_input_device *device;
+ cairo_surface_t *surface;
+ struct wl_buffer *buffer;
+
+ fprintf(stderr, "target %s\n", mime_type);
+ device = input_get_input_device(dnd_drag->input);
+ if (mime_type)
+ surface = dnd_drag->opaque;
+ else
+ surface = dnd_drag->translucent;
+
+ buffer = display_get_buffer_for_surface(dnd->display, surface);
+ wl_input_device_attach(device, dnd_drag->time, buffer,
+ dnd_drag->hotspot_x, dnd_drag->hotspot_y);
}
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)
+drag_finish(void *data, struct wl_drag *drag, int fd)
{
- fprintf(stderr, "drag pointer focus %p\n", surface);
+ struct dnd_drag *dnd_drag = data;
+ char text[] = "[drop data]";
+
+ fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
- wl_drag_accept(drag, "text/plain");
+ write(fd, text, sizeof text);
+ close(fd);
+
+ /* The 'finish' event marks the end of the session on the drag
+ * source side and we need to clean up the drag object created
+ * and the local state. */
+ wl_drag_destroy(drag);
+ cairo_surface_destroy(dnd_drag->translucent);
+ cairo_surface_destroy(dnd_drag->opaque);
+ free(dnd_drag);
}
+static const struct wl_drag_listener drag_listener = {
+ drag_target,
+ drag_finish
+};
+
static void
-drag_offer(void *data,
- struct wl_drag *drag, const char *type)
+drag_offer_offer(void *data,
+ struct wl_drag_offer *offer, const char *type)
{
- fprintf(stderr, "drag offer %s\n", type);
+ struct dnd_offer *dnd_offer = data;
+ char **p;
+
+ p = wl_array_add(&dnd_offer->types, sizeof *p);
+ if (p)
+ *p = strdup(type);
}
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)
+drag_offer_pointer_focus(void *data,
+ struct wl_drag_offer *offer,
+ uint32_t time, struct wl_surface *surface,
+ int32_t x, int32_t y,
+ int32_t surface_x, int32_t surface_y)
{
- fprintf(stderr, "drag motion %d,%d\n", surface_x, surface_y);
+ struct dnd_offer *dnd_offer = data;
+ struct window *window;
+ char **p, **end;
+
+ /* The last event in a dnd session is pointer_focus with a
+ * NULL surface, whether or not we get the drop event. We
+ * need to clean up the dnd_offer proxy and whatever state we
+ * allocated. */
+ if (!surface) {
+ fprintf(stderr, "pointer focus NULL, session over\n");
+ wl_array_release(&dnd_offer->types);
+ free(dnd_offer);
+ wl_drag_offer_destroy(offer);
+ return;
+ }
- /* 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");
+ fprintf(stderr, "drag pointer focus %p\n", surface);
+ fprintf(stderr, "offered types:\n");
+ end = dnd_offer->types.data + dnd_offer->types.size;
+ for (p = dnd_offer->types.data; p < end; p++)
+ fprintf(stderr, "\%s\n", *p);
+
+ window = wl_surface_get_user_data(surface);
+ dnd_offer->dnd = window_get_user_data(window);
+
+ if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
+ wl_drag_offer_accept(offer, time, "text/plain");
+ dnd_offer->drag_type = "text/plain";
+ } else {
+ wl_drag_offer_accept(offer, time, NULL);
+ dnd_offer->drag_type = NULL;
+ }
}
static void
-drag_target(void *data,
- struct wl_drag *drag, const char *mime_type)
+drag_offer_motion(void *data,
+ struct wl_drag_offer *offer, uint32_t time,
+ int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
{
- fprintf(stderr, "target %s\n", mime_type);
+ struct dnd_offer *dnd_offer = data;
+ struct dnd *dnd = dnd_offer->dnd;
+
+ if (!dnd_get_item(dnd, surface_x, surface_y)) {
+ fprintf(stderr, "drag offer motion %d, %d, accepting\n",
+ surface_x, surface_y);
+ wl_drag_offer_accept(offer, time, "text/plain");
+ dnd_offer->drag_type = "text/plain";
+ } else {
+ fprintf(stderr, "drag offer motion %d, %d, declining\n",
+ surface_x, surface_y);
+ wl_drag_offer_accept(offer, time, NULL);
+ dnd_offer->drag_type = NULL;
+ }
}
-static void
-drag_finish(void *data, struct wl_drag *drag)
+static gboolean
+drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
{
- fprintf(stderr, "drag finish\n");
- struct wl_array a;
- char text[] = "[drop data]";
+ struct dnd_offer *dnd_offer = 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_offer->tag);
- a.data = text;
- a.size = sizeof text;
+ g_io_channel_unref(source);
- wl_drag_send(drag, &a);
+ return TRUE;
}
static void
-drag_data(void *data,
- struct wl_drag *drag, struct wl_array *contents)
+drag_offer_drop(void *data, struct wl_drag_offer *offer)
{
- fprintf(stderr, "drag drop, data %s\n", contents->data);
+ struct dnd_offer *dnd_offer = data;
+ GIOChannel *channel;
+ int p[2];
+
+ if (!dnd_offer->drag_type) {
+ fprintf(stderr, "got 'drop', but no target\n");
+ /* FIXME: Should send response so compositor and
+ * source knows it's over. Can't send -1 to indicate
+ * 'no target' though becauses of the way fd passing
+ * currently works. */
+ return;
+ }
+
+ fprintf(stderr, "got 'drop', sending write end of pipe\n");
+
+ pipe(p);
+ wl_drag_offer_receive(offer, p[1]);
+ close(p[1]);
+
+ channel = g_io_channel_unix_new(p[0]);
+ dnd_offer->tag = g_io_add_watch(channel, G_IO_IN,
+ drop_io_func, dnd_offer);
}
-static const struct wl_drag_listener drag_listener = {
- drag_handle_device,
- drag_pointer_focus,
- drag_offer,
- drag_motion,
- drag_target,
- drag_finish,
- drag_data
+static const struct wl_drag_offer_listener drag_offer_listener = {
+ drag_offer_offer,
+ drag_offer_pointer_focus,
+ drag_offer_motion,
+ drag_offer_drop,
};
static void
+drag_offer_handler(struct wl_drag_offer *offer, struct display *display)
+{
+ struct dnd_offer *dnd_offer;
+
+ dnd_offer = malloc(sizeof *dnd_offer);
+ if (dnd_offer == NULL)
+ return;
+
+ wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
+ wl_array_init(&dnd_offer->types);
+}
+
+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 = dnd_drag->dnd;
+ cairo_surface_t *surface, *pointer;
+ int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
+ struct rectangle rectangle;
+ cairo_pattern_t *pattern;
+ cairo_t *cr;
+
+ pointer = display_get_pointer_surface(dnd->display,
+ POINTER_DRAGGING,
+ &pointer_width,
+ &pointer_height,
+ &hotspot_x,
+ &hotspot_y);
+
+ rectangle.width = item_width + 2 * pointer_width;
+ rectangle.height = item_height + 2 * pointer_height;
+ surface = display_create_surface(dnd->display, &rectangle);
+
+ cr = cairo_create(surface);
+ cairo_translate(cr, pointer_width, pointer_height);
+
+ cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
+ cairo_set_source_rgba(cr, 0, 0, 0, 0);
+ cairo_paint(cr);
+
+ 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);
+
+ cairo_set_source_surface(cr, pointer,
+ x - item->x - hotspot_x,
+ y - item->y - hotspot_y);
+ cairo_surface_destroy(pointer);
+ cairo_paint(cr);
+ /* FIXME: more cairo-gl brokeness */
+ display_flush_cairo_device(dnd->display);
+ cairo_destroy(cr);
+
+ dnd_drag->hotspot_x = pointer_width + x - item->x;
+ dnd_drag->hotspot_y = pointer_height + y - item->y;
+
+ return surface;
+}
+
+static void
dnd_button_handler(struct window *window,
struct input *input, uint32_t time,
int button, int 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;
- struct wl_buffer *buffer;
- cairo_surface_t *surface, *pointer;
- cairo_t *cr;
+ struct rectangle rectangle;
+ struct dnd_drag *dnd_drag;
- input_get_position(input, &x, &y);
window_get_child_rectangle(dnd->window, &rectangle);
-
+ input_get_position(input, &x, &y);
+ item = dnd_get_item(dnd, x, y);
x -= rectangle.x;
y -= rectangle.y;
- item = dnd_get_item(dnd, x, y);
if (item && state == 1) {
fprintf(stderr, "start drag, item %p\n", item);
- pointer = display_get_pointer_surface(dnd->display,
- POINTER_DRAGGING,
- &pointer_width,
- &pointer_height,
- &hotspot_x,
- &hotspot_y);
-
- rectangle.width = item_width + 2 * pointer_width;
- rectangle.height = item_height + 2 * pointer_height;
- surface = display_create_surface(dnd->display, &rectangle);
-
- cr = cairo_create(surface);
- cairo_translate(cr, pointer_width, pointer_height);
+ dnd_drag = malloc(sizeof *dnd_drag);
+ dnd_drag->dnd = dnd;
+ dnd_drag->input = input;
+ dnd_drag->time = time;
- cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
- cairo_set_source_rgba(cr, 0, 0, 0, 0);
- cairo_paint(cr);
-
- cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
- cairo_set_source_surface(cr, item->surface, 0, 0);
- cairo_paint(cr);
+ 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);
- 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);
-
- buffer = display_get_buffer_for_surface(dnd->display,
- surface);
window_start_drag(window, input, time,
- buffer,
- pointer_width + x - item->x,
- pointer_height + y - item->y);
-
- /* FIXME: We leak the surface because we can't free it
- * until the server has referenced it. */
+ &drag_listener, dnd_drag);
}
}
{
struct dnd *dnd = data;
struct item *item;
- struct rectangle rectangle;
- window_get_child_rectangle(dnd->window, &rectangle);
- item = dnd_get_item(dnd, sx - rectangle.x, sy - rectangle.y);
+ item = dnd_get_item(dnd, sx, sy);
if (item)
return POINTER_HAND1;
d = display_create(&argc, &argv, option_entries);
- display_add_drag_listener(d, &drag_listener, d);
+ display_set_drag_offer_handler(d, drag_offer_handler);
dnd = dnd_create (d);