458a5b966e9f8d5227053c2133edf632ca516726
[profile/ivi/ico-uxf-weston-plugin.git] / tests / test-homescreen.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   HomeScreen 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 <errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/ipc.h>
38 #include <sys/msg.h>
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/mman.h>
43 #include <signal.h>
44 #include <fcntl.h>
45 #include <linux/input.h>
46 #include <wayland-client.h>
47 #include "ico_window_mgr-client-protocol.h"
48 #include "ico_input_mgr-client-protocol.h"
49 #include "ico_input_mgr.h"
50 #include "test-common.h"
51
52 #define MAX_APPID   128
53 #define SHM_SIZE    (16*1024*1024)
54 #define MAX_CON_NAME    127
55 #define MAX_OUTPUT      8
56
57 struct surface_name {
58     struct surface_name *next;
59     int     surfaceid;
60     int     pid;
61     char    appid[MAX_APPID];
62     int     node;
63     int     x;
64     int     y;
65     int     width;
66     int     height;
67     int     visible;
68 };
69
70 struct display {
71     struct wl_display *display;
72     struct wl_registry *registry;
73     struct wl_compositor *compositor;
74     struct wl_shell *shell;
75     struct ico_window_mgr *ico_window_mgr;
76     struct ico_input_mgr_control *ico_input_mgr;
77     struct ico_input_mgr_device *ico_input_device;
78     struct ico_exinput *ico_exinput;
79     struct input *input;
80     int    num_output;
81     struct output *output[MAX_OUTPUT];
82     struct surface *surface;
83     struct surface_name *surface_name;
84     struct surface_name *bgsurface_name;
85     int    bg_created;
86     int    init_color;
87     int    init_width;
88     int    init_height;
89     int    surface_created;
90     int    surface_destroyed;
91     int    prompt;
92     int    visible_on_create;
93     char   connect[MAX_CON_NAME+1];
94 };
95
96 struct input {
97     struct display *display;
98     struct wl_seat *seat;
99     struct wl_pointer *pointer;
100     struct wl_keyboard *keyboard;
101     struct wl_touch *touch;
102     float x, y;
103     uint32_t button_mask;
104     struct surface *pointer_focus;
105     struct surface *keyboard_focus;
106     uint32_t last_key, last_key_state;
107 };
108
109 struct output {
110     struct display *display;
111     struct wl_output *output;
112     int x, y;
113     int width, height;
114 };
115
116 struct surface {
117     struct display *display;
118     struct wl_surface *surface;
119     struct wl_shell_surface *shell_surface;
120     struct output *output;
121     EGLDisplay  dpy;
122     EGLConfig   conf;
123     EGLContext  ctx;
124     EGLSurface  egl_surface;
125 };
126
127 static void clear_surface(struct display *display);
128
129 static void
130 pointer_handle_enter(void *data, struct wl_pointer *pointer,
131                      uint32_t serial, struct wl_surface *surface,
132                      wl_fixed_t x, wl_fixed_t y)
133 {
134     struct input *input = data;
135
136     input->pointer_focus = wl_surface_get_user_data(surface);
137     input->x = wl_fixed_to_double(x);
138     input->y = wl_fixed_to_double(y);
139     print_log("HOMESCREEN: got pointer enter (%d,%d), surface %p",
140               (int)input->x, (int)input->y, surface);
141 }
142
143 static void
144 pointer_handle_leave(void *data, struct wl_pointer *pointer,
145                      uint32_t serial, struct wl_surface *surface)
146 {
147     struct input *input = data;
148
149     input->pointer_focus = NULL;
150
151     print_log("HOMESCREEN: got pointer leave, surface %p", surface);
152 }
153
154 static void
155 pointer_handle_motion(void *data, struct wl_pointer *pointer,
156                       uint32_t time, wl_fixed_t x, wl_fixed_t y)
157 {
158     struct input *input = data;
159
160     input->x = wl_fixed_to_double(x);
161     input->y = wl_fixed_to_double(y);
162
163     print_log("HOMESCREEN: got pointer motion (%d,%d)", (int)input->x, (int)input->y);
164 }
165
166 static void
167 pointer_handle_button(void *data, struct wl_pointer *pointer,
168                       uint32_t serial, uint32_t time, uint32_t button, uint32_t state_w)
169 {
170     struct input *input = data;
171     uint32_t bit;
172     enum wl_pointer_button_state state = state_w;
173
174     bit = 1 << (button - BTN_LEFT);
175     if (state == WL_POINTER_BUTTON_STATE_PRESSED)
176         input->button_mask |= bit;
177     else
178         input->button_mask &= ~bit;
179     print_log("HOMESCREEN: got pointer button %u %u", button, state_w);
180 }
181
182 static void
183 pointer_handle_axis(void *data, struct wl_pointer *pointer,
184                     uint32_t time, uint32_t axis, wl_fixed_t value)
185 {
186     print_log("HOMESCREEN: got pointer axis %u %d", axis, value);
187 }
188
189 static void
190 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
191                        uint32_t format, int fd, uint32_t size)
192 {
193     close(fd);
194     print_log("HOMESCREEN: got keyboard keymap");
195 }
196
197 static void
198 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
199                       uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
200 {
201     struct input *input = data;
202
203     input->keyboard_focus = wl_surface_get_user_data(surface);
204     print_log("HOMESCREEN: got keyboard enter, surface %p", surface);
205 }
206
207 static void
208 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
209                       uint32_t serial, struct wl_surface *surface)
210 {
211     struct input *input = data;
212
213     input->keyboard_focus = NULL;
214     print_log("HOMESCREEN: got keyboard leave, surface %p", surface);
215 }
216
217 static void
218 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
219                     uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
220 {
221     struct input *input = data;
222
223     input->last_key = key;
224     input->last_key_state = state;
225
226     print_log("HOMESCREEN: got keyboard key %u %u", key, state);
227 }
228
229 static void
230 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
231                           uint32_t serial, uint32_t mods_depressed,
232                           uint32_t mods_latched, uint32_t mods_locked, uint32_t group)
233 {
234     print_log("HOMESCREEN: got keyboard modifier");
235 }
236
237 static void
238 touch_handle_down(void *data, struct wl_touch *wl_touch, uint32_t serial, uint32_t time,
239                   struct wl_surface *surface, int32_t id, wl_fixed_t x, wl_fixed_t y)
240 {
241     print_log("HOMESCREEN: got touch down %d (%d,%d)", id, x/256, y/256);
242 }
243
244 static void
245 touch_handle_up(void *data, struct wl_touch *wl_touch, uint32_t serial, uint32_t time,
246                 int32_t id)
247 {
248     print_log("HOMESCREEN: got touch up %d", id);
249 }
250
251 static void
252 touch_handle_motion(void *data, struct wl_touch *wl_touch, uint32_t time,
253                     int32_t id, wl_fixed_t x, wl_fixed_t y)
254 {
255     print_log("HOMESCREEN: got touch motion %d (%d,%d)", id, x/256, y/256);
256 }
257
258 static void
259 touch_handle_frame(void *data, struct wl_touch *wl_touch)
260 {
261     print_log("HOMESCREEN: got touch frame");
262 }
263
264 static void
265 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
266 {
267     print_log("HOMESCREEN: got touch cancel");
268 }
269
270 static const struct wl_pointer_listener pointer_listener = {
271     pointer_handle_enter,
272     pointer_handle_leave,
273     pointer_handle_motion,
274     pointer_handle_button,
275     pointer_handle_axis,
276 };
277
278 static const struct wl_keyboard_listener keyboard_listener = {
279     keyboard_handle_keymap,
280     keyboard_handle_enter,
281     keyboard_handle_leave,
282     keyboard_handle_key,
283     keyboard_handle_modifiers,
284 };
285
286 static const struct wl_touch_listener touch_listener = {
287     touch_handle_down,
288     touch_handle_up,
289     touch_handle_motion,
290     touch_handle_frame,
291     touch_handle_cancel
292 };
293
294 static void
295 seat_handle_capabilities(void *data, struct wl_seat *seat,
296                          enum wl_seat_capability caps)
297 {
298     struct input *input = data;
299
300     print_log("HOMESCREEN: seat_handle_capabilities caps=%x", caps);
301
302     if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
303         input->pointer = wl_seat_get_pointer(seat);
304         print_log("HOMESCREEN: seat_handle_capabilities add pointer=%x", (int)input->pointer);
305         wl_pointer_set_user_data(input->pointer, input);
306         wl_pointer_add_listener(input->pointer, &pointer_listener, input);
307     }
308     else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
309         print_log("HOMESCREEN: seat_handle_capabilities delete pointer");
310         wl_pointer_destroy(input->pointer);
311         input->pointer = NULL;
312     }
313
314     if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
315         input->keyboard = wl_seat_get_keyboard(seat);
316         print_log("HOMESCREEN: seat_handle_capabilities add keyboard=%x", (int)input->keyboard);
317         wl_keyboard_set_user_data(input->keyboard, input);
318         wl_keyboard_add_listener(input->keyboard, &keyboard_listener, input);
319     }
320     else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
321         print_log("HOMESCREEN: seat_handle_capabilities delete keyboard");
322         wl_keyboard_destroy(input->keyboard);
323         input->keyboard = NULL;
324     }
325
326     if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
327         input->touch = wl_seat_get_touch(seat);
328         print_log("HOMESCREEN: seat_handle_capabilities add touch=%x", (int)input->touch);
329         wl_touch_set_user_data(input->touch, input);
330         wl_touch_add_listener(input->touch, &touch_listener, input);
331     }
332     else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
333         print_log("HOMESCREEN: seat_handle_capabilities delete touch");
334         wl_touch_destroy(input->touch);
335         input->touch = NULL;
336     }
337 }
338
339 static const struct wl_seat_listener seat_listener = {
340     seat_handle_capabilities,
341 };
342
343 static void
344 surface_enter(void *data, struct wl_surface *wl_surface, struct wl_output *output)
345 {
346     struct surface *surface = data;
347
348     surface->output = wl_output_get_user_data(output);
349
350     print_log("HOMESCREEN: got surface enter, output %p", surface->output);
351 }
352
353 static void
354 surface_leave(void *data, struct wl_surface *wl_surface, struct wl_output *output)
355 {
356     struct surface *surface = data;
357
358     surface->output = NULL;
359
360     print_log("HOMESCREEN: got surface leave, output %p",
361               wl_output_get_user_data(output));
362 }
363
364 static const struct wl_surface_listener surface_listener = {
365     surface_enter,
366     surface_leave
367 };
368
369 static void
370 create_surface(struct display *display, const char *title)
371 {
372     struct surface *surface;
373     int id;
374
375     surface = malloc(sizeof *surface);
376     assert(surface);
377     surface->display = display;
378     display->surface = surface;
379     surface->surface = wl_compositor_create_surface(display->compositor);
380     wl_surface_add_listener(surface->surface, &surface_listener, surface);
381
382     if (display->shell) {
383         surface->shell_surface =
384             wl_shell_get_shell_surface(display->shell, surface->surface);
385         if (surface->shell_surface) {
386             wl_shell_surface_set_toplevel(surface->shell_surface);
387             wl_shell_surface_set_title(surface->shell_surface, title);
388         }
389     }
390     wl_display_flush(display->display);
391
392     id = wl_proxy_get_id((struct wl_proxy *) surface->surface);
393     print_log("HOMESCREEN: create surface = %d", id);
394
395     poll(NULL, 0, 100); /* Wait for next frame where we'll get events. */
396
397     wl_display_roundtrip(display->display);
398
399     surface->dpy = opengl_init(display->display, &surface->conf, &surface->ctx);
400     if (surface->dpy)   {
401         surface->egl_surface = opengl_create_window(display->display, surface->surface,
402                                                     surface->dpy, surface->conf,
403                                                     surface->ctx, display->init_width,
404                                                     display->init_height,
405                                                     display->init_color);
406         clear_surface(display);
407         print_log("HOMESCREEN: created egl_surface %08x", (int)surface->egl_surface);
408     }
409 }
410
411 static void
412 clear_surface(struct display *display)
413 {
414     if (! display->surface) {
415         create_surface(display, "HomeScreen-BG");
416     }
417     else    {
418         opengl_clear_window(display->init_color);
419         opengl_swap_buffer(display->display,
420                            display->surface->dpy, display->surface->egl_surface);
421     }
422 }
423
424 static void
425 output_handle_geometry(void *data, struct wl_output *wl_output, int x, int y,
426                        int physical_width, int physical_height, int subpixel,
427                        const char *make, const char *model, int32_t transform)
428 {
429     struct output *output = data;
430
431     print_log("HOMESCREEN: Event[handle_geometry] %08x x/y=%d/%d p.w/h=%d/%d trans=%d",
432               (int)wl_output, x, y, physical_width, physical_height, transform);
433
434     output->x = x;
435     output->y = y;
436 }
437
438 static void
439 output_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
440                    int width, int height, int refresh)
441 {
442     struct output *output = data;
443
444     print_log("HOMESCREEN: Event[handle_mode] %08x x/y=%d/%d flags=%08x refresh=%d",
445               (int)wl_output, width, height, flags, refresh);
446
447     if (flags & WL_OUTPUT_MODE_CURRENT) {
448         struct display  *display = output->display;
449
450         output->width = width;
451         output->height = height;
452
453         display->init_width = width;
454         display->init_height = height;
455
456         if (display->bgsurface_name)    {
457             ico_window_mgr_set_positionsize(display->ico_window_mgr,
458                                             display->bgsurface_name->surfaceid,
459                                             0, 0, 0, 0, width, height);
460         }
461         else if (display->bg_created == 0)  {
462             display->bg_created = 9;
463             create_surface(display, "HomeScreen-BG");
464         }
465     }
466 }
467
468 static const struct wl_output_listener output_listener = {
469     output_handle_geometry,
470     output_handle_mode
471 };
472
473 static int
474 search_surface(struct display *display, const char *surfname)
475 {
476     struct surface_name     *p;
477
478     p = display->surface_name;
479     while (p)   {
480         if (strcmp(p->appid, surfname) == 0)    break;
481         p = p->next;
482     }
483
484     if (p)  {
485         return p->surfaceid;
486     }
487     else    {
488         if ((strcasecmp(surfname, "all") == 0) ||
489             (strcasecmp(surfname, "main") == 0))    {
490             return ICO_WINDOW_MGR_V_MAINSURFACE;
491         }
492         return -1;
493     }
494 }
495
496 static struct surface_name *
497 search_surfacename(struct display *display, const char *surfname)
498 {
499     struct surface_name     *p;
500
501     p = display->surface_name;
502     while (p)   {
503         if (strcmp(p->appid, surfname) == 0)    break;
504         p = p->next;
505     }
506     if (! p)    {
507         print_log("HOMESCREEN: app(%s) dose not exist", surfname);
508     }
509     return p;
510 }
511
512 static struct surface_name *
513 search_surfaceid(struct display *display, const int surfaceid)
514 {
515     struct surface_name     *p;
516
517     p = display->surface_name;
518     while (p)   {
519         if (p->surfaceid == surfaceid)  {
520             return p;
521         }
522         p = p->next;
523     }
524     return NULL;
525 }
526
527 static void
528 window_created(void *data, struct ico_window_mgr *ico_window_mgr,
529                uint32_t surfaceid, const char *winname, int32_t pid,
530                const char *appid, int32_t layertype)
531 {
532     struct display *display = data;
533     struct surface_name     *p;
534     struct surface_name     *fp;
535
536     display->surface_created = 1;
537     p = display->surface_name;
538     fp = NULL;
539     while (p)   {
540         if (p->surfaceid == (int)surfaceid) break;
541         fp = p;
542         p = p->next;
543     }
544     if (p)  {
545         print_log("HOMESCREEN: Event[window_created] "
546                   "surface=%08x(app=%s,name=%s,type=%x) exist",
547                   (int)surfaceid, appid, winname, layertype);
548     }
549     else    {
550         print_log("HOMESCREEN: Event[window_created] "
551                   "new surface=%08x(app=%s) winname=%s layertype=%x",
552                   (int)surfaceid, appid, winname, layertype);
553         p = malloc(sizeof(struct surface_name));
554         if (! p)    {
555             return;
556         }
557         memset(p, 0, sizeof(struct surface_name));
558         if (fp) {
559             fp->next = p;
560         }
561         else    {
562             display->surface_name = p;
563         }
564     }
565     p->surfaceid = surfaceid;
566     p->pid = pid;
567     strncpy(p->appid, appid, MAX_APPID-1);
568
569     /* Set default size and show        */
570     if (p->width > 0)   {
571         ico_window_mgr_set_positionsize(display->ico_window_mgr, surfaceid,
572                                         0, p->x, p->y, p->width, p->height, 0);
573     }
574
575     print_log("HOMESCREEN: Created window[%08x] (app=%s)", (int)surfaceid, appid);
576
577     if (strncasecmp(appid, "test-homescreen", 15) == 0) {
578         display->bgsurface_name = p;
579         if (display->bg_created == 1)   {
580             display->bg_created = 9;
581             ico_window_mgr_set_positionsize(display->ico_window_mgr, surfaceid,
582                                             0, 0, 0,
583                                             display->init_width, display->init_height, 0);
584         }
585         ico_window_mgr_set_visible(display->ico_window_mgr, surfaceid, 1, 0, 0);
586         print_log("HOMESCREEN: Created window[%08x] (app=%s) Visible",
587                   (int)surfaceid, appid);
588         p->visible = 1;
589     }
590 }
591
592 static void
593 window_name(void *data, struct ico_window_mgr *ico_window_mgr,
594             uint32_t surfaceid, const char *winname)
595 {
596     print_log("HOMESCREEN: Window Name[%08x] (name=%s)", (int)surfaceid, winname);
597 }
598
599 static void
600 window_destroyed(void *data, struct ico_window_mgr *ico_window_mgr, uint32_t surfaceid)
601 {
602     struct display *display = data;
603     struct surface_name     *p;
604     struct surface_name     *fp;
605
606     display->surface_destroyed = 1;
607     p = search_surfaceid(display, (int)surfaceid);
608     if (! p)    {
609         print_log("HOMESCREEN: Event[window_destroyed] surface=%08x dose not exist",
610                   (int)surfaceid);
611     }
612     else    {
613         print_log("HOMESCREEN: Event[window_destroyed] surface=%08x", (int)surfaceid);
614         if (p == display->surface_name) {
615             display->surface_name = p->next;
616         }
617         else    {
618             fp = display->surface_name;
619             while (fp)  {
620                 if (fp->next == p)  {
621                     fp->next = p->next;
622                     break;
623                 }
624                 fp = fp->next;
625             }
626         }
627         free(p);
628     }
629 }
630
631 static void
632 window_visible(void *data, struct ico_window_mgr *ico_window_mgr,
633                uint32_t surfaceid, int32_t visible, int32_t raise, int32_t hint)
634 {
635     struct display *display = data;
636     struct surface_name     *p;
637
638     p = search_surfaceid(display, (int)surfaceid);
639     if (! p)    {
640         print_log("HOMESCREEN: Event[window_visible] surface=%08x dose not exist",
641                   (int)surfaceid);
642     }
643     else    {
644         print_log("HOMESCREEN: Event[window_visible] surface=%08x "
645                   "visible=%d raise=%d hint=%d", (int)surfaceid, visible, raise, hint);
646         p->visible = visible;
647         if (hint == 1)  {
648             ico_window_mgr_set_visible(display->ico_window_mgr, surfaceid,
649                                        visible, ICO_WINDOW_MGR_V_NOCHANGE, 0);
650         }
651     }
652 }
653
654 static void
655 window_configure(void *data, struct ico_window_mgr *ico_window_mgr,
656                  uint32_t surfaceid, uint32_t node, int32_t layertype, uint32_t layer,
657                  int32_t x, int32_t y, int32_t width, int32_t height, int32_t hint)
658 {
659     struct display *display = data;
660     struct surface_name     *p;
661
662     print_log("HOMESCREEN: Event[window_configure] surface=%08x "
663               "node=%x layer=%x.%x x/y=%d/%d w/h=%d/%d hint=%d",
664               (int)surfaceid, node, layertype, layer, x, y, width, height, hint);
665
666     p = search_surfaceid(display, (int)surfaceid);
667     if (! p)    {
668         print_log("HOMESCREEN: Event[window_configure] surface=%08x dose not exist",
669                   (int)surfaceid);
670     }
671     else    {
672         p->node = node;
673     }
674 }
675
676 static void
677 window_layer_visible(void *data, struct ico_window_mgr *ico_window_mgr,
678                      uint32_t layer, int32_t visible)
679 {
680     print_log("HOMESCREEN: Event[layer_visible]layer=%x visible=%d",
681               (int)layer, visible);
682 }
683
684 static void
685 window_active(void *data, struct ico_window_mgr *ico_window_mgr,
686               uint32_t surfaceid, const int32_t active)
687 {
688     print_log("HOMESCREEN: Event[window_active] surface=%08x acive=%d",
689               (int)surfaceid, (int)active);
690     if ((surfaceid & 0x0000ffff) == 0x0001) {
691         ico_window_mgr_set_visible(ico_window_mgr, surfaceid,
692                                    ICO_WINDOW_MGR_V_NOCHANGE, 0, 0);
693     }
694 }
695
696 static void
697 window_surfaces(void *data, struct ico_window_mgr *ico_window_mgr,
698                 const char *appid, int32_t pid, struct wl_array *surfaces)
699 {
700     print_log("HOMESCREEN: Event[app_surfaces] app=%s pid=%d", appid, pid);
701 }
702
703 static void
704 window_map(void *data, struct ico_window_mgr *ico_window_mgr,
705            int32_t event, uint32_t surfaceid, uint32_t type, uint32_t target,
706            int32_t width, int32_t height, int32_t stride, uint32_t format)
707 {
708     struct display *display = data;
709     char    sevt[16];
710
711     switch (event)  {
712     case ICO_WINDOW_MGR_MAP_SURFACE_EVENT_CONTENTS:
713         strcpy(sevt, "Contents");   break;
714     case ICO_WINDOW_MGR_MAP_SURFACE_EVENT_RESIZE:
715         strcpy(sevt, "Resize"); break;
716     case ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP:
717         strcpy(sevt, "Map");    break;
718     case ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP:
719         strcpy(sevt, "Unmap");  break;
720     case ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR:
721         sprintf(sevt, "Error %d", type);  break;
722     default:
723         sprintf(sevt, "??%d??", event); break;
724     }
725     print_log("HOMESCREEN: Event[map_surface] ev=%s(%d) surf=%08x type=%d target=%x "
726               "w/h/s/f=%d/%d/%d/%x",
727               sevt, event, (int)surfaceid, type, target, width, height, stride, format);
728     if ((event == ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP) ||
729         (event == ICO_WINDOW_MGR_MAP_SURFACE_EVENT_CONTENTS))   {
730         opengl_thumbnail(display->display, surfaceid, display->surface->dpy,
731                          display->surface->conf, display->surface->egl_surface,
732                          display->surface->ctx, target, width, height, stride, format);
733     }
734 }
735
736 static const struct ico_window_mgr_listener window_mgr_listener = {
737     window_created,
738     window_name,
739     window_destroyed,
740     window_visible,
741     window_configure,
742     window_active,
743     window_layer_visible,
744     window_surfaces,
745     window_map
746 };
747
748 static void
749 cb_input_capabilities(void *data, struct ico_exinput *ico_exinput,
750                       const char *device, int32_t type, const char *swname, int32_t input,
751                       const char *codename, int32_t code)
752 {
753     print_log("HOMESCREEN: Event[input_capabilities] device=%s type=%d sw=%s input=%d "
754               "code=%s[%d]", device, type, swname, input, codename, code);
755 }
756
757 static void
758 cb_input_code(void *data, struct ico_exinput *ico_exinput,
759               const char *device, int32_t input, const char *codename, int32_t code)
760 {
761     print_log("HOMESCREEN: Event[input_code] device=%s input=%d code=%s[%d]",
762               device, input, codename, code);
763 }
764
765 static void
766 cb_input_input(void *data, struct ico_exinput *ico_exinput, uint32_t time,
767                const char *device, int32_t input, int32_t code, int32_t state)
768 {
769     print_log("HOMESCREEN: Event[input_input] device=%s input=%d code=%d state=%d",
770               device, input, code, state);
771 }
772
773 static const struct ico_exinput_listener exinput_listener = {
774     cb_input_capabilities,
775     cb_input_code,
776     cb_input_input
777 };
778
779 static void
780 cb_input_regions(void *data, struct ico_input_mgr_device *ico_input_mgr_device,
781                  struct wl_array *regions)
782 {
783     struct ico_uifw_input_region    *region;
784     int     n;
785     char    schange[16];
786
787     n = 0;
788     if (regions)    {
789         wl_array_for_each(region, regions)  {
790             n ++;
791         }
792         print_log("HOMESCREEN: Event[input_regions] number of regions=%d", n);
793         n = 0;
794         wl_array_for_each(region, regions)  {
795             n ++;
796             switch (region->change) {
797             case ICO_INPUT_MGR_DEVICE_REGION_ADD:
798                 strcpy(schange, "Add");
799                 break;
800             case ICO_INPUT_MGR_DEVICE_REGION_REMOVE:
801                 strcpy(schange, "Remove");
802                 break;
803             case ICO_INPUT_MGR_DEVICE_REGION_REMOVEALL:
804                 strcpy(schange, "RemoveAll");
805                 break;
806             default:
807                 sprintf(schange, "?%d?", region->change);
808                 break;
809             }
810             print_log("HOMESCREEN:%2d. %s %d.%08x(%d/%d) %d/%d-%d/%d "
811                       "hot=%d/%d cur=%d/%d-%d/%d attr=%x",
812                       n, schange, region->node, region->surfaceid, region->surface_x,
813                       region->surface_y, region->x, region->y, region->width,
814                       region->height, region->hotspot_x, region->hotspot_y,
815                       region->cursor_x, region->cursor_y, region->cursor_width,
816                       region->cursor_height, region->attr);
817         }
818     }
819     else    {
820         print_log("HOMESCREEN: Event[input_regions] no region");
821     }
822 }
823
824 static const struct ico_input_mgr_device_listener device_listener = {
825     cb_input_regions
826 };
827
828
829 static void
830 handle_global(void *data, struct wl_registry *registry, uint32_t id,
831               const char *interface, uint32_t version)
832 {
833     struct display *display = data;
834     struct input *input;
835     struct output *output;
836
837     print_log("HOMESCREEN: handle_global: interface=<%s> id=%d", interface, (int)id);
838
839     if (strcmp(interface, "wl_compositor") == 0) {
840         display->compositor =
841             wl_registry_bind(display->registry, id, &wl_compositor_interface, 1);
842     }
843     else if (strcmp(interface, "wl_seat") == 0) {
844         input = calloc(1, sizeof *input);
845         input->display = display;
846         input->seat = wl_registry_bind(display->registry, id, &wl_seat_interface, 1);
847         input->pointer_focus = NULL;
848         input->keyboard_focus = NULL;
849
850         wl_seat_add_listener(input->seat, &seat_listener, input);
851         display->input = input;
852     }
853     else if (strcmp(interface, "wl_output") == 0) {
854         if (display->num_output < MAX_OUTPUT)   {
855             output = malloc(sizeof *output);
856             output->display = display;
857             output->output = wl_registry_bind(display->registry, id, &wl_output_interface, 1);
858             wl_output_add_listener(output->output, &output_listener, output);
859             display->output[display->num_output] = output;
860
861             print_log("HOMESCREEN: created output[%d] global %p",
862                       display->num_output, display->output[display->num_output]);
863             display->num_output ++;
864         }
865     }
866     else if (strcmp(interface, "wl_shell") == 0)    {
867         display->shell =
868             wl_registry_bind(display->registry, id, &wl_shell_interface, 1);
869     }
870     else if (strcmp(interface, "ico_window_mgr") == 0)  {
871         display->ico_window_mgr =
872             wl_registry_bind(display->registry, id, &ico_window_mgr_interface, 1);
873         ico_window_mgr_add_listener(display->ico_window_mgr, &window_mgr_listener, display);
874         print_log("HOMESCREEN: created window_mgr global %p", display->ico_window_mgr);
875
876         ico_window_mgr_declare_manager(display->ico_window_mgr, 1);
877     }
878     else if (strcmp(interface, "ico_input_mgr_control") == 0)   {
879         display->ico_input_mgr = wl_registry_bind(display->registry, id,
880                                                   &ico_input_mgr_control_interface, 1);
881         print_log("HOMESCREEN: created input_mgr global %p", display->ico_input_mgr);
882     }
883     else if (strcmp(interface, "ico_input_mgr_device") == 0)   {
884         display->ico_input_device = wl_registry_bind(display->registry, id,
885                                                      &ico_input_mgr_device_interface, 1);
886         ico_input_mgr_device_add_listener(display->ico_input_device,
887                                           &device_listener, display);
888         print_log("HOMESCREEN: created input_device global %p", display->ico_input_device);
889     }
890     else if (strcmp(interface, "ico_exinput") == 0)   {
891         display->ico_exinput =
892             wl_registry_bind(display->registry, id, &ico_exinput_interface, 1);
893         ico_exinput_add_listener(display->ico_exinput, &exinput_listener, display);
894         print_log("HOMESCREEN: created exinput global %p", display->ico_exinput);
895
896         ico_window_mgr_declare_manager(display->ico_window_mgr, 1);
897
898         display->bg_created = 1;
899         create_surface(display, "HomeScreen-BG");
900     }
901 }
902
903 static const struct wl_registry_listener registry_listener = {
904     handle_global
905 };
906
907 static void
908 launch_app(struct display *display, char *buf)
909 {
910     char    sbuf[256];
911
912     display->surface_created = 0;
913     display->surface_destroyed = 0;
914     snprintf(sbuf, sizeof(sbuf)-1, "%s &", skip_spaces(buf));
915     if (system(sbuf) < 0)   {
916         print_log("HOMESCREEN: Can not launch application[%s]", sbuf);
917     }
918     else    {
919         sleep_with_wayland(display->display, 500);
920     }
921 }
922
923 static void
924 kill_app(struct display *display, char *buf)
925 {
926     char    *args[10];
927     int     narg;
928     struct surface_name     *p;
929     struct surface_name     *fp;
930
931     narg = pars_command(buf, args, 10);
932     if (narg >= 1)  {
933         p = search_surfacename(display, args[0]);
934         if (! p)    {
935             print_log("HOMESCREEN: kill[%s] Application dose not exist", args[0]);
936         }
937         else if (kill(p->pid, SIGINT) < 0)   {
938             print_log("HOMESCREEN: kill[%s.%d] Application dose not exist",
939                       p->appid, p->pid);
940         }
941         else    {
942             sleep_with_wayland(display->display, 300);
943             p = search_surfacename(display, args[0]);
944             if ((p != NULL) && (kill(p->pid, SIGTERM) >= 0))    {
945                 sleep_with_wayland(display->display, 200);
946                 p = search_surfacename(display, args[0]);
947                 if (p)  {
948                     kill(p->pid, SIGKILL);
949                     sleep_with_wayland(display->display, 200);
950                 }
951             }
952         }
953         p = search_surfacename(display, args[0]);
954         if (p)  {
955             if (p == display->surface_name) {
956                 display->surface_name = p->next;
957             }
958             else    {
959                 fp = display->surface_name;
960                 while (fp)  {
961                     if (fp->next == p)  {
962                         fp->next = p->next;
963                         break;
964                     }
965                 }
966             }
967             free(p);
968         }
969     }
970     else    {
971         print_log("HOMESCREEN: kill command[kill appid] has no argument");
972     }
973 }
974
975 static void
976 layer_surface(struct display *display, char *buf)
977 {
978     char    *args[10];
979     int     narg;
980     int     surfaceid;
981     int     layerid;
982
983     narg = pars_command(buf, args, 10);
984     if (narg >= 2)  {
985         surfaceid = search_surface(display, args[0]);
986         layerid = strtol(args[1], (char **)0, 0);
987         if ((surfaceid >= 0) && (layerid >= 0)) {
988             print_log("HOMESCREEN: set_window_layer(%s,%08x)",
989                       args[0], surfaceid, layerid);
990             ico_window_mgr_set_window_layer(display->ico_window_mgr, surfaceid, layerid);
991         }
992         else    {
993             print_log("HOMESCREEN: Unknown surface(%s) at layer command", args[0]);
994         }
995     }
996     else    {
997         print_log("HOMESCREEN: layer command[layer appid layerid] has no argument");
998     }
999 }
1000
1001 static void
1002 positionsize_surface(struct display *display, char *buf)
1003 {
1004     char    *args[10];
1005     struct surface_name     *p;
1006     int     narg;
1007     int     surfaceid;
1008     int     x, y, width, height;
1009     int     anima = 0;
1010     int     node = 0;
1011
1012     narg = pars_command(buf, args, 10);
1013     if (narg >= 5)  {
1014         surfaceid = search_surface(display, args[0]);
1015         p = search_surfacename(display, args[0]);
1016         x = strtol(args[1], (char **)0, 0);
1017         y = strtol(args[2], (char **)0, 0);
1018         width = strtol(args[3], (char **)0, 0);
1019         height = strtol(args[4], (char **)0, 0);
1020         if (narg >= 6)  {
1021             node = strtol(args[5], (char **)0, 0);
1022             if (p)  {
1023                 p->node = node;
1024             }
1025         }
1026         else if (p) {
1027             node = p->node;
1028         }
1029         if (narg >= 7)  {
1030             anima = strtol(args[6], (char **)0, 0);
1031         }
1032         if ((surfaceid >= 0) && (x >= 0) && (y >=0) && (width >= 0) && (height >=0))    {
1033             print_log("HOMESCREEN: set_positionsize(%s,%08x,%d,%d,%d,%d,%d)",
1034                       args[0], surfaceid, node, x, y, width, height);
1035             ico_window_mgr_set_positionsize(display->ico_window_mgr, surfaceid,
1036                                             node, x, y, width, height, anima);
1037         }
1038         else    {
1039             print_log("HOMESCREEN: Unknown surface(%s) at positionsize command", args[0]);
1040         }
1041     }
1042     else    {
1043         print_log("HOMESCREEN: positionsize command"
1044                   "[positionsize appid x y width heigh node anima] has no argument");
1045     }
1046 }
1047
1048 static void
1049 move_surface(struct display *display, char *buf)
1050 {
1051     char    *args[10];
1052     struct surface_name     *p;
1053     int     narg;
1054     int     surfaceid;
1055     int     x, y;
1056     int     anima = 0;
1057     int     node = 0;
1058
1059     narg = pars_command(buf, args, 10);
1060     if (narg >= 3)  {
1061         surfaceid = search_surface(display, args[0]);
1062         p = search_surfacename(display, args[0]);
1063         x = strtol(args[1], (char **)0, 0);
1064         y = strtol(args[2], (char **)0, 0);
1065         if (narg >= 4)  {
1066             node = strtol(args[3], (char **)0, 0);
1067             if (node < 0)   {
1068                 if (p)  node = p->node;
1069                 else    node = 0;
1070             }
1071             if (p)  p->node = node;
1072         }
1073         else if (p) {
1074             node = p->node;
1075         }
1076         if (narg >= 5)  {
1077             anima = strtol(args[4], (char **)0, 0);
1078         }
1079
1080         if ((surfaceid >= 0) && (x >= 0) && (y >=0))    {
1081             print_log("HOMESCREEN: move(%s,%08x,%d.%d,%d anima=%d)", args[0], surfaceid,
1082                       node, x, y, anima);
1083             ico_window_mgr_set_positionsize(display->ico_window_mgr, surfaceid,
1084                                             node, x, y,
1085                                             ICO_WINDOW_MGR_V_NOCHANGE,
1086                                             ICO_WINDOW_MGR_V_NOCHANGE, anima);
1087         }
1088         else    {
1089             print_log("HOMESCREEN: Unknown surface(%s) at move command", args[0]);
1090         }
1091     }
1092     else    {
1093         print_log("HOMESCREEN: move command[positionsize appid x y node anima] has no argument");
1094     }
1095 }
1096
1097 static void
1098 resize_surface(struct display *display, char *buf)
1099 {
1100     char    *args[10];
1101     struct surface_name     *p;
1102     int     narg;
1103     int     surfaceid;
1104     int     width, height;
1105     int     anima = 0;
1106     int     node = 0;
1107
1108     narg = pars_command(buf, args, 10);
1109     if (narg >= 3)  {
1110         surfaceid = search_surface(display, args[0]);
1111         p = search_surfacename(display, args[0]);
1112         if (p)  {
1113             node = p->node;
1114         }
1115         width = strtol(args[1], (char **)0, 0);
1116         height = strtol(args[2], (char **)0, 0);
1117         if (narg >= 4)  {
1118             anima = strtol(args[3], (char **)0, 0);
1119         }
1120
1121         if ((surfaceid >= 0) && (width >= 0) && (height >=0))   {
1122             print_log("HOMESCREEN: resize(%s,%08x,%d.%d,%d,anima=%d)",
1123                       args[0], surfaceid, node, width, height, anima);
1124             ico_window_mgr_set_positionsize(display->ico_window_mgr, surfaceid,
1125                                             node, ICO_WINDOW_MGR_V_NOCHANGE,
1126                                             ICO_WINDOW_MGR_V_NOCHANGE, width, height, anima);
1127         }
1128         else    {
1129             print_log("HOMESCREEN: Unknown surface(%s) at resize command", args[0]);
1130         }
1131     }
1132     else    {
1133         print_log("HOMESCREEN: positionsize command"
1134                   "[resize appid width heigh anima] has no argument");
1135     }
1136 }
1137
1138 static void
1139 visible_surface(struct display *display, char *buf)
1140 {
1141     char    *args[10];
1142     int     narg;
1143     int     surfaceid;
1144     int     visible;
1145     int     raise;
1146     int     anima = 0;
1147
1148     narg = pars_command(buf, args, 10);
1149     if (narg >= 3)  {
1150         surfaceid = search_surface(display, args[0]);
1151         visible = strtol(args[1], (char **)0, 0);
1152         raise = strtol(args[2], (char **)0, 0);
1153         if (narg >= 4)  {
1154             anima = strtol(args[3], (char **)0, 0);
1155         }
1156         if ((surfaceid >= 0) && (visible >= 0) && (raise >=0))  {
1157             print_log("HOMESCREEN: visible(%s,%08x,%d,%d,%d)",
1158                       args[0], surfaceid, visible, raise, anima);
1159             ico_window_mgr_set_visible(display->ico_window_mgr, surfaceid,
1160                                        visible, raise, anima);
1161         }
1162         else    {
1163             print_log("HOMESCREEN: Unknown surface(%s) at visible command", args[0]);
1164         }
1165     }
1166     else    {
1167         print_log("HOMESCREEN: visible command[visible appid visible raise] "
1168                   "has no argument");
1169     }
1170 }
1171
1172 static void
1173 show_surface(struct display *display, char *buf, const int show)
1174 {
1175     char    *args[10];
1176     int     narg;
1177     int     surfaceid;
1178     int     anima = 0;
1179     int     ax = 0;
1180     int     ay = 0;
1181     int     awidth = 1;
1182     int     aheight = 1;
1183
1184     narg = pars_command(buf, args, 10);
1185     if (narg >= 1)  {
1186         surfaceid = search_surface(display, args[0]);
1187         if (narg >= 2)  {
1188             anima = strtol(args[1], (char **)0, 0);
1189             if (anima >= 2) {
1190                 ax = 0;
1191                 ay = 0;
1192                 awidth = 1;
1193                 aheight = 1;
1194                 if (narg >= 3)  ax = strtol(args[2], (char **)0, 0);
1195                 if (narg >= 4)  ay = strtol(args[3], (char **)0, 0);
1196                 if (narg >= 5)  awidth = strtol(args[4], (char **)0, 0);
1197                 if (narg >= 6)  aheight = strtol(args[5], (char **)0, 0);
1198             }
1199         }
1200         if (surfaceid >= 0) {
1201             if (show)   {
1202                 if (anima >= 2) {
1203                     print_log("HOMESCREEN: show anima(%s,%08x,x/y=%d/%d,w/h=%d/%d)",
1204                               args[0], surfaceid, ax, ay, awidth, aheight);
1205                     ico_window_mgr_visible_animation(display->ico_window_mgr, surfaceid,
1206                                                      1, ax, ay, awidth, aheight);
1207                 }
1208                 else    {
1209                     print_log("HOMESCREEN: show(%s,%08x,anima=%d)",
1210                               args[0], surfaceid, anima);
1211                     ico_window_mgr_set_visible(display->ico_window_mgr, surfaceid,
1212                                                1, ICO_WINDOW_MGR_V_NOCHANGE, anima);
1213                 }
1214             }
1215             else    {
1216                 if (anima >= 2) {
1217                     print_log("HOMESCREEN: hide anima(%s,%08x,x/y=%d/%d,w/h=%d/%d)",
1218                               args[0], surfaceid, ax, ay, awidth, aheight);
1219                     ico_window_mgr_visible_animation(display->ico_window_mgr, surfaceid,
1220                                                      0, ax, ay, awidth, aheight);
1221                 }
1222                 else    {
1223                     print_log("HOMESCREEN: hide(%s,%08x,anima=%d)",
1224                               args[0], surfaceid, anima);
1225                     ico_window_mgr_set_visible(display->ico_window_mgr, surfaceid,
1226                                                0, ICO_WINDOW_MGR_V_NOCHANGE, anima);
1227                 }
1228             }
1229         }
1230         else    {
1231             print_log("HOMESCREEN: Unknown surface(%s) at show/hide command", args[0]);
1232         }
1233     }
1234     else    {
1235         print_log("HOMESCREEN: show command[show/hide appid anima x y width height]"
1236                   " has no argument");
1237     }
1238 }
1239
1240 static void
1241 raise_surface(struct display *display, char *buf, const int raise)
1242 {
1243     char    *args[10];
1244     int     narg;
1245     int     surfaceid;
1246     int     anima = 0;
1247
1248     narg = pars_command(buf, args, 10);
1249     if (narg >= 1)  {
1250         surfaceid = search_surface(display, args[0]);
1251         if (narg >= 2)  {
1252             anima = strtol(args[1], (char **)0, 0);
1253         }
1254         if (surfaceid >= 0) {
1255             if (raise)  {
1256                 print_log("HOMESCREEN: raise(%s,%08x,anima=%d)", args[0], surfaceid, anima);
1257                 ico_window_mgr_set_visible(display->ico_window_mgr, surfaceid,
1258                                            ICO_WINDOW_MGR_V_NOCHANGE, 1, anima);
1259             }
1260             else    {
1261                 print_log("HOMESCREEN: lower(%s,%08x,anima=%d)", args[0], surfaceid, anima);
1262                 ico_window_mgr_set_visible(display->ico_window_mgr, surfaceid,
1263                                            ICO_WINDOW_MGR_V_NOCHANGE, 0, anima);
1264             }
1265         }
1266         else    {
1267             print_log("HOMESCREEN: Unknown surface(%s) at raise/lower command", args[0]);
1268         }
1269     }
1270     else    {
1271         print_log("HOMESCREEN: show command[raise/lower appid anima] has no argument");
1272     }
1273 }
1274
1275 static void
1276 active_window(struct display *display, char *buf)
1277 {
1278     char    *args[10];
1279     int     narg;
1280     int     surfaceid;
1281     int     target;
1282
1283     narg = pars_command(buf, args, 10);
1284     if (narg >= 1)  {
1285         surfaceid = search_surface(display, args[0]);
1286         if (narg >= 2)  {
1287             target = strtol(args[1], (char **)0, 0);
1288         }
1289         else    {
1290             target = ICO_WINDOW_MGR_ACTIVE_POINTER | ICO_WINDOW_MGR_ACTIVE_KEYBOARD;
1291         }
1292         if (surfaceid >= 0) {
1293             print_log("HOMESCREEN: active(%s,%08x,target=%x)", args[0], surfaceid, target);
1294             ico_window_mgr_set_active(display->ico_window_mgr, surfaceid, target);
1295         }
1296         else    {
1297             print_log("HOMESCREEN: Unknown surface(%s) at active command", args[0]);
1298         }
1299     }
1300     else    {
1301         print_log("HOMESCREEN: active command[active appid[target]] has no argument");
1302     }
1303 }
1304
1305
1306 static void
1307 animation_surface(struct display *display, char *buf)
1308 {
1309     char    *args[10];
1310     int     narg;
1311     int     surfaceid;
1312     int     time;
1313
1314     narg = pars_command(buf, args, 10);
1315     if (narg >= 2)  {
1316         surfaceid = search_surface(display, args[0]);
1317         if (surfaceid >= 0) {
1318             if (narg >= 3)  {
1319                 time = strtol(args[2], (char **)0, 0);
1320             }
1321             else    {
1322                 time = 0;
1323             }
1324             print_log("HOMESCREEN: animation(%s,%08x,%s,%d)",
1325                       args[0], surfaceid, args[1], time);
1326             ico_window_mgr_set_animation(display->ico_window_mgr, surfaceid, 0x7fffffff,
1327                                          args[1], time);
1328         }
1329         else    {
1330             print_log("HOMESCREEN: Unknown surface(%s) at animation command", args[0]);
1331         }
1332     }
1333     else    {
1334         print_log("HOMESCREEN: animation command"
1335                   "[animation appid animation time] has no argument");
1336     }
1337 }
1338
1339 static void
1340 map_surface(struct display *display, char *buf, int map)
1341 {
1342     char    *args[10];
1343     int     narg;
1344     int     surfaceid;
1345     int     fps;
1346
1347     narg = pars_command(buf, args, 10);
1348     if (narg >= 1)  {
1349         surfaceid = search_surface(display, args[0]);
1350         if (surfaceid >= 0) {
1351             if (narg >= 2)  {
1352                 fps = strtol(args[1], (char **)0, 0);
1353             }
1354             else    {
1355                 fps = 0;
1356             }
1357             if (map)    {
1358                 print_log("HOMESCREEN: map surface(%s,%08x,%d)",
1359                           args[0], surfaceid, fps);
1360                 ico_window_mgr_map_surface(display->ico_window_mgr, surfaceid, fps);
1361             }
1362             else    {
1363                 print_log("HOMESCREEN: unmap surface(%s,%08x)", args[0], surfaceid);
1364                 ico_window_mgr_unmap_surface(display->ico_window_mgr, surfaceid);
1365             }
1366         }
1367         else    {
1368             print_log("HOMESCREEN: Unknown surface(%s) at %s command", args[0],
1369                       map ? "map" : "unmap");
1370         }
1371     }
1372     else    {
1373         if (map)    {
1374             print_log("HOMESCREEN: map surface command"
1375                       "[map surface framerate] has no argument");
1376         }
1377         else    {
1378             print_log("HOMESCREEN: unmap surface command"
1379                       "[unmap surface] has no argument");
1380         }
1381     }
1382 }
1383
1384 static void
1385 visible_layer(struct display *display, char *buf)
1386 {
1387     char    *args[10];
1388     int     narg;
1389     int     layer;
1390     int     visible;
1391
1392     narg = pars_command(buf, args, 10);
1393     if (narg >= 2)  {
1394         layer = strtol(args[0], (char **)0, 0);
1395         visible = strtol(args[1], (char **)0, 0);
1396         ico_window_mgr_set_layer_visible(display->ico_window_mgr, layer, visible);
1397     }
1398     else    {
1399         print_log("HOMESCREEN: layer_visible command"
1400                   "[layer_visible layer visible] has no argument");
1401     }
1402 }
1403
1404 static void
1405 input_add(struct display *display, char *buf)
1406 {
1407     char    *args[10];
1408     int     narg;
1409     int     input;
1410     int     fix;
1411
1412     narg = pars_command(buf, args, 10);
1413     if (narg >= 3)  {
1414         input = strtol(args[1], (char **)0, 0);
1415         if (narg >= 4)  {
1416             fix = strtol(args[3], (char **)0, 0);
1417         }
1418         else    {
1419             fix = 0;
1420         }
1421         if ((input >= 0) && (fix >=0))  {
1422             print_log("HOMESCREEN: input_add(%s.%d to %s[%d])",
1423                       args[0], input, args[2], fix);
1424             ico_input_mgr_control_add_input_app(display->ico_input_mgr,
1425                                                 args[2], args[0], input, fix, 0);
1426         }
1427         else    {
1428             print_log("HOMESCREEN: Unknown input(%s) at input_add command", args[1]);
1429         }
1430     }
1431     else    {
1432         print_log("HOMESCREEN: input_add command[input_add device inputId appid fix] "
1433                   "has no argument");
1434     }
1435 }
1436
1437 static void
1438 input_del(struct display *display, char *buf)
1439 {
1440     char    *args[10];
1441     int     narg;
1442     int     input;
1443     char    wk1[32], wk2[32];
1444
1445     narg = pars_command(buf, args, 10);
1446     if (narg >= 3)  {
1447         input = strtol(args[1], (char **)0, 0);
1448         if (args[0][0] == '@')  {
1449             wk1[0] = 0;
1450             args[0] = wk1;
1451         }
1452         if (args[2][0] == '@')  {
1453             wk2[0] = 0;
1454             args[2] = wk2;
1455         }
1456         print_log("HOMESCREEN: input_del(%s.%d to %s)", args[0], input, args[2]);
1457         ico_input_mgr_control_del_input_app(display->ico_input_mgr,
1458                                             args[2], args[0], input);
1459     }
1460     else    {
1461         print_log("HOMESCREEN: input_del command[input_del device inputId appid] "
1462                   "has no argument");
1463     }
1464 }
1465
1466 static void
1467 input_send(struct display *display, char *buf)
1468 {
1469     char    *args[10];
1470     int     narg;
1471     int     surfaceid;
1472     int     type;
1473     int     no;
1474     int     code;
1475     int     value;
1476     char    appid[64];
1477
1478     narg = pars_command(buf, args, 10);
1479     if (narg >= 5)  {
1480         memset(appid, 0, sizeof(appid));
1481         if (args[0][0] == '@')  {
1482             strncpy(appid, &args[0][1], sizeof(appid)-1);
1483             surfaceid = 0;
1484         }
1485         else    {
1486             surfaceid = search_surface(display, args[0]);
1487         }
1488         if (strcasecmp(args[1], "POINTER") == 0)    {
1489             type = ICO_INPUT_MGR_DEVICE_TYPE_POINTER;
1490         }
1491         else if (strcasecmp(args[1], "KEYBOARD") == 0)  {
1492             type = ICO_INPUT_MGR_DEVICE_TYPE_KEYBOARD;
1493         }
1494         else if (strcasecmp(args[1], "TOUCH") == 0) {
1495             type = ICO_INPUT_MGR_DEVICE_TYPE_TOUCH;
1496         }
1497         else if (strcasecmp(args[1], "SWITCH") == 0)    {
1498             type = ICO_INPUT_MGR_DEVICE_TYPE_SWITCH;
1499         }
1500         else if (strcasecmp(args[1], "HAPTIC") == 0)    {
1501             type = ICO_INPUT_MGR_DEVICE_TYPE_HAPTIC;
1502         }
1503         else    {
1504             type = strtol(args[1], (char **)0, 0);
1505         }
1506         no = strtol(args[2], (char **)0, 0);
1507         if (strcasecmp(args[3], "ABS_X") == 0)  {
1508             code = ABS_X;
1509         }
1510         else if (strcasecmp(args[3], "ABS_Y") == 0) {
1511             code = ABS_Y;
1512         }
1513         else if (strcasecmp(args[3], "ABS_Z") == 0) {
1514             code = ABS_Z;
1515         }
1516         else if (strcasecmp(args[3], "REL_X") == 0) {
1517             code = REL_X | (EV_REL << 16);
1518         }
1519         else if (strcasecmp(args[3], "REL_Y") == 0) {
1520             code = REL_Y | (EV_REL << 16);
1521         }
1522         else if (strcasecmp(args[3], "REL_Z") == 0) {
1523             code = REL_Z | (EV_REL << 16);
1524         }
1525         else if (strcasecmp(args[3], "BTN_TOUCH") == 0) {
1526             code = BTN_TOUCH;
1527         }
1528         else if (strcasecmp(args[3], "BTN_LEFT") == 0)  {
1529             code = BTN_LEFT;
1530         }
1531         else if (strcasecmp(args[3], "BTN_RIGHT") == 0) {
1532             code = BTN_RIGHT;
1533         }
1534         else if (strcasecmp(args[3], "BTN_MIDDLE") == 0)    {
1535             code = BTN_MIDDLE;
1536         }
1537         else if (strcasecmp(args[3], "BTN_RIGHT") == 0) {
1538             code = BTN_RIGHT;
1539         }
1540         else    {
1541             code = strtol(args[3], (char **)0, 0);
1542         }
1543         value = strtol(args[4], (char **)0, 0);
1544         if (narg >= 6)  {
1545             value = (value << 16) + strtol(args[5], (char **)0, 0);
1546         }
1547         print_log("HOMESCREEN: input_send(%s.%x,%d,%d,%x,%d)",
1548                   appid, surfaceid, type, no, code, value);
1549         ico_input_mgr_control_send_input_event(display->ico_input_mgr,
1550                                                appid, surfaceid, type, no, 0, code, value);
1551     }
1552     else    {
1553         print_log("HOMESCREEN: input_send command[input_send {@app/serface} type no code "
1554                   "value [value2]] has no argument");
1555     }
1556 }
1557
1558 static void
1559 input_conf(struct display *display, char *buf)
1560 {
1561     char    *args[10];
1562     int     narg;
1563     int     type;
1564     int     input;
1565     int     code;
1566     char    wk1[32], wk2[32];
1567
1568     narg = pars_command(buf, args, 10);
1569     if (narg >= 4)  {
1570         type = strtol(args[1], (char **)0, 0);
1571         input = strtol(args[3], (char **)0, 0);
1572         if (narg >= 6)  {
1573             code = strtol(args[5], (char **)0, 0);
1574         }
1575         else    {
1576             code = 0;
1577             args[4] = wk1;
1578             strcpy(wk1, args[2]);
1579             args[5] = wk2;
1580             strcpy(wk2, "0");
1581         }
1582         if ((type >= 0) && (input >= 0) && (code >=0))  {
1583             ico_input_mgr_device_configure_input(display->ico_input_device, args[0], type,
1584                                                  args[2], input, args[4], code);
1585         }
1586         else    {
1587             print_log("HOMESCREEN: Unknown type(%s),input(%s) or code(%s) "
1588                       "at input_conf command", args[1], args[3], args[5]);
1589         }
1590     }
1591     else    {
1592         print_log("HOMESCREEN: input_conf command[input_conf device type swname input "
1593                   "codename code] has no argument");
1594     }
1595 }
1596
1597 static void
1598 input_code(struct display *display, char *buf)
1599 {
1600     char    *args[10];
1601     int     narg;
1602     int     input;
1603     int     code;
1604
1605     narg = pars_command(buf, args, 10);
1606     if (narg >= 4)  {
1607         input = strtol(args[1], (char **)0, 0);
1608         code = strtol(args[3], (char **)0, 0);
1609         if ((input >= 0) && (code >= 0))    {
1610             ico_input_mgr_device_configure_code(display->ico_input_device, args[0], input,
1611                                                 args[2], code);
1612         }
1613         else    {
1614             print_log("HOMESCREEN: Unknown input(%s) or code(%s) "
1615                       "at input_code command", args[1], args[3]);
1616         }
1617     }
1618     else    {
1619         print_log("HOMESCREEN: input_conf command[input_code device input codename code] "
1620                   "has no argument");
1621     }
1622 }
1623
1624 static void
1625 input_sw(struct display *display, char *buf)
1626 {
1627     char    *args[10];
1628     int     narg;
1629     int     timems;
1630     int     input;
1631     int     code;
1632     int     state;
1633     struct timeval  stv;
1634
1635     narg = pars_command(buf, args, 10);
1636     if (narg >= 4)  {
1637         input = strtol(args[1], (char **)0, 0);
1638         code = strtol(args[2], (char **)0, 0);
1639         state = strtol(args[3], (char **)0, 0);
1640         if ((input >= 0) && (state >= 0))   {
1641             gettimeofday(&stv, (struct timezone *)NULL);
1642             timems = (stv.tv_sec % 1000) * 1000 + (stv.tv_usec / 1000);
1643             ico_input_mgr_device_input_event(display->ico_input_device,
1644                                              timems, args[0], input, code, state);
1645         }
1646         else    {
1647             print_log("HOMESCREEN: Unknown input(%s),code(%s) or state(%s) "
1648                       "at input_sw command", args[1], args[2], args[3]);
1649         }
1650     }
1651     else    {
1652         print_log("HOMESCREEN: input_sw command[input_sw device input code, state] "
1653                   "has no argument");
1654     }
1655 }
1656
1657 static void
1658 send_event(const char *cmd)
1659 {
1660     static int  nmqinfo = 0;
1661     static struct   {
1662         int     mqkey;
1663         int     mqid;
1664     }           mqinfo[10];
1665     int     mqkey;
1666     int     mqid;
1667     struct {
1668         long    mtype;
1669         char    buf[240];
1670     }       mqbuf;
1671     int     pt, i;
1672
1673     if (cmd == NULL)    {
1674         return;
1675     }
1676     mqkey = 0;
1677     for (pt = 0; cmd[pt]; pt++) {
1678         if ((cmd[pt] >= '0') && (cmd[pt] <= '9'))   {
1679             mqkey = mqkey * 10 + cmd[pt] - '0';
1680         }
1681         else    {
1682             break;
1683         }
1684     }
1685     for (; cmd[pt] == ' '; pt++)    ;
1686
1687     if (mqkey <= 0) {
1688         mqkey = 5551;
1689         pt = 0;
1690     }
1691     for (i = 0; i < nmqinfo; i++)   {
1692         if (mqinfo[i].mqkey == mqkey)   {
1693             mqid = mqinfo[i].mqid;
1694             break;
1695         }
1696     }
1697     if (i >= nmqinfo)   {
1698         if (nmqinfo >= 10)  {
1699             fprintf(stderr, "HOMESCREEN: message queue(%d) overflow\n", mqkey);
1700             return;
1701         }
1702         mqid = msgget(mqkey, 0);
1703         if (mqid < 0)   {
1704             fprintf(stderr, "HOMESCREEN: message queue(%d(0x%x)) get error[%d]\n",
1705                     mqkey, mqkey, errno);
1706             return;
1707         }
1708         mqinfo[nmqinfo].mqkey = mqkey;
1709         mqinfo[nmqinfo].mqid = mqid;
1710         nmqinfo ++;
1711     }
1712
1713     memset(&mqbuf, 0, sizeof(mqbuf));
1714     mqbuf.mtype = 1;
1715     strncpy(mqbuf.buf, &cmd[pt], sizeof(mqbuf)-sizeof(long));
1716
1717     if (msgsnd(mqid, &mqbuf, sizeof(mqbuf)-sizeof(long), 0) < 0)    {
1718         fprintf(stderr, "HOMESCREEN: message queue(%d(0x%x)) send error[%d]\n",
1719                 mqkey, mqkey, errno);
1720         return;
1721     }
1722 }
1723
1724 static void
1725 set_region(struct display *display, char *buf)
1726 {
1727     char    *args[10];
1728     int     narg;
1729     int     x, y, width, height;
1730     int     hot_x, hot_y;
1731     int     c_x, c_y, c_width, c_height;
1732
1733     narg = pars_command(buf, args, 10);
1734     if (narg >= 5)  {
1735         x = strtol(args[1], (char **)0, 0);
1736         y = strtol(args[2], (char **)0, 0);
1737         width = strtol(args[3], (char **)0, 0);
1738         height = strtol(args[4], (char **)0, 0);
1739         hot_x = x + (width / 2);
1740         hot_y = y + (height / 2);
1741         c_x = x + 5;
1742         c_y = y + 5;
1743         c_width = width - 10;
1744         if (c_width <= 0)   c_width = 2;
1745         c_height = height - 10;
1746         if (c_height <= 0)  c_height = 2;
1747         print_log("HOMESCREEN: ico_exinput_set_input_region(%s,%d,%d-%d,%d,"
1748                   "hot=%d,%d,cur=%d,%d-%d,%d,attr=0)",
1749                   args[0] ? args[0] : "(null)", x, y, width, height,
1750                   hot_x, hot_y, c_x, c_y, c_width, c_height);
1751         if (strcasecmp(args[0], "NULL") == 0)   {
1752             ico_exinput_set_input_region(display->ico_exinput, "", x, y,
1753                                          width, height, hot_x, hot_y, c_x, c_y,
1754                                          c_width, c_height, 0);
1755         }
1756         else    {
1757             ico_exinput_set_input_region(display->ico_exinput, args[0], x, y,
1758                                          width, height, hot_x, hot_y, c_x, c_y,
1759                                          c_width, c_height, 0);
1760         }
1761     }
1762     else    {
1763         print_log("HOMESCREEN: set_region command[set_region winname@appid x y "
1764                   "width height] has no argument");
1765     }
1766 }
1767
1768 static void
1769 unset_region(struct display *display, char *buf)
1770 {
1771     char    *args[10];
1772     int     narg;
1773     int     x, y, width, height;
1774
1775     narg = pars_command(buf, args, 10);
1776     if (narg >= 1)  {
1777         if (narg >= 5) {
1778             x = strtol(args[1], (char **)0, 0);
1779             y = strtol(args[2], (char **)0, 0);
1780             width = strtol(args[3], (char **)0, 0);
1781             height = strtol(args[4], (char **)0, 0);
1782         }
1783         else    {
1784             x = 0;
1785             y = 0;
1786             width = 0;
1787             height = 0;
1788         }
1789         print_log("HOMESCREEN: ico_exinput_unset_input_region(%s,08x,%d,%d-%d,%d)",
1790                   args[0] ? args[0] : "(null)", x, y, width, height);
1791         if (strcasecmp(args[0], "NULL") == 0)   {
1792             ico_exinput_unset_input_region(display->ico_exinput, "", x, y,
1793                                            width, height);
1794         }
1795         else    {
1796             ico_exinput_unset_input_region(display->ico_exinput, args[0],
1797                                            x, y, width, height);
1798         }
1799     }
1800     else    {
1801         print_log("HOMESCREEN: unset_region command[unset_region winname@appid x y "
1802                   "width height] has no argument");
1803     }
1804 }
1805
1806 /*
1807  * Main Program
1808  *
1809  *   usage:
1810  *     test-homescreen < test-case-data-file > test-result-output
1811  */
1812 int main(int argc, char *argv[])
1813 {
1814     struct display *display;
1815     char buf[256];
1816     int ret, fd;
1817     int msec;
1818 #if 1           /* use mkostemp */
1819     extern int  mkostemp(char *template, int flags);
1820 #else           /* use mkostemp */
1821     long flags;
1822 #endif          /* use mkostemp */
1823
1824     display = malloc(sizeof *display);
1825     assert(display);
1826     memset((char *)display, 0, sizeof *display);
1827
1828     display->init_width = 640;
1829     display->init_height = 480;
1830     display->init_color = 0xFF304010;
1831
1832     for (fd = 1; fd < argc; fd++)   {
1833         if (argv[fd][0] == '-') {
1834             if (strncasecmp(argv[fd], "-visible=", 9) == 0) {
1835                 display->visible_on_create = argv[fd][9] & 1;
1836             }
1837             else if (strncasecmp(argv[fd], "-display=", 9) == 0)    {
1838                 strncpy(display->connect, &argv[fd][9], MAX_CON_NAME);
1839             }
1840             else if (strncasecmp(argv[fd], "-prompt=", 8) == 0) {
1841                 display->prompt = argv[fd][8] & 1;
1842             }
1843         }
1844     }
1845
1846     if (display->connect[0])    {
1847         display->display = wl_display_connect(display->connect);
1848     }
1849     else    {
1850         display->display = wl_display_connect(NULL);
1851     }
1852     assert(display->display);
1853
1854     display->registry = wl_display_get_registry(display->display);
1855     wl_registry_add_listener(display->registry, &registry_listener, display);
1856     wl_display_dispatch(display->display);
1857
1858     fd = 0;
1859
1860     while (1) {
1861         sleep_with_wayland(display->display, 20);
1862         if (display->prompt)    {
1863             printf("HOMESCREEN> "); fflush(stdout);
1864         }
1865         ret = getdata(display->ico_window_mgr, "HOMESCREEN> ", fd, buf, sizeof(buf));
1866         if (ret < 0) {
1867             fprintf(stderr, "HOMESCREEN: read error: fd %d, %m\n", fd);
1868             return -1;
1869         }
1870         if (ret == 0)   continue;
1871         wl_display_flush(display->display);
1872
1873         if ((strncasecmp(buf, "bye", 3) == 0) ||
1874             (strncasecmp(buf, "quit", 4) == 0) ||
1875             (strncasecmp(buf, "end", 3) == 0))  {
1876             /* Exit, end of test            */
1877             return 0;
1878         }
1879         else if (strncasecmp(buf, "launch", 6) == 0) {
1880             /* Launch test application      */
1881             launch_app(display, &buf[6]);
1882         }
1883         else if (strncasecmp(buf, "kill", 4) == 0) {
1884             /* Launch test application      */
1885             kill_app(display, &buf[4]);
1886         }
1887         else if (strncasecmp(buf, "layer_visible", 13) == 0) {
1888             /* Change layer visiblety       */
1889             visible_layer(display, &buf[13]);
1890         }
1891         else if (strncasecmp(buf, "layer", 5) == 0) {
1892             /* layer change surface window  */
1893             layer_surface(display, &buf[5]);
1894         }
1895         else if (strncasecmp(buf, "positionsize", 12) == 0) {
1896             /* Move and Ressize surface window*/
1897             positionsize_surface(display, &buf[12]);
1898         }
1899         else if (strncasecmp(buf, "move", 4) == 0) {
1900             /* Move surface window          */
1901             move_surface(display, &buf[4]);
1902         }
1903         else if (strncasecmp(buf, "resize", 6) == 0) {
1904             /* Resize surface window        */
1905             resize_surface(display, &buf[6]);
1906         }
1907         else if (strncasecmp(buf, "visible", 7) == 0) {
1908             /* Visible and Raise surface window*/
1909             visible_surface(display, &buf[7]);
1910         }
1911         else if (strncasecmp(buf, "show", 4) == 0) {
1912             /* Show/Hide surface window     */
1913             show_surface(display, &buf[4], 1);
1914         }
1915         else if (strncasecmp(buf, "hide", 4) == 0) {
1916             /* Show/Hide surface window     */
1917             show_surface(display, &buf[4], 0);
1918         }
1919         else if (strncasecmp(buf, "raise", 5) == 0) {
1920             /* Raise/Lower surface window   */
1921             raise_surface(display, &buf[5], 1);
1922         }
1923         else if (strncasecmp(buf, "lower", 5) == 0) {
1924             /* Raise/Lower surface window   */
1925             raise_surface(display, &buf[5], 0);
1926         }
1927         else if (strncasecmp(buf, "active", 6) == 0) {
1928             /* Active surface window        */
1929             active_window(display, &buf[6]);
1930         }
1931         else if (strncasecmp(buf, "animation", 9) == 0) {
1932             /* Set animation surface window */
1933             animation_surface(display, &buf[9]);
1934         }
1935         else if (strncasecmp(buf, "map", 3) == 0) {
1936             /* map surface                  */
1937             map_surface(display, &buf[3], 1);
1938         }
1939         else if (strncasecmp(buf, "unmap", 5) == 0) {
1940             /* unmap surface                */
1941             map_surface(display, &buf[5], 0);
1942         }
1943         else if (strncasecmp(buf, "input_add", 9) == 0) {
1944             /* Set input switch to application */
1945             input_add(display, &buf[9]);
1946         }
1947         else if (strncasecmp(buf, "input_del", 9) == 0) {
1948             /* Reset input switch to application*/
1949             input_del(display, &buf[9]);
1950         }
1951         else if (strncasecmp(buf, "input_send", 10) == 0) {
1952             /* Input event send to application*/
1953             input_send(display, &buf[10]);
1954         }
1955         else if (strncasecmp(buf, "input_conf", 10) == 0) {
1956             /* input switch configuration       */
1957             input_conf(display, &buf[10]);
1958         }
1959         else if (strncasecmp(buf, "input_code", 10) == 0) {
1960             /* input code configuration         */
1961             input_code(display, &buf[10]);
1962         }
1963         else if (strncasecmp(buf, "input_sw", 8) == 0) {
1964             /* input switch event               */
1965             input_sw(display, &buf[8]);
1966         }
1967         else if (strncasecmp(buf, "set_region", 10) == 0) {
1968             /* set input region                 */
1969             set_region(display, &buf[10]);
1970         }
1971         else if (strncasecmp(buf, "unset_region", 12) == 0) {
1972             /* unset input region               */
1973             unset_region(display, &buf[12]);
1974         }
1975         else if (strncasecmp(buf, "input_sw", 8) == 0) {
1976             /* input switch event               */
1977             input_sw(display, &buf[8]);
1978         }
1979         else if (strncasecmp(buf, "sleep", 5) == 0) {
1980             /* Sleep                            */
1981             msec = sec_str_2_value(&buf[6]);
1982             sleep_with_wayland(display->display, msec);
1983         }
1984         else if (strncasecmp(buf, "waitcreate", 10) == 0) {
1985             /* Wait surface create              */
1986             msec = sec_str_2_value(&buf[11]);
1987             wait_with_wayland(display->display, msec, &display->surface_created);
1988         }
1989         else if (strncasecmp(buf, "waitdestroy", 11) == 0) {
1990             /* Wait surface destrpy             */
1991             msec = sec_str_2_value(&buf[12]);
1992             wait_with_wayland(display->display, msec, &display->surface_destroyed);
1993         }
1994         else if (strncasecmp(buf, "event", 5) == 0) {
1995             /* Send touch panel event to Weston */
1996             send_event(&buf[6]);
1997         }
1998         else {
1999             print_log("HOMESCREEN: unknown command[%s]", buf);
2000             return -1;
2001         }
2002     }
2003
2004     print_log("HOMESCREEN: end");
2005
2006     send_event(NULL);
2007
2008     return 0;
2009 }
2010