blob: a825d123571a371ade6e79d352cd011138311cce [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;
39 int32_t x, y;
40 struct weston_test *test;
41};
42
43static void
44test_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
54static struct weston_seat *
55get_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
67static void
68notify_pointer_position(struct weston_test *test, struct wl_resource *resource)
69{
70 struct weston_seat *seat = get_seat(test);
Kristian Høgsberge3148752013-05-06 23:19:49 -040071 struct weston_pointer *pointer = seat->pointer;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080072
73 wl_test_send_pointer_position(resource, pointer->x, pointer->y);
74}
75
76static void
Giulio Camuffo184df502013-02-21 11:29:21 +010077test_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 -080078{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +010079 struct weston_test_surface *test_surface = surface->configure_private;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080080 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 Paalanenc3ce7382013-03-08 14:56:49 +020086 weston_surface_configure(surface, test_surface->x, test_surface->y,
87 width, height);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -080088
89 if (!weston_surface_is_mapped(surface))
90 weston_surface_update_transform(surface);
91}
92
93static void
94move_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øgsberg8f7f4832013-06-25 16:18:35 -040098 struct weston_surface *surface =
99 wl_resource_get_user_data(surface_resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800100 struct weston_test_surface *test_surface;
101
102 surface->configure = test_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +0100103 if (surface->configure_private == NULL)
104 surface->configure_private = malloc(sizeof *test_surface);
105 test_surface = surface->configure_private;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800106 if (test_surface == NULL) {
107 wl_resource_post_no_memory(resource);
108 return;
109 }
110
111 test_surface->surface = surface;
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400112 test_surface->test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800113 test_surface->x = x;
114 test_surface->y = y;
115}
116
117static void
118move_pointer(struct wl_client *client, struct wl_resource *resource,
119 int32_t x, int32_t y)
120{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400121 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800122 struct weston_seat *seat = get_seat(test);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400123 struct weston_pointer *pointer = seat->pointer;
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800124
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500125 notify_motion(seat, 100,
126 wl_fixed_from_int(x) - pointer->x,
127 wl_fixed_from_int(y) - pointer->y);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800128
129 notify_pointer_position(test, resource);
130}
131
132static void
133send_button(struct wl_client *client, struct wl_resource *resource,
134 int32_t button, uint32_t state)
135{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400136 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800137 struct weston_seat *seat = get_seat(test);
138
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800139 notify_button(seat, 100, button, state);
140}
141
142static void
143activate_surface(struct wl_client *client, struct wl_resource *resource,
144 struct wl_resource *surface_resource)
145{
146 struct weston_surface *surface = surface_resource ?
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400147 wl_resource_get_user_data(surface_resource) : NULL;
148 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800149 struct weston_seat *seat;
150
151 seat = get_seat(test);
152
153 if (surface) {
154 weston_surface_activate(surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -0400155 notify_keyboard_focus_in(seat, &seat->keyboard->keys,
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800156 STATE_UPDATE_AUTOMATIC);
157 }
158 else {
159 notify_keyboard_focus_out(seat);
160 weston_surface_activate(surface, seat);
161 }
162}
163
164static void
165send_key(struct wl_client *client, struct wl_resource *resource,
166 uint32_t key, enum wl_keyboard_key_state state)
167{
Kristian Høgsberg8f7f4832013-06-25 16:18:35 -0400168 struct weston_test *test = wl_resource_get_user_data(resource);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800169 struct weston_seat *seat = get_seat(test);
170
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800171 notify_key(seat, 100, key, state, STATE_UPDATE_AUTOMATIC);
172}
173
174static const struct wl_test_interface test_implementation = {
175 move_surface,
176 move_pointer,
177 send_button,
178 activate_surface,
179 send_key
180};
181
182static void
183bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id)
184{
185 struct weston_test *test = data;
186 struct wl_resource *resource;
187
Kristian Høgsberg442a5fa2013-07-03 18:13:33 -0400188 resource = wl_resource_create(client, &wl_test_interface, 1, id);
189 wl_resource_set_implementation(resource,
190 &test_implementation, test, NULL);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800191
192 notify_pointer_position(test, resource);
193}
194
195static void
196idle_launch_client(void *data)
197{
198 struct weston_test *test = data;
199 pid_t pid;
200 sigset_t allsigs;
201 char *path;
202
203 path = getenv("WESTON_TEST_CLIENT_PATH");
204 if (path == NULL)
205 exit(EXIT_FAILURE);
206 pid = fork();
207 if (pid == -1)
208 exit(EXIT_FAILURE);
209 if (pid == 0) {
210 sigfillset(&allsigs);
211 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
212 execl(path, path, NULL);
213 weston_log("compositor: executing '%s' failed: %m\n", path);
214 exit(EXIT_FAILURE);
215 }
216
217 test->process.pid = pid;
218 test->process.cleanup = test_client_sigchld;
219 weston_watch_process(&test->process);
220}
221
222WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500223module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -0700224 int *argc, char *argv[])
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800225{
226 struct weston_test *test;
227 struct wl_event_loop *loop;
228
Peter Huttererf3d62272013-08-08 11:57:05 +1000229 test = zalloc(sizeof *test);
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800230 if (test == NULL)
231 return -1;
232
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800233 test->compositor = ec;
234 weston_layer_init(&test->layer, &ec->cursor_layer.link);
235
Kristian Høgsberg919cddb2013-07-08 19:03:57 -0400236 if (wl_global_create(ec->wl_display, &wl_test_interface, 1,
237 test, bind_test) == NULL)
U. Artie Eoff65e7e7a2012-12-07 13:50:29 -0800238 return -1;
239
240 loop = wl_display_get_event_loop(ec->wl_display);
241 wl_event_loop_add_idle(loop, idle_launch_client, test);
242
243 return 0;
244}