U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 25 | #include <stdlib.h> |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 26 | #include <assert.h> |
| 27 | #include <signal.h> |
| 28 | #include <unistd.h> |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 29 | #include <string.h> |
Bryce Harrington | a768026 | 2014-11-19 17:18:34 -0800 | [diff] [blame] | 30 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 31 | #include "../src/compositor.h" |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 32 | #include "weston-test-server-protocol.h" |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 33 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 34 | #ifdef ENABLE_EGL |
| 35 | #include <EGL/egl.h> |
| 36 | #include <EGL/eglext.h> |
Daniel Stone | e789771 | 2015-02-11 18:15:17 +0000 | [diff] [blame] | 37 | #include "../src/weston-egl-ext.h" |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 38 | #endif /* ENABLE_EGL */ |
| 39 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 40 | struct weston_test { |
| 41 | struct weston_compositor *compositor; |
| 42 | struct weston_layer layer; |
| 43 | struct weston_process process; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame^] | 44 | struct weston_seat seat; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct weston_test_surface { |
| 48 | struct weston_surface *surface; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 49 | struct weston_view *view; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 50 | int32_t x, y; |
| 51 | struct weston_test *test; |
| 52 | }; |
| 53 | |
| 54 | static void |
| 55 | test_client_sigchld(struct weston_process *process, int status) |
| 56 | { |
| 57 | struct weston_test *test = |
| 58 | container_of(process, struct weston_test, process); |
| 59 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 60 | /* Chain up from weston-test-runner's exit code so that automake |
| 61 | * knows the exit status and can report e.g. skipped tests. */ |
| 62 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 63 | exit(WEXITSTATUS(status)); |
| 64 | |
| 65 | /* In case the child aborted or segfaulted... */ |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 66 | assert(status == 0); |
| 67 | |
| 68 | wl_display_terminate(test->compositor->wl_display); |
| 69 | } |
| 70 | |
| 71 | static struct weston_seat * |
| 72 | get_seat(struct weston_test *test) |
| 73 | { |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame^] | 74 | return &test->seat; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void |
| 78 | notify_pointer_position(struct weston_test *test, struct wl_resource *resource) |
| 79 | { |
| 80 | struct weston_seat *seat = get_seat(test); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 81 | struct weston_pointer *pointer = seat->pointer; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 82 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 83 | weston_test_send_pointer_position(resource, pointer->x, pointer->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 87 | test_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 88 | { |
Giulio Camuffo | 7fe01b1 | 2013-03-28 18:02:42 +0100 | [diff] [blame] | 89 | struct weston_test_surface *test_surface = surface->configure_private; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 90 | struct weston_test *test = test_surface->test; |
| 91 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 92 | if (wl_list_empty(&test_surface->view->layer_link.link)) |
| 93 | weston_layer_entry_insert(&test->layer.view_list, |
| 94 | &test_surface->view->layer_link); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 95 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 96 | weston_view_set_position(test_surface->view, |
| 97 | test_surface->x, test_surface->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 98 | |
Kristian Høgsberg | ace0a39 | 2013-11-13 21:55:57 -0800 | [diff] [blame] | 99 | weston_view_update_transform(test_surface->view); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void |
| 103 | move_surface(struct wl_client *client, struct wl_resource *resource, |
| 104 | struct wl_resource *surface_resource, |
| 105 | int32_t x, int32_t y) |
| 106 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 107 | struct weston_surface *surface = |
| 108 | wl_resource_get_user_data(surface_resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 109 | struct weston_test_surface *test_surface; |
| 110 | |
Giulio Camuffo | 7fe01b1 | 2013-03-28 18:02:42 +0100 | [diff] [blame] | 111 | test_surface = surface->configure_private; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 112 | if (!test_surface) { |
| 113 | test_surface = malloc(sizeof *test_surface); |
| 114 | if (!test_surface) { |
| 115 | wl_resource_post_no_memory(resource); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | test_surface->view = weston_view_create(surface); |
| 120 | if (!test_surface->view) { |
| 121 | wl_resource_post_no_memory(resource); |
| 122 | free(test_surface); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | surface->configure_private = test_surface; |
| 127 | surface->configure = test_surface_configure; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | test_surface->surface = surface; |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 131 | test_surface->test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 132 | test_surface->x = x; |
| 133 | test_surface->y = y; |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | move_pointer(struct wl_client *client, struct wl_resource *resource, |
| 138 | int32_t x, int32_t y) |
| 139 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 140 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 141 | struct weston_seat *seat = get_seat(test); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 142 | struct weston_pointer *pointer = seat->pointer; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 143 | |
Kristian Høgsberg | 068b61c | 2013-02-25 17:04:47 -0500 | [diff] [blame] | 144 | notify_motion(seat, 100, |
| 145 | wl_fixed_from_int(x) - pointer->x, |
| 146 | wl_fixed_from_int(y) - pointer->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 147 | |
| 148 | notify_pointer_position(test, resource); |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | send_button(struct wl_client *client, struct wl_resource *resource, |
| 153 | int32_t button, uint32_t state) |
| 154 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 155 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 156 | struct weston_seat *seat = get_seat(test); |
| 157 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 158 | notify_button(seat, 100, button, state); |
| 159 | } |
| 160 | |
| 161 | static void |
| 162 | activate_surface(struct wl_client *client, struct wl_resource *resource, |
| 163 | struct wl_resource *surface_resource) |
| 164 | { |
| 165 | struct weston_surface *surface = surface_resource ? |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 166 | wl_resource_get_user_data(surface_resource) : NULL; |
| 167 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 168 | struct weston_seat *seat; |
| 169 | |
| 170 | seat = get_seat(test); |
| 171 | |
| 172 | if (surface) { |
| 173 | weston_surface_activate(surface, seat); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 174 | notify_keyboard_focus_in(seat, &seat->keyboard->keys, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 175 | STATE_UPDATE_AUTOMATIC); |
| 176 | } |
| 177 | else { |
| 178 | notify_keyboard_focus_out(seat); |
| 179 | weston_surface_activate(surface, seat); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static void |
| 184 | send_key(struct wl_client *client, struct wl_resource *resource, |
| 185 | uint32_t key, enum wl_keyboard_key_state state) |
| 186 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 187 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 188 | struct weston_seat *seat = get_seat(test); |
| 189 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 190 | notify_key(seat, 100, key, state, STATE_UPDATE_AUTOMATIC); |
| 191 | } |
| 192 | |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 193 | static void |
| 194 | device_release(struct wl_client *client, |
| 195 | struct wl_resource *resource, const char *device) |
| 196 | { |
| 197 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 198 | struct weston_seat *seat = get_seat(test); |
| 199 | |
| 200 | if (strcmp(device, "pointer") == 0) { |
| 201 | weston_seat_release_pointer(seat); |
| 202 | } else if (strcmp(device, "keyboard") == 0) { |
| 203 | weston_seat_release_keyboard(seat); |
| 204 | } else if (strcmp(device, "touch") == 0) { |
| 205 | weston_seat_release_touch(seat); |
| 206 | } else if (strcmp(device, "seat") == 0) { |
| 207 | weston_seat_release(seat); |
| 208 | } else { |
| 209 | assert(0 && "Unsupported device"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | device_add(struct wl_client *client, |
| 215 | struct wl_resource *resource, const char *device) |
| 216 | { |
| 217 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 218 | struct weston_seat *seat = get_seat(test); |
| 219 | |
| 220 | if (strcmp(device, "pointer") == 0) { |
| 221 | weston_seat_init_pointer(seat); |
| 222 | } else if (strcmp(device, "keyboard") == 0) { |
| 223 | weston_seat_init_keyboard(seat, NULL); |
| 224 | } else if (strcmp(device, "touch") == 0) { |
| 225 | weston_seat_init_touch(seat); |
| 226 | } else { |
| 227 | assert(0 && "Unsupported device"); |
| 228 | } |
| 229 | } |
| 230 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 231 | #ifdef ENABLE_EGL |
| 232 | static int |
| 233 | is_egl_buffer(struct wl_resource *resource) |
| 234 | { |
| 235 | PFNEGLQUERYWAYLANDBUFFERWL query_buffer = |
| 236 | (void *) eglGetProcAddress("eglQueryWaylandBufferWL"); |
| 237 | EGLint format; |
| 238 | |
| 239 | if (query_buffer(eglGetCurrentDisplay(), |
| 240 | resource, |
| 241 | EGL_TEXTURE_FORMAT, |
| 242 | &format)) |
| 243 | return 1; |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | #endif /* ENABLE_EGL */ |
| 248 | |
| 249 | static void |
| 250 | get_n_buffers(struct wl_client *client, struct wl_resource *resource) |
| 251 | { |
| 252 | int n_buffers = 0; |
| 253 | |
| 254 | #ifdef ENABLE_EGL |
| 255 | struct wl_resource *buffer_resource; |
| 256 | int i; |
| 257 | |
| 258 | for (i = 0; i < 1000; i++) { |
| 259 | buffer_resource = wl_client_get_object(client, i); |
| 260 | |
| 261 | if (buffer_resource == NULL) |
| 262 | continue; |
| 263 | |
| 264 | if (is_egl_buffer(buffer_resource)) |
| 265 | n_buffers++; |
| 266 | } |
| 267 | #endif /* ENABLE_EGL */ |
| 268 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 269 | weston_test_send_n_egl_buffers(resource, n_buffers); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 272 | static const struct weston_test_interface test_implementation = { |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 273 | move_surface, |
| 274 | move_pointer, |
| 275 | send_button, |
| 276 | activate_surface, |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 277 | send_key, |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 278 | device_release, |
| 279 | device_add, |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 280 | get_n_buffers, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | static void |
| 284 | bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 285 | { |
| 286 | struct weston_test *test = data; |
| 287 | struct wl_resource *resource; |
| 288 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 289 | resource = wl_resource_create(client, &weston_test_interface, 1, id); |
Marek Chalupa | 42ebdda | 2014-07-11 12:33:02 +0200 | [diff] [blame] | 290 | if (!resource) { |
| 291 | wl_client_post_no_memory(client); |
| 292 | return; |
| 293 | } |
| 294 | |
Kristian Høgsberg | 442a5fa | 2013-07-03 18:13:33 -0400 | [diff] [blame] | 295 | wl_resource_set_implementation(resource, |
| 296 | &test_implementation, test, NULL); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 297 | |
| 298 | notify_pointer_position(test, resource); |
| 299 | } |
| 300 | |
| 301 | static void |
| 302 | idle_launch_client(void *data) |
| 303 | { |
| 304 | struct weston_test *test = data; |
| 305 | pid_t pid; |
| 306 | sigset_t allsigs; |
| 307 | char *path; |
| 308 | |
| 309 | path = getenv("WESTON_TEST_CLIENT_PATH"); |
| 310 | if (path == NULL) |
Pekka Paalanen | f72e479 | 2013-11-21 16:23:57 +0200 | [diff] [blame] | 311 | return; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 312 | pid = fork(); |
| 313 | if (pid == -1) |
| 314 | exit(EXIT_FAILURE); |
| 315 | if (pid == 0) { |
| 316 | sigfillset(&allsigs); |
| 317 | sigprocmask(SIG_UNBLOCK, &allsigs, NULL); |
| 318 | execl(path, path, NULL); |
| 319 | weston_log("compositor: executing '%s' failed: %m\n", path); |
| 320 | exit(EXIT_FAILURE); |
| 321 | } |
| 322 | |
| 323 | test->process.pid = pid; |
| 324 | test->process.cleanup = test_client_sigchld; |
| 325 | weston_watch_process(&test->process); |
| 326 | } |
| 327 | |
| 328 | WL_EXPORT int |
Kristian Høgsberg | cb4685b | 2013-02-20 15:37:49 -0500 | [diff] [blame] | 329 | module_init(struct weston_compositor *ec, |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 330 | int *argc, char *argv[]) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 331 | { |
| 332 | struct weston_test *test; |
| 333 | struct wl_event_loop *loop; |
| 334 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 335 | test = zalloc(sizeof *test); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 336 | if (test == NULL) |
| 337 | return -1; |
| 338 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 339 | test->compositor = ec; |
| 340 | weston_layer_init(&test->layer, &ec->cursor_layer.link); |
| 341 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 342 | if (wl_global_create(ec->wl_display, &weston_test_interface, 1, |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 343 | test, bind_test) == NULL) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 344 | return -1; |
| 345 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame^] | 346 | /* create our own seat */ |
| 347 | weston_seat_init(&test->seat, ec, "test-seat"); |
| 348 | |
| 349 | /* add devices */ |
| 350 | weston_seat_init_pointer(&test->seat); |
| 351 | weston_seat_init_keyboard(&test->seat, NULL); |
| 352 | weston_seat_init_touch(&test->seat); |
| 353 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 354 | loop = wl_display_get_event_loop(ec->wl_display); |
| 355 | wl_event_loop_add_idle(loop, idle_launch_client, test); |
| 356 | |
| 357 | return 0; |
| 358 | } |