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