U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 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 <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/mman.h> |
| 28 | |
| 29 | #include "../shared/os-compatibility.h" |
| 30 | #include "weston-test-client-helper.h" |
| 31 | |
| 32 | int |
| 33 | surface_contains(struct surface *surface, int x, int y) |
| 34 | { |
| 35 | /* test whether a global x,y point is contained in the surface */ |
| 36 | int sx = surface->x; |
| 37 | int sy = surface->y; |
| 38 | int sw = surface->width; |
| 39 | int sh = surface->height; |
| 40 | return x >= sx && y >= sy && x < sx + sw && y < sy + sh; |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | yield(struct client *client) |
| 45 | { |
| 46 | /* |
| 47 | * FIXME: ugh! how do we ensure all events have finished |
| 48 | * propagating to the client. The calls to usleep seem to do a |
| 49 | * pretty reasonable job... and without them, tests can fail |
| 50 | * intermittently. |
| 51 | */ |
| 52 | usleep(0.02 * 1e6); |
| 53 | wl_display_flush(client->wl_display); |
| 54 | wl_display_roundtrip(client->wl_display); |
| 55 | usleep(0.02 * 1e6); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | move_pointer(struct client *client, int x, int y) |
| 60 | { |
| 61 | wl_test_move_pointer(client->test->wl_test, x, y); |
| 62 | |
| 63 | yield(client); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | move_client(struct client *client, int x, int y) |
| 68 | { |
| 69 | struct surface *surface = client->surface; |
| 70 | |
| 71 | client->surface->x = x; |
| 72 | client->surface->y = y; |
| 73 | wl_test_move_surface(client->test->wl_test, surface->wl_surface, |
| 74 | surface->x, surface->y); |
| 75 | wl_surface_damage(surface->wl_surface, 0, 0, surface->width, |
| 76 | surface->height); |
| 77 | wl_surface_commit(surface->wl_surface); |
| 78 | |
| 79 | yield(client); |
| 80 | yield(client); |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, |
| 85 | uint32_t serial, struct wl_surface *wl_surface, |
| 86 | wl_fixed_t x, wl_fixed_t y) |
| 87 | { |
| 88 | struct pointer *pointer = data; |
| 89 | |
| 90 | pointer->focus = wl_surface_get_user_data(wl_surface); |
| 91 | pointer->x = wl_fixed_to_int(x); |
| 92 | pointer->y = wl_fixed_to_int(y); |
| 93 | |
| 94 | fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n", |
| 95 | pointer->x, pointer->y, pointer->focus); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, |
| 100 | uint32_t serial, struct wl_surface *wl_surface) |
| 101 | { |
| 102 | struct pointer *pointer = data; |
| 103 | |
| 104 | pointer->focus = NULL; |
| 105 | |
| 106 | fprintf(stderr, "test-client: got pointer leave, surface %p\n", |
| 107 | wl_surface_get_user_data(wl_surface)); |
| 108 | } |
| 109 | |
| 110 | static void |
| 111 | pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, |
| 112 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 113 | { |
| 114 | struct pointer *pointer = data; |
| 115 | |
| 116 | pointer->x = wl_fixed_to_int(x); |
| 117 | pointer->y = wl_fixed_to_int(y); |
| 118 | |
| 119 | fprintf(stderr, "test-client: got pointer motion %d %d\n", |
| 120 | pointer->x, pointer->y); |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | pointer_handle_button(void *data, struct wl_pointer *wl_pointer, |
| 125 | uint32_t serial, uint32_t time, uint32_t button, |
| 126 | uint32_t state) |
| 127 | { |
| 128 | struct pointer *pointer = data; |
| 129 | |
| 130 | pointer->button = button; |
| 131 | pointer->state = state; |
| 132 | |
| 133 | fprintf(stderr, "test-client: got pointer button %u %u\n", |
| 134 | button, state); |
| 135 | } |
| 136 | |
| 137 | static void |
| 138 | pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, |
| 139 | uint32_t time, uint32_t axis, wl_fixed_t value) |
| 140 | { |
| 141 | fprintf(stderr, "test-client: got pointer axis %u %d\n", axis, value); |
| 142 | } |
| 143 | |
| 144 | static const struct wl_pointer_listener pointer_listener = { |
| 145 | pointer_handle_enter, |
| 146 | pointer_handle_leave, |
| 147 | pointer_handle_motion, |
| 148 | pointer_handle_button, |
| 149 | pointer_handle_axis, |
| 150 | }; |
| 151 | |
| 152 | static void |
| 153 | keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard, |
| 154 | uint32_t format, int fd, uint32_t size) |
| 155 | { |
| 156 | close(fd); |
| 157 | |
| 158 | fprintf(stderr, "test-client: got keyboard keymap\n"); |
| 159 | } |
| 160 | |
| 161 | static void |
| 162 | keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, |
| 163 | uint32_t serial, struct wl_surface *wl_surface, |
| 164 | struct wl_array *keys) |
| 165 | { |
| 166 | struct keyboard *keyboard = data; |
| 167 | |
| 168 | keyboard->focus = wl_surface_get_user_data(wl_surface); |
| 169 | |
| 170 | fprintf(stderr, "test-client: got keyboard enter, surface %p\n", |
| 171 | keyboard->focus); |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard, |
| 176 | uint32_t serial, struct wl_surface *wl_surface) |
| 177 | { |
| 178 | struct keyboard *keyboard = data; |
| 179 | |
| 180 | keyboard->focus = NULL; |
| 181 | |
| 182 | fprintf(stderr, "test-client: got keyboard leave, surface %p\n", |
| 183 | wl_surface_get_user_data(wl_surface)); |
| 184 | } |
| 185 | |
| 186 | static void |
| 187 | keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, |
| 188 | uint32_t serial, uint32_t time, uint32_t key, |
| 189 | uint32_t state) |
| 190 | { |
| 191 | struct keyboard *keyboard = data; |
| 192 | |
| 193 | keyboard->key = key; |
| 194 | keyboard->state = state; |
| 195 | |
| 196 | fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state); |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, |
| 201 | uint32_t serial, uint32_t mods_depressed, |
| 202 | uint32_t mods_latched, uint32_t mods_locked, |
| 203 | uint32_t group) |
| 204 | { |
| 205 | struct keyboard *keyboard = data; |
| 206 | |
| 207 | keyboard->mods_depressed = mods_depressed; |
| 208 | keyboard->mods_latched = mods_latched; |
| 209 | keyboard->mods_locked = mods_locked; |
| 210 | keyboard->group = group; |
| 211 | |
| 212 | fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n", |
| 213 | mods_depressed, mods_latched, mods_locked, group); |
| 214 | } |
| 215 | |
| 216 | static const struct wl_keyboard_listener keyboard_listener = { |
| 217 | keyboard_handle_keymap, |
| 218 | keyboard_handle_enter, |
| 219 | keyboard_handle_leave, |
| 220 | keyboard_handle_key, |
| 221 | keyboard_handle_modifiers, |
| 222 | }; |
| 223 | |
| 224 | static void |
| 225 | surface_enter(void *data, |
| 226 | struct wl_surface *wl_surface, struct wl_output *output) |
| 227 | { |
| 228 | struct surface *surface = data; |
| 229 | |
| 230 | surface->output = wl_output_get_user_data(output); |
| 231 | |
| 232 | fprintf(stderr, "test-client: got surface enter output %p\n", |
| 233 | surface->output); |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | surface_leave(void *data, |
| 238 | struct wl_surface *wl_surface, struct wl_output *output) |
| 239 | { |
| 240 | struct surface *surface = data; |
| 241 | |
| 242 | surface->output = NULL; |
| 243 | |
| 244 | fprintf(stderr, "test-client: got surface leave output %p\n", |
| 245 | wl_output_get_user_data(output)); |
| 246 | } |
| 247 | |
| 248 | static const struct wl_surface_listener surface_listener = { |
| 249 | surface_enter, |
| 250 | surface_leave |
| 251 | }; |
| 252 | |
| 253 | static void |
| 254 | create_shm_buffer(struct client *client) |
| 255 | { |
| 256 | struct surface *surface = client->surface; |
| 257 | struct wl_shm *shm = client->wl_shm; |
| 258 | struct wl_shm_pool *pool; |
| 259 | int fd, size, stride; |
| 260 | |
| 261 | stride = surface->width * 4; |
| 262 | size = stride * surface->height; |
| 263 | |
| 264 | fd = os_create_anonymous_file(size); |
| 265 | assert(fd >= 0); |
| 266 | |
| 267 | surface->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 268 | fd, 0); |
| 269 | if (surface->data == MAP_FAILED) { |
| 270 | close(fd); |
| 271 | assert(surface->data != MAP_FAILED); |
| 272 | } |
| 273 | |
| 274 | pool = wl_shm_create_pool(shm, fd, size); |
| 275 | surface->wl_buffer = |
| 276 | wl_shm_pool_create_buffer(pool, 0, surface->width, |
| 277 | surface->height, stride, |
| 278 | WL_SHM_FORMAT_ARGB8888); |
| 279 | wl_shm_pool_destroy(pool); |
| 280 | |
| 281 | close(fd); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 286 | { |
| 287 | struct client *client = data; |
| 288 | |
| 289 | if (format == WL_SHM_FORMAT_ARGB8888) |
| 290 | client->has_argb = 1; |
| 291 | } |
| 292 | |
| 293 | struct wl_shm_listener shm_listener = { |
| 294 | shm_format |
| 295 | }; |
| 296 | |
| 297 | static void |
| 298 | test_handle_pointer_position(void *data, struct wl_test *wl_test, |
| 299 | wl_fixed_t x, wl_fixed_t y) |
| 300 | { |
| 301 | struct test *test = data; |
| 302 | test->pointer_x = wl_fixed_to_int(x); |
| 303 | test->pointer_y = wl_fixed_to_int(y); |
| 304 | |
| 305 | fprintf(stderr, "test-client: got global pointer %d %d\n", |
| 306 | test->pointer_x, test->pointer_y); |
| 307 | } |
| 308 | |
| 309 | static const struct wl_test_listener test_listener = { |
| 310 | test_handle_pointer_position |
| 311 | }; |
| 312 | |
| 313 | static void |
| 314 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 315 | enum wl_seat_capability caps) |
| 316 | { |
| 317 | struct input *input = data; |
| 318 | struct pointer *pointer; |
| 319 | struct keyboard *keyboard; |
| 320 | |
| 321 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
| 322 | pointer = calloc(1, sizeof *pointer); |
| 323 | pointer->wl_pointer = wl_seat_get_pointer(seat); |
| 324 | wl_pointer_set_user_data(pointer->wl_pointer, pointer); |
| 325 | wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener, |
| 326 | pointer); |
| 327 | input->pointer = pointer; |
| 328 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 329 | wl_pointer_destroy(input->pointer->wl_pointer); |
| 330 | free(input->pointer); |
| 331 | input->pointer = NULL; |
| 332 | } |
| 333 | |
| 334 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
| 335 | keyboard = calloc(1, sizeof *keyboard); |
| 336 | keyboard->wl_keyboard = wl_seat_get_keyboard(seat); |
| 337 | wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard); |
| 338 | wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener, |
| 339 | keyboard); |
| 340 | input->keyboard = keyboard; |
| 341 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 342 | wl_keyboard_destroy(input->keyboard->wl_keyboard); |
| 343 | free(input->keyboard); |
| 344 | input->keyboard = NULL; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static const struct wl_seat_listener seat_listener = { |
| 349 | seat_handle_capabilities, |
| 350 | }; |
| 351 | |
| 352 | static void |
| 353 | output_handle_geometry(void *data, |
| 354 | struct wl_output *wl_output, |
| 355 | int x, int y, |
| 356 | int physical_width, |
| 357 | int physical_height, |
| 358 | int subpixel, |
| 359 | const char *make, |
| 360 | const char *model, |
| 361 | int32_t transform) |
| 362 | { |
| 363 | struct output *output = data; |
| 364 | |
| 365 | output->x = x; |
| 366 | output->y = y; |
| 367 | } |
| 368 | |
| 369 | static void |
| 370 | output_handle_mode(void *data, |
| 371 | struct wl_output *wl_output, |
| 372 | uint32_t flags, |
| 373 | int width, |
| 374 | int height, |
| 375 | int refresh) |
| 376 | { |
| 377 | struct output *output = data; |
| 378 | |
| 379 | if (flags & WL_OUTPUT_MODE_CURRENT) { |
| 380 | output->width = width; |
| 381 | output->height = height; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static const struct wl_output_listener output_listener = { |
| 386 | output_handle_geometry, |
| 387 | output_handle_mode |
| 388 | }; |
| 389 | |
| 390 | static void |
| 391 | handle_global(void *data, struct wl_registry *registry, |
| 392 | uint32_t id, const char *interface, uint32_t version) |
| 393 | { |
| 394 | struct client *client = data; |
| 395 | struct input *input; |
| 396 | struct output *output; |
| 397 | struct test *test; |
| 398 | |
| 399 | if (strcmp(interface, "wl_compositor") == 0) { |
| 400 | client->wl_compositor = |
| 401 | wl_registry_bind(registry, id, |
| 402 | &wl_compositor_interface, 1); |
| 403 | } else if (strcmp(interface, "wl_seat") == 0) { |
| 404 | input = calloc(1, sizeof *input); |
| 405 | input->wl_seat = |
| 406 | wl_registry_bind(registry, id, |
| 407 | &wl_seat_interface, 1); |
| 408 | wl_seat_add_listener(input->wl_seat, &seat_listener, input); |
| 409 | client->input = input; |
| 410 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 411 | client->wl_shm = |
| 412 | wl_registry_bind(registry, id, |
| 413 | &wl_shm_interface, 1); |
| 414 | wl_shm_add_listener(client->wl_shm, &shm_listener, client); |
| 415 | } else if (strcmp(interface, "wl_output") == 0) { |
| 416 | output = malloc(sizeof *output); |
| 417 | output->wl_output = |
| 418 | wl_registry_bind(registry, id, |
| 419 | &wl_output_interface, 1); |
| 420 | wl_output_add_listener(output->wl_output, |
| 421 | &output_listener, output); |
| 422 | client->output = output; |
| 423 | } else if (strcmp(interface, "wl_test") == 0) { |
| 424 | test = calloc(1, sizeof *test); |
| 425 | test->wl_test = |
| 426 | wl_registry_bind(registry, id, |
| 427 | &wl_test_interface, 1); |
| 428 | wl_test_add_listener(test->wl_test, &test_listener, test); |
| 429 | client->test = test; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static const struct wl_registry_listener registry_listener = { |
| 434 | handle_global |
| 435 | }; |
| 436 | |
| 437 | struct client * |
| 438 | client_create(int x, int y, int width, int height) |
| 439 | { |
| 440 | struct client *client; |
| 441 | struct surface *surface; |
| 442 | |
| 443 | /* connect to display */ |
| 444 | client = calloc(1, sizeof *client); |
| 445 | client->wl_display = wl_display_connect(NULL); |
| 446 | assert(client->wl_display); |
| 447 | |
| 448 | /* setup registry so we can bind to interfaces */ |
| 449 | client->wl_registry = wl_display_get_registry(client->wl_display); |
| 450 | wl_registry_add_listener(client->wl_registry, ®istry_listener, client); |
| 451 | |
| 452 | /* trigger global listener */ |
| 453 | wl_display_dispatch(client->wl_display); |
| 454 | wl_display_roundtrip(client->wl_display); |
| 455 | |
| 456 | /* must have WL_SHM_FORMAT_ARGB32 */ |
| 457 | assert(client->has_argb); |
| 458 | |
| 459 | /* must have wl_test interface */ |
| 460 | assert(client->test); |
| 461 | |
| 462 | /* must have an output */ |
| 463 | assert(client->output); |
| 464 | |
| 465 | /* initialize the client surface */ |
| 466 | surface = calloc(1, sizeof *surface); |
| 467 | |
| 468 | surface->wl_surface = |
| 469 | wl_compositor_create_surface(client->wl_compositor); |
| 470 | assert(surface->wl_surface); |
| 471 | |
| 472 | wl_surface_add_listener(surface->wl_surface, &surface_listener, |
| 473 | surface); |
| 474 | |
| 475 | client->surface = surface; |
| 476 | wl_surface_set_user_data(surface->wl_surface, surface); |
| 477 | |
| 478 | surface->width = width; |
| 479 | surface->height = height; |
| 480 | create_shm_buffer(client); |
| 481 | |
| 482 | memset(surface->data, 64, width * height * 4); |
| 483 | wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0); |
| 484 | |
| 485 | move_client(client, x, y); |
| 486 | |
| 487 | return client; |
| 488 | } |