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