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