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