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