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> |
| 26 | #include <string.h> |
| 27 | #include <assert.h> |
| 28 | #include <signal.h> |
| 29 | #include <unistd.h> |
| 30 | #include "../src/compositor.h" |
| 31 | #include "wayland-test-server-protocol.h" |
| 32 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 33 | #ifdef ENABLE_EGL |
| 34 | #include <EGL/egl.h> |
| 35 | #include <EGL/eglext.h> |
| 36 | #endif /* ENABLE_EGL */ |
| 37 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 38 | struct weston_test { |
| 39 | struct weston_compositor *compositor; |
| 40 | struct weston_layer layer; |
| 41 | struct weston_process process; |
| 42 | }; |
| 43 | |
| 44 | struct weston_test_surface { |
| 45 | struct weston_surface *surface; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 46 | struct weston_view *view; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 47 | int32_t x, y; |
| 48 | struct weston_test *test; |
| 49 | }; |
| 50 | |
| 51 | static void |
| 52 | test_client_sigchld(struct weston_process *process, int status) |
| 53 | { |
| 54 | struct weston_test *test = |
| 55 | container_of(process, struct weston_test, process); |
| 56 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 57 | /* Chain up from weston-test-runner's exit code so that automake |
| 58 | * knows the exit status and can report e.g. skipped tests. */ |
| 59 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 60 | exit(WEXITSTATUS(status)); |
| 61 | |
| 62 | /* In case the child aborted or segfaulted... */ |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 63 | assert(status == 0); |
| 64 | |
| 65 | wl_display_terminate(test->compositor->wl_display); |
| 66 | } |
| 67 | |
| 68 | static struct weston_seat * |
| 69 | get_seat(struct weston_test *test) |
| 70 | { |
| 71 | struct wl_list *seat_list; |
| 72 | struct weston_seat *seat; |
| 73 | |
| 74 | seat_list = &test->compositor->seat_list; |
| 75 | assert(wl_list_length(seat_list) == 1); |
| 76 | seat = container_of(seat_list->next, struct weston_seat, link); |
| 77 | |
| 78 | return seat; |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | notify_pointer_position(struct weston_test *test, struct wl_resource *resource) |
| 83 | { |
| 84 | struct weston_seat *seat = get_seat(test); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 85 | struct weston_pointer *pointer = seat->pointer; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 86 | |
| 87 | wl_test_send_pointer_position(resource, pointer->x, pointer->y); |
| 88 | } |
| 89 | |
| 90 | static void |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 91 | 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] | 92 | { |
Giulio Camuffo | 7fe01b1 | 2013-03-28 18:02:42 +0100 | [diff] [blame] | 93 | struct weston_test_surface *test_surface = surface->configure_private; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 94 | struct weston_test *test = test_surface->test; |
| 95 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame^] | 96 | if (wl_list_empty(&test_surface->view->layer_link.link)) |
| 97 | weston_layer_entry_insert(&test->layer.view_list, |
| 98 | &test_surface->view->layer_link); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 99 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 100 | weston_view_set_position(test_surface->view, |
| 101 | test_surface->x, test_surface->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 102 | |
Kristian Høgsberg | ace0a39 | 2013-11-13 21:55:57 -0800 | [diff] [blame] | 103 | weston_view_update_transform(test_surface->view); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void |
| 107 | move_surface(struct wl_client *client, struct wl_resource *resource, |
| 108 | struct wl_resource *surface_resource, |
| 109 | int32_t x, int32_t y) |
| 110 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 111 | struct weston_surface *surface = |
| 112 | wl_resource_get_user_data(surface_resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 113 | struct weston_test_surface *test_surface; |
| 114 | |
Giulio Camuffo | 7fe01b1 | 2013-03-28 18:02:42 +0100 | [diff] [blame] | 115 | test_surface = surface->configure_private; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 116 | if (!test_surface) { |
| 117 | test_surface = malloc(sizeof *test_surface); |
| 118 | if (!test_surface) { |
| 119 | wl_resource_post_no_memory(resource); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | test_surface->view = weston_view_create(surface); |
| 124 | if (!test_surface->view) { |
| 125 | wl_resource_post_no_memory(resource); |
| 126 | free(test_surface); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | surface->configure_private = test_surface; |
| 131 | surface->configure = test_surface_configure; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | test_surface->surface = surface; |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 135 | test_surface->test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 136 | test_surface->x = x; |
| 137 | test_surface->y = y; |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | move_pointer(struct wl_client *client, struct wl_resource *resource, |
| 142 | int32_t x, int32_t y) |
| 143 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 144 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 145 | struct weston_seat *seat = get_seat(test); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 146 | struct weston_pointer *pointer = seat->pointer; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 147 | |
Kristian Høgsberg | 068b61c | 2013-02-25 17:04:47 -0500 | [diff] [blame] | 148 | notify_motion(seat, 100, |
| 149 | wl_fixed_from_int(x) - pointer->x, |
| 150 | wl_fixed_from_int(y) - pointer->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 151 | |
| 152 | notify_pointer_position(test, resource); |
| 153 | } |
| 154 | |
| 155 | static void |
| 156 | send_button(struct wl_client *client, struct wl_resource *resource, |
| 157 | int32_t button, uint32_t state) |
| 158 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 159 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 160 | struct weston_seat *seat = get_seat(test); |
| 161 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 162 | notify_button(seat, 100, button, state); |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | activate_surface(struct wl_client *client, struct wl_resource *resource, |
| 167 | struct wl_resource *surface_resource) |
| 168 | { |
| 169 | struct weston_surface *surface = surface_resource ? |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 170 | wl_resource_get_user_data(surface_resource) : NULL; |
| 171 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 172 | struct weston_seat *seat; |
| 173 | |
| 174 | seat = get_seat(test); |
| 175 | |
| 176 | if (surface) { |
| 177 | weston_surface_activate(surface, seat); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 178 | notify_keyboard_focus_in(seat, &seat->keyboard->keys, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 179 | STATE_UPDATE_AUTOMATIC); |
| 180 | } |
| 181 | else { |
| 182 | notify_keyboard_focus_out(seat); |
| 183 | weston_surface_activate(surface, seat); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void |
| 188 | send_key(struct wl_client *client, struct wl_resource *resource, |
| 189 | uint32_t key, enum wl_keyboard_key_state state) |
| 190 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 191 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 192 | struct weston_seat *seat = get_seat(test); |
| 193 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 194 | notify_key(seat, 100, key, state, STATE_UPDATE_AUTOMATIC); |
| 195 | } |
| 196 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 197 | #ifdef ENABLE_EGL |
| 198 | static int |
| 199 | is_egl_buffer(struct wl_resource *resource) |
| 200 | { |
| 201 | PFNEGLQUERYWAYLANDBUFFERWL query_buffer = |
| 202 | (void *) eglGetProcAddress("eglQueryWaylandBufferWL"); |
| 203 | EGLint format; |
| 204 | |
| 205 | if (query_buffer(eglGetCurrentDisplay(), |
| 206 | resource, |
| 207 | EGL_TEXTURE_FORMAT, |
| 208 | &format)) |
| 209 | return 1; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | #endif /* ENABLE_EGL */ |
| 214 | |
| 215 | static void |
| 216 | get_n_buffers(struct wl_client *client, struct wl_resource *resource) |
| 217 | { |
| 218 | int n_buffers = 0; |
| 219 | |
| 220 | #ifdef ENABLE_EGL |
| 221 | struct wl_resource *buffer_resource; |
| 222 | int i; |
| 223 | |
| 224 | for (i = 0; i < 1000; i++) { |
| 225 | buffer_resource = wl_client_get_object(client, i); |
| 226 | |
| 227 | if (buffer_resource == NULL) |
| 228 | continue; |
| 229 | |
| 230 | if (is_egl_buffer(buffer_resource)) |
| 231 | n_buffers++; |
| 232 | } |
| 233 | #endif /* ENABLE_EGL */ |
| 234 | |
| 235 | wl_test_send_n_egl_buffers(resource, n_buffers); |
| 236 | } |
| 237 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 238 | static const struct wl_test_interface test_implementation = { |
| 239 | move_surface, |
| 240 | move_pointer, |
| 241 | send_button, |
| 242 | activate_surface, |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 243 | send_key, |
| 244 | get_n_buffers, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | static void |
| 248 | bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 249 | { |
| 250 | struct weston_test *test = data; |
| 251 | struct wl_resource *resource; |
| 252 | |
Kristian Høgsberg | 442a5fa | 2013-07-03 18:13:33 -0400 | [diff] [blame] | 253 | resource = wl_resource_create(client, &wl_test_interface, 1, id); |
| 254 | wl_resource_set_implementation(resource, |
| 255 | &test_implementation, test, NULL); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 256 | |
| 257 | notify_pointer_position(test, resource); |
| 258 | } |
| 259 | |
| 260 | static void |
| 261 | idle_launch_client(void *data) |
| 262 | { |
| 263 | struct weston_test *test = data; |
| 264 | pid_t pid; |
| 265 | sigset_t allsigs; |
| 266 | char *path; |
| 267 | |
| 268 | path = getenv("WESTON_TEST_CLIENT_PATH"); |
| 269 | if (path == NULL) |
Pekka Paalanen | f72e479 | 2013-11-21 16:23:57 +0200 | [diff] [blame] | 270 | return; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 271 | pid = fork(); |
| 272 | if (pid == -1) |
| 273 | exit(EXIT_FAILURE); |
| 274 | if (pid == 0) { |
| 275 | sigfillset(&allsigs); |
| 276 | sigprocmask(SIG_UNBLOCK, &allsigs, NULL); |
| 277 | execl(path, path, NULL); |
| 278 | weston_log("compositor: executing '%s' failed: %m\n", path); |
| 279 | exit(EXIT_FAILURE); |
| 280 | } |
| 281 | |
| 282 | test->process.pid = pid; |
| 283 | test->process.cleanup = test_client_sigchld; |
| 284 | weston_watch_process(&test->process); |
| 285 | } |
| 286 | |
| 287 | WL_EXPORT int |
Kristian Høgsberg | cb4685b | 2013-02-20 15:37:49 -0500 | [diff] [blame] | 288 | module_init(struct weston_compositor *ec, |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 289 | int *argc, char *argv[]) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 290 | { |
| 291 | struct weston_test *test; |
| 292 | struct wl_event_loop *loop; |
| 293 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 294 | test = zalloc(sizeof *test); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 295 | if (test == NULL) |
| 296 | return -1; |
| 297 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 298 | test->compositor = ec; |
| 299 | weston_layer_init(&test->layer, &ec->cursor_layer.link); |
| 300 | |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 301 | if (wl_global_create(ec->wl_display, &wl_test_interface, 1, |
| 302 | test, bind_test) == NULL) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 303 | return -1; |
| 304 | |
| 305 | loop = wl_display_get_event_loop(ec->wl_display); |
| 306 | wl_event_loop_add_idle(loop, idle_launch_client, test); |
| 307 | |
| 308 | return 0; |
| 309 | } |