0.9.21 release -- It changes so that the layer controlling function of GENIVI may...
[profile/ivi/ico-uxf-weston-plugin.git] / src / ico_window_mgr.c
1 /*
2  * Copyright © 2010-2011 Intel Corporation
3  * Copyright © 2008-2011 Kristian Høgsberg
4  * Copyright © 2013-2014 TOYOTA MOTOR CORPORATION.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the copyright holders not be used in
11  * advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission.  The copyright holders make
13  * no representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24 /**
25  * @brief   Multi Window Manager (Weston(Wayland) PlugIn)
26  *
27  * @date    Feb-21-2014
28  */
29
30 #define _GNU_SOURCE
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <linux/input.h>
38 #include <assert.h>
39 #include <signal.h>
40 #include <math.h>
41 #include <time.h>
42 #include <sys/mman.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <limits.h>
47 #include <pixman.h>
48 #include <wayland-server.h>
49 #include <dirent.h>
50 #include <aul/aul.h>
51 #include <bundle.h>
52
53 #include <EGL/egl.h>
54 #include <GLES2/gl2.h>
55
56 #include <weston/compositor.h>
57
58 /* detail debug log */
59 #define UIFW_DETAIL_OUT 0                   /* 1=detail debug log/0=no detail log   */
60
61 #include <weston/weston-layout.h>
62 #include "ico_ivi_common_private.h"
63 #include "ico_window_mgr_private.h"
64 #include "ico_window_mgr-server-protocol.h"
65
66 /* gl_surface_state (inport from weston-1.4.0/src/gl-renderer.c */
67 enum buffer_type {
68     BUFFER_TYPE_NULL,
69     BUFFER_TYPE_SHM,
70     BUFFER_TYPE_EGL
71 };
72 struct uifw_gl_surface_state {      /* struct gl_surface_state  */
73     GLfloat color[4];
74     struct gl_shader *shader;
75
76     GLuint textures[3];
77     int num_textures;
78     int needs_full_upload;
79     pixman_region32_t texture_damage;
80
81     void *images[3];            /* EGLImageKHR */
82     GLenum target;
83     int num_images;
84
85     struct weston_buffer_reference buffer_ref;
86     enum buffer_type buffer_type;
87     int pitch; /* in pixels */
88     int height; /* in pixels */
89     int y_inverted;         /* add weston 1.3.x */
90 };
91
92 /* SurfaceID                            */
93 #define UIFW_HASH           64              /* Hash value (2's compliment)          */
94 #define SURCAFE_ID_MASK     0x0ffff         /* SurfaceId hash mask pattern          */
95
96 /* Internal fixed value                 */
97 #define ICO_WINDOW_MGR_APPID_FIXCOUNT   5   /* retry count of appid fix             */
98                                             /* show/hide animation with position    */
99 #define ICO_WINDOW_MGR_ANIMATION_POS    0x10000000
100
101 /* Multi Windiw Manager                 */
102 struct ico_win_mgr {
103     struct weston_compositor *compositor;   /* Weston compositor                    */
104     void    *shell;                         /* shell table address                  */
105     int32_t surface_head;                   /* (HostID << 24) | (DisplayNo << 16)   */
106
107     struct wl_list  client_list;            /* Clients                              */
108     struct wl_list  manager_list;           /* Manager(ex.HomeScreen) list          */
109
110     struct wl_list  map_list;               /* surface map list                     */
111     struct uifw_surface_map *free_maptable; /* free maped surface table list        */
112     struct weston_animation map_animation[ICO_IVI_MAX_DISPLAY];
113                                             /* animation for map check              */
114     struct wl_event_source  *wait_mapevent; /* map event send wait timer            */
115
116     struct uifw_win_surface *idhash[UIFW_HASH];  /* UIFW SerfaceID                  */
117     struct uifw_win_surface *wshash[UIFW_HASH];  /* Weston Surface                  */
118
119     char    shell_init;                     /* shell initialize flag                */
120     char    res[3];                         /* (unused)                             */
121 };
122
123 /* Internal macros                      */
124 /* UIFW SurfaceID                       */
125 #define MAKE_IDHASH(v)  (((uint32_t)v) & (UIFW_HASH-1))
126 /* Weston Surface                       */
127 #define MAKE_WSHASH(v)  ((((uint32_t)v) >> 5) & (UIFW_HASH-1))
128
129 /* function prototype                   */
130                                             /* get surface table from weston surface*/
131 static struct uifw_win_surface *find_uifw_win_surface_by_ws(
132                     struct weston_surface *wsurf);
133                                             /* bind shell client                    */
134 static void win_mgr_bind_client(struct wl_client *client, void *shell);
135                                             /* destroy client                       */
136 static void win_mgr_destroy_client(struct wl_listener *listener, void *data);
137 #if 0       /* work around: Walk through child processes until app ID is found  */
138                                             /* get parent pid                       */
139 static pid_t win_mgr_get_ppid(pid_t pid);
140 #endif      /* work around: Walk through child processes until app ID is found  */
141                                             /* get appid from pid                   */
142 static void win_mgr_get_client_appid(struct uifw_client *uclient);
143                                             /* create new surface                   */
144 static void win_mgr_register_surface(uint32_t id_surface, struct weston_surface *surface,
145                                      struct wl_client *client,
146                                      struct weston_layout_surface *ivisurf);
147                                             /* surface destroy                      */
148 static void win_mgr_destroy_surface(struct weston_surface *surface);
149                                             /* window manager surface configure     */
150 static void win_mgr_surface_configure(struct uifw_win_surface *usurf,
151                                       int x, int y, int width, int height);
152                                             /* set surface animation                */
153 static void uifw_set_animation(struct wl_client *client, struct wl_resource *resource,
154                                uint32_t surfaceid, int32_t type,
155                                const char *animation, int32_t time);
156                                             /* check and change all mapped surface  */
157 static void win_mgr_check_mapsurface(struct weston_animation *animation,
158                                      struct weston_output *output, uint32_t msecs);
159                                             /* check timer of mapped surface        */
160 static int win_mgr_timer_mapsurface(void *data);
161                                             /* check and change mapped surface      */
162 static void win_mgr_change_mapsurface(struct uifw_surface_map *sm, int event,
163                                       uint32_t curtime);
164                                             /* map surface to system application    */
165 static void uifw_map_surface(struct wl_client *client, struct wl_resource *resource,
166                              uint32_t surfaceid, int32_t framerate, const char *filepath);
167                                             /* unmap surface                        */
168 static void uifw_unmap_surface(struct wl_client *client, struct wl_resource *resource,
169                                uint32_t surfaceid);
170                                             /* bind manager                         */
171 static void bind_ico_win_mgr(struct wl_client *client,
172                              void *data, uint32_t version, uint32_t id);
173                                             /* unbind manager                       */
174 static void unbind_ico_win_mgr(struct wl_resource *resource);
175                                             /* convert animation name to Id value   */
176 static int ico_get_animation_name(const char *animation);
177                                             /* touch/click select surface           */
178 static void win_mgr_select_surface(struct weston_seat *seat,
179                                    struct weston_surface *focus, int target);
180 static void win_mgr_click_to_activate(struct weston_seat *seat, uint32_t time,
181                                       uint32_t button, void *data);
182 static void win_mgr_touch_to_activate(struct weston_seat *seat, uint32_t time,
183                                       void *data);
184                                             /* hook for create surface of ivi-shell */
185 static void ico_ivi_surfaceCreateNotification(struct weston_layout_surface *ivisurf,
186                                               void *userdata);
187                                             /* hook for remove surface of ivi-shell */
188 static void ico_ivi_surfaceRemoveNotification(struct weston_layout_surface *ivisurf,
189                                               void *userdata);
190                                             /* hook for configure surface of ivi-shell*/
191 static void ico_ivi_surfaceConfigureNotification(struct weston_layout_surface *ivisurf,
192                                                  void *userdata);
193                                             /* hook for property change of ivi-shell*/
194 static void ico_ivi_surfacePropertyNotification(struct weston_layout_surface *ivisurf,
195                                                 struct weston_layout_SurfaceProperties *prop,
196                                                 enum weston_layout_notification_mask mask,
197                                                 void *userdata);
198                                             /* hook for animation                   */
199 static int  (*win_mgr_hook_animation)(const int op, void *data) = NULL;
200                                             /* hook for input region                */
201 static void (*win_mgr_hook_change)(struct uifw_win_surface *usurf) = NULL;
202 static void (*win_mgr_hook_destory)(struct uifw_win_surface *usurf) = NULL;
203 static void (*win_mgr_hook_inputregion)(int set, struct uifw_win_surface *usurf,
204                                         int32_t x, int32_t y, int32_t width,
205                                         int32_t height, int32_t hotspot_x, int32_t hotspot_y,
206                                         int32_t cursor_x, int32_t cursor_y,
207                                         int32_t cursor_width, int32_t cursor_height,
208                                         uint32_t attr) = NULL;
209
210 /* static tables                        */
211 /* Multi Window Manager interface       */
212 static const struct ico_window_mgr_interface ico_window_mgr_implementation = {
213     uifw_set_animation,
214     uifw_map_surface,
215     uifw_unmap_surface
216 };
217
218
219 /* plugin common value(without ico_plugin_loader)   */
220 static int  _ico_ivi_option_flag = 0;           /* option flags                     */
221 static int  _ico_ivi_debug_level = 3;           /* debug Level                      */
222 static char *_ico_ivi_animation_name = NULL;    /* default animation name           */
223 static int  _ico_ivi_animation_time = 500;      /* default animation time           */
224 static int  _ico_ivi_animation_fps = 30;        /* animation frame rate             */
225
226 /* static management table              */
227 static struct ico_win_mgr       *_ico_win_mgr = NULL;
228 static int                      _ico_num_nodes = 0;
229 static struct uifw_node_table   _ico_node_table[ICO_IVI_MAX_DISPLAY];
230 static struct weston_seat       *touch_check_seat = NULL;
231
232
233 /*--------------------------------------------------------------------------*/
234 /**
235  * @brief   ico_ivi_optionflag: get option flags
236  *
237  * @param       None
238  * @return      option flags
239  */
240 /*--------------------------------------------------------------------------*/
241 WL_EXPORT   int
242 ico_ivi_optionflag(void)
243 {
244     return _ico_ivi_option_flag;
245 }
246
247 /*--------------------------------------------------------------------------*/
248 /**
249  * @brief   ico_ivi_debuglevel: answer debug output level.
250  *
251  * @param       none
252  * @return      debug output level
253  * @retval      0       No debug output
254  * @retval      1       Only error output
255  * @retval      2       Error and Warning output
256  * @retval      3       Error, Warning and information output
257  * @retval      4       Error, Warning, information and Debug Trace output
258  * @retval      5       All output with debug write
259  */
260 /*--------------------------------------------------------------------------*/
261 WL_EXPORT   int
262 ico_ivi_debuglevel(void)
263 {
264     return (_ico_ivi_debug_level & 0x0ffff);
265 }
266
267 /*--------------------------------------------------------------------------*/
268 /**
269  * @brief   ico_ivi_debugflag: get debug flags
270  *
271  * @param       None
272  * @return      debug flags
273  */
274 /*--------------------------------------------------------------------------*/
275 WL_EXPORT   int
276 ico_ivi_debugflag(void)
277 {
278     return ((_ico_ivi_debug_level >> 16) & 0x0ffff);
279 }
280
281 /*--------------------------------------------------------------------------*/
282 /**
283  * @brief   ico_ivi_default_animation_name: get default animation name
284  *
285  * @param       None
286  * @return      Default animation name
287  */
288 /*--------------------------------------------------------------------------*/
289 WL_EXPORT   const char *
290 ico_ivi_default_animation_name(void)
291 {
292     return _ico_ivi_animation_name;
293 }
294
295 /*--------------------------------------------------------------------------*/
296 /**
297  * @brief   ico_ivi_default_animation_time: get default animation time
298  *
299  * @param       None
300  * @return      Default animation time(miri sec)
301  */
302 /*--------------------------------------------------------------------------*/
303 WL_EXPORT   int
304 ico_ivi_default_animation_time(void)
305 {
306     return _ico_ivi_animation_time;
307 }
308
309 /*--------------------------------------------------------------------------*/
310 /**
311  * @brief   ico_ivi_default_animation_fps: get default animation frame rate
312  *
313  * @param       None
314  * @return      Default animation frame rate(frames/sec)
315  */
316 /*--------------------------------------------------------------------------*/
317 WL_EXPORT   int
318 ico_ivi_default_animation_fps(void)
319 {
320     return _ico_ivi_animation_fps;
321 }
322
323 /*--------------------------------------------------------------------------*/
324 /**
325  * @brief   ico_ivi_get_mynode: Get my NodeId
326  *
327  * @param       None
328  * @return      NodeId of my node
329  */
330 /*--------------------------------------------------------------------------*/
331 WL_EXPORT   int
332 ico_ivi_get_mynode(void)
333 {
334     /* Reference Platform 0.90 only support 1 ECU   */
335     return 0;
336 }
337
338 /*--------------------------------------------------------------------------*/
339 /**
340  * @brief   ico_ivi_surface_buffer_width: get surface buffer width
341  *
342  * @param[in]   es          weston surface
343  * @return      buffer width(if surface has no buffer, return 0)
344  */
345 /*--------------------------------------------------------------------------*/
346 WL_EXPORT   int
347 ico_ivi_surface_buffer_width(struct weston_surface *es)
348 {
349     int v;
350
351     if (! es->buffer_ref.buffer)    {
352         return 0;
353     }
354     if (es->buffer_viewport.viewport_set)   {
355         return es->buffer_viewport.dst_width;
356     }
357     switch (es->buffer_viewport.transform) {
358     case WL_OUTPUT_TRANSFORM_90:
359     case WL_OUTPUT_TRANSFORM_270:
360     case WL_OUTPUT_TRANSFORM_FLIPPED_90:
361     case WL_OUTPUT_TRANSFORM_FLIPPED_270:
362         v = es->buffer_ref.buffer->height;
363         break;
364     default:
365         v = es->buffer_ref.buffer->width;
366         break;
367     }
368     return (v / es->buffer_viewport.scale);
369 }
370
371 /*--------------------------------------------------------------------------*/
372 /**
373  * @brief   ico_ivi_surface_buffer_height: get surface buffer height
374  *
375  * @param[in]   es          weston surface
376  * @return      buffer height(if surface has no buffer, return 0)
377  */
378 /*--------------------------------------------------------------------------*/
379 WL_EXPORT   int
380 ico_ivi_surface_buffer_height(struct weston_surface *es)
381 {
382     int v;
383
384     if (! es->buffer_ref.buffer)    {
385         return 0;
386     }
387     if (es->buffer_viewport.viewport_set)   {
388         return es->buffer_viewport.dst_height;
389     }
390     switch (es->buffer_viewport.transform) {
391     case WL_OUTPUT_TRANSFORM_90:
392     case WL_OUTPUT_TRANSFORM_270:
393     case WL_OUTPUT_TRANSFORM_FLIPPED_90:
394     case WL_OUTPUT_TRANSFORM_FLIPPED_270:
395         v = es->buffer_ref.buffer->width;
396         break;
397     default:
398         v = es->buffer_ref.buffer->height;
399         break;
400     }
401     return (v / es->buffer_viewport.scale);
402 }
403
404 /*--------------------------------------------------------------------------*/
405 /**
406  * @brief   ico_ivi_surface_buffer_size: get surface buffer size
407  *
408  * @param[in]   es          weston surface
409  * @param[out]  width       buffer width
410  * @param[out]  height      buffer height
411  * @return      nothing
412  */
413 /*--------------------------------------------------------------------------*/
414 WL_EXPORT   void
415 ico_ivi_surface_buffer_size(struct weston_surface *es, int *width, int *height)
416 {
417     if (! es->buffer_ref.buffer)    {
418         *width = 0;
419         *height = 0;
420     }
421     else if (es->buffer_viewport.viewport_set)  {
422         *width = es->buffer_viewport.dst_width;
423         *height = es->buffer_viewport.dst_height;
424     }
425     else    {
426         switch (es->buffer_viewport.transform) {
427         case WL_OUTPUT_TRANSFORM_90:
428         case WL_OUTPUT_TRANSFORM_270:
429         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
430         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
431             *width = es->buffer_ref.buffer->height;
432             *height = es->buffer_ref.buffer->width;
433             break;
434         default:
435             *width = es->buffer_ref.buffer->width;
436             *height = es->buffer_ref.buffer->height;
437             break;
438         }
439         *width = *width / es->buffer_viewport.scale;
440         *height = *height / es->buffer_viewport.scale;
441     }
442 }
443
444 /*--------------------------------------------------------------------------*/
445 /**
446  * @brief   ico_ivi_surface_buffer_size: get surface buffer size
447  *
448  * @param[in]   es          weston surface
449  * @param[out]  width       buffer width
450  * @param[out]  height      buffer height
451  * @return      nothing
452  */
453 /*--------------------------------------------------------------------------*/
454 WL_EXPORT   struct weston_view *
455 ico_ivi_get_primary_view(struct uifw_win_surface *usurf)
456 {
457     struct weston_view  *ev = NULL;
458     struct shell_surface *shsurf;
459
460     if (_ico_win_mgr->compositor->shell_interface.get_primary_view) {
461         shsurf = usurf->surface->configure_private;
462         if (shsurf) {
463             ev = (*_ico_win_mgr->compositor->shell_interface.get_primary_view)(NULL, shsurf);
464         }
465     }
466     if (! ev)   {
467         ev = weston_layout_get_weston_view(usurf->ivisurf);
468     }
469     if (! ev)   {
470         uifw_error("ico_ivi_get_primary_view: usurf=%08x(%x) surface=%08x has no view",
471                    (int)usurf, usurf->surfaceid, (int)usurf->surface);
472     }
473     else    {
474         uifw_debug("ico_ivi_get_primary_view: %08x view=%08x view->surf=%08x",
475                    usurf->surfaceid, (int)ev, (int)ev->surface);
476     }
477     return ev;
478 }
479
480 /*--------------------------------------------------------------------------*/
481 /**
482  * @brief   ico_window_mgr_set_weston_surface: set weston surface
483  *
484  * @param[in]   usurf       UIFW surface
485  * @param[in]   x           X coordinate on screen
486  * @param[in]   y           Y coordinate on screen
487  * @param[in]   width       width
488  * @param[in]   height      height
489  * @return      none
490  */
491 /*--------------------------------------------------------------------------*/
492 WL_EXPORT   void
493 ico_window_mgr_set_weston_surface(struct uifw_win_surface *usurf,
494                                   int x, int y, int width, int height)
495 {
496     struct weston_surface *es = usurf->surface;
497     struct weston_view  *ev;
498     int     buf_width, buf_height;
499
500     if ((es == NULL) || (usurf->ivisurf == NULL))    {
501         uifw_trace("ico_window_mgr_set_weston_surface: usurf(%08x) has no surface",
502                    (int)usurf);
503         return;
504     }
505
506     if (es->buffer_ref.buffer != NULL)   {
507         ico_ivi_surface_buffer_size(es, &buf_width, &buf_height);
508         if ((width <= 0) || (height <= 0))    {
509             width = buf_width;
510             usurf->width = buf_width;
511             height = buf_height;
512             usurf->height = buf_height;
513         }
514         if ((usurf->width > buf_width) && (usurf->scalex <= 1.0f))  {
515             width = buf_width;
516             x += (usurf->width - buf_width)/2;
517         }
518         if ((usurf->height > buf_height) && (usurf->scaley <= 1.0f))    {
519             height = buf_height;
520             y += (usurf->height - buf_height)/2;
521         }
522         if (usurf->visible) {
523             x += usurf->node_tbl->disp_x;
524             y += usurf->node_tbl->disp_y;
525         }
526         else    {
527             x = ICO_IVI_MAX_COORDINATE+1;
528             y = ICO_IVI_MAX_COORDINATE+1;
529         }
530         ev = ico_ivi_get_primary_view(usurf);
531         if ((ev != NULL) &&
532             (((int)ev->geometry.x != x) || ((int)ev->geometry.y != y) ||
533              (es->width != width) || (es->height != height)))   {
534             weston_view_damage_below(ev);
535             win_mgr_surface_configure(usurf, x, y, width, height);
536         }
537         weston_surface_damage(es);
538     }
539 }
540
541 /*--------------------------------------------------------------------------*/
542 /**
543  * @brief   ico_window_mgr_get_usurf: find UIFW surface by surface id
544  *
545  * @param[in]   surfaceid   UIFW surface id
546  * @return      UIFW surface table address
547  * @retval      !=NULL      success(surface table address)
548  * @retval      NULL        error(surface id dose not exist)
549  */
550 /*--------------------------------------------------------------------------*/
551 WL_EXPORT   struct uifw_win_surface *
552 ico_window_mgr_get_usurf(const uint32_t surfaceid)
553 {
554     struct uifw_win_surface *usurf;
555
556     usurf = _ico_win_mgr->idhash[MAKE_IDHASH(surfaceid)];
557
558     while (usurf)   {
559         if (usurf->surfaceid == surfaceid) {
560             return usurf;
561         }
562         usurf = usurf->next_idhash;
563     }
564     uifw_trace("ico_window_mgr_get_usurf: NULL");
565     return NULL;
566 }
567
568 /*--------------------------------------------------------------------------*/
569 /**
570  * @brief   ico_window_mgr_get_usurf_client: find UIFW surface by surface id/or client
571  *
572  * @param[in]   surfaceid   UIFW surface id
573  * @param[in]   client      Wayland client
574  * @return      UIFW surface table address
575  * @retval      !=NULL      success(surface table address)
576  * @retval      NULL        error(surface id or client dose not exist)
577  */
578 /*--------------------------------------------------------------------------*/
579 WL_EXPORT   struct uifw_win_surface *
580 ico_window_mgr_get_usurf_client(const uint32_t surfaceid, struct wl_client *client)
581 {
582     struct uifw_win_surface *usurf;
583
584     usurf = ico_window_mgr_get_usurf(surfaceid);
585     return usurf;
586 }
587
588 /*--------------------------------------------------------------------------*/
589 /**
590  * @brief   find_uifw_win_surface_by_ws: find UIFW surface by weston surface
591  *
592  * @param[in]   wsurf       Weston surface
593  * @return      UIFW surface table address
594  * @retval      !=NULL      success(surface table address)
595  * @retval      NULL        error(surface dose not exist)
596  */
597 /*--------------------------------------------------------------------------*/
598 static struct uifw_win_surface *
599 find_uifw_win_surface_by_ws(struct weston_surface *wsurf)
600 {
601     struct uifw_win_surface *usurf;
602
603     usurf = _ico_win_mgr->wshash[MAKE_WSHASH(wsurf)];
604
605     while (usurf)   {
606         if (usurf->surface == wsurf) {
607             return usurf;
608         }
609         usurf = usurf->next_wshash;
610     }
611     uifw_trace("find_uifw_win_surface_by_ws: NULL");
612     return NULL;
613 }
614
615 /*--------------------------------------------------------------------------*/
616 /**
617  * @brief   ico_window_mgr_find_uclient: find UIFW client by wayland client
618  *
619  * @param[in]   client      Wayland client
620  * @return      UIFW client table address
621  * @retval      !=NULL      success(client table address)
622  * @retval      NULL        error(client dose not exist)
623  */
624 /*--------------------------------------------------------------------------*/
625 WL_EXPORT   struct uifw_client*
626 ico_window_mgr_find_uclient(struct wl_client *client)
627 {
628     struct uifw_client  *uclient;
629
630     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
631         if (uclient->client == client)  {
632             return(uclient);
633         }
634     }
635     uifw_trace("ico_window_mgr_find_uclient: client.%08x is NULL", (int)client);
636     return NULL;
637 }
638
639 /*--------------------------------------------------------------------------*/
640 /**
641  * @brief   ico_window_mgr_get_appid: find application id by wayland client
642  *
643  * @param[in]   client      Wayland client
644  * @return      application id
645  * @retval      !=NULL      success(application id)
646  * @retval      NULL        error(client dose not exist)
647  */
648 /*--------------------------------------------------------------------------*/
649 WL_EXPORT   char *
650 ico_window_mgr_get_appid(struct wl_client* client)
651 {
652     struct uifw_client  *uclient;
653
654     uclient = ico_window_mgr_find_uclient(client);
655
656     if (! uclient)  {
657         return NULL;
658     }
659     return uclient->appid;
660 }
661
662 /*--------------------------------------------------------------------------*/
663 /**
664  * @brief   ico_window_mgr_get_display_coordinate: get display coordinate
665  *
666  * @param[in]   displayno   display number
667  * @param[out]  x           relative X coordinate
668  * @param[out]  y           relative Y coordinate
669  * @return      none
670  */
671 /*--------------------------------------------------------------------------*/
672 WL_EXPORT   void
673 ico_window_mgr_get_display_coordinate(int displayno, int *x, int *y)
674 {
675     if ((displayno <= _ico_num_nodes) || (displayno < 0))   {
676         displayno = 0;
677     }
678     *x = _ico_node_table[displayno].disp_x;
679     *y = _ico_node_table[displayno].disp_y;
680 }
681
682 /*--------------------------------------------------------------------------*/
683 /**
684  * @brief   win_mgr_bind_client: desktop_shell from client
685  *
686  * @param[in]   client          Wayland client
687  * @param[in]   shell           shell table address
688  * @return      none
689  */
690 /*--------------------------------------------------------------------------*/
691 static void
692 win_mgr_bind_client(struct wl_client *client, void *shell)
693 {
694     struct uifw_client  *uclient;
695     int     newclient;
696     pid_t   pid;
697     uid_t   uid;
698     gid_t   gid;
699
700     uifw_trace("win_mgr_bind_client: Enter(client=%08x, shell=%08x)",
701                (int)client, (int)shell);
702
703     /* save shell table address             */
704     if (shell)  {
705         _ico_win_mgr->shell = shell;
706     }
707
708     /* set client                           */
709     uclient = ico_window_mgr_find_uclient(client);
710     if (! uclient)  {
711         /* client not exist, create client management table             */
712         uifw_trace("win_mgr_bind_client: Create Client");
713         uclient = (struct uifw_client *)malloc(sizeof(struct uifw_client));
714         if (!uclient)   {
715             uifw_error("win_mgr_bind_client: Error, No Memory");
716             return;
717         }
718         memset(uclient, 0, sizeof(struct uifw_client));
719         uclient->client = client;
720         wl_list_init(&uclient->surface_link);
721         newclient = 1;
722         uclient->destroy_listener.notify = win_mgr_destroy_client;
723         wl_client_add_destroy_listener(client, &uclient->destroy_listener);
724     }
725     else    {
726         newclient = 0;
727     }
728     wl_client_get_credentials(client, &pid, &uid, &gid);
729     uifw_trace("win_mgr_bind_client: client=%08x pid=%d uid=%d gid=%d",
730                (int)client, (int)pid, (int)uid, (int)gid);
731     if (pid > 0)    {
732         uclient->pid = (int)pid;
733         /* get applicationId from AppCore(AUL)  */
734         win_mgr_get_client_appid(uclient);
735
736         if (newclient > 0)  {
737             wl_list_insert(&_ico_win_mgr->client_list, &uclient->link);
738         }
739     }
740     else    {
741         uifw_trace("win_mgr_bind_client: client=%08x pid dose not exist", (int)client);
742     }
743     uifw_trace("win_mgr_bind_client: Leave");
744 }
745
746 /*--------------------------------------------------------------------------*/
747 /**
748  * @brief   win_mgr_destroy_client: destroy client
749  *
750  * @param[in]   listener    listener
751  * @param[in]   data        listener
752  * @return      none
753  */
754 /*--------------------------------------------------------------------------*/
755 static void
756 win_mgr_destroy_client(struct wl_listener *listener, void *data)
757 {
758     struct uifw_client  *uclient;
759
760     uclient = container_of(listener, struct uifw_client, destroy_listener);
761
762     uifw_trace("win_mgr_destroy_client: Enter(uclient=%08x)", (int)uclient);
763
764     if (uclient)    {
765         /* Client exist, Destory client management table             */
766         wl_list_remove(&uclient->link);
767         free(uclient);
768     }
769     uifw_trace("win_mgr_destroy_client: Leave");
770 }
771
772 #if 0       /* work around: Walk through child processes until app ID is found  */
773 /*--------------------------------------------------------------------------*/
774 /**
775  * @brief   win_mgr_get_ppid: Get parent process ID.
776  *
777  * Similar to getppid(), except that this implementation accepts an
778  * arbitrary process ID.
779  *
780  * @param[in]   pid     Process ID of child process
781  * @return      parent process ID on success, -1 on failure
782  */
783 /*--------------------------------------------------------------------------*/
784 static pid_t
785 win_mgr_get_ppid(pid_t pid)
786 {
787     pid_t ppid = -1;
788     char procpath[PATH_MAX] = { 0 };
789
790     snprintf(procpath, sizeof(procpath)-1, "/proc/%d/status", pid);
791
792     /* We better have read permissions! */
793     int const fd = open(procpath, O_RDONLY);
794
795     if (fd < 0)
796         return ppid;
797
798     char buffer[1024] = { 0 };
799
800     ssize_t const size = read(fd, buffer, sizeof(buffer));
801     close(fd);
802
803     if (size <= 0)
804         return ppid;
805
806     /* Find line containing the parent process ID. */
807     char const * const ppid_line = strstr(buffer, "PPid");
808
809     if (ppid_line != NULL)
810         sscanf(ppid_line, "PPid:    %d", &ppid);
811
812     return ppid;
813 }
814 #endif      /* work around: Walk through child processes until app ID is found  */
815
816 /*--------------------------------------------------------------------------*/
817 /**
818  * @brief   win_mgr_get_client_appid: get applicationId from pid
819  *
820  * @param[in]   uclient     UIFW client management table
821  * @return      none
822  */
823 /*--------------------------------------------------------------------------*/
824 static void
825 win_mgr_get_client_appid(struct uifw_client *uclient)
826 {
827     int status = AUL_R_ERROR;
828
829     memset(uclient->appid, 0, ICO_IVI_APPID_LENGTH);
830
831 #if 0       /* work around: Walk through child processes until app ID is found  */
832     /*
833      * Walk the parent process chain until we find a parent process
834      * with an app ID.
835      */
836     int pid;
837
838     for (pid = uclient->pid;
839          pid > 1 && status != AUL_R_OK;
840          pid = win_mgr_get_ppid(pid)) {
841
842         status = aul_app_get_appid_bypid(pid,
843                                          uclient->appid,
844                                          ICO_IVI_APPID_LENGTH);
845
846         uifw_trace("win_mgr_get_client_appid: aul_app_get_appid_bypid ret=%d "
847                    "pid=%d appid=<%s>", status, pid, uclient->appid);
848     }
849     /*
850      * Walk the child process chain as well since app ID was not yet found
851      */
852     if (status != AUL_R_OK) {
853
854         DIR *dr;
855         struct dirent *de;
856         struct stat ps;
857         pid_t   tpid;
858         uid_t   uid;
859         gid_t   gid;
860
861         dr = opendir("/proc/");
862
863         /* get uid */
864         wl_client_get_credentials(uclient->client, &tpid, &uid, &gid);
865
866         while(((de = readdir(dr)) != NULL) && (status != AUL_R_OK)) {
867
868             char fullpath[PATH_MAX] = { 0 };
869             int is_child = 0;
870             int tmppid;
871
872             snprintf(fullpath, sizeof(fullpath)-1, "/proc/%s", de->d_name);
873
874             if (stat(fullpath, &ps) == -1) {
875                 continue;
876             }
877
878             /* find pid dirs for this user (uid) only */
879             if (ps.st_uid != uid)
880                 continue;
881
882             pid = atoi(de->d_name);
883
884             /* check if it's a valid child */
885             if (pid < uclient->pid)
886                 continue;
887
888             /* scan up to pid to find if a chain exists */
889             for (tmppid = pid; tmppid > uclient->pid;) {
890                 tmppid = win_mgr_get_ppid(tmppid);
891                 if (tmppid == uclient->pid)
892                     is_child = 1;
893             }
894
895             if (is_child) {
896                 status = aul_app_get_appid_bypid(pid, uclient->appid,
897                                                       ICO_IVI_APPID_LENGTH);
898
899                 uifw_debug("win_mgr_get_client_appid: aul_app_get_appid_bypid "
900                            "ret=%d pid=%d appid=<%s>", status, pid,
901                            uclient->appid);
902             }
903         }
904     }
905 #else       /* work around: Walk through child processes until app ID is found  */
906     status = aul_app_get_appid_bypid(uclient->pid, uclient->appid, ICO_IVI_APPID_LENGTH);
907     uifw_trace("win_mgr_get_client_appid: aul_app_get_appid_bypid ret=%d "
908                "pid=%d appid=<%s>", status, uclient->pid, uclient->appid);
909 #endif      /* work around: Walk through child processes until app ID is found  */
910
911     if (uclient->appid[0] != 0) {
912         /* OK, end of get appid         */
913         uclient->fixed_appid = ICO_WINDOW_MGR_APPID_FIXCOUNT;
914     }
915     else    {
916         /* client does not exist in AppCore, search Linux process table */
917
918         int     fd;
919         int     size;
920         int     i;
921         int     j;
922         char    procpath[128];
923
924         uclient->fixed_appid ++;
925         memset(uclient->appid, 0, ICO_IVI_APPID_LENGTH);
926         snprintf(procpath, sizeof(procpath)-1, "/proc/%d/cmdline", uclient->pid);
927         fd = open(procpath, O_RDONLY);
928         if (fd >= 0)    {
929             size = read(fd, procpath, sizeof(procpath));
930             for (; size > 0; size--)    {
931                 if (procpath[size-1])   break;
932             }
933             if (size > 0)   {
934                 /* get program base name    */
935                 i = 0;
936                 for (j = 0; j < size; j++)  {
937                     if (procpath[j] == 0)   break;
938                     if (procpath[j] == '/') i = j + 1;
939                 }
940                 j = 0;
941                 for (; i < size; i++)   {
942                     uclient->appid[j] = procpath[i];
943                     if ((uclient->appid[j] == 0) ||
944                         (j >= (ICO_IVI_APPID_LENGTH-1)))    break;
945                     j++;
946                 }
947                 /* search application number in apprication start option    */
948                 if ((uclient->appid[j] == 0) && (j < (ICO_IVI_APPID_LENGTH-2))) {
949                     for (; i < size; i++)   {
950                         if ((procpath[i] == 0) &&
951                             (procpath[i+1] == '@')) {
952                             strncpy(&uclient->appid[j], &procpath[i+1],
953                                     ICO_IVI_APPID_LENGTH - j - 2);
954                         }
955                     }
956                 }
957             }
958             close(fd);
959         }
960         for (i = strlen(uclient->appid)-1; i >= 0; i--) {
961             if (uclient->appid[i] != ' ')   break;
962         }
963         uclient->appid[i+1] = 0;
964         if (uclient->appid[0])  {
965             uifw_trace("win_mgr_get_client_appid: pid=%d appid=<%s> from "
966                        "Process table(%d)",
967                        uclient->pid, uclient->appid, uclient->fixed_appid );
968         }
969         else    {
970             uifw_trace("win_mgr_get_client_appid: pid=%d dose not exist in Process table",
971                        uclient->pid);
972             sprintf(uclient->appid, "?%d?", uclient->pid);
973         }
974     }
975 }
976
977 /*--------------------------------------------------------------------------*/
978 /**
979  * @brief   ico_get_animation_name: convert animation name to Id value
980  *
981  * @param[in]   animation       animation name
982  * @return      animation Id value
983  */
984 /*--------------------------------------------------------------------------*/
985 static int
986 ico_get_animation_name(const char *animation)
987 {
988     int anima = ICO_WINDOW_MGR_ANIMATION_NONE;
989
990     if (strcasecmp(animation, "none") == 0) {
991         return ICO_WINDOW_MGR_ANIMATION_NONE;
992     }
993
994     if (win_mgr_hook_animation) {
995         anima = (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_NAME, (void *)animation);
996     }
997     if (anima <= 0) {
998         anima = ICO_WINDOW_MGR_ANIMATION_NONE;
999     }
1000     return anima;
1001 }
1002
1003 /*--------------------------------------------------------------------------*/
1004 /**
1005  * @brief   win_mgr_select_surface: select surface by mouse click
1006  *
1007  * @param[in]   seat            weston seat
1008  * @param[in]   focus           selected surface
1009  * @param[in]   select          selected device
1010  * @return      none
1011  */
1012 /*--------------------------------------------------------------------------*/
1013 static void
1014 win_mgr_select_surface(struct weston_seat *seat, struct weston_surface *focus, int select)
1015 {
1016     struct weston_surface   *surface;
1017     struct uifw_win_surface *usurf;
1018     struct uifw_manager     *mgr;
1019
1020     surface = weston_surface_get_main_surface(focus);
1021
1022     uifw_trace("win_mgr_select_surface: Enter(%08x,%d)", (int)surface, select);
1023
1024     if (! surface)  {
1025         uifw_trace("win_mgr_select_surface: Leave(no surface)");
1026         return;
1027     }
1028     /* find surface         */
1029     usurf = find_uifw_win_surface_by_ws(surface);
1030     if (! usurf) {
1031         uifw_trace("win_mgr_select_surface: Leave(usurf not exist)");
1032         return;
1033     }
1034     /* surface active       */
1035     weston_surface_activate(surface, seat);
1036
1037     /* send active event to manager     */
1038     wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
1039         uifw_trace("win_mgr_select_surface: Send Manager ACTIVE(surf=%08x)",
1040                    usurf->surfaceid);
1041         ico_window_mgr_send_window_active(mgr->resource, usurf->surfaceid,
1042                                           select);
1043     }
1044     uifw_trace("win_mgr_select_surface: Leave");
1045 }
1046
1047 /*--------------------------------------------------------------------------*/
1048 /**
1049  * @brief   win_mgr_click_to_activate: select surface by mouse click
1050  *
1051  * @param[in]   seat            weston seat
1052  * @param[in]   time            click time(current time)
1053  * @param[in]   button          click button
1054  * @param[in]   data            user data(unused)
1055  * @return      none
1056  */
1057 /*--------------------------------------------------------------------------*/
1058 static void
1059 win_mgr_click_to_activate(struct weston_seat *seat, uint32_t time,
1060                           uint32_t button, void *data)
1061 {
1062     if (seat->pointer->grab != &seat->pointer->default_grab)
1063         return;
1064     if (seat->pointer->focus == NULL)
1065         return;
1066
1067     win_mgr_select_surface(seat, seat->pointer->focus->surface,
1068                            ICO_WINDOW_MGR_SELECT_POINTER);
1069 }
1070
1071 /*--------------------------------------------------------------------------*/
1072 /**
1073  * @brief   win_mgr_touch_to_activate: select surface by touch touch-panel
1074  *
1075  * @param[in]   seat            weston seat
1076  * @param[in]   time            click time(current time)
1077  * @param[in]   data            user data(unused)
1078  * @return      none
1079  */
1080 /*--------------------------------------------------------------------------*/
1081 static void
1082 win_mgr_touch_to_activate(struct weston_seat *seat, uint32_t time, void *data)
1083 {
1084     if (seat->touch->grab != &seat->touch->default_grab)
1085         return;
1086     if (seat->touch->focus == NULL)
1087         return;
1088
1089     win_mgr_select_surface(seat, seat->touch->focus->surface, ICO_WINDOW_MGR_SELECT_TOUCH);
1090 }
1091
1092 /*--------------------------------------------------------------------------*/
1093 /**
1094  * @brief   ico_ivi_surfaceCreateNotification: create ivi-surface
1095  *
1096  * @param[in]   ivisurf         ivi surface
1097  * @param[in]   userdata        User Data(Unused)
1098  * @return      none
1099  */
1100 /*--------------------------------------------------------------------------*/
1101 static void
1102 ico_ivi_surfaceCreateNotification(struct weston_layout_surface *ivisurf, void *userdata)
1103 {
1104     uint32_t                id_surface;
1105     struct weston_view      *ev;
1106     struct weston_surface   *es;
1107     struct wl_client        *client;
1108
1109     id_surface = weston_layout_getIdOfSurface(ivisurf);
1110     uifw_trace("ico_ivi_surfaceCreateNotification: Create %x", id_surface);
1111
1112     /* set property notification    */
1113     if (weston_layout_surfaceAddNotification(ivisurf, ico_ivi_surfacePropertyNotification, NULL) != 0)  {
1114         uifw_error("ico_ivi_surfaceCreateNotification: weston_layout_surfaceAddNotification Error");
1115     }
1116     ev = weston_layout_get_weston_view(ivisurf);
1117     if (! ev)   {
1118         uifw_error("ico_ivi_surfaceCreateNotification: weston_layout_get_weston_view Error");
1119     }
1120     else    {
1121         es = ev->surface;
1122         if (! es)   {
1123             uifw_error("ico_ivi_surfaceCreateNotification: no weston_surface");
1124         }
1125         else    {
1126             client = wl_resource_get_client(es->resource);
1127             if (! client)   {
1128                 uifw_error("ico_ivi_surfaceCreateNotification: no wl_client");
1129             }
1130             else    {
1131                 win_mgr_register_surface(id_surface, es, client, ivisurf);
1132             }
1133         }
1134     }
1135 }
1136
1137 /*--------------------------------------------------------------------------*/
1138 /**
1139  * @brief   ico_ivi_surfaceRemoveNotification: remove ivi-surface
1140  *
1141  * @param[in]   ivisurf         ivi surface
1142  * @param[in]   userdata        User Data(Unused)
1143  * @return      none
1144  */
1145 /*--------------------------------------------------------------------------*/
1146 static void
1147 ico_ivi_surfaceRemoveNotification(struct weston_layout_surface *ivisurf, void *userdata)
1148 {
1149     uint32_t    id_surface;
1150     struct weston_view      *ev;
1151     struct weston_surface   *es;
1152
1153     id_surface = weston_layout_getIdOfSurface(ivisurf);
1154     uifw_trace("ico_ivi_surfaceRemoveNotification: Remove %x", id_surface);
1155
1156     ev = weston_layout_get_weston_view(ivisurf);
1157     if (! ev)   {
1158         uifw_error("ico_ivi_surfaceRemoveNotification: weston_layout_get_weston_view Error");
1159     }
1160     else    {
1161         es = ev->surface;
1162         if (! es)   {
1163             uifw_error("ico_ivi_surfaceRemoveNotification: no weston_surface");
1164         }
1165         else    {
1166             win_mgr_destroy_surface(es);
1167         }
1168     }
1169 }
1170
1171 /*--------------------------------------------------------------------------*/
1172 /**
1173  * @brief   ico_ivi_surfaceConfigureNotification: configure ivi-surface
1174  *
1175  * @param[in]   ivisurf         ivi surface
1176  * @param[in]   userdata        User Data(Unused)
1177  * @return      none
1178  */
1179 /*--------------------------------------------------------------------------*/
1180 static void
1181 ico_ivi_surfaceConfigureNotification(struct weston_layout_surface *ivisurf, void *userdata)
1182 {
1183     struct weston_view *view;
1184     struct weston_surface *surface;
1185     uint32_t    id_surface;
1186 #if 1   /* for check genivi     */
1187     struct weston_layout_SurfaceProperties  prop;
1188 #endif
1189
1190     id_surface = weston_layout_getIdOfSurface(ivisurf);
1191     view = weston_layout_get_weston_view(ivisurf);
1192     if (! view) {
1193         uifw_trace("ico_ivi_surfaceConfigureNotification: %08x has no view",
1194                    id_surface);
1195     }
1196     else    {
1197         surface = view->surface;
1198         if (! surface)  {
1199             uifw_trace("ico_ivi_surfaceConfigureNotification: %08x has no surface",
1200                        id_surface);
1201         }
1202         else    {
1203 #if 1   /* for check genivi     */
1204             if (weston_layout_getPropertiesOfSurface(ivisurf, &prop) != 0)  {
1205                 uifw_trace("ico_ivi_surfaceConfigureNotification: Properties get Error");
1206                 uifw_trace("ico_ivi_surfaceConfigureNotification: Configure %08x "
1207                            "x/y=%d/%d w/h=%d/%d",
1208                            id_surface, (int)view->geometry.x, (int)view->geometry.y,
1209                            surface->width, surface->height);
1210             }
1211             else    {
1212                 uifw_trace("ico_ivi_surfaceConfigureNotification: Configure %08x "
1213                            "x/y=%d/%d->%d/%d w/h=%d/%d(%d/%d)->%d/%d",
1214                            id_surface, prop.destX, prop.destY,
1215                            (int)view->geometry.x, (int)view->geometry.y,
1216                            prop.sourceWidth, prop.sourceHeight,
1217                            prop.origSourceWidth, prop.origSourceHeight,
1218                            surface->width, surface->height);
1219             }
1220 #else
1221             uifw_trace("ico_ivi_surfaceConfigureNotification: Configure %08x "
1222                        "x/y=%d/%d w/h=%d/%d",
1223                        id_surface, (int)view->geometry.x, (int)view->geometry.y,
1224                        surface->width, surface->height);
1225 #endif
1226             weston_layout_surfaceSetSourceRectangle(ivisurf,
1227                         0, 0, surface->width, surface->height);
1228             weston_layout_surfaceSetDestinationRectangle(ivisurf,
1229                         (uint32_t)view->geometry.x, (uint32_t)view->geometry.y,
1230                         surface->width, surface->height);
1231             weston_layout_commitChanges();
1232         }
1233     }
1234 }
1235
1236 /*--------------------------------------------------------------------------*/
1237 /**
1238  * @brief   ico_ivi_surfacePropertyNotification: property change ivi-surface
1239  *
1240  * @param[in]   ivisurf         ivi surface
1241  * @param[in]   userdata        User Data(Unused)
1242  * @return      none
1243  */
1244 /*--------------------------------------------------------------------------*/
1245 static void
1246 ico_ivi_surfacePropertyNotification(struct weston_layout_surface *ivisurf,
1247                                     struct weston_layout_SurfaceProperties *prop,
1248                                     enum weston_layout_notification_mask mask,
1249                                     void *userdata)
1250 {
1251     uint32_t    id_surface;
1252     int         retanima;
1253     struct uifw_win_surface *usurf;
1254
1255     id_surface = weston_layout_getIdOfSurface(ivisurf);
1256     if (mask != 0)  {
1257         usurf = ico_window_mgr_get_usurf(id_surface);
1258         uifw_trace("ico_ivi_surfacePropertyNotification: Property %x(%08x) usurf=%08x",
1259                    id_surface, mask, (int)usurf);
1260         if ((mask & IVI_NOTIFICATION_VISIBILITY) && (usurf != NULL))    {
1261             if ((usurf->visible == 0) && (prop->visibility)) {
1262                 uifw_trace("ico_ivi_surfacePropertyNotification: %08x Visible 0=>1",
1263                            id_surface);
1264                 usurf->visible = 1;
1265                 if ((usurf->animation.show_anima != ICO_WINDOW_MGR_ANIMATION_NONE) &&
1266                     (win_mgr_hook_animation != NULL))   {
1267                     /* show with animation      */
1268                     usurf->animation.pos_x = usurf->x;
1269                     usurf->animation.pos_y = usurf->y;
1270                     usurf->animation.pos_width = usurf->width;
1271                     usurf->animation.pos_height = usurf->height;
1272                     retanima =
1273                         (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_OPSHOW,
1274                                                   (void *)usurf);
1275                     uifw_trace("ico_ivi_surfacePropertyNotification: ret call anima = %d",
1276                                retanima);
1277                 }
1278             }
1279             else if ((usurf->visible != 0) && (! prop->visibility))  {
1280                 uifw_trace("ico_ivi_surfacePropertyNotification: %08x Visible 1=>0",
1281                            id_surface);
1282                 if ((usurf->animation.show_anima != ICO_WINDOW_MGR_ANIMATION_NONE) &&
1283                     (win_mgr_hook_animation != NULL))   {
1284                     /* hide with animation      */
1285                     usurf->animation.pos_x = usurf->x;
1286                     usurf->animation.pos_y = usurf->y;
1287                     usurf->animation.pos_width = usurf->width;
1288                     usurf->animation.pos_height = usurf->height;
1289                     retanima =
1290                         (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_OPHIDE,
1291                                                   (void *)usurf);
1292                 }
1293                 else    {
1294                     retanima = ICO_WINDOW_MGR_ANIMATION_RET_ANIMA;
1295                 }
1296                 if (retanima != ICO_WINDOW_MGR_ANIMATION_RET_ANIMANOCTL)    {
1297                     usurf->visible = 0;
1298                     uifw_trace("ico_ivi_surfacePropertyNotification: Change to UnVisible");
1299                 }
1300                 else    {
1301                     usurf->visible = 1;
1302                     uifw_trace("ico_ivi_surfacePropertyNotification: Change to Visible");
1303                     weston_layout_surfaceSetVisibility(ivisurf, 1);
1304                     weston_layout_commitChanges();
1305                 }
1306             }
1307             else    {
1308                 uifw_trace("ico_ivi_surfacePropertyNotification: visible no change");
1309             }
1310         }
1311     }
1312 }
1313
1314 /*--------------------------------------------------------------------------*/
1315 /**
1316  * @brief   win_mgr_register_surface: create UIFW surface
1317  *
1318  * @param[in]   id_surface      surface id (by ivi-shell)
1319  * @param[in]   surface         Weston surface
1320  * @param[in]   client          Wayland client
1321  * @param[in]   ivisurf         weston layout surface
1322  * @return      none
1323  */
1324 /*--------------------------------------------------------------------------*/
1325 static void
1326 win_mgr_register_surface(uint32_t id_surface, struct weston_surface *surface,
1327                          struct wl_client *client, struct weston_layout_surface *ivisurf)
1328 {
1329     struct uifw_win_surface *usurf;
1330     struct uifw_win_surface *phash;
1331     struct uifw_win_surface *bhash;
1332     uint32_t    hash;
1333
1334     uifw_trace("win_mgr_register_surface: Enter(surf=%x[%08x],client=%08x,ivisurf=%08x)",
1335                id_surface, (int)surface, (int)client, (int)ivisurf);
1336
1337     /* check new surface                    */
1338     if (find_uifw_win_surface_by_ws(surface))   {
1339         /* surface exist, NOP               */
1340         uifw_trace("win_mgr_register_surface: Leave(Already Exist)");
1341         return;
1342     }
1343
1344     /* set default color and shader */
1345     weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
1346     /* create UIFW surface management table */
1347     usurf = malloc(sizeof(struct uifw_win_surface));
1348     if (! usurf)    {
1349         uifw_error("win_mgr_register_surface: No Memory");
1350         return;
1351     }
1352
1353     memset(usurf, 0, sizeof(struct uifw_win_surface));
1354
1355     usurf->surfaceid = id_surface;
1356     usurf->surface = surface;
1357     usurf->ivisurf = ivisurf;
1358     usurf->node_tbl = &_ico_node_table[0];  /* set default node table (display no=0)    */
1359     wl_list_init(&usurf->client_link);
1360     wl_list_init(&usurf->animation.animation.link);
1361     wl_list_init(&usurf->surf_map);
1362     wl_list_init(&usurf->input_region);
1363     usurf->animation.hide_anima = ico_get_animation_name(ico_ivi_default_animation_name());
1364     usurf->animation.hide_time = ico_ivi_default_animation_time();
1365     usurf->animation.show_anima = usurf->animation.hide_anima;
1366     usurf->animation.show_time = usurf->animation.hide_time;
1367     usurf->animation.move_anima = usurf->animation.hide_anima;
1368     usurf->animation.move_time = usurf->animation.hide_time;
1369     usurf->animation.resize_anima = usurf->animation.hide_anima;
1370     usurf->animation.resize_time = usurf->animation.hide_time;
1371     usurf->visible = 0;
1372
1373     /* set client                           */
1374     usurf->uclient = ico_window_mgr_find_uclient(client);
1375     if (! usurf->uclient)  {
1376         /* client not exist, create client management table */
1377         uifw_trace("win_mgr_register_surface: Create Client");
1378         win_mgr_bind_client(client, NULL);
1379         usurf->uclient = ico_window_mgr_find_uclient(client);
1380         if (! usurf->uclient)  {
1381             uifw_error("win_mgr_register_surface: No Memory");
1382             return;
1383         }
1384     }
1385     wl_list_insert(usurf->uclient->surface_link.prev, &usurf->client_link);
1386
1387     /* make surface id hash table       */
1388     hash = MAKE_IDHASH(usurf->surfaceid);
1389     phash = _ico_win_mgr->idhash[hash];
1390     bhash = NULL;
1391     while (phash)   {
1392         bhash = phash;
1393         phash = phash->next_idhash;
1394     }
1395     if (bhash)  {
1396         bhash->next_idhash = usurf;
1397     }
1398     else    {
1399         _ico_win_mgr->idhash[hash] = usurf;
1400     }
1401
1402     /* make weston surface hash table   */
1403     hash = MAKE_WSHASH(usurf->surface);
1404     phash = _ico_win_mgr->wshash[hash];
1405     bhash = NULL;
1406     while (phash)   {
1407         bhash = phash;
1408         phash = phash->next_wshash;
1409     }
1410     if (bhash)  {
1411         bhash->next_wshash = usurf;
1412     }
1413     else    {
1414         _ico_win_mgr->wshash[hash] = usurf;
1415     }
1416     uifw_trace("win_mgr_register_surface: Leave(surfaceId=%08x)", usurf->surfaceid);
1417 }
1418
1419 /*--------------------------------------------------------------------------*/
1420 /**
1421  * @brief   uifw_set_animation: set animation of surface visible/unvisible
1422  *
1423  * @param[in]   client      Weyland client
1424  * @param[in]   resource    resource of request
1425  * @param[in]   surfaceid   UIFW surface id
1426  * @param[in]   type        how to change surface
1427  * @param[in]   anmation    animation name
1428  * @param[in]   time        animation time(ms), if 0, default time
1429  * @return      none
1430  */
1431 /*--------------------------------------------------------------------------*/
1432 static void
1433 uifw_set_animation(struct wl_client *client, struct wl_resource *resource,
1434                    uint32_t surfaceid, int32_t type, const char *animation, int32_t time)
1435 {
1436     int     animaid;
1437     struct uifw_win_surface *usurf = ico_window_mgr_get_usurf_client(surfaceid, client);
1438
1439     uifw_trace("uifw_set_transition: surf=%08x,type=%x,anim=%s,time=%d",
1440                surfaceid, type, animation, time);
1441
1442     if (usurf) {
1443         if ((*animation != 0) && (*animation != ' '))   {
1444             animaid = ico_get_animation_name(animation);
1445             uifw_trace("uifw_set_animation: Leave(OK) type=%d", animaid);
1446             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_HIDE)  {
1447                 if ((usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPHIDE) ||
1448                     (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPHIDEPOS))  {
1449                     usurf->animation.next_anima = animaid;
1450                 }
1451                 else    {
1452                     usurf->animation.hide_anima = animaid;
1453                 }
1454             }
1455             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_SHOW)  {
1456                 if ((usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPSHOW) ||
1457                     (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPSHOWPOS))  {
1458                     usurf->animation.next_anima = animaid;
1459                 }
1460                 else    {
1461                     usurf->animation.show_anima = animaid;
1462                 }
1463             }
1464             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_MOVE)  {
1465                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPMOVE)    {
1466                     usurf->animation.next_anima = animaid;
1467                 }
1468                 else    {
1469                     usurf->animation.move_anima = animaid;
1470                 }
1471             }
1472             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_RESIZE)    {
1473                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPRESIZE)  {
1474                     usurf->animation.next_anima = animaid;
1475                 }
1476                 else    {
1477                     usurf->animation.resize_anima = animaid;
1478                 }
1479             }
1480         }
1481         if ((time > 0) && (time < 10000))   {
1482             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_HIDE)  {
1483                 usurf->animation.hide_time = time;
1484             }
1485             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_SHOW)  {
1486                 usurf->animation.show_time = time;
1487             }
1488             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_MOVE)  {
1489                 usurf->animation.move_time = time;
1490             }
1491             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_RESIZE)    {
1492                 usurf->animation.resize_time = time;
1493             }
1494         }
1495     }
1496     else    {
1497         uifw_trace("uifw_set_animation: Surface(%08x) Not exist", surfaceid);
1498     }
1499 }
1500
1501 /*--------------------------------------------------------------------------*/
1502 /**
1503  * @brief   win_mgr_check_mapsurface: check and change all surface
1504  *
1505  * @param[in]   animation   weston animation table(unused)
1506  * @param[in]   outout      weston output table(unused)
1507  * @param[in]   mseces      current time(unused)
1508  * @return      none
1509  */
1510 /*--------------------------------------------------------------------------*/
1511 static void
1512 win_mgr_check_mapsurface(struct weston_animation *animation,
1513                          struct weston_output *output, uint32_t msecs)
1514 {
1515     struct uifw_surface_map *sm, *sm_tmp;
1516     uint32_t    curtime;
1517     int         wait = 99999999;
1518
1519     /* check touch down counter     */
1520     if ((touch_check_seat) &&
1521         (touch_check_seat->touch))  {
1522         if (touch_check_seat->touch->num_tp > 10)  {
1523             uifw_trace("win_mgr_check_mapsurface: illegal touch counter(num=%d), reset",
1524                        (int)touch_check_seat->touch->num_tp);
1525             touch_check_seat->touch->num_tp = 0;
1526         }
1527     }
1528
1529     /* check all mapped surfaces    */
1530     curtime = weston_compositor_get_time();
1531     wl_list_for_each_safe (sm, sm_tmp, &_ico_win_mgr->map_list, map_link)   {
1532         uifw_detail("win_mgr_check_mapsurface: sm=%08x surf=%08x",
1533                     (int)sm, sm->usurf->surfaceid);
1534         win_mgr_change_mapsurface(sm, 0, curtime);
1535         if (sm->eventque)   {
1536             if (sm->interval < wait)    {
1537                 wait = sm->interval;
1538             }
1539         }
1540     }
1541
1542     /* check frame interval         */
1543     if (wait < 2000)    {
1544         wait = wait / 2;
1545     }
1546     else    {
1547         wait = 1000;
1548     }
1549     wl_event_source_timer_update(_ico_win_mgr->wait_mapevent, wait);
1550 }
1551
1552 /*--------------------------------------------------------------------------*/
1553 /**
1554  * @brief   win_mgr_timer_mapsurface: mapped surface check timer
1555  *
1556  * @param[in]   data        user data(unused)
1557  * @return      fixed 1
1558  */
1559 /*--------------------------------------------------------------------------*/
1560 static int
1561 win_mgr_timer_mapsurface(void *data)
1562 {
1563     win_mgr_check_mapsurface(NULL, NULL, 0);
1564     return 1;
1565 }
1566
1567 /*--------------------------------------------------------------------------*/
1568 /**
1569  * @brief   win_mgr_change_mapsurface: check and change mapped surface
1570  *
1571  * @param[in]   sm          map surface table
1572  * @param[in]   event       send event (if 0, send if changed)
1573  * @param[in]   curtime     current time(ms)
1574  * @return      none
1575  */
1576 /*--------------------------------------------------------------------------*/
1577 static void
1578 win_mgr_change_mapsurface(struct uifw_surface_map *sm, int event, uint32_t curtime)
1579 {
1580     struct weston_surface   *es;
1581     struct wl_shm_buffer    *shm_buffer;
1582     int         width;
1583     int         height;
1584     uint32_t    format;
1585     uint32_t    dtime;
1586
1587     uifw_detail("win_mgr_change_mapsurface: surf=%08x event=%d", sm->usurf->surfaceid, event);
1588     if (event == 0) {
1589         event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_CONTENTS;
1590     }
1591
1592     /* check if buffered        */
1593     es = sm->usurf->surface;
1594     if ((es == NULL) ||
1595         ((sm->type == ICO_WINDOW_MGR_MAP_TYPE_EGL) &&
1596          ((es->buffer_ref.buffer == NULL) ||
1597           (es->buffer_ref.buffer->width <= 0) || (es->buffer_ref.buffer->height <= 0)))) {
1598         /* surface has no buffer    */
1599         uifw_debug("win_mgr_change_mapsurface: surface(%08x) has no buffer %08x %08x",
1600                    sm->usurf->surfaceid, (int)es,
1601                    es ? (int)es->buffer_ref.buffer : 0);
1602         if (sm->initflag)   {
1603             event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP;
1604         }
1605         else    {
1606             event = 0;
1607         }
1608     }
1609     else if (sm->type == ICO_WINDOW_MGR_MAP_TYPE_EGL)   {
1610         if ((es->buffer_ref.buffer->legacy_buffer != NULL) && (es->renderer_state != NULL)) {
1611             if ((void *)wl_resource_get_user_data(
1612                             (struct wl_resource *)es->buffer_ref.buffer->legacy_buffer)
1613                 == NULL)    {
1614                 /* surface has no buffer    */
1615                 uifw_debug("win_mgr_change_mapsurface: surface(%08x) has no buffer",
1616                            sm->usurf->surfaceid);
1617                 if (sm->initflag)   {
1618                     event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP;
1619                 }
1620                 else    {
1621                     event = 0;
1622                 }
1623             }
1624         }
1625     }
1626     else    {
1627         if (es->buffer_ref.buffer != NULL)  {
1628             shm_buffer = wl_shm_buffer_get(es->buffer_ref.buffer->resource);
1629             if (shm_buffer) {
1630                 format = wl_shm_buffer_get_format(shm_buffer);
1631                 if (format != WL_SHM_FORMAT_ARGB8888)   {
1632                     uifw_trace("win_mgr_change_mapsurface: %08x shm_buffer type %x",
1633                                sm->usurf->surfaceid, format);
1634                     event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP;
1635                 }
1636             }
1637         }
1638     }
1639
1640     if ((event != 0) && (event != ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP))  {
1641
1642         if (sm->type == ICO_WINDOW_MGR_MAP_TYPE_EGL)    {
1643             format = EGL_TEXTURE_RGBA;          /* currently only support RGBA  */
1644             width = es->buffer_ref.buffer->width;
1645             height = es->buffer_ref.buffer->height;
1646             if ((sm->initflag == 0) && (width > 0) && (height > 0)) {
1647                 sm->initflag = 1;
1648                 event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP;
1649             }
1650             else if ((width <= 0) || (height <= 0)) {
1651                 event = 0;
1652             }
1653             else if (event == ICO_WINDOW_MGR_MAP_SURFACE_EVENT_CONTENTS)    {
1654                 if ((sm->width != width) || (sm->height != height) ||
1655                     (format != sm->format)) {
1656                     event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_RESIZE;
1657                 }
1658                 else    {
1659                     if (es->buffer_ref.buffer->legacy_buffer != sm->curbuf) {
1660 #if  PERFORMANCE_EVALUATIONS > 0
1661                         uifw_perf("SWAP_BUFFER appid=%s surface=%08x",
1662                                   sm->usurf->uclient->appid, sm->usurf->surfaceid);
1663 #endif /*PERFORMANCE_EVALUATIONS*/
1664                         dtime = curtime - sm->lasttime;
1665                         if ((sm->interval > 0) && (dtime < sm->interval))   {
1666                             sm->eventque = 1;
1667                             event = 0;
1668                         }
1669                     }
1670                     else if (sm->eventque)  {
1671                         dtime = curtime - sm->lasttime;
1672                         if ((sm->interval > 0) && (dtime < sm->interval))   {
1673                             event = 0;
1674                         }
1675                     }
1676                     else    {
1677                         event =0;
1678                     }
1679                 }
1680             }
1681             sm->width = width;
1682             sm->height = height;
1683             sm->stride = width * 4;
1684             sm->format = format;
1685             sm->curbuf = es->buffer_ref.buffer->legacy_buffer;
1686         }
1687         else    {
1688             if ((sm->eventque != 0) ||
1689                 (es->buffer_ref.buffer == NULL) || (es->buffer_ref.buffer != sm->curbuf)) {
1690                 sm->curbuf = es->buffer_ref.buffer;
1691                 if (es->buffer_ref.buffer != NULL)  {
1692                     width = es->buffer_ref.buffer->width;
1693                     height = es->buffer_ref.buffer->height;
1694                 }
1695                 else    {
1696                     width = es->width;
1697                     height = es->height;
1698                 }
1699                 if ((sm->initflag == 0) && (width > 0) && (height > 0))    {
1700                     sm->initflag = 1;
1701                     event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP;
1702                     uifw_detail("win_mgr_change_mapsurface: PIX MAP event %08x",
1703                                 sm->usurf->surfaceid);
1704                 }
1705                 else    {
1706                     if ((width <= 0) || (height <= 0))  {
1707                         event = 0;
1708                         sm->curbuf = NULL;
1709                         uifw_detail("win_mgr_change_mapsurface: PIX %08x w/h=0/0",
1710                                     sm->usurf->surfaceid);
1711                     }
1712                     else if (event == ICO_WINDOW_MGR_MAP_SURFACE_EVENT_CONTENTS)    {
1713 #if  PERFORMANCE_EVALUATIONS > 0
1714                         if (sm->type != ICO_WINDOW_MGR_MAP_TYPE_SHM)    {
1715                             uifw_perf("SWAP_BUFFER appid=%s surface=%08x",
1716                                       sm->usurf->uclient->appid, sm->usurf->surfaceid);
1717                         }
1718 #endif /*PERFORMANCE_EVALUATIONS*/
1719                         if ((sm->width != width) || (sm->height != height)) {
1720                             event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_RESIZE;
1721                         }
1722                         else    {
1723                             dtime = curtime - sm->lasttime;
1724                             if ((sm->interval > 0) && (dtime < sm->interval))   {
1725                                 sm->eventque = 1;
1726                                 event = 0;
1727                                 uifw_detail("win_mgr_change_mapsurface: PIX %08x new queue",
1728                                             sm->usurf->surfaceid);
1729                             }
1730                             else if (sm->eventque)  {
1731                                 dtime = curtime - sm->lasttime;
1732                                 if ((sm->interval > 0) && (dtime < sm->interval))   {
1733                                     event = 0;
1734                                     uifw_detail("win_mgr_change_mapsurface: PIX %08x queued",
1735                                                 sm->usurf->surfaceid);
1736                                 }
1737                             }
1738                         }
1739                     }
1740                 }
1741                 sm->width = width;
1742                 sm->height = height;
1743                 sm->stride = width * 4;
1744                 sm->format = EGL_TEXTURE_RGBA;
1745             }
1746             else    {
1747                 event = 0;
1748             }
1749         }
1750     }
1751
1752     if (event != 0) {
1753         uifw_detail("win_mgr_change_mapsurface: send MAP event(ev=%d surf=%08x type=%d "
1754                     "w/h/s=%d/%d/%d format=%x",
1755                     event, sm->usurf->surfaceid, sm->type,
1756                     sm->width, sm->height, sm->stride, sm->format);
1757         sm->lasttime = curtime;
1758         sm->eventque = 0;
1759         if ((event != ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR) &&
1760             (sm->filepath != NULL)) {
1761             if (weston_layout_takeSurfaceScreenshot(sm->filepath,
1762                                                     sm->usurf->ivisurf) != 0)   {
1763                 uifw_warn("win_mgr_change_mapsurface: surface.%08x image read(%s) Error",
1764                           sm->usurf->surfaceid, sm->filepath);
1765                 event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR;
1766             }
1767         }
1768         ico_window_mgr_send_map_surface(sm->uclient->mgr->resource, event,
1769                                         sm->usurf->surfaceid, sm->type,
1770                                         sm->width, sm->height, sm->stride, sm->format);
1771         if (event == ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR)    {
1772             /* free map table if error  */
1773             wl_list_remove(&sm->surf_link);
1774             wl_list_remove(&sm->map_link);
1775             sm->usurf = (struct uifw_win_surface *)_ico_win_mgr->free_maptable;
1776             _ico_win_mgr->free_maptable = sm;
1777         }
1778     }
1779 }
1780
1781 /*--------------------------------------------------------------------------*/
1782 /**
1783  * @brief   uifw_map_surface: mapped surface buffer to system application
1784  *
1785  * @param[in]   client      Weyland client
1786  * @param[in]   resource    resource of request
1787  * @param[in]   surfaceid   surface id
1788  * @param[in]   framerate   frame rate of surface update(frame/sec)
1789  * @param[in]   filepath    surface image file path(if NULL, not create file)
1790  * @return      none
1791  */
1792 /*--------------------------------------------------------------------------*/
1793 static void
1794 uifw_map_surface(struct wl_client *client, struct wl_resource *resource,
1795                  uint32_t surfaceid, int32_t framerate, const char *filepath)
1796 {
1797     struct uifw_win_surface     *usurf;
1798     struct weston_surface       *es;
1799     struct uifw_surface_map     *sm;
1800     struct weston_buffer        *buffer;
1801     struct wl_shm_buffer        *shm_buffer;
1802     struct uifw_client          *uclient;
1803     struct uifw_gl_surface_state *gl_state;
1804     int     maptype;
1805     int     format;
1806
1807     uifw_trace("uifw_map_surface: Enter(surface=%08x,fps=%d,file=%s)",
1808                surfaceid, framerate, filepath ? filepath : "(null)");
1809
1810     uclient = ico_window_mgr_find_uclient(client);
1811     usurf = ico_window_mgr_get_usurf(surfaceid);
1812     if (! usurf)    {
1813         /* surface dose not exist, error        */
1814         ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
1815                                         surfaceid, 1, 0, 0, 0, 0);
1816         uifw_trace("uifw_map_surface: Leave(surface=%08x dose not exist)", surfaceid);
1817         return;
1818     }
1819
1820     /* check if buffered        */
1821     es = usurf->surface;
1822     if (es == NULL) {
1823         /* surface has no buffer, error         */
1824         ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
1825                                         surfaceid, 2, 0, 0, 0, 0);
1826         uifw_trace("uifw_map_surface: Leave(surface(%08x) has no surface)", surfaceid);
1827         return;
1828     }
1829     buffer = es->buffer_ref.buffer;
1830
1831     /* check buffer type        */
1832     gl_state = (struct uifw_gl_surface_state *)es->renderer_state;
1833     if (gl_state == NULL)   {
1834         ico_window_mgr_send_map_surface(resource,
1835                                         ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
1836                                         surfaceid, 3, 0, 0, 0, 0);
1837         uifw_trace("uifw_map_surface: Leave(surface(%08x) has no gl_state)", surfaceid);
1838         return;
1839     }
1840     else if (gl_state->buffer_type == BUFFER_TYPE_SHM)  {
1841         maptype = -1;
1842         format = 0xff;
1843         if (ico_ivi_optionflag() & ICO_IVI_OPTION_SUPPORT_SHM)  {
1844             if (buffer != NULL) {
1845                 shm_buffer = wl_shm_buffer_get(buffer->resource);
1846                 if (shm_buffer) {
1847                     format = wl_shm_buffer_get_format(shm_buffer);
1848                     uifw_detail("uifw_map_surface: %08x shm_buffer type %x",
1849                                 surfaceid, format);
1850                     if (format == WL_SHM_FORMAT_ARGB8888)   {
1851                         maptype = ICO_WINDOW_MGR_MAP_TYPE_SHM;
1852                     }
1853                 }
1854             }
1855             else    {
1856                 maptype = ICO_WINDOW_MGR_MAP_TYPE_SHM;
1857             }
1858         }
1859         if (maptype < 0)    {
1860             ico_window_mgr_send_map_surface(resource,
1861                                             ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
1862                                             surfaceid, 4, 0, 0, 0, 0);
1863             uifw_trace("uifw_map_surface: Leave(surface(%08x) not support shm_buffer(%x))",
1864                        surfaceid, format);
1865             return;
1866         }
1867     }
1868     else    {
1869         maptype = ICO_WINDOW_MGR_MAP_TYPE_EGL;
1870     }
1871
1872     /* maximum framerate        */
1873     if (maptype == ICO_WINDOW_MGR_MAP_TYPE_EGL) {
1874         if ((framerate <= 0) || (framerate > 15))
1875             framerate = 15;
1876     }
1877     else    {
1878         if ((framerate <= 0) || (framerate > 5))
1879             framerate = 5;
1880     }
1881
1882     /* check same surface       */
1883     wl_list_for_each(sm, &usurf->surf_map, surf_link) {
1884         if ((sm->usurf == usurf) && (sm->uclient == uclient))   {
1885             break;
1886         }
1887     }
1888
1889     if (&sm->surf_link == &usurf->surf_map) {
1890         /* create map table         */
1891         sm = _ico_win_mgr->free_maptable;
1892         if (sm) {
1893             _ico_win_mgr->free_maptable = (struct uifw_surface_map *)sm->usurf;
1894         }
1895         else    {
1896             sm = (struct uifw_surface_map *)malloc(sizeof(struct uifw_surface_map));
1897             if (! sm)   {
1898                 ico_window_mgr_send_map_surface(resource,
1899                                                 ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
1900                                                 surfaceid, 5, 0, 0, 0, 0);
1901                 uifw_trace("uifw_map_surface: Leave(malloc error)");
1902                 return;
1903             }
1904         }
1905         memset(sm, 0, sizeof(struct uifw_surface_map));
1906
1907         wl_list_init(&sm->map_link);
1908         wl_list_init(&sm->surf_link);
1909         sm->usurf = usurf;
1910         sm->uclient = uclient;
1911         sm->type = maptype;
1912         sm->framerate = framerate;
1913         sm->interval = (1000 / sm->framerate) - 1;
1914         wl_list_insert(_ico_win_mgr->map_list.next, &sm->map_link);
1915         wl_list_insert(usurf->surf_map.prev, &sm->surf_link);
1916     }
1917     else    {
1918         /* change frame rate    */
1919         uifw_trace("uifw_map_surface: Leave(chagne frame rate %d->%d",
1920                    sm->framerate, framerate);
1921         if (sm->framerate != framerate) {
1922             sm->framerate = framerate;
1923             sm->interval = (1000 / sm->framerate) - 1;
1924             win_mgr_change_mapsurface(sm, 0, weston_compositor_get_time());
1925         }
1926         return;
1927     }
1928
1929     memset(sm->filepath, 0, ICO_IVI_FILEPATH_LENGTH);
1930     if ((filepath != NULL) && (*filepath != 0) && (*filepath != ' '))   {
1931         strncpy(sm->filepath, filepath, ICO_IVI_FILEPATH_LENGTH-1);
1932     }
1933
1934     if (buffer != NULL) {
1935         sm->width = buffer->width;
1936         sm->height = buffer->height;
1937         if (maptype != ICO_WINDOW_MGR_MAP_TYPE_EGL) {
1938             sm->stride = sm->width * 4;
1939             sm->format = EGL_TEXTURE_RGBA;
1940             if ((sm->width > 0) && (sm->height > 0))    {
1941                 sm->initflag = 1;
1942             }
1943             uifw_debug("uifw_map_surface: map type=%d,surface=%08x,fps=%d,w/h=%d/%d",
1944                        maptype, surfaceid, framerate, buffer->width, buffer->height);
1945         }
1946         else    {
1947             if (wl_resource_get_user_data((struct wl_resource *)buffer->legacy_buffer)
1948                 != NULL)    {
1949                 sm->format = EGL_TEXTURE_RGBA;
1950                 if ((sm->width > 0) && (sm->height > 0) && (sm->stride > 0) &&
1951                     (gl_state != NULL))  {
1952                     sm->initflag = 1;
1953                 }
1954                 uifw_debug("uifw_map_surface: map EGL surface=%08x,fps=%d,w/h=%d/%d",
1955                            surfaceid, framerate, buffer->width, buffer->height);
1956             }
1957             else    {
1958                 uifw_debug("uifw_map_surface: map EGL but no buffer surface=%08x,fps=%d",
1959                            surfaceid, framerate);
1960             }
1961         }
1962     }
1963     else if (maptype != ICO_WINDOW_MGR_MAP_TYPE_EGL)    {
1964         sm->width = es->width;
1965         sm->height = es->height;
1966         sm->stride = sm->width * 4;
1967         sm->format = EGL_TEXTURE_RGBA;
1968         if ((sm->width > 0) && (sm->height > 0))    {
1969             sm->initflag = 1;
1970         }
1971         uifw_debug("uifw_map_surface: map type=%d,surface=%08x,fps=%d,w/h=%d/%d",
1972                    maptype, surfaceid, framerate, sm->width, sm->height);
1973     }
1974     else    {
1975         uifw_debug("uifw_map_surface: map EGL but no buffer surface=%08x,fps=%d",
1976                    surfaceid, framerate);
1977     }
1978
1979     /* send map event                       */
1980     if (sm->initflag)   {
1981         win_mgr_change_mapsurface(sm, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP,
1982                                   weston_compositor_get_time());
1983     }
1984     uifw_trace("uifw_map_surface: Leave");
1985 }
1986
1987 /*--------------------------------------------------------------------------*/
1988 /**
1989  * @brief   uifw_unmap_surface: unmap surface buffer
1990  *
1991  * @param[in]   client      Weyland client
1992  * @param[in]   resource    resource of request
1993  * @param[in]   surfaceid   surface id
1994  * @return      none
1995  */
1996 /*--------------------------------------------------------------------------*/
1997 static void
1998 uifw_unmap_surface(struct wl_client *client, struct wl_resource *resource,
1999                    uint32_t surfaceid)
2000 {
2001     struct uifw_win_surface *usurf;
2002     struct uifw_surface_map *sm, *sm_tmp;
2003     struct uifw_client      *uclient;
2004
2005     uifw_trace("uifw_unmap_surface: Enter(surface=%08x)", surfaceid);
2006
2007     usurf = ico_window_mgr_get_usurf(surfaceid);
2008     if (! usurf)    {
2009         /* surface dose not exist, error        */
2010         uifw_trace("uifw_unmap_surface: Leave(surface=%08x dose not exist)", surfaceid);
2011         return;
2012     }
2013     if (client) {
2014         uclient = ico_window_mgr_find_uclient(client);
2015         if ((! uclient) || (! uclient->mgr))    {
2016             /* client dose not exist, error         */
2017             uifw_trace("uifw_unmap_surface: Leave(client=%08x dose not exist)", (int)client);
2018             return;
2019         }
2020     }
2021     else    {
2022         uclient = NULL;
2023         wl_list_for_each (sm, &usurf->surf_map, surf_link) {
2024             if (sm->uclient->mgr != NULL) {
2025                 uifw_trace("uifw_unmap_surface: send UNMAP event(ev=%d surf=%08x "
2026                            "w/h/s=%d/%d/%d format=%x",
2027                            ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP, surfaceid,
2028                            sm->width, sm->height, sm->stride, sm->format);
2029                 ico_window_mgr_send_map_surface(sm->uclient->mgr->resource,
2030                                                 ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP,
2031                                                 surfaceid, sm->type, sm->width,
2032                                                 sm->height, sm->stride, sm->format);
2033             }
2034         }
2035     }
2036     wl_list_for_each_safe (sm, sm_tmp, &usurf->surf_map, surf_link) {
2037         if (((uclient != NULL) && (sm->uclient != uclient)))   continue;
2038         wl_list_remove(&sm->surf_link);
2039         wl_list_remove(&sm->map_link);
2040         if (sm->filepath[0])    {
2041             unlink(sm->filepath);
2042         }
2043         sm->usurf = (struct uifw_win_surface *)_ico_win_mgr->free_maptable;
2044         _ico_win_mgr->free_maptable = sm;
2045     }
2046     uifw_trace("uifw_unmap_surface: Leave");
2047 }
2048
2049 /*--------------------------------------------------------------------------*/
2050 /**
2051  * @brief   win_mgr_surface_configure: UIFW surface configure
2052  *
2053  * @param[in]   usurf       UIFW surface
2054  * @param[in]   x           X coordinate on screen
2055  * @param[in]   y           Y coordinate on screen
2056  * @param[in]   width       surface width
2057  * @param[in]   height      surface height
2058  * @return      none
2059  */
2060 /*--------------------------------------------------------------------------*/
2061 static void
2062 win_mgr_surface_configure(struct uifw_win_surface *usurf,
2063                           int x, int y, int width, int height)
2064 {
2065     struct weston_surface   *es;
2066     struct weston_view      *ev;
2067
2068     es = usurf->surface;
2069     if ((es != NULL) && (es->buffer_ref.buffer))  {
2070         if (usurf->client_width == 0)   {
2071             usurf->client_width = es->width;
2072             if (usurf->client_width == 0)
2073                 usurf->client_width = ico_ivi_surface_buffer_width(es);
2074         }
2075         if (usurf->client_height == 0)  {
2076             usurf->client_height = es->height;
2077             if (usurf->client_height == 0)
2078                 usurf->client_height = ico_ivi_surface_buffer_height(es);
2079         }
2080
2081         /* not set geometry width/height    */
2082         ev = ico_ivi_get_primary_view(usurf);
2083         weston_view_set_position(ev, x + usurf->xadd, y + usurf->yadd);
2084     }
2085 }
2086
2087 /*--------------------------------------------------------------------------*/
2088 /**
2089  * @brief   win_mgr_destroy_surface: surface destroy
2090  *
2091  * @param[in]   surface     Weston surface
2092  * @return      none
2093  */
2094 /*--------------------------------------------------------------------------*/
2095 static void
2096 win_mgr_destroy_surface(struct weston_surface *surface)
2097 {
2098     struct uifw_win_surface *usurf;
2099     struct uifw_win_surface *phash;
2100     struct uifw_win_surface *bhash;
2101     uint32_t    hash;
2102
2103     usurf = find_uifw_win_surface_by_ws(surface);
2104     if (! usurf) {
2105         uifw_trace("win_mgr_destroy_surface: UIFW surface Not Exist");
2106         return;
2107     }
2108     uifw_trace("win_mgr_destroy_surface: Enter(%08x) %08x", (int)surface, usurf->surfaceid);
2109
2110     /* destory input region         */
2111     if (win_mgr_hook_destory)   {
2112         (*win_mgr_hook_destory)(usurf);
2113     }
2114
2115     /* unmap surface                */
2116     if (&usurf->surf_map != usurf->surf_map.next)   {
2117         uifw_unmap_surface(NULL, NULL, usurf->surfaceid);
2118     }
2119
2120     /* destroy animation extenson   */
2121     if (win_mgr_hook_animation) {
2122         (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_DESTROY, (void *)usurf);
2123     }
2124
2125     /* delete from cleint list      */
2126     wl_list_remove(&usurf->client_link);
2127
2128     /* delete from hash table       */
2129     hash = MAKE_IDHASH(usurf->surfaceid);
2130     phash = _ico_win_mgr->idhash[hash];
2131     bhash = NULL;
2132     while ((phash) && (phash != usurf)) {
2133         bhash = phash;
2134         phash = phash->next_idhash;
2135     }
2136     if (bhash)  {
2137         bhash->next_idhash = usurf->next_idhash;
2138     }
2139     else    {
2140         _ico_win_mgr->idhash[hash] = usurf->next_idhash;
2141     }
2142
2143     hash = MAKE_WSHASH(usurf->surface);
2144     phash = _ico_win_mgr->wshash[hash];
2145     bhash = NULL;
2146     while ((phash) && (phash != usurf)) {
2147         bhash = phash;
2148         phash = phash->next_wshash;
2149     }
2150     if (bhash)  {
2151         bhash->next_wshash = usurf->next_wshash;
2152     }
2153     else    {
2154         _ico_win_mgr->wshash[hash] = usurf->next_wshash;
2155     }
2156
2157     free(usurf);
2158     uifw_trace("win_mgr_destroy_surface: Leave(OK)");
2159 }
2160
2161 /*--------------------------------------------------------------------------*/
2162 /**
2163  * @brief   bind_ico_win_mgr: bind Multi Window Manager from client
2164  *
2165  * @param[in]   client      client
2166  * @param[in]   data        user data(unused)
2167  * @param[in]   version     protocol version(unused)
2168  * @param[in]   id          client object id
2169  * @return      none
2170  */
2171 /*--------------------------------------------------------------------------*/
2172 static void
2173 bind_ico_win_mgr(struct wl_client *client,
2174                  void *data, uint32_t version, uint32_t id)
2175 {
2176     struct wl_resource  *add_resource;
2177     struct uifw_manager *mgr;
2178     struct uifw_client  *uclient;
2179
2180     uifw_trace("bind_ico_win_mgr: Enter(client=%08x, id=%x)", (int)client, (int)id);
2181
2182     add_resource = wl_resource_create(client, &ico_window_mgr_interface, 1, id);
2183     if (add_resource)   {
2184         wl_resource_set_implementation(add_resource, &ico_window_mgr_implementation,
2185                                        _ico_win_mgr, unbind_ico_win_mgr);
2186     }
2187
2188     /* Create client management tabel       */
2189     uclient = ico_window_mgr_find_uclient(client);
2190     if (! uclient)  {
2191         win_mgr_bind_client(client, NULL);
2192         uclient = ico_window_mgr_find_uclient(client);
2193     }
2194
2195     /* Manager                              */
2196     mgr = (struct uifw_manager *)malloc(sizeof(struct uifw_manager));
2197     if (! mgr)  {
2198         uifw_error("bind_ico_win_mgr: Error, No Memory");
2199         return;
2200     }
2201     memset(mgr, 0, sizeof(struct uifw_manager));
2202     mgr->resource = add_resource;
2203     if (uclient)    {
2204         uclient->mgr = mgr;
2205     }
2206     wl_list_insert(&_ico_win_mgr->manager_list, &mgr->link);
2207
2208     uifw_trace("bind_ico_win_mgr: Leave");
2209 }
2210
2211 /*--------------------------------------------------------------------------*/
2212 /**
2213  * @brief   unbind_ico_win_mgr: unbind Multi Window Manager from client
2214  *
2215  * @param[in]   resource    client resource
2216  * @return      none
2217  */
2218 /*--------------------------------------------------------------------------*/
2219 static void
2220 unbind_ico_win_mgr(struct wl_resource *resource)
2221 {
2222     struct uifw_manager *mgr, *itmp;
2223
2224     uifw_trace("unbind_ico_win_mgr: Enter");
2225
2226     /* Remove manager from manager list */
2227     wl_list_for_each_safe (mgr, itmp, &_ico_win_mgr->manager_list, link)    {
2228         if (mgr->resource == resource) {
2229             wl_list_remove(&mgr->link);
2230             free(mgr);
2231         }
2232     }
2233     uifw_trace("unbind_ico_win_mgr: Leave");
2234 }
2235
2236 /*--------------------------------------------------------------------------*/
2237 /**
2238  * @brief   ico_window_mgr_get_uclient: get UIFW client table
2239  *
2240  * @param[in]   appid       application Id
2241  * @return      UIFW client table
2242  * @retval      !=NULL      success(UIFW client table address)
2243  * @retval      = NULL      error(appid not exist)
2244  */
2245 /*--------------------------------------------------------------------------*/
2246 WL_EXPORT   struct uifw_client *
2247 ico_window_mgr_get_uclient(const char *appid)
2248 {
2249     struct uifw_client *uclient;
2250
2251     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
2252         if (strcmp(uclient->appid, appid) == 0) {
2253             return uclient;
2254         }
2255     }
2256     return NULL;
2257 }
2258
2259 /*--------------------------------------------------------------------------*/
2260 /**
2261  * @brief   ico_window_mgr_get_client_usurf: get client UIFW surface table
2262  *
2263  * @param[in]   target      surface window name and application Id(winname@appid)
2264  * @return      UIFW surface table
2265  * @retval      !=NULL      success(UIFW surface table address)
2266  * @retval      = NULL      error(appid or winname not exist)
2267  */
2268 /*--------------------------------------------------------------------------*/
2269 WL_EXPORT   struct uifw_win_surface *
2270 ico_window_mgr_get_client_usurf(const char *target)
2271 {
2272     struct uifw_client      *uclient;
2273     struct uifw_win_surface *usurf;
2274     int     i, j;
2275     char    winname[ICO_IVI_WINNAME_LENGTH];
2276     char    appid[ICO_IVI_APPID_LENGTH];
2277
2278     /* get window name and application id   */
2279     j = 0;
2280     for (i = 0; target[i]; i++) {
2281         if (target[i] == '@')   {
2282             if (target[i+1] != '@') break;
2283             i ++;
2284         }
2285         if (j < (ICO_IVI_WINNAME_LENGTH-1)) {
2286             winname[j++] = target[i];
2287         }
2288     }
2289     winname[j] = 0;
2290     if (target[i] == '@')   {
2291         i ++;
2292     }
2293     else    {
2294         winname[0] = 0;
2295         i = 0;
2296     }
2297     j = 0;
2298     for ( ; target[i]; i++) {
2299         if ((target[i] == '@') && (target[i+1] == '@')) i ++;
2300         if (j < (ICO_IVI_APPID_LENGTH-1))  {
2301             appid[j++] = target[i];
2302         }
2303     }
2304     appid[j] = 0;
2305     uifw_debug("ico_window_mgr_get_client_usurf: target=<%s> appid=<%s> win=<%s>",
2306                target, appid, winname);
2307
2308     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
2309         if (strcmp(uclient->appid, appid) == 0) {
2310             wl_list_for_each (usurf, &uclient->surface_link, client_link)   {
2311                 if ((winname[0] == 0) ||
2312                     (strcmp(winname, usurf->winname) == 0)) {
2313                     return usurf;
2314                 }
2315             }
2316         }
2317     }
2318     return NULL;
2319 }
2320
2321 /*--------------------------------------------------------------------------*/
2322 /**
2323  * @brief   ico_window_mgr_set_hook_animation: set animation hook routine
2324  *
2325  * @param[in]   hook_animation  hook routine
2326  * @return      none
2327  */
2328 /*--------------------------------------------------------------------------*/
2329 WL_EXPORT   void
2330 ico_window_mgr_set_hook_animation(int (*hook_animation)(const int op, void *data))
2331 {
2332     win_mgr_hook_animation = hook_animation;
2333 }
2334
2335 /*--------------------------------------------------------------------------*/
2336 /**
2337  * @brief   ico_window_mgr_set_hook_change: set input region hook routine
2338  *
2339  * @param[in]   hook_change     hook routine
2340  * @return      none
2341  */
2342 /*--------------------------------------------------------------------------*/
2343 WL_EXPORT   void
2344 ico_window_mgr_set_hook_change(void (*hook_change)(struct uifw_win_surface *usurf))
2345 {
2346     win_mgr_hook_change = hook_change;
2347 }
2348
2349 /*--------------------------------------------------------------------------*/
2350 /**
2351  * @brief   ico_window_mgr_set_hook_destory: set input region hook routine
2352  *
2353  * @param[in]   hook_destroy    hook routine
2354  * @return      none
2355  */
2356 /*--------------------------------------------------------------------------*/
2357 WL_EXPORT   void
2358 ico_window_mgr_set_hook_destory(void (*hook_destroy)(struct uifw_win_surface *usurf))
2359 {
2360     win_mgr_hook_destory = hook_destroy;
2361 }
2362
2363 /*--------------------------------------------------------------------------*/
2364 /**
2365  * @brief   ico_window_mgr_set_hook_inputregion: set input region hook routine
2366  *
2367  * @param[in]   hook_inputregion    hook routine
2368  * @return      none
2369  */
2370 /*--------------------------------------------------------------------------*/
2371 WL_EXPORT   void
2372 ico_window_mgr_set_hook_inputregion(
2373         void (*hook_inputregion)(int set, struct uifw_win_surface *usurf,
2374                                  int32_t x, int32_t y, int32_t width,
2375                                  int32_t height, int32_t hotspot_x, int32_t hotspot_y,
2376                                  int32_t cursor_x, int32_t cursor_y, int32_t cursor_width,
2377                                  int32_t cursor_height, uint32_t attr))
2378 {
2379     win_mgr_hook_inputregion = hook_inputregion;
2380 }
2381
2382 /*--------------------------------------------------------------------------*/
2383 /**
2384  * @brief   module_init: initialize ico_window_mgr
2385  *                       this function called from ico_pluign_loader
2386  *
2387  * @param[in]   es          weston compositor
2388  * @param[in]   argc        number of arguments(unused)
2389  * @param[in]   argv        argument list(unused)
2390  * @return      result
2391  * @retval      0           sccess
2392  * @retval      -1          error
2393  */
2394 /*--------------------------------------------------------------------------*/
2395 WL_EXPORT   int
2396 module_init(struct weston_compositor *ec, int *argc, char *argv[])
2397 {
2398     int     nodeId;
2399     int     i;
2400     int     idx;
2401     struct weston_output *output;
2402     struct weston_config_section *section;
2403     char    *displayno = NULL;
2404     char    *p;
2405     struct wl_event_loop *loop;
2406
2407     uifw_info("ico_window_mgr: Enter(module_init)");
2408
2409     /* get ivi debug level                      */
2410     section = weston_config_get_section(ec->config, "ivi-option", NULL, NULL);
2411     if (section)    {
2412         weston_config_section_get_int(section, "flag", &_ico_ivi_option_flag, 0);
2413         weston_config_section_get_int(section, "log", &_ico_ivi_debug_level, 3);
2414     }
2415
2416     /* get display number                       */
2417     section = weston_config_get_section(ec->config, "ivi-display", NULL, NULL);
2418     if (section)    {
2419         weston_config_section_get_string(section, "displayno", &displayno, NULL);
2420     }
2421
2422     /* get animation default                    */
2423     section = weston_config_get_section(ec->config, "ivi-animation", NULL, NULL);
2424     if (section)    {
2425         weston_config_section_get_string(section, "default", &_ico_ivi_animation_name, NULL);
2426         weston_config_section_get_int(section, "time", &_ico_ivi_animation_time, 500);
2427         weston_config_section_get_int(section, "fps", &_ico_ivi_animation_fps, 30);
2428     }
2429     if (_ico_ivi_animation_name == NULL)
2430         _ico_ivi_animation_name = (char *)"fade";
2431     if (_ico_ivi_animation_time < 100)  _ico_ivi_animation_time = 500;
2432     if (_ico_ivi_animation_fps < 3)     _ico_ivi_animation_fps = 30;
2433
2434     /* create ico_window_mgr management table   */
2435     _ico_win_mgr = (struct ico_win_mgr *)malloc(sizeof(struct ico_win_mgr));
2436     if (_ico_win_mgr == NULL)   {
2437         uifw_error("ico_window_mgr: malloc failed");
2438         return -1;
2439     }
2440
2441     memset(_ico_win_mgr, 0, sizeof(struct ico_win_mgr));
2442
2443     _ico_win_mgr->compositor = ec;
2444
2445     uifw_trace("ico_window_mgr: wl_global_create(bind_ico_win_mgr)");
2446     if (wl_global_create(ec->wl_display, &ico_window_mgr_interface, 1,
2447                          _ico_win_mgr, bind_ico_win_mgr) == NULL)  {
2448         uifw_error("ico_window_mgr: Error(wl_global_create)");
2449         return -1;
2450     }
2451
2452     wl_list_init(&_ico_win_mgr->client_list);
2453     wl_list_init(&_ico_win_mgr->manager_list);
2454     wl_list_init(&_ico_win_mgr->map_list);
2455     _ico_win_mgr->free_maptable = NULL;
2456
2457     /* create display list                  */
2458     if (displayno != NULL)   {
2459         p = displayno;
2460     }
2461     else    {
2462         p = NULL;
2463     }
2464     _ico_num_nodes = 0;
2465     wl_list_for_each (output, &ec->output_list, link) {
2466         wl_list_init(&_ico_win_mgr->map_animation[_ico_num_nodes].link);
2467         _ico_win_mgr->map_animation[_ico_num_nodes].frame = win_mgr_check_mapsurface;
2468         wl_list_insert(output->animation_list.prev,
2469                        &_ico_win_mgr->map_animation[_ico_num_nodes].link);
2470         _ico_num_nodes++;
2471         if (_ico_num_nodes >= ICO_IVI_MAX_DISPLAY)   break;
2472     }
2473     memset(&_ico_node_table[0], 0, sizeof(_ico_node_table));
2474     i = 0;
2475     wl_list_for_each (output, &ec->output_list, link) {
2476         p = strtok(p, ",");
2477         if (p)  {
2478             idx = strtol(p, (char **)0, 0);
2479             uifw_trace("ico_window_mgr: config Display.%d is weston display.%d", i, idx);
2480             p = NULL;
2481             if ((idx < 0) || (idx >= _ico_num_nodes))   {
2482                 idx = i;
2483             }
2484         }
2485         else    {
2486             idx = i;
2487         }
2488         if (_ico_node_table[idx].node)  {
2489             for (idx = 0; idx < _ico_num_nodes; idx++)  {
2490                 if (_ico_node_table[idx].node == 0) break;
2491             }
2492             if (idx >= _ico_num_nodes)  {
2493                 uifw_error("ico_window_mgr: number of display overflow");
2494                 idx = 0;
2495             }
2496         }
2497         _ico_node_table[idx].node = idx + 0x100;
2498         _ico_node_table[idx].displayno = i;
2499         _ico_node_table[idx].output = output;
2500         _ico_node_table[idx].disp_x = output->x;
2501         _ico_node_table[idx].disp_y = output->y;
2502         _ico_node_table[idx].disp_width = output->width;
2503         _ico_node_table[idx].disp_height = output->height;
2504         i ++;
2505         if (i >= _ico_num_nodes) break;
2506     }
2507     idx = 0;
2508     for (i = 0; i < _ico_num_nodes; i++)    {
2509         _ico_node_table[i].node &= 0x0ff;
2510         uifw_info("ico_window_mgr: Display.%d no=%d x/y=%d/%d w/h=%d/%d",
2511                   i, _ico_node_table[i].displayno,
2512                   _ico_node_table[i].disp_x, _ico_node_table[i].disp_y,
2513                   _ico_node_table[i].disp_width, _ico_node_table[i].disp_height);
2514     }
2515     if (displayno)  free(displayno);
2516
2517     /* my node Id ... this version fixed 0  */
2518     nodeId = ico_ivi_get_mynode();
2519
2520     _ico_win_mgr->surface_head = ICO_IVI_SURFACEID_BASE(nodeId);
2521     uifw_trace("ico_window_mgr: NoedId=%04x SurfaceIdBase=%08x",
2522                 nodeId, _ico_win_mgr->surface_head);
2523
2524     /* get seat for touch down counter check    */
2525     touch_check_seat = container_of(ec->seat_list.next, struct weston_seat, link);
2526     loop = wl_display_get_event_loop(ec->wl_display);
2527     _ico_win_mgr->wait_mapevent =
2528             wl_event_loop_add_timer(loop, win_mgr_timer_mapsurface, NULL);
2529     wl_event_source_timer_update(_ico_win_mgr->wait_mapevent, 1000);
2530
2531     uifw_info("ico_window_mgr: animation name=%s time=%d fps=%d",
2532               _ico_ivi_animation_name, _ico_ivi_animation_time, _ico_ivi_animation_fps);
2533     uifw_info("ico_window_mgr: option flag=0x%04x log level=%d debug flag=0x%04x",
2534               _ico_ivi_option_flag, _ico_ivi_debug_level & 0x0ffff,
2535               (_ico_ivi_debug_level >> 16) & 0x0ffff);
2536
2537     /* touch/click binding for select surface           */
2538     weston_compositor_add_button_binding(ec, BTN_LEFT, 0, win_mgr_click_to_activate, NULL);
2539     weston_compositor_add_touch_binding(ec, 0, win_mgr_touch_to_activate, NULL);
2540
2541     /* set Notification function for GENIVI ivi-shell   */
2542     if (weston_layout_setNotificationCreateSurface(ico_ivi_surfaceCreateNotification, NULL) != 0)   {
2543         uifw_error("ico_window_mgr: weston_layout_setNotificationCreateSurface Error");
2544     }
2545     if (weston_layout_setNotificationRemoveSurface(ico_ivi_surfaceRemoveNotification, NULL) != 0)   {
2546         uifw_error("ico_window_mgr: weston_layout_setNotificationRemoveSurface Error");
2547     }
2548     if (weston_layout_setNotificationConfigureSurface(ico_ivi_surfaceConfigureNotification, NULL) != 0) {
2549         uifw_error("ico_window_mgr: weston_layout_setNotificationConfigureSurface Error");
2550     }
2551     uifw_info("ico_window_mgr: Leave(module_init)");
2552
2553     return 0;
2554 }