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