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