The change of the configuration and the implementation of the event input.
[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 <EGL/egl.h>
50 #include <GLES2/gl2.h>
51 #include <GLES2/gl2ext.h>
52
53 #include <weston/compositor.h>
54 #include "ico_ivi_common.h"
55 #include "ico_ivi_shell.h"
56 #include "ico_window_mgr.h"
57 #include "desktop-shell-server-protocol.h"
58 #include "ico_window_mgr-server-protocol.h"
59
60 /* SurfaceID                            */
61 #define INIT_SURFACE_IDS    1024            /* SurfaceId table initiale size        */
62 #define ADD_SURFACE_IDS     512             /* SurfaceId table additional size      */
63 #define SURCAFE_ID_MASK     0x0ffff         /* SurfaceId bit mask pattern           */
64 #define UIFW_HASH           64              /* Hash value (2's compliment)          */
65
66 /* Internal fixed value                 */
67 #define ICO_WINDOW_MGR_APPID_FIXCOUNT   5   /* retry count of appid fix             */
68                                             /* show/hide animation with position    */
69 #define ICO_WINDOW_MGR_ANIMATION_POS    0x10000000
70
71 /* wl_drm_buffer (inport from mesa-9.1.3/src/egl/wayland/wayland-drm/wayland-drm.h) */
72 struct uifw_wl_buffer   {
73     struct wl_resource resource;
74     int32_t width, height;
75     uint32_t busy_count;
76 };
77 struct uifw_drm_buffer {
78     struct uifw_wl_buffer buffer;
79     void *drm;                  /* struct wl_drm    */
80     uint32_t format;
81     const void *driver_format;
82     int32_t offset[3];
83     int32_t stride[3];
84     void *driver_buffer;
85 };
86
87 /* __DRIimage (inport from mesa-9.1.3/src/mesa/drivers/dri/intel/intel_regions.h)   */
88 struct uifw_dri_region  {
89    void *bo;                /**< buffer manager's buffer */
90    uint32_t refcount;       /**< Reference count for region */
91    uint32_t cpp;            /**< bytes per pixel */
92    uint32_t width;          /**< in pixels */
93    uint32_t height;         /**< in pixels */
94    uint32_t pitch;          /**< in bytes */
95    char *map;               /**< only non-NULL when region is actually mapped */
96    uint32_t map_refcount;   /**< Reference count for mapping */
97    uint32_t tiling;         /**< Which tiling mode the region is in */
98    uint32_t name;           /**< Global name for the bo */
99    void *screen;            /* screen   */
100 };
101 struct uifw_dri_image   {
102    struct uifw_dri_region *region;
103    int internal_format;
104    uint32_t dri_format;
105    uint32_t format;
106    uint32_t offset;
107    uint32_t strides[3];
108    uint32_t offsets[3];
109    void *planar_format; /* image_format */
110    void *data;
111 };
112
113 /* gl_surface_state (inport from weston-1.2.1/src/gl-renderer.c)    */
114 enum buffer_type {
115     BUFFER_TYPE_NULL,
116     BUFFER_TYPE_SHM,
117     BUFFER_TYPE_EGL
118 };
119 struct uifw_gl_surface_state {
120     GLfloat color[4];
121     struct gl_shader *shader;
122
123     GLuint textures[3];
124     int num_textures;
125     int needs_full_upload;
126     pixman_region32_t texture_damage;
127
128     void *images[3];            /* EGLImageKHR */
129     GLenum target;
130     int num_images;
131
132     struct weston_buffer_reference buffer_ref;
133     enum buffer_type buffer_type;
134     int pitch; /* in pixels */
135     int height; /* in pixels */
136 };
137
138 /* Multi Windiw Manager                 */
139 struct ico_win_mgr {
140     struct weston_compositor *compositor;   /* Weston compositor                    */
141     void    *shell;                         /* shell(ico_ivi_shell) table address   */
142     int32_t surface_head;                   /* (HostID << 24) | (DisplayNo << 16)   */
143
144     struct wl_list  client_list;            /* Clients                              */
145     struct wl_list  manager_list;           /* Manager(ex.HomeScreen) list          */
146     int             num_manager;            /* Number of managers                   */
147     struct wl_list  ivi_layer_list;         /* Layer management table list          */
148     struct wl_list  map_list;               /* surface map list                     */
149     struct uifw_surface_map *free_maptable; /* free maped surface table list        */
150     struct weston_animation map_animation[ICO_IVI_MAX_DISPLAY];
151                                             /* animation for map check              */
152     struct uifw_win_surface *active_pointer_usurf;  /* Active Pointer Surface       */
153     struct uifw_win_surface *active_keyboard_usurf; /* Active Keyboard Surface      */
154
155     struct uifw_win_surface *idhash[UIFW_HASH];  /* UIFW SerfaceID                  */
156     struct uifw_win_surface *wshash[UIFW_HASH];  /* Weston Surface                  */
157
158     uint32_t surfaceid_count;               /* Number of surface id                 */
159     uint32_t surfaceid_max;                 /* Maximum number of surface id         */
160     uint16_t *surfaceid_map;                /* SurfaceId assign bit map             */
161
162     char    shell_init;                     /* shell initialize flag                */
163     char    res;                            /* (unused)                             */
164 };
165
166 /* Internal macros                      */
167 /* UIFW SurfaceID                       */
168 #define MAKE_IDHASH(v)  (((uint32_t)v) & (UIFW_HASH-1))
169 /* Weston Surface                       */
170 #define MAKE_WSHASH(v)  ((((uint32_t)v) >> 5) & (UIFW_HASH-1))
171
172 /* function prototype                   */
173                                             /* get surface table from weston surface*/
174 static struct uifw_win_surface *find_uifw_win_surface_by_ws(
175                     struct weston_surface *wsurf);
176                                             /* get client table from weston client  */
177 static struct uifw_client* find_client_from_client(struct wl_client *client);
178                                             /* assign new surface id                */
179 static uint32_t generate_id(void);
180                                             /* bind shell client                    */
181 static void win_mgr_bind_client(struct wl_client *client, void *shell);
182                                             /* unind shell client                   */
183 static void win_mgr_unbind_client(struct wl_client *client);
184                                             /* get appid from pid                   */
185 static void win_mgr_get_client_appid(struct uifw_client *uclient);
186                                             /* create new surface                   */
187 static void win_mgr_register_surface(
188                     struct wl_client *client, struct wl_resource *resource,
189                     struct weston_surface *surface, struct shell_surface *shsurf);
190                                             /* surface destroy                      */
191 static void win_mgr_destroy_surface(struct weston_surface *surface);
192                                             /* map new surface                      */
193 static void win_mgr_map_surface(struct weston_surface *surface, int32_t *width,
194                                 int32_t *height, int32_t *sx, int32_t *sy);
195                                             /* send surface change event to manager */
196 static void win_mgr_change_surface(struct weston_surface *surface,
197                                    const int to, const int manager);
198                                             /* window manager surface configure     */
199 static void win_mgr_surface_configure(struct uifw_win_surface *usurf,
200                                       int x, int y, int width, int height);
201                                             /* shell surface configure              */
202 static void win_mgr_shell_configure(struct weston_surface *surface);
203                                             /* surface select                       */
204 static void win_mgr_select_surface(struct weston_surface *surface);
205                                             /* surface set title                    */
206 static void win_mgr_set_title(struct weston_surface *surface, const char *title);
207                                             /* surface move request from shell      */
208 static void win_mgr_surface_move(struct weston_surface *surface, int *dx, int *dy);
209                                             /* set raise                            */
210 static void win_mgr_set_raise(struct uifw_win_surface *usurf, const int raise);
211                                             /* surface change from manager          */
212 static int win_mgr_surface_change_mgr(struct weston_surface *surface, const int x,
213                                       const int y, const int width, const int height);
214                                             /* create new layer                     */
215 static struct uifw_win_layer *win_mgr_create_layer(struct uifw_win_surface *usurf,
216                                                    const uint32_t layer);
217                                             /* set surface layer                    */
218 static void win_mgr_set_layer(struct uifw_win_surface *usurf, const uint32_t layer);
219                                             /* set active surface                   */
220 static void win_mgr_set_active(struct uifw_win_surface *usurf, const int target);
221
222                                             /* declare manager                      */
223 static void uifw_declare_manager(struct wl_client *client, struct wl_resource *resource,
224                                  int manager);
225                                             /* set window layer                     */
226 static void uifw_set_window_layer(struct wl_client *client,
227                                   struct wl_resource *resource,
228                                   uint32_t surfaceid, uint32_t layer);
229                                             /* set surface size and position        */
230 static void uifw_set_positionsize(struct wl_client *client, struct wl_resource *resource,
231                                   uint32_t surfaceid, uint32_t node, int32_t x, int32_t y,
232                                   int32_t width, int32_t height, int32_t flags);
233                                             /* show/hide and raise/lower surface    */
234 static void uifw_set_visible(struct wl_client *client, struct wl_resource *resource,
235                              uint32_t surfaceid, int32_t visible, int32_t raise,
236                              int32_t flag);
237                                             /* set surface animation                */
238 static void uifw_set_animation(struct wl_client *client, struct wl_resource *resource,
239                                uint32_t surfaceid, int32_t type,
240                                const char *animation, int32_t time);
241                                             /* set surface attributes               */
242 static void uifw_set_attributes(struct wl_client *client, struct wl_resource *resource,
243                                 uint32_t surfaceid, uint32_t attributes);
244                                             /* surface visibility control with animation*/
245 static void uifw_visible_animation(struct wl_client *client, struct wl_resource *resource,
246                                    uint32_t surfaceid, int32_t visible,
247                                    int32_t x, int32_t y, int32_t width, int32_t height);
248                                             /* set active surface (form HomeScreen) */
249 static void uifw_set_active(struct wl_client *client, struct wl_resource *resource,
250                             uint32_t surfaceid, int32_t active);
251                                             /* layer visibility control             */
252 static void uifw_set_layer_visible(struct wl_client *client, struct wl_resource *resource,
253                                    uint32_t layer, int32_t visible);
254                                             /* get application surfaces             */
255 static void uifw_get_surfaces(struct wl_client *client, struct wl_resource *resource,
256                               const char *appid);
257                                             /* check and change all mapped surface  */
258 static void win_mgr_check_surfacemap(struct weston_animation *animation,
259                                      struct weston_output *output, uint32_t msecs);
260                                             /* check and change mapped surface      */
261 static void win_mgr_change_mapsurface(struct uifw_surface_map *sm, int event);
262                                             /* map surface to system application    */
263 static void uifw_map_surface(struct wl_client *client, struct wl_resource *resource,
264                              uint32_t surfaceid, int32_t framerate);
265                                             /* unmap surface                        */
266 static void uifw_unmap_surface(struct wl_client *client, struct wl_resource *resource,
267                                uint32_t surfaceid);
268                                             /* bind manager                         */
269 static void bind_ico_win_mgr(struct wl_client *client,
270                              void *data, uint32_t version, uint32_t id);
271                                             /* unbind manager                       */
272 static void unbind_ico_win_mgr(struct wl_resource *resource);
273                                             /* send event to manager                */
274 static int ico_win_mgr_send_to_mgr(const int event, struct uifw_win_surface *usurf,
275                                    const int param1, const int param2, const int param3,
276                                    const int param4, const int param5);
277                                             /* set surface transform                */
278 static int win_mgr_set_scale(struct uifw_win_surface *usurf);
279                                             /* convert animation name to Id value   */
280 static int ico_get_animation_name(const char *animation);
281                                             /* hook for animation                   */
282 static int  (*win_mgr_hook_animation)(const int op, void *data) = NULL;
283
284 /* static tables                        */
285 /* Multi Window Manager interface       */
286 static const struct ico_window_mgr_interface ico_window_mgr_implementation = {
287     uifw_declare_manager,
288     uifw_set_window_layer,
289     uifw_set_positionsize,
290     uifw_set_visible,
291     uifw_set_animation,
292     uifw_set_attributes,
293     uifw_visible_animation,
294     uifw_set_active,
295     uifw_set_layer_visible,
296     uifw_get_surfaces,
297     uifw_map_surface,
298     uifw_unmap_surface
299 };
300
301 /* plugin common value(without ico_plugin_loader)   */
302 static int  _ico_ivi_debug_flag = 0;            /* Debug flags                      */
303 static int  _ico_ivi_debug_level = 3;           /* Debug Level                      */
304 static char *_ico_ivi_animation_name = NULL;    /* Default animation name           */
305 static int  _ico_ivi_animation_time = 500;      /* Default animation time           */
306 static int  _ico_ivi_animation_fps = 15;        /* Animation frame rate             */
307 static int  _ico_ivi_background_layer = 0;      /* Background layer                 */
308 static int  _ico_ivi_default_layer = 1;         /* Deafult layer id at surface create*/
309 static int  _ico_ivi_input_layer = 101;         /* Input layer id                   */
310 static int  _ico_ivi_cursor_layer = 102;        /* Cursor layer id                  */
311 static int  _ico_ivi_startup_layer = 109;       /* Deafult layer id at system startup*/
312
313 /* static management table              */
314 static struct ico_win_mgr       *_ico_win_mgr = NULL;
315 static int                      _ico_num_nodes = 0;
316 static struct uifw_node_table   _ico_node_table[ICO_IVI_MAX_DISPLAY];
317
318
319 /*--------------------------------------------------------------------------*/
320 /**
321  * @brief   ico_ivi_debugflag: get debug flags
322  *
323  * @param       None
324  * @return      debug flags
325  */
326 /*--------------------------------------------------------------------------*/
327 WL_EXPORT   int
328 ico_ivi_debugflag(void)
329 {
330     return _ico_ivi_debug_flag;
331 }
332
333 /*--------------------------------------------------------------------------*/
334 /**
335  * @brief   ico_ivi_debuglevel: answer debug output level.
336  *
337  * @param       none
338  * @return      debug output level
339  * @retval      0       No debug output
340  * @retval      1       Only error output
341  * @retval      2       Error and Warning output
342  * @retval      3       Error, Warning and information output
343  * @retval      4       All output with debug write
344  */
345 /*--------------------------------------------------------------------------*/
346 WL_EXPORT   int
347 ico_ivi_debuglevel(void)
348 {
349     return _ico_ivi_debug_level;
350 }
351
352 /*--------------------------------------------------------------------------*/
353 /**
354  * @brief   ico_ivi_default_animation_name: get default animation name
355  *
356  * @param       None
357  * @return      Default animation name
358  */
359 /*--------------------------------------------------------------------------*/
360 WL_EXPORT   const char *
361 ico_ivi_default_animation_name(void)
362 {
363     return _ico_ivi_animation_name;
364 }
365
366 /*--------------------------------------------------------------------------*/
367 /**
368  * @brief   ico_ivi_default_animation_time: get default animation time
369  *
370  * @param       None
371  * @return      Default animation time(miri sec)
372  */
373 /*--------------------------------------------------------------------------*/
374 WL_EXPORT   int
375 ico_ivi_default_animation_time(void)
376 {
377     return _ico_ivi_animation_time;
378 }
379
380 /*--------------------------------------------------------------------------*/
381 /**
382  * @brief   ico_ivi_default_animation_fps: get default animation frame rate
383  *
384  * @param       None
385  * @return      Default animation frame rate(frames/sec)
386  */
387 /*--------------------------------------------------------------------------*/
388 WL_EXPORT   int
389 ico_ivi_default_animation_fps(void)
390 {
391     return _ico_ivi_animation_fps;
392 }
393
394 /*--------------------------------------------------------------------------*/
395 /**
396  * @brief   ico_ivi_get_mynode: Get my NodeId
397  *
398  * @param       None
399  * @return      NodeId of my node
400  */
401 /*--------------------------------------------------------------------------*/
402 WL_EXPORT   int
403 ico_ivi_get_mynode(void)
404 {
405     /* Reference Platform 0.90 only support 1 ECU   */
406     return 0;
407 }
408
409 /*--------------------------------------------------------------------------*/
410 /**
411  * @brief   ico_window_mgr_set_weston_surface: set weston surface
412  *
413  * @param[in]   usurf       UIFW surface
414  * @param[in]   x           X coordinate on screen
415  * @param[in]   y           Y coordinate on screen
416  * @param[in]   width       width
417  * @param[in]   height      height
418  * @return      none
419  */
420 /*--------------------------------------------------------------------------*/
421 WL_EXPORT   void
422 ico_window_mgr_set_weston_surface(struct uifw_win_surface *usurf,
423                                   int x, int y, int width, int height)
424 {
425     struct weston_surface *es = usurf->surface;
426     int     buf_width, buf_height;
427
428     if (es == NULL) {
429         uifw_trace("ico_window_mgr_set_weston_surface: usurf(%08x) has no surface",
430                    (int)usurf);
431         return;
432     }
433
434     if (es->buffer_ref.buffer != NULL)   {
435         buf_width = weston_surface_buffer_width(es);
436         buf_height = weston_surface_buffer_height(es);
437         if ((width <= 0) || (height <= 0))    {
438             width = buf_width;
439             usurf->width = buf_width;
440             height = buf_height;
441             usurf->height = buf_height;
442         }
443         if (usurf->width > buf_width)   {
444             width = buf_width;
445             x += (usurf->width - buf_width)/2;
446         }
447         if (usurf->height > buf_height) {
448             height = buf_height;
449             y += (usurf->height - buf_height)/2;
450         }
451         if (usurf->visible) {
452             x += usurf->node_tbl->disp_x;
453             y += usurf->node_tbl->disp_y;
454         }
455         else    {
456             x = ICO_IVI_MAX_COORDINATE+1;
457             y = ICO_IVI_MAX_COORDINATE+1;
458         }
459         if ((es->geometry.x != x) || (es->geometry.y != y) ||
460             (es->geometry.width != width) || (es->geometry.height != height))    {
461             weston_surface_damage_below(es);
462             win_mgr_surface_configure(usurf, x, y, width, height);
463         }
464         weston_surface_damage(es);
465         weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
466     }
467 }
468
469 /*--------------------------------------------------------------------------*/
470 /**
471  * @brief   ico_window_mgr_set_weston_surface: set weston surface
472  *
473  * @param[in]   usurf       UIFW surface
474  * @param[in]   x           X coordinate on screen
475  * @param[in]   y           Y coordinate on screen
476  * @param[in]   width       width
477  * @param[in]   height      height
478  * @return      none
479  */
480 /*--------------------------------------------------------------------------*/
481 WL_EXPORT   void
482 ico_window_mgr_change_surface(struct uifw_win_surface *usurf,
483                               const int to, const int manager)
484 {
485     uifw_trace("ico_window_mgr_change_surface: Enter(%08x,%d,%d)",
486                usurf->surfaceid, to, manager);
487     win_mgr_change_surface(usurf->surface, to, manager);
488     uifw_trace("ico_window_mgr_change_surface: Leave");
489 }
490
491 /*--------------------------------------------------------------------------*/
492 /**
493  * @brief   ico_window_mgr_get_usurf: find UIFW surface by surface id
494  *
495  * @param[in]   surfaceid   UIFW surface id
496  * @return      UIFW surface table address
497  * @retval      !=NULL      success(surface table address)
498  * @retval      NULL        error(surface id dose not exist)
499  */
500 /*--------------------------------------------------------------------------*/
501 WL_EXPORT   struct uifw_win_surface *
502 ico_window_mgr_get_usurf(const uint32_t surfaceid)
503 {
504     struct uifw_win_surface *usurf;
505
506     usurf = _ico_win_mgr->idhash[MAKE_IDHASH(surfaceid)];
507
508     while (usurf)   {
509         if (usurf->surfaceid == surfaceid) {
510             return usurf;
511         }
512         usurf = usurf->next_idhash;
513     }
514     uifw_trace("ico_window_mgr_get_usurf: NULL");
515     return NULL;
516 }
517
518 /*--------------------------------------------------------------------------*/
519 /**
520  * @brief   find_uifw_win_surface_by_ws: find UIFW surface by weston surface
521  *
522  * @param[in]   wsurf       Weston surface
523  * @return      UIFW surface table address
524  * @retval      !=NULL      success(surface table address)
525  * @retval      NULL        error(surface dose not exist)
526  */
527 /*--------------------------------------------------------------------------*/
528 static struct uifw_win_surface *
529 find_uifw_win_surface_by_ws(struct weston_surface *wsurf)
530 {
531     struct uifw_win_surface *usurf;
532
533     usurf = _ico_win_mgr->wshash[MAKE_WSHASH(wsurf)];
534
535     while (usurf)   {
536         if (usurf->surface == wsurf) {
537             return usurf;
538         }
539         usurf = usurf->next_wshash;
540     }
541     uifw_trace("find_uifw_win_surface_by_ws: NULL");
542     return NULL;
543 }
544
545 /*--------------------------------------------------------------------------*/
546 /**
547  * @brief   find_client_from_client: find UIFW client by wayland client
548  *
549  * @param[in]   client      Wayland client
550  * @return      UIFW client table address
551  * @retval      !=NULL      success(client table address)
552  * @retval      NULL        error(client dose not exist)
553  */
554 /*--------------------------------------------------------------------------*/
555 static struct uifw_client*
556 find_client_from_client(struct wl_client *client)
557 {
558     struct uifw_client  *uclient;
559
560     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
561         if (uclient->client == client)  {
562             return(uclient);
563         }
564     }
565     uifw_trace("find_client_from_client: client.%08x is NULL", (int)client);
566     return NULL;
567 }
568
569 /*--------------------------------------------------------------------------*/
570 /**
571  * @brief   ico_window_mgr_get_appid: find application id by wayland client
572  *
573  * @param[in]   client      Wayland client
574  * @return      application id
575  * @retval      !=NULL      success(application id)
576  * @retval      NULL        error(client dose not exist)
577  */
578 /*--------------------------------------------------------------------------*/
579 WL_EXPORT   char *
580 ico_window_mgr_get_appid(struct wl_client* client)
581 {
582     struct uifw_client  *uclient;
583
584     uclient = find_client_from_client(client);
585
586     if (! uclient)  {
587         return NULL;
588     }
589     return uclient->appid;
590 }
591
592 /*--------------------------------------------------------------------------*/
593 /**
594  * @brief   generate_id: generate uniq id for UIFW surface id
595  *
596  * @param       none
597  * @return      uniq id for UIFW surface id
598  */
599 /*--------------------------------------------------------------------------*/
600 static uint32_t
601 generate_id(void)
602 {
603     int     rep;
604     int     i;
605     int     map;
606     uint16_t *new_map;
607     uint32_t surfaceId;
608
609     /* next assign id                           */
610     _ico_win_mgr->surfaceid_count ++;
611
612     /* serach free id from bitmap               */
613     for (rep = 0; rep < (int)(_ico_win_mgr->surfaceid_max/16); rep++)   {
614         if (_ico_win_mgr->surfaceid_count >= _ico_win_mgr->surfaceid_max)   {
615             _ico_win_mgr->surfaceid_count = 0;
616         }
617         if (_ico_win_mgr->surfaceid_map[_ico_win_mgr->surfaceid_count/16] != 0xffff)    {
618             /* find free id from bitmap         */
619             map = 1 << (_ico_win_mgr->surfaceid_count % 16);
620             for (i = (_ico_win_mgr->surfaceid_count % 16); i < 16; i++) {
621                 if ((_ico_win_mgr->surfaceid_map[_ico_win_mgr->surfaceid_count/16] & map)
622                         == 0) {
623                     _ico_win_mgr->surfaceid_map[_ico_win_mgr->surfaceid_count/16] |= map;
624                     _ico_win_mgr->surfaceid_count
625                         = (_ico_win_mgr->surfaceid_count/16)*16 + i;
626
627                     surfaceId = (_ico_win_mgr->surfaceid_count + 1)
628                                 | _ico_win_mgr->surface_head;
629                     uifw_trace("generate_id: SurfaceId=%08x", surfaceId);
630                     return(surfaceId);
631                 }
632                 map = map << 1;
633             }
634         }
635         _ico_win_mgr->surfaceid_count += 16;
636     }
637
638     /* no free id in bitmap, extend bitmap      */
639     if ((_ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS) > SURCAFE_ID_MASK)  {
640         /* too many surfaces, system error      */
641         uifw_trace("generate_id: SurffaceId Overflow(%d, Max=%d), Abort",
642                    _ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS, SURCAFE_ID_MASK);
643         fprintf(stderr, "generate_id: SurffaceId Overflow(%d, Max=%d), Abort\n",
644                 _ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS, SURCAFE_ID_MASK);
645         abort();
646     }
647
648     new_map = (uint16_t *) malloc((_ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS) / 8);
649     memcpy(new_map, _ico_win_mgr->surfaceid_map, _ico_win_mgr->surfaceid_max/8);
650     memset(&new_map[_ico_win_mgr->surfaceid_max/16], 0, ADD_SURFACE_IDS/8);
651     _ico_win_mgr->surfaceid_count = _ico_win_mgr->surfaceid_max;
652     new_map[_ico_win_mgr->surfaceid_count/16] |= 1;
653     _ico_win_mgr->surfaceid_max += ADD_SURFACE_IDS;
654     free(_ico_win_mgr->surfaceid_map);
655     _ico_win_mgr->surfaceid_map = new_map;
656
657     uifw_trace("generate_id: Extent SurfaceId=%d(Max.%d)",
658                _ico_win_mgr->surfaceid_count+1, _ico_win_mgr->surfaceid_max);
659     surfaceId = (_ico_win_mgr->surfaceid_count + 1) | _ico_win_mgr->surface_head;
660
661     uifw_trace("generate_id: SurfaceId=%08x", surfaceId);
662     return(surfaceId);
663 }
664
665 /*--------------------------------------------------------------------------*/
666 /**
667  * @brief   win_mgr_bind_client: desktop_shell from client
668  *
669  * @param[in]   client          Wayland client
670  * @param[in]   shell           shell(ico_ivi_shell) table address
671  * @return      none
672  */
673 /*--------------------------------------------------------------------------*/
674 static void
675 win_mgr_bind_client(struct wl_client *client, void *shell)
676 {
677     struct uifw_client  *uclient;
678     int     newclient;
679     pid_t   pid;
680     uid_t   uid;
681     gid_t   gid;
682
683     uifw_trace("win_mgr_bind_client: Enter(client=%08x, shell=%08x)",
684                (int)client, (int)shell);
685
686     /* save shell table address             */
687     if (shell)  {
688         _ico_win_mgr->shell = shell;
689     }
690
691     /* set client                           */
692     uclient = find_client_from_client(client);
693     if (! uclient)  {
694         /* client not exist, create client management table             */
695         uifw_trace("win_mgr_bind_client: Create Client");
696         uclient = (struct uifw_client *)malloc(sizeof(struct uifw_client));
697         if (!uclient)   {
698             uifw_error("win_mgr_bind_client: Error, No Memory");
699             return;
700         }
701         memset(uclient, 0, sizeof(struct uifw_client));
702         uclient->client = client;
703         wl_list_init(&uclient->surface_link);
704         newclient = 1;
705     }
706     else    {
707         newclient = 0;
708     }
709     wl_client_get_credentials(client, &pid, &uid, &gid);
710     uifw_trace("win_mgr_bind_client: client=%08x pid=%d uid=%d gid=%d",
711                (int)client, (int)pid, (int)uid, (int)gid);
712     if (pid > 0)    {
713         uclient->pid = (int)pid;
714         /* get applicationId from AppCore(AUL)  */
715         win_mgr_get_client_appid(uclient);
716
717         /* weston internal client, not manage   */
718         if (strcmp(uclient->appid, "weston") == 0)  {
719             free(uclient);
720             uifw_trace("win_mgr_bind_client: client=%08x is internal, delete", (int)client);
721         }
722         else if (newclient > 0)  {
723             wl_list_insert(&_ico_win_mgr->client_list, &uclient->link);
724         }
725     }
726     else    {
727         uifw_trace("win_mgr_bind_client: client=%08x pid dose not exist", (int)client);
728     }
729     uifw_trace("win_mgr_bind_client: Leave");
730 }
731
732 /*--------------------------------------------------------------------------*/
733 /**
734  * @brief   win_mgr_unbind_client: unbind desktop_shell from client
735  *
736  * @param[in]   client          Wayland client
737  * @return      none
738  */
739 /*--------------------------------------------------------------------------*/
740 static void
741 win_mgr_unbind_client(struct wl_client *client)
742 {
743     struct uifw_client  *uclient;
744
745     uifw_trace("win_mgr_unbind_client: Enter(client=%08x)", (int)client);
746
747     uclient = find_client_from_client(client);
748     if (uclient)    {
749         /* Client exist, Destory client management table             */
750         wl_list_remove(&uclient->link);
751         free(uclient);
752     }
753     uifw_trace("win_mgr_unbind_client: Leave");
754 }
755
756 /*--------------------------------------------------------------------------*/
757 /**
758  * @brief   win_mgr_get_client_appid: get applicationId from pid
759  *
760  * @param[in]   uclient     UIFW client management table
761  * @return      none
762  */
763 /*--------------------------------------------------------------------------*/
764 static void
765 win_mgr_get_client_appid(struct uifw_client *uclient)
766 {
767     int     fd;
768     int     size;
769     int     i;
770     int     j;
771     char    procpath[128];
772
773     uifw_trace("win_mgr_get_client_appid: Enter(pid=%d)", uclient->pid);
774
775     memset(uclient->appid, 0, ICO_IVI_APPID_LENGTH);
776     i = aul_app_get_appid_bypid(uclient->pid, uclient->appid, ICO_IVI_APPID_LENGTH);
777     uifw_trace("win_mgr_get_client_appid: aul_app_get_appid_bypid ret=%d "
778                "pid=%d appid=<%s>", i, uclient->pid, uclient->appid);
779     if (uclient->appid[0] != 0) {
780         /* OK, end of get appid         */
781         uclient->fixed_appid = ICO_WINDOW_MGR_APPID_FIXCOUNT;
782     }
783     else    {
784         uclient->fixed_appid ++;
785         /* client dose not exist in AppCore, search Linux process table */
786         uifw_trace("win_mgr_get_client_appid: pid=%d dose not exist in AppCore(AUL)",
787                    uclient->pid);
788
789         memset(uclient->appid, 0, ICO_IVI_APPID_LENGTH);
790         snprintf(procpath, sizeof(procpath)-1, "/proc/%d/cmdline", uclient->pid);
791         fd = open(procpath, O_RDONLY);
792         if (fd >= 0)    {
793             size = read(fd, procpath, sizeof(procpath));
794             for (; size > 0; size--)    {
795                 if (procpath[size-1])   break;
796             }
797             if (size > 0)   {
798                 /* get program base name    */
799                 i = 0;
800                 for (j = 0; j < size; j++)  {
801                     if (procpath[j] == 0)   break;
802                     if (procpath[j] == '/') i = j + 1;
803                 }
804                 j = 0;
805                 for (; i < size; i++)   {
806                     uclient->appid[j] = procpath[i];
807                     if ((uclient->appid[j] == 0) ||
808                         (j >= (ICO_IVI_APPID_LENGTH-1)))    break;
809                     j++;
810                 }
811                 /* search application number in apprication start option    */
812                 if ((uclient->appid[j] == 0) && (j < (ICO_IVI_APPID_LENGTH-2))) {
813                     for (; i < size; i++)   {
814                         if ((procpath[i] == 0) &&
815                             (procpath[i+1] == '@')) {
816                             strncpy(&uclient->appid[j], &procpath[i+1],
817                                     ICO_IVI_APPID_LENGTH - j - 2);
818                         }
819                     }
820                 }
821             }
822             close(fd);
823         }
824         for (i = strlen(uclient->appid)-1; i >= 0; i--) {
825             if (uclient->appid[i] != ' ')   break;
826         }
827         uclient->appid[i+1] = 0;
828         if (uclient->appid[0])  {
829             uifw_trace("win_mgr_get_client_appid: pid=%d appid=<%s> from "
830                        "Process table", uclient->pid, uclient->appid);
831         }
832         else    {
833             uifw_trace("win_mgr_get_client_appid: pid=%d dose not exist in Process table",
834                        uclient->pid);
835             sprintf(uclient->appid, "?%d?", uclient->pid);
836         }
837     }
838     uifw_trace("win_mgr_get_client_appid: Leave(pid=%d,appid=%s)",
839                uclient->pid, uclient->appid);
840 }
841
842 /*--------------------------------------------------------------------------*/
843 /**
844  * @brief   ico_get_animation_name: convert animation name to Id value
845  *
846  * @param[in]   animation       animation name
847  * @return      animation Id value
848  */
849 /*--------------------------------------------------------------------------*/
850 static int
851 ico_get_animation_name(const char *animation)
852 {
853     int anima = ICO_WINDOW_MGR_ANIMATION_NONE;
854
855     if (strcasecmp(animation, "none") == 0) {
856         return ICO_WINDOW_MGR_ANIMATION_NONE;
857     }
858
859     if (win_mgr_hook_animation) {
860         anima = (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_NAME, (void *)animation);
861     }
862     if (anima <= 0) {
863         anima = ICO_WINDOW_MGR_ANIMATION_NONE;
864     }
865     return anima;
866 }
867
868 /*--------------------------------------------------------------------------*/
869 /**
870  * @brief   win_mgr_register_surface: create UIFW surface
871  *
872  * @param[in]   client          Wayland client
873  * @param[in]   resource        client resource
874  * @param[in]   surface         Weston surface
875  * @param[in]   shsurf          shell surface
876  * @return      none
877  */
878 /*--------------------------------------------------------------------------*/
879 static void
880 win_mgr_register_surface(struct wl_client *client, struct wl_resource *resource,
881                          struct weston_surface *surface, struct shell_surface *shsurf)
882 {
883     struct uifw_win_surface *usurf;
884     struct uifw_win_surface *phash;
885     struct uifw_win_surface *bhash;
886     uint32_t    hash;
887
888     uifw_trace("win_mgr_register_surface: Enter(surf=%08x,client=%08x,res=%08x)",
889                (int)surface, (int)client, (int)resource);
890
891     /* check new surface                    */
892     if (find_uifw_win_surface_by_ws(surface))   {
893         /* surface exist, NOP               */
894         uifw_trace("win_mgr_register_surface: Leave(Already Exist)");
895         return;
896     }
897
898     /* set default color and shader */
899     weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
900
901     /* create UIFW surface management table */
902     usurf = malloc(sizeof(struct uifw_win_surface));
903     if (! usurf)    {
904         uifw_error("win_mgr_register_surface: No Memory");
905         return;
906     }
907
908     memset(usurf, 0, sizeof(struct uifw_win_surface));
909
910     usurf->surfaceid = generate_id();
911     usurf->surface = surface;
912     usurf->shsurf = shsurf;
913     usurf->node_tbl = &_ico_node_table[0];  /* set default node table (display no=0)    */
914     wl_list_init(&usurf->ivi_layer);
915     wl_list_init(&usurf->client_link);
916     wl_list_init(&usurf->animation.animation.link);
917     wl_list_init(&usurf->surf_map);
918     usurf->animation.hide_anima = ico_get_animation_name(ico_ivi_default_animation_name());
919     usurf->animation.hide_time = ico_ivi_default_animation_time();;
920     usurf->animation.show_anima = usurf->animation.hide_anima;
921     usurf->animation.show_time = usurf->animation.hide_time;
922     usurf->animation.move_anima = usurf->animation.hide_anima;
923     usurf->animation.move_time = usurf->animation.hide_time;
924     usurf->animation.resize_anima = usurf->animation.hide_anima;
925     usurf->animation.resize_time = usurf->animation.hide_time;
926
927     if ((_ico_win_mgr->num_manager <= 0) ||
928         (ico_ivi_debugflag() & ICO_IVI_DEBUG_SHOW_SURFACE)) {
929         uifw_trace("win_mgr_register_surface: No Manager, Force visible");
930         usurf->visible = 1;
931     }
932     else    {
933         uifw_trace("win_mgr_register_surface: Manager exist, Not visible");
934         usurf->visible = 0;
935     }
936
937     /* set client                           */
938     usurf->uclient = find_client_from_client(client);
939     if (! usurf->uclient)  {
940         /* client not exist, create client management table */
941         uifw_trace("win_mgr_register_surface: Create Client");
942         win_mgr_bind_client(client, NULL);
943         usurf->uclient = find_client_from_client(client);
944         if (! usurf->uclient)  {
945             uifw_error("win_mgr_register_surface: No Memory");
946             return;
947         }
948     }
949     wl_list_insert(usurf->uclient->surface_link.prev, &usurf->client_link);
950
951     /* make surface id hash table       */
952     hash = MAKE_IDHASH(usurf->surfaceid);
953     phash = _ico_win_mgr->idhash[hash];
954     bhash = NULL;
955     while (phash)   {
956         bhash = phash;
957         phash = phash->next_idhash;
958     }
959     if (bhash)  {
960         bhash->next_idhash = usurf;
961     }
962     else    {
963         _ico_win_mgr->idhash[hash] = usurf;
964     }
965
966     /* make weston surface hash table   */
967     hash = MAKE_WSHASH(usurf->surface);
968     phash = _ico_win_mgr->wshash[hash];
969     bhash = NULL;
970     while (phash)   {
971         bhash = phash;
972         phash = phash->next_wshash;
973     }
974     if (bhash)  {
975         bhash->next_wshash = usurf;
976     }
977     else    {
978         _ico_win_mgr->wshash[hash] = usurf;
979     }
980     /* set default layer id             */
981     win_mgr_set_layer(usurf, (_ico_win_mgr->num_manager > 0) ? _ico_ivi_default_layer :
982                                                                _ico_ivi_startup_layer);
983     uifw_trace("win_mgr_register_surface: Leave(surfaceId=%08x)", usurf->surfaceid);
984 }
985
986 /*--------------------------------------------------------------------------*/
987 /**
988  * @brief   win_mgr_map_surface: map surface
989  *
990  * @param[in]   surface         Weston surface
991  * @param[in]   width           surface width
992  * @param[in]   height          surface height
993  * @param[in]   sx              X coordinate on screen
994  * @param[in]   sy              Y coordinate on screen
995  * @return      none
996  */
997 /*--------------------------------------------------------------------------*/
998 static void
999 win_mgr_map_surface(struct weston_surface *surface, int32_t *width, int32_t *height,
1000                     int32_t *sx, int32_t *sy)
1001 {
1002     struct uifw_win_surface *usurf;
1003
1004     uifw_trace("win_mgr_map_surface: Enter(%08x, x/y=%d/%d w/h=%d/%d)",
1005                (int)surface, *sx, *sy, *width, *height);
1006
1007     usurf = find_uifw_win_surface_by_ws(surface);
1008
1009     if (usurf) {
1010         uifw_trace("win_mgr_map_surface: surf=%08x w/h=%d/%d vis=%d",
1011                    usurf->surfaceid, usurf->width, usurf->height, usurf->visible);
1012         if ((usurf->width > 0) && (usurf->height > 0)) {
1013             uifw_trace("win_mgr_map_surface: HomeScreen registed, PositionSize"
1014                        "(surf=%08x x/y=%d/%d w/h=%d/%d vis=%d",
1015                        usurf->surfaceid, usurf->x, usurf->y, usurf->width, usurf->height,
1016                        usurf->visible);
1017             *width = usurf->width;
1018             *height = usurf->height;
1019             win_mgr_surface_configure(usurf, usurf->node_tbl->disp_x + usurf->x,
1020                                       usurf->node_tbl->disp_y + usurf->y,
1021                                       usurf->width, usurf->height);
1022         }
1023         else    {
1024             uifw_trace("win_mgr_map_surface: HomeScreen not regist Surface, "
1025                        "Change PositionSize(surf=%08x x/y=%d/%d w/h=%d/%d)",
1026                        usurf->surfaceid, *sx, *sy, *width, *height);
1027             usurf->width = *width;
1028             usurf->height = *height;
1029             usurf->x = *sx;
1030             usurf->y = *sy;
1031             if (usurf->x < 0)   usurf->x = 0;
1032             if (usurf->y < 0)   usurf->y = 0;
1033
1034             if (_ico_win_mgr->num_manager > 0)  {
1035                 /* HomeScreen exist, coodinate set by HomeScreen                */
1036                 if (usurf->visible) {
1037                     win_mgr_surface_configure(usurf, usurf->node_tbl->disp_x + usurf->x,
1038                                               usurf->node_tbl->disp_y + usurf->y,
1039                                               usurf->width, usurf->height);
1040                 }
1041                 else    {
1042                     win_mgr_surface_configure(usurf, ICO_IVI_MAX_COORDINATE+1,
1043                                               ICO_IVI_MAX_COORDINATE+1,
1044                                               usurf->width, usurf->height);
1045                 }
1046                 uifw_trace("win_mgr_map_surface: Change size/position x/y=%d/%d w/h=%d/%d",
1047                            (int)surface->geometry.x, (int)surface->geometry.y,
1048                            surface->geometry.width, surface->geometry.height);
1049             }
1050             if ((_ico_win_mgr->num_manager <= 0) ||
1051                 (ico_ivi_debugflag() & ICO_IVI_DEBUG_SHOW_SURFACE)) {
1052                 uifw_trace("win_mgr_map_surface: Np HomeScreen, chaneg to Visible");
1053                 ico_window_mgr_set_visible(usurf, 1);
1054             }
1055         }
1056         usurf->mapped = 1;
1057         if (usurf->visible) {
1058             ico_window_mgr_restack_layer(NULL, FALSE);
1059         }
1060         uifw_trace("win_mgr_map_surface: Leave");
1061     }
1062     else    {
1063         uifw_trace("win_mgr_map_surface: Leave(No UIFW Surface)");
1064     }
1065 }
1066
1067 /*--------------------------------------------------------------------------*/
1068 /**
1069  * @brief   ico_window_mgr_restack_layer: restack surface list
1070  *
1071  * @param[in]   usurf           UIFW surface (if NULL, no surface)
1072  * @param[in]   omit_touch      omit touch layer flag (TRUE=omit/FALSE=not omit)
1073  * @return      none
1074  */
1075 /*--------------------------------------------------------------------------*/
1076 WL_EXPORT   void
1077 ico_window_mgr_restack_layer(struct uifw_win_surface *usurf, const int omit_touch)
1078 {
1079     struct uifw_win_surface  *es;
1080     struct uifw_win_layer *el;
1081     int32_t buf_width, buf_height;
1082     float   new_x, new_y;
1083     struct weston_layer *wlayer;
1084     int     num_visible = 0;
1085
1086     uifw_trace("ico_window_mgr_restack_layer: Enter(surf=%08x,omit=%d)",
1087                (int)usurf, omit_touch);
1088
1089     /* make compositor surface list     */
1090     wlayer = ico_ivi_shell_current_layer();
1091
1092     wl_list_init(&wlayer->surface_list);
1093     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
1094         wl_list_for_each (es, &el->surface_list, ivi_layer) {
1095             if ((es->mapped != 0) && (es->surface != NULL))    {
1096                 if ((el->visible == FALSE) || (es->visible == FALSE) ||
1097                     ((omit_touch != FALSE) &&
1098                      ((el->layer_type == ICO_WINDOW_MGR_LAYER_TYPE_INPUT) ||
1099                       (el->layer_type == ICO_WINDOW_MGR_LAYER_TYPE_CURSOR))))   {
1100                     new_x = (float)(ICO_IVI_MAX_COORDINATE+1);
1101                     new_y = (float)(ICO_IVI_MAX_COORDINATE+1);
1102                 }
1103                 else if (es->surface->buffer_ref.buffer)   {
1104                     buf_width = weston_surface_buffer_width(es->surface);
1105                     buf_height = weston_surface_buffer_height(es->surface);
1106                     if (es->width > buf_width) {
1107                         new_x = (float)(es->x +
1108                                 (es->width - es->surface->geometry.width)/2);
1109                     }
1110                     else    {
1111                         new_x = (float)es->x;
1112                     }
1113                     if (es->height > buf_height) {
1114                         new_y = (float) (es->y +
1115                                 (es->height - es->surface->geometry.height)/2);
1116                     }
1117                     else    {
1118                         new_y = (float)es->y;
1119                     }
1120                     new_x += es->node_tbl->disp_x + es->xadd;
1121                     new_y += es->node_tbl->disp_y + es->yadd;
1122                     num_visible ++;
1123                 }
1124                 else    {
1125                     new_x = (float)(ICO_IVI_MAX_COORDINATE+1);
1126                     new_y = (float)(ICO_IVI_MAX_COORDINATE+1);
1127                 }
1128                 wl_list_insert(wlayer->surface_list.prev, &es->surface->layer_link);
1129                 if ((new_x != es->surface->geometry.x) ||
1130                     (new_y != es->surface->geometry.y)) {
1131                     weston_surface_damage_below(es->surface);
1132                     weston_surface_set_position(es->surface, (float)new_x, (float)new_y);
1133                     weston_surface_damage(es->surface);
1134                 }
1135 #if 0           /* too many debug log   */
1136                 uifw_trace("ico_window_mgr_restack_layer: %08x x/y=%d/%d w/h=%d/%d",
1137                            es->surfaceid,
1138                            (int)es->surface->geometry.x, (int)es->surface->geometry.y,
1139                            es->surface->geometry.width, es->surface->geometry.height);
1140 #endif
1141             }
1142         }
1143     }
1144
1145     /* damage(redraw) target surfacem if target exist   */
1146     if (usurf) {
1147         weston_surface_damage(usurf->surface);
1148     }
1149
1150     /* composit and draw screen(plane)  */
1151     if (omit_touch == FALSE)    {
1152         weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
1153
1154         if ((_ico_win_mgr->shell_init == 0) && (num_visible > 0) &&
1155             (_ico_win_mgr->shell != NULL) && (_ico_win_mgr->num_manager > 0))   {
1156             /* start shell fade         */
1157             _ico_win_mgr->shell_init = 1;
1158             ico_ivi_shell_startup(_ico_win_mgr->shell);
1159         }
1160     }
1161     uifw_trace("ico_window_mgr_restack_layer: Leave");
1162 }
1163
1164 /*--------------------------------------------------------------------------*/
1165 /**
1166  * @brief   win_mgr_create_layer: create new layer
1167  *
1168  * @param[in]   usurf       UIFW surface, (if need)
1169  * @param[in]   layer       layer id
1170  * @return      new layer
1171  * @retval      != NULL     success(layer management table)
1172  * @retval      == NULL     error(No Memory)
1173  */
1174 /*--------------------------------------------------------------------------*/
1175 static struct uifw_win_layer *
1176 win_mgr_create_layer(struct uifw_win_surface *usurf, const uint32_t layer)
1177 {
1178     struct uifw_win_layer *el;
1179     struct uifw_win_layer *new_el;
1180
1181     new_el = malloc(sizeof(struct uifw_win_layer));
1182     if (! new_el)   {
1183         uifw_trace("win_mgr_create_layer: Leave(No Memory)");
1184         return NULL;
1185     }
1186
1187     memset(new_el, 0, sizeof(struct uifw_win_layer));
1188     new_el->layer = layer;
1189     if ((int)layer == _ico_ivi_background_layer )   {
1190         new_el->layer_type = ICO_WINDOW_MGR_LAYER_TYPE_BACKGROUND;
1191     }
1192     else if ((int)layer == _ico_ivi_input_layer )   {
1193         new_el->layer_type = ICO_WINDOW_MGR_LAYER_TYPE_INPUT;
1194     }
1195     else if ((int)layer == _ico_ivi_cursor_layer )  {
1196         new_el->layer_type = ICO_WINDOW_MGR_LAYER_TYPE_CURSOR;
1197     }
1198     else    {
1199         new_el->layer_type = ICO_WINDOW_MGR_LAYER_TYPE_NORMAL;
1200     }
1201     new_el->visible = TRUE;
1202     wl_list_init(&new_el->surface_list);
1203     wl_list_init(&new_el->link);
1204
1205     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
1206         if (layer >= el->layer) break;
1207     }
1208     if (&el->link == &_ico_win_mgr->ivi_layer_list)    {
1209         wl_list_insert(_ico_win_mgr->ivi_layer_list.prev, &new_el->link);
1210     }
1211     else    {
1212         wl_list_insert(el->link.prev, &new_el->link);
1213     }
1214
1215     if (usurf)  {
1216         wl_list_remove(&usurf->ivi_layer);
1217         wl_list_insert(&new_el->surface_list, &usurf->ivi_layer);
1218         usurf->win_layer = new_el;
1219     }
1220     return new_el;
1221 }
1222
1223 /*--------------------------------------------------------------------------*/
1224 /**
1225  * @brief   win_mgr_set_layer: set(or change) surface layer
1226  *
1227  * @param[in]   usurf       UIFW surface
1228  * @param[in]   layer       layer id
1229  * @return      none
1230  */
1231 /*--------------------------------------------------------------------------*/
1232 static void
1233 win_mgr_set_layer(struct uifw_win_surface *usurf, const uint32_t layer)
1234 {
1235     struct uifw_win_layer *el;
1236     struct uifw_win_layer *new_el;
1237
1238     uifw_trace("win_mgr_set_layer: Enter([%08x],%08x,%x)",
1239                (int)usurf, (int)usurf->surface, layer);
1240
1241     /* check if same layer                      */
1242     if ((usurf->win_layer != NULL) && (usurf->win_layer->layer == layer))   {
1243         uifw_trace("win_mgr_set_layer: Leave(Same Layer)");
1244         return;
1245     }
1246
1247     /* search existing layer                    */
1248     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
1249         if (el->layer == layer) break;
1250     }
1251
1252     if (&el->link == &_ico_win_mgr->ivi_layer_list)    {
1253         /* layer not exist, create new layer    */
1254         uifw_trace("win_mgr_set_layer: New Layer %d", layer);
1255         new_el = win_mgr_create_layer(usurf, layer);
1256         if (! new_el)   {
1257             uifw_trace("win_mgr_set_layer: Leave(No Memory)");
1258             return;
1259         }
1260     }
1261     else    {
1262         uifw_trace("win_mgr_set_layer: Add surface to Layer %d", layer);
1263         wl_list_remove(&usurf->ivi_layer);
1264         wl_list_insert(&el->surface_list, &usurf->ivi_layer);
1265         usurf->win_layer = el;
1266     }
1267
1268     /* rebild compositor surface list       */
1269     if (usurf->visible) {
1270         ico_window_mgr_restack_layer(usurf, 0);
1271     }
1272     uifw_trace("win_mgr_set_layer: Leave");
1273 }
1274
1275 /*--------------------------------------------------------------------------*/
1276 /**
1277  * @brief   win_mgr_set_active: set(or change) active surface
1278  *
1279  * @param[in]   usurf       UIFW surface
1280  * @param[in]   target      target device
1281  * @return      none
1282  */
1283 /*--------------------------------------------------------------------------*/
1284 static void
1285 win_mgr_set_active(struct uifw_win_surface *usurf, const int target)
1286 {
1287     struct weston_seat *seat;
1288     struct weston_surface *surface;
1289     int object = target;
1290     wl_fixed_t sx, sy;
1291
1292     uifw_trace("win_mgr_set_active: Enter(%08x,%x)", (int)usurf, target);
1293
1294     if ((usurf) && (usurf->shsurf) && (usurf->surface)) {
1295         surface = usurf->surface;
1296         if ((target & (ICO_WINDOW_MGR_ACTIVE_POINTER|ICO_WINDOW_MGR_ACTIVE_KEYBOARD)) == 0) {
1297             surface = NULL;
1298             if (_ico_win_mgr->active_pointer_usurf == usurf) {
1299                 object |= ICO_WINDOW_MGR_ACTIVE_POINTER;
1300             }
1301             if (_ico_win_mgr->active_keyboard_usurf == usurf)    {
1302                 object |= ICO_WINDOW_MGR_ACTIVE_KEYBOARD;
1303             }
1304         }
1305         else    {
1306             if (object & ICO_WINDOW_MGR_ACTIVE_POINTER) {
1307                 _ico_win_mgr->active_pointer_usurf = usurf;
1308             }
1309             if (object & ICO_WINDOW_MGR_ACTIVE_KEYBOARD)    {
1310                 _ico_win_mgr->active_keyboard_usurf = usurf;
1311             }
1312         }
1313     }
1314     else    {
1315         surface = NULL;
1316         if (target == 0)    {
1317             object = ICO_WINDOW_MGR_ACTIVE_POINTER | ICO_WINDOW_MGR_ACTIVE_KEYBOARD;
1318         }
1319         if (object & ICO_WINDOW_MGR_ACTIVE_POINTER) {
1320             _ico_win_mgr->active_pointer_usurf = NULL;
1321         }
1322         if (object & ICO_WINDOW_MGR_ACTIVE_KEYBOARD)    {
1323             _ico_win_mgr->active_keyboard_usurf = NULL;
1324         }
1325     }
1326
1327     wl_list_for_each(seat, &_ico_win_mgr->compositor->seat_list, link) {
1328         if ((object & ICO_WINDOW_MGR_ACTIVE_POINTER) && (seat->pointer))   {
1329             if (surface)    {
1330                 if (seat->pointer->focus != surface) {
1331                     uifw_trace("win_mgr_set_active: pointer change surface(%08x=>%08x)",
1332                                (int)seat->pointer->focus, (int)surface);
1333                     weston_surface_from_global_fixed(surface,
1334                                                      seat->pointer->x,
1335                                                      seat->pointer->y,
1336                                                      &sx, &sy);
1337                     weston_pointer_set_focus(seat->pointer, surface, sx, sy);
1338                 }
1339                 else    {
1340                     uifw_trace("win_mgr_set_active: pointer nochange surface(%08x)",
1341                                (int)surface);
1342                 }
1343             }
1344             else    {
1345                 uifw_trace("win_mgr_set_active: pointer reset surface(%08x)",
1346                            (int)seat->pointer->focus);
1347                 weston_pointer_set_focus(seat->pointer, NULL,
1348                                          wl_fixed_from_int(0), wl_fixed_from_int(0));
1349             }
1350         }
1351         if ((object & ICO_WINDOW_MGR_ACTIVE_KEYBOARD) && (seat->keyboard))  {
1352             if (surface)    {
1353                 if (seat->keyboard->focus != surface)    {
1354                     weston_keyboard_set_focus(seat->keyboard, surface);
1355                     uifw_trace("win_mgr_set_active: keyboard change surface(%08x=>%08x)",
1356                                (int)seat->keyboard->focus, (int)surface);
1357                 }
1358                 else    {
1359                     uifw_trace("win_mgr_set_active: keyboard nochange surface(%08x)",
1360                                (int)surface);
1361                 }
1362             }
1363             else    {
1364                 uifw_trace("win_mgr_set_active: keyboard reset surface(%08x)",
1365                            (int)seat->keyboard);
1366                 weston_keyboard_set_focus(seat->keyboard, NULL);
1367             }
1368         }
1369     }
1370     uifw_trace("win_mgr_set_active: Leave(%08x)", (int)usurf);
1371 }
1372
1373 /*--------------------------------------------------------------------------*/
1374 /**
1375  * @brief   uifw_declare_manager: declare manager(ex.SystemController) client
1376  *
1377  * @param[in]   client      Weyland client
1378  * @param[in]   resource    resource of request
1379  * @param[in]   manager     manager(1=manager, 0=not manager)
1380  * @return      none
1381  */
1382 /*--------------------------------------------------------------------------*/
1383 static void
1384 uifw_declare_manager(struct wl_client *client, struct wl_resource *resource, int manager)
1385 {
1386     struct uifw_manager* mgr;
1387     struct uifw_win_surface *usurf;
1388     struct uifw_client *uclient;
1389     struct uifw_win_layer *el;
1390
1391     uifw_trace("uifw_declare_manager: Enter client=%08x manager=%d",
1392                (int)client, manager);
1393
1394     uclient = find_client_from_client(client);
1395     if (! uclient)  {
1396         uifw_trace("uifw_declare_manager: Leave(unknown client=%08x)", (int)client);
1397         return;
1398     }
1399
1400     uclient->manager = manager;
1401
1402     /* client set to manager            */
1403     _ico_win_mgr->num_manager = 0;
1404     wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
1405         if (mgr->resource == resource)  {
1406             if (mgr->manager != manager)    {
1407                 uifw_trace("uifw_declare_manager: Event Client.%08x Callback %d=>%d",
1408                            (int)client, mgr->manager, manager);
1409                 mgr->manager = manager;
1410
1411                 if (manager)    {
1412                     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
1413                         wl_list_for_each (usurf, &el->surface_list, ivi_layer) {
1414                             /* send window create event to manager  */
1415                             if (usurf->created != 0)    {
1416                                 uifw_trace("uifw_declare_manager: Send manager(%08x) "
1417                                            "WINDOW_CREATED(surf=%08x,pid=%d,appid=%s)",
1418                                            (int)resource, usurf->surfaceid,
1419                                            usurf->uclient->pid, usurf->uclient->appid);
1420                                 ico_window_mgr_send_window_created(resource,
1421                                                                    usurf->surfaceid,
1422                                                                    usurf->winname,
1423                                                                    usurf->uclient->pid,
1424                                                                    usurf->uclient->appid);
1425                             }
1426                         }
1427                     }
1428                 }
1429             }
1430         }
1431         if (mgr->manager)   {
1432             _ico_win_mgr->num_manager++;
1433         }
1434     }
1435     uifw_trace("uifw_declare_manager: Leave(managers=%d)", _ico_win_mgr->num_manager);
1436 }
1437
1438 /*--------------------------------------------------------------------------*/
1439 /**
1440  * @brief   uifw_set_window_layer: set layer id to surface
1441  *
1442  * @param[in]   client      Weyland client
1443  * @param[in]   resource    resource of request
1444  * @param[in]   surfaceid   UIFW surface id
1445  * @param[in]   layer       layer id
1446  * @return      none
1447  */
1448 /*--------------------------------------------------------------------------*/
1449 static void
1450 uifw_set_window_layer(struct wl_client *client, struct wl_resource *resource,
1451                       uint32_t surfaceid, uint32_t layer)
1452 {
1453     uifw_trace("uifw_set_window_layer: Enter res=%08x surfaceid=%08x layer=%d",
1454                (int)resource, surfaceid, layer);
1455
1456     struct uifw_win_surface *usurf = ico_window_mgr_get_usurf(surfaceid);
1457
1458     if (! usurf)    {
1459         uifw_trace("uifw_set_window_layer: Leave(No Surface(id=%08x))", surfaceid);
1460         return;
1461     }
1462     if (usurf->win_layer->layer != layer) {
1463         win_mgr_set_layer(usurf, layer);
1464
1465         win_mgr_change_surface(usurf->surface, -1, 1);
1466     }
1467     uifw_trace("uifw_set_window_layer: Leave");
1468 }
1469
1470 /*--------------------------------------------------------------------------*/
1471 /**
1472  * @brief   uifw_set_positionsize: set surface position and size
1473  *
1474  * @param[in]   client      Weyland client
1475  * @param[in]   resource    resource of request
1476  * @param[in]   surfaceid   UIFW surface id
1477  * @param[in]   node        surface node id
1478  * @param[in]   x           X coordinate on screen(if bigger than 16383, no change)
1479  * @param[in]   y           Y coordinate on screen(if bigger than 16383, no change)
1480  * @param[in]   width       surface width(if bigger than 16383, no change)
1481  * @param[in]   height      surface height(if bigger than 16383, no change)
1482  * @param[in]   flags       with/without animation and client configure flag
1483  * @return      none
1484  */
1485 /*--------------------------------------------------------------------------*/
1486 static void
1487 uifw_set_positionsize(struct wl_client *client, struct wl_resource *resource,
1488                       uint32_t surfaceid, uint32_t node, int32_t x, int32_t y,
1489                       int32_t width, int32_t height, int32_t flags)
1490 {
1491     struct uifw_client *uclient;
1492     struct weston_surface *es;
1493     int     op;
1494     int     retanima;
1495
1496     uifw_trace("uifw_set_positionsize: Enter surf=%08x node=%x x/y/w/h=%d/%d/%d/%d flag=%x",
1497                surfaceid, node, x, y, width, height, flags);
1498
1499     if (((int)node) >= _ico_num_nodes)  {
1500         uifw_trace("uifw_set_positionsize: node=%d dose not exist(max=%d)",
1501                    node, _ico_num_nodes);
1502         node = 0;
1503     }
1504     struct uifw_win_surface *usurf = ico_window_mgr_get_usurf(surfaceid);
1505
1506     if (usurf && (usurf->surface))  {
1507         /* weston surface exist             */
1508         usurf->node_tbl = &_ico_node_table[node];
1509         es = usurf->surface;
1510         retanima = ICO_WINDOW_MGR_ANIMATION_RET_NOANIMA;
1511
1512         /* if x,y,width,height bigger then ICO_IVI_MAX_COORDINATE, no change    */
1513         if (x > ICO_IVI_MAX_COORDINATE)         x = usurf->x;
1514         if (y > ICO_IVI_MAX_COORDINATE)         y = usurf->y;
1515         if (width > ICO_IVI_MAX_COORDINATE)     width = usurf->width;
1516         if (height > ICO_IVI_MAX_COORDINATE)    height = usurf->height;
1517
1518         /* check animation                  */
1519         if ((usurf->animation.restrain_configure != 0) &&
1520             (x == usurf->x) && (y == usurf->y) &&
1521             (width == usurf->width) && (height == usurf->height))   {
1522             uifw_trace("uifw_set_positionsize: Leave(same position size at animation)");
1523             return;
1524         }
1525
1526         uclient = find_client_from_client(client);
1527         if (uclient)    {
1528             if (! uclient->manager) uclient = NULL;
1529         }
1530         if (! uclient)  {
1531             if ((usurf->width > 0) && (usurf->height > 0))  {
1532                 win_mgr_surface_change_mgr(es, x, y, width, height);
1533                 uifw_trace("uifw_set_positionsize: Leave(Request from App)");
1534                 return;
1535             }
1536
1537             uifw_trace("uifw_set_positionsize: Initial Position/Size visible=%d",
1538                        usurf->visible);
1539             /* Initiale position is (0,0)   */
1540             weston_surface_set_position(es, (float)(usurf->node_tbl->disp_x),
1541                                         (float)(usurf->node_tbl->disp_y));
1542         }
1543
1544         uifw_trace("uifw_set_positionsize: Old geometry x/y=%d/%d,w/h=%d/%d",
1545                    (int)es->geometry.x, (int)es->geometry.y,
1546                    (int)es->geometry.width, (int)es->geometry.height);
1547
1548         usurf->animation.pos_x = usurf->x;
1549         usurf->animation.pos_y = usurf->y;
1550         usurf->animation.pos_width = usurf->width;
1551         usurf->animation.pos_height = usurf->height;
1552         usurf->animation.no_configure = (flags & ICO_WINDOW_MGR_FLAGS_NO_CONFIGURE) ? 1 : 0;
1553         usurf->x = x;
1554         usurf->y = y;
1555         usurf->width = width;
1556         usurf->height = height;
1557         if (_ico_win_mgr->num_manager <= 0) {
1558             /* no manager(HomeScreen), set geometory    */
1559             weston_surface_set_position(es, (float)(usurf->node_tbl->disp_x + x),
1560                                         (float)(usurf->node_tbl->disp_y + y));
1561         }
1562         if ((es->output) && (es->buffer_ref.buffer) &&
1563             (es->geometry.width > 0) && (es->geometry.height > 0)) {
1564             uifw_trace("uifw_set_positionsize: Fixed Geometry, Change(Vis=%d)",
1565                        usurf->visible);
1566             if (usurf->visible) {
1567                 if ((flags & ICO_WINDOW_MGR_FLAGS_ANIMATION) &&
1568                     (win_mgr_hook_animation != NULL))   {
1569                     /* with animation   */
1570                     if ((x != (usurf->surface->geometry.x - usurf->node_tbl->disp_x)) ||
1571                         (y != (usurf->surface->geometry.y - usurf->node_tbl->disp_y)))  {
1572                         op = ICO_WINDOW_MGR_ANIMATION_OPMOVE;
1573                     }
1574                     else if ((width != usurf->surface->geometry.width) ||
1575                              (height != usurf->surface->geometry.height))   {
1576                         op = ICO_WINDOW_MGR_ANIMATION_OPRESIZE;
1577                     }
1578                     else    {
1579                         op = ICO_WINDOW_MGR_ANIMATION_OPNONE;
1580                     }
1581                     if (((op == ICO_WINDOW_MGR_ANIMATION_OPMOVE) &&
1582                          (usurf->animation.move_anima != ICO_WINDOW_MGR_ANIMATION_NONE)) ||
1583                         ((op == ICO_WINDOW_MGR_ANIMATION_OPRESIZE) &&
1584                          (usurf->animation.resize_anima != ICO_WINDOW_MGR_ANIMATION_NONE))) {
1585                         retanima = (*win_mgr_hook_animation)(op, (void *)usurf);
1586                         uifw_trace("uifw_set_positionsize: ret call anima = %d", retanima);
1587                     }
1588                 }
1589                 if ((retanima == ICO_WINDOW_MGR_ANIMATION_RET_NOANIMA) ||
1590                     (retanima != ICO_WINDOW_MGR_ANIMATION_RET_ANIMANOCTL))  {
1591                     ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
1592                                                       usurf->width, usurf->height);
1593                 }
1594             }
1595         }
1596         if ((retanima == ICO_WINDOW_MGR_ANIMATION_RET_NOANIMA) ||
1597             (retanima != ICO_WINDOW_MGR_ANIMATION_RET_ANIMANOCTL))  {
1598             win_mgr_change_surface(es,
1599                                    (flags & ICO_WINDOW_MGR_FLAGS_NO_CONFIGURE) ? -1 : 0, 1);
1600         }
1601         uifw_trace("uifw_set_positionsize: Leave(OK,output=%08x)", (int)es->output);
1602     }
1603     else    {
1604         uifw_trace("uifw_set_positionsize: Leave(surf=%08x NOT Found)", surfaceid);
1605     }
1606 }
1607
1608 /*--------------------------------------------------------------------------*/
1609 /**
1610  * @brief   uifw_set_visible: surface visible/raise control
1611  *
1612  * @param[in]   client      Weyland client
1613  * @param[in]   resource    resource of request
1614  * @param[in]   surfaceid   UIFW surface id
1615  * @param[in]   visible     visible(1=show/0=hide/other=no change)
1616  * @param[in]   raise       raise(1=raise/0=lower/other=no change)
1617  * @param[in]   flags       with/without animation
1618  * @return      none
1619  */
1620 /*--------------------------------------------------------------------------*/
1621 static void
1622 uifw_set_visible(struct wl_client *client, struct wl_resource *resource,
1623                  uint32_t surfaceid, int32_t visible, int32_t raise, int32_t flags)
1624 {
1625     struct uifw_win_surface *usurf;
1626     struct uifw_client *uclient;
1627     int     restack;
1628     int     retanima;
1629
1630     uifw_trace("uifw_set_visible: Enter(surf=%08x,%d,%d,%x)",
1631                surfaceid, visible, raise, flags);
1632
1633     uclient = find_client_from_client(client);
1634     if (uclient)    {
1635         if (! uclient->manager) {
1636             uifw_trace("uifw_set_visible: Request from App(%s), not Manager",
1637                        uclient->appid);
1638             uclient = NULL;
1639         }
1640         else    {
1641             uifw_trace("uifw_set_visible: Request from Manager(%s)", uclient->appid);
1642         }
1643     }
1644     else    {
1645         uifw_trace("uifw_set_visible: Request from Unknown App, not Manager");
1646     }
1647
1648     usurf = ico_window_mgr_get_usurf(surfaceid);
1649
1650     if ((! usurf) || (! usurf->surface))    {
1651         uifw_trace("uifw_set_visible: Leave(Surface Not Exist)");
1652         return;
1653     }
1654     restack = 0;
1655
1656     if (visible == ICO_WINDOW_MGR_VISIBLE_SHOW) {
1657
1658         if (! usurf->visible)  {
1659             usurf->visible = 1;
1660             uifw_trace("uifw_set_visible: Change to Visible");
1661
1662             ico_ivi_shell_set_toplevel(usurf->shsurf);
1663
1664             /* Weston surface configure                     */
1665             uifw_trace("uifw_set_visible: Visible to Weston WSurf=%08x,%d.%d/%d/%d/%d",
1666                        (int)usurf->surface, usurf->node_tbl->node, usurf->x, usurf->y,
1667                        usurf->width, usurf->height);
1668             ico_ivi_shell_set_surface_type(usurf->shsurf);
1669             ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
1670                                               usurf->width, usurf->height);
1671
1672             restack = 1;                    /* need damage      */
1673
1674             if ((flags & (ICO_WINDOW_MGR_ANIMATION_POS|ICO_WINDOW_MGR_FLAGS_ANIMATION)) &&
1675                 (usurf->animation.show_anima != ICO_WINDOW_MGR_ANIMATION_NONE) &&
1676                 (win_mgr_hook_animation != NULL))   {
1677                 usurf->animation.pos_x = usurf->x;
1678                 usurf->animation.pos_y = usurf->y;
1679                 usurf->animation.pos_width = usurf->width;
1680                 usurf->animation.pos_height = usurf->height;
1681                 usurf->animation.no_configure = 0;
1682                 retanima = (*win_mgr_hook_animation)(
1683                                 (flags & ICO_WINDOW_MGR_ANIMATION_POS) ?
1684                                     ICO_WINDOW_MGR_ANIMATION_OPSHOWPOS :
1685                                     ICO_WINDOW_MGR_ANIMATION_OPSHOW,
1686                                 (void *)usurf);
1687                 uifw_trace("uifw_set_visible: ret call anima = %d", retanima);
1688             }
1689         }
1690         else if ((raise != ICO_WINDOW_MGR_RAISE_LOWER) &&
1691                  (raise != ICO_WINDOW_MGR_RAISE_RAISE))  {
1692             uifw_trace("uifw_set_visible: Leave(No Change)");
1693             return;
1694         }
1695     }
1696     else if (visible == ICO_WINDOW_MGR_VISIBLE_HIDE)    {
1697
1698         if (usurf->visible)    {
1699
1700             /* Weston surface configure                     */
1701             weston_surface_damage_below(usurf->surface);
1702             ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
1703                                               usurf->width, usurf->height);
1704
1705             retanima = ICO_WINDOW_MGR_ANIMATION_RET_ANIMA;
1706             if ((flags & (ICO_WINDOW_MGR_FLAGS_ANIMATION|ICO_WINDOW_MGR_ANIMATION_POS)) &&
1707                 (usurf->animation.hide_anima != ICO_WINDOW_MGR_ANIMATION_NONE) &&
1708                 (win_mgr_hook_animation != NULL))   {
1709                 usurf->animation.pos_x = usurf->x;
1710                 usurf->animation.pos_y = usurf->y;
1711                 usurf->animation.pos_width = usurf->width;
1712                 usurf->animation.pos_height = usurf->height;
1713                 usurf->animation.no_configure = 0;
1714                 retanima = (*win_mgr_hook_animation)(
1715                                 (flags & ICO_WINDOW_MGR_ANIMATION_POS) ?
1716                                     ICO_WINDOW_MGR_ANIMATION_OPHIDEPOS :
1717                                     ICO_WINDOW_MGR_ANIMATION_OPHIDE,
1718                                 (void *)usurf);
1719             }
1720             if (retanima != ICO_WINDOW_MGR_ANIMATION_RET_ANIMANOCTL)    {
1721                 usurf->visible = 0;
1722                 uifw_trace("uifw_set_visible: Change to UnVisible");
1723                 /* change visible to unvisible, restack surface list    */
1724                 restack = 1;
1725                 /* Weston surface configure                     */
1726                 ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
1727                                                   usurf->width, usurf->height);
1728             }
1729             else    {
1730                 uifw_trace("uifw_set_visible: UnVisible but animation");
1731             }
1732         }
1733         else if ((raise != ICO_WINDOW_MGR_RAISE_LOWER) &&
1734                  (raise != ICO_WINDOW_MGR_RAISE_RAISE))  {
1735             uifw_trace("uifw_set_visible: Leave(No Change)");
1736             return;
1737         }
1738     }
1739     else if ((raise != ICO_WINDOW_MGR_RAISE_LOWER) &&
1740              (raise != ICO_WINDOW_MGR_RAISE_RAISE))  {
1741         uifw_trace("uifw_set_visible: Leave(No Change)");
1742         return;
1743     }
1744
1745     /* raise/lower                              */
1746     if ((raise == ICO_WINDOW_MGR_RAISE_LOWER) || (raise == ICO_WINDOW_MGR_RAISE_RAISE))  {
1747         win_mgr_set_raise(usurf, raise);
1748         if (usurf->visible == 0)    {
1749             restack |= 2;
1750         }
1751     }
1752     else    {
1753         raise = ICO_WINDOW_MGR_V_NOCHANGE;
1754     }
1755
1756     if ((restack == 1) && (usurf->surface) &&
1757         (usurf->surface->buffer_ref.buffer) && (usurf->surface->output))   {
1758         weston_surface_damage(usurf->surface);
1759     }
1760     else if(restack & 2)    {
1761         ico_window_mgr_restack_layer(usurf, 0);
1762     }
1763
1764     /* send event(VISIBLE) to manager           */
1765     if (restack)    {
1766         ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
1767                                 usurf,
1768                                 (visible == ICO_WINDOW_MGR_VISIBLE_SHOW) ? 1 :
1769                                     ((visible == ICO_WINDOW_MGR_VISIBLE_HIDE) ? 0 :
1770                                         ICO_WINDOW_MGR_V_NOCHANGE),
1771                                 raise, uclient ? 0 : 1, 0,0);
1772     }
1773     uifw_trace("uifw_set_visible: Leave(OK)");
1774 }
1775
1776 /*--------------------------------------------------------------------------*/
1777 /**
1778  * @brief   uifw_set_animation: set animation of surface visible/unvisible
1779  *
1780  * @param[in]   client      Weyland client
1781  * @param[in]   resource    resource of request
1782  * @param[in]   surfaceid   UIFW surface id
1783  * @param[in]   type        how to change surface
1784  * @param[in]   anmation    animation name
1785  * @param[in]   time        animation time(ms), if 0, default time
1786  * @return      none
1787  */
1788 /*--------------------------------------------------------------------------*/
1789 static void
1790 uifw_set_animation(struct wl_client *client, struct wl_resource *resource,
1791                    uint32_t surfaceid, int32_t type, const char *animation, int32_t time)
1792 {
1793     int animaid;
1794     struct uifw_win_surface *usurf = ico_window_mgr_get_usurf(surfaceid);
1795
1796     uifw_trace("uifw_set_transition: Enter(surf=%08x,type=%x,anim=%s,time=%d)",
1797                surfaceid, type, animation, time);
1798
1799     if (usurf) {
1800         if ((*animation != 0) && (*animation != ' '))   {
1801             animaid = ico_get_animation_name(animation);
1802             uifw_trace("uifw_set_animation: Leave(OK) type=%d", animaid);
1803             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_HIDE)  {
1804                 if ((usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPHIDE) ||
1805                     (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPHIDEPOS))  {
1806                     usurf->animation.next_anima = animaid;
1807                 }
1808                 else    {
1809                     usurf->animation.hide_anima = animaid;
1810                 }
1811             }
1812             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_SHOW)  {
1813                 if ((usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPSHOW) ||
1814                     (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPSHOWPOS))  {
1815                     usurf->animation.next_anima = animaid;
1816                 }
1817                 else    {
1818                     usurf->animation.show_anima = animaid;
1819                 }
1820             }
1821             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_MOVE)  {
1822                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPMOVE)    {
1823                     usurf->animation.next_anima = animaid;
1824                 }
1825                 else    {
1826                     usurf->animation.move_anima = animaid;
1827                 }
1828             }
1829             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_RESIZE)    {
1830                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPRESIZE)  {
1831                     usurf->animation.next_anima = animaid;
1832                 }
1833                 else    {
1834                     usurf->animation.resize_anima = animaid;
1835                 }
1836             }
1837         }
1838         if ((time > 0) && (time != ICO_WINDOW_MGR_V_NOCHANGE))  {
1839             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_HIDE)  {
1840                 usurf->animation.hide_time = time;
1841             }
1842             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_SHOW)  {
1843                 usurf->animation.show_time = time;
1844             }
1845             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_MOVE)  {
1846                 usurf->animation.move_time = time;
1847             }
1848             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_RESIZE)    {
1849                 usurf->animation.resize_time = time;
1850             }
1851         }
1852     }
1853     else    {
1854         uifw_trace("uifw_set_animation: Leave(Surface(%08x) Not exist)", surfaceid);
1855     }
1856 }
1857
1858 /*--------------------------------------------------------------------------*/
1859 /**
1860  * @brief   uifw_set_attributes: set surface attributes
1861  *
1862  * @param[in]   client      Weyland client
1863  * @param[in]   resource    resource of request
1864  * @param[in]   surfaceid   UIFW surface id
1865  * @param[in]   attributes  surface attributes
1866  * @return      none
1867  */
1868 /*--------------------------------------------------------------------------*/
1869 static void
1870 uifw_set_attributes(struct wl_client *client, struct wl_resource *resource,
1871                     uint32_t surfaceid, uint32_t attributes)
1872 {
1873     struct uifw_win_surface *usurf = ico_window_mgr_get_usurf(surfaceid);
1874
1875     uifw_trace("uifw_set_attributes: Enter(surf=%08x,attributes=%x)", surfaceid, attributes);
1876
1877     if (usurf) {
1878         usurf->attributes = attributes;
1879         if ((attributes & (ICO_WINDOW_MGR_ATTR_ALIGN_LEFT|ICO_WINDOW_MGR_ATTR_ALIGN_RIGHT)) ==
1880             (ICO_WINDOW_MGR_ATTR_ALIGN_LEFT|ICO_WINDOW_MGR_ATTR_ALIGN_RIGHT))   {
1881             usurf->attributes &=
1882                 ~(ICO_WINDOW_MGR_ATTR_ALIGN_LEFT|ICO_WINDOW_MGR_ATTR_ALIGN_RIGHT);
1883         }
1884         if ((attributes & (ICO_WINDOW_MGR_ATTR_ALIGN_TOP|ICO_WINDOW_MGR_ATTR_ALIGN_BOTTOM)) ==
1885             (ICO_WINDOW_MGR_ATTR_ALIGN_TOP|ICO_WINDOW_MGR_ATTR_ALIGN_BOTTOM))   {
1886             usurf->attributes &=
1887                 ~(ICO_WINDOW_MGR_ATTR_ALIGN_TOP|ICO_WINDOW_MGR_ATTR_ALIGN_BOTTOM);
1888         }
1889     }
1890     uifw_trace("uifw_set_attributes: Leave");
1891 }
1892
1893 /*--------------------------------------------------------------------------*/
1894 /**
1895  * @brief   uifw_visible_animation: surface visibility control with animation
1896  *
1897  * @param[in]   client      Weyland client
1898  * @param[in]   resource    resource of request
1899  * @param[in]   surfaceid   surface id
1900  * @param[in]   visible     visible(1=show/0=hide)
1901  * @param[in]   x           X coordinate on screen(if bigger than 16383, no change)
1902  * @param[in]   y           Y coordinate on screen(if bigger than 16383, no change)
1903  * @param[in]   width       surface width(if bigger than 16383, no change)
1904  * @param[in]   height      surface height(if bigger than 16383, no change)
1905  * @return      none
1906  */
1907 /*--------------------------------------------------------------------------*/
1908 static void
1909 uifw_visible_animation(struct wl_client *client, struct wl_resource *resource,
1910                        uint32_t surfaceid, int32_t visible,
1911                        int32_t x, int32_t y, int32_t width, int32_t height)
1912 {
1913     struct uifw_win_surface *usurf;
1914
1915     uifw_trace("uifw_visible_animation: Enter(%08x,%d,x/y=%d/%d,w/h=%d/%d)",
1916                surfaceid, visible, x, y, width, height);
1917
1918     usurf = ico_window_mgr_get_usurf(surfaceid);
1919
1920     if ((! usurf) || (! usurf->surface))    {
1921         uifw_trace("uifw_visible_animation: Leave(Surface Not Exist)");
1922         return;
1923     }
1924
1925     usurf->animation.pos_x = x;
1926     usurf->animation.pos_y = y;
1927     if (width > 0)  usurf->animation.pos_width = width;
1928     else            usurf->animation.pos_width = 1;
1929     if (height > 0) usurf->animation.pos_height = height;
1930     else            usurf->animation.pos_height = 1;
1931     usurf->animation.no_configure = 0;
1932
1933     uifw_set_visible(client, resource, surfaceid, visible,
1934                      ICO_WINDOW_MGR_V_NOCHANGE, ICO_WINDOW_MGR_ANIMATION_POS);
1935
1936     uifw_trace("uifw_visible_animation: Leave");
1937 }
1938
1939 /*--------------------------------------------------------------------------*/
1940 /**
1941  * @brief   uifw_set_active: set active surface
1942  *
1943  * @param[in]   client      Weyland client
1944  * @param[in]   resource    resource of request
1945  * @param[in]   surfaceid   UIFW surface id
1946  * @param[in]   active      target device
1947  * @return      none
1948  */
1949 /*--------------------------------------------------------------------------*/
1950 static void
1951 uifw_set_active(struct wl_client *client, struct wl_resource *resource,
1952                 uint32_t surfaceid, int32_t active)
1953 {
1954     struct uifw_win_surface *usurf;
1955
1956     uifw_trace("uifw_set_active: Enter(surf=%08x,active=%x)", surfaceid, active);
1957
1958     if ((surfaceid > 0) &&
1959         ((active & (ICO_WINDOW_MGR_ACTIVE_POINTER|ICO_WINDOW_MGR_ACTIVE_KEYBOARD)) != 0)) {
1960         usurf = ico_window_mgr_get_usurf(surfaceid);
1961     }
1962     else    {
1963         usurf = NULL;
1964     }
1965     if (usurf) {
1966         switch (active & (ICO_WINDOW_MGR_ACTIVE_POINTER|ICO_WINDOW_MGR_ACTIVE_KEYBOARD)) {
1967         case ICO_WINDOW_MGR_ACTIVE_POINTER:
1968             if (usurf != _ico_win_mgr->active_pointer_usurf)  {
1969                 if (_ico_win_mgr->active_pointer_usurf)   {
1970                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1971                                             _ico_win_mgr->active_pointer_usurf,
1972                                             (_ico_win_mgr->active_keyboard_usurf ==
1973                                              _ico_win_mgr->active_pointer_usurf) ?
1974                                                 ICO_WINDOW_MGR_ACTIVE_KEYBOARD :
1975                                                 ICO_WINDOW_MGR_ACTIVE_NONE,
1976                                             0,0,0,0);
1977                 }
1978                 _ico_win_mgr->active_pointer_usurf = usurf;
1979                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1980                                         usurf,
1981                                         ICO_WINDOW_MGR_ACTIVE_POINTER |
1982                                         (_ico_win_mgr->active_keyboard_usurf == usurf) ?
1983                                             ICO_WINDOW_MGR_ACTIVE_KEYBOARD : 0,
1984                                         0,0,0,0);
1985                 win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER);
1986             }
1987             break;
1988         case ICO_WINDOW_MGR_ACTIVE_KEYBOARD:
1989             if (usurf != _ico_win_mgr->active_keyboard_usurf) {
1990                 if (_ico_win_mgr->active_keyboard_usurf)   {
1991                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
1992                                             _ico_win_mgr->active_keyboard_usurf,
1993                                             (_ico_win_mgr->active_keyboard_usurf ==
1994                                              _ico_win_mgr->active_pointer_usurf) ?
1995                                                 ICO_WINDOW_MGR_ACTIVE_POINTER :
1996                                                 ICO_WINDOW_MGR_ACTIVE_NONE,
1997                                             0,0,0,0);
1998                 }
1999                 _ico_win_mgr->active_keyboard_usurf = usurf;
2000                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
2001                                         usurf,
2002                                         ICO_WINDOW_MGR_ACTIVE_KEYBOARD |
2003                                         (_ico_win_mgr->active_pointer_usurf == usurf) ?
2004                                             ICO_WINDOW_MGR_ACTIVE_POINTER : 0,
2005                                         0,0,0,0);
2006                 win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
2007             }
2008             break;
2009         default:
2010             if ((usurf != _ico_win_mgr->active_pointer_usurf) ||
2011                 (usurf != _ico_win_mgr->active_keyboard_usurf))   {
2012                 if (_ico_win_mgr->active_pointer_usurf)   {
2013                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
2014                                             _ico_win_mgr->active_pointer_usurf,
2015                                             ICO_WINDOW_MGR_ACTIVE_NONE,
2016                                             0,0,0,0);
2017                     if (_ico_win_mgr->active_keyboard_usurf ==
2018                         _ico_win_mgr->active_pointer_usurf)   {
2019                         _ico_win_mgr->active_keyboard_usurf = NULL;
2020                     }
2021                 }
2022                 if (_ico_win_mgr->active_keyboard_usurf)   {
2023                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
2024                                             _ico_win_mgr->active_keyboard_usurf,
2025                                             ICO_WINDOW_MGR_ACTIVE_NONE,
2026                                             0,0,0,0);
2027                 }
2028                 _ico_win_mgr->active_pointer_usurf = usurf;
2029                 _ico_win_mgr->active_keyboard_usurf = usurf;
2030                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
2031                                         usurf,
2032                                         ICO_WINDOW_MGR_ACTIVE_POINTER |
2033                                             ICO_WINDOW_MGR_ACTIVE_KEYBOARD,
2034                                         0,0,0,0);
2035                 win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER |
2036                                               ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
2037             }
2038             break;
2039         }
2040         uifw_trace("uifw_set_active: Leave(Change Active)");
2041     }
2042     else    {
2043         win_mgr_set_active(NULL, active);
2044         uifw_trace("uifw_set_active: Leave(Reset active surface)");
2045     }
2046 }
2047
2048 /*--------------------------------------------------------------------------*/
2049 /**
2050  * @brief   uifw_set_layer_visible: layer visible control
2051  *
2052  * @param[in]   client      Weyland client
2053  * @param[in]   resource    resource of request
2054  * @param[in]   layer       layer id
2055  * @param[in]   visible     visible(1=show/0=hide)
2056  * @return      none
2057  */
2058 /*--------------------------------------------------------------------------*/
2059 static void
2060 uifw_set_layer_visible(struct wl_client *client, struct wl_resource *resource,
2061                        uint32_t layer, int32_t visible)
2062 {
2063     struct uifw_win_layer   *el;
2064     struct uifw_win_layer   *new_el;
2065     struct uifw_win_surface *usurf;
2066
2067     uifw_trace("uifw_set_layer_visible: Enter(layer=%d, visilbe=%d)", layer, visible);
2068
2069     /* Search Layer                             */
2070     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
2071         if (el->layer == layer) break;
2072     }
2073
2074     if (&el->link == &_ico_win_mgr->ivi_layer_list)    {
2075         /* layer not exist, create new layer    */
2076         uifw_trace("uifw_set_layer_visible: New Layer %d", layer);
2077         new_el = win_mgr_create_layer(NULL, layer);
2078         if (! new_el)   {
2079             uifw_trace("uifw_set_layer_visible: Leave(No Memory)");
2080             return;
2081         }
2082         new_el->visible = (visible != 0) ? TRUE : FALSE;
2083         ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_LAYER_VISIBLE, NULL,
2084                                 layer, new_el->visible, 0,0,0);
2085         uifw_trace("uifw_set_layer_visible: Leave(new layer)");
2086         return;
2087     }
2088
2089     /* control all surface in layer */
2090     if ((el->visible != FALSE) && (visible == 0))   {
2091         /* layer change to NOT visible  */
2092         uifw_trace("uifw_set_layer_visible: change to not visible");
2093         el->visible = FALSE;
2094     }
2095     else if ((el->visible == FALSE) && (visible != 0))  {
2096         /* layer change to visible      */
2097         uifw_trace("uifw_set_layer_visible: change to visible");
2098         el->visible = TRUE;
2099     }
2100     else    {
2101         /* no change    */
2102         uifw_trace("uifw_set_layer_visible: Leave(no Change %d=>%d)",
2103                    el->visible, visible);
2104         return;
2105     }
2106
2107     /* set damege area          */
2108     wl_list_for_each (usurf, &el->surface_list, ivi_layer) {
2109         if ((usurf->visible != FALSE) && (usurf->surface != NULL) &&
2110             (usurf->surface->output != NULL))  {
2111             /* Damage(redraw) target surface    */
2112             weston_surface_damage_below(usurf->surface);
2113         }
2114     }
2115
2116     /* rebild compositor surface list       */
2117     ico_window_mgr_restack_layer(NULL, 0);
2118
2119     /* send layer visible event to manager  */
2120     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_LAYER_VISIBLE, NULL,
2121                             layer, el->visible, 0,0,0);
2122
2123     uifw_trace("uifw_set_layer_visible: Leave");
2124 }
2125
2126 /*--------------------------------------------------------------------------*/
2127 /**
2128  * @brief   uifw_get_surfaces: get application surfaces
2129  *
2130  * @param[in]   client      Weyland client
2131  * @param[in]   resource    resource of request
2132  * @param[in]   appid       application id
2133  * @return      none
2134  */
2135 /*--------------------------------------------------------------------------*/
2136 static void
2137 uifw_get_surfaces(struct wl_client *client, struct wl_resource *resource, const char *appid)
2138 {
2139     struct uifw_client  *uclient;
2140     struct uifw_win_layer *el;
2141     struct uifw_win_surface *usurf;
2142     struct wl_array     reply;
2143     uint32_t            *up;
2144
2145     uifw_trace("uifw_get_surfaces: Enter(appid=%s)", appid);
2146
2147     wl_array_init(&reply);
2148
2149     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
2150         if (strcmp(uclient->appid, appid) == 0) break;
2151     }
2152     if (&uclient->link == &_ico_win_mgr->client_list)    {
2153         uifw_trace("uifw_get_surfaces: appid=%s dose not exist", appid);
2154     }
2155     else    {
2156         wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
2157             wl_list_for_each (usurf, &el->surface_list, ivi_layer) {
2158                 if (usurf->uclient == uclient)  {
2159                     uifw_trace("uifw_get_surfaces: %s surf=%08x", appid, usurf->surfaceid);
2160                     up = (uint32_t *)wl_array_add(&reply, sizeof(uint32_t));
2161                     if (up) {
2162                         *up = usurf->surfaceid;
2163                     }
2164                 }
2165             }
2166         }
2167     }
2168     ico_window_mgr_send_app_surfaces(resource, appid, &reply);
2169
2170     wl_array_release(&reply);
2171     uifw_trace("uifw_get_surfaces: Leave");
2172 }
2173
2174 /*--------------------------------------------------------------------------*/
2175 /**
2176  * @brief   win_mgr_check_surfacemap: check and change all surface
2177  *
2178  * @param[in]   animation   weston animation table(unused)
2179  * @param[in]   outout      weston output table
2180  * @param[in]   mseces      current time(unused)
2181  * @return      none
2182  */
2183 /*--------------------------------------------------------------------------*/
2184 static void
2185 win_mgr_check_surfacemap(struct weston_animation *animation,
2186                             struct weston_output *output, uint32_t msecs)
2187 {
2188     struct uifw_surface_map *sm;
2189
2190     wl_list_for_each(sm, &_ico_win_mgr->map_list, map_link) {
2191         if (sm->usurf->surface->output == output)   {
2192             win_mgr_change_mapsurface(sm, 0);
2193         }
2194     }
2195 }
2196
2197 /*--------------------------------------------------------------------------*/
2198 /**
2199  * @brief   win_mgr_change_mapsurface: check and change mapped surface
2200  *
2201  * @param[in]   sm          map surface table
2202  * @param[in]   event       send event (if 0, send if changed)
2203  * @return      none
2204  */
2205 /*--------------------------------------------------------------------------*/
2206 static void
2207 win_mgr_change_mapsurface(struct uifw_surface_map *sm, int event)
2208 {
2209     struct uifw_drm_buffer  *drm_buffer;
2210     struct uifw_dri_image   *dri_image;
2211     struct uifw_dri_region  *dri_region;
2212     struct uifw_gl_surface_state *gl_state;
2213     struct weston_surface   *es;
2214     uint32_t    eglname = 0;
2215     int         width;
2216     int         height;
2217     int         stride;
2218     uint32_t    format;
2219
2220     /* check if buffered        */
2221     es = sm->usurf->surface;
2222     if ((es == NULL) || (es->buffer_ref.buffer == NULL) ||
2223         (es->buffer_ref.buffer->width <= 0) || (es->buffer_ref.buffer->height <= 0) ||
2224         (es->buffer_ref.buffer->legacy_buffer == NULL) || (es->renderer_state == NULL) ||
2225         (((struct uifw_drm_buffer *)es->buffer_ref.buffer->legacy_buffer)->driver_buffer
2226          == NULL))  {
2227         /* surface has no buffer, error         */
2228         uifw_trace("win_mgr_change_mapsurface: surface(%08x) has no buffer",
2229                    sm->usurf->surfaceid);
2230         sm->width = 0;
2231         sm->height = 0;
2232         sm->stride = 0;
2233         sm->eglname = 0;
2234         sm->format = 0;
2235         if (sm->initflag)   {
2236             event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP;
2237         }
2238         else    {
2239             event = 0;
2240         }
2241     }
2242     else    {
2243         gl_state = (struct uifw_gl_surface_state *)es->renderer_state;
2244         if (gl_state->buffer_type == BUFFER_TYPE_SHM)   {
2245             event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR;
2246         }
2247         else if (gl_state->buffer_type != BUFFER_TYPE_EGL)   {
2248             event = 0;
2249         }
2250         else    {
2251             drm_buffer = (struct uifw_drm_buffer *)es->buffer_ref.buffer->legacy_buffer;
2252             dri_image = (struct uifw_dri_image *)drm_buffer->driver_buffer;
2253             dri_region = dri_image->region;
2254             width = es->buffer_ref.buffer->width;
2255             height = es->buffer_ref.buffer->height;
2256             stride = drm_buffer->stride[0];
2257             if (drm_buffer->format == 0x34325258)   {
2258                 format = EGL_TEXTURE_RGB;
2259             }
2260             else if (drm_buffer->format == 0x34325241)  {
2261                 format = EGL_TEXTURE_RGBA;
2262             }
2263             else    {
2264                 /* unknown format, error    */
2265                 format = EGL_NO_TEXTURE;
2266             }
2267             eglname = dri_region->name;
2268
2269             if ((sm->initflag == 0) && (width > 0) && (height > 0) && (stride > 0)) {
2270                 sm->initflag = 1;
2271                 event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP;
2272             }
2273             else    {
2274                 if ((width <= 0) || (height <= 0) || (stride <= 0)) {
2275                     event = 0;
2276                 }
2277                 else if (event == 0)    {
2278                     if ((sm->width != width) || (sm->height != height) ||
2279                         (sm->stride != stride)) {
2280                         event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_RESIZE;
2281                     }
2282                     else if ((eglname != sm->eglname) || (format != sm->format))    {
2283                         event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_CONTENTS;
2284                     }
2285                 }
2286             }
2287             sm->width = width;
2288             sm->height = height;
2289             sm->stride = stride;
2290             sm->eglname = eglname;
2291             sm->format = format;
2292             sm->eglname = eglname;
2293         }
2294     }
2295
2296     if (event != 0) {
2297         uifw_trace("win_mgr_change_mapsurface: send MAP event(ev=%d surf=%08x name=%08x "
2298                    "w/h/s=%d/%d/%d format=%x",
2299                    event, sm->usurf->surfaceid, sm->eglname, sm->width, sm->height,
2300                    sm->stride, sm->format);
2301         ico_window_mgr_send_map_surface(sm->uclient->mgr->resource, event,
2302                                         sm->usurf->surfaceid, sm->type, sm->eglname,
2303                                         sm->width, sm->height, sm->stride, sm->format);
2304         if (event == ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR)    {
2305             /* free map table if error  */
2306             wl_list_remove(&sm->surf_link);
2307             wl_list_remove(&sm->map_link);
2308             sm->usurf = (struct uifw_win_surface *)_ico_win_mgr->free_maptable;
2309             _ico_win_mgr->free_maptable = sm;
2310         }
2311     }
2312     uifw_trace("win_mgr_change_mapsurface: Leave");
2313 }
2314
2315 /*--------------------------------------------------------------------------*/
2316 /**
2317  * @brief   uifw_map_surface: mapped surface buffer to system application
2318  *
2319  * @param[in]   client      Weyland client
2320  * @param[in]   resource    resource of request
2321  * @param[in]   surfaceid   surface id
2322  * @param[in]   framerate   frame rate of surface update(frame/sec)
2323  * @return      none
2324  */
2325 /*--------------------------------------------------------------------------*/
2326 static void
2327 uifw_map_surface(struct wl_client *client, struct wl_resource *resource,
2328                  uint32_t surfaceid, int32_t framerate)
2329 {
2330     struct uifw_win_surface     *usurf;
2331     struct weston_surface       *es;
2332     struct uifw_surface_map     *sm;
2333     struct weston_buffer        *buffer;
2334     struct uifw_client          *uclient;
2335     struct uifw_drm_buffer      *drm_buffer;
2336     struct uifw_gl_surface_state *gl_state;
2337
2338     uifw_trace("uifw_map_surface: Enter(surface=%08x,fps=%d)", surfaceid, framerate);
2339
2340     usurf = ico_window_mgr_get_usurf(surfaceid);
2341     if (! usurf)    {
2342         /* surface dose not exist, error        */
2343         ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
2344                                         surfaceid, 1, 0, 0, 0, 0, 0);
2345         uifw_trace("uifw_map_surface: Leave(surface=%08x dose not exist)", surfaceid);
2346         return;
2347     }
2348     uclient = find_client_from_client(client);
2349     if ((! uclient) || (! uclient->mgr))    {
2350         /* client dose not exist, error         */
2351         ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
2352                                         surfaceid, 2, 0, 0, 0, 0, 0);
2353         uifw_trace("uifw_map_surface: Leave(client=%08x dose not exist)", (int)client);
2354         return;
2355     }
2356
2357     es = usurf->surface;
2358
2359     /* check if buffered        */
2360     if (es == NULL) {
2361         /* surface has no buffer, error         */
2362         ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
2363                                         surfaceid, 3, 0, 0, 0, 0, 0);
2364         uifw_trace("uifw_map_surface: Leave(surface(%08x) has no buffer)", surfaceid);
2365         return;
2366     }
2367
2368     /* check buffer type        */
2369     gl_state = (struct uifw_gl_surface_state *)es->renderer_state;
2370     if (gl_state) {
2371         if (gl_state->buffer_type == BUFFER_TYPE_SHM)   {
2372             /* wl_shm_buffer not support    */
2373             ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
2374                                             surfaceid, 4, 0, 0, 0, 0, 0);
2375             uifw_trace("uifw_map_surface: Leave(surface(%08x) is wl_shm_buffer, "
2376                        "not support)", surfaceid);
2377             return;
2378         }
2379     }
2380
2381     /* create map table         */
2382     sm = _ico_win_mgr->free_maptable;
2383     if (sm) {
2384         _ico_win_mgr->free_maptable = (struct uifw_surface_map *)sm->usurf;
2385     }
2386     else    {
2387         sm = (struct uifw_surface_map *)malloc(sizeof(struct uifw_surface_map));
2388         if (! sm)   {
2389             ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
2390                                             surfaceid, 5, 0, 0, 0, 0, 0);
2391             uifw_trace("uifw_map_surface: Leave(malloc error)");
2392             return;
2393         }
2394     }
2395     memset(sm, 0, sizeof(struct uifw_surface_map));
2396
2397     wl_list_init(&sm->map_link);
2398     wl_list_init(&sm->surf_link);
2399     sm->usurf = usurf;
2400     sm->uclient = uclient;
2401     sm->type = ICO_WINDOW_MGR_MAP_TYPE_EGL;
2402     sm->eglname = 0;
2403     sm->framerate = framerate;
2404     wl_list_insert(_ico_win_mgr->map_list.next, &sm->map_link);
2405     wl_list_insert(usurf->surf_map.prev, &sm->surf_link);
2406
2407     buffer = es->buffer_ref.buffer;
2408     if ((buffer != NULL) && (gl_state != NULL) &&
2409         (gl_state->buffer_type == BUFFER_TYPE_EGL)) {
2410         sm->width = buffer->width;
2411         sm->height = buffer->height;
2412         drm_buffer = (struct uifw_drm_buffer *)buffer->legacy_buffer;
2413         if (drm_buffer != NULL) {
2414             sm->stride = drm_buffer->stride[0];
2415             if (drm_buffer->format == 0x34325258)   {
2416                 sm->format = EGL_TEXTURE_RGB;
2417             }
2418             else if (drm_buffer->format == 0x34325241)  {
2419                 sm->format = EGL_TEXTURE_RGBA;
2420             }
2421             else    {
2422                 /* unknown format, error    */
2423                 sm->format = EGL_NO_TEXTURE;
2424             }
2425             if ((sm->width > 0) && (sm->height > 0) && (sm->stride > 0) &&
2426                 (gl_state != NULL))  {
2427                 sm->initflag = 1;
2428             }
2429         }
2430     }
2431
2432     /* send map event                       */
2433     if (sm->initflag)   {
2434         win_mgr_change_mapsurface(sm, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP);
2435     }
2436     uifw_trace("uifw_map_surface: Leave");
2437 }
2438
2439 /*--------------------------------------------------------------------------*/
2440 /**
2441  * @brief   uifw_unmap_surface: unmap surface buffer
2442  *
2443  * @param[in]   client      Weyland client
2444  * @param[in]   resource    resource of request
2445  * @param[in]   surfaceid   surface id
2446  * @return      none
2447  */
2448 /*--------------------------------------------------------------------------*/
2449 static void
2450 uifw_unmap_surface(struct wl_client *client, struct wl_resource *resource,
2451                    uint32_t surfaceid)
2452 {
2453     struct uifw_win_surface *usurf;
2454     struct uifw_surface_map *sm, *sm_tmp;
2455     struct uifw_client      *uclient;
2456
2457     uifw_trace("uifw_unmap_surface: Enter(surface=%08x)", surfaceid);
2458
2459     usurf = ico_window_mgr_get_usurf(surfaceid);
2460     if (! usurf)    {
2461         /* surface dose not exist, error        */
2462         uifw_trace("uifw_unmap_surface: Leave(surface=%08x dose not exist)", surfaceid);
2463         return;
2464     }
2465     if (client) {
2466         uclient = find_client_from_client(client);
2467         if ((! uclient) || (! uclient->mgr))    {
2468             /* client dose not exist, error         */
2469             uifw_trace("uifw_unmap_surface: Leave(client=%08x dose not exist)", (int)client);
2470             return;
2471         }
2472     }
2473     else    {
2474         uclient = NULL;
2475         wl_list_for_each(sm, &usurf->surf_map, surf_link) {
2476             if (sm->uclient->mgr != NULL) {
2477                 uifw_trace("uifw_unmap_surface: send MAP event(ev=%d surf=%08x name=%08x "
2478                            "w/h/s=%d/%d/%d format=%x",
2479                            ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP, surfaceid,
2480                            sm->eglname, sm->width, sm->height, sm->stride, sm->format);
2481                 ico_window_mgr_send_map_surface(sm->uclient->mgr->resource,
2482                                                 ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP,
2483                                                 surfaceid, sm->type, sm->eglname, sm->width,
2484                                                 sm->height, sm->stride, sm->format);
2485             }
2486         }
2487     }
2488
2489     wl_list_for_each_safe(sm, sm_tmp, &usurf->surf_map, surf_link) {
2490         if (((uclient != NULL) && (sm->uclient != uclient)))   continue;
2491         /* send unmap event                     */
2492         if ((uclient != NULL) && (uclient->mgr != NULL))    {
2493             uifw_trace("uifw_unmap_surface: send MAP event(ev=%d surf=%08x name=%08x "
2494                        "w/h/s=%d/%d/%d format=%x",
2495                        ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP, surfaceid,
2496                        sm->eglname, sm->width, sm->height, sm->stride, sm->format);
2497             ico_window_mgr_send_map_surface(uclient->mgr->resource,
2498                                             ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP,
2499                                             surfaceid, sm->type, sm->eglname, sm->width,
2500                                             sm->height, sm->stride, sm->format);
2501         }
2502         wl_list_remove(&sm->surf_link);
2503         wl_list_remove(&sm->map_link);
2504         sm->usurf = (struct uifw_win_surface *)_ico_win_mgr->free_maptable;
2505         _ico_win_mgr->free_maptable = sm;
2506     }
2507     uifw_trace("uifw_unmap_surface: Leave");
2508 }
2509
2510 /*--------------------------------------------------------------------------*/
2511 /**
2512  * @brief   win_mgr_surface_change_mgr: surface chagen from manager(HomeScreen)
2513  *
2514  * @param[in]   surface     Weston surface
2515  * @param[in]   x           X coordinate on screen
2516  * @param[in]   y           Y coordinate on screen
2517  * @param[in]   width       surface width
2518  * @param[in]   height      surface height
2519  * @return      number of managers
2520  * @retval      > 0         number of managers
2521  * @retval      0           manager not exist
2522  */
2523 /*--------------------------------------------------------------------------*/
2524 static int
2525 win_mgr_surface_change_mgr(struct weston_surface *surface,
2526                            const int x, const int y, const int width, const int height)
2527 {
2528     int     num_mgr;
2529     struct uifw_win_surface *usurf;
2530
2531     uifw_trace("win_mgr_surface_change_mgr: Enter(%08x,x/y=%d/%d,w/h=%d/%d)",
2532                (int)surface, x, y, width, height);
2533
2534     usurf = find_uifw_win_surface_by_ws(surface);
2535     if (! usurf) {
2536         uifw_trace("win_mgr_surface_change_mgr: Leave(Not Exist)");
2537         return 0;
2538     }
2539
2540     num_mgr = ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
2541                                       usurf, x, y, width, height, 1);
2542
2543     uifw_trace("win_mgr_surface_change_mgr: Leave(%d)", num_mgr);
2544     return num_mgr;
2545 }
2546
2547 /*--------------------------------------------------------------------------*/
2548 /**
2549  * @brief   win_mgr_change_surface: surface change
2550  *
2551  * @param[in]   surface     Weston surface
2552  * @param[in]   to          destination(0=Client&Manager,1=Client,-1=Manager)
2553  * @param[in]   manager     request from manager(0=Client,1=Manager)
2554  * @return      none
2555  */
2556 /*--------------------------------------------------------------------------*/
2557 static void
2558 win_mgr_change_surface(struct weston_surface *surface, const int to, const int manager)
2559 {
2560     struct uifw_win_surface *usurf;
2561     struct weston_surface   *es;
2562     int     x;
2563     int     y;
2564     int     repaint = 0;
2565
2566     uifw_trace("win_mgr_change_surface: Enter(%08x,%d,%d)", (int)surface, to, manager);
2567
2568     /* Find surface         */
2569     usurf = find_uifw_win_surface_by_ws(surface);
2570     if (! usurf) {
2571         uifw_trace("win_mgr_change_surface: Leave(Not Exist)");
2572         return;
2573     }
2574     es = usurf->surface;
2575     if (! es)   {
2576         uifw_trace("win_mgr_change_surface: Leave(No weston surface)");
2577         return;
2578     }
2579
2580     /* if not configure surface, set surface size   */
2581     if ((usurf->width <= 0) || (usurf->height <= 0))  {
2582         uifw_trace("win_mgr_change_surface: set surface x/y=%d/%d=>%d/%d w/h=%d/%d=>%d/%d",
2583                    (int)es->geometry.x, (int)es->geometry.y, usurf->x, usurf->y,
2584                    usurf->width, usurf->height, es->geometry.width, es->geometry.height);
2585         usurf->width = es->geometry.width;
2586         usurf->height = es->geometry.height;
2587         win_mgr_set_scale(usurf);
2588         if (usurf->visible) {
2589             weston_surface_set_position(usurf->surface,
2590                                         (float)(usurf->node_tbl->disp_x +
2591                                                 usurf->x + usurf->xadd),
2592                                         (float)(usurf->node_tbl->disp_y +
2593                                                 usurf->y + usurf->yadd));
2594             ico_window_mgr_restack_layer(usurf, 0);
2595         }
2596         else    {
2597             weston_surface_set_position(usurf->surface, (float)(ICO_IVI_MAX_COORDINATE+1),
2598                                         (float)(ICO_IVI_MAX_COORDINATE+1));
2599         }
2600     }
2601
2602     /* send wayland event to client     */
2603     if ((to >= 0) && (usurf->shsurf != NULL) && (manager !=0) &&
2604         (usurf->width > 0) && (usurf->height > 0))  {
2605         if ((usurf->width != usurf->conf_width) ||
2606             (usurf->height != usurf->conf_height))  {
2607             usurf->conf_width = usurf->width;
2608             usurf->conf_height = usurf->height;
2609             uifw_trace("win_mgr_change_surface: SURFACE_CONFIGURE %08x(%08x),w/h=%d/%d ",
2610                        usurf->surfaceid, (int)es, usurf->width, usurf->height);
2611             ico_ivi_shell_send_configure(es,
2612                                          WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT,
2613                                          usurf->width, usurf->height);
2614         }
2615     }
2616
2617     if (usurf->visible) {
2618         x = usurf->node_tbl->disp_x + usurf->x;
2619         y = usurf->node_tbl->disp_y + usurf->y;
2620     }
2621     else    {
2622         x = ICO_IVI_MAX_COORDINATE+1;
2623         y = ICO_IVI_MAX_COORDINATE+1;
2624     }
2625     /* change geometry if request from manager  */
2626     if (manager)    {
2627         if ((usurf->width != es->geometry.width) ||
2628             (usurf->height != es->geometry.height) ||
2629             (es->geometry.x != (float)x) || (es->geometry.y != (float)y))   {
2630             win_mgr_surface_configure(usurf, (float)x, (float)y, usurf->width, usurf->height);
2631             repaint ++;
2632         }
2633     }
2634
2635     /* send manager event to HomeScreen */
2636     if (to <= 0)    {
2637         if (manager)    {
2638             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
2639                                     usurf, usurf->x, usurf->y,
2640                                     usurf->width, usurf->height, 0);
2641         }
2642         else    {
2643             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
2644                                     usurf, (int)es->geometry.x, (int)es->geometry.y,
2645                                     es->geometry.width, es->geometry.height, 1);
2646         }
2647     }
2648
2649     /* change geometry if request from client   */
2650     if (! manager)  {
2651         if ((usurf->width != es->geometry.width) || (usurf->height != es->geometry.height) ||
2652             (es->geometry.x != (float)x) || (es->geometry.y != (float)y))   {
2653             win_mgr_surface_configure(usurf, x, y, usurf->width, usurf->height);
2654             repaint ++;
2655         }
2656     }
2657
2658     if (repaint)    {
2659         uifw_trace("win_mgr_change_surface: repaint");
2660         weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
2661     }
2662     uifw_trace("win_mgr_change_surface: Leave(OK)");
2663 }
2664
2665 /*--------------------------------------------------------------------------*/
2666 /**
2667  * @brief   win_mgr_surface_configure: UIFW surface configure
2668  *
2669  * @param[in]   usurf       UIFW surface
2670  * @param[in]   x           X coordinate on screen
2671  * @param[in]   y           Y coordinate on screen
2672  * @param[in]   width       surface width
2673  * @param[in]   height      surface height
2674  * @return      none
2675  */
2676 /*--------------------------------------------------------------------------*/
2677 static void
2678 win_mgr_surface_configure(struct uifw_win_surface *usurf,
2679                           int x, int y, int width, int height)
2680 {
2681     struct weston_surface   *es;
2682
2683     es = usurf->surface;
2684     if ((es != NULL) && (es->buffer_ref.buffer))  {
2685         if (usurf->client_width == 0)   {
2686             usurf->client_width = es->geometry.width;
2687             if (usurf->client_width == 0)
2688                 usurf->client_width = weston_surface_buffer_width(es);
2689         }
2690         if (usurf->client_height == 0)  {
2691             usurf->client_height = es->geometry.height;
2692             if (usurf->client_height == 0)
2693                 usurf->client_height = weston_surface_buffer_height(es);
2694         }
2695
2696         /* not set geometry width/height    */
2697         win_mgr_set_scale(usurf);
2698         weston_surface_set_position(es, x + usurf->xadd, y + usurf->yadd);
2699     }
2700 }
2701
2702 /*--------------------------------------------------------------------------*/
2703 /**
2704  * @brief   win_mgr_shell_configure: shell surface configure
2705  *
2706  * @param[in]   surface     Weston surface
2707  * @return      none
2708  */
2709 /*--------------------------------------------------------------------------*/
2710 static void
2711 win_mgr_shell_configure(struct weston_surface *surface)
2712 {
2713     struct uifw_win_surface *usurf;
2714     int     buf_width;
2715     int     buf_height;
2716
2717     uifw_trace("win_mgr_shell_configure: Enter(%08x)", (int)surface);
2718
2719     /* Find UIFW surface        */
2720     usurf = find_uifw_win_surface_by_ws(surface);
2721     if (! usurf) {
2722         uifw_trace("win_mgr_shell_configure: Leave(Not Exist)");
2723         return;
2724     }
2725
2726     usurf->client_width = surface->geometry.width;
2727     usurf->client_height = surface->geometry.height;
2728     buf_width = weston_surface_buffer_width(surface);
2729     buf_height = weston_surface_buffer_height(surface);
2730     uifw_trace("win_mgr_shell_configure: %08x client w/h=%d/%d buf=%d/%d",
2731                usurf->surfaceid,
2732                usurf->client_width, usurf->client_height, buf_width, buf_height);
2733     if (usurf->client_width > buf_width)    usurf->client_width = buf_width;
2734     if (usurf->client_height > buf_height)  usurf->client_height = buf_height;
2735
2736     /* send event to manager    */
2737     win_mgr_change_surface(surface, -1, 0);
2738
2739     uifw_trace("win_mgr_shell_configure: Leave");
2740 }
2741
2742 /*--------------------------------------------------------------------------*/
2743 /**
2744  * @brief   win_mgr_select_surface: select surface by Bottun/Touch
2745  *
2746  * @param[in]   surface     Weston surface
2747  * @return      none
2748  */
2749 /*--------------------------------------------------------------------------*/
2750 static void
2751 win_mgr_select_surface(struct weston_surface *surface)
2752 {
2753     struct uifw_win_surface *usurf;
2754
2755     uifw_trace("win_mgr_select_surface: Enter(%08x)", (int)surface);
2756
2757     /* find surface         */
2758     usurf = find_uifw_win_surface_by_ws(surface);
2759     if (! usurf) {
2760         uifw_trace("win_mgr_select_surface: Leave(Not Exist)");
2761         return;
2762     }
2763     if (usurf != _ico_win_mgr->active_pointer_usurf)  {
2764
2765         /* send active event to manager     */
2766         if (ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
2767                                     usurf, ICO_WINDOW_MGR_ACTIVE_SELECTED, 0,0,0,0) <= 0) {
2768             uifw_trace("win_mgr_select_surface: not found manager, set active");
2769             win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER |
2770                                         ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
2771         }
2772     }
2773     uifw_trace("win_mgr_select_surface: Leave(OK)");
2774 }
2775
2776 /*--------------------------------------------------------------------------*/
2777 /**
2778  * @brief   win_mgr_set_title: set tile name to surface
2779  *
2780  * @param[in]   surface     weston surface
2781  * @param[in]   title       title name
2782  * @return      none
2783  */
2784 /*--------------------------------------------------------------------------*/
2785 static void
2786 win_mgr_set_title(struct weston_surface *surface, const char *title)
2787 {
2788     struct uifw_win_surface *usurf;
2789
2790     uifw_trace("win_mgr_set_title: Enter(%08x) name=%s", (int)surface, title);
2791
2792     usurf = find_uifw_win_surface_by_ws(surface);
2793     if (! usurf) {
2794         uifw_trace("win_mgr_set_title: Leave(Not Exist)");
2795         return;
2796     }
2797     if (((usurf->width > 0) && (usurf->height > 0)) &&
2798         ((usurf->created == 0) ||
2799          (strncmp(title, usurf->winname, ICO_IVI_WINNAME_LENGTH-1) != 0)))    {
2800         strncpy(usurf->winname, title, ICO_IVI_WINNAME_LENGTH-1);
2801         usurf->winname[ICO_IVI_WINNAME_LENGTH-1] = 0;
2802         if (usurf->created == 0)    {
2803             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CREATED, usurf, 0,0,0,0,0);
2804             usurf->created = 1;
2805         }
2806         else    {
2807             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_NAME, usurf, 0,0,0,0,0);
2808         }
2809     }
2810     else    {
2811         strncpy(usurf->winname, title, ICO_IVI_WINNAME_LENGTH-1);
2812         usurf->winname[ICO_IVI_WINNAME_LENGTH-1] = 0;
2813     }
2814     uifw_trace("win_mgr_set_title: Leave");
2815 }
2816
2817 /*--------------------------------------------------------------------------*/
2818 /**
2819  * @brief   win_mgr_surface_move: set tile name to surface
2820  *
2821  * @param[in]   surface     weston surface
2822  * @param[in]   title       title name
2823  * @return      none
2824  */
2825 /*--------------------------------------------------------------------------*/
2826 static void
2827 win_mgr_surface_move(struct weston_surface *surface, int *dx, int *dy)
2828 {
2829     struct uifw_win_surface *usurf;
2830
2831     uifw_trace("win_mgr_surface_move: Enter(%08x) x/y=%d,%d", (int)surface, *dx, *dy);
2832
2833     usurf = find_uifw_win_surface_by_ws(surface);
2834     if (! usurf) {
2835         uifw_trace("win_mgr_surface_move: Leave(Not Exist)");
2836         return;
2837     }
2838     if (usurf->visible) {
2839         *dx = usurf->node_tbl->disp_x + usurf->x;
2840         *dy = usurf->node_tbl->disp_y + usurf->y;
2841     }
2842     else    {
2843         *dx = ICO_IVI_MAX_COORDINATE+1;
2844         *dy = ICO_IVI_MAX_COORDINATE+1;
2845     }
2846     uifw_trace("win_mgr_surface_move: Leave(change to x/y=%d/%d)", *dx, *dy);
2847 }
2848
2849 /*--------------------------------------------------------------------------*/
2850 /**
2851  * @brief   ico_window_mgr_set_visible: change surface visibility
2852  *
2853  * @param[in]   usurf       UIFW surface
2854  * @param[in]   visible     visible(=1)/unvisible(0)
2855  * @return      none
2856  */
2857 /*--------------------------------------------------------------------------*/
2858 WL_EXPORT   void
2859 ico_window_mgr_set_visible(struct uifw_win_surface *usurf, const int visible)
2860 {
2861     if (visible)    {
2862         if (usurf->visible == 0)    {
2863             uifw_trace("ico_window_mgr_set_visible: Chagne to Visible(%08x)", (int)usurf);
2864             usurf->visible = 1;
2865             /* change unvisible to visible, restack surface list    */
2866             ico_window_mgr_restack_layer(usurf, 0);
2867         }
2868     }
2869     else    {
2870         if (usurf->visible != 0)    {
2871             uifw_trace("ico_window_mgr_set_visible: Chagne to Unvisible(%08x)", (int)usurf);
2872             usurf->visible = 0;
2873             /* change visible to unvisible, restack surface list    */
2874             ico_window_mgr_restack_layer(usurf, 0);
2875         }
2876     }
2877     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
2878                             usurf, usurf->visible, usurf->raise, 0, 0,0);
2879 }
2880
2881 /*--------------------------------------------------------------------------*/
2882 /**
2883  * @brief   win_mgr_set_raise: change surface raise/lower
2884  *
2885  * @param[in]   usurf       UIFW surface
2886  * @param[in]   raise       raise(=1)/lower(0)
2887  * @return      none
2888  */
2889 /*--------------------------------------------------------------------------*/
2890 static void
2891 win_mgr_set_raise(struct uifw_win_surface *usurf, const int raise)
2892 {
2893     uifw_trace("win_mgr_set_raise: Enter(%08x,%d) layer=%x",
2894                (int)usurf, raise, (int)usurf->win_layer->layer);
2895
2896     wl_list_remove(&usurf->ivi_layer);
2897     if (raise)  {
2898         /* raise ... surface stack to top of layer          */
2899         wl_list_insert(&usurf->win_layer->surface_list, &usurf->ivi_layer);
2900         usurf->raise = 1;
2901         uifw_trace("win_mgr_set_raise: Raise Link to Top");
2902     }
2903     else    {
2904         /* Lower ... surface stack to bottom of layer       */
2905         wl_list_insert(usurf->win_layer->surface_list.prev, &usurf->ivi_layer);
2906         usurf->raise = 0;
2907         uifw_trace("win_mgr_set_raise: Lower Link to Bottom");
2908     }
2909
2910     /* rebild compositor surface list               */
2911     if (usurf->visible) {
2912         ico_window_mgr_restack_layer(usurf, 0);
2913     }
2914     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
2915                             usurf, usurf->visible, usurf->raise, 0, 0,0);
2916
2917     uifw_trace("win_mgr_set_raise: Leave");
2918 }
2919
2920 /*--------------------------------------------------------------------------*/
2921 /**
2922  * @brief   win_mgr_destroy_surface: surface destroy
2923  *
2924  * @param[in]   surface     Weston surface
2925  * @return      none
2926  */
2927 /*--------------------------------------------------------------------------*/
2928 static void
2929 win_mgr_destroy_surface(struct weston_surface *surface)
2930 {
2931     struct uifw_win_surface *usurf;
2932     struct uifw_win_surface *phash;
2933     struct uifw_win_surface *bhash;
2934     uint32_t    hash;
2935
2936     usurf = find_uifw_win_surface_by_ws(surface);
2937     if (! usurf) {
2938         uifw_trace("win_mgr_destroy_surface: UIFW surface Not Exist");
2939         return;
2940     }
2941     uifw_trace("win_mgr_destroy_surface: Enter(%08x) %08x", (int)surface, usurf->surfaceid);
2942
2943     /* unmap surface                */
2944     if (&usurf->surf_map != usurf->surf_map.next)   {
2945         uifw_unmap_surface(NULL, NULL, usurf->surfaceid);
2946     }
2947
2948     /* destroy active surface       */
2949     if (usurf == _ico_win_mgr->active_pointer_usurf)  {
2950         _ico_win_mgr->active_pointer_usurf = NULL;
2951     }
2952     if (usurf == _ico_win_mgr->active_keyboard_usurf) {
2953         _ico_win_mgr->active_keyboard_usurf = NULL;
2954     }
2955
2956     /* destroy animation extenson   */
2957     if (win_mgr_hook_animation) {
2958         (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_DESTROY, (void *)usurf);
2959     }
2960
2961     /* delete from layer list       */
2962     wl_list_remove(&usurf->ivi_layer);
2963     ico_window_mgr_restack_layer(NULL, 0);
2964
2965     /* delete from cleint list      */
2966     wl_list_remove(&usurf->client_link);
2967
2968     /* delete from hash table       */
2969     hash = MAKE_IDHASH(usurf->surfaceid);
2970     phash = _ico_win_mgr->idhash[hash];
2971     bhash = NULL;
2972     while ((phash) && (phash != usurf)) {
2973         bhash = phash;
2974         phash = phash->next_idhash;
2975     }
2976     if (bhash)  {
2977         bhash->next_idhash = usurf->next_idhash;
2978     }
2979     else    {
2980         _ico_win_mgr->idhash[hash] = usurf->next_idhash;
2981     }
2982
2983     hash = MAKE_WSHASH(usurf->surface);
2984     phash = _ico_win_mgr->wshash[hash];
2985     bhash = NULL;
2986     while ((phash) && (phash != usurf)) {
2987         bhash = phash;
2988         phash = phash->next_wshash;
2989     }
2990     if (bhash)  {
2991         bhash->next_wshash = usurf->next_wshash;
2992     }
2993     else    {
2994         _ico_win_mgr->wshash[hash] = usurf->next_wshash;
2995     }
2996
2997     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_DESTROYED,
2998                            usurf, 0,0,0,0,0);
2999
3000     hash = usurf->surfaceid & SURCAFE_ID_MASK;
3001     _ico_win_mgr->surfaceid_map[(hash - 1)/16] &= ~(1 << ((hash - 1) % 16));
3002
3003     free(usurf);
3004     uifw_trace("win_mgr_destroy_surface: Leave(OK)");
3005 }
3006
3007 /*--------------------------------------------------------------------------*/
3008 /**
3009  * @brief   bind_ico_win_mgr: bind Multi Window Manager from client
3010  *
3011  * @param[in]   client      client
3012  * @param[in]   data        user data(unused)
3013  * @param[in]   version     protocol version(unused)
3014  * @param[in]   id          client object id
3015  * @return      none
3016  */
3017 /*--------------------------------------------------------------------------*/
3018 static void
3019 bind_ico_win_mgr(struct wl_client *client,
3020                  void *data, uint32_t version, uint32_t id)
3021 {
3022     struct wl_resource  *add_resource;
3023     struct uifw_manager *mgr;
3024     struct uifw_client  *uclient;
3025
3026     uifw_trace("bind_ico_win_mgr: Enter(client=%08x, id=%x)", (int)client, (int)id);
3027
3028     add_resource = wl_resource_create(client, &ico_window_mgr_interface, 1, id);
3029     if (add_resource)   {
3030         wl_resource_set_implementation(add_resource, &ico_window_mgr_implementation,
3031                                        _ico_win_mgr, unbind_ico_win_mgr);
3032     }
3033
3034     /* Create client management tabel       */
3035     uclient = find_client_from_client(client);
3036     if (! uclient)  {
3037         win_mgr_bind_client(client, NULL);
3038         uclient = find_client_from_client(client);
3039     }
3040
3041     /* Manager                              */
3042     mgr = (struct uifw_manager *)malloc(sizeof(struct uifw_manager));
3043     if (! mgr)  {
3044         uifw_error("bind_ico_win_mgr: Error, No Memory");
3045         return;
3046     }
3047     memset(mgr, 0, sizeof(struct uifw_manager));
3048     mgr->resource = add_resource;
3049     if (uclient)    {
3050         uclient->mgr = mgr;
3051     }
3052     wl_list_insert(&_ico_win_mgr->manager_list, &mgr->link);
3053
3054     uifw_trace("bind_ico_win_mgr: Leave");
3055 }
3056
3057 /*--------------------------------------------------------------------------*/
3058 /**
3059  * @brief   unbind_ico_win_mgr: unbind Multi Window Manager from client
3060  *
3061  * @param[in]   resource    client resource
3062  * @return      none
3063  */
3064 /*--------------------------------------------------------------------------*/
3065 static void
3066 unbind_ico_win_mgr(struct wl_resource *resource)
3067 {
3068     struct uifw_manager *mgr, *itmp;
3069
3070     uifw_trace("unbind_ico_win_mgr: Enter");
3071
3072     /* Remove manager from manager list */
3073     _ico_win_mgr->num_manager = 0;
3074     wl_list_for_each_safe (mgr, itmp, &_ico_win_mgr->manager_list, link)    {
3075         if (mgr->resource == resource) {
3076             wl_list_remove(&mgr->link);
3077             free(mgr);
3078         }
3079         else    {
3080             if (mgr->manager)   {
3081                 _ico_win_mgr->num_manager++;
3082             }
3083         }
3084     }
3085     uifw_trace("unbind_ico_win_mgr: Leave");
3086 }
3087
3088 /*--------------------------------------------------------------------------*/
3089 /**
3090  * @brief   ico_win_mgr_send_to_mgr: send event to manager(HomeScreen)
3091  *
3092  * @param[in]   event       event code(if -1, not send event)
3093  * @param[in]   usurf       UIFW surface table
3094  * @param[in]   param1      parameter 1
3095  * @param[in]      :             :
3096  * @param[in]   param5      parameter 5
3097  * @return      number of managers
3098  * @retval      > 0         number of managers
3099  * @retval      0           manager not exist
3100  */
3101 /*--------------------------------------------------------------------------*/
3102 static int
3103 ico_win_mgr_send_to_mgr(const int event, struct uifw_win_surface *usurf,
3104                         const int param1, const int param2, const int param3,
3105                         const int param4, const int param5)
3106 {
3107     int     num_mgr = 0;
3108     struct uifw_manager* mgr;
3109
3110     /* if appid not fix, check and fix appid    */
3111     if ((usurf != NULL) &&
3112         (usurf->uclient->fixed_appid < ICO_WINDOW_MGR_APPID_FIXCOUNT)) {
3113         win_mgr_get_client_appid(usurf->uclient);
3114     }
3115
3116     /* send created if not send created event   */
3117     if ((usurf != NULL) && (usurf->created == 0) &&
3118         (((usurf->width > 0) && (usurf->height > 0)) ||
3119          ((event != ICO_WINDOW_MGR_WINDOW_CREATED) &&
3120           (event != ICO_WINDOW_MGR_WINDOW_NAME))))  {
3121         wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
3122             if (mgr->manager)   {
3123                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) WINDOW_CREATED"
3124                            "(surf=%08x,name=%s,pid=%d,appid=%s)", (int)mgr->resource,
3125                            usurf->surfaceid, usurf->winname, usurf->uclient->pid,
3126                            usurf->uclient->appid);
3127                 ico_window_mgr_send_window_created(mgr->resource, usurf->surfaceid,
3128                                                    usurf->winname, usurf->uclient->pid,
3129                                                    usurf->uclient->appid);
3130             }
3131         }
3132         usurf->created = 1;
3133     }
3134
3135     wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
3136         if (mgr->manager)   {
3137             num_mgr ++;
3138
3139             switch(event)   {
3140             case ICO_WINDOW_MGR_WINDOW_CREATED:
3141                 /* Do nothing anymore because sended window create event    */
3142                 break;
3143
3144             case ICO_WINDOW_MGR_WINDOW_NAME:
3145                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) WINDOW_NAME"
3146                            "(surf=%08x,name=%s)", (int)mgr->resource,
3147                            usurf->surfaceid, usurf->winname);
3148                 ico_window_mgr_send_window_name(mgr->resource, usurf->surfaceid,
3149                                                 usurf->winname);
3150                 break;
3151
3152             case ICO_WINDOW_MGR_WINDOW_DESTROYED:
3153                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) DESTROYED"
3154                            "(surf=%08x)", (int)mgr->resource, usurf->surfaceid);
3155                 ico_window_mgr_send_window_destroyed(mgr->resource, usurf->surfaceid);
3156                 break;
3157
3158             case ICO_WINDOW_MGR_WINDOW_VISIBLE:
3159                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) VISIBLE"
3160                            "(surf=%08x,vis=%d,raise=%d,hint=%d)",
3161                            (int)mgr->resource, usurf->surfaceid, param1, param2, param3);
3162                 ico_window_mgr_send_window_visible(mgr->resource,
3163                                                    usurf->surfaceid, param1, param2, param3);
3164                 break;
3165
3166             case ICO_WINDOW_MGR_WINDOW_CONFIGURE:
3167                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) CONFIGURE"
3168                            "(surf=%08x,app=%s,node=%x,layer=%x,x/y=%d/%d,w/h=%d/%d,hint=%d)",
3169                            (int)mgr->resource, usurf->surfaceid, usurf->uclient->appid,
3170                            usurf->node_tbl->node, usurf->win_layer->layer,
3171                            param1, param2, param3, param4, param5);
3172                 ico_window_mgr_send_window_configure(mgr->resource, usurf->surfaceid,
3173                                                      usurf->node_tbl->node,
3174                                                      usurf->win_layer->layer,
3175                                                      param1, param2, param3, param4, param5);
3176                 break;
3177
3178             case ICO_WINDOW_MGR_WINDOW_ACTIVE:
3179                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) ACTIVE"
3180                            "(surf=%08x,active=%d)", (int)mgr->resource, usurf->surfaceid,
3181                            param1);
3182                 ico_window_mgr_send_window_active(mgr->resource, usurf->surfaceid,
3183                                                   (uint32_t)param1);
3184                 break;
3185
3186             case ICO_WINDOW_MGR_LAYER_VISIBLE:
3187                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) LAYER_VISIBLE"
3188                            "(layer=%x,visivle=%d)", (int)mgr->resource, param1, param2);
3189                 ico_window_mgr_send_layer_visible(mgr->resource, (uint32_t)param1, param2);
3190                 break;
3191
3192             default:
3193                 uifw_error("ico_win_mgr_send_to_mgr: Illegal event(%08x)", event);
3194                 break;
3195             }
3196         }
3197     }
3198     return num_mgr;
3199 }
3200
3201 /*--------------------------------------------------------------------------*/
3202 /**
3203  * @brief   win_mgr_set_scale: set surface transform scale
3204  *
3205  * @param[in]   usurf       UIFW surface
3206  * @return      chagne display
3207  * @retval      =1          change display
3208  * @retval      =0          no change
3209  */
3210 /*--------------------------------------------------------------------------*/
3211 static int
3212 win_mgr_set_scale(struct uifw_win_surface *usurf)
3213 {
3214     struct weston_surface   *es;
3215     float   scalex;
3216     float   scaley;
3217     int     ret = 0;
3218
3219     es = usurf->surface;
3220     if ((es != NULL) && (es->buffer_ref.buffer))  {
3221         if (usurf->client_width == 0)   usurf->client_width = es->geometry.width;
3222         if (usurf->client_height == 0)  usurf->client_height = es->geometry.height;
3223         scalex = (float)usurf->width / (float)usurf->client_width;
3224         scaley = (float)usurf->height / (float)usurf->client_height;
3225         uifw_trace("win_mgr_set_scale: %08x X=%4.2f(%d/%d) Y=%4.2f(%d/%d)",
3226                    usurf->surfaceid, scalex, usurf->width, usurf->client_width,
3227                    scaley, usurf->height, usurf->client_height);
3228         usurf->xadd = 0;
3229         usurf->yadd = 0;
3230         if (usurf->attributes & ICO_WINDOW_MGR_ATTR_FIXED_ASPECT)   {
3231             if (scalex > scaley)    {
3232                 scalex = scaley;
3233                 if ((usurf->attributes & ICO_WINDOW_MGR_ATTR_ALIGN_LEFT) == 0)  {
3234                     usurf->xadd = (float)usurf->width - ((float)usurf->client_width * scalex);
3235                     if ((usurf->attributes & ICO_WINDOW_MGR_ATTR_ALIGN_RIGHT) == 0) {
3236                         usurf->xadd /= 2;
3237                     }
3238                 }
3239             }
3240             else if (scalex < scaley)   {
3241                 scaley = scalex;
3242                 if ((usurf->attributes & ICO_WINDOW_MGR_ATTR_ALIGN_TOP) == 0)   {
3243                     usurf->yadd = (float)usurf->height - ((float)usurf->client_height * scaley);
3244                     if ((usurf->attributes & ICO_WINDOW_MGR_ATTR_ALIGN_BOTTOM) == 0)    {
3245                         usurf->yadd /= 2;
3246                     }
3247                 }
3248             }
3249             uifw_trace("win_mgr_set_scale: %08x fixed aspect x/yadd=%d/%d",
3250                        usurf->surfaceid, usurf->xadd, usurf->yadd);
3251         }
3252         if ((scalex != usurf->scalex) || (scaley != usurf->scaley)) {
3253             usurf->scalex = scalex;
3254             usurf->scaley = scaley;
3255             if ((scalex != 1.0f) || (scaley != 1.0f))   {
3256                 weston_matrix_init(&usurf->transform.matrix);
3257                 weston_matrix_scale(&usurf->transform.matrix, scalex, scaley, 1.0f);
3258                 uifw_trace("win_mgr_set_scale: change scale(%d)", usurf->set_transform);
3259                 if (usurf->set_transform == 0)  {
3260                     usurf->set_transform = 1;
3261                     wl_list_init(&usurf->transform.link);
3262                     wl_list_insert(&es->geometry.transformation_list, &usurf->transform.link);
3263                 }
3264             }
3265             else if (usurf->set_transform != 0) {
3266                 uifw_trace("win_mgr_set_scale: reset transform");
3267                 usurf->set_transform = 0;
3268                 wl_list_remove(&usurf->transform.link);
3269             }
3270             weston_surface_geometry_dirty(es);
3271             weston_surface_update_transform(es);
3272             weston_surface_damage_below(es);
3273             weston_surface_damage(es);
3274             ret = 1;
3275         }
3276     }
3277     return ret;
3278 }
3279
3280 /*--------------------------------------------------------------------------*/
3281 /**
3282  * @brief   ico_window_mgr_get_uclient: get UIFW client table
3283  *
3284  * @param[in]   appid       application Id
3285  * @return      UIFW client table
3286  * @retval      !=NULL      success(UIFW client table address)
3287  * @retval      = NULL      error(appid not exist)
3288  */
3289 /*--------------------------------------------------------------------------*/
3290 WL_EXPORT   struct uifw_client *
3291 ico_window_mgr_get_uclient(const char *appid)
3292 {
3293     struct uifw_client *uclient;
3294
3295     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
3296         if (strcmp(uclient->appid, appid) == 0) {
3297             return uclient;
3298         }
3299     }
3300     return NULL;
3301 }
3302
3303 /*--------------------------------------------------------------------------*/
3304 /**
3305  * @brief   ico_window_mgr_get_client_usurf: get client UIFW surface table
3306  *
3307  * @param[in]   appid       application Id
3308  * @param[in]   winname     window name
3309  * @return      UIFW surface table
3310  * @retval      !=NULL      success(UIFW surface table address)
3311  * @retval      = NULL      error(appid or winname not exist)
3312  */
3313 /*--------------------------------------------------------------------------*/
3314 WL_EXPORT   struct uifw_win_surface *
3315 ico_window_mgr_get_client_usurf(const char *appid, const char *winname)
3316 {
3317     struct uifw_client      *uclient;
3318     struct uifw_win_surface *usurf;
3319
3320     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
3321         if (strcmp(uclient->appid, appid) == 0) {
3322             wl_list_for_each (usurf, &uclient->surface_link, client_link)   {
3323                 if ((winname == NULL) || (*winname == 0) ||
3324                     (strcmp(winname, usurf->winname) == 0)) {
3325                     return usurf;
3326                 }
3327             }
3328         }
3329     }
3330     return NULL;
3331 }
3332
3333 /*--------------------------------------------------------------------------*/
3334 /**
3335  * @brief   ico_window_mgr_set_hook_animation: set animation hook routine
3336  *
3337  * @param[in]   hook_animation  hook routine
3338  * @return      none
3339  */
3340 /*--------------------------------------------------------------------------*/
3341 WL_EXPORT   void
3342 ico_window_mgr_set_hook_animation(int (*hook_animation)(const int op, void *data))
3343 {
3344     win_mgr_hook_animation = hook_animation;
3345 }
3346
3347 /*--------------------------------------------------------------------------*/
3348 /**
3349  * @brief   module_init: initialize ico_window_mgr
3350  *                       this function called from ico_pluign_loader
3351  *
3352  * @param[in]   es          weston compositor
3353  * @param[in]   argc        number of arguments(unused)
3354  * @param[in]   argv        argument list(unused)
3355  * @return      result
3356  * @retval      0           sccess
3357  * @retval      -1          error
3358  */
3359 /*--------------------------------------------------------------------------*/
3360 WL_EXPORT   int
3361 module_init(struct weston_compositor *ec, int *argc, char *argv[])
3362 {
3363     int     nodeId;
3364     int     i;
3365     int     idx;
3366     char    *p;
3367     struct weston_output *output;
3368     struct weston_config_section *section;
3369     char    *displayno = NULL;
3370
3371     uifw_info("ico_window_mgr: Enter(module_init)");
3372
3373     /* get ivi debug level                      */
3374     section = weston_config_get_section(ec->config, "ivi-debug", NULL, NULL);
3375     if (section)    {
3376         weston_config_section_get_int(section, "flag", &_ico_ivi_debug_flag, 0);
3377         weston_config_section_get_int(section, "log", &_ico_ivi_debug_level, 3);
3378     }
3379
3380     /* get display number list                  */
3381     section = weston_config_get_section(ec->config, "ivi-display", NULL, NULL);
3382     if (section)    {
3383         weston_config_section_get_string(section, "displayno", &displayno, NULL);
3384     }
3385
3386     /* get layer id                             */
3387     section = weston_config_get_section(ec->config, "ivi-layer", NULL, NULL);
3388     if (section)    {
3389         weston_config_section_get_int(section, "default", &_ico_ivi_default_layer, 1);
3390         weston_config_section_get_int(section, "startup", &_ico_ivi_startup_layer, 109);
3391         weston_config_section_get_int(section, "input", &_ico_ivi_input_layer, 101);
3392         weston_config_section_get_int(section, "cursor", &_ico_ivi_cursor_layer, 102);
3393         weston_config_section_get_int(section, "background", &_ico_ivi_background_layer, 0);
3394     }
3395
3396     /* get animation default                    */
3397     section = weston_config_get_section(ec->config, "ivi-animation", NULL, NULL);
3398     if (section)    {
3399         weston_config_section_get_string(section, "default", &_ico_ivi_animation_name, NULL);
3400         weston_config_section_get_int(section, "time", &_ico_ivi_animation_time, 600);
3401         weston_config_section_get_int(section, "fps", &_ico_ivi_animation_fps, 15);
3402     }
3403     if (_ico_ivi_animation_time < 100)  _ico_ivi_animation_time = 600;
3404     if (_ico_ivi_animation_fps < 2)     _ico_ivi_animation_fps = 15;
3405
3406     /* create ico_window_mgr management table   */
3407     _ico_win_mgr = (struct ico_win_mgr *)malloc(sizeof(struct ico_win_mgr));
3408     if (_ico_win_mgr == NULL)   {
3409         uifw_error("ico_window_mgr: malloc failed");
3410         return -1;
3411     }
3412
3413     memset(_ico_win_mgr, 0, sizeof(struct ico_win_mgr));
3414     _ico_win_mgr->surfaceid_map = (uint16_t *) malloc(INIT_SURFACE_IDS/8);
3415     if (! _ico_win_mgr->surfaceid_map)  {
3416         uifw_error("ico_window_mgr: malloc failed");
3417         return -1;
3418     }
3419     uifw_debug("ico_window_mgr: table=%08x", (int)_ico_win_mgr);
3420     memset(_ico_win_mgr->surfaceid_map, 0, INIT_SURFACE_IDS/8);
3421
3422     _ico_win_mgr->compositor = ec;
3423
3424     _ico_win_mgr->surfaceid_max = INIT_SURFACE_IDS;
3425     _ico_win_mgr->surfaceid_count = INIT_SURFACE_IDS;
3426
3427     uifw_trace("ico_window_mgr: wl_global_create(bind_ico_win_mgr)");
3428     if (wl_global_create(ec->wl_display, &ico_window_mgr_interface, 1,
3429                          _ico_win_mgr, bind_ico_win_mgr) == NULL)  {
3430         uifw_error("ico_window_mgr: Error(wl_global_create)");
3431         return -1;
3432     }
3433
3434     wl_list_init(&_ico_win_mgr->client_list);
3435     wl_list_init(&_ico_win_mgr->manager_list);
3436     wl_list_init(&_ico_win_mgr->ivi_layer_list);
3437     wl_list_init(&_ico_win_mgr->map_list);
3438
3439     _ico_win_mgr->free_maptable = NULL;
3440
3441     /* create display list                  */
3442     if (displayno != NULL)   {
3443         p = displayno;
3444     }
3445     else    {
3446         p = NULL;
3447     }
3448     _ico_num_nodes = 0;
3449     wl_list_for_each(output, &ec->output_list, link) {
3450         wl_list_init(&_ico_win_mgr->map_animation[_ico_num_nodes].link);
3451         _ico_win_mgr->map_animation[_ico_num_nodes].frame = win_mgr_check_surfacemap;
3452         wl_list_insert(output->animation_list.prev,
3453                        &_ico_win_mgr->map_animation[_ico_num_nodes].link);
3454         _ico_num_nodes++;
3455         if (_ico_num_nodes >= ICO_IVI_MAX_DISPLAY)   break;
3456     }
3457     memset(&_ico_node_table[0], 0, sizeof(_ico_node_table));
3458     i = 0;
3459     wl_list_for_each(output, &ec->output_list, link) {
3460         p = strtok(p, ",");
3461         if (p)  {
3462             idx = strtol(p, (char **)0, 0);
3463             uifw_trace("ico_window_mgr: config Display.%d is %d", i, idx);
3464             p = NULL;
3465             if ((idx < 0) || (idx >= _ico_num_nodes))   {
3466                 idx = i;
3467             }
3468         }
3469         else    {
3470             idx = i;
3471         }
3472         if (_ico_node_table[idx].node)  {
3473             for (idx = 0; idx < _ico_num_nodes; idx++)  {
3474                 if (_ico_node_table[idx].node == 0) break;
3475             }
3476             if (idx >= _ico_num_nodes)  {
3477                 uifw_error("ico_window_mgr: number of display overflow");
3478                 idx = 0;
3479             }
3480         }
3481         _ico_node_table[idx].node = idx + 0x100;
3482         _ico_node_table[idx].displayno = i;
3483         _ico_node_table[idx].disp_x = output->x;
3484         _ico_node_table[idx].disp_y = output->y;
3485         _ico_node_table[idx].disp_width = output->width;
3486         _ico_node_table[idx].disp_height = output->height;
3487         i ++;
3488         if (i >= _ico_num_nodes) break;
3489     }
3490     idx = 0;
3491     for (i = 0; i < _ico_num_nodes; i++)    {
3492         _ico_node_table[i].node &= 0x0ff;
3493         uifw_trace("ico_window_mgr: Display.%d no=%d x/y=%d/%d w/h=%d/%d",
3494                    i, _ico_node_table[i].displayno,
3495                    _ico_node_table[i].disp_x, _ico_node_table[i].disp_y,
3496                    _ico_node_table[i].disp_width, _ico_node_table[i].disp_height);
3497     }
3498     if (displayno)  free(displayno);
3499
3500     /* my node Id ... this version fixed 0  */
3501     nodeId = ico_ivi_get_mynode();
3502
3503     _ico_win_mgr->surface_head = ICO_IVI_SURFACEID_BASE(nodeId);
3504     uifw_trace("ico_window_mgr: NoedId=%04x SurfaceIdBase=%08x",
3505                 nodeId, _ico_win_mgr->surface_head);
3506
3507     /* Hook to IVI-Shell                            */
3508     ico_ivi_shell_hook_bind(win_mgr_bind_client);
3509     ico_ivi_shell_hook_unbind(win_mgr_unbind_client);
3510     ico_ivi_shell_hook_create(win_mgr_register_surface);
3511     ico_ivi_shell_hook_destroy(win_mgr_destroy_surface);
3512     ico_ivi_shell_hook_map(win_mgr_map_surface);
3513     ico_ivi_shell_hook_configure(win_mgr_shell_configure);
3514     ico_ivi_shell_hook_select(win_mgr_select_surface);
3515     ico_ivi_shell_hook_title(win_mgr_set_title);
3516     ico_ivi_shell_hook_move(win_mgr_surface_move);
3517
3518     uifw_info("ico_window_mgr: animation name=%s time=%d fps=%d",
3519               _ico_ivi_animation_name, _ico_ivi_animation_time, _ico_ivi_animation_fps);
3520     uifw_info("ico_window_mgr: layer default=%d startup=%d background=%d",
3521               _ico_ivi_default_layer, _ico_ivi_startup_layer, _ico_ivi_background_layer);
3522     uifw_info("ico_window_mgr: layer input=%d cursor=%d",
3523               _ico_ivi_input_layer, _ico_ivi_cursor_layer);
3524     uifw_info("ico_window_mgr: debug flag=%x log level=%d",
3525               _ico_ivi_debug_flag, _ico_ivi_debug_level);
3526
3527     uifw_info("ico_window_mgr: Leave(module_init)");
3528
3529     return 0;
3530 }
3531