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