2 * Copyright © 2012 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29 #include <wayland-client.h>
30 #include <linux/input.h>
33 struct wl_display *display;
34 struct wl_compositor *compositor;
36 struct output *output;
37 struct surface *surface;
42 struct wl_pointer *pointer;
43 struct wl_keyboard *keyboard;
46 struct surface *pointer_focus;
47 struct surface *keyboard_focus;
48 uint32_t last_key, last_key_state;
52 struct wl_output *output;
58 struct wl_surface *surface;
59 struct output *output;
63 pointer_handle_enter(void *data, struct wl_pointer *pointer,
64 uint32_t serial, struct wl_surface *surface,
65 wl_fixed_t x, wl_fixed_t y)
67 struct input *input = data;
69 input->pointer_focus = wl_surface_get_user_data(surface);
70 input->x = wl_fixed_to_double(x);
71 input->y = wl_fixed_to_double(y);
72 fprintf(stderr, "test-client: got pointer enter %f %f, surface %p\n",
73 input->x, input->y, surface);
77 pointer_handle_leave(void *data, struct wl_pointer *pointer,
78 uint32_t serial, struct wl_surface *surface)
80 struct input *input = data;
82 input->pointer_focus = NULL;
84 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
89 pointer_handle_motion(void *data, struct wl_pointer *pointer,
90 uint32_t time, wl_fixed_t x, wl_fixed_t y)
92 struct input *input = data;
94 input->x = wl_fixed_to_double(x);
95 input->y = wl_fixed_to_double(y);
97 fprintf(stderr, "test-client: got pointer motion %f %f\n",
102 pointer_handle_button(void *data, struct wl_pointer *pointer,
103 uint32_t serial, uint32_t time, uint32_t button,
106 struct input *input = data;
108 enum wl_pointer_button_state state = state_w;
110 bit = 1 << (button - BTN_LEFT);
111 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
112 input->button_mask |= bit;
114 input->button_mask &= ~bit;
115 fprintf(stderr, "test-client: got pointer button %u %u\n",
120 pointer_handle_axis(void *data, struct wl_pointer *pointer,
121 uint32_t time, uint32_t axis, wl_fixed_t value)
123 fprintf(stderr, "test-client: got pointer axis %u %d\n", axis, value);
127 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
128 uint32_t format, int fd, uint32_t size)
131 fprintf(stderr, "test-client: got keyboard keymap\n");
135 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
136 uint32_t serial, struct wl_surface *surface,
137 struct wl_array *keys)
139 struct input *input = data;
141 input->keyboard_focus = wl_surface_get_user_data(surface);
142 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
147 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
148 uint32_t serial, struct wl_surface *surface)
150 struct input *input = data;
152 input->keyboard_focus = NULL;
153 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
158 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
159 uint32_t serial, uint32_t time, uint32_t key,
162 struct input *input = data;
164 input->last_key = key;
165 input->last_key_state = state;
167 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
171 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
172 uint32_t serial, uint32_t mods_depressed,
173 uint32_t mods_latched, uint32_t mods_locked,
176 fprintf(stderr, "test-client: got keyboard modifier\n");
179 static const struct wl_pointer_listener pointer_listener = {
180 pointer_handle_enter,
181 pointer_handle_leave,
182 pointer_handle_motion,
183 pointer_handle_button,
187 static const struct wl_keyboard_listener keyboard_listener = {
188 keyboard_handle_keymap,
189 keyboard_handle_enter,
190 keyboard_handle_leave,
192 keyboard_handle_modifiers,
196 seat_handle_capabilities(void *data, struct wl_seat *seat,
197 enum wl_seat_capability caps)
199 struct input *input = data;
201 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
202 input->pointer = wl_seat_get_pointer(seat);
203 wl_pointer_set_user_data(input->pointer, input);
204 wl_pointer_add_listener(input->pointer, &pointer_listener,
206 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
207 wl_pointer_destroy(input->pointer);
208 input->pointer = NULL;
211 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
212 input->keyboard = wl_seat_get_keyboard(seat);
213 wl_keyboard_set_user_data(input->keyboard, input);
214 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
216 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
217 wl_keyboard_destroy(input->keyboard);
218 input->keyboard = NULL;
222 static const struct wl_seat_listener seat_listener = {
223 seat_handle_capabilities,
227 output_handle_geometry(void *data,
228 struct wl_output *wl_output,
237 struct output *output = data;
244 output_handle_mode(void *data,
245 struct wl_output *wl_output,
251 struct output *output = data;
253 if (flags & WL_OUTPUT_MODE_CURRENT) {
254 output->width = width;
255 output->height = height;
259 static const struct wl_output_listener output_listener = {
260 output_handle_geometry,
265 handle_global(struct wl_display *_display, uint32_t id,
266 const char *interface, uint32_t version, void *data)
268 struct display *display = data;
270 struct output *output;
272 if (strcmp(interface, "wl_compositor") == 0) {
273 display->compositor =
274 wl_display_bind(display->display,
275 id, &wl_compositor_interface);
276 } else if (strcmp(interface, "wl_seat") == 0) {
277 input = calloc(1, sizeof *input);
278 input->seat = wl_display_bind(display->display, id,
280 input->pointer_focus = NULL;
281 input->keyboard_focus = NULL;
283 wl_seat_add_listener(input->seat, &seat_listener, input);
284 display->input = input;
285 } else if (strcmp(interface, "wl_output") == 0) {
286 output = malloc(sizeof *output);
287 output->output = wl_display_bind(display->display,
288 id, &wl_output_interface);
289 wl_output_add_listener(output->output,
290 &output_listener, output);
291 display->output = output;
293 fprintf(stderr, "test-client: created output global %p\n",
299 surface_enter(void *data,
300 struct wl_surface *wl_surface, struct wl_output *output)
302 struct surface *surface = data;
304 surface->output = wl_output_get_user_data(output);
306 fprintf(stderr, "test-client: got surface enter, output %p\n",
311 surface_leave(void *data,
312 struct wl_surface *wl_surface, struct wl_output *output)
314 struct surface *surface = data;
316 surface->output = NULL;
318 fprintf(stderr, "test-client: got surface leave, output %p\n",
319 wl_output_get_user_data(output));
322 static const struct wl_surface_listener surface_listener = {
328 send_keyboard_state(int fd, struct display *display)
332 int focus = display->input->keyboard_focus != NULL;
335 assert(display->input->keyboard_focus == display->surface);
338 wl_display_flush(display->display);
340 len = snprintf(buf, sizeof buf, "%u %u %d\n", display->input->last_key,
341 display->input->last_key_state, focus);
342 assert(write(fd, buf, len) == len);
344 wl_display_roundtrip(display->display);
348 send_button_state(int fd, struct display *display)
353 wl_display_flush(display->display);
355 len = snprintf(buf, sizeof buf, "%u\n", display->input->button_mask);
356 assert(write(fd, buf, len) == len);
358 wl_display_roundtrip(display->display);
362 send_state(int fd, struct display* display)
366 int visible = display->surface->output != NULL;
367 wl_fixed_t x = wl_fixed_from_int(-1);
368 wl_fixed_t y = wl_fixed_from_int(-1);
370 if (display->input->pointer_focus != NULL) {
371 assert(display->input->pointer_focus == display->surface);
372 x = wl_fixed_from_double(display->input->x);
373 y = wl_fixed_from_double(display->input->y);
377 /* FIXME: this fails on multi-display setup */
378 /* assert(display->surface->output == display->output); */
381 wl_display_flush(display->display);
383 len = snprintf(buf, sizeof buf, "%d %d %d\n", x, y, visible);
384 assert(write(fd, buf, len) == len);
386 wl_display_roundtrip(display->display);
390 create_surface(int fd, struct display *display)
392 struct surface *surface;
396 surface = malloc(sizeof *surface);
398 display->surface = surface;
399 surface->surface = wl_compositor_create_surface(display->compositor);
400 wl_surface_add_listener(surface->surface, &surface_listener, surface);
402 wl_display_flush(display->display);
404 len = snprintf(buf, sizeof buf, "surface %d\n",
405 wl_proxy_get_id((struct wl_proxy *) surface->surface));
406 assert(write(fd, buf, len) == len);
408 poll(NULL, 0, 100); /* Wait for next frame where we'll get events. */
410 wl_display_roundtrip(display->display);
413 int main(int argc, char *argv[])
415 struct display *display;
419 display = malloc(sizeof *display);
422 display->display = wl_display_connect(NULL);
423 assert(display->display);
425 wl_display_add_global_listener(display->display,
426 handle_global, display);
427 wl_display_iterate(display->display, WL_DISPLAY_READABLE);
428 wl_display_roundtrip(display->display);
431 p = getenv("TEST_SOCKET");
433 fd = strtol(p, NULL, 0);
436 ret = read(fd, buf, sizeof buf);
438 fprintf(stderr, "test-client: read error: fd %d, %m\n",
443 fprintf(stderr, "test-client: got %.*s\n", ret - 1, buf);
445 if (strncmp(buf, "bye\n", ret) == 0) {
447 } else if (strncmp(buf, "create-surface\n", ret) == 0) {
448 create_surface(fd, display);
449 } else if (strncmp(buf, "send-state\n", ret) == 0) {
450 send_state(fd, display);
451 } else if (strncmp(buf, "send-button-state\n", ret) == 0) {
452 send_button_state(fd, display);
453 } else if (strncmp(buf, "send-keyboard-state\n", ret) == 0) {
454 send_keyboard_state(fd, display);
456 fprintf(stderr, "test-client: unknown command %.*s\n",