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