Support Tizen 3.0
[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 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    Jul-26-2013
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/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <wayland-server.h>
46 #include <aul/aul.h>
47 #include <bundle.h>
48
49 #include <weston/compositor.h>
50 #include "ico_ivi_common.h"
51 #include "ico_ivi_shell.h"
52 #include "ico_window_mgr.h"
53 #include "desktop-shell-server-protocol.h"
54 #include "ico_window_mgr-server-protocol.h"
55
56 /* SurfaceID                            */
57 #define INIT_SURFACE_IDS    1024            /* SurfaceId table initiale size        */
58 #define ADD_SURFACE_IDS     512             /* SurfaceId table additional size      */
59 #define SURCAFE_ID_MASK     0x0ffff         /* SurfaceId bit mask pattern           */
60 #define UIFW_HASH    64                     /* Hash value (2's compliment)          */
61
62 /* Client attribute table               */
63 #define MAX_CLIENT_ATTR     4
64 struct uifw_client_attr {
65     char    appid[ICO_IVI_APPID_LENGTH];    /* ApplicationId                        */
66     struct _uifw_client_attr_value {
67         short   attr;
68         short   res;
69         int     value;
70     }       attrs[MAX_CLIENT_ATTR];
71     struct wl_list  link;
72 };
73
74 /* Manager table                        */
75 struct uifw_manager {
76     struct wl_resource *resource;           /* Manager resource                     */
77     int     manager;                        /* Manager(=event send flag)            */
78     struct wl_list link;                    /* link to next manager                 */
79 };
80
81 /* Multi Windiw Manager                 */
82 struct ico_win_mgr {
83     struct weston_compositor *compositor;   /* Weston compositor                    */
84     void    *shell;                         /* shell(ico_ivi_shell) table address   */
85     int32_t surface_head;                   /* (HostID << 24) | (DisplayNo << 16)   */
86
87     struct wl_list  client_list;            /* Clients                              */
88     struct wl_list  manager_list;           /* Manager(ex.HomeScreen) list          */
89     int             num_manager;            /* Number of managers                   */
90     struct wl_list  ivi_layer_list;         /* Layer management table list          */
91     struct uifw_win_surface *active_pointer_usurf;  /* Active Pointer Surface       */
92     struct uifw_win_surface *active_keyboard_usurf; /* Active Keyboard Surface      */
93
94     struct uifw_win_surface *idhash[UIFW_HASH];  /* UIFW SerfaceID                  */
95     struct uifw_win_surface *wshash[UIFW_HASH];  /* Weston Surface                  */
96
97     uint32_t surfaceid_count;               /* Number of surface id                 */
98     uint32_t surfaceid_max;                 /* Maximum number of surface id         */
99     uint16_t *surfaceid_map;                /* SurfaceId assign bit map             */
100
101     char    shell_init;                     /* shell initialize flag                */
102     char    res;                            /* (unused)                             */
103 };
104
105 /* Internal macros                      */
106 /* UIFW SurfaceID                       */
107 #define MAKE_IDHASH(v)  (((uint32_t)v) & (UIFW_HASH-1))
108 /* Weston Surface                       */
109 #define MAKE_WSHASH(v)  ((((uint32_t)v) >> 5) & (UIFW_HASH-1))
110
111 /* function prototype                   */
112                                             /* get surface table from surfece id    */
113 static struct uifw_win_surface* find_uifw_win_surface_by_id(uint32_t surfaceid);
114                                             /* get surface table from weston surface*/
115 static struct uifw_win_surface* find_uifw_win_surface_by_ws(
116                     struct weston_surface *wsurf);
117                                             /* get client table from weston client  */
118 static struct uifw_client* find_client_from_client(struct wl_client* client);
119                                             /* assign new surface id                */
120 static uint32_t generate_id(void);
121                                             /* bind shell client                    */
122 static void win_mgr_bind_client(struct wl_client *client, void *shell);
123                                             /* unind shell client                   */
124 static void win_mgr_unbind_client(struct wl_client *client);
125                                             /* create new surface                   */
126 static void win_mgr_register_surface(
127                     struct wl_client *client, struct wl_resource *resource,
128                     struct weston_surface *surface, struct shell_surface *shsurf);
129                                             /* surface destory                      */
130 static void win_mgr_destroy_surface(struct weston_surface *surface);
131                                             /* map new surface                      */
132 static void win_mgr_map_surface(struct weston_surface *surface, int32_t *width,
133                                 int32_t *height, int32_t *sx, int32_t *sy);
134                                             /* send surface change event to manager */
135 static void win_mgr_change_surface(struct weston_surface *surface,
136                                    const int to, const int manager);
137                                             /* surface select                       */
138 static void win_mgr_select_surface(struct weston_surface *surface);
139                                             /* surface set title                    */
140 static void win_mgr_set_title(struct weston_surface *surface, const char *title);
141                                             /* surface move request from shell      */
142 static void win_mgr_surface_move(struct weston_surface *surface, int *dx, int *dy);
143                                             /* set raise                            */
144 static void win_mgr_set_raise(struct uifw_win_surface *usurf, const int raise);
145                                             /* surface change from manager          */
146 static int win_mgr_surface_change_mgr(struct weston_surface *surface, const int x,
147                                       const int y, const int width, const int height);
148                                             /* restack surface list                 */
149 static void win_mgr_restack_ivi_layer(struct uifw_win_surface *usurf);
150                                             /* create new layer                     */
151 static struct uifw_win_layer *win_mgr_create_layer(struct uifw_win_surface *usurf,
152                                                    const uint32_t layer);
153                                             /* set surface layer                    */
154 static void win_mgr_set_layer(struct uifw_win_surface *usurf, const uint32_t layer);
155                                             /* change weston surface                */
156 static void win_mgr_set_weston_surface(struct uifw_win_surface *usurf);
157                                             /* set active surface                   */
158 static void win_mgr_set_active(struct uifw_win_surface *usurf, const int target);
159
160                                             /* declare manager                      */
161 static void uifw_declare_manager(struct wl_client *client, struct wl_resource *resource,
162                                  int manager);
163                                             /* create layer and set attribute       */
164 static void uifw_create_layer(struct wl_client *client, struct wl_resource *resource,
165                               uint32_t layer, int32_t attribute);
166                                             /* set window layer                     */
167 static void uifw_set_window_layer(struct wl_client *client,
168                                   struct wl_resource *resource,
169                                   uint32_t surfaceid, uint32_t layer);
170                                             /* set surface size and position        */
171 static void uifw_set_positionsize(struct wl_client *client, struct wl_resource *resource,
172                                   uint32_t surfaceid, uint32_t node, int32_t x, int32_t y,
173                                   int32_t width, int32_t height, int32_t animation);
174                                             /* show/hide and raise/lower surface    */
175 static void uifw_set_visible(struct wl_client *client, struct wl_resource *resource,
176                              uint32_t surfaceid, int32_t visible, int32_t raise,
177                              int32_t animation);
178                                             /* set surface animation                */
179 static void uifw_set_animation(struct wl_client *client, struct wl_resource *resource,
180                                uint32_t surfaceid, int32_t type,
181                                const char *animation, int32_t time);
182                                             /* set active surface (form HomeScreen) */
183 static void uifw_set_active(struct wl_client *client, struct wl_resource *resource,
184                             uint32_t surfaceid, int32_t active);
185                                             /* layer visibility control             */
186 static void uifw_set_layer_visible(struct wl_client *client, struct wl_resource *resource,
187                                    uint32_t layer, int32_t visible);
188                                             /* get application surfaces             */
189 static void uifw_get_surfaces(struct wl_client *client, struct wl_resource *resource,
190                               const char *appid);
191                                             /* map surface buffer to shared memory  */
192 static void uifw_map_surface(struct wl_client *client, struct wl_resource *resource,
193                              uint32_t surfaceid, const char *mapname, int32_t framerate);
194                                             /* unmap surface buffer                 */
195 static void uifw_unmap_surface(struct wl_client *client, struct wl_resource *resource,
196                                uint32_t surfaceid);
197                                             /* bind manager                         */
198 static void bind_ico_win_mgr(struct wl_client *client,
199                              void *data, uint32_t version, uint32_t id);
200                                             /* unbind manager                       */
201 static void unbind_ico_win_mgr(struct wl_resource *resource);
202                                             /* send event to manager                */
203 static int ico_win_mgr_send_to_mgr(const int event, struct uifw_win_surface *usurf,
204                                    const int param1, const int param2, const int param3,
205                                    const int param4, const int param5);
206                                             /* convert animation name to Id value   */
207 static int ico_get_animation_name(const char *animation);
208                                             /* hook for animation                   */
209 static int  (*win_mgr_hook_animation)(const int op, void *data) = NULL;
210
211 /* static tables                        */
212 /* Multi Window Manager interface       */
213 static const struct ico_window_mgr_interface ico_window_mgr_implementation = {
214     uifw_declare_manager,
215     uifw_create_layer,
216     uifw_set_window_layer,
217     uifw_set_positionsize,
218     uifw_set_visible,
219     uifw_set_animation,
220     uifw_set_active,
221     uifw_set_layer_visible,
222     uifw_get_surfaces,
223     uifw_map_surface,
224     uifw_unmap_surface
225 };
226
227 /* plugin common value(without ico_plugin_loader)   */
228 static int  _ico_ivi_debug_flag = 0;            /* Debug flags                      */
229 static int  _ico_ivi_debug_level = 3;           /* Debug Level                      */
230 static char *_ico_ivi_animation_name = NULL;    /* Default animation name           */
231 static int  _ico_ivi_animation_time = 500;      /* Default animation time           */
232 static int  _ico_ivi_animation_fps = 15;        /* Animation frame rate             */
233
234 /* static management table              */
235 static struct ico_win_mgr       *_ico_win_mgr = NULL;
236 static int                      _ico_num_nodes = 0;
237 static struct uifw_node_table   _ico_node_table[ICO_IVI_MAX_DISPLAY];
238
239
240 /*--------------------------------------------------------------------------*/
241 /**
242  * @brief   ico_ivi_debugflag: get debug flags
243  *
244  * @param       None
245  * @return      debug flags
246  */
247 /*--------------------------------------------------------------------------*/
248 WL_EXPORT   int
249 ico_ivi_debugflag(void)
250 {
251     return _ico_ivi_debug_flag;
252 }
253
254 /*--------------------------------------------------------------------------*/
255 /**
256  * @brief   ico_ivi_debuglevel: answer debug output level.
257  *
258  * @param       none
259  * @return      debug output level
260  * @retval      0       No debug output
261  * @retval      1       Only error output
262  * @retval      2       Error and Warning output
263  * @retval      3       Error, Warning and information output
264  * @retval      4       All output with debug write
265  */
266 /*--------------------------------------------------------------------------*/
267 WL_EXPORT   int
268 ico_ivi_debuglevel(void)
269 {
270     return _ico_ivi_debug_level;
271 }
272
273 /*--------------------------------------------------------------------------*/
274 /**
275  * @brief   ico_ivi_default_animation_name: get default animation name
276  *
277  * @param       None
278  * @return      Default animation name
279  */
280 /*--------------------------------------------------------------------------*/
281 WL_EXPORT   const char *
282 ico_ivi_default_animation_name(void)
283 {
284     return _ico_ivi_animation_name;
285 }
286
287 /*--------------------------------------------------------------------------*/
288 /**
289  * @brief   ico_ivi_default_animation_time: get default animation time
290  *
291  * @param       None
292  * @return      Default animation time(miri sec)
293  */
294 /*--------------------------------------------------------------------------*/
295 WL_EXPORT   int
296 ico_ivi_default_animation_time(void)
297 {
298     return _ico_ivi_animation_time;
299 }
300
301 /*--------------------------------------------------------------------------*/
302 /**
303  * @brief   ico_ivi_default_animation_fps: get default animation frame rate
304  *
305  * @param       None
306  * @return      Default animation frame rate(frames/sec)
307  */
308 /*--------------------------------------------------------------------------*/
309 WL_EXPORT   int
310 ico_ivi_default_animation_fps(void)
311 {
312     return _ico_ivi_animation_fps;
313 }
314
315 /*--------------------------------------------------------------------------*/
316 /**
317  * @brief   ico_ivi_get_mynode: Get my NodeId
318  *
319  * @param       None
320  * @return      NodeId of my node
321  */
322 /*--------------------------------------------------------------------------*/
323 WL_EXPORT   int
324 ico_ivi_get_mynode(void)
325 {
326     /* Reference Platform 0.90 only support 1 ECU   */
327     return 0;
328 }
329
330 /*--------------------------------------------------------------------------*/
331 /**
332  * @brief   find_uifw_win_surface_by_id: find UIFW surface by surface id
333  *
334  * @param[in]   surfaceid   UIFW surface id
335  * @return      UIFW surface table address
336  * @retval      !=NULL      success(surface table address)
337  * @retval      NULL        error(surface id dose not exist)
338  */
339 /*--------------------------------------------------------------------------*/
340 static struct uifw_win_surface*
341 find_uifw_win_surface_by_id(uint32_t surfaceid)
342 {
343     struct uifw_win_surface* usurf;
344
345     usurf = _ico_win_mgr->idhash[MAKE_IDHASH(surfaceid)];
346
347     while (usurf)   {
348         if (usurf->surfaceid == surfaceid) {
349             return usurf;
350         }
351         usurf = usurf->next_idhash;
352     }
353     uifw_trace("find_uifw_win_surface_by_id: NULL");
354     return NULL;
355 }
356
357 /*--------------------------------------------------------------------------*/
358 /**
359  * @brief   find_uifw_win_surface_by_ws: find UIFW surface by weston surface
360  *
361  * @param[in]   wsurf       Weston surface
362  * @return      UIFW surface table address
363  * @retval      !=NULL      success(surface table address)
364  * @retval      NULL        error(surface dose not exist)
365  */
366 /*--------------------------------------------------------------------------*/
367 static struct uifw_win_surface*
368 find_uifw_win_surface_by_ws(struct weston_surface *wsurf)
369 {
370     struct uifw_win_surface* usurf;
371
372     usurf = _ico_win_mgr->wshash[MAKE_WSHASH(wsurf)];
373
374     while (usurf)   {
375         if (usurf->surface == wsurf) {
376             return usurf;
377         }
378         usurf = usurf->next_wshash;
379     }
380     uifw_trace("find_uifw_win_surface_by_ws: NULL");
381     return NULL;
382 }
383
384 /*--------------------------------------------------------------------------*/
385 /**
386  * @brief   find_client_from_client: find UIFW client by wayland client
387  *
388  * @param[in]   client      Wayland client
389  * @return      UIFW client table address
390  * @retval      !=NULL      success(client table address)
391  * @retval      NULL        error(client dose not exist)
392  */
393 /*--------------------------------------------------------------------------*/
394 static struct uifw_client*
395 find_client_from_client(struct wl_client* client)
396 {
397     struct uifw_client  *uclient;
398
399     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
400         if (uclient->client == client)  {
401             return(uclient);
402         }
403     }
404     uifw_trace("find_client_from_client: client.%08x is NULL", (int)client);
405     return NULL;
406 }
407
408 /*--------------------------------------------------------------------------*/
409 /**
410  * @brief   ico_window_mgr_get_appid: find application id by wayland client
411  *
412  * @param[in]   client      Wayland client
413  * @return      application id
414  * @retval      !=NULL      success(application id)
415  * @retval      NULL        error(client dose not exist)
416  */
417 /*--------------------------------------------------------------------------*/
418 WL_EXPORT   char *
419 ico_window_mgr_get_appid(struct wl_client* client)
420 {
421     struct uifw_client  *uclient;
422
423     uclient = find_client_from_client(client);
424
425     if (! uclient)  {
426         return NULL;
427     }
428     return uclient->appid;
429 }
430
431 /*--------------------------------------------------------------------------*/
432 /**
433  * @brief   generate_id: generate uniq id for UIFW surface id
434  *
435  * @param       none
436  * @return      uniq id for UIFW surface id
437  */
438 /*--------------------------------------------------------------------------*/
439 static uint32_t
440 generate_id(void)
441 {
442     int     rep;
443     int     i;
444     int     map;
445     uint16_t *new_map;
446     uint32_t surfaceId;
447
448     /* next assign id                           */
449     _ico_win_mgr->surfaceid_count ++;
450
451     /* serach free id from bitmap               */
452     for (rep = 0; rep < (int)(_ico_win_mgr->surfaceid_max/16); rep++)   {
453         if (_ico_win_mgr->surfaceid_count >= _ico_win_mgr->surfaceid_max)   {
454             _ico_win_mgr->surfaceid_count = 0;
455         }
456         if (_ico_win_mgr->surfaceid_map[_ico_win_mgr->surfaceid_count/16] != 0xffff)    {
457             /* find free id from bitmap         */
458             map = 1 << (_ico_win_mgr->surfaceid_count % 16);
459             for (i = (_ico_win_mgr->surfaceid_count % 16); i < 16; i++) {
460                 if ((_ico_win_mgr->surfaceid_map[_ico_win_mgr->surfaceid_count/16] & map)
461                         == 0) {
462                     _ico_win_mgr->surfaceid_map[_ico_win_mgr->surfaceid_count/16] |= map;
463                     _ico_win_mgr->surfaceid_count
464                         = (_ico_win_mgr->surfaceid_count/16)*16 + i;
465
466                     surfaceId = (_ico_win_mgr->surfaceid_count + 1)
467                                 | _ico_win_mgr->surface_head;
468                     uifw_trace("generate_id: SurfaceId=%08x", surfaceId);
469                     return(surfaceId);
470                 }
471                 map = map << 1;
472             }
473         }
474         _ico_win_mgr->surfaceid_count += 16;
475     }
476
477     /* no free id in bitmap, extend bitmap      */
478     if ((_ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS) > SURCAFE_ID_MASK)  {
479         /* too many surfaces, system error      */
480         uifw_trace("generate_id: SurffaceId Overflow(%d, Max=%d), Abort",
481                    _ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS, SURCAFE_ID_MASK);
482         fprintf(stderr, "generate_id: SurffaceId Overflow(%d, Max=%d), Abort\n",
483                 _ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS, SURCAFE_ID_MASK);
484         abort();
485     }
486
487     new_map = (uint16_t *) malloc((_ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS) / 8);
488     memcpy(new_map, _ico_win_mgr->surfaceid_map, _ico_win_mgr->surfaceid_max/8);
489     memset(&new_map[_ico_win_mgr->surfaceid_max/16], 0, ADD_SURFACE_IDS/8);
490     _ico_win_mgr->surfaceid_count = _ico_win_mgr->surfaceid_max;
491     new_map[_ico_win_mgr->surfaceid_count/16] |= 1;
492     _ico_win_mgr->surfaceid_max += ADD_SURFACE_IDS;
493     free(_ico_win_mgr->surfaceid_map);
494     _ico_win_mgr->surfaceid_map = new_map;
495
496     uifw_trace("generate_id: Extent SurfaceId=%d(Max.%d)",
497                _ico_win_mgr->surfaceid_count+1, _ico_win_mgr->surfaceid_max);
498     surfaceId = (_ico_win_mgr->surfaceid_count + 1) | _ico_win_mgr->surface_head;
499
500     uifw_trace("generate_id: SurfaceId=%08x", surfaceId);
501     return(surfaceId);
502 }
503
504 /*--------------------------------------------------------------------------*/
505 /**
506  * @brief   win_mgr_bind_client: desktop_shell from client
507  *
508  * @param[in]   client          Wayland client
509  * @param[in]   shell           shell(ico_ivi_shell) table address
510  * @return      none
511  */
512 /*--------------------------------------------------------------------------*/
513 static void
514 win_mgr_bind_client(struct wl_client *client, void *shell)
515 {
516     struct uifw_client  *uclient;
517     int     newclient;
518     pid_t   pid;
519     uid_t   uid;
520     gid_t   gid;
521     int     fd;
522     int     size;
523     int     i;
524     int     j;
525     char    procpath[128];
526
527     uifw_trace("win_mgr_bind_client: Enter(client=%08x, sjell=%08x)",
528                (int)client, (int)shell);
529
530     /* save shell table address             */
531     if (shell)  {
532         _ico_win_mgr->shell = shell;
533     }
534
535     /* set client                           */
536     uclient = find_client_from_client(client);
537     if (! uclient)  {
538         /* client not exist, create client management table             */
539         uifw_trace("win_mgr_bind_client: Create Client");
540         uclient = (struct uifw_client *)malloc(sizeof(struct uifw_client));
541         if (!uclient)   {
542             uifw_error("win_mgr_bind_client: Error, No Memory");
543             return;
544         }
545         memset(uclient, 0, sizeof(struct uifw_client));
546         uclient->client = client;
547         newclient = 1;
548     }
549     else    {
550         newclient = 0;
551     }
552     wl_client_get_credentials(client, &pid, &uid, &gid);
553     uifw_trace("win_mgr_bind_client: client=%08x pid=%d uid=%d gid=%d",
554                (int)client, (int)pid, (int)uid, (int)gid);
555     if (pid > 0)    {
556         uclient->pid = (int)pid;
557         /* get applicationId from AppCore(AUL)  */
558         if (aul_app_get_appid_bypid(uclient->pid, uclient->appid, ICO_IVI_APPID_LENGTH)
559                         == AUL_R_OK)    {
560             uifw_trace("win_mgr_bind_client: client=%08x pid=%d appid=<%s>",
561                        (int)client, uclient->pid, uclient->appid);
562         }
563         else    {
564             /* client dose not exist in AppCore, search Linux process table */
565             uifw_trace("win_mgr_bind_client: pid=%d dose not exist in AppCore(AUL)",
566                        uclient->pid);
567
568             memset(uclient->appid, 0, ICO_IVI_APPID_LENGTH);
569             snprintf(procpath, sizeof(procpath)-1, "/proc/%d/cmdline", uclient->pid);
570             fd = open(procpath, O_RDONLY);
571             if (fd >= 0)    {
572                 size = read(fd, procpath, sizeof(procpath));
573                 for (; size > 0; size--)    {
574                     if (procpath[size-1])   break;
575                 }
576                 if (size > 0)   {
577                     /* get program base name    */
578                     i = 0;
579                     for (j = 0; j < size; j++)  {
580                         if (procpath[j] == 0)   break;
581                         if (procpath[j] == '/') i = j + 1;
582                     }
583                     j = 0;
584                     for (; i < size; i++)   {
585                         uclient->appid[j] = procpath[i];
586                         if ((uclient->appid[j] == 0) ||
587                             (j >= (ICO_IVI_APPID_LENGTH-1)))    break;
588                         j++;
589                     }
590                     /* search application number in apprication start option    */
591                     if ((uclient->appid[j] == 0) && (j < (ICO_IVI_APPID_LENGTH-2))) {
592                         for (; i < size; i++)   {
593                             if ((procpath[i] == 0) &&
594                                 (procpath[i+1] == '@')) {
595                                 strncpy(&uclient->appid[j], &procpath[i+1],
596                                         ICO_IVI_APPID_LENGTH - j - 2);
597                             }
598                         }
599                     }
600                 }
601                 close(fd);
602             }
603             if (uclient->appid[0])  {
604                 uifw_trace("win_mgr_bind_client: client=%08x pid=%d appid=<%s> from "
605                            "Process table", (int)client, uclient->pid, uclient->appid);
606                 /* weston internal client, not manage   */
607                 if (strcmp(uclient->appid, "weston") == 0)  {
608                     newclient = -newclient;
609                 }
610             }
611             else    {
612                 uifw_trace("win_mgr_bind_client: pid=%d dose not exist in Process table",
613                            uclient->pid);
614                 sprintf(uclient->appid, "?%d?", uclient->pid);
615             }
616         }
617         if (newclient > 0)  {
618             wl_list_insert(&_ico_win_mgr->client_list, &uclient->link);
619         }
620         else if (newclient < 0) {
621             free(uclient);
622             uifw_trace("win_mgr_bind_client: client=%08x is internal, delete", (int)client);
623         }
624     }
625     else    {
626         uifw_trace("win_mgr_bind_client: client=%08x pid dose not exist", (int)client);
627     }
628     uifw_trace("win_mgr_bind_client: Leave");
629 }
630
631 /*--------------------------------------------------------------------------*/
632 /**
633  * @brief   win_mgr_unbind_client: unbind desktop_shell from client
634  *
635  * @param[in]   client          Wayland client
636  * @return      none
637  */
638 /*--------------------------------------------------------------------------*/
639 static void
640 win_mgr_unbind_client(struct wl_client *client)
641 {
642     struct uifw_client  *uclient;
643
644     uifw_trace("win_mgr_unbind_client: Enter(client=%08x)", (int)client);
645
646     uclient = find_client_from_client(client);
647     if (uclient)    {
648         /* Client exist, Destory client management table             */
649         wl_list_remove(&uclient->link);
650         free(uclient);
651     }
652     uifw_trace("win_mgr_unbind_client: Leave");
653 }
654
655 /*--------------------------------------------------------------------------*/
656 /**
657  * @brief   ico_get_animation_name: convert animation name to Id value
658  *
659  * @param[in]   animation       animation name
660  * @return      animation Id value
661  */
662 /*--------------------------------------------------------------------------*/
663 static int
664 ico_get_animation_name(const char *animation)
665 {
666     int anima = ICO_WINDOW_MGR_ANIMATION_NONE;
667
668     if (strcasecmp(animation, "none") == 0) {
669         return ICO_WINDOW_MGR_ANIMATION_NONE;
670     }
671
672     if (win_mgr_hook_animation) {
673         anima = (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_NAME, (void *)animation);
674     }
675     if (anima <= 0) {
676         anima = ICO_WINDOW_MGR_ANIMATION_NONE;
677     }
678     return anima;
679 }
680
681 /*--------------------------------------------------------------------------*/
682 /**
683  * @brief   win_mgr_register_surface: create UIFW surface
684  *
685  * @param[in]   client          Wayland client
686  * @param[in]   resource        client resource
687  * @param[in]   surface         Weston surface
688  * @param[in]   shsurf          shell surface
689  * @return      none
690  */
691 /*--------------------------------------------------------------------------*/
692 static void
693 win_mgr_register_surface(struct wl_client *client, struct wl_resource *resource,
694                          struct weston_surface *surface, struct shell_surface *shsurf)
695 {
696     struct uifw_win_surface *usurf;
697     struct uifw_win_surface *phash;
698     struct uifw_win_surface *bhash;
699     uint32_t    hash;
700
701     uifw_trace("win_mgr_register_surface: Enter(surf=%08x,client=%08x,res=%08x)",
702                (int)surface, (int)client, (int)resource);
703
704     /* check new surface                    */
705     if (find_uifw_win_surface_by_ws(surface))   {
706         /* surface exist, NOP               */
707         uifw_trace("win_mgr_register_surface: Leave(Already Exist)");
708         return;
709     }
710
711     /* set default color and shader */
712     weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
713
714     /* create UIFW surface management table */
715     usurf = malloc(sizeof(struct uifw_win_surface));
716     if (! usurf)    {
717         uifw_error("win_mgr_register_surface: No Memory");
718         return;
719     }
720
721     memset(usurf, 0, sizeof(struct uifw_win_surface));
722
723     usurf->surfaceid = generate_id();
724     usurf->surface = surface;
725     usurf->shsurf = shsurf;
726     usurf->node_tbl = &_ico_node_table[0];  /* set default node table (display no=0)    */
727     wl_list_init(&usurf->ivi_layer);
728     wl_list_init(&usurf->animation.animation.link);
729     usurf->animation.hide_name = ico_get_animation_name(ico_ivi_default_animation_name());
730     usurf->animation.hide_time = ico_ivi_default_animation_time();;
731     usurf->animation.show_name = usurf->animation.hide_name;
732     usurf->animation.show_time = usurf->animation.hide_time;
733     usurf->animation.move_name = usurf->animation.hide_name;
734     usurf->animation.move_time = usurf->animation.hide_time;
735     usurf->animation.resize_name = usurf->animation.hide_name;
736     usurf->animation.resize_time = usurf->animation.hide_time;
737
738     if ((_ico_win_mgr->num_manager <= 0) ||
739         (ico_ivi_debugflag() & ICO_IVI_DEBUG_SHOW_SURFACE)) {
740         uifw_trace("win_mgr_register_surface: No Manager, Force visible");
741         usurf->visible = 1;
742     }
743     else    {
744         uifw_trace("win_mgr_register_surface: Manager exist, Not visible");
745         usurf->visible = 0;
746     }
747
748     /* set client                           */
749     usurf->uclient = find_client_from_client(client);
750     if (! usurf->uclient)  {
751         /* client not exist, create client management table */
752         uifw_trace("win_mgr_register_surface: Create Client");
753         win_mgr_bind_client(client, NULL);
754         usurf->uclient = find_client_from_client(client);
755         if (! usurf->uclient)  {
756             uifw_error("win_mgr_register_surface: No Memory");
757             return;
758         }
759     }
760
761     /* make surface id hash table       */
762     hash = MAKE_IDHASH(usurf->surfaceid);
763     phash = _ico_win_mgr->idhash[hash];
764     bhash = NULL;
765     while (phash)   {
766         bhash = phash;
767         phash = phash->next_idhash;
768     }
769     if (bhash)  {
770         bhash->next_idhash = usurf;
771     }
772     else    {
773         _ico_win_mgr->idhash[hash] = usurf;
774     }
775
776     /* make weston surface hash table   */
777     hash = MAKE_WSHASH(usurf->surface);
778     phash = _ico_win_mgr->wshash[hash];
779     bhash = NULL;
780     while (phash)   {
781         bhash = phash;
782         phash = phash->next_wshash;
783     }
784     if (bhash)  {
785         bhash->next_wshash = usurf;
786     }
787     else    {
788         _ico_win_mgr->wshash[hash] = usurf;
789     }
790     /* set default layer id             */
791     win_mgr_set_layer(usurf, ICO_IVI_DEFAULT_LAYER);
792
793     uifw_trace("win_mgr_register_surface: Leave(surfaceId=%08x)", usurf->surfaceid);
794 }
795
796 /*--------------------------------------------------------------------------*/
797 /**
798  * @brief   win_mgr_map_surface: map surface
799  *
800  * @param[in]   surface         Weston surface
801  * @param[in]   width           surface width
802  * @param[in]   height          surface height
803  * @param[in]   sx              X coordinate on screen
804  * @param[in]   sy              Y coordinate on screen
805  * @return      none
806  */
807 /*--------------------------------------------------------------------------*/
808 static void
809 win_mgr_map_surface(struct weston_surface *surface, int32_t *width, int32_t *height,
810                     int32_t *sx, int32_t *sy)
811 {
812     struct uifw_win_surface *usurf;
813
814     uifw_trace("win_mgr_map_surface: Enter(%08x, x/y=%d/%d w/h=%d/%d)",
815                (int)surface, *sx, *sy, *width, *height);
816
817     usurf = find_uifw_win_surface_by_ws(surface);
818
819     if (usurf) {
820         uifw_trace("win_mgr_map_surface: surf=%08x w/h=%d/%d vis=%d",
821                    usurf->surfaceid, usurf->width, usurf->height, usurf->visible);
822         if ((usurf->width > 0) && (usurf->height > 0)) {
823             uifw_trace("win_mgr_map_surface: HomeScreen registed, PositionSize"
824                        "(surf=%08x x/y=%d/%d w/h=%d/%d vis=%d",
825                        usurf->surfaceid, usurf->x, usurf->y, usurf->width, usurf->height,
826                        usurf->visible);
827             *width = usurf->width;
828             *height = usurf->height;
829             weston_surface_configure(surface, usurf->node_tbl->disp_x + usurf->x,
830                                      usurf->node_tbl->disp_y + usurf->y,
831                                      usurf->width, usurf->height);
832         }
833         else    {
834             uifw_trace("win_mgr_map_surface: HomeScreen not regist Surface, "
835                        "Change PositionSize(surf=%08x x/y=%d/%d w/h=%d/%d)",
836                        usurf->surfaceid, *sx, *sy, *width, *height);
837             usurf->width = *width;
838             usurf->height = *height;
839             usurf->x = *sx;
840             usurf->y = *sy;
841             if (usurf->x < 0)   usurf->x = 0;
842             if (usurf->y < 0)   usurf->y = 0;
843
844             if (_ico_win_mgr->num_manager > 0)  {
845                 /* HomeScreen exist, coodinate set by HomeScreen                */
846                 if (usurf->visible) {
847                     weston_surface_configure(surface, usurf->node_tbl->disp_x + usurf->x,
848                                              usurf->node_tbl->disp_y + usurf->y,
849                                              usurf->width, usurf->height);
850                 }
851                 else    {
852                     weston_surface_configure(surface, ICO_IVI_MAX_COORDINATE+1,
853                                              ICO_IVI_MAX_COORDINATE+1,
854                                              usurf->width, usurf->height);
855                 }
856                 uifw_trace("win_mgr_map_surface: Change size/position x/y=%d/%d w/h=%d/%d",
857                            (int)surface->geometry.x, (int)surface->geometry.y,
858                            surface->geometry.width, surface->geometry.height);
859             }
860             if ((_ico_win_mgr->num_manager <= 0) ||
861                 (ico_ivi_debugflag() & ICO_IVI_DEBUG_SHOW_SURFACE)) {
862                 uifw_trace("win_mgr_map_surface: Np HomeScreen, chaneg to Visible");
863                 ico_window_mgr_set_visible(usurf, 1);
864             }
865         }
866         usurf->mapped = 1;
867         if (usurf->visible) {
868             win_mgr_restack_ivi_layer(NULL);
869         }
870         uifw_trace("win_mgr_map_surface: Leave");
871     }
872     else    {
873         uifw_trace("win_mgr_map_surface: Leave(No UIFW Surface)");
874     }
875 }
876
877 /*--------------------------------------------------------------------------*/
878 /**
879  * @brief   win_mgr_restack_ivi_layer: restack surface list
880  *
881  * @param[in]   usurf           UIFW surface
882  * @return      none
883  */
884 /*--------------------------------------------------------------------------*/
885 static void
886 win_mgr_restack_ivi_layer(struct uifw_win_surface *usurf)
887 {
888     struct uifw_win_surface  *es;
889     struct uifw_win_layer *el;
890     int32_t buf_width, buf_height;
891     float   new_x, new_y;
892     struct weston_layer *wlayer;
893     int     num_visible = 0;
894
895     uifw_trace("win_mgr_restack_ivi_layer: Enter(surf=%08x)", (int)usurf);
896
897     /* make compositor surface list     */
898     wlayer = ico_ivi_shell_current_layer();
899
900     wl_list_init(&wlayer->surface_list);
901     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
902         wl_list_for_each (es, &el->surface_list, ivi_layer) {
903             if ((es->mapped != 0) && (es->surface != NULL))    {
904                 if ((el->visible == FALSE) || (es->visible == FALSE))   {
905                     new_x = (float)(ICO_IVI_MAX_COORDINATE+1);
906                     new_y = (float)(ICO_IVI_MAX_COORDINATE+1);
907                 }
908                 else if (es->surface->buffer_ref.buffer)   {
909                     buf_width = weston_surface_buffer_width(es->surface);
910                     buf_height = weston_surface_buffer_height(es->surface);
911                     if (es->width > buf_width) {
912                         new_x = (float)(es->x +
913                                 (es->width - es->surface->geometry.width)/2);
914                     }
915                     else    {
916                         new_x = (float)es->x;
917                     }
918                     if (es->height > buf_height) {
919                         new_y = (float) (es->y +
920                                 (es->height - es->surface->geometry.height)/2);
921                     }
922                     else    {
923                         new_y = (float)es->y;
924                     }
925                     new_x += es->node_tbl->disp_x;
926                     new_y += es->node_tbl->disp_y;
927                     num_visible ++;
928                 }
929                 else    {
930                     new_x = (float)(ICO_IVI_MAX_COORDINATE+1);
931                     new_y = (float)(ICO_IVI_MAX_COORDINATE+1);
932                 }
933                 wl_list_insert(wlayer->surface_list.prev, &es->surface->layer_link);
934                 if ((new_x != es->surface->geometry.x) ||
935                     (new_y != es->surface->geometry.y)) {
936                     weston_surface_damage_below(es->surface);
937                     weston_surface_set_position(es->surface, (float)new_x, (float)new_y);
938                     weston_surface_damage(es->surface);
939                 }
940                 uifw_trace("win_mgr_restack_ivi_layer: %08x x/y=%d/%d w/h=%d/%d",
941                            es->surfaceid,
942                            (int)es->surface->geometry.x, (int)es->surface->geometry.y,
943                            es->surface->geometry.width, es->surface->geometry.height);
944             }
945         }
946     }
947
948     /* damage(redraw) target surfacem if target exist   */
949     if (usurf) {
950         weston_surface_damage(usurf->surface);
951     }
952
953     /* composit and draw screen(plane)  */
954     weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
955
956     if ((_ico_win_mgr->shell_init == 0) && (num_visible > 0) &&
957         (_ico_win_mgr->shell != NULL) && (_ico_win_mgr->num_manager > 0))   {
958         /* start shell fade         */
959         _ico_win_mgr->shell_init = 1;
960         ico_ivi_shell_startup(_ico_win_mgr->shell);
961     }
962     uifw_trace("win_mgr_restack_ivi_layer: Leave");
963 }
964
965 /*--------------------------------------------------------------------------*/
966 /**
967  * @brief   win_mgr_create_layer: create new layer
968  *
969  * @param[in]   usurf       UIFW surface, (if need)
970  * @param[in]   layer       layer id
971  * @return      new layer
972  * @retval      != NULL     success(layer management table)
973  * @retval      == NULL     error(No Memory)
974  */
975 /*--------------------------------------------------------------------------*/
976 static struct uifw_win_layer *
977 win_mgr_create_layer(struct uifw_win_surface *usurf, const uint32_t layer)
978 {
979     struct uifw_win_layer *el;
980     struct uifw_win_layer *new_el;
981
982     new_el = malloc(sizeof(struct uifw_win_layer));
983     if (! new_el)   {
984         uifw_trace("win_mgr_create_layer: Leave(No Memory)");
985         return NULL;
986     }
987
988     memset(new_el, 0, sizeof(struct uifw_win_layer));
989     new_el->layer = layer;
990     new_el->attribute = ICO_WINDOW_MGR_LAYER_ATTR_NORMAL;
991     new_el->visible = TRUE;
992     wl_list_init(&new_el->surface_list);
993     wl_list_init(&new_el->link);
994
995     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
996         if (layer >= el->layer) break;
997     }
998     if (&el->link == &_ico_win_mgr->ivi_layer_list)    {
999         wl_list_insert(_ico_win_mgr->ivi_layer_list.prev, &new_el->link);
1000     }
1001     else    {
1002         wl_list_insert(el->link.prev, &new_el->link);
1003     }
1004
1005     if (usurf)  {
1006         wl_list_remove(&usurf->ivi_layer);
1007         wl_list_insert(&new_el->surface_list, &usurf->ivi_layer);
1008         usurf->win_layer = new_el;
1009     }
1010     return new_el;
1011 }
1012
1013 /*--------------------------------------------------------------------------*/
1014 /**
1015  * @brief   win_mgr_set_layer: set(or change) surface layer
1016  *
1017  * @param[in]   usurf       UIFW surface
1018  * @param[in]   layer       layer id
1019  * @return      none
1020  */
1021 /*--------------------------------------------------------------------------*/
1022 static void
1023 win_mgr_set_layer(struct uifw_win_surface *usurf, const uint32_t layer)
1024 {
1025     struct uifw_win_layer *el;
1026     struct uifw_win_layer *new_el;
1027
1028     uifw_trace("win_mgr_set_layer: Enter([%08x],%08x,%x)",
1029                (int)usurf, (int)usurf->surface, layer);
1030
1031     /* check if same layer                      */
1032     if ((usurf->win_layer != NULL) && (usurf->win_layer->layer == layer))   {
1033         uifw_trace("win_mgr_set_layer: Leave(Same Layer)");
1034         return;
1035     }
1036
1037     /* search existing layer                    */
1038     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
1039         if (el->layer == layer) break;
1040     }
1041
1042     if (&el->link == &_ico_win_mgr->ivi_layer_list)    {
1043         /* layer not exist, create new layer    */
1044         uifw_trace("win_mgr_set_layer: New Layer %d", layer);
1045         new_el = win_mgr_create_layer(usurf, layer);
1046         if (! new_el)   {
1047             uifw_trace("win_mgr_set_layer: Leave(No Memory)");
1048             return;
1049         }
1050     }
1051     else    {
1052         uifw_trace("win_mgr_set_layer: Add surface to Layer %d", layer);
1053         wl_list_remove(&usurf->ivi_layer);
1054         wl_list_insert(&el->surface_list, &usurf->ivi_layer);
1055         usurf->win_layer = el;
1056     }
1057
1058     /* rebild compositor surface list       */
1059     if (usurf->visible) {
1060         win_mgr_restack_ivi_layer(usurf);
1061     }
1062     uifw_trace("win_mgr_set_layer: Leave");
1063 }
1064
1065 /*--------------------------------------------------------------------------*/
1066 /**
1067  * @brief   win_mgr_set_active: set(or change) active surface
1068  *
1069  * @param[in]   usurf       UIFW surface
1070  * @param[in]   target      target device
1071  * @return      none
1072  */
1073 /*--------------------------------------------------------------------------*/
1074 static void
1075 win_mgr_set_active(struct uifw_win_surface *usurf, const int target)
1076 {
1077     struct weston_seat *seat;
1078     struct weston_surface *surface;
1079     int object = target;
1080     wl_fixed_t sx, sy;
1081
1082     uifw_trace("win_mgr_set_active: Enter(%08x,%x)", (int)usurf, target);
1083
1084     if ((usurf) && (usurf->shsurf) && (usurf->surface)) {
1085         surface = usurf->surface;
1086         if ((target & (ICO_WINDOW_MGR_ACTIVE_POINTER|ICO_WINDOW_MGR_ACTIVE_KEYBOARD)) == 0) {
1087             surface = NULL;
1088             if (_ico_win_mgr->active_pointer_usurf == usurf) {
1089                 object |= ICO_WINDOW_MGR_ACTIVE_POINTER;
1090             }
1091             if (_ico_win_mgr->active_keyboard_usurf == usurf)    {
1092                 object |= ICO_WINDOW_MGR_ACTIVE_KEYBOARD;
1093             }
1094         }
1095         else    {
1096             if (object & ICO_WINDOW_MGR_ACTIVE_POINTER) {
1097                 _ico_win_mgr->active_pointer_usurf = usurf;
1098             }
1099             if (object & ICO_WINDOW_MGR_ACTIVE_KEYBOARD)    {
1100                 _ico_win_mgr->active_keyboard_usurf = usurf;
1101             }
1102         }
1103     }
1104     else    {
1105         surface = NULL;
1106         if (target == 0)    {
1107             object = ICO_WINDOW_MGR_ACTIVE_POINTER | ICO_WINDOW_MGR_ACTIVE_KEYBOARD;
1108         }
1109         if (object & ICO_WINDOW_MGR_ACTIVE_POINTER) {
1110             _ico_win_mgr->active_pointer_usurf = NULL;
1111         }
1112         if (object & ICO_WINDOW_MGR_ACTIVE_KEYBOARD)    {
1113             _ico_win_mgr->active_keyboard_usurf = NULL;
1114         }
1115     }
1116
1117     wl_list_for_each(seat, &_ico_win_mgr->compositor->seat_list, link) {
1118         if ((object & ICO_WINDOW_MGR_ACTIVE_POINTER) && (seat->pointer))   {
1119             if (surface)    {
1120                 if (seat->pointer->focus != surface) {
1121                     uifw_trace("win_mgr_set_active: pointer change surface(%08x=>%08x)",
1122                                (int)seat->pointer->focus, (int)surface);
1123                     weston_surface_from_global_fixed(surface,
1124                                                      seat->pointer->x,
1125                                                      seat->pointer->y,
1126                                                      &sx, &sy);
1127                     weston_pointer_set_focus(seat->pointer, surface, sx, sy);
1128                 }
1129                 else    {
1130                     uifw_trace("win_mgr_set_active: pointer nochange surface(%08x)",
1131                                (int)surface);
1132                 }
1133             }
1134             else    {
1135                 uifw_trace("win_mgr_set_active: pointer reset surface(%08x)",
1136                            (int)seat->pointer->focus);
1137                 weston_pointer_set_focus(seat->pointer, NULL,
1138                                          wl_fixed_from_int(0), wl_fixed_from_int(0));
1139             }
1140         }
1141         if ((object & ICO_WINDOW_MGR_ACTIVE_KEYBOARD) && (seat->keyboard))  {
1142             if (surface)    {
1143                 if (seat->keyboard->focus != surface)    {
1144                     weston_keyboard_set_focus(seat->keyboard, surface);
1145                     uifw_trace("win_mgr_set_active: keyboard change surface(%08x=>%08x)",
1146                                (int)seat->keyboard->focus, (int)surface);
1147                 }
1148                 else    {
1149                     uifw_trace("win_mgr_set_active: keyboard nochange surface(%08x)",
1150                                (int)surface);
1151                 }
1152             }
1153             else    {
1154                 uifw_trace("win_mgr_set_active: keyboard reset surface(%08x)",
1155                            (int)seat->keyboard);
1156                 weston_keyboard_set_focus(seat->keyboard, NULL);
1157             }
1158         }
1159     }
1160     uifw_trace("win_mgr_set_active: Leave(%08x)", (int)usurf);
1161 }
1162
1163 /*--------------------------------------------------------------------------*/
1164 /**
1165  * @brief   uifw_declare_manager: declare manager(ex.SystemController) client
1166  *
1167  * @param[in]   client      Weyland client
1168  * @param[in]   resource    resource of request
1169  * @param[in]   manager     manager(1=manager, 0=not manager)
1170  * @return      none
1171  */
1172 /*--------------------------------------------------------------------------*/
1173 static void
1174 uifw_declare_manager(struct wl_client *client, struct wl_resource *resource, int manager)
1175 {
1176     struct uifw_manager* mgr;
1177     struct uifw_win_surface *usurf;
1178     struct uifw_client *uclient;
1179     struct uifw_win_layer *el;
1180
1181     uifw_trace("uifw_declare_manager: Enter client=%08x manager=%d",
1182                (int)client, manager);
1183
1184     uclient = find_client_from_client(client);
1185     if (uclient)    {
1186         uclient->manager = manager;
1187     }
1188
1189     /* client set to manager            */
1190     _ico_win_mgr->num_manager = 0;
1191     wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
1192         if (mgr->resource == resource)  {
1193             if (mgr->manager != manager)    {
1194                 uifw_trace("uifw_declare_manager: Event Client.%08x Callback %d=>%d",
1195                            (int)client, mgr->manager, manager);
1196                 mgr->manager = manager;
1197
1198                 if (manager)    {
1199                     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
1200                         wl_list_for_each (usurf, &el->surface_list, ivi_layer) {
1201                             /* send window create event to manager  */
1202                             if (usurf->created != 0)    {
1203                                 uifw_trace("uifw_declare_manager: Send manager(%08x) "
1204                                            "WINDOW_CREATED(surf=%08x,pid=%d,appid=%s)",
1205                                            (int)resource, usurf->surfaceid,
1206                                            usurf->uclient->pid, usurf->uclient->appid);
1207                                 ico_window_mgr_send_window_created(resource,
1208                                                                    usurf->surfaceid,
1209                                                                    usurf->winname,
1210                                                                    usurf->uclient->pid,
1211                                                                    usurf->uclient->appid);
1212                             }
1213                         }
1214                     }
1215                 }
1216             }
1217         }
1218         if (mgr->manager)   {
1219             _ico_win_mgr->num_manager++;
1220         }
1221     }
1222     uifw_trace("uifw_declare_manager: Leave(managers=%d)", _ico_win_mgr->num_manager);
1223 }
1224
1225 /*--------------------------------------------------------------------------*/
1226 /**
1227  * @brief   uifw_create_layer: create layer Id and set attribute
1228  *
1229  * @param[in]   client      Weyland client
1230  * @param[in]   resource    resource of request
1231  * @param[in]   layer       layer id
1232  * @param[in]   attribute   layer attribute
1233  * @return      none
1234  */
1235 /*--------------------------------------------------------------------------*/
1236 static void
1237 uifw_create_layer(struct wl_client *client, struct wl_resource *resource,
1238                   uint32_t layer, int32_t attribute)
1239 {
1240     uifw_trace("uifw_create_layer: Enter(layer=%x,attr=%x)", layer, attribute);
1241     uifw_trace("uifw_create_layer: Leave");
1242 }
1243
1244 /*--------------------------------------------------------------------------*/
1245 /**
1246  * @brief   uifw_set_window_layer: set layer id to surface
1247  *
1248  * @param[in]   client      Weyland client
1249  * @param[in]   resource    resource of request
1250  * @param[in]   surfaceid   UIFW surface id
1251  * @param[in]   layer       layer id
1252  * @return      none
1253  */
1254 /*--------------------------------------------------------------------------*/
1255 static void
1256 uifw_set_window_layer(struct wl_client *client, struct wl_resource *resource,
1257                       uint32_t surfaceid, uint32_t layer)
1258 {
1259     uifw_trace("uifw_set_window_layer: Enter res=%08x surfaceid=%08x layer=%d",
1260                (int)resource, surfaceid, layer);
1261
1262     struct uifw_win_surface *usurf = find_uifw_win_surface_by_id(surfaceid);
1263
1264     if (! usurf)    {
1265         uifw_trace("uifw_set_window_layer: Leave(No Surface(id=%08x))", surfaceid);
1266         return;
1267     }
1268     if (usurf->win_layer->layer != layer) {
1269         win_mgr_set_layer(usurf, layer);
1270
1271         win_mgr_change_surface(usurf->surface, 1, 1);
1272     }
1273     uifw_trace("uifw_set_window_layer: Leave");
1274 }
1275
1276 /*--------------------------------------------------------------------------*/
1277 /**
1278  * @brief   win_mgr_set_weston_surface: set weston surface from UIFW surface
1279  *
1280  * @param[in]   usurf       UIFW surface
1281  * @return      none
1282  */
1283 /*--------------------------------------------------------------------------*/
1284 static void
1285 win_mgr_set_weston_surface(struct uifw_win_surface *usurf)
1286 {
1287     struct weston_surface *es = usurf->surface;
1288     int     buf_width, buf_height;
1289     int     width;
1290     int     height;
1291     int     x;
1292     int     y;
1293
1294     if (es == NULL) {
1295         uifw_trace("win_mgr_set_weston_surface: usurf(%08x) has no surface", (int)usurf);
1296         return;
1297     }
1298
1299     if (es->buffer_ref.buffer != NULL)   {
1300         buf_width = weston_surface_buffer_width(es);
1301         buf_height = weston_surface_buffer_height(es);
1302         width = usurf->width;
1303         height = usurf->height;
1304         x = usurf->x;
1305         y = usurf->y;
1306         if ((width <= 0) || (height <= 0))    {
1307             width = buf_width;
1308             usurf->width = buf_width;
1309             height = buf_height;
1310             usurf->height = buf_height;
1311         }
1312         if (usurf->width > buf_width)   {
1313             width = buf_width;
1314             x += (usurf->width - buf_width)/2;
1315         }
1316         if (usurf->height > buf_height) {
1317             height = buf_height;
1318             y += (usurf->height - buf_height)/2;
1319         }
1320         if (usurf->visible) {
1321             x += usurf->node_tbl->disp_x;
1322             y += usurf->node_tbl->disp_y;
1323         }
1324         else    {
1325             x = ICO_IVI_MAX_COORDINATE+1;
1326             y = ICO_IVI_MAX_COORDINATE+1;
1327         }
1328         if ((es->geometry.x != x) || (es->geometry.y != y) ||
1329             (es->geometry.width != width) || (es->geometry.height != height))    {
1330             weston_surface_damage_below(es);
1331             weston_surface_configure(es, x, y, width, height);
1332         }
1333         weston_surface_damage(es);
1334         weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
1335     }
1336 }
1337
1338 /*--------------------------------------------------------------------------*/
1339 /**
1340  * @brief   uifw_set_positionsize: set surface position and size
1341  *
1342  * @param[in]   client      Weyland client
1343  * @param[in]   resource    resource of request
1344  * @param[in]   surfaceid   UIFW surface id
1345  * @param[in]   node        surface node id
1346  * @param[in]   x           X coordinate on screen(if bigger than 16383, no change)
1347  * @param[in]   y           Y coordinate on screen(if bigger than 16383, no change)
1348  * @param[in]   width       surface width(if bigger than 16383, no change)
1349  * @param[in]   height      surface height(if bigger than 16383, no change)
1350  * @param[in]   animation   with/without animation
1351  * @return      none
1352  */
1353 /*--------------------------------------------------------------------------*/
1354 static void
1355 uifw_set_positionsize(struct wl_client *client, struct wl_resource *resource,
1356                       uint32_t surfaceid, uint32_t node, int32_t x, int32_t y,
1357                       int32_t width, int32_t height, int32_t animation)
1358 {
1359     struct uifw_client *uclient;
1360     struct weston_surface *es;
1361     int     cx, cy, cwidth, cheight;
1362
1363     uifw_trace("uifw_set_positionsize: Enter surf=%08x node=%x x/y/w/h=%d/%d/%d/%d anim=%d",
1364                surfaceid, node, x, y, width, height, animation);
1365
1366     if (((int)node) >= _ico_num_nodes)  {
1367         uifw_trace("uifw_set_positionsize: node=%d dose not exist(max=%d)",
1368                    node, _ico_num_nodes);
1369         node = 0;
1370     }
1371     struct uifw_win_surface* usurf = find_uifw_win_surface_by_id(surfaceid);
1372
1373     if (usurf && (usurf->surface))  {
1374         /* weston surface exist             */
1375         usurf->node_tbl = &_ico_node_table[node];
1376         es = usurf->surface;
1377
1378         /* if x,y,width,height bigger then ICO_IVI_MAX_COORDINATE, no change    */
1379         cx = usurf->x;
1380         cy = usurf->y;
1381         cwidth = usurf->width;
1382         cheight = usurf->height;
1383         if (x > ICO_IVI_MAX_COORDINATE)         x = cx;
1384         if (y > ICO_IVI_MAX_COORDINATE)         y = cy;
1385         if (width > ICO_IVI_MAX_COORDINATE)     width = cwidth;
1386         if (height > ICO_IVI_MAX_COORDINATE)    height = cheight;
1387
1388         /* check animation                  */
1389         if ((usurf->animation.restrain_configure != 0) &&
1390             (x == usurf->x) && (y == usurf->y) &&
1391             (width == usurf->width) && (height == usurf->height))   {
1392             uifw_trace("uifw_set_positionsize: Leave(same position size at animation)");
1393             return;
1394         }
1395
1396         uclient = find_client_from_client(client);
1397         if (uclient)    {
1398             if (! uclient->manager) uclient = NULL;
1399         }
1400         if (! uclient)  {
1401             if ((usurf->width > 0) && (usurf->height > 0))  {
1402                 win_mgr_surface_change_mgr(es, x, y, width, height);
1403                 uifw_trace("uifw_set_positionsize: Leave(Request from App)");
1404                 return;
1405             }
1406
1407             uifw_trace("uifw_set_positionsize: Initial Position/Size visible=%d",
1408                        usurf->visible);
1409             /* Initiale position is (0,0)   */
1410             weston_surface_set_position(es, (float)(usurf->node_tbl->disp_x),
1411                                         (float)(usurf->node_tbl->disp_y));
1412         }
1413
1414         uifw_trace("uifw_set_positionsize: Old geometry x/y=%d/%d,w/h=%d/%d",
1415                    (int)es->geometry.x, (int)es->geometry.y,
1416                    (int)es->geometry.width, (int)es->geometry.height);
1417
1418         usurf->x = x;
1419         usurf->y = y;
1420         usurf->width = width;
1421         usurf->height = height;
1422         if (_ico_win_mgr->num_manager <= 0) {
1423             /* no manager(HomeScreen), set geometory    */
1424             weston_surface_set_position(es, (float)(usurf->node_tbl->disp_x + x),
1425                                         (float)(usurf->node_tbl->disp_y + y));
1426         }
1427         if ((es->output) && (es->buffer_ref.buffer) &&
1428             (es->geometry.width > 0) && (es->geometry.height > 0)) {
1429             uifw_trace("uifw_set_positionsize: Fixed Geometry, Change(Vis=%d)",
1430                        usurf->visible);
1431             if (usurf->visible) {
1432                 win_mgr_set_weston_surface(usurf);
1433             }
1434         }
1435         win_mgr_change_surface(es, 0, 1);
1436
1437         uifw_trace("uifw_set_positionsize: Leave(OK,output=%08x)", (int)es->output);
1438     }
1439     else    {
1440         uifw_trace("uifw_set_positionsize: Leave(surf=%08x NOT Found)", surfaceid);
1441     }
1442 }
1443
1444 /*--------------------------------------------------------------------------*/
1445 /**
1446  * @brief   uifw_set_visible: surface visible/raise control
1447  *
1448  * @param[in]   client      Weyland client
1449  * @param[in]   resource    resource of request
1450  * @param[in]   surfaceid   UIFW surface id
1451  * @param[in]   visible     visible(1=show/0=hide/other=no change)
1452  * @param[in]   raise       raise(1=raise/0=lower/other=no change)
1453  * @param[in]   animation   with/without animation
1454  * @return      none
1455  */
1456 /*--------------------------------------------------------------------------*/
1457 static void
1458 uifw_set_visible(struct wl_client *client, struct wl_resource *resource,
1459                  uint32_t surfaceid, int32_t visible, int32_t raise, int32_t animation)
1460 {
1461     struct uifw_win_surface* usurf;
1462     struct uifw_client *uclient;
1463     int     restack;
1464     int     retanim;
1465
1466     uifw_trace("uifw_set_visible: Enter(surf=%08x,%d,%d,%d)",
1467                surfaceid, visible, raise, animation);
1468
1469     uclient = find_client_from_client(client);
1470     if (uclient)    {
1471         if (! uclient->manager) {
1472             uifw_trace("uifw_set_visible: Request from App(%s), not Manager",
1473                        uclient->appid);
1474             uclient = NULL;
1475         }
1476         else    {
1477             uifw_trace("uifw_set_visible: Request from Manager(%s)", uclient->appid);
1478         }
1479     }
1480     else    {
1481         uifw_trace("uifw_set_visible: Request from Unknown App, not Manager");
1482     }
1483
1484     usurf = find_uifw_win_surface_by_id(surfaceid);
1485
1486     if ((! usurf) || (! usurf->surface))    {
1487         uifw_trace("uifw_set_visible: Leave(Surface Not Exist)");
1488         return;
1489     }
1490     restack = 0;
1491
1492     if (visible == ICO_WINDOW_MGR_VISIBLE_SHOW) {
1493
1494         if (! usurf->visible)  {
1495             usurf->visible = 1;
1496             uifw_trace("uifw_set_visible: Change to Visible");
1497
1498             ico_ivi_shell_set_toplevel(usurf->shsurf);
1499
1500             /* Weston surface configure                     */
1501             uifw_trace("uifw_set_visible: Visible to Weston WSurf=%08x,%d.%d/%d/%d/%d",
1502                        (int)usurf->surface, usurf->node_tbl->node, usurf->x, usurf->y,
1503                        usurf->width, usurf->height);
1504             ico_ivi_shell_set_surface_type(usurf->shsurf);
1505             win_mgr_set_weston_surface(usurf);
1506
1507             restack = 1;                    /* need damage      */
1508
1509             if ((animation == ICO_WINDOW_MGR_ANIMATION_ANIMATION) &&
1510                 (usurf->animation.show_name != ICO_WINDOW_MGR_ANIMATION_NONE) &&
1511                 (win_mgr_hook_animation != NULL))   {
1512                 retanim = (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_OPSHOW,
1513                                                     (void *)usurf);
1514                 uifw_trace("uifw_set_visible: ret call animation = %d", retanim);
1515             }
1516         }
1517         else if ((raise != ICO_WINDOW_MGR_RAISE_LOWER) &&
1518                  (raise != ICO_WINDOW_MGR_RAISE_RAISE))  {
1519             uifw_trace("uifw_set_visible: Leave(No Change)");
1520             return;
1521         }
1522     }
1523     else if (visible == ICO_WINDOW_MGR_VISIBLE_HIDE)    {
1524
1525         if (usurf->visible)    {
1526
1527             /* Weston surface configure                     */
1528             weston_surface_damage_below(usurf->surface);
1529             win_mgr_set_weston_surface(usurf);
1530
1531             retanim = ICO_WINDOW_MGR_ANIMATION_RET_ANIMA;
1532             if ((animation == ICO_WINDOW_MGR_ANIMATION_ANIMATION) &&
1533                 (usurf->animation.hide_name != ICO_WINDOW_MGR_ANIMATION_NONE) &&
1534                 (win_mgr_hook_animation != NULL))   {
1535                 retanim = (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_OPHIDE,
1536                                                     (void *)usurf);
1537             }
1538             if (retanim != ICO_WINDOW_MGR_ANIMATION_RET_ANIMASHOW)    {
1539                 usurf->visible = 0;
1540                 uifw_trace("uifw_set_visible: Change to UnVisible");
1541                 /* change visible to unvisible, restack surface list    */
1542                 restack = 1;
1543                 /* Weston surface configure                     */
1544                 win_mgr_set_weston_surface(usurf);
1545             }
1546             else    {
1547                 uifw_trace("uifw_set_visible: UnVisible but animation");
1548             }
1549         }
1550         else if ((raise != ICO_WINDOW_MGR_RAISE_LOWER) &&
1551                  (raise != ICO_WINDOW_MGR_RAISE_RAISE))  {
1552             uifw_trace("uifw_set_visible: Leave(No Change)");
1553             return;
1554         }
1555     }
1556     else if ((raise != ICO_WINDOW_MGR_RAISE_LOWER) &&
1557              (raise != ICO_WINDOW_MGR_RAISE_RAISE))  {
1558         uifw_trace("uifw_set_visible: Leave(No Change)");
1559         return;
1560     }
1561
1562     /* raise/lower                              */
1563     if ((raise == ICO_WINDOW_MGR_RAISE_LOWER) || (raise == ICO_WINDOW_MGR_RAISE_RAISE))  {
1564         win_mgr_set_raise(usurf, raise);
1565         if (usurf->visible == 0)    {
1566             restack |= 2;
1567         }
1568     }
1569     else    {
1570         raise = ICO_WINDOW_MGR_V_NOCHANGE;
1571     }
1572
1573     if ((restack == 1) && (usurf->surface) &&
1574         (usurf->surface->buffer_ref.buffer) && (usurf->surface->output))   {
1575         weston_surface_damage(usurf->surface);
1576     }
1577     else if(restack & 2)    {
1578         win_mgr_restack_ivi_layer(usurf);
1579     }
1580
1581     /* send event(VISIBLE) to manager           */
1582     if (restack)    {
1583         ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
1584                                 usurf,
1585                                 (visible == ICO_WINDOW_MGR_VISIBLE_SHOW) ? 1 :
1586                                     ((visible == ICO_WINDOW_MGR_VISIBLE_HIDE) ? 0 :
1587                                         ICO_WINDOW_MGR_V_NOCHANGE),
1588                                 raise, uclient ? 0 : 1, 0,0);
1589     }
1590     uifw_trace("uifw_set_visible: Leave(OK)");
1591 }
1592
1593 /*--------------------------------------------------------------------------*/
1594 /**
1595  * @brief   uifw_set_animation: set animation of surface visible/unvisible
1596  *
1597  * @param[in]   client      Weyland client
1598  * @param[in]   resource    resource of request
1599  * @param[in]   surfaceid   UIFW surface id
1600  * @param[in]   type        how to change surface
1601  * @param[in]   anmation    animation name
1602  * @param[in]   time        animation time(ms), if 0, default time
1603  * @return      none
1604  */
1605 /*--------------------------------------------------------------------------*/
1606 static void
1607 uifw_set_animation(struct wl_client *client, struct wl_resource *resource,
1608                    uint32_t surfaceid, int32_t type, const char *animation, int32_t time)
1609 {
1610     int animaid;
1611     struct uifw_win_surface* usurf = find_uifw_win_surface_by_id(surfaceid);
1612
1613     uifw_trace("uifw_set_transition: Enter(surf=%08x,type=%x,anim=%s,time=%d)",
1614                surfaceid, type, animation, time);
1615
1616     if (usurf) {
1617         if ((*animation != 0) && (*animation != ' '))   {
1618             animaid = ico_get_animation_name(animation);
1619             uifw_trace("uifw_set_animation: Leave(OK) type=%d", animaid);
1620             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_HIDE)  {
1621                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPHIDE)    {
1622                     usurf->animation.next_name = animaid;
1623                 }
1624                 else    {
1625                     usurf->animation.hide_name = animaid;
1626                 }
1627             }
1628             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_SHOW)  {
1629                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPSHOW)    {
1630                     usurf->animation.next_name = animaid;
1631                 }
1632                 else    {
1633                     usurf->animation.show_name = animaid;
1634                 }
1635             }
1636             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_MOVE)  {
1637                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPMOVE)    {
1638                     usurf->animation.next_name = animaid;
1639                 }
1640                 else    {
1641                     usurf->animation.move_name = animaid;
1642                 }
1643             }
1644             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_RESIZE)    {
1645                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPRESIZE)  {
1646                     usurf->animation.next_name = animaid;
1647                 }
1648                 else    {
1649                     usurf->animation.resize_name = animaid;
1650                 }
1651             }
1652         }
1653         if ((time > 0) && (time != ICO_WINDOW_MGR_V_NOCHANGE))  {
1654             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_HIDE)  {
1655                 usurf->animation.hide_time = time;
1656             }
1657             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_SHOW)  {
1658                 usurf->animation.show_time = time;
1659             }
1660             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_MOVE)  {
1661                 usurf->animation.move_time = time;
1662             }
1663             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_RESIZE)    {
1664                 usurf->animation.resize_time = time;
1665             }
1666         }
1667     }
1668     else    {
1669         uifw_trace("uifw_set_animation: Leave(Surface(%08x) Not exist)", surfaceid);
1670     }
1671 }
1672
1673 /*--------------------------------------------------------------------------*/
1674 /**
1675  * @brief   uifw_set_active: set active surface
1676  *
1677  * @param[in]   client      Weyland client
1678  * @param[in]   resource    resource of request
1679  * @param[in]   surfaceid   UIFW surface id
1680  * @param[in]   active      target device
1681  * @return      none
1682  */
1683 /*--------------------------------------------------------------------------*/
1684 static void
1685 uifw_set_active(struct wl_client *client, struct wl_resource *resource,
1686                 uint32_t surfaceid, int32_t active)
1687 {
1688     struct uifw_win_surface* usurf;
1689
1690     uifw_trace("uifw_set_active: Enter(surf=%08x,active=%x)", surfaceid, active);
1691
1692     if ((surfaceid > 0) &&
1693         ((active & (ICO_WINDOW_MGR_ACTIVE_POINTER|ICO_WINDOW_MGR_ACTIVE_KEYBOARD)) != 0)) {
1694         usurf = find_uifw_win_surface_by_id(surfaceid);
1695     }
1696     else    {
1697         usurf = NULL;
1698     }
1699     if (usurf) {
1700         switch (active & (ICO_WINDOW_MGR_ACTIVE_POINTER|ICO_WINDOW_MGR_ACTIVE_KEYBOARD)) {
1701         case ICO_WINDOW_MGR_ACTIVE_POINTER:
1702             if (usurf != _ico_win_mgr->active_pointer_usurf)  {
1703                 if (_ico_win_mgr->active_pointer_usurf)   {
1704                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1705                                             _ico_win_mgr->active_pointer_usurf,
1706                                             (_ico_win_mgr->active_keyboard_usurf ==
1707                                              _ico_win_mgr->active_pointer_usurf) ?
1708                                                 ICO_WINDOW_MGR_ACTIVE_KEYBOARD :
1709                                                 ICO_WINDOW_MGR_ACTIVE_NONE,
1710                                             0,0,0,0);
1711                 }
1712                 _ico_win_mgr->active_pointer_usurf = usurf;
1713                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1714                                         usurf,
1715                                         ICO_WINDOW_MGR_ACTIVE_POINTER |
1716                                         (_ico_win_mgr->active_keyboard_usurf == usurf) ?
1717                                             ICO_WINDOW_MGR_ACTIVE_KEYBOARD : 0,
1718                                         0,0,0,0);
1719                 win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER);
1720             }
1721             break;
1722         case ICO_WINDOW_MGR_ACTIVE_KEYBOARD:
1723             if (usurf != _ico_win_mgr->active_keyboard_usurf) {
1724                 if (_ico_win_mgr->active_keyboard_usurf)   {
1725                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1726                                             _ico_win_mgr->active_keyboard_usurf,
1727                                             (_ico_win_mgr->active_keyboard_usurf ==
1728                                              _ico_win_mgr->active_pointer_usurf) ?
1729                                                 ICO_WINDOW_MGR_ACTIVE_POINTER :
1730                                                 ICO_WINDOW_MGR_ACTIVE_NONE,
1731                                             0,0,0,0);
1732                 }
1733                 _ico_win_mgr->active_keyboard_usurf = usurf;
1734                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1735                                         usurf,
1736                                         ICO_WINDOW_MGR_ACTIVE_KEYBOARD |
1737                                         (_ico_win_mgr->active_pointer_usurf == usurf) ?
1738                                             ICO_WINDOW_MGR_ACTIVE_POINTER : 0,
1739                                         0,0,0,0);
1740                 win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
1741             }
1742             break;
1743         default:
1744             if ((usurf != _ico_win_mgr->active_pointer_usurf) ||
1745                 (usurf != _ico_win_mgr->active_keyboard_usurf))   {
1746                 if (_ico_win_mgr->active_pointer_usurf)   {
1747                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1748                                             _ico_win_mgr->active_pointer_usurf,
1749                                             ICO_WINDOW_MGR_ACTIVE_NONE,
1750                                             0,0,0,0);
1751                     if (_ico_win_mgr->active_keyboard_usurf ==
1752                         _ico_win_mgr->active_pointer_usurf)   {
1753                         _ico_win_mgr->active_keyboard_usurf = NULL;
1754                     }
1755                 }
1756                 if (_ico_win_mgr->active_keyboard_usurf)   {
1757                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1758                                             _ico_win_mgr->active_keyboard_usurf,
1759                                             ICO_WINDOW_MGR_ACTIVE_NONE,
1760                                             0,0,0,0);
1761                 }
1762                 _ico_win_mgr->active_pointer_usurf = usurf;
1763                 _ico_win_mgr->active_keyboard_usurf = usurf;
1764                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1765                                         usurf,
1766                                         ICO_WINDOW_MGR_ACTIVE_POINTER |
1767                                             ICO_WINDOW_MGR_ACTIVE_KEYBOARD,
1768                                         0,0,0,0);
1769                 win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER |
1770                                               ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
1771             }
1772             break;
1773         }
1774         uifw_trace("uifw_set_active: Leave(Change Active)");
1775     }
1776     else    {
1777         win_mgr_set_active(NULL, active);
1778         uifw_trace("uifw_set_active: Leave(Reset active surface)");
1779     }
1780 }
1781
1782 /*--------------------------------------------------------------------------*/
1783 /**
1784  * @brief   uifw_set_layer_visible: layer visible control
1785  *
1786  * @param[in]   client      Weyland client
1787  * @param[in]   resource    resource of request
1788  * @param[in]   layer       layer id
1789  * @param[in]   visible     visible(1=show/0=hide)
1790  * @return      none
1791  */
1792 /*--------------------------------------------------------------------------*/
1793 static void
1794 uifw_set_layer_visible(struct wl_client *client, struct wl_resource *resource,
1795                        uint32_t layer, int32_t visible)
1796 {
1797     struct uifw_win_layer   *el;
1798     struct uifw_win_layer   *new_el;
1799     struct uifw_win_surface *usurf;
1800
1801     uifw_trace("uifw_set_layer_visible: Enter(layer=%d, visilbe=%d)", layer, visible);
1802
1803     /* Search Layer                             */
1804     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
1805         if (el->layer == layer) break;
1806     }
1807
1808     if (&el->link == &_ico_win_mgr->ivi_layer_list)    {
1809         /* layer not exist, create new layer    */
1810         uifw_trace("uifw_set_layer_visible: New Layer %d", layer);
1811         new_el = win_mgr_create_layer(NULL, layer);
1812         if (! new_el)   {
1813             uifw_trace("uifw_set_layer_visible: Leave(No Memory)");
1814             return;
1815         }
1816         new_el->visible = (visible != 0) ? TRUE : FALSE;
1817         ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_LAYER_VISIBLE, NULL,
1818                                 layer, new_el->visible, 0,0,0);
1819         uifw_trace("uifw_set_layer_visible: Leave(new layer)");
1820         return;
1821     }
1822
1823     /* control all surface in layer */
1824     if ((el->visible != FALSE) && (visible == 0))   {
1825         /* layer change to NOT visible  */
1826         uifw_trace("uifw_set_layer_visible: change to not visible");
1827         el->visible = FALSE;
1828     }
1829     else if ((el->visible == FALSE) && (visible != 0))  {
1830         /* layer change to visible      */
1831         uifw_trace("uifw_set_layer_visible: change to visible");
1832         el->visible = TRUE;
1833     }
1834     else    {
1835         /* no change    */
1836         uifw_trace("uifw_set_layer_visible: Leave(no Change %d=>%d)",
1837                    el->visible, visible);
1838         return;
1839     }
1840
1841     /* set damege area          */
1842     wl_list_for_each (usurf, &el->surface_list, ivi_layer) {
1843         if ((usurf->visible != FALSE) && (usurf->surface != NULL) &&
1844             (usurf->surface->output != NULL))  {
1845             /* Damage(redraw) target surface    */
1846             weston_surface_damage_below(usurf->surface);
1847         }
1848     }
1849
1850     /* rebild compositor surface list       */
1851     win_mgr_restack_ivi_layer(NULL);
1852
1853     /* send layer visible event to manager  */
1854     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_LAYER_VISIBLE, NULL,
1855                             layer, el->visible, 0,0,0);
1856
1857     uifw_trace("uifw_set_layer_visible: Leave");
1858 }
1859
1860 /*--------------------------------------------------------------------------*/
1861 /**
1862  * @brief   uifw_get_surfaces: get application surfaces
1863  *
1864  * @param[in]   client      Weyland client
1865  * @param[in]   resource    resource of request
1866  * @param[in]   appid       application id
1867  * @return      none
1868  */
1869 /*--------------------------------------------------------------------------*/
1870 static void
1871 uifw_get_surfaces(struct wl_client *client, struct wl_resource *resource, const char *appid)
1872 {
1873     uifw_trace("uifw_get_surfaces: Enter(appid=%s)", appid);
1874     uifw_trace("uifw_get_surfaces: Leave");
1875 }
1876
1877 /*--------------------------------------------------------------------------*/
1878 /**
1879  * @brief   uifw_map_surface: map surface buffer to shared memory
1880  *
1881  * @param[in]   client      Weyland client
1882  * @param[in]   resource    resource of request
1883  * @param[in]   surfaceid   surface id
1884  * @param[in]   mapname     shared memory map name
1885  * @param[in]   framerate   frame rate of shared memory update(frame/sec)
1886  * @return      none
1887  */
1888 /*--------------------------------------------------------------------------*/
1889 static void
1890 uifw_map_surface(struct wl_client *client, struct wl_resource *resource,
1891                  uint32_t surfaceid, const char *mapname, int32_t framerate)
1892 {
1893     uifw_trace("uifw_map_surface: Enter(surface=%08x,map=%s,fps=%d)",
1894                surfaceid, mapname, framerate);
1895     uifw_trace("uifw_map_surface: Leave");
1896 }
1897
1898 /*--------------------------------------------------------------------------*/
1899 /**
1900  * @brief   uifw_unmap_surface: unmap surface buffer to shared memory
1901  *
1902  * @param[in]   client      Weyland client
1903  * @param[in]   resource    resource of request
1904  * @param[in]   surfaceid   surface id
1905  * @return      none
1906  */
1907 /*--------------------------------------------------------------------------*/
1908 static void
1909 uifw_unmap_surface(struct wl_client *client, struct wl_resource *resource,
1910                    uint32_t surfaceid)
1911 {
1912     uifw_trace("uifw_map_surface: Enter(surface=%08x)", surfaceid);
1913     uifw_trace("uifw_map_surface: Leave");
1914 }
1915
1916 /*--------------------------------------------------------------------------*/
1917 /**
1918  * @brief   win_mgr_surface_change_mgr: surface chagen from manager(HomeScreen)
1919  *
1920  * @param[in]   surface     Weston surface
1921  * @param[in]   x           X coordinate on screen
1922  * @param[in]   y           Y coordinate on screen
1923  * @param[in]   width       surface width
1924  * @param[in]   height      surface height
1925  * @return      number of managers
1926  * @retval      > 0         number of managers
1927  * @retval      0           manager not exist
1928  */
1929 /*--------------------------------------------------------------------------*/
1930 static int
1931 win_mgr_surface_change_mgr(struct weston_surface *surface,
1932                            const int x, const int y, const int width, const int height)
1933 {
1934     int     num_mgr;
1935     struct uifw_win_surface *usurf;
1936
1937     uifw_trace("win_mgr_surface_change_mgr: Enter(%08x,x/y=%d/%d,w/h=%d/%d)",
1938                (int)surface, x, y, width, height);
1939
1940     usurf = find_uifw_win_surface_by_ws(surface);
1941     if (! usurf) {
1942         uifw_trace("win_mgr_surface_change_mgr: Leave(Not Exist)");
1943         return 0;
1944     }
1945
1946     num_mgr = ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
1947                                       usurf, x, y, width, height, 1);
1948
1949     uifw_trace("win_mgr_surface_change_mgr: Leave(%d)", num_mgr);
1950     return num_mgr;
1951 }
1952
1953 /*--------------------------------------------------------------------------*/
1954 /**
1955  * @brief   win_mgr_change_surface: surface change
1956  *
1957  * @param[in]   surface     Weston surface
1958  * @param[in]   to          destination(0=Client&Manager,1=Client,-1=Manager)
1959  * @param[in]   manager     request from manager(0=Client,1=Manager)
1960  * @return      none
1961  */
1962 /*--------------------------------------------------------------------------*/
1963 static void
1964 win_mgr_change_surface(struct weston_surface *surface, const int to, const int manager)
1965 {
1966     struct uifw_win_surface *usurf;
1967     int     x;
1968     int     y;
1969
1970     uifw_trace("win_mgr_change_surface: Enter(%08x,%d,%d)", (int)surface, to, manager);
1971
1972     /* Find surface         */
1973     usurf = find_uifw_win_surface_by_ws(surface);
1974     if (! usurf) {
1975         uifw_trace("win_mgr_change_surface: Leave(Not Exist)");
1976         return;
1977     }
1978
1979     /* if not configure surface, set surface size   */
1980     if (((usurf->width <= 0) || (usurf->height <= 0)) && (usurf->surface))  {
1981         uifw_trace("win_mgr_change_surface: set surface x/y=%d/%d=>%d/%d w/h=%d/%d=>%d/%d",
1982                    (int)usurf->surface->geometry.x, (int)usurf->surface->geometry.y,
1983                    usurf->x, usurf->y, usurf->width, usurf->height,
1984                    usurf->surface->geometry.width, usurf->surface->geometry.height);
1985         usurf->width = usurf->surface->geometry.width;
1986         usurf->height = usurf->surface->geometry.height;
1987         if (usurf->visible) {
1988             weston_surface_set_position(usurf->surface,
1989                                         (float)(usurf->node_tbl->disp_x + usurf->x),
1990                                         (float)(usurf->node_tbl->disp_y + usurf->y));
1991             win_mgr_restack_ivi_layer(usurf);
1992         }
1993         else    {
1994             weston_surface_set_position(usurf->surface, (float)(ICO_IVI_MAX_COORDINATE+1),
1995                                         (float)(ICO_IVI_MAX_COORDINATE+1));
1996         }
1997     }
1998
1999     /* send wayland event to client     */
2000     if ((to >= 0) && (usurf->shsurf != NULL) && (manager !=0) &&
2001         (usurf->width > 0) && (usurf->height > 0))  {
2002         uifw_trace("win_mgr_change_surface: SHELL_SURFACE_CONFIGURE %08x(%08x),w/h=%d/%d ",
2003                    usurf->surfaceid, (int)usurf->surface, usurf->width, usurf->height);
2004         ico_ivi_shell_send_configure(usurf->surface,
2005                                      WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT,
2006                                      usurf->width, usurf->height);
2007     }
2008
2009     if (usurf->visible) {
2010         x = usurf->node_tbl->disp_x + usurf->x;
2011         y = usurf->node_tbl->disp_y + usurf->y;
2012     }
2013     else    {
2014         x = ICO_IVI_MAX_COORDINATE+1;
2015         y = ICO_IVI_MAX_COORDINATE+1;
2016     }
2017     /* change geometry if request from manager  */
2018     if (manager)    {
2019         if ((usurf->width != usurf->surface->geometry.width) ||
2020             (usurf->height != usurf->surface->geometry.height) ||
2021             (usurf->surface->geometry.x != (float)x) ||
2022             (usurf->surface->geometry.y != (float)y))   {
2023             weston_surface_configure(usurf->surface, (float)x, (float)y,
2024                                      usurf->width, usurf->height);
2025             weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
2026         }
2027     }
2028
2029     /* send manager event to HomeScreen */
2030     if (to <= 0)    {
2031         if (manager)    {
2032             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
2033                                     usurf, usurf->x, usurf->y,
2034                                     usurf->width, usurf->height, 0);
2035         }
2036         else    {
2037             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
2038                                     usurf, (int)usurf->surface->geometry.x,
2039                                     (int)usurf->surface->geometry.y,
2040                                     usurf->surface->geometry.width,
2041                                     usurf->surface->geometry.height, 1);
2042         }
2043     }
2044
2045     /* change geometry if request from client   */
2046     if (! manager)  {
2047         if ((usurf->width != usurf->surface->geometry.width) ||
2048             (usurf->height != usurf->surface->geometry.height) ||
2049             (usurf->surface->geometry.x != (float)x) ||
2050             (usurf->surface->geometry.y != (float)y))   {
2051             weston_surface_configure(usurf->surface, (float)x, (float)y,
2052                                      usurf->width, usurf->height);
2053             weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
2054         }
2055     }
2056     uifw_trace("win_mgr_change_surface: Leave(OK)");
2057 }
2058
2059 /*--------------------------------------------------------------------------*/
2060 /**
2061  * @brief   win_mgr_select_surface: select surface by Bottun/Touch
2062  *
2063  * @param[in]   surface     Weston surface
2064  * @return      none
2065  */
2066 /*--------------------------------------------------------------------------*/
2067 static void
2068 win_mgr_select_surface(struct weston_surface *surface)
2069 {
2070     struct uifw_win_surface *usurf;
2071
2072     uifw_trace("win_mgr_select_surface: Enter(%08x)", (int)surface);
2073
2074     /* find surface         */
2075     usurf = find_uifw_win_surface_by_ws(surface);
2076     if (! usurf) {
2077         uifw_trace("win_mgr_select_surface: Leave(Not Exist)");
2078         return;
2079     }
2080     if (usurf != _ico_win_mgr->active_pointer_usurf)  {
2081
2082         /* send active event to manager     */
2083         if (ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
2084                                     usurf, ICO_WINDOW_MGR_ACTIVE_SELECTED, 0,0,0,0) <= 0) {
2085             uifw_trace("win_mgr_select_surface: not found manager, set active");
2086             win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER |
2087                                         ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
2088         }
2089     }
2090     uifw_trace("win_mgr_select_surface: Leave(OK)");
2091 }
2092
2093 /*--------------------------------------------------------------------------*/
2094 /**
2095  * @brief   win_mgr_set_title: set tile name to surface
2096  *
2097  * @param[in]   surface     weston surface
2098  * @param[in]   title       title name
2099  * @return      none
2100  */
2101 /*--------------------------------------------------------------------------*/
2102 static void
2103 win_mgr_set_title(struct weston_surface *surface, const char *title)
2104 {
2105     struct uifw_win_surface *usurf;
2106
2107     uifw_trace("win_mgr_set_title: Enter(%08x) name=%s", (int)surface, title);
2108
2109     usurf = find_uifw_win_surface_by_ws(surface);
2110     if (! usurf) {
2111         uifw_trace("win_mgr_set_title: Leave(Not Exist)");
2112         return;
2113     }
2114     if (((usurf->width > 0) && (usurf->height > 0)) &&
2115         ((usurf->created == 0) ||
2116          (strncmp(title, usurf->winname, ICO_IVI_WINNAME_LENGTH-1) != 0)))    {
2117         strncpy(usurf->winname, title, ICO_IVI_WINNAME_LENGTH-1);
2118         usurf->winname[ICO_IVI_WINNAME_LENGTH-1] = 0;
2119         if (usurf->created == 0)    {
2120             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CREATED, usurf, 0,0,0,0,0);
2121             usurf->created = 1;
2122         }
2123         else    {
2124             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_NAME, usurf, 0,0,0,0,0);
2125         }
2126     }
2127     else    {
2128         strncpy(usurf->winname, title, ICO_IVI_WINNAME_LENGTH-1);
2129         usurf->winname[ICO_IVI_WINNAME_LENGTH-1] = 0;
2130     }
2131     uifw_trace("win_mgr_set_title: Leave");
2132 }
2133
2134 /*--------------------------------------------------------------------------*/
2135 /**
2136  * @brief   win_mgr_surface_move: set tile name to surface
2137  *
2138  * @param[in]   surface     weston surface
2139  * @param[in]   title       title name
2140  * @return      none
2141  */
2142 /*--------------------------------------------------------------------------*/
2143 static void
2144 win_mgr_surface_move(struct weston_surface *surface, int *dx, int *dy)
2145 {
2146     struct uifw_win_surface *usurf;
2147
2148     uifw_trace("win_mgr_surface_move: Enter(%08x) x/y=%d,%d", (int)surface, *dx, *dy);
2149
2150     usurf = find_uifw_win_surface_by_ws(surface);
2151     if (! usurf) {
2152         uifw_trace("win_mgr_surface_move: Leave(Not Exist)");
2153         return;
2154     }
2155     if (usurf->visible) {
2156         *dx = usurf->node_tbl->disp_x + usurf->x;
2157         *dy = usurf->node_tbl->disp_y + usurf->y;
2158     }
2159     else    {
2160         *dx = ICO_IVI_MAX_COORDINATE+1;
2161         *dy = ICO_IVI_MAX_COORDINATE+1;
2162     }
2163     uifw_trace("win_mgr_surface_move: Leave(change to x/y=%d/%d)", *dx, *dy);
2164 }
2165
2166 /*--------------------------------------------------------------------------*/
2167 /**
2168  * @brief   ico_window_mgr_set_visible: change surface visibility
2169  *
2170  * @param[in]   usurf       UIFW surface
2171  * @param[in]   visible     visible(=1)/unvisible(0)
2172  * @return      none
2173  */
2174 /*--------------------------------------------------------------------------*/
2175 WL_EXPORT void
2176 ico_window_mgr_set_visible(struct uifw_win_surface *usurf, const int visible)
2177 {
2178     if (visible)    {
2179         if (usurf->visible == 0)    {
2180             uifw_trace("ico_window_mgr_set_visible: Chagne to Visible(%08x)", (int)usurf);
2181             usurf->visible = 1;
2182             /* change unvisible to visible, restack surface list    */
2183             win_mgr_restack_ivi_layer(usurf);
2184         }
2185     }
2186     else    {
2187         if (usurf->visible != 0)    {
2188             uifw_trace("ico_window_mgr_set_visible: Chagne to Unvisible(%08x)", (int)usurf);
2189             usurf->visible = 0;
2190             /* change visible to unvisible, restack surface list    */
2191             win_mgr_restack_ivi_layer(usurf);
2192         }
2193     }
2194     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
2195                             usurf, usurf->visible, usurf->raise, 0, 0,0);
2196 }
2197
2198 /*--------------------------------------------------------------------------*/
2199 /**
2200  * @brief   win_mgr_set_raise: change surface raise/lower
2201  *
2202  * @param[in]   usurf       UIFW surface
2203  * @param[in]   raise       raise(=1)/lower(0)
2204  * @return      none
2205  */
2206 /*--------------------------------------------------------------------------*/
2207 static void
2208 win_mgr_set_raise(struct uifw_win_surface *usurf, const int raise)
2209 {
2210     uifw_trace("win_mgr_set_raise: Enter(%08x,%d) layer=%x",
2211                (int)usurf, raise, (int)usurf->win_layer->layer);
2212
2213     wl_list_remove(&usurf->ivi_layer);
2214     if (raise)  {
2215         /* raise ... surface stack to top of layer          */
2216         wl_list_insert(&usurf->win_layer->surface_list, &usurf->ivi_layer);
2217         usurf->raise = 1;
2218         uifw_trace("win_mgr_set_raise: Raise Link to Top");
2219     }
2220     else    {
2221         /* Lower ... surface stack to bottom of layer       */
2222         wl_list_insert(usurf->win_layer->surface_list.prev, &usurf->ivi_layer);
2223         usurf->raise = 0;
2224         uifw_trace("win_mgr_set_raise: Lower Link to Bottom");
2225     }
2226
2227     /* rebild compositor surface list               */
2228     if (usurf->visible) {
2229         win_mgr_restack_ivi_layer(usurf);
2230     }
2231     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
2232                             usurf, usurf->visible, usurf->raise, 0, 0,0);
2233
2234     uifw_trace("win_mgr_set_raise: Leave");
2235 }
2236
2237 /*--------------------------------------------------------------------------*/
2238 /**
2239  * @brief   win_mgr_destroy_surface: surface destroy
2240  *
2241  * @param[in]   surface     Weston surface
2242  * @return      none
2243  */
2244 /*--------------------------------------------------------------------------*/
2245 static void
2246 win_mgr_destroy_surface(struct weston_surface *surface)
2247 {
2248     struct uifw_win_surface *usurf;
2249     struct uifw_win_surface *phash;
2250     struct uifw_win_surface *bhash;
2251     uint32_t    hash;
2252
2253     usurf = find_uifw_win_surface_by_ws(surface);
2254     if (! usurf) {
2255         uifw_trace("win_mgr_destroy_surface: UIFW surface Not Exist");
2256         return;
2257     }
2258     uifw_trace("win_mgr_destroy_surface: Enter(%08x) %08x", (int)surface, usurf->surfaceid);
2259
2260     /* destroy active surface       */
2261     if (usurf == _ico_win_mgr->active_pointer_usurf)  {
2262         _ico_win_mgr->active_pointer_usurf = NULL;
2263     }
2264     if (usurf == _ico_win_mgr->active_keyboard_usurf) {
2265         _ico_win_mgr->active_keyboard_usurf = NULL;
2266     }
2267
2268     /* destory animation extenson   */
2269     if (win_mgr_hook_animation) {
2270         (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_DESTROY, (void *)usurf);
2271     }
2272
2273     /* delete from layer list       */
2274     wl_list_remove(&usurf->ivi_layer);
2275     win_mgr_restack_ivi_layer(NULL);
2276
2277     /* delete from hash table       */
2278     hash = MAKE_IDHASH(usurf->surfaceid);
2279     phash = _ico_win_mgr->idhash[hash];
2280     bhash = NULL;
2281     while ((phash) && (phash != usurf)) {
2282         bhash = phash;
2283         phash = phash->next_idhash;
2284     }
2285     if (bhash)  {
2286         bhash->next_idhash = usurf->next_idhash;
2287     }
2288     else    {
2289         _ico_win_mgr->idhash[hash] = usurf->next_idhash;
2290     }
2291
2292     hash = MAKE_WSHASH(usurf->surface);
2293     phash = _ico_win_mgr->wshash[hash];
2294     bhash = NULL;
2295     while ((phash) && (phash != usurf)) {
2296         bhash = phash;
2297         phash = phash->next_wshash;
2298     }
2299     if (bhash)  {
2300         bhash->next_wshash = usurf->next_wshash;
2301     }
2302     else    {
2303         _ico_win_mgr->wshash[hash] = usurf->next_wshash;
2304     }
2305
2306     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_DESTROYED,
2307                            usurf, 0,0,0,0,0);
2308
2309     hash = usurf->surfaceid & SURCAFE_ID_MASK;
2310     _ico_win_mgr->surfaceid_map[(hash - 1)/16] &= ~(1 << ((hash - 1) % 16));
2311
2312     free(usurf);
2313     uifw_trace("win_mgr_destroy_surface: Leave(OK)");
2314 }
2315
2316 /*--------------------------------------------------------------------------*/
2317 /**
2318  * @brief   bind_ico_win_mgr: bind Multi Window Manager from client
2319  *
2320  * @param[in]   client      client
2321  * @param[in]   data        user data(unused)
2322  * @param[in]   version     protocol version(unused)
2323  * @param[in]   id          client object id
2324  * @return      none
2325  */
2326 /*--------------------------------------------------------------------------*/
2327 static void
2328 bind_ico_win_mgr(struct wl_client *client,
2329                  void *data, uint32_t version, uint32_t id)
2330 {
2331     struct wl_resource  *add_resource;
2332     struct uifw_manager *nm;
2333     struct uifw_client  *uclient;
2334
2335     uifw_trace("bind_ico_win_mgr: Enter(client=%08x, id=%x)", (int)client, (int)id);
2336
2337     add_resource = wl_resource_create(client, &ico_window_mgr_interface, 1, id);
2338     if (add_resource)   {
2339         wl_resource_set_implementation(add_resource, &ico_window_mgr_implementation,
2340                                        _ico_win_mgr, unbind_ico_win_mgr);
2341     }
2342
2343     /* Create client management tabel       */
2344     uclient = find_client_from_client(client);
2345     if (! uclient)  {
2346         win_mgr_bind_client(client, NULL);
2347     }
2348
2349     /* Manager                              */
2350     nm = (struct uifw_manager *)malloc(sizeof(struct uifw_manager));
2351     memset(nm, 0, sizeof(struct uifw_manager));
2352     nm->resource = add_resource;
2353     wl_list_insert(&_ico_win_mgr->manager_list, &nm->link);
2354
2355     uifw_trace("bind_ico_win_mgr: Leave");
2356 }
2357
2358 /*--------------------------------------------------------------------------*/
2359 /**
2360  * @brief   unbind_ico_win_mgr: unbind Multi Window Manager from client
2361  *
2362  * @param[in]   resource    client resource
2363  * @return      none
2364  */
2365 /*--------------------------------------------------------------------------*/
2366 static void
2367 unbind_ico_win_mgr(struct wl_resource *resource)
2368 {
2369     struct uifw_manager *mgr, *itmp;
2370
2371     uifw_trace("unbind_ico_win_mgr: Enter");
2372
2373     /* Remove manager from manager list */
2374     _ico_win_mgr->num_manager = 0;
2375     wl_list_for_each_safe (mgr, itmp, &_ico_win_mgr->manager_list, link)    {
2376         if (mgr->resource == resource) {
2377             wl_list_remove(&mgr->link);
2378             free(mgr);
2379         }
2380         else    {
2381             if (mgr->manager)   {
2382                 _ico_win_mgr->num_manager++;
2383             }
2384         }
2385     }
2386     uifw_trace("unbind_ico_win_mgr: Leave");
2387 }
2388
2389 /*--------------------------------------------------------------------------*/
2390 /**
2391  * @brief   ico_win_mgr_send_to_mgr: send event to manager(HomeScreen)
2392  *
2393  * @param[in]   event       event code(if -1, not send event)
2394  * @param[in]   usurf       UIFW surface table
2395  * @param[in]   param1      parameter 1
2396  * @param[in]      :             :
2397  * @param[in]   param5      parameter 5
2398  * @return      number of managers
2399  * @retval      > 0         number of managers
2400  * @retval      0           manager not exist
2401  */
2402 /*--------------------------------------------------------------------------*/
2403 static int
2404 ico_win_mgr_send_to_mgr(const int event, struct uifw_win_surface *usurf,
2405                         const int param1, const int param2, const int param3,
2406                         const int param4, const int param5)
2407 {
2408     int     num_mgr = 0;
2409     struct uifw_manager* mgr;
2410
2411     /* send created if not send created event   */
2412     if ((usurf != NULL) && (usurf->created == 0) &&
2413         (((usurf->width > 0) && (usurf->height > 0)) ||
2414          ((event != ICO_WINDOW_MGR_WINDOW_CREATED) &&
2415           (event != ICO_WINDOW_MGR_WINDOW_NAME))))  {
2416         wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
2417             if (mgr->manager)   {
2418                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) WINDOW_CREATED"
2419                            "(surf=%08x,name=%s,pid=%d,appid=%s)", (int)mgr->resource,
2420                            usurf->surfaceid, usurf->winname, usurf->uclient->pid,
2421                            usurf->uclient->appid);
2422                 ico_window_mgr_send_window_created(mgr->resource, usurf->surfaceid,
2423                                                    usurf->winname, usurf->uclient->pid,
2424                                                    usurf->uclient->appid);
2425             }
2426         }
2427         usurf->created = 1;
2428     }
2429
2430     wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
2431         if (mgr->manager)   {
2432             num_mgr ++;
2433
2434             switch(event)   {
2435             case ICO_WINDOW_MGR_WINDOW_CREATED:
2436                 /* Do nothing anymore because sended window create event    */
2437                 break;
2438
2439             case ICO_WINDOW_MGR_WINDOW_NAME:
2440                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) WINDOW_NAME"
2441                            "(surf=%08x,name=%s)", (int)mgr->resource,
2442                            usurf->surfaceid, usurf->winname);
2443                 ico_window_mgr_send_window_name(mgr->resource, usurf->surfaceid,
2444                                                 usurf->winname);
2445                 break;
2446
2447             case ICO_WINDOW_MGR_WINDOW_DESTROYED:
2448                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) DESTROYED"
2449                            "(surf=%08x)", (int)mgr->resource, usurf->surfaceid);
2450                 ico_window_mgr_send_window_destroyed(mgr->resource, usurf->surfaceid);
2451                 break;
2452
2453             case ICO_WINDOW_MGR_WINDOW_VISIBLE:
2454                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) VISIBLE"
2455                            "(surf=%08x,vis=%d,raise=%d,hint=%d)",
2456                            (int)mgr->resource, usurf->surfaceid, param1, param2, param3);
2457                 ico_window_mgr_send_window_visible(mgr->resource,
2458                                                    usurf->surfaceid, param1, param2, param3);
2459                 break;
2460
2461             case ICO_WINDOW_MGR_WINDOW_CONFIGURE:
2462                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) CONFIGURE"
2463                            "(surf=%08x,app=%s,node=%x,layer=%x,x/y=%d/%d,w/h=%d/%d,hint=%d)",
2464                            (int)mgr->resource, usurf->surfaceid, usurf->uclient->appid,
2465                            usurf->node_tbl->node, usurf->win_layer->layer,
2466                            param1, param2, param3, param4, param5);
2467                 ico_window_mgr_send_window_configure(mgr->resource, usurf->surfaceid,
2468                                                      usurf->node_tbl->node,
2469                                                      usurf->win_layer->layer,
2470                                                      param1, param2, param3, param4, param5);
2471                 break;
2472
2473             case ICO_WINDOW_MGR_WINDOW_ACTIVE:
2474                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) ACTIVE"
2475                            "(surf=%08x,active=%d)", (int)mgr->resource, usurf->surfaceid,
2476                            param1);
2477                 ico_window_mgr_send_window_active(mgr->resource, usurf->surfaceid,
2478                                                   (uint32_t)param1);
2479                 break;
2480
2481             case ICO_WINDOW_MGR_LAYER_VISIBLE:
2482                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) LAYER_VISIBLE"
2483                            "(layer=%x,visivle=%d)", (int)mgr->resource, param1, param2);
2484                 ico_window_mgr_send_layer_visible(mgr->resource, (uint32_t)param1, param2);
2485                 break;
2486
2487             case ICO_WINDOW_MGR_MAP_SURFACE:
2488                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) MAP SURFACE"
2489                            "(%08x,ev=%x,w/h/s/f=%d/%d/%d/%x)", (int)mgr->resource,
2490                            usurf->surfaceid,
2491                            param1, param2, param3, param4, param5);
2492                 ico_window_mgr_send_map_surface(mgr->resource, param1, usurf->surfaceid,
2493                                                 param2, param3, param4, param5);
2494                 break;
2495
2496             default:
2497                 uifw_error("ico_win_mgr_send_to_mgr: Illegal event(%08x)", event);
2498                 break;
2499             }
2500         }
2501     }
2502     return num_mgr;
2503 }
2504
2505 /*--------------------------------------------------------------------------*/
2506 /**
2507  * @brief   ico_window_mgr_set_hook_animation: set animation hook routine
2508  *
2509  * @param[in]   hook_animation  hook routine
2510  * @return      none
2511  */
2512 /*--------------------------------------------------------------------------*/
2513 WL_EXPORT void
2514 ico_window_mgr_set_hook_animation(int (*hook_animation)(const int op, void *data))
2515 {
2516     win_mgr_hook_animation = hook_animation;
2517 }
2518
2519 /*--------------------------------------------------------------------------*/
2520 /**
2521  * @brief   module_init: initialize ico_window_mgr
2522  *                       this function called from ico_pluign_loader
2523  *
2524  * @param[in]   es          weston compositor
2525  * @param[in]   argc        number of arguments(unused)
2526  * @param[in]   argv        argument list(unused)
2527  * @return      result
2528  * @retval      0           sccess
2529  * @retval      -1          error
2530  */
2531 /*--------------------------------------------------------------------------*/
2532 WL_EXPORT int
2533 module_init(struct weston_compositor *ec, int *argc, char *argv[])
2534 {
2535     int     nodeId;
2536     int     i;
2537     int     idx;
2538     struct weston_output *output;
2539     struct weston_config_section *section;
2540     char    *displayno = NULL;
2541     char    *p;
2542
2543     uifw_info("ico_window_mgr: Enter(module_init)");
2544
2545     /* get ivi debug level                      */
2546     section = weston_config_get_section(ec->config, "ivi-debug", NULL, NULL);
2547     if (section)    {
2548         weston_config_section_get_int(section, "flag", &_ico_ivi_debug_flag, 0);
2549         weston_config_section_get_int(section, "log", &_ico_ivi_debug_level, 3);
2550     }
2551
2552     /* get animation default                    */
2553     section = weston_config_get_section(ec->config, "ivi-animation", NULL, NULL);
2554     if (section)    {
2555         weston_config_section_get_string(section, "default", &_ico_ivi_animation_name, NULL);
2556         weston_config_section_get_int(section, "time", &_ico_ivi_animation_time, 500);
2557         weston_config_section_get_int(section, "fps", &_ico_ivi_animation_fps, 15);
2558     }
2559
2560     /* get display number list                  */
2561     section = weston_config_get_section(ec->config, "ivi-display", NULL, NULL);
2562     if (section)    {
2563         weston_config_section_get_string(section, "displayno", &displayno, NULL);
2564     }
2565
2566     /* create ico_window_mgr management table   */
2567     _ico_win_mgr = (struct ico_win_mgr *)malloc(sizeof(struct ico_win_mgr));
2568     if (_ico_win_mgr == NULL)   {
2569         uifw_error("ico_window_mgr: malloc failed");
2570         return -1;
2571     }
2572
2573     memset(_ico_win_mgr, 0, sizeof(struct ico_win_mgr));
2574     _ico_win_mgr->surfaceid_map = (uint16_t *) malloc(INIT_SURFACE_IDS/8);
2575     if (! _ico_win_mgr->surfaceid_map)  {
2576         uifw_error("ico_window_mgr: malloc failed");
2577         return -1;
2578     }
2579     uifw_trace("ico_window_mgr: table=%08x", (int)_ico_win_mgr);
2580     memset(_ico_win_mgr->surfaceid_map, 0, INIT_SURFACE_IDS/8);
2581
2582     _ico_win_mgr->compositor = ec;
2583
2584     _ico_win_mgr->surfaceid_max = INIT_SURFACE_IDS;
2585     _ico_win_mgr->surfaceid_count = INIT_SURFACE_IDS;
2586
2587     uifw_trace("ico_window_mgr: wl_global_create(bind_ico_win_mgr)");
2588     if (wl_global_create(ec->wl_display, &ico_window_mgr_interface, 1,
2589                          _ico_win_mgr, bind_ico_win_mgr) == NULL)  {
2590         uifw_error("ico_window_mgr: Error(wl_global_create)");
2591         return -1;
2592     }
2593
2594     wl_list_init(&_ico_win_mgr->client_list);
2595     wl_list_init(&_ico_win_mgr->manager_list);
2596     wl_list_init(&_ico_win_mgr->ivi_layer_list);
2597
2598     /* create display list                  */
2599     if (displayno != NULL)   {
2600         p = displayno;
2601     }
2602     else    {
2603         p = NULL;
2604     }
2605     _ico_num_nodes = 0;
2606     wl_list_for_each(output, &ec->output_list, link) {
2607         _ico_num_nodes++;
2608         if (_ico_num_nodes >= ICO_IVI_MAX_DISPLAY)   break;
2609     }
2610     memset(&_ico_node_table[0], 0, sizeof(_ico_node_table));
2611     i = 0;
2612     wl_list_for_each(output, &ec->output_list, link) {
2613         p = strtok(p, ",");
2614         if (p)  {
2615             idx = strtol(p, (char **)0, 0);
2616             uifw_trace("ico_window_mgr: config Display.%d is %d", i, idx);
2617             p = NULL;
2618             if ((idx < 0) || (idx >= _ico_num_nodes))   {
2619                 idx = i;
2620             }
2621         }
2622         else    {
2623             idx = i;
2624         }
2625         if (_ico_node_table[idx].node)  {
2626             for (idx = 0; idx < _ico_num_nodes; idx++)  {
2627                 if (_ico_node_table[idx].node == 0) break;
2628             }
2629             if (idx >= _ico_num_nodes)  {
2630                 uifw_error("ico_window_mgr: number of display overflow");
2631                 idx = 0;
2632             }
2633         }
2634         _ico_node_table[idx].node = idx + 0x100;
2635         _ico_node_table[idx].displayno = i;
2636         _ico_node_table[idx].disp_x = output->x;
2637         _ico_node_table[idx].disp_y = output->y;
2638         _ico_node_table[idx].disp_width = output->width;
2639         _ico_node_table[idx].disp_height = output->height;
2640         i ++;
2641         if (i >= _ico_num_nodes) break;
2642     }
2643     idx = 0;
2644     for (i = 0; i < _ico_num_nodes; i++)    {
2645         _ico_node_table[i].node &= 0x0ff;
2646         uifw_trace("ico_window_mgr: Display.%d no=%d x/y=%d/%d w/h=%d/%d",
2647                    i, _ico_node_table[i].displayno,
2648                    _ico_node_table[i].disp_x, _ico_node_table[i].disp_y,
2649                    _ico_node_table[i].disp_width, _ico_node_table[i].disp_height);
2650     }
2651     if (displayno)  free(displayno);
2652
2653     /* my node Id ... this version fixed 0  */
2654     nodeId = ico_ivi_get_mynode();
2655
2656     _ico_win_mgr->surface_head = ICO_IVI_SURFACEID_BASE(nodeId);
2657     uifw_trace("ico_window_mgr: NoedId=%04x SurfaceIdBase=%08x",
2658                 nodeId, _ico_win_mgr->surface_head);
2659
2660     /* Hook to IVI-Shell                            */
2661     ico_ivi_shell_hook_bind(win_mgr_bind_client);
2662     ico_ivi_shell_hook_unbind(win_mgr_unbind_client);
2663     ico_ivi_shell_hook_create(win_mgr_register_surface);
2664     ico_ivi_shell_hook_destroy(win_mgr_destroy_surface);
2665     ico_ivi_shell_hook_map(win_mgr_map_surface);
2666     ico_ivi_shell_hook_change(win_mgr_change_surface);
2667     ico_ivi_shell_hook_select(win_mgr_select_surface);
2668     ico_ivi_shell_hook_title(win_mgr_set_title);
2669     ico_ivi_shell_hook_move(win_mgr_surface_move);
2670
2671     uifw_info("ico_window_mgr: Leave(module_init)");
2672
2673     return 0;
2674 }
2675