tests: allow weston test plugin to keep on running
[profile/ivi/weston-ivi-shell.git] / tests / weston-test.c
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         struct weston_view *view;
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);
72         struct weston_pointer *pointer = seat->pointer;
73
74         wl_test_send_pointer_position(resource, pointer->x, pointer->y);
75 }
76
77 static void
78 test_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
79 {
80         struct weston_test_surface *test_surface = surface->configure_private;
81         struct weston_test *test = test_surface->test;
82
83         if (wl_list_empty(&test_surface->view->layer_link))
84                 wl_list_insert(&test->layer.view_list,
85                                &test_surface->view->layer_link);
86
87         weston_view_configure(test_surface->view,
88                               test_surface->x, test_surface->y,
89                               width, height);
90
91         weston_view_update_transform(test_surface->view);
92 }
93
94 static void
95 move_surface(struct wl_client *client, struct wl_resource *resource,
96              struct wl_resource *surface_resource,
97              int32_t x, int32_t y)
98 {
99         struct weston_surface *surface =
100                 wl_resource_get_user_data(surface_resource);
101         struct weston_test_surface *test_surface;
102
103         test_surface = surface->configure_private;
104         if (!test_surface) {
105                 test_surface = malloc(sizeof *test_surface);
106                 if (!test_surface) {
107                         wl_resource_post_no_memory(resource);
108                         return;
109                 }
110
111                 test_surface->view = weston_view_create(surface);
112                 if (!test_surface->view) {
113                         wl_resource_post_no_memory(resource);
114                         free(test_surface);
115                         return;
116                 }
117
118                 surface->configure_private = test_surface;
119                 surface->configure = test_surface_configure;
120         }
121
122         test_surface->surface = surface;
123         test_surface->test = wl_resource_get_user_data(resource);
124         test_surface->x = x;
125         test_surface->y = y;
126 }
127
128 static void
129 move_pointer(struct wl_client *client, struct wl_resource *resource,
130              int32_t x, int32_t y)
131 {
132         struct weston_test *test = wl_resource_get_user_data(resource);
133         struct weston_seat *seat = get_seat(test);
134         struct weston_pointer *pointer = seat->pointer;
135
136         notify_motion(seat, 100,
137                       wl_fixed_from_int(x) - pointer->x,
138                       wl_fixed_from_int(y) - pointer->y);
139
140         notify_pointer_position(test, resource);
141 }
142
143 static void
144 send_button(struct wl_client *client, struct wl_resource *resource,
145             int32_t button, uint32_t state)
146 {
147         struct weston_test *test = wl_resource_get_user_data(resource);
148         struct weston_seat *seat = get_seat(test);
149
150         notify_button(seat, 100, button, state);
151 }
152
153 static void
154 activate_surface(struct wl_client *client, struct wl_resource *resource,
155                  struct wl_resource *surface_resource)
156 {
157         struct weston_surface *surface = surface_resource ?
158                 wl_resource_get_user_data(surface_resource) : NULL;
159         struct weston_test *test = wl_resource_get_user_data(resource);
160         struct weston_seat *seat;
161
162         seat = get_seat(test);
163
164         if (surface) {
165                 weston_surface_activate(surface, seat);
166                 notify_keyboard_focus_in(seat, &seat->keyboard->keys,
167                                          STATE_UPDATE_AUTOMATIC);
168         }
169         else {
170                 notify_keyboard_focus_out(seat);
171                 weston_surface_activate(surface, seat);
172         }
173 }
174
175 static void
176 send_key(struct wl_client *client, struct wl_resource *resource,
177          uint32_t key, enum wl_keyboard_key_state state)
178 {
179         struct weston_test *test = wl_resource_get_user_data(resource);
180         struct weston_seat *seat = get_seat(test);
181
182         notify_key(seat, 100, key, state, STATE_UPDATE_AUTOMATIC);
183 }
184
185 static const struct wl_test_interface test_implementation = {
186         move_surface,
187         move_pointer,
188         send_button,
189         activate_surface,
190         send_key
191 };
192
193 static void
194 bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id)
195 {
196         struct weston_test *test = data;
197         struct wl_resource *resource;
198
199         resource = wl_resource_create(client, &wl_test_interface, 1, id);
200         wl_resource_set_implementation(resource,
201                                        &test_implementation, test, NULL);
202
203         notify_pointer_position(test, resource);
204 }
205
206 static void
207 idle_launch_client(void *data)
208 {
209         struct weston_test *test = data;
210         pid_t pid;
211         sigset_t allsigs;
212         char *path;
213
214         path = getenv("WESTON_TEST_CLIENT_PATH");
215         if (path == NULL)
216                 return;
217         pid = fork();
218         if (pid == -1)
219                 exit(EXIT_FAILURE);
220         if (pid == 0) {
221                 sigfillset(&allsigs);
222                 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
223                 execl(path, path, NULL);
224                 weston_log("compositor: executing '%s' failed: %m\n", path);
225                 exit(EXIT_FAILURE);
226         }
227
228         test->process.pid = pid;
229         test->process.cleanup = test_client_sigchld;
230         weston_watch_process(&test->process);
231 }
232
233 WL_EXPORT int
234 module_init(struct weston_compositor *ec,
235             int *argc, char *argv[])
236 {
237         struct weston_test *test;
238         struct wl_event_loop *loop;
239
240         test = zalloc(sizeof *test);
241         if (test == NULL)
242                 return -1;
243
244         test->compositor = ec;
245         weston_layer_init(&test->layer, &ec->cursor_layer.link);
246
247         if (wl_global_create(ec->wl_display, &wl_test_interface, 1,
248                              test, bind_test) == NULL)
249                 return -1;
250
251         loop = wl_display_get_event_loop(ec->wl_display);
252         wl_event_loop_add_idle(loop, idle_launch_client, test);
253
254         return 0;
255 }