blob: 5f341d86d3961a1f348b3e9fc724676bc09eff8d [file] [log] [blame]
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -08001/*
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
31struct weston_test {
32 struct weston_compositor *compositor;
33 struct weston_layer layer;
34 struct weston_process process;
35};
36
37struct weston_test_surface {
38 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -050039 struct weston_view *view;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080040 int32_t x, y;
41 struct weston_test *test;
42};
43
44static void
45test_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
55static struct weston_seat *
56get_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
68static void
69notify_pointer_position(struct weston_test *test, struct wl_resource *resource)
70{
71 struct weston_seat *seat = get_seat(test);
Kristian Høgsberge3148752013-05-06 23:19:49 -040072 struct weston_pointer *pointer = seat->pointer;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080073
74 wl_test_send_pointer_position(resource, pointer->x, pointer->y);
75}
76
77static void
Giulio Camuffo184df502013-02-21 11:29:21 +010078test_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080079{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +010080 struct weston_test_surface *test_surface = surface->configure_private;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080081 struct weston_test *test = test_surface->test;
82
Jason Ekstranda7af7042013-10-12 22:38:11 -050083 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 Eoff65e7e7a2012-12-07 13:50:29 -080086
Jason Ekstranda7af7042013-10-12 22:38:11 -050087 weston_view_configure(test_surface->view,
88 test_surface->x, test_surface->y,
89 width, height);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080090
Jason Ekstranda7af7042013-10-12 22:38:11 -050091 if (!weston_view_is_mapped(test_surface->view))
92 weston_view_update_transform(test_surface->view);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080093}
94
95static void
96move_surface(struct wl_client *client, struct wl_resource *resource,
97 struct wl_resource *surface_resource,
98 int32_t x, int32_t y)
99{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400100 struct weston_surface *surface =
101 wl_resource_get_user_data(surface_resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800102 struct weston_test_surface *test_surface;
103
Giulio Camuffo7fe01b12013-03-28 18:02:42 +0100104 test_surface = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500105 if (!test_surface) {
106 test_surface = malloc(sizeof *test_surface);
107 if (!test_surface) {
108 wl_resource_post_no_memory(resource);
109 return;
110 }
111
112 test_surface->view = weston_view_create(surface);
113 if (!test_surface->view) {
114 wl_resource_post_no_memory(resource);
115 free(test_surface);
116 return;
117 }
118
119 surface->configure_private = test_surface;
120 surface->configure = test_surface_configure;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800121 }
122
123 test_surface->surface = surface;
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400124 test_surface->test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800125 test_surface->x = x;
126 test_surface->y = y;
127}
128
129static void
130move_pointer(struct wl_client *client, struct wl_resource *resource,
131 int32_t x, int32_t y)
132{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400133 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800134 struct weston_seat *seat = get_seat(test);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400135 struct weston_pointer *pointer = seat->pointer;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800136
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500137 notify_motion(seat, 100,
138 wl_fixed_from_int(x) - pointer->x,
139 wl_fixed_from_int(y) - pointer->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800140
141 notify_pointer_position(test, resource);
142}
143
144static void
145send_button(struct wl_client *client, struct wl_resource *resource,
146 int32_t button, uint32_t state)
147{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400148 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800149 struct weston_seat *seat = get_seat(test);
150
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800151 notify_button(seat, 100, button, state);
152}
153
154static void
155activate_surface(struct wl_client *client, struct wl_resource *resource,
156 struct wl_resource *surface_resource)
157{
158 struct weston_surface *surface = surface_resource ?
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400159 wl_resource_get_user_data(surface_resource) : NULL;
160 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800161 struct weston_seat *seat;
162
163 seat = get_seat(test);
164
165 if (surface) {
166 weston_surface_activate(surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400167 notify_keyboard_focus_in(seat, &seat->keyboard->keys,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800168 STATE_UPDATE_AUTOMATIC);
169 }
170 else {
171 notify_keyboard_focus_out(seat);
172 weston_surface_activate(surface, seat);
173 }
174}
175
176static void
177send_key(struct wl_client *client, struct wl_resource *resource,
178 uint32_t key, enum wl_keyboard_key_state state)
179{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400180 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800181 struct weston_seat *seat = get_seat(test);
182
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800183 notify_key(seat, 100, key, state, STATE_UPDATE_AUTOMATIC);
184}
185
186static const struct wl_test_interface test_implementation = {
187 move_surface,
188 move_pointer,
189 send_button,
190 activate_surface,
191 send_key
192};
193
194static void
195bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id)
196{
197 struct weston_test *test = data;
198 struct wl_resource *resource;
199
Kristian Høgsberg442a5fa2013-07-03 18:13:33 -0400200 resource = wl_resource_create(client, &wl_test_interface, 1, id);
201 wl_resource_set_implementation(resource,
202 &test_implementation, test, NULL);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800203
204 notify_pointer_position(test, resource);
205}
206
207static void
208idle_launch_client(void *data)
209{
210 struct weston_test *test = data;
211 pid_t pid;
212 sigset_t allsigs;
213 char *path;
214
215 path = getenv("WESTON_TEST_CLIENT_PATH");
216 if (path == NULL)
217 exit(EXIT_FAILURE);
218 pid = fork();
219 if (pid == -1)
220 exit(EXIT_FAILURE);
221 if (pid == 0) {
222 sigfillset(&allsigs);
223 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
224 execl(path, path, NULL);
225 weston_log("compositor: executing '%s' failed: %m\n", path);
226 exit(EXIT_FAILURE);
227 }
228
229 test->process.pid = pid;
230 test->process.cleanup = test_client_sigchld;
231 weston_watch_process(&test->process);
232}
233
234WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500235module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700236 int *argc, char *argv[])
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800237{
238 struct weston_test *test;
239 struct wl_event_loop *loop;
240
Peter Huttererf3d62272013-08-08 11:57:05 +1000241 test = zalloc(sizeof *test);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800242 if (test == NULL)
243 return -1;
244
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800245 test->compositor = ec;
246 weston_layer_init(&test->layer, &ec->cursor_layer.link);
247
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400248 if (wl_global_create(ec->wl_display, &wl_test_interface, 1,
249 test, bind_test) == NULL)
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800250 return -1;
251
252 loop = wl_display_get_event_loop(ec->wl_display);
253 wl_event_loop_add_idle(loop, idle_launch_client, test);
254
255 return 0;
256}