toytoolkit: Don't draw shadows for maximized windows.
[profile/ivi/weston.git] / tests / test-client.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
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.
13  *
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
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #include <poll.h>
29 #include <wayland-client.h>
30 #include <linux/input.h>
31
32 struct display {
33         struct wl_display *display;
34         struct wl_compositor *compositor;
35         struct input *input;
36         struct output *output;
37         struct surface *surface;
38 };
39
40 struct input {
41         struct wl_seat *seat;
42         struct wl_pointer *pointer;
43         struct wl_keyboard *keyboard;
44         float x, y;
45         uint32_t button_mask;
46         struct surface *pointer_focus;
47         struct surface *keyboard_focus;
48         uint32_t last_key, last_key_state;
49 };
50
51 struct output {
52         struct wl_output *output;
53         int x, y;
54         int width, height;
55 };
56
57 struct surface {
58         struct wl_surface *surface;
59         struct output *output;
60 };
61
62 static void
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)
66 {
67         struct input *input = data;
68
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);
74 }
75
76 static void
77 pointer_handle_leave(void *data, struct wl_pointer *pointer,
78                      uint32_t serial, struct wl_surface *surface)
79 {
80         struct input *input = data;
81
82         input->pointer_focus = NULL;
83         
84         fprintf(stderr, "test-client: got pointer leave, surface %p\n",
85                 surface);
86 }
87
88 static void
89 pointer_handle_motion(void *data, struct wl_pointer *pointer,
90                       uint32_t time, wl_fixed_t x, wl_fixed_t y)
91 {
92         struct input *input = data;
93
94         input->x = wl_fixed_to_double(x);
95         input->y = wl_fixed_to_double(y);
96         
97         fprintf(stderr, "test-client: got pointer motion %f %f\n",
98                 input->x, input->y);
99 }
100
101 static void
102 pointer_handle_button(void *data, struct wl_pointer *pointer,
103                       uint32_t serial, uint32_t time, uint32_t button,
104                       uint32_t state_w)
105 {
106         struct input *input = data;
107         uint32_t bit;
108         enum wl_pointer_button_state state = state_w;
109
110         bit = 1 << (button - BTN_LEFT);
111         if (state == WL_POINTER_BUTTON_STATE_PRESSED)
112                 input->button_mask |= bit;
113         else
114                 input->button_mask &= ~bit;
115         fprintf(stderr, "test-client: got pointer button %u %u\n",
116                 button, state_w);
117 }
118
119 static void
120 pointer_handle_axis(void *data, struct wl_pointer *pointer,
121                     uint32_t time, uint32_t axis, wl_fixed_t value)
122 {
123         fprintf(stderr, "test-client: got pointer axis %u %d\n", axis, value);
124 }
125
126 static void
127 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
128                        uint32_t format, int fd, uint32_t size)
129 {
130         close(fd);
131         fprintf(stderr, "test-client: got keyboard keymap\n");
132 }
133
134 static void
135 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
136                       uint32_t serial, struct wl_surface *surface,
137                       struct wl_array *keys)
138 {
139         struct input *input = data;
140
141         input->keyboard_focus = wl_surface_get_user_data(surface);
142         fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
143                 surface);
144 }
145
146 static void
147 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
148                       uint32_t serial, struct wl_surface *surface)
149 {
150         struct input *input = data;
151
152         input->keyboard_focus = NULL;
153         fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
154                 surface);
155 }
156
157 static void
158 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
159                     uint32_t serial, uint32_t time, uint32_t key,
160                     uint32_t state)
161 {
162         struct input *input = data;
163
164         input->last_key = key;
165         input->last_key_state = state;
166
167         fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
168 }
169
170 static void
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,
174                           uint32_t group)
175 {
176         fprintf(stderr, "test-client: got keyboard modifier\n");
177 }
178
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,
184         pointer_handle_axis,
185 };
186
187 static const struct wl_keyboard_listener keyboard_listener = {
188         keyboard_handle_keymap,
189         keyboard_handle_enter,
190         keyboard_handle_leave,
191         keyboard_handle_key,
192         keyboard_handle_modifiers,
193 };
194
195 static void
196 seat_handle_capabilities(void *data, struct wl_seat *seat,
197                          enum wl_seat_capability caps)
198 {
199         struct input *input = data;
200
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,
205                                         input);
206         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
207                 wl_pointer_destroy(input->pointer);
208                 input->pointer = NULL;
209         }
210
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,
215                                          input);
216         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
217                 wl_keyboard_destroy(input->keyboard);
218                 input->keyboard = NULL;
219         }
220 }
221
222 static const struct wl_seat_listener seat_listener = {
223         seat_handle_capabilities,
224 };
225
226 static void
227 output_handle_geometry(void *data,
228                        struct wl_output *wl_output,
229                        int x, int y,
230                        int physical_width,
231                        int physical_height,
232                        int subpixel,
233                        const char *make,
234                        const char *model,
235                        int32_t transform)
236 {
237         struct output *output = data;
238
239         output->x = x;
240         output->y = y;
241 }
242
243 static void
244 output_handle_mode(void *data,
245                    struct wl_output *wl_output,
246                    uint32_t flags,
247                    int width,
248                    int height,
249                    int refresh)
250 {
251         struct output *output = data;
252
253         if (flags & WL_OUTPUT_MODE_CURRENT) {
254                 output->width = width;
255                 output->height = height;
256         }
257 }
258
259 static const struct wl_output_listener output_listener = {
260         output_handle_geometry,
261         output_handle_mode
262 };
263
264 static void
265 handle_global(struct wl_display *_display, uint32_t id,
266               const char *interface, uint32_t version, void *data)
267 {
268         struct display *display = data;
269         struct input *input;
270         struct output *output;
271
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,
279                                               &wl_seat_interface);
280                 input->pointer_focus = NULL;
281                 input->keyboard_focus = NULL;
282
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;
292
293                 fprintf(stderr, "test-client: created output global %p\n",
294                         display->output);
295         }
296 }
297
298 static void
299 surface_enter(void *data,
300               struct wl_surface *wl_surface, struct wl_output *output)
301 {
302         struct surface *surface = data;
303
304         surface->output = wl_output_get_user_data(output);
305
306         fprintf(stderr, "test-client: got surface enter, output %p\n",
307                 surface->output);
308 }
309
310 static void
311 surface_leave(void *data,
312               struct wl_surface *wl_surface, struct wl_output *output)
313 {
314         struct surface *surface = data;
315
316         surface->output = NULL;
317
318         fprintf(stderr, "test-client: got surface leave, output %p\n",
319                 wl_output_get_user_data(output));
320 }
321
322 static const struct wl_surface_listener surface_listener = {
323         surface_enter,
324         surface_leave
325 };
326
327 static void
328 send_keyboard_state(int fd, struct display *display)
329 {
330         char buf[64];
331         int len;
332         int focus = display->input->keyboard_focus != NULL;
333
334         if (focus) {
335                 assert(display->input->keyboard_focus == display->surface);
336         }
337
338         wl_display_flush(display->display);
339
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);
343
344         wl_display_roundtrip(display->display);
345 }
346
347 static void
348 send_button_state(int fd, struct display *display)
349 {
350         char buf[64];
351         int len;
352
353         wl_display_flush(display->display);
354
355         len = snprintf(buf, sizeof buf, "%u\n", display->input->button_mask);
356         assert(write(fd, buf, len) == len);
357
358         wl_display_roundtrip(display->display);
359 }
360
361 static void
362 send_state(int fd, struct display* display)
363 {
364         char buf[64];
365         int len;
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);
369
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);
374         }
375
376         if (visible) {
377                 /* FIXME: this fails on multi-display setup */
378                 /* assert(display->surface->output == display->output); */
379         }
380
381         wl_display_flush(display->display);
382
383         len = snprintf(buf, sizeof buf, "%d %d %d\n", x, y, visible);
384         assert(write(fd, buf, len) == len);
385
386         wl_display_roundtrip(display->display);
387 }
388
389 static void
390 create_surface(int fd, struct display *display)
391 {
392         struct surface *surface;
393         char buf[64];
394         int len;
395
396         surface = malloc(sizeof *surface);
397         assert(surface);
398         display->surface = surface;
399         surface->surface = wl_compositor_create_surface(display->compositor);
400         wl_surface_add_listener(surface->surface, &surface_listener, surface);
401
402         wl_display_flush(display->display);
403
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);
407
408         poll(NULL, 0, 100); /* Wait for next frame where we'll get events. */
409
410         wl_display_roundtrip(display->display);
411 }
412
413 int main(int argc, char *argv[])
414 {
415         struct display *display;
416         char buf[256], *p;
417         int ret, fd;
418
419         display = malloc(sizeof *display);
420         assert(display);
421
422         display->display = wl_display_connect(NULL);
423         assert(display->display);
424
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);
429
430         fd = 0;
431         p = getenv("TEST_SOCKET");
432         if (p)
433                 fd = strtol(p, NULL, 0);
434
435         while (1) {
436                 ret = read(fd, buf, sizeof buf);
437                 if (ret == -1) {
438                         fprintf(stderr, "test-client: read error: fd %d, %m\n",
439                                 fd);
440                         return -1;
441                 }
442
443                 fprintf(stderr, "test-client: got %.*s\n", ret - 1, buf);
444
445                 if (strncmp(buf, "bye\n", ret) == 0) {
446                         return 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);
455                 } else {
456                         fprintf(stderr, "test-client: unknown command %.*s\n",
457                                 ret, buf);
458                         return -1;
459                 }
460         }
461
462         assert(0);
463 }