Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 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> |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 32 | #include <sys/epoll.h> |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 33 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 34 | #include <wayland-client.h> |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 35 | |
| 36 | #include "window.h" |
Benjamin Franzke | 47eb8f4 | 2011-10-07 09:08:56 +0200 | [diff] [blame] | 37 | #include "cairo-util.h" |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 38 | |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 39 | struct dnd { |
| 40 | struct window *window; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 41 | struct widget *widget; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 42 | struct display *display; |
| 43 | uint32_t key; |
| 44 | struct item *items[16]; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 45 | }; |
Kristian Høgsberg | 1d7ffd3 | 2010-08-25 16:34:05 -0400 | [diff] [blame] | 46 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 47 | struct dnd_drag { |
| 48 | cairo_surface_t *translucent; |
| 49 | cairo_surface_t *opaque; |
Kristian Høgsberg | 1d7ffd3 | 2010-08-25 16:34:05 -0400 | [diff] [blame] | 50 | int hotspot_x, hotspot_y; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 51 | struct dnd *dnd; |
| 52 | struct input *input; |
Kristian Høgsberg | ce457ba | 2010-09-14 15:39:45 -0400 | [diff] [blame] | 53 | uint32_t time; |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 54 | struct item *item; |
| 55 | int x_offset, y_offset; |
| 56 | const char *mime_type; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 57 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 58 | struct wl_data_source *data_source; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | struct item { |
| 62 | cairo_surface_t *surface; |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 63 | int seed; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 64 | int x, y; |
| 65 | }; |
| 66 | |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 67 | struct dnd_flower_message { |
| 68 | int seed, x_offset, y_offset; |
| 69 | }; |
| 70 | |
| 71 | |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 72 | static const int item_width = 64; |
| 73 | static const int item_height = 64; |
| 74 | static const int item_padding = 16; |
| 75 | |
| 76 | static struct item * |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 77 | item_create(struct display *display, int x, int y, int seed) |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 78 | { |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 79 | struct item *item; |
| 80 | struct timeval tv; |
| 81 | |
| 82 | item = malloc(sizeof *item); |
| 83 | if (item == NULL) |
| 84 | return NULL; |
| 85 | |
| 86 | |
| 87 | gettimeofday(&tv, NULL); |
| 88 | item->seed = seed ? seed : tv.tv_usec; |
| 89 | srandom(item->seed); |
| 90 | |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 91 | const int petal_count = 3 + random() % 5; |
| 92 | const double r1 = 20 + random() % 10; |
| 93 | const double r2 = 5 + random() % 12; |
| 94 | const double u = (10 + random() % 90) / 100.0; |
| 95 | const double v = (random() % 90) / 100.0; |
| 96 | |
| 97 | cairo_t *cr; |
| 98 | int i; |
| 99 | double t, dt = 2 * M_PI / (petal_count * 2); |
| 100 | double x1, y1, x2, y2, x3, y3; |
| 101 | struct rectangle rect; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 102 | |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 103 | |
| 104 | rect.width = item_width; |
| 105 | rect.height = item_height; |
Kristian Høgsberg | 3be87d1 | 2011-05-13 13:45:17 -0400 | [diff] [blame] | 106 | item->surface = display_create_surface(display, NULL, &rect, 0); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 107 | |
| 108 | item->x = x; |
| 109 | item->y = y; |
| 110 | |
| 111 | cr = cairo_create(item->surface); |
| 112 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 113 | cairo_set_source_rgba(cr, 0, 0, 0, 0); |
| 114 | cairo_paint(cr); |
| 115 | |
| 116 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 117 | cairo_translate(cr, item_width / 2, item_height / 2); |
| 118 | t = random(); |
| 119 | cairo_move_to(cr, cos(t) * r1, sin(t) * r1); |
| 120 | for (i = 0; i < petal_count; i++, t += dt * 2) { |
| 121 | x1 = cos(t) * r1; |
| 122 | y1 = sin(t) * r1; |
| 123 | x2 = cos(t + dt) * r2; |
| 124 | y2 = sin(t + dt) * r2; |
| 125 | x3 = cos(t + 2 * dt) * r1; |
| 126 | y3 = sin(t + 2 * dt) * r1; |
| 127 | |
| 128 | cairo_curve_to(cr, |
| 129 | x1 - y1 * u, y1 + x1 * u, |
| 130 | x2 + y2 * v, y2 - x2 * v, |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 131 | x2, y2); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 132 | |
| 133 | cairo_curve_to(cr, |
| 134 | x2 - y2 * v, y2 + x2 * v, |
| 135 | x3 + y3 * u, y3 - x3 * u, |
| 136 | x3, y3); |
| 137 | } |
| 138 | |
| 139 | cairo_close_path(cr); |
| 140 | |
| 141 | cairo_set_source_rgba(cr, |
| 142 | 0.5 + (random() % 50) / 49.0, |
| 143 | 0.5 + (random() % 50) / 49.0, |
| 144 | 0.5 + (random() % 50) / 49.0, |
| 145 | 0.5 + (random() % 100) / 99.0); |
| 146 | |
| 147 | cairo_fill_preserve(cr); |
| 148 | |
| 149 | cairo_set_line_width(cr, 1); |
| 150 | cairo_set_source_rgba(cr, |
| 151 | 0.5 + (random() % 50) / 49.0, |
| 152 | 0.5 + (random() % 50) / 49.0, |
| 153 | 0.5 + (random() % 50) / 49.0, |
| 154 | 0.5 + (random() % 100) / 99.0); |
| 155 | cairo_stroke(cr); |
| 156 | |
| 157 | cairo_destroy(cr); |
| 158 | |
| 159 | return item; |
| 160 | } |
| 161 | |
| 162 | static void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 163 | dnd_redraw_handler(struct widget *widget, void *data) |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 164 | { |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 165 | struct dnd *dnd = data; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 166 | struct rectangle allocation; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 167 | cairo_t *cr; |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 168 | cairo_surface_t *surface; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 169 | int i; |
| 170 | |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 171 | surface = window_get_surface(dnd->window); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 172 | cr = cairo_create(surface); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 173 | widget_get_allocation(dnd->widget, &allocation); |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 174 | cairo_rectangle(cr, allocation.x, allocation.y, |
| 175 | allocation.width, allocation.height); |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 176 | |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 177 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 178 | cairo_set_source_rgba(cr, 0, 0, 0, 0.8); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 179 | cairo_fill(cr); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 180 | |
| 181 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 182 | for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) { |
| 183 | if (!dnd->items[i]) |
| 184 | continue; |
| 185 | cairo_set_source_surface(cr, dnd->items[i]->surface, |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 186 | dnd->items[i]->x + allocation.x, |
| 187 | dnd->items[i]->y + allocation.y); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 188 | cairo_paint(cr); |
| 189 | } |
| 190 | |
| 191 | cairo_destroy(cr); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 192 | cairo_surface_destroy(surface); |
| 193 | } |
| 194 | |
| 195 | static void |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 196 | keyboard_focus_handler(struct window *window, |
| 197 | struct input *device, void *data) |
| 198 | { |
| 199 | struct dnd *dnd = data; |
| 200 | |
| 201 | window_schedule_redraw(dnd->window); |
| 202 | } |
| 203 | |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 204 | static int |
| 205 | dnd_add_item(struct dnd *dnd, struct item *item) |
| 206 | { |
| 207 | int i; |
| 208 | |
| 209 | for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) { |
| 210 | if (dnd->items[i] == 0) { |
| 211 | dnd->items[i] = item; |
| 212 | return i; |
| 213 | } |
| 214 | } |
| 215 | return -1; |
| 216 | } |
| 217 | |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 218 | static struct item * |
| 219 | dnd_get_item(struct dnd *dnd, int32_t x, int32_t y) |
| 220 | { |
| 221 | struct item *item; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 222 | struct rectangle allocation; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 223 | int i; |
| 224 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 225 | widget_get_allocation(dnd->widget, &allocation); |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 226 | |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 227 | x -= allocation.x; |
| 228 | y -= allocation.y; |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 229 | |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 230 | for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) { |
| 231 | item = dnd->items[i]; |
| 232 | if (item && |
| 233 | item->x <= x && x < item->x + item_width && |
| 234 | item->y <= y && y < item->y + item_height) |
| 235 | return item; |
| 236 | } |
| 237 | |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | static void |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 242 | data_source_target(void *data, |
| 243 | struct wl_data_source *source, const char *mime_type) |
Kristian Høgsberg | 506e20e | 2010-08-19 17:26:02 -0400 | [diff] [blame] | 244 | { |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 245 | struct dnd_drag *dnd_drag = data; |
| 246 | struct dnd *dnd = dnd_drag->dnd; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 247 | cairo_surface_t *surface; |
| 248 | struct wl_buffer *buffer; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 249 | struct wl_data_device *device; |
Kristian Høgsberg | 1d7ffd3 | 2010-08-25 16:34:05 -0400 | [diff] [blame] | 250 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 251 | device = input_get_data_device(dnd_drag->input); |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 252 | dnd_drag->mime_type = mime_type; |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 253 | if (mime_type) |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 254 | surface = dnd_drag->opaque; |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 255 | else |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 256 | surface = dnd_drag->translucent; |
| 257 | |
| 258 | buffer = display_get_buffer_for_surface(dnd->display, surface); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 259 | wl_data_device_attach(device, dnd_drag->time, buffer, |
| 260 | dnd_drag->hotspot_x, dnd_drag->hotspot_y); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static void |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 264 | data_source_send(void *data, struct wl_data_source *source, |
| 265 | const char *mime_type, int32_t fd) |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 266 | { |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 267 | struct dnd_flower_message dnd_flower_message; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 268 | struct dnd_drag *dnd_drag = data; |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 269 | |
| 270 | dnd_flower_message.seed = dnd_drag->item->seed; |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 271 | dnd_flower_message.x_offset = dnd_drag->x_offset; |
| 272 | dnd_flower_message.y_offset = dnd_drag->y_offset; |
| 273 | |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 274 | write(fd, &dnd_flower_message, sizeof dnd_flower_message); |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 275 | close(fd); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 276 | } |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 277 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 278 | static void |
| 279 | data_source_cancelled(void *data, struct wl_data_source *source) |
| 280 | { |
| 281 | struct dnd_drag *dnd_drag = data; |
| 282 | |
| 283 | /* The 'cancelled' event means that the source is no longer in |
| 284 | * use by the drag (or current selection). We need to clean |
| 285 | * up the drag object created and the local state. */ |
| 286 | |
| 287 | wl_data_source_destroy(dnd_drag->data_source); |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 288 | |
| 289 | /* Destroy the item that has been dragged out */ |
| 290 | cairo_surface_destroy(dnd_drag->item->surface); |
| 291 | free(dnd_drag->item); |
| 292 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 293 | cairo_surface_destroy(dnd_drag->translucent); |
| 294 | cairo_surface_destroy(dnd_drag->opaque); |
| 295 | free(dnd_drag); |
| 296 | } |
| 297 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 298 | static const struct wl_data_source_listener data_source_listener = { |
| 299 | data_source_target, |
| 300 | data_source_send, |
| 301 | data_source_cancelled |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 302 | }; |
| 303 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 304 | static cairo_surface_t * |
| 305 | create_drag_cursor(struct dnd_drag *dnd_drag, |
| 306 | struct item *item, int32_t x, int32_t y, double opacity) |
| 307 | { |
| 308 | struct dnd *dnd = dnd_drag->dnd; |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 309 | cairo_surface_t *surface, *pointer; |
| 310 | int32_t pointer_width, pointer_height, hotspot_x, hotspot_y; |
| 311 | struct rectangle rectangle; |
| 312 | cairo_pattern_t *pattern; |
| 313 | cairo_t *cr; |
| 314 | |
| 315 | pointer = display_get_pointer_surface(dnd->display, |
| 316 | POINTER_DRAGGING, |
| 317 | &pointer_width, |
| 318 | &pointer_height, |
| 319 | &hotspot_x, |
| 320 | &hotspot_y); |
| 321 | |
| 322 | rectangle.width = item_width + 2 * pointer_width; |
| 323 | rectangle.height = item_height + 2 * pointer_height; |
Kristian Høgsberg | 3be87d1 | 2011-05-13 13:45:17 -0400 | [diff] [blame] | 324 | surface = display_create_surface(dnd->display, NULL, &rectangle, 0); |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 325 | |
| 326 | cr = cairo_create(surface); |
| 327 | cairo_translate(cr, pointer_width, pointer_height); |
| 328 | |
| 329 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 330 | cairo_set_source_rgba(cr, 0, 0, 0, 0); |
| 331 | cairo_paint(cr); |
| 332 | |
| 333 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 334 | cairo_set_source_surface(cr, item->surface, 0, 0); |
| 335 | pattern = cairo_pattern_create_rgba(0, 0, 0, opacity); |
| 336 | cairo_mask(cr, pattern); |
| 337 | cairo_pattern_destroy(pattern); |
| 338 | |
| 339 | cairo_set_source_surface(cr, pointer, |
| 340 | x - item->x - hotspot_x, |
| 341 | y - item->y - hotspot_y); |
| 342 | cairo_surface_destroy(pointer); |
| 343 | cairo_paint(cr); |
| 344 | /* FIXME: more cairo-gl brokeness */ |
Benjamin Franzke | 47eb8f4 | 2011-10-07 09:08:56 +0200 | [diff] [blame] | 345 | surface_flush_device(surface); |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 346 | cairo_destroy(cr); |
| 347 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 348 | dnd_drag->hotspot_x = pointer_width + x - item->x; |
| 349 | dnd_drag->hotspot_y = pointer_height + y - item->y; |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 350 | |
| 351 | return surface; |
| 352 | } |
| 353 | |
Kristian Høgsberg | 506e20e | 2010-08-19 17:26:02 -0400 | [diff] [blame] | 354 | static void |
Kristian Høgsberg | a8a0db3 | 2012-01-09 11:12:05 -0500 | [diff] [blame] | 355 | dnd_button_handler(struct widget *widget, |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 356 | struct input *input, uint32_t time, |
| 357 | int button, int state, void *data) |
| 358 | { |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 359 | struct dnd *dnd = data; |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 360 | int32_t x, y; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 361 | struct item *item; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 362 | struct rectangle allocation; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 363 | struct dnd_drag *dnd_drag; |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 364 | int i; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 365 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 366 | widget_get_allocation(dnd->widget, &allocation); |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 367 | input_get_position(input, &x, &y); |
| 368 | item = dnd_get_item(dnd, x, y); |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 369 | x -= allocation.x; |
| 370 | y -= allocation.y; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 371 | |
| 372 | if (item && state == 1) { |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 373 | dnd_drag = malloc(sizeof *dnd_drag); |
| 374 | dnd_drag->dnd = dnd; |
| 375 | dnd_drag->input = input; |
Kristian Høgsberg | ce457ba | 2010-09-14 15:39:45 -0400 | [diff] [blame] | 376 | dnd_drag->time = time; |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 377 | dnd_drag->item = item; |
| 378 | dnd_drag->x_offset = x - item->x; |
| 379 | dnd_drag->y_offset = y - item->y; |
| 380 | |
| 381 | for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) { |
| 382 | if (item == dnd->items[i]){ |
| 383 | dnd->items[i] = 0; |
| 384 | break; |
| 385 | } |
| 386 | } |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 387 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 388 | dnd_drag->data_source = |
| 389 | display_create_data_source(dnd->display); |
| 390 | wl_data_source_add_listener(dnd_drag->data_source, |
| 391 | &data_source_listener, |
| 392 | dnd_drag); |
| 393 | wl_data_source_offer(dnd_drag->data_source, |
| 394 | "application/x-wayland-dnd-flower"); |
| 395 | wl_data_source_offer(dnd_drag->data_source, |
| 396 | "text/plain; charset=utf-8"); |
| 397 | wl_data_device_start_drag(input_get_data_device(input), |
| 398 | dnd_drag->data_source, |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 399 | window_get_wl_surface(dnd->window), |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 400 | time); |
| 401 | |
| 402 | input_set_pointer_image(input, time, POINTER_DRAGGING); |
| 403 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 404 | dnd_drag->opaque = |
| 405 | create_drag_cursor(dnd_drag, item, x, y, 1); |
| 406 | dnd_drag->translucent = |
| 407 | create_drag_cursor(dnd_drag, item, x, y, 0.2); |
Kristian Høgsberg | 1d7ffd3 | 2010-08-25 16:34:05 -0400 | [diff] [blame] | 408 | |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 409 | window_schedule_redraw(dnd->window); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | static int |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 414 | lookup_cursor(struct dnd *dnd, int x, int y) |
| 415 | { |
| 416 | struct item *item; |
| 417 | |
| 418 | item = dnd_get_item(dnd, x, y); |
| 419 | if (item) |
| 420 | return POINTER_HAND1; |
| 421 | else |
| 422 | return POINTER_LEFT_PTR; |
| 423 | } |
| 424 | |
Kristian Høgsberg | bb901fa | 2012-01-09 11:22:32 -0500 | [diff] [blame] | 425 | static int |
Kristian Høgsberg | ac7619f | 2012-01-09 09:26:38 -0500 | [diff] [blame] | 426 | dnd_enter_handler(struct widget *widget, |
| 427 | struct input *input, uint32_t time, |
| 428 | int32_t x, int32_t y, void *data) |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 429 | { |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 430 | return lookup_cursor(data, x, y); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static int |
Kristian Høgsberg | 5f190ef | 2012-01-09 09:44:45 -0500 | [diff] [blame] | 434 | dnd_motion_handler(struct widget *widget, |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 435 | struct input *input, uint32_t time, |
Kristian Høgsberg | 5f190ef | 2012-01-09 09:44:45 -0500 | [diff] [blame] | 436 | int32_t x, int32_t y, void *data) |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 437 | { |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 438 | return lookup_cursor(data, x, y); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static void |
| 442 | dnd_data_handler(struct window *window, |
| 443 | struct input *input, uint32_t time, |
| 444 | int32_t x, int32_t y, const char **types, void *data) |
| 445 | { |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 446 | struct dnd *dnd = data; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 447 | |
| 448 | if (!dnd_get_item(dnd, x, y)) { |
| 449 | input_accept(input, time, types[0]); |
| 450 | } else { |
| 451 | input_accept(input, time, NULL); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | static void |
| 456 | dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data) |
| 457 | { |
| 458 | struct dnd *dnd = user_data; |
| 459 | struct dnd_flower_message *message = data; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 460 | struct item *item; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 461 | struct rectangle allocation; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 462 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 463 | if (len == 0) { |
| 464 | return; |
| 465 | } else if (len != sizeof *message) { |
| 466 | fprintf(stderr, "odd message length %ld, expected %ld\n", |
| 467 | len, sizeof *message); |
| 468 | return; |
| 469 | } |
| 470 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 471 | widget_get_allocation(dnd->widget, &allocation); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 472 | item = item_create(dnd->display, |
| 473 | x - message->x_offset - allocation.x, |
| 474 | y - message->y_offset - allocation.y, |
| 475 | message->seed); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 476 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 477 | dnd_add_item(dnd, item); |
| 478 | window_schedule_redraw(dnd->window); |
| 479 | } |
| 480 | |
| 481 | static void |
| 482 | dnd_drop_handler(struct window *window, struct input *input, |
| 483 | int32_t x, int32_t y, void *data) |
| 484 | { |
| 485 | struct dnd *dnd = data; |
| 486 | |
| 487 | if (dnd_get_item(dnd, x, y)) { |
| 488 | fprintf(stderr, "got 'drop', but no target\n"); |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | input_receive_drag_data(input, |
| 493 | "application/x-wayland-dnd-flower", |
| 494 | dnd_receive_func, dnd); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static struct dnd * |
| 498 | dnd_create(struct display *display) |
| 499 | { |
| 500 | struct dnd *dnd; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 501 | int i, x, y; |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 502 | int32_t width, height; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 503 | |
| 504 | dnd = malloc(sizeof *dnd); |
| 505 | if (dnd == NULL) |
| 506 | return dnd; |
| 507 | memset(dnd, 0, sizeof *dnd); |
| 508 | |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame^] | 509 | dnd->window = window_create(display); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 510 | dnd->widget = frame_create(dnd->window, dnd); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 511 | window_set_title(dnd->window, "Wayland Drag and Drop Demo"); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 512 | |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 513 | dnd->display = display; |
| 514 | dnd->key = 100; |
| 515 | |
| 516 | for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) { |
| 517 | x = (i % 4) * (item_width + item_padding) + item_padding; |
| 518 | y = (i / 4) * (item_height + item_padding) + item_padding; |
| 519 | if ((i ^ (i >> 2)) & 1) |
Joel Teichroeb | 0c007ae | 2010-11-30 10:22:16 -0800 | [diff] [blame] | 520 | dnd->items[i] = item_create(display, x, y, 0); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 521 | else |
| 522 | dnd->items[i] = NULL; |
| 523 | } |
| 524 | |
| 525 | window_set_user_data(dnd->window, dnd); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 526 | window_set_keyboard_focus_handler(dnd->window, |
| 527 | keyboard_focus_handler); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 528 | window_set_data_handler(dnd->window, dnd_data_handler); |
| 529 | window_set_drop_handler(dnd->window, dnd_drop_handler); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 530 | |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 531 | widget_set_redraw_handler(dnd->widget, dnd_redraw_handler); |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 532 | widget_set_enter_handler(dnd->widget, dnd_enter_handler); |
| 533 | widget_set_motion_handler(dnd->widget, dnd_motion_handler); |
| 534 | widget_set_button_handler(dnd->widget, dnd_button_handler); |
Kristian Høgsberg | ac7619f | 2012-01-09 09:26:38 -0500 | [diff] [blame] | 535 | |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 536 | width = 4 * (item_width + item_padding) + item_padding; |
| 537 | height = 4 * (item_height + item_padding) + item_padding; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 538 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 539 | widget_schedule_resize(dnd->widget, width, height); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 540 | |
| 541 | return dnd; |
| 542 | } |
| 543 | |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 544 | int |
| 545 | main(int argc, char *argv[]) |
| 546 | { |
| 547 | struct display *d; |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 548 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 549 | d = display_create(&argc, &argv, NULL); |
Yuval Fledel | e9f5e36 | 2010-11-22 21:34:19 +0200 | [diff] [blame] | 550 | if (d == NULL) { |
| 551 | fprintf(stderr, "failed to create display: %m\n"); |
| 552 | return -1; |
| 553 | } |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 554 | |
Kristian Høgsberg | 0043961 | 2011-01-25 15:16:01 -0500 | [diff] [blame] | 555 | dnd_create(d); |
Kristian Høgsberg | b8cc24e | 2010-08-18 20:31:06 -0400 | [diff] [blame] | 556 | |
| 557 | display_run(d); |
| 558 | |
| 559 | return 0; |
| 560 | } |