[PATCH] Made the dnd client actually work

= =
Sat Nov 27 15:41:42 PST 2010


---
 clients/dnd.c    |  108 ++++++++++++++++++++++++++++++++++++++++++++---------
 clients/window.c |    5 ++-
 2 files changed, 93 insertions(+), 20 deletions(-)

diff --git a/clients/dnd.c b/clients/dnd.c
index dedf353..fdbd79e 100644
--- a/clients/dnd.c
+++ b/clients/dnd.c
@@ -53,6 +53,9 @@ struct dnd_drag {
 	struct dnd *dnd;
 	struct input *input;
 	uint32_t time;
+	struct item *item;
+	int x_offset, y_offset;
+	const char *mime_type;
 };
 
 struct dnd_offer {
@@ -60,20 +63,39 @@ struct dnd_offer {
 	struct wl_array types;
 	const char *drag_type;
 	uint32_t tag;
+	int x, y;
 };
 
 struct item {
 	cairo_surface_t *surface;
+	int seed;
 	int x, y;
 };
 
+struct 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;
@@ -85,11 +107,7 @@ item_create(struct display *display, int x, int y)
 	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;
@@ -205,6 +223,17 @@ keyboard_focus_handler(struct window *window,
 
 	window_schedule_redraw(dnd->window);
 }
+static int
+dnd_add_item(struct dnd *dnd, struct item *item){
+	int i;
+	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)
@@ -241,6 +270,7 @@ drag_target(void *data,
 
 	fprintf(stderr, "target %s\n", mime_type);
 	device = input_get_input_device(dnd_drag->input);
+	dnd_drag->mime_type = mime_type;
 	if (mime_type)
 		surface = dnd_drag->opaque;
 	else
@@ -255,11 +285,28 @@ static void
 drag_finish(void *data, struct wl_drag *drag, int fd)
 {
 	struct dnd_drag *dnd_drag = data;
-	char text[] = "[drop data]";
+
+	if (!dnd_drag->mime_type){
+		
+		dnd_add_item(dnd_drag->dnd, dnd_drag->item);
+		return;
+	}
+	
+	struct message *message;
+	message = malloc(sizeof *message);
+	if (message == NULL)
+		return;
+	
+	
+	
+	message->seed = dnd_drag->item->seed;
+
+	message->x_offset = dnd_drag->x_offset;
+	message->y_offset = dnd_drag->y_offset;
 
 	fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
 
-	write(fd, text, sizeof text);
+	write(fd, message, sizeof *message);
 	close(fd);
 
 	/* The 'finish' event marks the end of the session on the drag
@@ -269,6 +316,7 @@ drag_finish(void *data, struct wl_drag *drag, int fd)
 	cairo_surface_destroy(dnd_drag->translucent);
 	cairo_surface_destroy(dnd_drag->opaque);
 	free(dnd_drag);
+	free(message);
 }
 
 static const struct wl_drag_listener drag_listener = {
@@ -305,8 +353,6 @@ drag_offer_pointer_focus(void *data,
 	 * 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;
 	}
@@ -335,13 +381,14 @@ drag_offer_motion(void *data,
 		  int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
 {
 	struct dnd_offer *dnd_offer = data;
-	struct dnd *dnd = dnd_offer->dnd;
 
-	if (!dnd_get_item(dnd, surface_x, surface_y)) {
+	if (!dnd_get_item(dnd_offer->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";
+		dnd_offer->x = surface_x;
+		dnd_offer->y = surface_y;
 	} else {
 		fprintf(stderr, "drag offer motion %d, %d, declining\n",
 			surface_x, surface_y);
@@ -354,19 +401,34 @@ static gboolean
 drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
 {
 	struct dnd_offer *dnd_offer = data;
+	struct dnd *dnd = dnd_offer->dnd;
 	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);
+	len = read(fd, buffer, sizeof buffer);
+	fprintf(stderr, "read %d bytes\n", len);
+	
 	close(fd);
 	g_source_remove(dnd_offer->tag);
 
 	g_io_channel_unref(source);
+	
+	struct message *message = (struct message *)buffer;
+
+	
+	struct item *item = item_create(dnd->display, 
+		dnd_offer->x-message->x_offset-26, 
+		dnd_offer->y-message->y_offset-66, 
+		message->seed);
+
+	dnd_add_item(dnd, item);
+	window_schedule_redraw(dnd->window);
 
+	wl_array_release(&dnd_offer->types);
+	free(dnd_offer);
+	
 	return TRUE;
 }
 
@@ -491,6 +553,17 @@ dnd_button_handler(struct window *window,
 		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;
+		
+		int i;
+		for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
+			if (item == dnd->items[i]){
+				dnd->items[i] = 0;
+				break;
+			}
+		}
 
 		dnd_drag->opaque =
 			create_drag_cursor(dnd_drag, item, x, y, 1);
@@ -499,6 +572,7 @@ dnd_button_handler(struct window *window,
 
 		window_start_drag(window, input, time,
 				  &drag_listener, dnd_drag);
+		window_schedule_redraw(dnd->window);
 	}
 }
 
@@ -542,7 +616,7 @@ dnd_create(struct display *display)
 		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;
 	}
@@ -575,10 +649,6 @@ main(int argc, char *argv[])
 {
 	struct display *d;
 	struct dnd *dnd;
-	struct timeval tv;
-
-	gettimeofday(&tv, NULL);
-	srandom(tv.tv_usec);
 
 	d = display_create(&argc, &argv, option_entries);
 	if (d == NULL) {
diff --git a/clients/window.c b/clients/window.c
index bc76937..58b14a2 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -1018,8 +1018,11 @@ handle_configure(void *data, struct wl_shell *shell,
 	window->pending_allocation.width = width;
 	window->pending_allocation.height = height;
 
-	if (!(edges & 15))
+	if (!(edges & 15)){
+		window->allocation.x = window->pending_allocation.x;
+		window->allocation.y = window->pending_allocation.y;
 		return;
+	}
 
 	if (window->resize_handler)
 		(*window->resize_handler)(window,
-- 
1.7.3.2


--------------010904060001050300090402--


More information about the wayland-devel mailing list