packaging: Update dependencies and bump version to 0.9.23
[profile/ivi/ico-uxf-weston-plugin.git] / tests / test-client.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2013 TOYOTA MOTOR CORPORATION
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting documentation, and
9  * that the name of the copyright holders not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  The copyright holders make no representations
12  * about the suitability of this software for any purpose.  It is provided "as
13  * is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21  * OF THIS SOFTWARE.
22  */
23 /**
24  * @brief   Wayland Application for unit test of Weston(Wayland) IVI plugins
25  *
26  * @date    Feb-08-2013
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <assert.h>
34 #include <poll.h>
35 #include <wayland-client.h>
36 #include <linux/input.h>
37 #include "ico_window_mgr-client-protocol.h"
38 #include "ico_input_mgr-client-protocol.h"
39 #include "test-common.h"
40
41 #define MAX_CON_NAME    127
42
43 struct display {
44     struct wl_display *display;
45     struct wl_registry *registry;
46     struct wl_compositor *compositor;
47     struct wl_shell *shell;
48     struct ico_window_mgr *ico_window_mgr;
49     struct ico_exinput *ico_exinput;
50     struct input *input;
51     struct output *output;
52     struct surface *surface;
53     int    init_color;
54     int    init_width;
55     int    init_height;
56     int    prompt;
57     char   connect[MAX_CON_NAME+1];
58 };
59
60 struct input {
61     struct display *display;
62     struct wl_seat *seat;
63     struct wl_pointer *pointer;
64     struct wl_keyboard *keyboard;
65     struct wl_touch *touch;
66     float x, y;
67     uint32_t button_mask;
68     struct surface *pointer_focus;
69     struct surface *keyboard_focus;
70     uint32_t last_key, last_key_state;
71 };
72
73 struct output {
74     struct display *display;
75     struct wl_output *output;
76     int x, y;
77     int width, height;
78 };
79
80 struct surface {
81     struct display *display;
82     struct wl_surface *surface;
83     struct wl_shell_surface *shell_surface;
84     struct output *output;
85     EGLDisplay  dpy;
86     EGLConfig   conf;
87     EGLContext  ctx;
88     EGLSurface  egl_surface;
89 };
90
91 static void clear_surface(struct display *display);
92
93 static void
94 pointer_handle_enter(void *data, struct wl_pointer *pointer,
95                      uint32_t serial, struct wl_surface *surface,
96                      wl_fixed_t x, wl_fixed_t y)
97 {
98     struct input *input = data;
99
100     input->pointer_focus = wl_surface_get_user_data(surface);
101     input->x = wl_fixed_to_double(x);
102     input->y = wl_fixed_to_double(y);
103     print_log("CLIENT: got pointer enter (%d,%d)=(%d,%d), surface %p",
104               x, y, (int)input->x, (int)input->y, surface);
105 }
106
107 static void
108 pointer_handle_leave(void *data, struct wl_pointer *pointer,
109                      uint32_t serial, struct wl_surface *surface)
110 {
111     struct input *input = data;
112
113     input->pointer_focus = NULL;
114
115     print_log("CLIENT: got pointer leave, surface %p", surface);
116 }
117
118 static void
119 pointer_handle_motion(void *data, struct wl_pointer *pointer,
120                       uint32_t time, wl_fixed_t x, wl_fixed_t y)
121 {
122     struct input *input = data;
123
124     input->x = wl_fixed_to_double(x);
125     input->y = wl_fixed_to_double(y);
126
127     print_log("CLIENT: got pointer motion (%d,%d)=(%d,%d)",
128               x, y, (int)input->x, (int)input->y);
129 }
130
131 static void
132 pointer_handle_button(void *data, struct wl_pointer *pointer,
133                       uint32_t serial, uint32_t time, uint32_t button, uint32_t state_w)
134 {
135     struct input *input = data;
136     uint32_t bit;
137     enum wl_pointer_button_state state = state_w;
138
139     bit = 1 << (button - BTN_LEFT);
140     if (state == WL_POINTER_BUTTON_STATE_PRESSED)
141         input->button_mask |= bit;
142     else
143         input->button_mask &= ~bit;
144     print_log("CLIENT: got pointer button %u %u", button, state_w);
145 }
146
147 static void
148 pointer_handle_axis(void *data, struct wl_pointer *pointer,
149                     uint32_t time, uint32_t axis, wl_fixed_t value)
150 {
151     fprintf(stderr, "CLIENT: got pointer axis %u %d\n", axis, value);
152 }
153
154 static void
155 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
156                        uint32_t format, int fd, uint32_t size)
157 {
158     close(fd);
159     print_log("CLIENT: got keyboard keymap");
160 }
161
162 static void
163 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
164                       uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
165 {
166     struct input *input = data;
167
168     input->keyboard_focus = wl_surface_get_user_data(surface);
169     print_log("CLIENT: got keyboard enter, surface %p", surface);
170 }
171
172 static void
173 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
174                       uint32_t serial, struct wl_surface *surface)
175 {
176     struct input *input = data;
177
178     input->keyboard_focus = NULL;
179     print_log("CLIENT: got keyboard leave, surface %p", surface);
180 }
181
182 static void
183 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
184                     uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
185 {
186     struct input *input = data;
187
188     input->last_key = key;
189     input->last_key_state = state;
190
191     print_log("CLIENT: got keyboard key %u %u", key, state);
192 }
193
194 static void
195 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
196                           uint32_t serial, uint32_t mods_depressed,
197                           uint32_t mods_latched, uint32_t mods_locked, uint32_t group)
198 {
199     print_log("CLIENT: got keyboard modifier");
200 }
201
202 static void
203 touch_handle_down(void *data, struct wl_touch *wl_touch, uint32_t serial, uint32_t time,
204                   struct wl_surface *surface, int32_t id, wl_fixed_t x, wl_fixed_t y)
205 {
206     print_log("CLIENT: got touch down %d (%d,%d)", id, x/256, y/256);
207 }
208
209 static void
210 touch_handle_up(void *data, struct wl_touch *wl_touch, uint32_t serial, uint32_t time,
211                 int32_t id)
212 {
213     print_log("CLIENT: got touch up %d", id);
214 }
215
216 static void
217 touch_handle_motion(void *data, struct wl_touch *wl_touch, uint32_t time,
218                     int32_t id, wl_fixed_t x, wl_fixed_t y)
219 {
220     print_log("CLIENT: got touch motion %d (%d,%d)", id, x/256, y/256);
221 }
222
223 static void
224 touch_handle_frame(void *data, struct wl_touch *wl_touch)
225 {
226     print_log("CLIENT: got touch frame");
227 }
228
229 static void
230 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
231 {
232     print_log("CLIENT: got touch cancel");
233 }
234
235 static const struct wl_pointer_listener pointer_listener = {
236     pointer_handle_enter,
237     pointer_handle_leave,
238     pointer_handle_motion,
239     pointer_handle_button,
240     pointer_handle_axis,
241 };
242
243 static const struct wl_keyboard_listener keyboard_listener = {
244     keyboard_handle_keymap,
245     keyboard_handle_enter,
246     keyboard_handle_leave,
247     keyboard_handle_key,
248     keyboard_handle_modifiers,
249 };
250
251 static const struct wl_touch_listener touch_listener = {
252     touch_handle_down,
253     touch_handle_up,
254     touch_handle_motion,
255     touch_handle_frame,
256     touch_handle_cancel
257 };
258
259 static void
260 seat_handle_capabilities(void *data, struct wl_seat *seat,
261                          enum wl_seat_capability caps)
262 {
263     struct input *input = data;
264
265     if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
266         input->pointer = wl_seat_get_pointer(seat);
267         wl_pointer_set_user_data(input->pointer, input);
268         wl_pointer_add_listener(input->pointer, &pointer_listener, input);
269     }
270     else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
271         wl_pointer_destroy(input->pointer);
272         input->pointer = NULL;
273     }
274
275     if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
276         input->keyboard = wl_seat_get_keyboard(seat);
277         wl_keyboard_set_user_data(input->keyboard, input);
278         wl_keyboard_add_listener(input->keyboard, &keyboard_listener, input);
279     }
280     else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
281         wl_keyboard_destroy(input->keyboard);
282         input->keyboard = NULL;
283     }
284
285     if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
286         input->touch = wl_seat_get_touch(seat);
287         wl_touch_set_user_data(input->touch, input);
288         wl_touch_add_listener(input->touch, &touch_listener, input);
289     }
290     else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
291         wl_touch_destroy(input->touch);
292         input->touch = NULL;
293     }
294 }
295
296 static const struct wl_seat_listener seat_listener = {
297     seat_handle_capabilities,
298 };
299
300 static void
301 output_handle_geometry(void *data, struct wl_output *wl_output, int x, int y,
302                        int physical_width, int physical_height, int subpixel,
303                        const char *make, const char *model, int32_t transform)
304 {
305     struct output *output = data;
306
307     print_log("CLIENT: Event[handle_geometry] x/y=%d/%d p.w/h=%d/%d trans=%d",
308               x, y, physical_width, physical_height, transform);
309
310     output->x = x;
311     output->y = y;
312 }
313
314 static void
315 output_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
316                    int width, int height, int refresh)
317 {
318     struct output *output = data;
319
320     print_log("CLIENT: Event[handle_mode] %08x x/y=%d/%d flags=%08x refresh=%d",
321               (int)wl_output, width, height, flags, refresh);
322
323     if (flags & WL_OUTPUT_MODE_CURRENT) {
324         output->width = width;
325         output->height = height;
326     }
327 }
328
329 static const struct wl_output_listener output_listener = {
330     output_handle_geometry,
331     output_handle_mode
332 };
333
334 static void
335 cb_input_capabilities(void *data, struct ico_exinput *ico_exinput,
336                       const char *device, int32_t type, const char *swname, int32_t input,
337                       const char *codename, int32_t code)
338 {
339     print_log("CLIENT: Event[input_capabilities] device=%s type=%d sw=%s input=%d "
340               "code=%s[%d]", device, type, swname, input, codename, code);
341 }
342
343 static void
344 cb_input_code(void *data, struct ico_exinput *ico_exinput,
345               const char *device, int32_t input, const char *codename, int32_t code)
346 {
347     print_log("CLIENT: Event[input_code] device=%s input=%d code=%s[%d]",
348               device, input, codename, code);
349 }
350
351 static void
352 cb_input_input(void *data, struct ico_exinput *ico_exinput, uint32_t time,
353                const char *device, int32_t input, int32_t code, int32_t state)
354 {
355     print_log("CLIENT: Event[input_input] device=%s input=%d code=%d state=%d",
356               device, input, code, state);
357 }
358
359 static const struct ico_exinput_listener exinput_listener = {
360     cb_input_capabilities,
361     cb_input_code,
362     cb_input_input
363 };
364
365 static void
366 shell_surface_ping(void *data, struct wl_shell_surface *wl_shell_surface, uint32_t serial)
367 {
368     print_log("CLIENT: shell_surface_ping: surface=%08x serial=%d",
369               (int)wl_shell_surface, serial);
370 }
371
372 static void
373 shell_surface_configure(void *data, struct wl_shell_surface *wl_shell_surface,
374                         uint32_t edges, int32_t width, int32_t height)
375 {
376     print_log("CLIENT: shell_surface_configure: surface=%08x edg=%x, width=%d height=%d",
377               (int)wl_shell_surface, edges, width, height);
378 }
379
380 static void
381 shell_surface_popup_done(void *data, struct wl_shell_surface *wl_shell_surface)
382 {
383     print_log("CLIENT: shell_surface_popup_done: surface=%08x", (int)wl_shell_surface);
384 }
385
386 static const struct wl_shell_surface_listener shell_surface_listener = {
387     shell_surface_ping,
388     shell_surface_configure,
389     shell_surface_popup_done
390 };
391
392 static void
393 handle_global(void *data, struct wl_registry *registry, uint32_t id,
394               const char *interface, uint32_t version)
395 {
396     struct display *display = data;
397     struct input *input;
398     struct output *output;
399
400     print_log("CLIENT: handle_global: interface=<%s> id=%d", interface, (int)id);
401
402     if (strcmp(interface, "wl_compositor") == 0) {
403         display->compositor = wl_registry_bind(display->registry, id,
404                                                &wl_compositor_interface, 1);
405     }
406     else if (strcmp(interface, "wl_seat") == 0) {
407         input = calloc(1, sizeof *input);
408         input->display = display;
409         input->seat = wl_registry_bind(display->registry, id, &wl_seat_interface, 1);
410         input->pointer_focus = NULL;
411         input->keyboard_focus = NULL;
412
413         wl_seat_add_listener(input->seat, &seat_listener, input);
414         display->input = input;
415     }
416     else if (strcmp(interface, "wl_output") == 0) {
417         output = malloc(sizeof *output);
418         output->display = display;
419         output->output = wl_registry_bind(display->registry, id, &wl_output_interface, 1);
420         wl_output_add_listener(output->output,
421                        &output_listener, output);
422         display->output = output;
423
424         print_log("CLIENT: created output global %p", display->output);
425     }
426     else if (strcmp(interface, "wl_shell") == 0)    {
427         display->shell = wl_registry_bind(display->registry, id, &wl_shell_interface, 1);
428     }
429     else if (strcmp(interface, "ico_window_mgr") == 0)  {
430 #if 0
431         display->ico_window_mgr = wl_registry_bind(display->registry, id,
432                                                    &ico_window_mgr_interface, 1);
433         print_log("CLIENT: created window_mgr global %p", display->ico_window_mgr);
434 #endif
435     }
436     else if (strcmp(interface, "ico_exinput") == 0)   {
437 #if 0
438         display->ico_exinput = wl_registry_bind(display->registry, id,
439                                                 &ico_exinput_interface, 1);
440         ico_exinput_add_listener(display->ico_exinput, &exinput_listener, display);
441         print_log("CLIENT: created exinput global %p", display->ico_exinput);
442 #endif
443     }
444 }
445
446 static const struct wl_registry_listener registry_listener = {
447     handle_global
448 };
449
450 static void
451 surface_enter(void *data, struct wl_surface *wl_surface, struct wl_output *output)
452 {
453     struct surface *surface = data;
454
455     surface->output = wl_output_get_user_data(output);
456
457     print_log("CLIENT: got surface enter, output %p", surface->output);
458 }
459
460 static void
461 surface_leave(void *data, struct wl_surface *wl_surface, struct wl_output *output)
462 {
463     struct surface *surface = data;
464
465     surface->output = NULL;
466
467     print_log("CLIENT: got surface leave, output %p", wl_output_get_user_data(output));
468 }
469
470 static const struct wl_surface_listener surface_listener = {
471     surface_enter,
472     surface_leave
473 };
474
475 static void
476 send_keyboard_state(struct display *display)
477 {
478     int focus = display->input->keyboard_focus != NULL;
479
480     if (focus) {
481         assert(display->input->keyboard_focus == display->surface);
482     }
483
484     wl_display_flush(display->display);
485
486     print_log("CLIENT: keyboard_state %u %u %d",
487               display->input->last_key, display->input->last_key_state, focus);
488
489     wl_display_roundtrip(display->display);
490 }
491
492 static void
493 send_button_state(struct display *display)
494 {
495     wl_display_roundtrip(display->display);
496
497     print_log("CLIENT: button_state %u", display->input->button_mask);
498
499     wl_display_roundtrip(display->display);
500 }
501
502 static void
503 send_state(struct display* display)
504 {
505     int visible = display->surface->output != NULL;
506     wl_fixed_t x = wl_fixed_from_int(-1);
507     wl_fixed_t y = wl_fixed_from_int(-1);
508
509     if (display->input->pointer_focus != NULL) {
510         assert(display->input->pointer_focus == display->surface);
511         x = wl_fixed_from_double(display->input->x);
512         y = wl_fixed_from_double(display->input->y);
513     }
514
515     if (visible) {
516         /* FIXME: this fails on multi-display setup */
517         /* assert(display->surface->output == display->output); */
518     }
519
520     wl_display_flush(display->display);
521
522     print_log("CLIENT: state %d %d %d", x, y, visible);
523
524     wl_display_roundtrip(display->display);
525 }
526
527 static void
528 create_surface(struct display *display, const char *title)
529 {
530     struct surface *surface;
531
532     surface = malloc(sizeof *surface);
533     assert(surface);
534     surface->display = display;
535     display->surface = surface;
536     surface->surface = wl_compositor_create_surface(display->compositor);
537     wl_surface_add_listener(surface->surface, &surface_listener, surface);
538
539     if (display->shell) {
540         surface->shell_surface =
541             wl_shell_get_shell_surface(display->shell, surface->surface);
542         if (surface->shell_surface) {
543             wl_shell_surface_add_listener(surface->shell_surface,
544                                           &shell_surface_listener, display);
545             wl_shell_surface_set_toplevel(surface->shell_surface);
546             wl_shell_surface_set_title(surface->shell_surface, title);
547         }
548     }
549     wl_display_flush(display->display);
550
551     print_log("CLIENT: create surface %d shell=%08x",
552               wl_proxy_get_id((struct wl_proxy *) surface->surface),
553               (int)surface->shell_surface);
554
555     poll(NULL, 0, 100); /* Wait for next frame where we'll get events. */
556
557     wl_display_roundtrip(display->display);
558
559     surface->dpy = opengl_init(display->display, &surface->conf, &surface->ctx);
560     if (surface->dpy)   {
561         surface->egl_surface = opengl_create_window(display->display, surface->surface,
562                                                     surface->dpy, surface->conf,
563                                                     surface->ctx, display->init_width,
564                                                     display->init_height,
565                                                     display->init_color);
566         clear_surface(display);
567         print_log("CLIENT: created egl_surface %08x", (int)surface->egl_surface);
568     }
569 }
570
571 static void
572 clear_surface(struct display *display)
573 {
574     if (! display->surface) {
575         create_surface(display, "test-client");
576     }
577     else    {
578         opengl_clear_window(display->init_color);
579         opengl_swap_buffer(display->display,
580                            display->surface->dpy, display->surface->egl_surface);
581     }
582 }
583
584 static void
585 set_region(struct display *display, char *buf)
586 {
587     char    *args[10];
588     int     narg;
589     int     x, y, width, height;
590     int     hot_x, hot_y;
591     int     c_x, c_y, c_width, c_height;
592
593     narg = pars_command(buf, args, 10);
594     if (narg >= 5)  {
595         x = strtol(args[1], (char **)0, 0);
596         y = strtol(args[2], (char **)0, 0);
597         width = strtol(args[3], (char **)0, 0);
598         height = strtol(args[4], (char **)0, 0);
599         hot_x = x + (width / 2);
600         hot_y = y + (height / 2);
601         c_x = x + 5;
602         c_y = y + 5;
603         c_width = width - 10;
604         if (c_width <= 0)   c_width = 2;
605         c_height = height - 10;
606         if (c_height <= 0)  c_height = 2;
607         print_log("CLIENT: ico_exinput_set_input_region(%s,%d,%d-%d,%d,"
608                   "hot=%d,%d,cur=%d,%d-%d,%d,attr=0)",
609                   args[0] ? args[0] : "(null)", x, y, width, height,
610                   hot_x, hot_y, c_x, c_y, c_width, c_height);
611         if (strcasecmp(args[0], "NULL") == 0)   {
612             ico_exinput_set_input_region(display->ico_exinput, "", x, y,
613                                          width, height, hot_x, hot_y, c_x, c_y,
614                                          c_width, c_height, 0);
615         }
616         else    {
617             ico_exinput_set_input_region(display->ico_exinput, args[0], x, y,
618                                          width, height, hot_x, hot_y, c_x, c_y,
619                                          c_width, c_height, 0);
620         }
621     }
622     else    {
623         print_log("CLIENT: set_region command[set_region winname@appid x y "
624                   "width height] has no argument");
625     }
626 }
627
628 static void
629 unset_region(struct display *display, char *buf)
630 {
631     char    *args[10];
632     int     narg;
633     int     x, y, width, height;
634
635     narg = pars_command(buf, args, 10);
636     if (narg >= 1)  {
637         if (narg >= 5) {
638             x = strtol(args[1], (char **)0, 0);
639             y = strtol(args[2], (char **)0, 0);
640             width = strtol(args[3], (char **)0, 0);
641             height = strtol(args[4], (char **)0, 0);
642         }
643         else    {
644             x = 0;
645             y = 0;
646             width = 0;
647             height = 0;
648         }
649         print_log("CLIENT: ico_exinput_unset_input_region(%s,08x,%d,%d-%d,%d)",
650                   args[0] ? args[0] : "(null)", x, y, width, height);
651         if (strcasecmp(args[0], "NULL") == 0)   {
652             ico_exinput_unset_input_region(display->ico_exinput, "", x, y,
653                                            width, height);
654         }
655         else    {
656             ico_exinput_unset_input_region(display->ico_exinput, args[0],
657                                            x, y, width, height);
658         }
659     }
660     else    {
661         print_log("CLIENT: unset_region command[unset_region winname@appid x y "
662                   "width height] has no argument");
663     }
664 }
665
666 int main(int argc, char *argv[])
667 {
668     struct display *display;
669     char buf[256];
670     int ret, fd;
671     int msec;
672     int postsec = 0;
673
674     display = malloc(sizeof *display);
675     assert(display);
676     memset((char *)display, 0, sizeof *display);
677
678     display->init_width = 640;
679     display->init_height = 480;
680     display->init_color = 0xA0A08020;
681     for (fd = 1; fd < argc; fd++ )  {
682         if (argv[fd][0] == '-') {
683             if (strncasecmp(argv[fd], "-color=", 7) == 0)   {
684                 display->init_color = strtoul(&argv[fd][7], (char **)0, 0);
685             }
686             else if (strncasecmp(argv[fd], "-width=", 7) == 0)  {
687                 display->init_width = strtol(&argv[fd][7], (char **)0, 0);
688             }
689             else if (strncasecmp(argv[fd], "-height=", 8) == 0) {
690                 display->init_height = strtol(&argv[fd][8], (char **)0, 0);
691             }
692             else if (strncasecmp(argv[fd], "-display=", 9) == 0)   {
693                 strncpy(display->connect, &argv[fd][9], MAX_CON_NAME);
694             }
695             else if (strncasecmp(argv[fd], "-postsleep=", 11) == 0)   {
696                 postsec = sec_str_2_value(&argv[fd][11]);
697             }
698             else if (strncasecmp(argv[fd], "-prompt=", 8) == 0)  {
699                 if (argv[fd][8] == 0)   {
700                     display->prompt = argv[fd][8] & 1;
701                 }
702                 else    {
703                     display->prompt = 1;
704                 }
705             }
706         }
707     }
708
709     if (display->connect[0])    {
710         display->display = wl_display_connect(display->connect);
711     }
712     else    {
713         display->display = wl_display_connect(NULL);
714     }
715     if (! display->display) {
716         fprintf(stderr, "CLIENT: can not connect to weston\n");
717         exit(2);
718     }
719
720     display->registry = wl_display_get_registry(display->display);
721     wl_registry_add_listener(display->registry,
722                  &registry_listener, display);
723     wl_display_dispatch(display->display);
724     sleep_with_wayland(display->display, 1000);
725
726     fd = 0;
727
728     while (1) {
729         sleep_with_wayland(display->display, 20);
730         if (display->prompt)    {
731             printf("CLIENT> "); fflush(stdout);
732         }
733         ret = getdata(display->ico_window_mgr, "CLIENT> ", fd, buf, sizeof(buf));
734         if (ret < 0) {
735             fprintf(stderr, "CLIENT: read error: fd %d, %m\n",
736                 fd);
737             break;
738         }
739         if (ret == 0)   continue;
740         wl_display_flush(display->display);
741
742         if ((strncasecmp(buf, "bye", 3) == 0) ||
743             (strncasecmp(buf, "quit", 4) == 0) ||
744             (strncasecmp(buf, "end", 3) == 0))  {
745             /* Exit, end of test            */
746             break;
747         }
748         else if (strncasecmp(buf, "create-surface", ret) == 0) {
749             create_surface(display, "test-client");
750         }
751         else if (strncasecmp(buf, "clear-surface", 13) == 0) {
752             display->init_color = strtoul(&buf[14], (char **)0, 0);
753             clear_surface(display);
754         }
755         else if (strncasecmp(buf, "set_region", 10) == 0) {
756             /* set input region                 */
757             set_region(display, &buf[10]);
758         }
759         else if (strncasecmp(buf, "unset_region", 12) == 0) {
760             /* unset input region               */
761             unset_region(display, &buf[12]);
762         }
763         else if (strncasecmp(buf, "send-state", ret) == 0) {
764             send_state(display);
765         }
766         else if (strncasecmp(buf, "send-button-state", ret) == 0) {
767             send_button_state(display);
768         }
769         else if (strncasecmp(buf, "send-keyboard-state", ret) == 0) {
770             send_keyboard_state(display);
771         }
772         else if (strncasecmp(buf, "sleep", 5) == 0) {
773             msec = sec_str_2_value(&buf[6]);
774             sleep_with_wayland(display->display, msec);
775         }
776         else {
777             print_log("CLIENT: unknown command[%s]", buf);
778             return -1;
779         }
780     }
781     if (postsec > 0)    {
782         sleep_with_wayland(display->display, postsec);
783     }
784
785     exit(0);
786 }
787