fec092b65e678b17099a3d1c4383691c01af87d2
[profile/ivi/ico-uxf-weston-plugin.git] / src / ico_window_mgr.c
1 /*
2  * Copyright © 2010-2011 Intel Corporation
3  * Copyright © 2008-2011 Kristian Høgsberg
4  * Copyright © 2013-2014 TOYOTA MOTOR CORPORATION.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the copyright holders not be used in
11  * advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission.  The copyright holders make
13  * no representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24 /**
25  * @brief   Multi Window Manager (Weston(Wayland) PlugIn)
26  *
27  * @date    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/mman.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <limits.h>
47 #include <pixman.h>
48 #include <wayland-server.h>
49 #include <dirent.h>
50 #include <aul/aul.h>
51 #include <bundle.h>
52
53 #include <EGL/egl.h>
54 #include <GLES2/gl2.h>
55 #include <GLES2/gl2ext.h>
56 #include <GL/internal/dri_interface.h>
57
58 #include <weston/compositor.h>
59 #include <libdrm/intel_bufmgr.h>
60
61 /* detail debug log */
62 #define UIFW_DETAIL_OUT 0                   /* 1=detail debug log/0=no detail log   */
63
64 #include "ico_ivi_common_private.h"
65 #include "ico_ivi_shell_private.h"
66 #include "ico_window_mgr_private.h"
67 #include "ico_window_mgr.h"
68 #include "desktop-shell-server-protocol.h"
69 #include "ico_window_mgr-server-protocol.h"
70
71 /* SurfaceID                            */
72 #define INIT_SURFACE_IDS    1024            /* SurfaceId table initiale size        */
73 #define ADD_SURFACE_IDS     512             /* SurfaceId table additional size      */
74 #define SURCAFE_ID_MASK     0x0ffff         /* SurfaceId bit mask pattern           */
75 #define UIFW_HASH           64              /* Hash value (2's compliment)          */
76
77 /* Internal fixed value                 */
78 #define ICO_WINDOW_MGR_APPID_FIXCOUNT   5   /* retry count of appid fix             */
79                                             /* show/hide animation with position    */
80 #define ICO_WINDOW_MGR_ANIMATION_POS    0x10000000
81
82 /* GPU type code                        */
83 #define ICO_GPUTYPE_NOACCELERATION      0   /* Do not use GPU-dependent acceleration*/
84 #define ICO_GPUTYPE_INTEL_SANDYBRIDGE   1   /* Intel Sandybridge Mobile             */
85
86 /* Code for Intel Sandybridge Mobile GPU dependent acceleration */
87
88 /* wl_drm_buffer (inport from mesa-9.1.3 & mesa-9.2.1/          */
89 /*                src/egl/wayland/wayland-drm/wayland-drm.h)    */
90 struct uifw_drm_buffer {
91     struct wl_resource *resource;
92     void *drm;                  /* struct wl_drm    */
93     int32_t width, height;
94     uint32_t format;
95     const void *driver_format;
96     int32_t offset[3];
97     int32_t stride[3];
98     void *driver_buffer;
99 };
100
101 /* __DRIimage (inport from mesa-9.1.3/src/mesa/drivers/dri/intel/intel_regions.h    */
102 /*                         mesa-9.2.1/src/mesa/drivers/dri/i915/intel_regions.h     */
103 /*                         mesa-9.2.1/src/mesa/drivers/dri/i965/intel_regions.h)    */
104 struct uifw_intel_region    {   /* struct intel_region for mesa 9.2.1   */
105    void *bo;                /**< buffer manager's buffer */
106    uint32_t refcount;       /**< Reference count for region */
107    uint32_t cpp;            /**< bytes per pixel */
108    uint32_t width;          /**< in pixels */
109    uint32_t height;         /**< in pixels */
110    uint32_t pitch;          /**< in bytes */
111    uint32_t tiling;         /**< Which tiling mode the region is in */
112    uint32_t name;           /**< Global name for the bo */
113 };
114
115 struct uifw_dri_image   {       /* struct __DRIimageRec */
116    struct uifw_intel_region *region;
117    int internal_format;
118    uint32_t dri_format;
119    uint32_t format;
120    uint32_t offset;
121    uint32_t strides[3];
122    uint32_t offsets[3];
123    void *planar_format; /* intel_image_format */
124    uint32_t width;
125    uint32_t height;
126    uint32_t tile_x;
127    uint32_t tile_y;
128    bool has_depthstencil;           /* i965 only    */
129    void *data;
130 };
131
132 /* gl_surface_state (inport from weston-1.2.1/src/gl-renderer.c,    */
133 /*                               weston-1.3.1/src/gl-renderer.c     */
134 enum buffer_type {
135     BUFFER_TYPE_NULL,
136     BUFFER_TYPE_SHM,
137     BUFFER_TYPE_EGL
138 };
139 struct uifw_gl_surface_state {      /* struct gl_surface_state  */
140     GLfloat color[4];
141     struct gl_shader *shader;
142
143     GLuint textures[3];
144     int num_textures;
145     int needs_full_upload;
146     pixman_region32_t texture_damage;
147
148     void *images[3];            /* EGLImageKHR */
149     GLenum target;
150     int num_images;
151
152     struct weston_buffer_reference buffer_ref;
153     enum buffer_type buffer_type;
154     int pitch; /* in pixels */
155     int height; /* in pixels */
156     int y_inverted;         /* add weston 1.3.x */
157 };
158
159 /* API access control configuration     */
160 struct ico_api_permit    {
161     struct ico_api_permit       *next;      /* access control configuration list    */
162     struct ico_uifw_api_permit  permit;     /* permission                           */
163 };
164
165 /* Multi Windiw Manager                 */
166 struct ico_win_mgr {
167     struct weston_compositor *compositor;   /* Weston compositor                    */
168     void    *shell;                         /* shell(ico_ivi_shell) table address   */
169     int32_t surface_head;                   /* (HostID << 24) | (DisplayNo << 16)   */
170
171     struct wl_list  client_list;            /* Clients                              */
172     struct wl_list  manager_list;           /* Manager(ex.HomeScreen) list          */
173     int             num_manager;            /* Number of managers                   */
174
175     struct wl_list  ivi_layer_list;         /* Layer management table list          */
176     struct uifw_win_layer *touch_layer;     /* layer table for touch panel layer    */
177
178     struct wl_list  map_list;               /* surface map list                     */
179     struct uifw_surface_map *free_maptable; /* free maped surface table list        */
180     struct weston_animation map_animation[ICO_IVI_MAX_DISPLAY];
181                                             /* animation for map check              */
182     struct wl_event_source  *wait_mapevent; /* map event send wait timer            */
183
184     struct uifw_win_surface *active_pointer_usurf;  /* Active Pointer Surface       */
185     struct uifw_win_surface *active_keyboard_usurf; /* Active Keyboard Surface      */
186
187     struct uifw_win_surface *idhash[UIFW_HASH];  /* UIFW SerfaceID                  */
188     struct uifw_win_surface *wshash[UIFW_HASH];  /* Weston Surface                  */
189
190     uint32_t surfaceid_count;               /* Number of surface id                 */
191     uint32_t surfaceid_max;                 /* Maximum number of surface id         */
192     uint16_t *surfaceid_map;                /* SurfaceId assign bit map             */
193
194     struct ico_api_permit   *access_config; /* access control configuration list    */
195     uint32_t default_access;                /* default permit APIs                  */
196
197     char    shell_init;                     /* shell initialize flag                */
198     char    res[3];                         /* (unused)                             */
199 };
200
201 /* access control api table             */
202 static const struct {
203     char        name[24];                   /* API name                             */
204     uint32_t    api;                        /* API flag                             */
205 } access_api_table[] = {
206     { "declare_manager", ICO_UIFW_WINDOW_MGR_DECLARE_MANAGER },
207     { "set_window_layer", ICO_UIFW_WINDOW_MGR_SET_WINDOW_LAYER },
208     { "set_positionsize", ICO_UIFW_WINDOW_MGR_SET_POSITIONSIZE },
209     { "set_visible", ICO_UIFW_WINDOW_MGR_SET_VISIBLE },
210     { "set_animation", ICO_UIFW_WINDOW_MGR_SET_ANIMATION },
211     { "set_attributes", ICO_UIFW_WINDOW_MGR_SET_ATTRIBUTES },
212     { "visible_animation", ICO_UIFW_WINDOW_MGR_VISIBLE_ANIMATION },
213     { "set_active", ICO_UIFW_WINDOW_MGR_SET_ACTIVE },
214     { "set_layer_visible", ICO_UIFW_WINDOW_MGR_SET_LAYER_VISIBLE },
215     { "get_surfaces", ICO_UIFW_WINDOW_MGR_GET_SURFACES },
216     { "set_map_buffer", ICO_UIFW_WINDOW_MGR_SET_MAP_BUFFER },
217     { "map_surface", ICO_UIFW_WINDOW_MGR_MAP_SURFACE },
218     { "unmap_surface", ICO_UIFW_WINDOW_MGR_UNMAP_SURFACE },
219
220     { "add_input_app", ICO_UIFW_INPUT_MGR_CONTROL_ADD_INPUT_APP },
221     { "del_input_app", ICO_UIFW_INPUT_MGR_CONTROL_DEL_INPUT_APP },
222     { "send_input_event", ICO_UIFW_INPUT_MGR_CONTROL_SEND_INPUT_EVENT },
223     { "set_input_region", ICO_UIFW_EXINPUT_SET_INPUT_REGION },
224     { "unset_input_region", ICO_UIFW_EXINPUT_UNSET_INPUT_REGION },
225     { "confiure_input", ICO_UIFW_INPUT_MGR_DEVICE_CONFIGURE_INPUT },
226     { "confiure_code", ICO_UIFW_INPUT_MGR_DEVICE_CONFIGURE_CODE },
227     { "input_event", ICO_UIFW_INPUT_MGR_DEVICE_INPUT_EVENT },
228
229     { "\0", 0 }
230 };
231
232 /* Internal macros                      */
233 /* UIFW SurfaceID                       */
234 #define MAKE_IDHASH(v)  (((uint32_t)v) & (UIFW_HASH-1))
235 /* Weston Surface                       */
236 #define MAKE_WSHASH(v)  ((((uint32_t)v) >> 5) & (UIFW_HASH-1))
237
238 /* function prototype                   */
239                                             /* get surface table from weston surface*/
240 static struct uifw_win_surface *find_uifw_win_surface_by_ws(
241                     struct weston_surface *wsurf);
242                                             /* assign new surface id                */
243 static uint32_t generate_id(void);
244                                             /* bind shell client                    */
245 static void win_mgr_bind_client(struct wl_client *client, void *shell);
246                                             /* unind shell client                   */
247 static void win_mgr_unbind_client(struct wl_client *client);
248 #if 0       /* work around: Walk through child processes until app ID is found  */
249                                             /* get parent pid                       */
250 static pid_t win_mgr_get_ppid(pid_t pid);
251 #endif      /* work around: Walk through child processes until app ID is found  */
252                                             /* get appid from pid                   */
253 static void win_mgr_get_client_appid(struct uifw_client *uclient);
254                                             /* create new surface                   */
255 static void win_mgr_register_surface(int layertype, struct weston_surface *surface,
256                                      struct wl_client *client,
257                                      struct shell_surface *shsurf);
258                                             /* surface destroy                      */
259 static void win_mgr_destroy_surface(struct weston_surface *surface);
260                                             /* map new surface                      */
261 static void win_mgr_surface_map(struct weston_surface *surface, int32_t *width,
262                                 int32_t *height, int32_t *sx, int32_t *sy);
263                                             /* send surface change event to manager */
264 static void win_mgr_change_surface(struct weston_surface *surface,
265                                    const int to, const int manager);
266                                             /* window manager surface configure     */
267 static void win_mgr_surface_configure(struct uifw_win_surface *usurf,
268                                       int x, int y, int width, int height);
269                                             /* shell surface configure              */
270 static void win_mgr_shell_configure(struct weston_surface *surface);
271                                             /* surface select                       */
272 static void win_mgr_select_surface(struct weston_surface *surface);
273                                             /* surface set title                    */
274 static char *win_mgr_set_title(struct weston_surface *surface, const char *title);
275                                             /* surface move request from shell      */
276 static void win_mgr_surface_move(struct weston_surface *surface, int *dx, int *dy);
277                                             /* shell layer visible control          */
278 static void win_mgr_show_layer(int layertype, int show, void *data);
279                                             /* shell full screen surface control    */
280 static int win_mgr_fullscreen(int event, struct weston_surface *surface);
281                                             /* set raise                            */
282 static void win_mgr_set_raise(struct uifw_win_surface *usurf, const int raise);
283                                             /* surface change from manager          */
284 static int win_mgr_surface_change_mgr(struct weston_surface *surface, const int x,
285                                       const int y, const int width, const int height);
286                                             /* reset surface focus                  */
287 static void win_mgr_reset_focus(struct uifw_win_surface *usurf);
288                                             /* create new layer                     */
289 static struct uifw_win_layer *win_mgr_create_layer(struct uifw_win_surface *usurf,
290                                                    const uint32_t layer, const int layertype);
291                                             /* set surface layer                    */
292 static void win_mgr_set_layer(struct uifw_win_surface *usurf, const uint32_t layer);
293                                             /* set active surface                   */
294 static void win_mgr_set_active(struct uifw_win_surface *usurf, const int target);
295
296                                             /* declare manager                      */
297 static void uifw_declare_manager(struct wl_client *client, struct wl_resource *resource,
298                                  int manager);
299                                             /* set window layer                     */
300 static void uifw_set_window_layer(struct wl_client *client,
301                                   struct wl_resource *resource,
302                                   uint32_t surfaceid, uint32_t layer);
303                                             /* set surface size and position        */
304 static void uifw_set_positionsize(struct wl_client *client, struct wl_resource *resource,
305                                   uint32_t surfaceid, uint32_t node, int32_t x, int32_t y,
306                                   int32_t width, int32_t height, int32_t flags);
307                                             /* show/hide and raise/lower surface    */
308 static void uifw_set_visible(struct wl_client *client, struct wl_resource *resource,
309                              uint32_t surfaceid, int32_t visible, int32_t raise,
310                              int32_t flag);
311                                             /* set surface animation                */
312 static void uifw_set_animation(struct wl_client *client, struct wl_resource *resource,
313                                uint32_t surfaceid, int32_t type,
314                                const char *animation, int32_t time);
315                                             /* set surface attributes               */
316 static void uifw_set_attributes(struct wl_client *client, struct wl_resource *resource,
317                                 uint32_t surfaceid, uint32_t attributes);
318                                             /* surface visibility control with animation*/
319 static void uifw_visible_animation(struct wl_client *client, struct wl_resource *resource,
320                                    uint32_t surfaceid, int32_t visible,
321                                    int32_t x, int32_t y, int32_t width, int32_t height);
322                                             /* set active surface (form HomeScreen) */
323 static void uifw_set_active(struct wl_client *client, struct wl_resource *resource,
324                             uint32_t surfaceid, int32_t active);
325                                             /* layer visibility control             */
326 static void uifw_set_layer_visible(struct wl_client *client, struct wl_resource *resource,
327                                    uint32_t layer, int32_t visible);
328                                             /* get application surfaces             */
329 static void uifw_get_surfaces(struct wl_client *client, struct wl_resource *resource,
330                               const char *appid, int32_t pid);
331                                             /* check and change all mapped surface  */
332 static void win_mgr_check_mapsurface(struct weston_animation *animation,
333                                      struct weston_output *output, uint32_t msecs);
334                                             /* check timer of mapped surface        */
335 static int win_mgr_timer_mapsurface(void *data);
336                                             /* check and change mapped surface      */
337 static void win_mgr_change_mapsurface(struct uifw_surface_map *sm, int event,
338                                       uint32_t curtime);
339                                             /* set map buffer                       */
340 static void uifw_set_map_buffer(struct wl_client *client, struct wl_resource *resource,
341                                 const char *shmname, uint32_t bufsize, uint32_t bufnum);
342                                             /* map surface to system application    */
343 static void uifw_map_surface(struct wl_client *client, struct wl_resource *resource,
344                              uint32_t surfaceid, int32_t framerate);
345                                             /* unmap surface                        */
346 static void uifw_unmap_surface(struct wl_client *client, struct wl_resource *resource,
347                                uint32_t surfaceid);
348                                             /* bind manager                         */
349 static void bind_ico_win_mgr(struct wl_client *client,
350                              void *data, uint32_t version, uint32_t id);
351                                             /* unbind manager                       */
352 static void unbind_ico_win_mgr(struct wl_resource *resource);
353                                             /* send event to manager                */
354 static int ico_win_mgr_send_to_mgr(const int event, struct uifw_win_surface *usurf,
355                                    const int param1, const int param2, const int param3,
356                                    const int param4, const int param5);
357                                             /* set surface transform                */
358 static int win_mgr_set_scale(struct uifw_win_surface *usurf);
359                                             /* convert animation name to Id value   */
360 static int ico_get_animation_name(const char *animation);
361                                             /* read surface pixels                  */
362 static int ico_read_surface_pixels(struct weston_surface *es, pixman_format_code_t format,
363                                    void *pixels, int x, int y, int width, int height);
364                                             /* hook for animation                   */
365 static int  (*win_mgr_hook_animation)(const int op, void *data) = NULL;
366                                             /* hook for input region                */
367 static void (*win_mgr_hook_change)(struct uifw_win_surface *usurf) = NULL;
368 static void (*win_mgr_hook_destory)(struct uifw_win_surface *usurf) = NULL;
369 static void (*win_mgr_hook_inputregion)(int set, struct uifw_win_surface *usurf,
370                                         int32_t x, int32_t y, int32_t width,
371                                         int32_t height, int32_t hotspot_x, int32_t hotspot_y,
372                                         int32_t cursor_x, int32_t cursor_y,
373                                         int32_t cursor_width, int32_t cursor_height,
374                                         uint32_t attr) = NULL;
375
376 /* static tables                        */
377 /* Multi Window Manager interface       */
378 static const struct ico_window_mgr_interface ico_window_mgr_implementation = {
379     uifw_declare_manager,
380     uifw_set_window_layer,
381     uifw_set_positionsize,
382     uifw_set_visible,
383     uifw_set_animation,
384     uifw_set_attributes,
385     uifw_visible_animation,
386     uifw_set_active,
387     uifw_set_layer_visible,
388     uifw_get_surfaces,
389     uifw_set_map_buffer,
390     uifw_map_surface,
391     uifw_unmap_surface
392 };
393
394
395 /* GPU driver name          */
396 static const struct _ico_gputype_table  {
397     int     gpu_type;                           /* GPU type code                    */
398     char    *gpu_name;                          /* GPU driver name                  */
399 }   ico_window_mgr_gpu_type[] = {
400     { ICO_GPUTYPE_INTEL_SANDYBRIDGE, "Mesa DRI Intel(R) Sandybridge Mobile" },
401     { 0, "\0" }
402 };
403
404 /* plugin common value(without ico_plugin_loader)   */
405 static int  _ico_ivi_option_flag = 0;           /* option flags                     */
406 static int  _ico_ivi_debug_level = 3;           /* debug Level                      */
407 static char *_ico_ivi_animation_name = NULL;    /* default animation name           */
408 static int  _ico_ivi_animation_time = 500;      /* default animation time           */
409 static int  _ico_ivi_animation_fps = 30;        /* animation frame rate             */
410 static char *_ico_ivi_inputpanel_animation = NULL; /* default animation name for input panel*/
411 static int  _ico_ivi_inputpanel_anima_time = 0; /* default animation time for input panel*/
412
413 static int  _ico_ivi_inputpanel_display = 0;    /* input panel display number       */
414 static int  _ico_ivi_inputdeco_mag = 100;       /* input panel magnification rate(%)*/
415 static int  _ico_ivi_inputdeco_diff = 0;        /* input panel difference from the bottom*/
416
417 static int  _ico_ivi_background_layer = 0;      /* background layer                 */
418 static int  _ico_ivi_default_layer = 1;         /* deafult layer id at surface create*/
419 static int  _ico_ivi_touch_layer = 101;         /* touch panel layer id             */
420 static int  _ico_ivi_cursor_layer = 102;        /* cursor layer id                  */
421 static int  _ico_ivi_startup_layer = 109;       /* deafult layer id at system startup*/
422
423 static int  _ico_ivi_gpu_type = 0;              /* GPU type for get EGL buffer      */
424 static int  _ico_ivi_map_framerate_gpu = 30;    /* framerate of map surface for GPU*/
425 static int  _ico_ivi_map_framerate_shm = 2;     /* framerate of map surface for shm buffer*/
426 static int  _ico_ivi_map_framerate_pixel = 5;   /* framerate of map surface for ReadPixels*/
427
428 /* static management table              */
429 static struct ico_win_mgr       *_ico_win_mgr = NULL;
430 static int                      _ico_num_nodes = 0;
431 static struct uifw_node_table   _ico_node_table[ICO_IVI_MAX_DISPLAY];
432 static struct weston_seat       *touch_check_seat = NULL;
433
434
435 /*--------------------------------------------------------------------------*/
436 /**
437  * @brief   ico_ivi_optionflag: get option flags
438  *
439  * @param       None
440  * @return      option flags
441  */
442 /*--------------------------------------------------------------------------*/
443 WL_EXPORT   int
444 ico_ivi_optionflag(void)
445 {
446     return _ico_ivi_option_flag;
447 }
448
449 /*--------------------------------------------------------------------------*/
450 /**
451  * @brief   ico_ivi_debuglevel: answer debug output level.
452  *
453  * @param       none
454  * @return      debug output level
455  * @retval      0       No debug output
456  * @retval      1       Only error output
457  * @retval      2       Error and Warning output
458  * @retval      3       Error, Warning and information output
459  * @retval      4       Error, Warning, information and Debug Trace output
460  * @retval      5       All output with debug write
461  */
462 /*--------------------------------------------------------------------------*/
463 WL_EXPORT   int
464 ico_ivi_debuglevel(void)
465 {
466     return (_ico_ivi_debug_level & 0x0ffff);
467 }
468
469 /*--------------------------------------------------------------------------*/
470 /**
471  * @brief   ico_ivi_debugflag: get debug flags
472  *
473  * @param       None
474  * @return      debug flags
475  */
476 /*--------------------------------------------------------------------------*/
477 WL_EXPORT   int
478 ico_ivi_debugflag(void)
479 {
480     return ((_ico_ivi_debug_level >> 16) & 0x0ffff);
481 }
482
483 /*--------------------------------------------------------------------------*/
484 /**
485  * @brief   ico_ivi_default_animation_name: get default animation name
486  *
487  * @param       None
488  * @return      Default animation name
489  */
490 /*--------------------------------------------------------------------------*/
491 WL_EXPORT   const char *
492 ico_ivi_default_animation_name(void)
493 {
494     return _ico_ivi_animation_name;
495 }
496
497 /*--------------------------------------------------------------------------*/
498 /**
499  * @brief   ico_ivi_default_animation_time: get default animation time
500  *
501  * @param       None
502  * @return      Default animation time(miri sec)
503  */
504 /*--------------------------------------------------------------------------*/
505 WL_EXPORT   int
506 ico_ivi_default_animation_time(void)
507 {
508     return _ico_ivi_animation_time;
509 }
510
511 /*--------------------------------------------------------------------------*/
512 /**
513  * @brief   ico_ivi_default_animation_fps: get default animation frame rate
514  *
515  * @param       None
516  * @return      Default animation frame rate(frames/sec)
517  */
518 /*--------------------------------------------------------------------------*/
519 WL_EXPORT   int
520 ico_ivi_default_animation_fps(void)
521 {
522     return _ico_ivi_animation_fps;
523 }
524
525 /*--------------------------------------------------------------------------*/
526 /**
527  * @brief   ico_ivi_get_mynode: Get my NodeId
528  *
529  * @param       None
530  * @return      NodeId of my node
531  */
532 /*--------------------------------------------------------------------------*/
533 WL_EXPORT   int
534 ico_ivi_get_mynode(void)
535 {
536     /* Reference Platform 0.90 only support 1 ECU   */
537     return 0;
538 }
539
540 /*--------------------------------------------------------------------------*/
541 /**
542  * @brief   ico_ivi_surface_buffer_width: get surface buffer width
543  *
544  * @param[in]   es          weston surface
545  * @return      buffer width(if surface has no buffer, return 0)
546  */
547 /*--------------------------------------------------------------------------*/
548 WL_EXPORT   int
549 ico_ivi_surface_buffer_width(struct weston_surface *es)
550 {
551     int v;
552
553     if (! es->buffer_ref.buffer)    {
554         return 0;
555     }
556     if (es->buffer_viewport.viewport_set)   {
557         return es->buffer_viewport.dst_width;
558     }
559     switch (es->buffer_viewport.transform) {
560     case WL_OUTPUT_TRANSFORM_90:
561     case WL_OUTPUT_TRANSFORM_270:
562     case WL_OUTPUT_TRANSFORM_FLIPPED_90:
563     case WL_OUTPUT_TRANSFORM_FLIPPED_270:
564         v = es->buffer_ref.buffer->height;
565         break;
566     default:
567         v = es->buffer_ref.buffer->width;
568         break;
569     }
570     return (v / es->buffer_viewport.scale);
571 }
572
573 /*--------------------------------------------------------------------------*/
574 /**
575  * @brief   ico_ivi_surface_buffer_height: get surface buffer height
576  *
577  * @param[in]   es          weston surface
578  * @return      buffer height(if surface has no buffer, return 0)
579  */
580 /*--------------------------------------------------------------------------*/
581 WL_EXPORT   int
582 ico_ivi_surface_buffer_height(struct weston_surface *es)
583 {
584     int v;
585
586     if (! es->buffer_ref.buffer)    {
587         return 0;
588     }
589     if (es->buffer_viewport.viewport_set)   {
590         return es->buffer_viewport.dst_height;
591     }
592     switch (es->buffer_viewport.transform) {
593     case WL_OUTPUT_TRANSFORM_90:
594     case WL_OUTPUT_TRANSFORM_270:
595     case WL_OUTPUT_TRANSFORM_FLIPPED_90:
596     case WL_OUTPUT_TRANSFORM_FLIPPED_270:
597         v = es->buffer_ref.buffer->width;
598         break;
599     default:
600         v = es->buffer_ref.buffer->height;
601         break;
602     }
603     return (v / es->buffer_viewport.scale);
604 }
605
606 /*--------------------------------------------------------------------------*/
607 /**
608  * @brief   ico_ivi_surface_buffer_size: get surface buffer size
609  *
610  * @param[in]   es          weston surface
611  * @param[out]  width       buffer width
612  * @param[out]  height      buffer height
613  * @return      nothing
614  */
615 /*--------------------------------------------------------------------------*/
616 WL_EXPORT   void
617 ico_ivi_surface_buffer_size(struct weston_surface *es, int *width, int *height)
618 {
619     if (! es->buffer_ref.buffer)    {
620         *width = 0;
621         *height = 0;
622     }
623     else if (es->buffer_viewport.viewport_set)  {
624         *width = es->buffer_viewport.dst_width;
625         *height = es->buffer_viewport.dst_height;
626     }
627     else    {
628         switch (es->buffer_viewport.transform) {
629         case WL_OUTPUT_TRANSFORM_90:
630         case WL_OUTPUT_TRANSFORM_270:
631         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
632         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
633             *width = es->buffer_ref.buffer->height;
634             *height = es->buffer_ref.buffer->width;
635             break;
636         default:
637             *width = es->buffer_ref.buffer->width;
638             *height = es->buffer_ref.buffer->height;
639             break;
640         }
641         *width = *width / es->buffer_viewport.scale;
642         *height = *height / es->buffer_viewport.scale;
643     }
644 }
645
646 /*--------------------------------------------------------------------------*/
647 /**
648  * @brief   ico_ivi_surface_buffer_size: get surface buffer size
649  *
650  * @param[in]   es          weston surface
651  * @param[out]  width       buffer width
652  * @param[out]  height      buffer height
653  * @return      nothing
654  */
655 /*--------------------------------------------------------------------------*/
656 WL_EXPORT   struct weston_view *
657 ico_ivi_get_primary_view(struct uifw_win_surface *usurf)
658 {
659     struct weston_view  *ev;
660
661     if (usurf->layertype == LAYER_TYPE_INPUTPANEL)  {
662         ev = ico_input_panel_get_view((void *)(usurf->shsurf));
663     }
664     else    {
665         ev = (*_ico_win_mgr->compositor->shell_interface.get_primary_view)
666                                                             (NULL, usurf->shsurf);
667     }
668     if (! ev)   {
669         uifw_error("ico_ivi_get_primary_view: usurf=%08x(%x) surface=%08x has no view",
670                    (int)usurf, usurf->surfaceid, (int)usurf->surface);
671     }
672     else    {
673         uifw_debug("ico_ivi_get_primary_view: %08x[%d] view=%08x view->surf=%08x",
674                    usurf->surfaceid, usurf->layertype, (int)ev, (int)ev->surface);
675     }
676     return ev;
677 }
678
679 /*--------------------------------------------------------------------------*/
680 /**
681  * @brief   ico_window_mgr_set_weston_surface: set weston surface
682  *
683  * @param[in]   usurf       UIFW surface
684  * @param[in]   x           X coordinate on screen
685  * @param[in]   y           Y coordinate on screen
686  * @param[in]   width       width
687  * @param[in]   height      height
688  * @return      none
689  */
690 /*--------------------------------------------------------------------------*/
691 WL_EXPORT   void
692 ico_window_mgr_set_weston_surface(struct uifw_win_surface *usurf,
693                                   int x, int y, int width, int height)
694 {
695     struct weston_surface *es = usurf->surface;
696     struct weston_view  *ev;
697     int     buf_width, buf_height;
698
699     if ((es == NULL) || (usurf->shsurf == NULL))    {
700         uifw_trace("ico_window_mgr_set_weston_surface: usurf(%08x) has no surface",
701                    (int)usurf);
702         return;
703     }
704
705     if (es->buffer_ref.buffer != NULL)   {
706         ico_ivi_surface_buffer_size(es, &buf_width, &buf_height);
707         if ((width <= 0) || (height <= 0))    {
708             width = buf_width;
709             usurf->width = buf_width;
710             height = buf_height;
711             usurf->height = buf_height;
712         }
713         if ((usurf->width > buf_width) && (usurf->scalex <= 1.0f))  {
714             width = buf_width;
715             x += (usurf->width - buf_width)/2;
716         }
717         if ((usurf->height > buf_height) && (usurf->scaley <= 1.0f))    {
718             height = buf_height;
719             y += (usurf->height - buf_height)/2;
720         }
721         if (usurf->visible) {
722             x += usurf->node_tbl->disp_x;
723             y += usurf->node_tbl->disp_y;
724         }
725         else    {
726             x = ICO_IVI_MAX_COORDINATE+1;
727             y = ICO_IVI_MAX_COORDINATE+1;
728         }
729         ev = ico_ivi_get_primary_view(usurf);
730         if ((ev != NULL) &&
731             (((int)ev->geometry.x != x) || ((int)ev->geometry.y != y) ||
732              (es->width != width) || (es->height != height)))   {
733             weston_view_damage_below(ev);
734             win_mgr_surface_configure(usurf, x, y, width, height);
735         }
736         weston_surface_damage(es);
737         weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
738     }
739 }
740
741 /*--------------------------------------------------------------------------*/
742 /**
743  * @brief   ico_window_mgr_set_weston_surface: set weston surface
744  *
745  * @param[in]   usurf       UIFW surface
746  * @param[in]   x           X coordinate on screen
747  * @param[in]   y           Y coordinate on screen
748  * @param[in]   width       width
749  * @param[in]   height      height
750  * @return      none
751  */
752 /*--------------------------------------------------------------------------*/
753 WL_EXPORT   void
754 ico_window_mgr_change_surface(struct uifw_win_surface *usurf,
755                               const int to, const int manager)
756 {
757     uifw_trace("ico_window_mgr_change_surface: Enter(%08x,%d,%d)",
758                usurf->surfaceid, to, manager);
759     win_mgr_change_surface(usurf->surface, to, manager);
760     uifw_trace("ico_window_mgr_change_surface: Leave");
761 }
762
763 /*--------------------------------------------------------------------------*/
764 /**
765  * @brief   ico_window_mgr_get_usurf: find UIFW surface by surface id
766  *
767  * @param[in]   surfaceid   UIFW surface id
768  * @return      UIFW surface table address
769  * @retval      !=NULL      success(surface table address)
770  * @retval      NULL        error(surface id dose not exist)
771  */
772 /*--------------------------------------------------------------------------*/
773 WL_EXPORT   struct uifw_win_surface *
774 ico_window_mgr_get_usurf(const uint32_t surfaceid)
775 {
776     struct uifw_win_surface *usurf;
777
778     usurf = _ico_win_mgr->idhash[MAKE_IDHASH(surfaceid)];
779
780     while (usurf)   {
781         if (usurf->surfaceid == surfaceid) {
782             return usurf;
783         }
784         usurf = usurf->next_idhash;
785     }
786     uifw_trace("ico_window_mgr_get_usurf: NULL");
787     return NULL;
788 }
789
790 /*--------------------------------------------------------------------------*/
791 /**
792  * @brief   ico_window_mgr_get_usurf_client: find UIFW surface by surface id/or client
793  *
794  * @param[in]   surfaceid   UIFW surface id
795  * @param[in]   client      Wayland client
796  * @return      UIFW surface table address
797  * @retval      !=NULL      success(surface table address)
798  * @retval      NULL        error(surface id or client dose not exist)
799  */
800 /*--------------------------------------------------------------------------*/
801 WL_EXPORT   struct uifw_win_surface *
802 ico_window_mgr_get_usurf_client(const uint32_t surfaceid, struct wl_client *client)
803 {
804     struct uifw_win_surface *usurf;
805     struct uifw_client *uclient;
806
807     if (surfaceid == ICO_WINDOW_MGR_V_MAINSURFACE) {
808         uclient = ico_window_mgr_find_uclient(client);
809         if (uclient)    {
810             if (&uclient->surface_link != uclient->surface_link.next)   {
811                 usurf = container_of (uclient->surface_link.next,
812                                       struct uifw_win_surface, client_link);
813             }
814             else    {
815                 usurf = NULL;
816             }
817         }
818         else    {
819             usurf = NULL;
820         }
821     }
822     else    {
823         usurf = ico_window_mgr_get_usurf(surfaceid);
824     }
825     return usurf;
826 }
827
828 /*--------------------------------------------------------------------------*/
829 /**
830  * @brief   find_uifw_win_surface_by_ws: find UIFW surface by weston surface
831  *
832  * @param[in]   wsurf       Weston surface
833  * @return      UIFW surface table address
834  * @retval      !=NULL      success(surface table address)
835  * @retval      NULL        error(surface dose not exist)
836  */
837 /*--------------------------------------------------------------------------*/
838 static struct uifw_win_surface *
839 find_uifw_win_surface_by_ws(struct weston_surface *wsurf)
840 {
841     struct uifw_win_surface *usurf;
842
843     usurf = _ico_win_mgr->wshash[MAKE_WSHASH(wsurf)];
844
845     while (usurf)   {
846         if (usurf->surface == wsurf) {
847             return usurf;
848         }
849         usurf = usurf->next_wshash;
850     }
851     uifw_trace("find_uifw_win_surface_by_ws: NULL");
852     return NULL;
853 }
854
855 /*--------------------------------------------------------------------------*/
856 /**
857  * @brief   ico_window_mgr_find_uclient: find UIFW client by wayland client
858  *
859  * @param[in]   client      Wayland client
860  * @return      UIFW client table address
861  * @retval      !=NULL      success(client table address)
862  * @retval      NULL        error(client dose not exist)
863  */
864 /*--------------------------------------------------------------------------*/
865 WL_EXPORT   struct uifw_client*
866 ico_window_mgr_find_uclient(struct wl_client *client)
867 {
868     struct uifw_client  *uclient;
869
870     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
871         if (uclient->client == client)  {
872             return(uclient);
873         }
874     }
875     uifw_trace("ico_window_mgr_find_uclient: client.%08x is NULL", (int)client);
876     return NULL;
877 }
878
879 /*--------------------------------------------------------------------------*/
880 /**
881  * @brief   ico_window_mgr_get_appid: find application id by wayland client
882  *
883  * @param[in]   client      Wayland client
884  * @return      application id
885  * @retval      !=NULL      success(application id)
886  * @retval      NULL        error(client dose not exist)
887  */
888 /*--------------------------------------------------------------------------*/
889 WL_EXPORT   char *
890 ico_window_mgr_get_appid(struct wl_client* client)
891 {
892     struct uifw_client  *uclient;
893
894     uclient = ico_window_mgr_find_uclient(client);
895
896     if (! uclient)  {
897         return NULL;
898     }
899     return uclient->appid;
900 }
901
902 /*--------------------------------------------------------------------------*/
903 /**
904  * @brief   ico_window_mgr_get_display_coordinate: get display coordinate
905  *
906  * @param[in]   displayno   display number
907  * @param[out]  x           relative X coordinate
908  * @param[out]  y           relative Y coordinate
909  * @return      none
910  */
911 /*--------------------------------------------------------------------------*/
912 WL_EXPORT   void
913 ico_window_mgr_get_display_coordinate(int displayno, int *x, int *y)
914 {
915     if ((displayno <= _ico_num_nodes) || (displayno < 0))   {
916         displayno = 0;
917     }
918     *x = _ico_node_table[displayno].disp_x;
919     *y = _ico_node_table[displayno].disp_y;
920 }
921
922 /*--------------------------------------------------------------------------*/
923 /**
924  * @brief   generate_id: generate uniq id for UIFW surface id
925  *
926  * @param       none
927  * @return      uniq id for UIFW surface id
928  */
929 /*--------------------------------------------------------------------------*/
930 static uint32_t
931 generate_id(void)
932 {
933     int     rep;
934     int     i;
935     int     map;
936     uint16_t *new_map;
937     uint32_t surfaceId;
938
939     /* next assign id                           */
940     _ico_win_mgr->surfaceid_count ++;
941
942     /* serach free id from bitmap               */
943     for (rep = 0; rep < (int)(_ico_win_mgr->surfaceid_max/16); rep++)   {
944         if (_ico_win_mgr->surfaceid_count >= _ico_win_mgr->surfaceid_max)   {
945             _ico_win_mgr->surfaceid_count = 0;
946         }
947         if (_ico_win_mgr->surfaceid_map[_ico_win_mgr->surfaceid_count/16] != 0xffff)    {
948             /* find free id from bitmap         */
949             map = 1 << (_ico_win_mgr->surfaceid_count % 16);
950             for (i = (_ico_win_mgr->surfaceid_count % 16); i < 16; i++) {
951                 if ((_ico_win_mgr->surfaceid_map[_ico_win_mgr->surfaceid_count/16] & map)
952                         == 0) {
953                     _ico_win_mgr->surfaceid_map[_ico_win_mgr->surfaceid_count/16] |= map;
954                     _ico_win_mgr->surfaceid_count
955                         = (_ico_win_mgr->surfaceid_count/16)*16 + i;
956
957                     surfaceId = (_ico_win_mgr->surfaceid_count + 1)
958                                 | _ico_win_mgr->surface_head;
959                     uifw_trace("generate_id: SurfaceId=%08x", surfaceId);
960                     return(surfaceId);
961                 }
962                 map = map << 1;
963             }
964         }
965         _ico_win_mgr->surfaceid_count += 16;
966     }
967
968     /* no free id in bitmap, extend bitmap      */
969     if ((_ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS) > SURCAFE_ID_MASK)  {
970         /* too many surfaces, system error      */
971         uifw_trace("generate_id: SurffaceId Overflow(%d, Max=%d), Abort",
972                    _ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS, SURCAFE_ID_MASK);
973         fprintf(stderr, "generate_id: SurffaceId Overflow(%d, Max=%d), Abort\n",
974                 _ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS, SURCAFE_ID_MASK);
975         abort();
976     }
977
978     new_map = (uint16_t *) malloc((_ico_win_mgr->surfaceid_max + ADD_SURFACE_IDS) / 8);
979     memcpy(new_map, _ico_win_mgr->surfaceid_map, _ico_win_mgr->surfaceid_max/8);
980     memset(&new_map[_ico_win_mgr->surfaceid_max/16], 0, ADD_SURFACE_IDS/8);
981     _ico_win_mgr->surfaceid_count = _ico_win_mgr->surfaceid_max;
982     new_map[_ico_win_mgr->surfaceid_count/16] |= 1;
983     _ico_win_mgr->surfaceid_max += ADD_SURFACE_IDS;
984     free(_ico_win_mgr->surfaceid_map);
985     _ico_win_mgr->surfaceid_map = new_map;
986
987     uifw_trace("generate_id: Extent SurfaceId=%d(Max.%d)",
988                _ico_win_mgr->surfaceid_count+1, _ico_win_mgr->surfaceid_max);
989     surfaceId = (_ico_win_mgr->surfaceid_count + 1) | _ico_win_mgr->surface_head;
990
991     uifw_trace("generate_id: SurfaceId=%08x", surfaceId);
992     return(surfaceId);
993 }
994
995 /*--------------------------------------------------------------------------*/
996 /**
997  * @brief   win_mgr_bind_client: desktop_shell from client
998  *
999  * @param[in]   client          Wayland client
1000  * @param[in]   shell           shell(ico_ivi_shell) table address
1001  * @return      none
1002  */
1003 /*--------------------------------------------------------------------------*/
1004 static void
1005 win_mgr_bind_client(struct wl_client *client, void *shell)
1006 {
1007     struct uifw_client  *uclient;
1008     int     newclient;
1009     pid_t   pid;
1010     uid_t   uid;
1011     gid_t   gid;
1012     struct ico_api_permit   *accp;
1013     char    subject[ICO_UIFW_MAX_SUBJECT_NAME];
1014     int     fd;
1015     int     i;
1016
1017     uifw_trace("win_mgr_bind_client: Enter(client=%08x, shell=%08x)",
1018                (int)client, (int)shell);
1019
1020     /* save shell table address             */
1021     if (shell)  {
1022         _ico_win_mgr->shell = shell;
1023     }
1024
1025     /* set client                           */
1026     uclient = ico_window_mgr_find_uclient(client);
1027     if (! uclient)  {
1028         /* client not exist, create client management table             */
1029         uifw_trace("win_mgr_bind_client: Create Client");
1030         uclient = (struct uifw_client *)malloc(sizeof(struct uifw_client));
1031         if (!uclient)   {
1032             uifw_error("win_mgr_bind_client: Error, No Memory");
1033             return;
1034         }
1035         memset(uclient, 0, sizeof(struct uifw_client));
1036         uclient->client = client;
1037         uclient->api_access_control = _ico_win_mgr->default_access;
1038         wl_list_init(&uclient->surface_link);
1039         newclient = 1;
1040     }
1041     else    {
1042         newclient = 0;
1043     }
1044     wl_client_get_credentials(client, &pid, &uid, &gid);
1045     uifw_trace("win_mgr_bind_client: client=%08x pid=%d uid=%d gid=%d",
1046                (int)client, (int)pid, (int)uid, (int)gid);
1047     if (pid > 0)    {
1048         uclient->pid = (int)pid;
1049         /* get applicationId from AppCore(AUL)  */
1050         win_mgr_get_client_appid(uclient);
1051
1052         /* get SMACK subject label  */
1053         if (! _ico_win_mgr->access_config)  {
1054             /* no access control, permit all APIs   */
1055             uclient->api_access_control = 0xffffffff;
1056         }
1057         else    {
1058             sprintf(subject, "/proc/%d/attr/current", (int)pid);
1059             fd = open(subject, O_RDONLY);
1060             if (fd < 0) {
1061                 uifw_warn("win_mgr_bind_client: pid=%d(%s) not exist current",
1062                           pid, uclient->appid);
1063             }
1064             else    {
1065                 i = read(fd, subject, sizeof(subject)-1);
1066                 if (i > 0)  {
1067                     subject[i] = 0;
1068                     accp = _ico_win_mgr->access_config;
1069                     while (accp)    {
1070                         if (strcmp(subject, accp->permit.subject) == 0) {
1071                             uclient->api_access_control |= accp->permit.api_permit;
1072                             break;
1073                         }
1074                         accp = accp->next;
1075                     }
1076                 }
1077                 else    {
1078                     i = errno;
1079                     uifw_trace("win_mgr_bind_client: pid=%d(%s) current read error<%d>",
1080                                pid, uclient->appid, i);
1081                     if (i == EINVAL)    {
1082                         /* SMACK not active     */
1083                         uclient->api_access_control = 0xffffffff;
1084                     }
1085                 }
1086                 close(fd);
1087             }
1088         }
1089         uifw_trace("win_mgr_bind_client: pid=%d(%s) access_control=%08x",
1090                    pid, uclient->appid, uclient->api_access_control);
1091
1092         if (newclient > 0)  {
1093             wl_list_insert(&_ico_win_mgr->client_list, &uclient->link);
1094         }
1095     }
1096     else    {
1097         uifw_trace("win_mgr_bind_client: client=%08x pid dose not exist", (int)client);
1098     }
1099     uifw_trace("win_mgr_bind_client: Leave");
1100 }
1101
1102 /*--------------------------------------------------------------------------*/
1103 /**
1104  * @brief   win_mgr_unbind_client: unbind desktop_shell from client
1105  *
1106  * @param[in]   client          Wayland client
1107  * @return      none
1108  */
1109 /*--------------------------------------------------------------------------*/
1110 static void
1111 win_mgr_unbind_client(struct wl_client *client)
1112 {
1113     struct uifw_client  *uclient;
1114
1115     uifw_trace("win_mgr_unbind_client: Enter(client=%08x)", (int)client);
1116
1117     uclient = ico_window_mgr_find_uclient(client);
1118     if (uclient)    {
1119         /* Client exist, Destory client management table             */
1120         wl_list_remove(&uclient->link);
1121         free(uclient);
1122     }
1123     uifw_trace("win_mgr_unbind_client: Leave");
1124 }
1125
1126 #if 0       /* work around: Walk through child processes until app ID is found  */
1127 /*--------------------------------------------------------------------------*/
1128 /**
1129  * @brief   win_mgr_get_ppid: Get parent process ID.
1130  *
1131  * Similar to getppid(), except that this implementation accepts an
1132  * arbitrary process ID.
1133  *
1134  * @param[in]   pid     Process ID of child process
1135  * @return      parent process ID on success, -1 on failure
1136  */
1137 /*--------------------------------------------------------------------------*/
1138 static pid_t
1139 win_mgr_get_ppid(pid_t pid)
1140 {
1141     pid_t ppid = -1;
1142     char procpath[PATH_MAX] = { 0 };
1143
1144     snprintf(procpath, sizeof(procpath)-1, "/proc/%d/status", pid);
1145
1146     /* We better have read permissions! */
1147     int const fd = open(procpath, O_RDONLY);
1148
1149     if (fd < 0)
1150         return ppid;
1151
1152     char buffer[1024] = { 0 };
1153
1154     ssize_t const size = read(fd, buffer, sizeof(buffer));
1155     close(fd);
1156
1157     if (size <= 0)
1158         return ppid;
1159
1160     /* Find line containing the parent process ID. */
1161     char const * const ppid_line = strstr(buffer, "PPid");
1162
1163     if (ppid_line != NULL)
1164         sscanf(ppid_line, "PPid:    %d", &ppid);
1165
1166     return ppid;
1167 }
1168 #endif      /* work around: Walk through child processes until app ID is found  */
1169
1170 /*--------------------------------------------------------------------------*/
1171 /**
1172  * @brief   win_mgr_get_client_appid: get applicationId from pid
1173  *
1174  * @param[in]   uclient     UIFW client management table
1175  * @return      none
1176  */
1177 /*--------------------------------------------------------------------------*/
1178 static void
1179 win_mgr_get_client_appid(struct uifw_client *uclient)
1180 {
1181     int status = AUL_R_ERROR;
1182
1183     memset(uclient->appid, 0, ICO_IVI_APPID_LENGTH);
1184
1185 #if 0       /* work around: Walk through child processes until app ID is found  */
1186     /*
1187      * Walk the parent process chain until we find a parent process
1188      * with an app ID.
1189      */
1190     int pid;
1191
1192     for (pid = uclient->pid;
1193          pid > 1 && status != AUL_R_OK;
1194          pid = win_mgr_get_ppid(pid)) {
1195
1196         status = aul_app_get_appid_bypid(pid,
1197                                          uclient->appid,
1198                                          ICO_IVI_APPID_LENGTH);
1199
1200         uifw_trace("win_mgr_get_client_appid: aul_app_get_appid_bypid ret=%d "
1201                    "pid=%d appid=<%s>", status, pid, uclient->appid);
1202     }
1203     /*
1204      * Walk the child process chain as well since app ID was not yet found
1205      */
1206     if (status != AUL_R_OK) {
1207
1208         DIR *dr;
1209         struct dirent *de;
1210         struct stat ps;
1211         pid_t   tpid;
1212         uid_t   uid;
1213         gid_t   gid;
1214
1215         dr = opendir("/proc/");
1216
1217         /* get uid */
1218         wl_client_get_credentials(uclient->client, &tpid, &uid, &gid);
1219
1220         while(((de = readdir(dr)) != NULL) && (status != AUL_R_OK)) {
1221
1222             char fullpath[PATH_MAX] = { 0 };
1223             int is_child = 0;
1224             int tmppid;
1225
1226             snprintf(fullpath, sizeof(fullpath)-1, "/proc/%s", de->d_name);
1227
1228             if (stat(fullpath, &ps) == -1) {
1229                 continue;
1230             }
1231
1232             /* find pid dirs for this user (uid) only */
1233             if (ps.st_uid != uid)
1234                 continue;
1235
1236             pid = atoi(de->d_name);
1237
1238             /* check if it's a valid child */
1239             if (pid < uclient->pid)
1240                 continue;
1241
1242             /* scan up to pid to find if a chain exists */
1243             for (tmppid = pid; tmppid > uclient->pid;) {
1244                 tmppid = win_mgr_get_ppid(tmppid);
1245                 if (tmppid == uclient->pid)
1246                     is_child = 1;
1247             }
1248
1249             if (is_child) {
1250                 status = aul_app_get_appid_bypid(pid, uclient->appid,
1251                                                       ICO_IVI_APPID_LENGTH);
1252
1253                 uifw_debug("win_mgr_get_client_appid: aul_app_get_appid_bypid "
1254                            "ret=%d pid=%d appid=<%s>", status, pid,
1255                            uclient->appid);
1256             }
1257         }
1258     }
1259 #else       /* work around: Walk through child processes until app ID is found  */
1260     status = aul_app_get_appid_bypid(uclient->pid, uclient->appid, ICO_IVI_APPID_LENGTH);
1261     uifw_trace("win_mgr_get_client_appid: aul_app_get_appid_bypid ret=%d "
1262                "pid=%d appid=<%s>", status, uclient->pid, uclient->appid);
1263 #endif      /* work around: Walk through child processes until app ID is found  */
1264
1265     if (uclient->appid[0] != 0) {
1266         /* OK, end of get appid         */
1267         uclient->fixed_appid = ICO_WINDOW_MGR_APPID_FIXCOUNT;
1268     }
1269     else    {
1270         /* client does not exist in AppCore, search Linux process table */
1271
1272         int     fd;
1273         int     size;
1274         int     i;
1275         int     j;
1276         char    procpath[128];
1277
1278         uclient->fixed_appid ++;
1279         memset(uclient->appid, 0, ICO_IVI_APPID_LENGTH);
1280         snprintf(procpath, sizeof(procpath)-1, "/proc/%d/cmdline", uclient->pid);
1281         fd = open(procpath, O_RDONLY);
1282         if (fd >= 0)    {
1283             size = read(fd, procpath, sizeof(procpath));
1284             for (; size > 0; size--)    {
1285                 if (procpath[size-1])   break;
1286             }
1287             if (size > 0)   {
1288                 /* get program base name    */
1289                 i = 0;
1290                 for (j = 0; j < size; j++)  {
1291                     if (procpath[j] == 0)   break;
1292                     if (procpath[j] == '/') i = j + 1;
1293                 }
1294                 j = 0;
1295                 for (; i < size; i++)   {
1296                     uclient->appid[j] = procpath[i];
1297                     if ((uclient->appid[j] == 0) ||
1298                         (j >= (ICO_IVI_APPID_LENGTH-1)))    break;
1299                     j++;
1300                 }
1301                 /* search application number in apprication start option    */
1302                 if ((uclient->appid[j] == 0) && (j < (ICO_IVI_APPID_LENGTH-2))) {
1303                     for (; i < size; i++)   {
1304                         if ((procpath[i] == 0) &&
1305                             (procpath[i+1] == '@')) {
1306                             strncpy(&uclient->appid[j], &procpath[i+1],
1307                                     ICO_IVI_APPID_LENGTH - j - 2);
1308                         }
1309                     }
1310                 }
1311             }
1312             close(fd);
1313         }
1314         for (i = strlen(uclient->appid)-1; i >= 0; i--) {
1315             if (uclient->appid[i] != ' ')   break;
1316         }
1317         uclient->appid[i+1] = 0;
1318         if (uclient->appid[0])  {
1319             uifw_trace("win_mgr_get_client_appid: pid=%d appid=<%s> from "
1320                        "Process table(%d)",
1321                        uclient->pid, uclient->appid, uclient->fixed_appid );
1322         }
1323         else    {
1324             uifw_trace("win_mgr_get_client_appid: pid=%d dose not exist in Process table",
1325                        uclient->pid);
1326             sprintf(uclient->appid, "?%d?", uclient->pid);
1327         }
1328     }
1329 }
1330
1331 /*--------------------------------------------------------------------------*/
1332 /**
1333  * @brief   ico_get_animation_name: convert animation name to Id value
1334  *
1335  * @param[in]   animation       animation name
1336  * @return      animation Id value
1337  */
1338 /*--------------------------------------------------------------------------*/
1339 static int
1340 ico_get_animation_name(const char *animation)
1341 {
1342     int anima = ICO_WINDOW_MGR_ANIMATION_NONE;
1343
1344     if (strcasecmp(animation, "none") == 0) {
1345         return ICO_WINDOW_MGR_ANIMATION_NONE;
1346     }
1347
1348     if (win_mgr_hook_animation) {
1349         anima = (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_NAME, (void *)animation);
1350     }
1351     if (anima <= 0) {
1352         anima = ICO_WINDOW_MGR_ANIMATION_NONE;
1353     }
1354     return anima;
1355 }
1356
1357 /*--------------------------------------------------------------------------*/
1358 /**
1359  * @brief   ico_read_surface_pixels: read surface pixel image
1360  *                                   this function inport from weston 1.3.1(by Tizen)
1361  *
1362  * @param[in]   es      weston surface
1363  * @param[in]   format  pixel format
1364  * @param[out]  pixels  pixel read buffer
1365  * @param[in]   x       X coordinate
1366  * @param[in]   y       Y coordinate
1367  * @param[in]   width   width
1368  * @param[in]   height  height
1369  * @return      success(=0) or error(-1)
1370  */
1371 /*--------------------------------------------------------------------------*/
1372 static int
1373 ico_read_surface_pixels(struct weston_surface *es, pixman_format_code_t format,
1374                         void *pixels, int x, int y, int width, int height)
1375 {
1376     struct weston_buffer *buffer = es->buffer_ref.buffer;
1377     struct uifw_gl_surface_state *gs = es->renderer_state;
1378     GLenum gl_format;
1379     int size;
1380     struct wl_shm_buffer *shm_buffer = NULL;
1381     GLuint fbo;
1382
1383     switch (format) {
1384     case PIXMAN_a8r8g8b8:
1385         gl_format = GL_BGRA_EXT;
1386         break;
1387     case PIXMAN_a8b8g8r8:
1388         gl_format = GL_RGBA;
1389         break;
1390     default:
1391         return -1;
1392     }
1393
1394     if (buffer) {
1395         shm_buffer = wl_shm_buffer_get(buffer->resource);
1396     }
1397     if (shm_buffer) {
1398         size = buffer->width * 4 * buffer->height;
1399         memcpy(pixels, wl_shm_buffer_get_data(shm_buffer), size);
1400     } else if (gs != NULL)  {
1401         glGenFramebuffers(1, &fbo);
1402         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1403         glFramebufferTexture2D(GL_FRAMEBUFFER,
1404                                GL_COLOR_ATTACHMENT0,
1405                                GL_TEXTURE_2D,
1406                                gs->textures[0], 0);
1407
1408         glReadPixels(x, y, width, height,
1409                      gl_format, GL_UNSIGNED_BYTE, pixels);
1410
1411         glBindFramebuffer(GL_FRAMEBUFFER, 0);
1412         glDeleteFramebuffers(1, &fbo);
1413     }
1414     else    {
1415         return -1;
1416     }
1417     return 0;
1418 }
1419
1420 /*--------------------------------------------------------------------------*/
1421 /**
1422  * @brief   win_mgr_register_surface: create UIFW surface
1423  *
1424  * @param[in]   layertype       surface layer type
1425  * @param[in]   surface         Weston surface
1426  * @param[in]   client          Wayland client
1427  * @param[in]   shsurf          shell surface
1428  * @return      none
1429  */
1430 /*--------------------------------------------------------------------------*/
1431 static void
1432 win_mgr_register_surface(int layertype, struct weston_surface *surface,
1433                          struct wl_client *client, struct shell_surface *shsurf)
1434 {
1435     struct uifw_win_surface *usurf;
1436     struct uifw_win_surface *phash;
1437     struct uifw_win_surface *bhash;
1438     int         layer;
1439     uint32_t    hash;
1440
1441     uifw_trace("win_mgr_register_surface: Enter(surf=%08x,client=%08x,type=%x,shsurf=%08x)",
1442                (int)surface, (int)client, layertype, (int)shsurf);
1443
1444     /* check new surface                    */
1445     if (find_uifw_win_surface_by_ws(surface))   {
1446         /* surface exist, NOP               */
1447         uifw_trace("win_mgr_register_surface: Leave(Already Exist)");
1448         return;
1449     }
1450
1451     /* set default color and shader */
1452     weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
1453
1454     /* create UIFW surface management table */
1455     usurf = malloc(sizeof(struct uifw_win_surface));
1456     if (! usurf)    {
1457         uifw_error("win_mgr_register_surface: No Memory");
1458         return;
1459     }
1460
1461     memset(usurf, 0, sizeof(struct uifw_win_surface));
1462
1463     usurf->surfaceid = generate_id();
1464     usurf->surface = surface;
1465     usurf->shsurf = shsurf;
1466     usurf->layertype = layertype;
1467     usurf->node_tbl = &_ico_node_table[0];  /* set default node table (display no=0)    */
1468     wl_list_init(&usurf->ivi_layer);
1469     wl_list_init(&usurf->client_link);
1470     wl_list_init(&usurf->animation.animation.link);
1471     wl_list_init(&usurf->surf_map);
1472     wl_list_init(&usurf->input_region);
1473     usurf->animation.hide_anima = ico_get_animation_name(ico_ivi_default_animation_name());
1474     usurf->animation.hide_time = ico_ivi_default_animation_time();
1475     usurf->animation.show_anima = usurf->animation.hide_anima;
1476     usurf->animation.show_time = usurf->animation.hide_time;
1477     usurf->animation.move_anima = usurf->animation.hide_anima;
1478     usurf->animation.move_time = usurf->animation.hide_time;
1479     usurf->animation.resize_anima = usurf->animation.hide_anima;
1480     usurf->animation.resize_time = usurf->animation.hide_time;
1481     if (layertype == LAYER_TYPE_INPUTPANEL) {
1482         usurf->attributes = ICO_WINDOW_MGR_ATTR_FIXED_ASPECT;
1483         usurf->animation.hide_anima = ico_get_animation_name(_ico_ivi_inputpanel_animation);
1484         usurf->animation.hide_time = _ico_ivi_inputpanel_anima_time;
1485         usurf->animation.show_anima = usurf->animation.hide_anima;
1486         usurf->animation.show_time = usurf->animation.hide_time;
1487     }
1488     if ((layertype != LAYER_TYPE_INPUTPANEL) &&
1489         ((_ico_win_mgr->num_manager <= 0) ||
1490          (ico_ivi_optionflag() & ICO_IVI_OPTION_SHOW_SURFACE))) {
1491         uifw_trace("win_mgr_register_surface: No Manager, Force visible");
1492         usurf->visible = 1;
1493     }
1494     else    {
1495         uifw_trace("win_mgr_register_surface: Manager exist, Not visible");
1496         usurf->visible = 0;
1497     }
1498
1499     /* set client                           */
1500     usurf->uclient = ico_window_mgr_find_uclient(client);
1501     if (! usurf->uclient)  {
1502         /* client not exist, create client management table */
1503         uifw_trace("win_mgr_register_surface: Create Client");
1504         win_mgr_bind_client(client, NULL);
1505         usurf->uclient = ico_window_mgr_find_uclient(client);
1506         if (! usurf->uclient)  {
1507             uifw_error("win_mgr_register_surface: No Memory");
1508             return;
1509         }
1510     }
1511     wl_list_insert(usurf->uclient->surface_link.prev, &usurf->client_link);
1512
1513     /* make surface id hash table       */
1514     hash = MAKE_IDHASH(usurf->surfaceid);
1515     phash = _ico_win_mgr->idhash[hash];
1516     bhash = NULL;
1517     while (phash)   {
1518         bhash = phash;
1519         phash = phash->next_idhash;
1520     }
1521     if (bhash)  {
1522         bhash->next_idhash = usurf;
1523     }
1524     else    {
1525         _ico_win_mgr->idhash[hash] = usurf;
1526     }
1527
1528     /* make weston surface hash table   */
1529     hash = MAKE_WSHASH(usurf->surface);
1530     phash = _ico_win_mgr->wshash[hash];
1531     bhash = NULL;
1532     while (phash)   {
1533         bhash = phash;
1534         phash = phash->next_wshash;
1535     }
1536     if (bhash)  {
1537         bhash->next_wshash = usurf;
1538     }
1539     else    {
1540         _ico_win_mgr->wshash[hash] = usurf;
1541     }
1542
1543     /* set default layer id             */
1544     switch (layertype)  {
1545     case LAYER_TYPE_BACKGROUND:
1546         layer = _ico_ivi_background_layer;
1547         break;
1548     case LAYER_TYPE_TOUCH:
1549         layer = _ico_ivi_touch_layer;
1550         break;
1551     case LAYER_TYPE_CURSOR:
1552         layer = _ico_ivi_cursor_layer;
1553         break;
1554     default:
1555         if ((_ico_win_mgr->num_manager > 0) ||
1556             (layertype == LAYER_TYPE_INPUTPANEL))   {
1557             layer = _ico_ivi_default_layer;
1558         }
1559         else    {
1560             layer = _ico_ivi_startup_layer;
1561         }
1562         break;
1563     }
1564     win_mgr_set_layer(usurf, layer);
1565
1566     uifw_trace("win_mgr_register_surface: Leave(surfaceId=%08x)", usurf->surfaceid);
1567 }
1568
1569 /*--------------------------------------------------------------------------*/
1570 /**
1571  * @brief   win_mgr_surface_map: map surface to display
1572  *
1573  * @param[in]   surface         Weston surface
1574  * @param[in]   width           surface width
1575  * @param[in]   height          surface height
1576  * @param[in]   sx              X coordinate on screen
1577  * @param[in]   sy              Y coordinate on screen
1578  * @return      none
1579  */
1580 /*--------------------------------------------------------------------------*/
1581 static void
1582 win_mgr_surface_map(struct weston_surface *surface, int32_t *width, int32_t *height,
1583                     int32_t *sx, int32_t *sy)
1584 {
1585     struct uifw_win_surface *usurf;
1586
1587     uifw_trace("win_mgr_surface_map: Enter(%08x, x/y=%d/%d w/h=%d/%d)",
1588                (int)surface, *sx, *sy, *width, *height);
1589
1590     usurf = find_uifw_win_surface_by_ws(surface);
1591
1592     if ((usurf != NULL) && (usurf->mapped == 0))    {
1593         uifw_trace("win_mgr_surface_map: surf=%08x w/h=%d/%d vis=%d",
1594                    usurf->surfaceid, usurf->width, usurf->height, usurf->visible);
1595         if ((usurf->width > 0) && (usurf->height > 0)) {
1596             uifw_trace("win_mgr_surface_map: HomeScreen registed, PositionSize"
1597                        "(surf=%08x x/y=%d/%d w/h=%d/%d vis=%d",
1598                        usurf->surfaceid, usurf->x, usurf->y, usurf->width, usurf->height,
1599                        usurf->visible);
1600             *width = usurf->width;
1601             *height = usurf->height;
1602             win_mgr_surface_configure(usurf, usurf->node_tbl->disp_x + usurf->x,
1603                                       usurf->node_tbl->disp_y + usurf->y,
1604                                       usurf->width, usurf->height);
1605         }
1606         else    {
1607             uifw_trace("win_mgr_surface_map: HomeScreen not regist Surface, "
1608                        "Change PositionSize(surf=%08x x/y=%d/%d w/h=%d/%d)",
1609                        usurf->surfaceid, *sx, *sy, *width, *height);
1610             usurf->width = *width;
1611             usurf->height = *height;
1612             usurf->x = *sx;
1613             usurf->y = *sy;
1614             if (usurf->x < 0)   usurf->x = 0;
1615             if (usurf->y < 0)   usurf->y = 0;
1616             if (usurf->layertype == LAYER_TYPE_INPUTPANEL)  {
1617                 /* set position */
1618                 usurf->node_tbl = &_ico_node_table[_ico_ivi_inputpanel_display];
1619
1620                 usurf->width = (float)usurf->surface->width
1621                                * (float)_ico_ivi_inputdeco_mag / 100.0f;
1622                 usurf->height = (float)usurf->surface->height
1623                                 * (float)_ico_ivi_inputdeco_mag / 100.0f;
1624
1625                 if ((usurf->width > (usurf->node_tbl->disp_width - 16)) ||
1626                     (usurf->height > (usurf->node_tbl->disp_height - 16)))  {
1627                     usurf->x = (usurf->node_tbl->disp_width
1628                                - usurf->surface->width) / 2;
1629                     usurf->y = usurf->node_tbl->disp_height
1630                                - usurf->surface->height - 16
1631                                - _ico_ivi_inputdeco_diff;
1632                     if (usurf->x < 0)   usurf->x = 0;
1633                     if (usurf->y < 0)   usurf->y = 0;
1634                 }
1635                 else    {
1636                     win_mgr_set_scale(usurf);
1637
1638                     usurf->x = (usurf->node_tbl->disp_width
1639                                 - usurf->width) / 2;
1640                     usurf->y = usurf->node_tbl->disp_height
1641                                - usurf->height - 16 - _ico_ivi_inputdeco_diff;
1642                     if (usurf->x < 0)   usurf->x = 0;
1643                     if (usurf->y < 0)   usurf->y = 0;
1644                 }
1645                 uifw_trace("win_mgr_surface_map: set position %08x %d.%d/%d",
1646                            usurf->surfaceid, usurf->node_tbl->node, usurf->x, usurf->y);
1647             }
1648             if (((ico_ivi_optionflag() & ICO_IVI_OPTION_SHOW_SURFACE) == 0) &&
1649                 (_ico_win_mgr->num_manager > 0))    {
1650                 /* HomeScreen exist, coodinate set by HomeScreen                */
1651                 if (usurf->visible) {
1652                     win_mgr_surface_configure(usurf, usurf->node_tbl->disp_x + usurf->x,
1653                                               usurf->node_tbl->disp_y + usurf->y,
1654                                               usurf->width, usurf->height);
1655                 }
1656                 else    {
1657                     win_mgr_surface_configure(usurf, ICO_IVI_MAX_COORDINATE+1,
1658                                               ICO_IVI_MAX_COORDINATE+1,
1659                                               usurf->width, usurf->height);
1660                 }
1661                 uifw_trace("win_mgr_surface_map: Change size/position x/y=%d/%d w/h=%d/%d",
1662                            usurf->x, usurf->y, surface->width, surface->height);
1663             }
1664             else if (usurf->layertype != LAYER_TYPE_INPUTPANEL) {
1665                 uifw_trace("win_mgr_surface_map: No HomeScreen, chaneg to Visible");
1666                 ico_window_mgr_set_visible(usurf, 1);
1667             }
1668             else    {
1669                 if (usurf->visible) {
1670                     win_mgr_surface_configure(usurf, usurf->node_tbl->disp_x + usurf->x,
1671                                               usurf->node_tbl->disp_y + usurf->y,
1672                                               usurf->width, usurf->height);
1673                 }
1674                 else    {
1675                     win_mgr_surface_configure(usurf, ICO_IVI_MAX_COORDINATE+1,
1676                                               ICO_IVI_MAX_COORDINATE+1,
1677                                               usurf->width, usurf->height);
1678                 }
1679             }
1680         }
1681         usurf->mapped = 1;
1682         if (usurf->visible) {
1683             ico_window_mgr_restack_layer(NULL);
1684         }
1685         uifw_trace("win_mgr_surface_map: Leave");
1686     }
1687     else    {
1688         uifw_trace("win_mgr_surface_map: Leave(No UIFW Surface or mapped)");
1689     }
1690 }
1691
1692 /*--------------------------------------------------------------------------*/
1693 /**
1694  * @brief   ico_window_mgr_restack_layer: restack surface list
1695  *
1696  * @param[in]   usurf           UIFW surface (if NULL, no surface)
1697  * @return      none
1698  */
1699 /*--------------------------------------------------------------------------*/
1700 WL_EXPORT   void
1701 ico_window_mgr_restack_layer(struct uifw_win_surface *usurf)
1702 {
1703     struct uifw_win_surface  *eu;
1704     struct uifw_win_layer *el;
1705     int32_t buf_width, buf_height;
1706     float   new_x, new_y;
1707     struct weston_layer *wlayer;
1708     struct weston_view  *view, *viewtmp;
1709     int     num_visible = 0;
1710     int     layertype;
1711
1712     /* make compositor surface list     */
1713     wlayer = ico_ivi_shell_weston_layer();
1714
1715     uifw_trace("ico_window_mgr_restack_layer: Enter(surf=%08x) layer=%08x",
1716                (int)usurf, (int)wlayer);
1717
1718     /* remove all surfaces in panel_layer   */
1719     wl_list_for_each_safe (view, viewtmp, &wlayer->view_list, layer_link)   {
1720         wl_list_remove(&view->layer_link);
1721         wl_list_init(&view->layer_link);
1722     }
1723     wl_list_init(&wlayer->view_list);
1724
1725     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link)  {
1726         wl_list_for_each (eu, &el->surface_list, ivi_layer) {
1727             if (eu->surface == NULL)    continue;
1728
1729             /* target only panel or unknown layer   */
1730             layertype = ico_ivi_shell_layertype(eu->surface);
1731             if ((layertype != LAYER_TYPE_PANEL) && (layertype != LAYER_TYPE_INPUTPANEL) &&
1732                 (layertype != LAYER_TYPE_FULLSCREEN) && (layertype != LAYER_TYPE_CURSOR) &&
1733                 (layertype != LAYER_TYPE_UNKNOWN))  {
1734                 continue;
1735             }
1736
1737             if (eu->mapped != 0)    {
1738                 if ((el->visible == FALSE) || (eu->visible == FALSE))   {
1739                     new_x = (float)(ICO_IVI_MAX_COORDINATE+1);
1740                     new_y = (float)(ICO_IVI_MAX_COORDINATE+1);
1741                 }
1742                 else if (eu->surface->buffer_ref.buffer)    {
1743                     ico_ivi_surface_buffer_size(eu->surface, &buf_width, &buf_height);
1744                     if ((eu->width > buf_width) && (eu->scalex <= 1.0f))    {
1745                         new_x = (float)(eu->x +
1746                                 (eu->width - eu->surface->width)/2);
1747                     }
1748                     else    {
1749                         new_x = (float)eu->x;
1750                     }
1751                     if ((eu->height > buf_height) && (eu->scaley <= 1.0f))  {
1752                         new_y = (float) (eu->y +
1753                                 (eu->height - eu->surface->height)/2);
1754                     }
1755                     else    {
1756                         new_y = (float)eu->y;
1757                     }
1758                     new_x += eu->node_tbl->disp_x + eu->xadd;
1759                     new_y += eu->node_tbl->disp_y + eu->yadd;
1760                     num_visible ++;
1761                 }
1762                 else    {
1763                     new_x = (float)(eu->x + eu->node_tbl->disp_x + eu->xadd);
1764                     new_y = (float)(eu->y + eu->node_tbl->disp_y + eu->yadd);
1765                 }
1766                 wl_list_for_each (view, &eu->surface->views, surface_link)  {
1767                     wl_list_remove(&view->layer_link);
1768                     wl_list_init(&view->layer_link);
1769                     wl_list_insert(wlayer->view_list.prev, &view->layer_link);
1770                     if ((eu->restrain_configure == 0) &&
1771                         ((new_x != view->geometry.x) ||
1772                          (new_y != view->geometry.y)))   {
1773                         weston_view_damage_below(view);
1774                         weston_view_set_position(view, (float)new_x, (float)new_y);
1775                         weston_surface_damage(eu->surface);
1776                     }
1777                     uifw_debug("ico_window_mgr_restack_layer:%3d(%d).%08x(%08x:%d) "
1778                                "x/y=%d/%d w/h=%d/%d[%x]",
1779                                el->layer, el->visible, eu->surfaceid, (int)eu->surface,
1780                                eu->visible, (int)view->geometry.x,
1781                                (int)view->geometry.y, eu->surface->width,
1782                                eu->surface->height, eu->layertype);
1783                 }
1784             }
1785         }
1786     }
1787
1788     /* damage(redraw) target surfacem if target exist   */
1789     if (usurf) {
1790         weston_surface_damage(usurf->surface);
1791     }
1792
1793     /* composit and draw screen(plane)  */
1794     weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
1795
1796     if ((_ico_win_mgr->shell_init == 0) && (num_visible > 0) &&
1797         (_ico_win_mgr->shell != NULL) && (_ico_win_mgr->num_manager > 0))   {
1798         /* start shell fade         */
1799         _ico_win_mgr->shell_init = 1;
1800         ico_ivi_shell_startup(_ico_win_mgr->shell);
1801     }
1802     uifw_trace("ico_window_mgr_restack_layer: Leave");
1803 }
1804
1805 /*--------------------------------------------------------------------------*/
1806 /**
1807  * @brief   ico_window_mgr_touch_layer: touch panel layer control
1808  *
1809  * @param[in]   omit        omit touch layer flag (TRUE=omit/FALSE=not omit)
1810  * @return      none
1811  */
1812 /*--------------------------------------------------------------------------*/
1813 WL_EXPORT   void
1814 ico_window_mgr_touch_layer(int omit)
1815 {
1816     struct uifw_win_surface *eu;
1817     struct weston_view      *ev;
1818
1819     /* check current touch layer mode   */
1820     if ((_ico_win_mgr->touch_layer == NULL) ||
1821         ((omit != FALSE) && (_ico_win_mgr->touch_layer->visible == FALSE))) {
1822         uifw_trace("ico_window_mgr_touch_layer: touch layer not exist or hide");
1823         return;
1824     }
1825
1826     wl_list_for_each (eu, &_ico_win_mgr->touch_layer->surface_list, ivi_layer) {
1827         if ((eu->surface == NULL) || (eu->mapped == 0)) continue;
1828         ev = ico_ivi_get_primary_view(eu);
1829         if (omit != FALSE)  {
1830             eu->animation.pos_x = (int)ev->geometry.x;
1831             eu->animation.pos_y = (int)ev->geometry.y;
1832             ev->geometry.x = (float)(ICO_IVI_MAX_COORDINATE+1);
1833             ev->geometry.y = (float)(ICO_IVI_MAX_COORDINATE+1);
1834         }
1835         else    {
1836             ev->geometry.x = (float)eu->animation.pos_x;
1837             ev->geometry.y = (float)eu->animation.pos_y;
1838         }
1839     }
1840 }
1841
1842 /*--------------------------------------------------------------------------*/
1843 /**
1844  * @brief   win_mgr_create_layer: create new layer
1845  *
1846  * @param[in]   usurf       UIFW surface, (if need)
1847  * @param[in]   layer       layer id
1848  * @param[in]   layertype   layer type if need
1849  * @return      new layer
1850  * @retval      != NULL     success(layer management table)
1851  * @retval      == NULL     error(No Memory)
1852  */
1853 /*--------------------------------------------------------------------------*/
1854 static struct uifw_win_layer *
1855 win_mgr_create_layer(struct uifw_win_surface *usurf, const uint32_t layer,
1856                      const int layertype)
1857 {
1858     struct uifw_win_layer *el;
1859     struct uifw_win_layer *new_el;
1860
1861     new_el = malloc(sizeof(struct uifw_win_layer));
1862     if (! new_el)   {
1863         uifw_trace("win_mgr_create_layer: Leave(No Memory)");
1864         return NULL;
1865     }
1866
1867     memset(new_el, 0, sizeof(struct uifw_win_layer));
1868     new_el->layer = layer;
1869     if ((int)layer == _ico_ivi_background_layer )   {
1870         new_el->layertype = LAYER_TYPE_BACKGROUND;
1871     }
1872     else if ((int)layer == _ico_ivi_touch_layer )   {
1873         new_el->layertype = LAYER_TYPE_TOUCH;
1874         _ico_win_mgr->touch_layer = new_el;
1875     }
1876     else if ((int)layer == _ico_ivi_cursor_layer )  {
1877         new_el->layertype = LAYER_TYPE_CURSOR;
1878     }
1879     else    {
1880         new_el->layertype = LAYER_TYPE_PANEL;
1881     }
1882     new_el->visible = TRUE;
1883     wl_list_init(&new_el->surface_list);
1884     wl_list_init(&new_el->link);
1885
1886     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
1887         if (layer >= el->layer) break;
1888     }
1889     if (&el->link == &_ico_win_mgr->ivi_layer_list)    {
1890         wl_list_insert(_ico_win_mgr->ivi_layer_list.prev, &new_el->link);
1891     }
1892     else    {
1893         wl_list_insert(el->link.prev, &new_el->link);
1894     }
1895
1896     if (usurf)  {
1897         wl_list_remove(&usurf->ivi_layer);
1898         wl_list_insert(&new_el->surface_list, &usurf->ivi_layer);
1899         usurf->win_layer = new_el;
1900         if (new_el->layertype == LAYER_TYPE_CURSOR) {
1901             usurf->layertype = LAYER_TYPE_CURSOR;
1902         }
1903     }
1904     return new_el;
1905 }
1906
1907 /*--------------------------------------------------------------------------*/
1908 /**
1909  * @brief   win_mgr_set_layer: set(or change) surface layer
1910  *
1911  * @param[in]   usurf       UIFW surface
1912  * @param[in]   layer       layer id
1913  * @return      none
1914  */
1915 /*--------------------------------------------------------------------------*/
1916 static void
1917 win_mgr_set_layer(struct uifw_win_surface *usurf, const uint32_t layer)
1918 {
1919     struct uifw_win_layer *el;
1920     struct uifw_win_layer *new_el;
1921     uint32_t    oldlayer;
1922
1923     uifw_trace("win_mgr_set_layer: Enter(%08x,%08x,%x)",
1924                usurf->surfaceid, (int)usurf->surface, layer);
1925
1926     /* check if same layer                      */
1927     if (usurf->win_layer != NULL)   {
1928         oldlayer = usurf->win_layer->layer ;
1929         if (oldlayer == layer)  {
1930             uifw_trace("win_mgr_set_layer: Leave(Same Layer)");
1931             return;
1932         }
1933     }
1934     else    {
1935         oldlayer = 0;
1936     }
1937
1938     /* search existing layer                    */
1939     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
1940         if (el->layer == layer) break;
1941     }
1942
1943     if (&el->link == &_ico_win_mgr->ivi_layer_list)    {
1944         /* layer not exist, create new layer    */
1945         uifw_trace("win_mgr_set_layer: New Layer %d(%d)", layer, usurf->layertype);
1946         new_el = win_mgr_create_layer(usurf, layer, usurf->layertype);
1947         if (! new_el)   {
1948             uifw_trace("win_mgr_set_layer: Leave(No Memory)");
1949             return;
1950         }
1951     }
1952     else    {
1953         uifw_trace("win_mgr_set_layer: Add surface to Layer %d", layer);
1954         usurf->win_layer = el;
1955         win_mgr_set_raise(usurf, 3);
1956     }
1957
1958     /* set input region                     */
1959     if (layer == (uint32_t)_ico_ivi_cursor_layer)   {
1960         uifw_trace("win_mgr_set_layer: %08x cursor Change to cursor layer (%d,%d)-(%d,%d)",
1961                    usurf->surfaceid,
1962                    usurf->surface->input.extents.x1, usurf->surface->input.extents.y1,
1963                    usurf->surface->input.extents.x2, usurf->surface->input.extents.y2);
1964         pixman_region32_fini(&usurf->surface->pending.input);
1965         pixman_region32_init_rect(&usurf->surface->pending.input,
1966                                   ICO_IVI_MAX_COORDINATE + 1, ICO_IVI_MAX_COORDINATE + 1,
1967                                   1, 1);
1968     }
1969     else if (oldlayer == (uint32_t)_ico_ivi_cursor_layer)   {
1970         uifw_trace("win_mgr_set_layer: %08x cursor Change to normal layer (%d,%d)-(%d,%d)",
1971                    usurf->surfaceid,
1972                    usurf->surface->input.extents.x1, usurf->surface->input.extents.y1,
1973                    usurf->surface->input.extents.x2, usurf->surface->input.extents.y2);
1974         pixman_region32_fini(&usurf->surface->pending.input);
1975         pixman_region32_init_rect(&usurf->surface->pending.input,
1976                                   INT32_MIN, INT32_MIN, UINT32_MAX, UINT32_MAX);
1977     }
1978
1979     /* rebild compositor surface list       */
1980     if (usurf->visible) {
1981         ico_window_mgr_restack_layer(usurf);
1982     }
1983     uifw_trace("win_mgr_set_layer: Leave");
1984 }
1985
1986 /*--------------------------------------------------------------------------*/
1987 /**
1988  * @brief   win_mgr_set_active: set(or change) active surface
1989  *
1990  * @param[in]   usurf       UIFW surface (if NULL, change to inactive)
1991  * @param[in]   target      target device
1992  * @return      none
1993  */
1994 /*--------------------------------------------------------------------------*/
1995 static void
1996 win_mgr_set_active(struct uifw_win_surface *usurf, const int target)
1997 {
1998     struct weston_seat *seat;
1999     struct weston_surface *surface;
2000     int     object = target;
2001 #if 0               /* pointer grab can not release */
2002     int     savetp, i;
2003 #endif              /* pointer grab can not release */
2004
2005     uifw_trace("win_mgr_set_active: Enter(%08x,%x)", usurf ? usurf->surfaceid : 0, target);
2006
2007     if ((usurf) && (usurf->shsurf) && (usurf->surface)) {
2008         surface = usurf->surface;
2009         if ((object & (ICO_WINDOW_MGR_ACTIVE_POINTER|ICO_WINDOW_MGR_ACTIVE_KEYBOARD)) == 0) {
2010             surface = NULL;
2011             if (_ico_win_mgr->active_pointer_usurf == usurf) {
2012                 object |= ICO_WINDOW_MGR_ACTIVE_POINTER;
2013             }
2014             if (_ico_win_mgr->active_keyboard_usurf == usurf)    {
2015                 object |= ICO_WINDOW_MGR_ACTIVE_KEYBOARD;
2016             }
2017         }
2018         else    {
2019             if (object & ICO_WINDOW_MGR_ACTIVE_POINTER) {
2020                 _ico_win_mgr->active_pointer_usurf = usurf;
2021             }
2022             if (object & ICO_WINDOW_MGR_ACTIVE_KEYBOARD)    {
2023                 _ico_win_mgr->active_keyboard_usurf = usurf;
2024             }
2025         }
2026     }
2027     else    {
2028         surface = NULL;
2029         if (object == 0)    {
2030             object = ICO_WINDOW_MGR_ACTIVE_POINTER | ICO_WINDOW_MGR_ACTIVE_KEYBOARD;
2031         }
2032         if (object & ICO_WINDOW_MGR_ACTIVE_POINTER) {
2033             _ico_win_mgr->active_pointer_usurf = NULL;
2034         }
2035         if (object & ICO_WINDOW_MGR_ACTIVE_KEYBOARD)    {
2036             _ico_win_mgr->active_keyboard_usurf = NULL;
2037         }
2038     }
2039
2040     wl_list_for_each (seat, &_ico_win_mgr->compositor->seat_list, link) {
2041 #if 0               /* pointer grab can not release */
2042         if (object & ICO_WINDOW_MGR_ACTIVE_POINTER) {
2043             if (surface)    {
2044                 if ((seat->pointer != NULL) && (seat->pointer->focus != surface))   {
2045                     uifw_trace("win_mgr_set_active: pointer reset focus(%08x)",
2046                                (int)seat->pointer->focus);
2047                     if (seat->pointer->button_count > 0)    {
2048                         /* emulate button release   */
2049                         notify_button(seat, weston_compositor_get_time(),
2050                                       seat->pointer->grab_button,
2051                                       WL_POINTER_BUTTON_STATE_RELEASED);
2052                         /* count up button, because real mouse botan release    */
2053                         seat->pointer->button_count ++;
2054                     }
2055                     weston_pointer_set_focus(seat->pointer, NULL,
2056                                              wl_fixed_from_int(0), wl_fixed_from_int(0));
2057                 }
2058                 else    {
2059                     uifw_trace("win_mgr_set_active: pointer nochange surface(%08x)",
2060                                (int)surface);
2061                 }
2062                 if ((seat->touch != NULL) && (seat->touch->focus != surface))   {
2063                     uifw_trace("win_mgr_set_active: touch reset surface(%08x)",
2064                                (int)seat->touch->focus);
2065                     if (seat->num_tp > 10)  {
2066                         seat->num_tp = 0;       /* safty gard   */
2067                     }
2068                     else if (seat->num_tp > 0)   {
2069                         /* emulate touch up         */
2070                         savetp = seat->num_tp;
2071                         for (i = 0; i < savetp; i++)    {
2072                             notify_touch(seat, weston_compositor_get_time(), i+1,
2073                                          seat->touch->grab_x, seat->touch->grab_y,
2074                                          WL_TOUCH_UP);
2075                         }
2076                         /* touch count up, becase real touch release    */
2077                         seat->num_tp = savetp;
2078                     }
2079                     weston_touch_set_focus(seat, NULL);
2080                 }
2081                 else    {
2082                     uifw_trace("win_mgr_set_active: touch nochange surface(%08x)",
2083                                (int)surface);
2084                 }
2085             }
2086             else    {
2087                 uifw_trace("win_mgr_set_active: pointer reset surface(%08x)",
2088                            (int)seat->pointer->focus);
2089                 if ((seat->pointer != NULL) && (seat->pointer->focus != NULL))  {
2090                     if (seat->pointer->button_count > 0)    {
2091                         /* emulate button release   */
2092                         notify_button(seat, weston_compositor_get_time(),
2093                                       seat->pointer->grab_button,
2094                                       WL_POINTER_BUTTON_STATE_RELEASED);
2095                         seat->pointer->button_count ++;
2096                     }
2097                     weston_pointer_set_focus(seat->pointer, NULL,
2098                                              wl_fixed_from_int(0), wl_fixed_from_int(0));
2099                 }
2100                 if ((seat->touch != NULL) && (seat->touch->focus != NULL))  {
2101                     if (seat->num_tp > 10)  {
2102                         seat->num_tp = 0;       /* safty gard   */
2103                     }
2104                     else if (seat->num_tp > 0)   {
2105                         /* emulate touch up         */
2106                         savetp = seat->num_tp;
2107                         for (i = 0; i < savetp; i++)    {
2108                             notify_touch(seat, weston_compositor_get_time(), i+1,
2109                                          seat->touch->grab_x, seat->touch->grab_y,
2110                                          WL_TOUCH_UP);
2111                         }
2112                         /* touch count up, becase real touch release    */
2113                         seat->num_tp = savetp;
2114                     }
2115                     weston_touch_set_focus(seat, NULL);
2116                 }
2117             }
2118         }
2119 #endif              /* pointer grab can not release */
2120         if ((object & ICO_WINDOW_MGR_ACTIVE_KEYBOARD) && (seat->keyboard))  {
2121             if (surface)    {
2122 #if 0               /* pointer grab can not release */
2123                 if (seat->keyboard->focus != surface)    {
2124 #endif              /* pointer grab can not release */
2125                     weston_keyboard_set_focus(seat->keyboard, surface);
2126                     uifw_trace("win_mgr_set_active: keyboard change surface(%08x=>%08x)",
2127                                (int)seat->keyboard->focus, (int)surface);
2128 #if 0               /* pointer grab can not release */
2129                 }
2130                 else    {
2131                     uifw_trace("win_mgr_set_active: keyboard nochange surface(%08x)",
2132                                (int)surface);
2133                 }
2134 #endif              /* pointer grab can not release */
2135             }
2136             else    {
2137                 uifw_trace("win_mgr_set_active: keyboard reset surface(%08x)",
2138                            (int)seat->keyboard);
2139                 weston_keyboard_set_focus(seat->keyboard, NULL);
2140             }
2141         }
2142     }
2143     uifw_trace("win_mgr_set_active: Leave");
2144 }
2145
2146 /*--------------------------------------------------------------------------*/
2147 /**
2148  * @brief   ico_window_mgr_ismykeyboard: check active keyboard
2149  *
2150  * @param[in]   usurf       UIFW surface
2151  * @return      check result
2152  * @retval      =1          usurf is active keyboard surface
2153  * @retval      =0          usurf is not active
2154  */
2155 /*--------------------------------------------------------------------------*/
2156 WL_EXPORT   int
2157 ico_window_mgr_ismykeyboard(struct uifw_win_surface *usurf)
2158 {
2159     return (_ico_win_mgr->active_keyboard_usurf == usurf) ? 1 : 0;
2160 }
2161
2162 /*--------------------------------------------------------------------------*/
2163 /**
2164  * @brief   uifw_declare_manager: declare manager(ex.SystemController) client
2165  *
2166  * @param[in]   client      Weyland client
2167  * @param[in]   resource    resource of request
2168  * @param[in]   manager     manager(1=manager, 0=not manager)
2169  * @return      none
2170  */
2171 /*--------------------------------------------------------------------------*/
2172 static void
2173 uifw_declare_manager(struct wl_client *client, struct wl_resource *resource, int manager)
2174 {
2175     struct uifw_manager* mgr;
2176     struct uifw_win_surface *usurf;
2177     struct uifw_client *uclient;
2178     struct uifw_win_layer *el;
2179
2180     uifw_trace("uifw_declare_manager: Enter client=%08x manager=%d",
2181                (int)client, manager);
2182
2183     /* check for access control         */
2184     uclient = ico_window_mgr_find_uclient(client);
2185     if (! uclient)  {
2186         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2187                                "ico_window_mgr_declare_manager: unknown client");
2188         uifw_trace("uifw_declare_manager: Leave(unknown client=%08x)", (int)client);
2189         return;
2190     }
2191     if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_DECLARE_MANAGER) == 0)   {
2192         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2193                                "ico_window_mgr_declare_manager: not permitted");
2194         uifw_trace("uifw_declare_manager: Leave(%s not permitted)", uclient->appid);
2195         return;
2196     }
2197
2198     uclient->manager = manager;
2199
2200     /* client set to manager            */
2201     _ico_win_mgr->num_manager = 0;
2202     wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
2203         if (mgr->resource == resource)  {
2204             if (mgr->manager != manager)    {
2205                 uifw_trace("uifw_declare_manager: Event Client.%08x Callback %d=>%d",
2206                            (int)client, mgr->manager, manager);
2207                 mgr->manager = manager;
2208
2209                 if (manager)    {
2210                     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
2211                         wl_list_for_each (usurf, &el->surface_list, ivi_layer) {
2212                             /* send window create event to manager  */
2213                             if (usurf->created != 0)    {
2214                                 uifw_trace("uifw_declare_manager: Send manager(%08x) "
2215                                            "WINDOW_CREATED(surf=%08x,pid=%d,appid=%s)",
2216                                            (int)resource, usurf->surfaceid,
2217                                            usurf->uclient->pid, usurf->uclient->appid);
2218                                 ico_window_mgr_send_window_created(resource,
2219                                                                    usurf->surfaceid,
2220                                                                    usurf->winname,
2221                                                                    usurf->uclient->pid,
2222                                                                    usurf->uclient->appid,
2223                                                                    usurf->layertype << 12);
2224                             }
2225                         }
2226                     }
2227                 }
2228             }
2229         }
2230         if (mgr->manager)   {
2231             _ico_win_mgr->num_manager++;
2232         }
2233     }
2234     uifw_trace("uifw_declare_manager: Leave(managers=%d)", _ico_win_mgr->num_manager);
2235 }
2236
2237 /*--------------------------------------------------------------------------*/
2238 /**
2239  * @brief   uifw_set_window_layer: set layer id to surface
2240  *
2241  * @param[in]   client      Weyland client
2242  * @param[in]   resource    resource of request
2243  * @param[in]   surfaceid   UIFW surface id
2244  * @param[in]   layer       layer id
2245  * @return      none
2246  */
2247 /*--------------------------------------------------------------------------*/
2248 static void
2249 uifw_set_window_layer(struct wl_client *client, struct wl_resource *resource,
2250                       uint32_t surfaceid, uint32_t layer)
2251 {
2252     struct uifw_client      *uclient;
2253     struct uifw_win_surface *usurf;
2254
2255     if (layer == ICO_WINDOW_MGR_LAYERTYPE_BACKGROUND)  {
2256         layer = _ico_ivi_background_layer;
2257     }
2258     else if (layer == ICO_WINDOW_MGR_LAYERTYPE_TOUCH)  {
2259         layer = _ico_ivi_touch_layer;
2260     }
2261     else if (layer == ICO_WINDOW_MGR_LAYERTYPE_CURSOR)    {
2262         layer = _ico_ivi_cursor_layer;
2263     }
2264     else if (layer == ICO_WINDOW_MGR_LAYERTYPE_STARTUP)    {
2265         layer = _ico_ivi_startup_layer;
2266     }
2267
2268     uifw_trace("uifw_set_window_layer: Enter res=%08x surfaceid=%08x layer=%d",
2269                (int)resource, surfaceid, layer);
2270
2271     /* check for access control         */
2272     if (resource != NULL)   {
2273         /* resource is NULL, internal use   */
2274         uclient = ico_window_mgr_find_uclient(client);
2275         if (! uclient)  {
2276             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2277                                    "ico_window_mgr_set_window_layer: unknown client");
2278             uifw_trace("uifw_set_window_layer: Leave(unknown client=%08x)", (int)client);
2279             return;
2280         }
2281         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_SET_WINDOW_LAYER) == 0)  {
2282             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2283                                    "ico_window_mgr_set_window_layer: not permitted");
2284             uifw_trace("uifw_set_window_layer: Leave(%s not permitted)", uclient->appid);
2285             return;
2286         }
2287     }
2288     usurf = ico_window_mgr_get_usurf_client(surfaceid, client);
2289
2290     if (! usurf)    {
2291         uifw_trace("uifw_set_window_layer: Leave(No Surface(id=%08x))", surfaceid);
2292         return;
2293     }
2294
2295     if (usurf->win_layer->layer != layer) {
2296         win_mgr_set_layer(usurf, layer);
2297         win_mgr_change_surface(usurf->surface, -1, 1);
2298     }
2299     uifw_trace("uifw_set_window_layer: Leave");
2300 }
2301
2302 /*--------------------------------------------------------------------------*/
2303 /**
2304  * @brief   uifw_set_positionsize: set surface position and size
2305  *
2306  * @param[in]   client      Weyland client
2307  * @param[in]   resource    resource of request
2308  * @param[in]   surfaceid   UIFW surface id
2309  * @param[in]   node        surface node id
2310  * @param[in]   x           X coordinate on screen(if bigger than 16383, no change)
2311  * @param[in]   y           Y coordinate on screen(if bigger than 16383, no change)
2312  * @param[in]   width       surface width(if bigger than 16383, no change)
2313  * @param[in]   height      surface height(if bigger than 16383, no change)
2314  * @param[in]   flags       with/without animation and client configure flag
2315  * @return      none
2316  */
2317 /*--------------------------------------------------------------------------*/
2318 static void
2319 uifw_set_positionsize(struct wl_client *client, struct wl_resource *resource,
2320                       uint32_t surfaceid, uint32_t node, int32_t x, int32_t y,
2321                       int32_t width, int32_t height, int32_t flags)
2322 {
2323     struct uifw_client      *uclient;
2324     struct uifw_win_surface *usurf;
2325     struct weston_surface   *es;
2326     struct weston_view      *ev;
2327     int     op;
2328     int     retanima;
2329     int     oldx, oldy;
2330     struct uifw_node_table  *oldnode;
2331
2332     uifw_trace("uifw_set_positionsize: Enter surf=%08x node=%x x/y/w/h=%d/%d/%d/%d flag=%x",
2333                surfaceid, node, x, y, width, height, flags);
2334
2335     /* check for access control         */
2336     uclient = ico_window_mgr_find_uclient(client);
2337     if (resource != NULL)   {
2338         /* resource is NULL, internal use   */
2339         if (! uclient)  {
2340             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2341                                    "ico_window_mgr_set_positionsize: unknown client");
2342             uifw_trace("uifw_set_positionsize: Leave(unknown client=%08x)", (int)client);
2343             return;
2344         }
2345         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_SET_POSITIONSIZE) == 0)  {
2346             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2347                                    "ico_window_mgr_set_positionsize: not permitted");
2348             uifw_trace("uifw_set_positionsize: Leave(%s not permitted)", uclient->appid);
2349             return;
2350         }
2351     }
2352     usurf = ico_window_mgr_get_usurf_client(surfaceid, client);
2353     if (! usurf)    {
2354         uifw_trace("uifw_set_positionsize: Leave(surf=%08x NOT Found)", surfaceid);
2355         return;
2356     }
2357     oldx = usurf->x;
2358     oldy = usurf->y;
2359     oldnode = usurf->node_tbl;
2360
2361     usurf->disable = 0;
2362     if (((int)node) >= _ico_num_nodes)  {
2363         uifw_trace("uifw_set_positionsize: node=%d dose not exist(max=%d)",
2364                    node, _ico_num_nodes);
2365         if ((ico_ivi_optionflag() & ICO_IVI_OPTION_SHOW_NODISP) == 0)   {
2366             if (usurf->visible) {
2367                 /* no display, change to hide   */
2368                 uifw_set_visible(client, resource, surfaceid, ICO_WINDOW_MGR_VISIBLE_HIDE,
2369                                  ICO_WINDOW_MGR_V_NOCHANGE, 0);
2370             }
2371             usurf->disable = 1;
2372         }
2373         node = 0;
2374     }
2375     usurf->node_tbl = &_ico_node_table[node];
2376
2377     es = usurf->surface;
2378     if (es)  {
2379         /* weston surface exist             */
2380         es = usurf->surface;
2381         retanima = ICO_WINDOW_MGR_ANIMATION_RET_NOANIMA;
2382
2383         /* if x,y,width,height bigger then ICO_IVI_MAX_COORDINATE, no change    */
2384         if (x > ICO_IVI_MAX_COORDINATE)         x = usurf->x;
2385         if (y > ICO_IVI_MAX_COORDINATE)         y = usurf->y;
2386         if (width > ICO_IVI_MAX_COORDINATE)     width = usurf->width;
2387         if (height > ICO_IVI_MAX_COORDINATE)    height = usurf->height;
2388
2389         /* check animation                  */
2390         if ((usurf->animation.restrain_configure != 0) &&
2391             (x == usurf->x) && (y == usurf->y) &&
2392             (width == usurf->width) && (height == usurf->height))   {
2393             uifw_trace("uifw_set_positionsize: Leave(same position size at animation)");
2394             return;
2395         }
2396
2397         if (uclient)    {
2398             if ((surfaceid != ICO_WINDOW_MGR_V_MAINSURFACE) &&
2399                 (uclient->manager == 0) && (uclient->privilege == 0))   uclient = NULL;
2400         }
2401         ev = ico_ivi_get_primary_view(usurf);
2402         if (! uclient)  {
2403             if ((usurf->width > 0) && (usurf->height > 0))  {
2404                 win_mgr_surface_change_mgr(es, x, y, width, height);
2405                 uifw_trace("uifw_set_positionsize: Leave(Request from App)");
2406                 return;
2407             }
2408
2409             uifw_trace("uifw_set_positionsize: Initial Position/Size visible=%d",
2410                        usurf->visible);
2411             /* Initiale position is (0,0)   */
2412             weston_view_set_position(ev, (float)(usurf->node_tbl->disp_x),
2413                                      (float)(usurf->node_tbl->disp_y));
2414         }
2415
2416         uifw_trace("uifw_set_positionsize: Old geometry x/y=%d/%d,w/h=%d/%d",
2417                    (int)ev->geometry.x, (int)ev->geometry.y,
2418                    (int)es->width, (int)es->height);
2419
2420         usurf->animation.pos_x = usurf->x;
2421         usurf->animation.pos_y = usurf->y;
2422         usurf->animation.pos_width = usurf->width;
2423         usurf->animation.pos_height = usurf->height;
2424         usurf->animation.no_configure = (flags & ICO_WINDOW_MGR_FLAGS_NO_CONFIGURE) ? 1 : 0;
2425
2426         usurf->x = x;
2427         usurf->y = y;
2428         usurf->width = width;
2429         usurf->height = height;
2430         if (_ico_win_mgr->num_manager <= 0) {
2431             /* no manager(HomeScreen), set geometory    */
2432             weston_view_set_position(ev, (float)(usurf->node_tbl->disp_x + x),
2433                                      (float)(usurf->node_tbl->disp_y + y));
2434         }
2435         if ((es->output) && (es->buffer_ref.buffer) &&
2436             (es->width > 0) && (es->height > 0)) {
2437             uifw_trace("uifw_set_positionsize: Fixed Geometry, Change(Vis=%d)",
2438                        usurf->visible);
2439             if (usurf->visible) {
2440                 if ((flags & ICO_WINDOW_MGR_FLAGS_ANIMATION) &&
2441                     (win_mgr_hook_animation != NULL))   {
2442                     /* with animation   */
2443                     if ((x != (ev->geometry.x - usurf->node_tbl->disp_x)) ||
2444                         (y != (ev->geometry.y - usurf->node_tbl->disp_y)))  {
2445                         op = ICO_WINDOW_MGR_ANIMATION_OPMOVE;
2446                     }
2447                     else if ((width != usurf->surface->width) ||
2448                              (height != usurf->surface->height))   {
2449                         op = ICO_WINDOW_MGR_ANIMATION_OPRESIZE;
2450                     }
2451                     else    {
2452                         op = ICO_WINDOW_MGR_ANIMATION_OPNONE;
2453                     }
2454                     if (((op == ICO_WINDOW_MGR_ANIMATION_OPMOVE) &&
2455                          (usurf->animation.move_anima != ICO_WINDOW_MGR_ANIMATION_NONE)) ||
2456                         ((op == ICO_WINDOW_MGR_ANIMATION_OPRESIZE) &&
2457                          (usurf->animation.resize_anima != ICO_WINDOW_MGR_ANIMATION_NONE))) {
2458                         retanima = (*win_mgr_hook_animation)(op, (void *)usurf);
2459                         uifw_trace("uifw_set_positionsize: ret call anima = %d", retanima);
2460                     }
2461                 }
2462                 if ((retanima == ICO_WINDOW_MGR_ANIMATION_RET_NOANIMA) ||
2463                     (retanima != ICO_WINDOW_MGR_ANIMATION_RET_ANIMANOCTL))  {
2464                     ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
2465                                                       usurf->width, usurf->height);
2466                 }
2467             }
2468         }
2469         if ((retanima == ICO_WINDOW_MGR_ANIMATION_RET_NOANIMA) ||
2470             (retanima != ICO_WINDOW_MGR_ANIMATION_RET_ANIMANOCTL))  {
2471             win_mgr_change_surface(es,
2472                                    (flags & ICO_WINDOW_MGR_FLAGS_NO_CONFIGURE) ? -1 : 0, 1);
2473         }
2474         uifw_trace("uifw_set_positionsize: Leave(OK,output=%08x)", (int)es->output);
2475     }
2476     else    {
2477         usurf->x = x;
2478         usurf->y = y;
2479         usurf->width = width;
2480         usurf->height = height;
2481         uifw_trace("uifw_set_positionsize: Leave(OK, but no buffer)");
2482     }
2483
2484     /* if position change, call hook for input region   */
2485     if (win_mgr_hook_change != NULL)    {
2486         if ((oldx != usurf->x) || (oldy != usurf->y) || (oldnode != usurf->node_tbl))   {
2487             (*win_mgr_hook_change)(usurf);
2488         }
2489     }
2490 }
2491
2492 /*--------------------------------------------------------------------------*/
2493 /**
2494  * @brief   uifw_set_visible: surface visible/raise control
2495  *
2496  * @param[in]   client      Weyland client
2497  * @param[in]   resource    resource of request
2498  * @param[in]   surfaceid   UIFW surface id
2499  * @param[in]   visible     visible(1=show/0=hide/other=no change)
2500  * @param[in]   raise       raise(1=raise/0=lower/other=no change)
2501  * @param[in]   flags       with/without animation
2502  * @return      none
2503  */
2504 /*--------------------------------------------------------------------------*/
2505 static void
2506 uifw_set_visible(struct wl_client *client, struct wl_resource *resource,
2507                  uint32_t surfaceid, int32_t visible, int32_t raise, int32_t flags)
2508 {
2509     struct uifw_win_surface *usurf;
2510     struct uifw_client      *uclient;
2511     struct weston_view      *ev;
2512     int     restack;
2513     int     retanima;
2514     int     oldvisible;
2515
2516     uifw_trace("uifw_set_visible: Enter(surf=%08x,%d,%d,%x)",
2517                surfaceid, visible, raise, flags);
2518
2519     /* check for access control         */
2520     uclient = ico_window_mgr_find_uclient(client);
2521     if (resource != NULL)   {
2522         /* resource is NULL, internal use   */
2523         if (! uclient)  {
2524             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2525                                    "ico_window_mgr_set_visible: unknown client");
2526             uifw_trace("uifw_set_visible: Leave(unknown client=%08x)", (int)client);
2527             return;
2528         }
2529         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_SET_POSITIONSIZE) == 0)  {
2530             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2531                                    "ico_window_mgr_set_visible: not permitted");
2532             uifw_trace("uifw_set_visible: Leave(%s not permitted)", uclient->appid);
2533             return;
2534         }
2535     }
2536     usurf = ico_window_mgr_get_usurf_client(surfaceid, client);
2537     if ((! usurf) || (! usurf->surface))    {
2538         uifw_trace("uifw_set_visible: Leave(Surface Not Exist)");
2539         return;
2540     }
2541     oldvisible = ico_window_mgr_is_visible(usurf);
2542
2543     if ((surfaceid != ICO_WINDOW_MGR_V_MAINSURFACE) &&
2544         (uclient->manager == 0) && (uclient->privilege == 0))   {
2545         uifw_trace("uifw_set_visible: Request from App(%s), not Manager",
2546                    uclient ? uclient->appid : "");
2547         uclient = NULL;
2548     }
2549     else    {
2550         uifw_trace("uifw_set_visible: Request from Manager(%s)", uclient->appid);
2551     }
2552
2553     restack = 0;
2554
2555     if ((usurf->disable == 0) && (visible == ICO_WINDOW_MGR_VISIBLE_SHOW))  {
2556
2557 #if  PERFORMANCE_EVALUATIONS > 0
2558         if (! usurf->visible)   {
2559             uifw_perf("SWAP_BUFFER Show appid=%s surface=%08x",
2560                       usurf->uclient->appid, usurf->surfaceid);
2561         }
2562 #endif /*PERFORMANCE_EVALUATIONS*/
2563         if ((! usurf->visible) ||
2564             (usurf->animation.state != ICO_WINDOW_MGR_ANIMATION_STATE_NONE))    {
2565             usurf->visible = 1;
2566             uifw_trace("uifw_set_visible: Change to Visible");
2567
2568             if (usurf->layertype != LAYER_TYPE_INPUTPANEL)  {
2569                 ico_ivi_shell_set_toplevel(usurf->shsurf);
2570             }
2571
2572             /* Weston surface configure                     */
2573             uifw_trace("uifw_set_visible: Visible to Weston WSurf=%08x,%d.%d/%d/%d/%d",
2574                        (int)usurf->surface, usurf->node_tbl->node, usurf->x, usurf->y,
2575                        usurf->width, usurf->height);
2576             ico_ivi_shell_set_surface_type(usurf->shsurf);
2577             ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
2578                                               usurf->width, usurf->height);
2579
2580             restack = 1;                    /* need damage      */
2581
2582             if ((flags & (ICO_WINDOW_MGR_ANIMATION_POS|ICO_WINDOW_MGR_FLAGS_ANIMATION)) &&
2583                 (usurf->animation.show_anima != ICO_WINDOW_MGR_ANIMATION_NONE) &&
2584                 (win_mgr_hook_animation != NULL))   {
2585                 usurf->animation.pos_x = usurf->x;
2586                 usurf->animation.pos_y = usurf->y;
2587                 usurf->animation.pos_width = usurf->width;
2588                 usurf->animation.pos_height = usurf->height;
2589                 usurf->animation.no_configure = 0;
2590                 retanima = (*win_mgr_hook_animation)(
2591                                 (flags & ICO_WINDOW_MGR_ANIMATION_POS) ?
2592                                     ICO_WINDOW_MGR_ANIMATION_OPSHOWPOS :
2593                                     ICO_WINDOW_MGR_ANIMATION_OPSHOW,
2594                                 (void *)usurf);
2595                 uifw_trace("uifw_set_visible: ret call anima = %d", retanima);
2596             }
2597         }
2598         else if ((raise != ICO_WINDOW_MGR_RAISE_LOWER) &&
2599                  (raise != ICO_WINDOW_MGR_RAISE_RAISE))  {
2600             uifw_trace("uifw_set_visible: Leave(No Change)");
2601             return;
2602         }
2603     }
2604     else if (visible == ICO_WINDOW_MGR_VISIBLE_HIDE)    {
2605
2606 #if  PERFORMANCE_EVALUATIONS > 0
2607         if (usurf->visible) {
2608             uifw_perf("SWAP_BUFFER Hide appid=%s surface=%08x",
2609                       usurf->uclient->appid, usurf->surfaceid);
2610         }
2611 #endif /*PERFORMANCE_EVALUATIONS*/
2612         if ((usurf->visible) ||
2613             (usurf->animation.state != ICO_WINDOW_MGR_ANIMATION_STATE_NONE))    {
2614
2615             /* Reset focus                                  */
2616             win_mgr_reset_focus(usurf);
2617
2618             /* Weston surface configure                     */
2619             ev = ico_ivi_get_primary_view(usurf);
2620             weston_view_damage_below(ev);
2621             ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
2622                                               usurf->width, usurf->height);
2623
2624             retanima = ICO_WINDOW_MGR_ANIMATION_RET_ANIMA;
2625             if ((flags & (ICO_WINDOW_MGR_FLAGS_ANIMATION|ICO_WINDOW_MGR_ANIMATION_POS)) &&
2626                 (usurf->animation.hide_anima != ICO_WINDOW_MGR_ANIMATION_NONE) &&
2627                 (win_mgr_hook_animation != NULL))   {
2628                 usurf->animation.pos_x = usurf->x;
2629                 usurf->animation.pos_y = usurf->y;
2630                 usurf->animation.pos_width = usurf->width;
2631                 usurf->animation.pos_height = usurf->height;
2632                 usurf->animation.no_configure = 0;
2633                 retanima = (*win_mgr_hook_animation)(
2634                                 (flags & ICO_WINDOW_MGR_ANIMATION_POS) ?
2635                                     ICO_WINDOW_MGR_ANIMATION_OPHIDEPOS :
2636                                     ICO_WINDOW_MGR_ANIMATION_OPHIDE,
2637                                 (void *)usurf);
2638             }
2639             if (retanima != ICO_WINDOW_MGR_ANIMATION_RET_ANIMANOCTL)    {
2640                 usurf->visible = 0;
2641                 uifw_trace("uifw_set_visible: Change to UnVisible");
2642                 /* change visible to unvisible, restack surface list    */
2643                 restack = 1;
2644                 /* Weston surface configure                     */
2645                 ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
2646                                                   usurf->width, usurf->height);
2647             }
2648             else    {
2649                 uifw_trace("uifw_set_visible: UnVisible but animation");
2650             }
2651         }
2652         else if ((raise != ICO_WINDOW_MGR_RAISE_LOWER) &&
2653                  (raise != ICO_WINDOW_MGR_RAISE_RAISE))  {
2654             uifw_trace("uifw_set_visible: Leave(No Change)");
2655             return;
2656         }
2657     }
2658     else if ((raise != ICO_WINDOW_MGR_RAISE_LOWER) &&
2659              (raise != ICO_WINDOW_MGR_RAISE_RAISE))  {
2660         uifw_trace("uifw_set_visible: Leave(No Change)");
2661         return;
2662     }
2663
2664     /* raise/lower                              */
2665     if ((raise == ICO_WINDOW_MGR_RAISE_LOWER) || (raise == ICO_WINDOW_MGR_RAISE_RAISE))  {
2666         win_mgr_set_raise(usurf, raise);
2667         if (usurf->visible == 0)    {
2668             restack |= 2;
2669         }
2670     }
2671     else    {
2672         raise = ICO_WINDOW_MGR_V_NOCHANGE;
2673     }
2674
2675     if (restack)    {
2676         ico_window_mgr_restack_layer(usurf);
2677     }
2678
2679     /* send event(VISIBLE) to manager           */
2680     if (restack)    {
2681         ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
2682                                 usurf,
2683                                 (visible == ICO_WINDOW_MGR_VISIBLE_SHOW) ? 1 :
2684                                     ((visible == ICO_WINDOW_MGR_VISIBLE_HIDE) ? 0 :
2685                                         ICO_WINDOW_MGR_V_NOCHANGE),
2686                                 raise, uclient ? 0 : 1, 0,0);
2687     }
2688
2689     /* if visible change, call hook for input region    */
2690     if (win_mgr_hook_change != NULL)    {
2691         if (oldvisible != ico_window_mgr_is_visible(usurf))    {
2692             (*win_mgr_hook_change)(usurf);
2693         }
2694     }
2695     uifw_trace("uifw_set_visible: Leave(OK)");
2696 }
2697
2698 /*--------------------------------------------------------------------------*/
2699 /**
2700  * @brief   uifw_set_animation: set animation of surface visible/unvisible
2701  *
2702  * @param[in]   client      Weyland client
2703  * @param[in]   resource    resource of request
2704  * @param[in]   surfaceid   UIFW surface id
2705  * @param[in]   type        how to change surface
2706  * @param[in]   anmation    animation name
2707  * @param[in]   time        animation time(ms), if 0, default time
2708  * @return      none
2709  */
2710 /*--------------------------------------------------------------------------*/
2711 static void
2712 uifw_set_animation(struct wl_client *client, struct wl_resource *resource,
2713                    uint32_t surfaceid, int32_t type, const char *animation, int32_t time)
2714 {
2715     struct uifw_client *uclient;
2716     int     animaid;
2717     struct uifw_win_surface *usurf = ico_window_mgr_get_usurf_client(surfaceid, client);
2718
2719     uifw_trace("uifw_set_transition: surf=%08x,type=%x,anim=%s,time=%d",
2720                surfaceid, type, animation, time);
2721
2722     /* check for access control         */
2723     if (resource != NULL)   {
2724         /* resource is NULL, internal use   */
2725         uclient = ico_window_mgr_find_uclient(client);
2726         if (! uclient)  {
2727             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2728                                    "ico_window_mgr_set_animation: unknown client");
2729             uifw_trace("uifw_set_animation: Leave(unknown client=%08x)", (int)client);
2730             return;
2731         }
2732         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_SET_ANIMATION) == 0) {
2733             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2734                                    "ico_window_mgr_set_animation: not permitted");
2735             uifw_trace("uifw_set_animation: Leave(%s not permitted)", uclient->appid);
2736             return;
2737         }
2738     }
2739     if (usurf) {
2740         if ((*animation != 0) && (*animation != ' '))   {
2741             animaid = ico_get_animation_name(animation);
2742             uifw_trace("uifw_set_animation: Leave(OK) type=%d", animaid);
2743             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_HIDE)  {
2744                 if ((usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPHIDE) ||
2745                     (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPHIDEPOS))  {
2746                     usurf->animation.next_anima = animaid;
2747                 }
2748                 else    {
2749                     usurf->animation.hide_anima = animaid;
2750                 }
2751             }
2752             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_SHOW)  {
2753                 if ((usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPSHOW) ||
2754                     (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPSHOWPOS))  {
2755                     usurf->animation.next_anima = animaid;
2756                 }
2757                 else    {
2758                     usurf->animation.show_anima = animaid;
2759                 }
2760             }
2761             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_MOVE)  {
2762                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPMOVE)    {
2763                     usurf->animation.next_anima = animaid;
2764                 }
2765                 else    {
2766                     usurf->animation.move_anima = animaid;
2767                 }
2768             }
2769             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_RESIZE)    {
2770                 if (usurf->animation.type == ICO_WINDOW_MGR_ANIMATION_OPRESIZE)  {
2771                     usurf->animation.next_anima = animaid;
2772                 }
2773                 else    {
2774                     usurf->animation.resize_anima = animaid;
2775                 }
2776             }
2777         }
2778         if ((time > 0) && (time != ICO_WINDOW_MGR_V_NOCHANGE))  {
2779             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_HIDE)  {
2780                 usurf->animation.hide_time = time;
2781             }
2782             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_SHOW)  {
2783                 usurf->animation.show_time = time;
2784             }
2785             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_MOVE)  {
2786                 usurf->animation.move_time = time;
2787             }
2788             if (type & ICO_WINDOW_MGR_ANIMATION_TYPE_RESIZE)    {
2789                 usurf->animation.resize_time = time;
2790             }
2791         }
2792     }
2793     else    {
2794         uifw_trace("uifw_set_animation: Surface(%08x) Not exist", surfaceid);
2795     }
2796 }
2797
2798 /*--------------------------------------------------------------------------*/
2799 /**
2800  * @brief   uifw_set_attributes: set surface attributes
2801  *
2802  * @param[in]   client      Weyland client
2803  * @param[in]   resource    resource of request
2804  * @param[in]   surfaceid   UIFW surface id
2805  * @param[in]   attributes  surface attributes
2806  * @return      none
2807  */
2808 /*--------------------------------------------------------------------------*/
2809 static void
2810 uifw_set_attributes(struct wl_client *client, struct wl_resource *resource,
2811                     uint32_t surfaceid, uint32_t attributes)
2812 {
2813     struct uifw_client *uclient;
2814     struct uifw_win_surface *usurf = ico_window_mgr_get_usurf_client(surfaceid, client);
2815
2816     uifw_trace("uifw_set_attributes: Enter(surf=%08x,attributes=%x)", surfaceid, attributes);
2817
2818     /* check for access control         */
2819     if (resource != NULL)   {
2820         /* resource is NULL, internal use   */
2821         uclient = ico_window_mgr_find_uclient(client);
2822         if (! uclient)  {
2823             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2824                                    "ico_window_mgr_set_attributes: unknown client");
2825             uifw_trace("uifw_set_attributes: Leave(unknown client=%08x)", (int)client);
2826             return;
2827         }
2828         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_SET_ATTRIBUTES) == 0)    {
2829             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2830                                    "ico_window_mgr_set_attributes: not permitted");
2831             uifw_trace("uifw_set_attributes: Leave(%s not permitted)", uclient->appid);
2832             return;
2833         }
2834     }
2835     if (usurf) {
2836         usurf->attributes = attributes;
2837         if ((attributes & (ICO_WINDOW_MGR_ATTR_ALIGN_LEFT|ICO_WINDOW_MGR_ATTR_ALIGN_RIGHT)) ==
2838             (ICO_WINDOW_MGR_ATTR_ALIGN_LEFT|ICO_WINDOW_MGR_ATTR_ALIGN_RIGHT))   {
2839             usurf->attributes &=
2840                 ~(ICO_WINDOW_MGR_ATTR_ALIGN_LEFT|ICO_WINDOW_MGR_ATTR_ALIGN_RIGHT);
2841         }
2842         if ((attributes & (ICO_WINDOW_MGR_ATTR_ALIGN_TOP|ICO_WINDOW_MGR_ATTR_ALIGN_BOTTOM)) ==
2843             (ICO_WINDOW_MGR_ATTR_ALIGN_TOP|ICO_WINDOW_MGR_ATTR_ALIGN_BOTTOM))   {
2844             usurf->attributes &=
2845                 ~(ICO_WINDOW_MGR_ATTR_ALIGN_TOP|ICO_WINDOW_MGR_ATTR_ALIGN_BOTTOM);
2846         }
2847     }
2848     uifw_trace("uifw_set_attributes: Leave");
2849 }
2850
2851 /*--------------------------------------------------------------------------*/
2852 /**
2853  * @brief   uifw_visible_animation: surface visibility control with animation
2854  *
2855  * @param[in]   client      Weyland client
2856  * @param[in]   resource    resource of request
2857  * @param[in]   surfaceid   surface id
2858  * @param[in]   visible     visible(1=show/0=hide)
2859  * @param[in]   x           X coordinate on screen(if bigger than 16383, no change)
2860  * @param[in]   y           Y coordinate on screen(if bigger than 16383, no change)
2861  * @param[in]   width       surface width(if bigger than 16383, no change)
2862  * @param[in]   height      surface height(if bigger than 16383, no change)
2863  * @return      none
2864  */
2865 /*--------------------------------------------------------------------------*/
2866 static void
2867 uifw_visible_animation(struct wl_client *client, struct wl_resource *resource,
2868                        uint32_t surfaceid, int32_t visible,
2869                        int32_t x, int32_t y, int32_t width, int32_t height)
2870 {
2871     struct uifw_client *uclient;
2872     struct uifw_win_surface *usurf;
2873
2874     uifw_trace("uifw_visible_animation: Enter(%08x,%d,x/y=%d/%d,w/h=%d/%d)",
2875                surfaceid, visible, x, y, width, height);
2876
2877     /* check for access control         */
2878     if (resource != NULL)   {
2879         /* resource is NULL, internal use   */
2880         uclient = ico_window_mgr_find_uclient(client);
2881         if (! uclient)  {
2882             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2883                                    "ico_window_mgr_visible_animation: unknown client");
2884             uifw_trace("uifw_visible_animation: Leave(unknown client=%08x)", (int)client);
2885             return;
2886         }
2887         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_VISIBLE_ANIMATION) == 0) {
2888             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2889                                    "ico_window_mgr_visible_animation: not permitted");
2890             uifw_trace("uifw_visible_animation: Leave(%s not permitted)", uclient->appid);
2891             return;
2892         }
2893     }
2894     usurf = ico_window_mgr_get_usurf_client(surfaceid, client);
2895
2896     if ((! usurf) || (! usurf->surface))    {
2897         uifw_trace("uifw_visible_animation: Leave(Surface Not Exist)");
2898         return;
2899     }
2900
2901     usurf->animation.pos_x = x;
2902     usurf->animation.pos_y = y;
2903     if (width > 0)  usurf->animation.pos_width = width;
2904     else            usurf->animation.pos_width = 1;
2905     if (height > 0) usurf->animation.pos_height = height;
2906     else            usurf->animation.pos_height = 1;
2907     usurf->animation.no_configure = 0;
2908
2909     uifw_set_visible(client, resource, surfaceid, visible,
2910                      ICO_WINDOW_MGR_V_NOCHANGE, ICO_WINDOW_MGR_ANIMATION_POS);
2911
2912     uifw_trace("uifw_visible_animation: Leave");
2913 }
2914
2915 /*--------------------------------------------------------------------------*/
2916 /**
2917  * @brief   uifw_set_active: set active surface
2918  *
2919  * @param[in]   client      Weyland client
2920  * @param[in]   resource    resource of request
2921  * @param[in]   surfaceid   UIFW surface id
2922  * @param[in]   active      target device
2923  * @return      none
2924  */
2925 /*--------------------------------------------------------------------------*/
2926 static void
2927 uifw_set_active(struct wl_client *client, struct wl_resource *resource,
2928                 uint32_t surfaceid, int32_t active)
2929 {
2930     struct uifw_client *uclient;
2931     struct uifw_win_surface *usurf;
2932
2933     uifw_trace("uifw_set_active: Enter(surf=%08x,active=%x)", surfaceid, active);
2934
2935     /* check for access control         */
2936     if (resource != NULL)   {
2937         /* resource is NULL, internal use   */
2938         uclient = ico_window_mgr_find_uclient(client);
2939         if (! uclient)  {
2940             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2941                                    "ico_window_mgr_set_active: unknown client");
2942             uifw_trace("uifw_set_active: Leave(unknown client=%08x)", (int)client);
2943             return;
2944         }
2945         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_SET_ACTIVE) == 0)    {
2946             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2947                                    "ico_window_mgr_set_active: not permitted");
2948             uifw_trace("uifw_set_active: Leave(%s not permitted)", uclient->appid);
2949             return;
2950         }
2951     }
2952     if ((surfaceid > 0) &&
2953         ((active & (ICO_WINDOW_MGR_ACTIVE_POINTER|ICO_WINDOW_MGR_ACTIVE_KEYBOARD)) != 0)) {
2954         usurf = ico_window_mgr_get_usurf_client(surfaceid, client);
2955     }
2956     else    {
2957         usurf = NULL;
2958     }
2959     if (usurf) {
2960         switch (active & (ICO_WINDOW_MGR_ACTIVE_POINTER|ICO_WINDOW_MGR_ACTIVE_KEYBOARD)) {
2961         case ICO_WINDOW_MGR_ACTIVE_POINTER:
2962             if (usurf != _ico_win_mgr->active_pointer_usurf)  {
2963                 uifw_trace("uifw_set_active: pointer active change %08x->%08x",
2964                            _ico_win_mgr->active_pointer_usurf ?
2965                                _ico_win_mgr->active_pointer_usurf->surfaceid : 0,
2966                            usurf ? usurf->surfaceid : 0);
2967                 if (_ico_win_mgr->active_pointer_usurf)   {
2968                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
2969                                             _ico_win_mgr->active_pointer_usurf,
2970                                             (_ico_win_mgr->active_keyboard_usurf ==
2971                                              _ico_win_mgr->active_pointer_usurf) ?
2972                                                 ICO_WINDOW_MGR_ACTIVE_KEYBOARD :
2973                                                 ICO_WINDOW_MGR_ACTIVE_NONE,
2974                                             0,0,0,0);
2975                 }
2976                 _ico_win_mgr->active_pointer_usurf = usurf;
2977                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
2978                                         usurf,
2979                                         ICO_WINDOW_MGR_ACTIVE_POINTER |
2980                                         (_ico_win_mgr->active_keyboard_usurf == usurf) ?
2981                                             ICO_WINDOW_MGR_ACTIVE_KEYBOARD : 0,
2982                                         0,0,0,0);
2983                 win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER);
2984             }
2985             break;
2986         case ICO_WINDOW_MGR_ACTIVE_KEYBOARD:
2987             if (usurf != _ico_win_mgr->active_keyboard_usurf) {
2988                 uifw_trace("uifw_set_active: keyboard active change %08x->%08x",
2989                            _ico_win_mgr->active_keyboard_usurf ?
2990                                _ico_win_mgr->active_keyboard_usurf->surfaceid : 0,
2991                            usurf ? usurf->surfaceid : 0);
2992                 if (_ico_win_mgr->active_keyboard_usurf)   {
2993                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
2994                                             _ico_win_mgr->active_keyboard_usurf,
2995                                             (_ico_win_mgr->active_keyboard_usurf ==
2996                                              _ico_win_mgr->active_pointer_usurf) ?
2997                                                 ICO_WINDOW_MGR_ACTIVE_POINTER :
2998                                                 ICO_WINDOW_MGR_ACTIVE_NONE,
2999                                             0,0,0,0);
3000                 }
3001                 _ico_win_mgr->active_keyboard_usurf = usurf;
3002                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
3003                                         usurf,
3004                                         ICO_WINDOW_MGR_ACTIVE_KEYBOARD |
3005                                         (_ico_win_mgr->active_pointer_usurf == usurf) ?
3006                                             ICO_WINDOW_MGR_ACTIVE_POINTER : 0,
3007                                         0,0,0,0);
3008                 win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
3009             }
3010             break;
3011         default:
3012             if ((usurf != _ico_win_mgr->active_pointer_usurf) ||
3013                 (usurf != _ico_win_mgr->active_keyboard_usurf))   {
3014                 uifw_trace("uifw_set_active: active change %08x/%08x->%08x",
3015                            _ico_win_mgr->active_pointer_usurf ?
3016                                _ico_win_mgr->active_pointer_usurf->surfaceid : 0,
3017                            _ico_win_mgr->active_keyboard_usurf ?
3018                                _ico_win_mgr->active_keyboard_usurf->surfaceid : 0,
3019                            usurf ? usurf->surfaceid : 0);
3020                 if (_ico_win_mgr->active_pointer_usurf)   {
3021                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
3022                                             _ico_win_mgr->active_pointer_usurf,
3023                                             ICO_WINDOW_MGR_ACTIVE_NONE,
3024                                             0,0,0,0);
3025                     if (_ico_win_mgr->active_keyboard_usurf ==
3026                         _ico_win_mgr->active_pointer_usurf)   {
3027                         _ico_win_mgr->active_keyboard_usurf = NULL;
3028                     }
3029                 }
3030                 if (_ico_win_mgr->active_keyboard_usurf)   {
3031                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
3032                                             _ico_win_mgr->active_keyboard_usurf,
3033                                             ICO_WINDOW_MGR_ACTIVE_NONE,
3034                                             0,0,0,0);
3035                 }
3036                 _ico_win_mgr->active_pointer_usurf = usurf;
3037                 _ico_win_mgr->active_keyboard_usurf = usurf;
3038                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
3039                                         usurf,
3040                                         ICO_WINDOW_MGR_ACTIVE_POINTER |
3041                                             ICO_WINDOW_MGR_ACTIVE_KEYBOARD,
3042                                         0,0,0,0);
3043                 win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER |
3044                                               ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
3045             }
3046             break;
3047         }
3048         uifw_trace("uifw_set_active: Leave(Change Active)");
3049     }
3050     else    {
3051         win_mgr_set_active(NULL, active);
3052         uifw_trace("uifw_set_active: Leave(Reset active surface)");
3053     }
3054 }
3055
3056 /*--------------------------------------------------------------------------*/
3057 /**
3058  * @brief   uifw_set_layer_visible: layer visible control
3059  *
3060  * @param[in]   client      Weyland client
3061  * @param[in]   resource    resource of request
3062  * @param[in]   layer       layer id
3063  * @param[in]   visible     visible(1=show/0=hide)
3064  * @return      none
3065  */
3066 /*--------------------------------------------------------------------------*/
3067 static void
3068 uifw_set_layer_visible(struct wl_client *client, struct wl_resource *resource,
3069                        uint32_t layer, int32_t visible)
3070 {
3071     struct uifw_client      *uclient;
3072     struct uifw_win_layer   *el;
3073     struct uifw_win_layer   *new_el;
3074     struct uifw_win_surface *usurf;
3075     struct weston_view      *ev;
3076     int     layertype = 0;
3077
3078     if ((layer == ICO_WINDOW_MGR_LAYERTYPE_BACKGROUND) ||
3079         (layer == (uint32_t)_ico_ivi_background_layer))   {
3080         layer = _ico_ivi_background_layer;
3081         layertype = LAYER_TYPE_BACKGROUND;
3082     }
3083     else if ((layer == ICO_WINDOW_MGR_LAYERTYPE_TOUCH) ||
3084              (layer == (uint32_t)_ico_ivi_touch_layer))   {
3085         layer = _ico_ivi_touch_layer;
3086         layertype = LAYER_TYPE_TOUCH;
3087     }
3088     else if ((layer == ICO_WINDOW_MGR_LAYERTYPE_CURSOR) ||
3089              (layer == (uint32_t)_ico_ivi_cursor_layer))  {
3090         layer = _ico_ivi_cursor_layer;
3091         layertype = LAYER_TYPE_CURSOR;
3092     }
3093     else if (layer == ICO_WINDOW_MGR_LAYERTYPE_STARTUP)    {
3094         layer = _ico_ivi_startup_layer;
3095     }
3096     uifw_trace("uifw_set_layer_visible: Enter(layer=%d, visilbe=%d)", layer, visible);
3097
3098     /* check for access control         */
3099     if (resource != NULL)   {
3100         /* resource is NULL, internal use   */
3101         uclient = ico_window_mgr_find_uclient(client);
3102         if (! uclient)  {
3103             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3104                                    "ico_window_mgr_set_layer_visible: unknown client");
3105             uifw_trace("uifw_set_layer_visible: Leave(unknown client=%08x)", (int)client);
3106             return;
3107         }
3108         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_SET_LAYER_VISIBLE) == 0) {
3109             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3110                                    "ico_window_mgr_set_layer_visible: not permitted");
3111             uifw_trace("uifw_set_layer_visible: Leave(%s not permitted)", uclient->appid);
3112             return;
3113         }
3114     }
3115     /* Search Layer                             */
3116     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
3117         if (el->layer == layer) break;
3118     }
3119
3120     if (&el->link == &_ico_win_mgr->ivi_layer_list)    {
3121         /* layer not exist, create new layer    */
3122         uifw_trace("uifw_set_layer_visible: New Layer %d", layer);
3123         new_el = win_mgr_create_layer(NULL, layer, layertype);
3124         if (! new_el)   {
3125             uifw_trace("uifw_set_layer_visible: Leave(No Memory)");
3126             return;
3127         }
3128         new_el->visible = (visible != 0) ? TRUE : FALSE;
3129         ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_LAYER_VISIBLE, NULL,
3130                                 layer, new_el->visible, 0,0,0);
3131         uifw_trace("uifw_set_layer_visible: Leave(new layer)");
3132         return;
3133     }
3134
3135     /* control all surface in layer */
3136     if ((el->visible != FALSE) && (visible == 0))   {
3137         /* layer change to NOT visible  */
3138         uifw_trace("uifw_set_layer_visible: change to not visible");
3139         el->visible = FALSE;
3140     }
3141     else if ((el->visible == FALSE) && (visible != 0))  {
3142         /* layer change to visible      */
3143         uifw_trace("uifw_set_layer_visible: change to visible");
3144         el->visible = TRUE;
3145     }
3146     else    {
3147         /* no change    */
3148         uifw_trace("uifw_set_layer_visible: Leave(no Change %d=>%d)",
3149                    el->visible, visible);
3150         return;
3151     }
3152
3153     /* set damege area          */
3154     wl_list_for_each (usurf, &el->surface_list, ivi_layer) {
3155         if ((usurf->visible != FALSE) && (usurf->surface != NULL) &&
3156             (usurf->surface->output != NULL))  {
3157             /* Reset focus if hide              */
3158             if (visible == 0)   {
3159                 win_mgr_reset_focus(usurf);
3160             }
3161             /* Damage(redraw) target surface    */
3162             ev = ico_ivi_get_primary_view(usurf);
3163             weston_view_damage_below(ev);
3164         }
3165     }
3166
3167     /* rebild compositor surface list       */
3168     ico_window_mgr_restack_layer(NULL);
3169
3170     /* send layer visible event to manager  */
3171     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_LAYER_VISIBLE, NULL,
3172                             layer, el->visible, 0,0,0);
3173
3174     uifw_trace("uifw_set_layer_visible: Leave");
3175 }
3176
3177 /*--------------------------------------------------------------------------*/
3178 /**
3179  * @brief   uifw_get_surfaces: get application surfaces
3180  *
3181  * @param[in]   client      Weyland client
3182  * @param[in]   resource    resource of request
3183  * @param[in]   appid       application id
3184  * @param[in]   pid         process id
3185  * @return      none
3186  */
3187 /*--------------------------------------------------------------------------*/
3188 static void
3189 uifw_get_surfaces(struct wl_client *client, struct wl_resource *resource,
3190                   const char *appid, int32_t pid)
3191 {
3192     struct uifw_client  *uclient;
3193     struct uifw_win_layer *el;
3194     struct uifw_win_surface *usurf;
3195     struct wl_array     reply;
3196     uint32_t            *up;
3197
3198     uifw_trace("uifw_get_surfaces: Enter(appid=%s, pid=%d)", appid ? appid : " ", pid);
3199
3200     /* check for access control         */
3201     uclient = ico_window_mgr_find_uclient(client);
3202     if (! uclient)  {
3203         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3204                                "ico_window_mgr_get_surfaces: unknown client");
3205         uifw_trace("uifw_get_surfaces: Leave(unknown client=%08x)", (int)client);
3206         return;
3207     }
3208     if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_GET_SURFACES) == 0)  {
3209         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3210                                "ico_window_mgr_get_surfaces: not permitted");
3211         uifw_trace("uifw_get_surfaces: Leave(%s not permitted)", uclient->appid);
3212         return;
3213     }
3214
3215     wl_array_init(&reply);
3216
3217     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
3218         if ((appid != NULL) && (*appid != ' ')) {
3219             if (strcmp(uclient->appid, appid) == 0) break;
3220         }
3221         if (pid != 0)   {
3222             if (uclient->pid == pid)    break;
3223         }
3224     }
3225     if (&uclient->link == &_ico_win_mgr->client_list)    {
3226         uifw_trace("uifw_get_surfaces: appid=%s pid=%d dose not exist",
3227                    appid ? appid : " ", pid);
3228     }
3229     else    {
3230         wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link) {
3231             wl_list_for_each (usurf, &el->surface_list, ivi_layer) {
3232                 if (usurf->uclient == uclient)  {
3233                     uifw_trace("uifw_get_surfaces: %s(%d) surf=%08x",
3234                                uclient->appid, uclient->pid, usurf->surfaceid);
3235                     up = (uint32_t *)wl_array_add(&reply, sizeof(uint32_t));
3236                     if (up) {
3237                         *up = usurf->surfaceid;
3238                     }
3239                 }
3240             }
3241         }
3242     }
3243     ico_window_mgr_send_app_surfaces(resource, uclient->appid, uclient->pid, &reply);
3244
3245     wl_array_release(&reply);
3246     uifw_trace("uifw_get_surfaces: Leave");
3247 }
3248
3249 /*--------------------------------------------------------------------------*/
3250 /**
3251  * @brief   win_mgr_check_mapsurface: check and change all surface
3252  *
3253  * @param[in]   animation   weston animation table(unused)
3254  * @param[in]   outout      weston output table(unused)
3255  * @param[in]   mseces      current time(unused)
3256  * @return      none
3257  */
3258 /*--------------------------------------------------------------------------*/
3259 static void
3260 win_mgr_check_mapsurface(struct weston_animation *animation,
3261                          struct weston_output *output, uint32_t msecs)
3262 {
3263     struct uifw_surface_map *sm, *sm_tmp;
3264     uint32_t    curtime;
3265     int         wait = 99999999;
3266
3267     /* check touch down counter     */
3268     if ((touch_check_seat) &&
3269         (touch_check_seat->touch))  {
3270         if (touch_check_seat->touch->num_tp > 10)  {
3271             uifw_trace("win_mgr_check_mapsurface: illegal touch counter(num=%d), reset",
3272                        (int)touch_check_seat->touch->num_tp);
3273             touch_check_seat->touch->num_tp = 0;
3274         }
3275     }
3276
3277     /* check all mapped surfaces    */
3278     curtime = weston_compositor_get_time();
3279     wl_list_for_each_safe (sm, sm_tmp, &_ico_win_mgr->map_list, map_link)   {
3280         uifw_detail("win_mgr_check_mapsurface: sm=%08x surf=%08x",
3281                     (int)sm, sm->usurf->surfaceid);
3282         win_mgr_change_mapsurface(sm, 0, curtime);
3283         if (sm->eventque)   {
3284             if (sm->interval < wait)    {
3285                 wait = sm->interval;
3286             }
3287         }
3288     }
3289
3290     /* check frame interval         */
3291     if (wait < 2000)    {
3292         wait = wait / 2;
3293     }
3294     else    {
3295         wait = 1000;
3296     }
3297     wl_event_source_timer_update(_ico_win_mgr->wait_mapevent, wait);
3298 }
3299
3300 /*--------------------------------------------------------------------------*/
3301 /**
3302  * @brief   win_mgr_timer_mapsurface: mapped surface check timer
3303  *
3304  * @param[in]   data        user data(unused)
3305  * @return      fixed 1
3306  */
3307 /*--------------------------------------------------------------------------*/
3308 static int
3309 win_mgr_timer_mapsurface(void *data)
3310 {
3311     win_mgr_check_mapsurface(NULL, NULL, 0);
3312     return 1;
3313 }
3314
3315 /*--------------------------------------------------------------------------*/
3316 /**
3317  * @brief   win_mgr_change_mapsurface: check and change mapped surface
3318  *
3319  * @param[in]   sm          map surface table
3320  * @param[in]   event       send event (if 0, send if changed)
3321  * @param[in]   curtime     current time(ms)
3322  * @return      none
3323  */
3324 /*--------------------------------------------------------------------------*/
3325 static void
3326 win_mgr_change_mapsurface(struct uifw_surface_map *sm, int event, uint32_t curtime)
3327 {
3328     struct uifw_drm_buffer  *drm_buffer;
3329     struct uifw_dri_image   *dri_image;
3330     struct uifw_intel_region  *dri_region;
3331     struct uifw_gl_surface_state *gl_state;
3332     struct weston_surface   *es;
3333     struct weston_view      *ev;
3334     struct wl_shm_buffer    *shm_buffer;
3335     uint32_t    eglname;
3336     int         width;
3337     int         height;
3338     int         stride;
3339     uint32_t    format;
3340     uint32_t    dtime;
3341     int         idx, idx2;
3342     int         delta, settime2;
3343     struct ico_uifw_image_buffer    *p;
3344
3345     uifw_detail("win_mgr_change_mapsurface: surf=%08x event=%d", sm->usurf->surfaceid, event);
3346     if (event == 0) {
3347         event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_CONTENTS;
3348     }
3349
3350     /* check if buffered        */
3351     drm_buffer = NULL;
3352     es = sm->usurf->surface;
3353     if ((es == NULL) ||
3354         ((sm->type == ICO_WINDOW_MGR_MAP_TYPE_EGL) &&
3355          ((es->buffer_ref.buffer == NULL) ||
3356           (es->buffer_ref.buffer->width <= 0) || (es->buffer_ref.buffer->height <= 0)))) {
3357         /* surface has no buffer    */
3358         uifw_debug("win_mgr_change_mapsurface: surface(%08x) has no buffer %08x %08x",
3359                    sm->usurf->surfaceid, (int)es,
3360                    es ? (int)es->buffer_ref.buffer : 0);
3361         if (sm->initflag)   {
3362             event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP;
3363         }
3364         else    {
3365             event = 0;
3366         }
3367     }
3368     else if (sm->type == ICO_WINDOW_MGR_MAP_TYPE_EGL)   {
3369         if ((es->buffer_ref.buffer->legacy_buffer != NULL) && (es->renderer_state != NULL)) {
3370             drm_buffer = (struct uifw_drm_buffer *)wl_resource_get_user_data(
3371                                 (struct wl_resource *)es->buffer_ref.buffer->legacy_buffer);
3372             if ((drm_buffer != NULL) && (drm_buffer->driver_buffer == NULL))    {
3373                 drm_buffer = NULL;
3374             }
3375         }
3376         if (drm_buffer == NULL) {
3377             /* surface has no buffer    */
3378             uifw_debug("win_mgr_change_mapsurface: surface(%08x) has no buffer",
3379                        sm->usurf->surfaceid);
3380             if (sm->initflag)   {
3381                 event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP;
3382             }
3383             else    {
3384                 event = 0;
3385             }
3386         }
3387     }
3388     else if (sm->uclient->shmbuf == NULL)   {
3389         /* no GPU acceleration but no buffer    */
3390         uifw_debug("win_mgr_change_mapsurface: client has no shared memory buffer");
3391         if (sm->initflag)   {
3392             event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP;
3393         }
3394         else    {
3395             event = 0;
3396         }
3397     }
3398     else if (sm->type == ICO_WINDOW_MGR_MAP_TYPE_SHM)   {
3399         if (es->buffer_ref.buffer != NULL)  {
3400             shm_buffer = wl_shm_buffer_get(es->buffer_ref.buffer->resource);
3401             if (shm_buffer) {
3402                 format = wl_shm_buffer_get_format(shm_buffer);
3403                 if (format != WL_SHM_FORMAT_ARGB8888)   {
3404                     uifw_trace("win_mgr_change_mapsurface: %08x shm_buffer type %x",
3405                                sm->usurf->surfaceid, format);
3406                     event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP;
3407                 }
3408             }
3409         }
3410     }
3411
3412     if ((event != 0) && (event != ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP))  {
3413
3414         if (sm->type == ICO_WINDOW_MGR_MAP_TYPE_EGL)    {
3415             gl_state = (struct uifw_gl_surface_state *)es->renderer_state;
3416             if (gl_state->buffer_type == BUFFER_TYPE_SHM)   {
3417                 event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR;
3418             }
3419             else if (gl_state->buffer_type != BUFFER_TYPE_EGL)   {
3420                 event = 0;
3421             }
3422             else    {
3423                 dri_image = (struct uifw_dri_image *)drm_buffer->driver_buffer;
3424                 dri_region = dri_image->region;
3425                 width = es->buffer_ref.buffer->width;
3426                 height = es->buffer_ref.buffer->height;
3427                 stride = drm_buffer->stride[0];
3428                 if (drm_buffer->format == __DRI_IMAGE_FOURCC_XRGB8888)  {
3429                     format = EGL_TEXTURE_RGB;
3430                 }
3431                 else if (drm_buffer->format == __DRI_IMAGE_FOURCC_ARGB8888) {
3432                     format = EGL_TEXTURE_RGBA;
3433                 }
3434                 else    {
3435                     /* unknown format, error    */
3436                     format = EGL_NO_TEXTURE;
3437                 }
3438                 eglname = dri_region->name;
3439                 if (eglname == 0)   {
3440                     if (drm_intel_bo_flink((drm_intel_bo *)dri_region->bo, &eglname))   {
3441                         uifw_warn("win_mgr_change_mapsurface: drm_intel_bo_flink() Error");
3442                         eglname = 0;
3443                     }
3444                 }
3445                 if ((sm->initflag == 0) && (eglname != 0) &&
3446                     (width > 0) && (height > 0) && (stride > 0))    {
3447                     sm->initflag = 1;
3448                     event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP;
3449                 }
3450                 else    {
3451                     if ((eglname == 0) || (width <= 0) || (height <= 0) || (stride <= 0))   {
3452                         event = 0;
3453                     }
3454                     else if (event == ICO_WINDOW_MGR_MAP_SURFACE_EVENT_CONTENTS)    {
3455                         if ((sm->width != width) || (sm->height != height) ||
3456                             (sm->stride != stride) || (format != sm->format))   {
3457                             event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_RESIZE;
3458                         }
3459                         else if (eglname != sm->eglname)    {
3460 #if  PERFORMANCE_EVALUATIONS > 0
3461                             uifw_perf("SWAP_BUFFER appid=%s surface=%08x name=%d",
3462                                       sm->usurf->uclient->appid, sm->usurf->surfaceid,
3463                                       sm->eglname);
3464 #endif /*PERFORMANCE_EVALUATIONS*/
3465                             dtime = curtime - sm->lasttime;
3466                             if ((sm->interval > 0) && (dtime < sm->interval))   {
3467                                 sm->eventque = 1;
3468                                 event = 0;
3469                             }
3470                         }
3471                         else if (sm->eventque)  {
3472                             dtime = curtime - sm->lasttime;
3473                             if ((sm->interval > 0) && (dtime < sm->interval))   {
3474                                 event = 0;
3475                             }
3476                         }
3477                         else    {
3478                             event =0;
3479                         }
3480                     }
3481                 }
3482                 sm->width = width;
3483                 sm->height = height;
3484                 sm->stride = stride;
3485                 sm->eglname = eglname;
3486                 sm->format = format;
3487             }
3488         }
3489         else    {
3490             if ((sm->type != ICO_WINDOW_MGR_MAP_TYPE_PIXEL) || (sm->eventque != 0) ||
3491                 (es->buffer_ref.buffer == NULL) || (es->buffer_ref.buffer != sm->curbuf)) {
3492                 sm->curbuf = es->buffer_ref.buffer;
3493                 if (es->buffer_ref.buffer != NULL)  {
3494                     width = es->buffer_ref.buffer->width;
3495                     height = es->buffer_ref.buffer->height;
3496                 }
3497                 else    {
3498                     width = sm->usurf->client_width;
3499                     height = sm->usurf->client_height;
3500                 }
3501                 /* get shared memory buffer area    */
3502                 idx2 = sm->uclient->bufnum;
3503                 settime2 = 0x7fffffff;
3504                 for (idx = 0; idx < sm->uclient->bufnum; idx++) {
3505                     p = (struct ico_uifw_image_buffer *)
3506                         (((char *)sm->uclient->shmbuf) + idx * sm->uclient->bufsize);
3507                     if (p->settime == p->reftime)   break;
3508                     delta = curtime - p->settime;
3509                     if (delta < 0)  {
3510                         delta = delta + 0x80000000;
3511                     }
3512                     if ((delta > 3000) && (delta < settime2))   {
3513                         idx2 = idx;
3514                         settime2 = p->settime;
3515                     }
3516                 }
3517                 uifw_detail("win_mgr_change_mapsurface: PIX %08x idx=%d idx2=%d w/h=%d/%d",
3518                             sm->usurf->surfaceid, idx, idx2, width, height);
3519                 if (idx >= sm->uclient->bufnum) {
3520                     idx = idx2;
3521                 }
3522                 if (idx >= sm->uclient->bufnum) {
3523                     uifw_debug("win_mgr_change_mapsurface: shared buffer full");
3524                     event = 0;
3525                     sm->curbuf = NULL;
3526                     sm->eventque = 1;
3527                 }
3528                 else if ((sm->initflag == 0) && (width > 0) && (height > 0))    {
3529                     sm->initflag = 1;
3530                     event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP;
3531                     uifw_detail("win_mgr_change_mapsurface: PIX MAP event %08x",
3532                                 sm->usurf->surfaceid);
3533                 }
3534                 else    {
3535                     if ((width <= 0) || (height <= 0))  {
3536                         event = 0;
3537                         sm->curbuf = NULL;
3538                         uifw_detail("win_mgr_change_mapsurface: PIX %08x w/h=0/0",
3539                                     sm->usurf->surfaceid);
3540                     }
3541                     else if (event == ICO_WINDOW_MGR_MAP_SURFACE_EVENT_CONTENTS)    {
3542 #if  PERFORMANCE_EVALUATIONS > 0
3543                         if (sm->type != ICO_WINDOW_MGR_MAP_TYPE_SHM)    {
3544                             uifw_perf("SWAP_BUFFER appid=%s surface=%08x",
3545                                       sm->usurf->uclient->appid, sm->usurf->surfaceid);
3546                         }
3547 #endif /*PERFORMANCE_EVALUATIONS*/
3548                         if ((sm->width != width) || (sm->height != height)) {
3549                             event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_RESIZE;
3550                         }
3551                         else    {
3552                             dtime = curtime - sm->lasttime;
3553                             if ((sm->interval > 0) && (dtime < sm->interval))   {
3554                                 sm->eventque = 1;
3555                                 event = 0;
3556                                 uifw_detail("win_mgr_change_mapsurface: PIX %08x new queue",
3557                                             sm->usurf->surfaceid);
3558                             }
3559                             else if (sm->eventque)  {
3560                                 dtime = curtime - sm->lasttime;
3561                                 if ((sm->interval > 0) && (dtime < sm->interval))   {
3562                                     event = 0;
3563                                     uifw_detail("win_mgr_change_mapsurface: PIX %08x queued",
3564                                                 sm->usurf->surfaceid);
3565                                 }
3566                             }
3567                         }
3568                     }
3569                 }
3570                 sm->width = width;
3571                 sm->height = height;
3572                 sm->stride = width * 4;
3573                 sm->format = EGL_TEXTURE_RGBA;
3574                 if (event != 0) {
3575                     /* read pixel           */
3576                     p = (struct ico_uifw_image_buffer *)
3577                         (((char *)sm->uclient->shmbuf) + idx * sm->uclient->bufsize);
3578                     height = (sm->uclient->bufsize - sizeof(struct ico_uifw_image_buffer))
3579                              / (width * 4);
3580                     uifw_detail("win_mgr_change_mapsurface: PIX read buf=%08x height=%d(%d)",
3581                                 (int)p, height, sm->height);
3582                     if ((height < sm->height) &&
3583                         (sm->type == ICO_WINDOW_MGR_MAP_TYPE_SHM))  {
3584                         uifw_warn("win_mgr_change_mapsurface: Buffer SHM, "
3585                                   "but buffer overflow(%d>%d)", sm->height, height);
3586                         event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR;
3587                         sm->eventque = 0;
3588                     }
3589                     else    {
3590                         ev = ico_ivi_get_primary_view(sm->usurf);
3591                         if ((ev == NULL) || (ev->output == NULL))   {
3592                             uifw_debug("win_mgr_change_mapsurface: surface %08x has no view",
3593                                        sm->usurf->surfaceid);
3594                             event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR;
3595                         }
3596                         else    {
3597                             if (height > sm->height)    {
3598                                 height = sm->height;
3599                             }
3600                             if (ico_read_surface_pixels(es, PIXMAN_a8r8g8b8, p->image,
3601                                                         0, 0, sm->width, height) != 0)  {
3602                                 uifw_debug("win_mgr_change_mapsurface: Error read pixel %s.%08x",
3603                                            sm->usurf->uclient->appid, sm->usurf->surfaceid);
3604                                 event = ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR;
3605                                 sm->eventque = 0;
3606                             }
3607                             else    {
3608                                 uifw_detail("win_mgr_change_mapsurface: PIX read pixels(%d)",
3609                                             idx+1);
3610                                 p->surfaceid = sm->usurf->surfaceid;
3611                                 p->settime = curtime;
3612                                 p->width = sm->width;
3613                                 p->height = height;
3614                                 sm->eglname = idx + 1;
3615                             }
3616                         }
3617                     }
3618                 }
3619             }
3620             else    {
3621                 event = 0;
3622             }
3623         }
3624     }
3625
3626     if (event != 0) {
3627         uifw_detail("win_mgr_change_mapsurface: send MAP event(ev=%d surf=%08x type=%d "
3628                     "name=%d w/h/s=%d/%d/%d format=%x",
3629                     event, sm->usurf->surfaceid, sm->type, sm->eglname,
3630                     sm->width, sm->height, sm->stride, sm->format);
3631         sm->lasttime = curtime;
3632         sm->eventque = 0;
3633         ico_window_mgr_send_map_surface(sm->uclient->mgr->resource, event,
3634                                         sm->usurf->surfaceid, sm->type, sm->eglname,
3635                                         sm->width, sm->height, sm->stride, sm->format);
3636         if (event == ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR)    {
3637             /* free map table if error  */
3638             wl_list_remove(&sm->surf_link);
3639             wl_list_remove(&sm->map_link);
3640             sm->usurf = (struct uifw_win_surface *)_ico_win_mgr->free_maptable;
3641             _ico_win_mgr->free_maptable = sm;
3642         }
3643     }
3644 }
3645
3646 /*--------------------------------------------------------------------------*/
3647 /**
3648  * @brief   uifw_set_map_buffer: set map surface image buffer
3649  *
3650  * @param[in]   client      Weyland client
3651  * @param[in]   resource    resource of request
3652  * @param[in]   shmname     shared memory name(POSIX I/F)
3653  * @param[in]   bufsize     buffer size in byte
3654  * @param[in]   bufnum      number of buffers
3655  * @return      none
3656  */
3657 /*--------------------------------------------------------------------------*/
3658 static void
3659 uifw_set_map_buffer(struct wl_client *client, struct wl_resource *resource,
3660                     const char *shmname, uint32_t bufsize, uint32_t bufnum)
3661 {
3662     struct uifw_client              *uclient;
3663     struct ico_uifw_image_buffer    *p;
3664     char    *shmbuf;
3665     int     shmfd;
3666     int     i;
3667
3668     uifw_trace("uifw_set_map_buffer: Enter(%s,%d,%d)",
3669                shmname ? shmname : "(null)", bufsize, bufnum);
3670
3671     /* check for access control         */
3672     uclient = ico_window_mgr_find_uclient(client);
3673     if (! uclient)  {
3674         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3675                                "ico_window_mgr_set_map_buffer: unknown client");
3676         uifw_trace("uifw_set_map_buffer: Leave(unknown client=%08x)", (int)client);
3677         return;
3678     }
3679     if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_SET_MAP_BUFFER) == 0)    {
3680         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3681                                "ico_window_mgr_set_map_buffer: not permitted");
3682         uifw_trace("uifw_set_map_buffer: Leave(%s not permitted)", uclient->appid);
3683         return;
3684     }
3685
3686     if ((shmname == NULL) || (*shmname == 0) || (*shmname == ' ') ||
3687         (bufsize == 0) || (bufnum == 0))    {
3688         /* delete shared memory buffer          */
3689         if (uclient->shmbuf)    {
3690             munmap(uclient->shmbuf, uclient->bufsize * uclient->bufnum);
3691             uclient->shmbuf = NULL;
3692         }
3693         uifw_trace("uifw_set_map_buffer: Leave(delete shared memory buffer)");
3694         return;
3695     }
3696
3697     shmfd = shm_open(shmname, O_RDWR, 0600);
3698     if (shmfd == -1)    {
3699         /* shared memory dose not exist         */
3700         uifw_trace("uifw_set_map_buffer: Leave(shared memory(%s) dose not exist)", shmname);
3701         return;
3702     }
3703
3704     shmbuf = (char *)mmap(NULL, bufsize * bufnum, PROT_READ|PROT_WRITE, MAP_SHARED, shmfd, 0);
3705     close(shmfd);
3706
3707     if (shmbuf == NULL) {
3708         /* can not map shared memory            */
3709         uifw_trace("uifw_set_map_buffer: Leave(can not map shared memory(%s))", shmname);
3710         return;
3711     }
3712     if (uclient->shmbuf)    {
3713         munmap(uclient->shmbuf, uclient->bufsize * uclient->bufnum);
3714     }
3715
3716     uclient->shmbuf = shmbuf;
3717     uclient->bufsize = bufsize;
3718     uclient->bufnum = bufnum;
3719     for (i = 0; i < (int)bufnum; i++)    {
3720         p = (struct ico_uifw_image_buffer *)(((char *)shmbuf) + bufsize * i);
3721         memset((char *)p, 0, sizeof(struct ico_uifw_image_buffer));
3722         memcpy((char *)&p->magich, ICO_UIFW_IMAGE_BUFFER_MAGICH, 4);
3723         memcpy((char *)&p->magict, ICO_UIFW_IMAGE_BUFFER_MAGICT, 4);
3724     }
3725     uifw_trace("uifw_set_map_buffer: Leave(shm addr=%08x)", (int)uclient->shmbuf);
3726 }
3727
3728 /*--------------------------------------------------------------------------*/
3729 /**
3730  * @brief   uifw_map_surface: mapped surface buffer to system application
3731  *
3732  * @param[in]   client      Weyland client
3733  * @param[in]   resource    resource of request
3734  * @param[in]   surfaceid   surface id
3735  * @param[in]   framerate   frame rate of surface update(frame/sec)
3736  * @return      none
3737  */
3738 /*--------------------------------------------------------------------------*/
3739 static void
3740 uifw_map_surface(struct wl_client *client, struct wl_resource *resource,
3741                  uint32_t surfaceid, int32_t framerate)
3742 {
3743     struct uifw_win_surface     *usurf;
3744     struct weston_surface       *es;
3745     struct uifw_surface_map     *sm;
3746     struct weston_buffer        *buffer;
3747     struct wl_shm_buffer        *shm_buffer;
3748     struct uifw_client          *uclient;
3749     struct uifw_drm_buffer      *drm_buffer;
3750     struct uifw_gl_surface_state *gl_state;
3751     int     maptype;
3752     int     format;
3753
3754     uifw_trace("uifw_map_surface: Enter(surface=%08x,fps=%d)", surfaceid, framerate);
3755
3756     /* check for access control         */
3757     uclient = ico_window_mgr_find_uclient(client);
3758     if (resource != NULL)   {
3759         /* resource is NULL, internal use   */
3760         if (! uclient)  {
3761             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3762                                    "ico_window_mgr_map_surface: unknown client");
3763             uifw_trace("uifw_map_surface: Leave(unknown client=%08x)", (int)client);
3764             return;
3765         }
3766         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_MAP_SURFACE) == 0)   {
3767             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3768                                    "ico_window_mgr_map_surface: not permitted");
3769             uifw_trace("uifw_map_surface: Leave(%s not permitted)", uclient->appid);
3770             return;
3771         }
3772     }
3773     if (! uclient)  {
3774         uifw_trace("uifw_map_surface: Leave(unknown client=%08x)", (int)client);
3775         return;
3776     }
3777
3778     usurf = ico_window_mgr_get_usurf(surfaceid);
3779     if (! usurf)    {
3780         /* surface dose not exist, error        */
3781         ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
3782                                         surfaceid, 1, 0, 0, 0, 0, 0);
3783         uifw_trace("uifw_map_surface: Leave(surface=%08x dose not exist)", surfaceid);
3784         return;
3785     }
3786     if (usurf->layertype == LAYER_TYPE_INPUTPANEL)  {
3787         /* input panel surface dose not suport, error   */
3788         ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
3789                                         surfaceid, 1, 0, 0, 0, 0, 0);
3790         uifw_trace("uifw_map_surface: Leave(surface=%08x is input panel)", surfaceid);
3791         return;
3792     }
3793
3794     /* check if buffered        */
3795     es = usurf->surface;
3796     if (es == NULL) {
3797         /* surface has no buffer, error         */
3798         ico_window_mgr_send_map_surface(resource, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
3799                                         surfaceid, 3, 0, 0, 0, 0, 0);
3800         uifw_trace("uifw_map_surface: Leave(surface(%08x) has no surface)", surfaceid);
3801         return;
3802     }
3803     buffer = es->buffer_ref.buffer;
3804
3805     /* check buffer type        */
3806     gl_state = (struct uifw_gl_surface_state *)es->renderer_state;
3807     if ((_ico_ivi_gpu_type == ICO_GPUTYPE_NOACCELERATION) ||
3808         (gl_state == NULL) || (gl_state->buffer_type == BUFFER_TYPE_SHM))   {
3809         /* No Acceleration or wl_shm_buffer support ReadPixels  */
3810         if ((gl_state != NULL) && (gl_state->buffer_type == BUFFER_TYPE_SHM))   {
3811             maptype = -1;
3812             format = 0xff;
3813             if (ico_ivi_optionflag() & ICO_IVI_OPTION_SUPPORT_SHM)  {
3814                 if (buffer != NULL) {
3815                     shm_buffer = wl_shm_buffer_get(buffer->resource);
3816                     if (shm_buffer) {
3817                         format = wl_shm_buffer_get_format(shm_buffer);
3818                         uifw_detail("uifw_map_surface: %08x shm_buffer type %x",
3819                                     surfaceid, format);
3820                         if (format == WL_SHM_FORMAT_ARGB8888)   {
3821                             maptype = ICO_WINDOW_MGR_MAP_TYPE_SHM;
3822                         }
3823                     }
3824                 }
3825                 else    {
3826                     maptype = ICO_WINDOW_MGR_MAP_TYPE_SHM;
3827                 }
3828             }
3829             if (maptype < 0)    {
3830                 ico_window_mgr_send_map_surface(resource,
3831                                                 ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
3832                                                 surfaceid, 5, 0, 0, 0, 0, 0);
3833                 uifw_trace("uifw_map_surface: Leave(surface(%08x) not support shm_buffer(%x))",
3834                            surfaceid, format);
3835                 return;
3836             }
3837         }
3838         else    {
3839             maptype = ICO_WINDOW_MGR_MAP_TYPE_PIXEL;
3840         }
3841     }
3842     else    {
3843         /* H/W(GPU) driver EGL buffer (Intel only)  */
3844         maptype = ICO_WINDOW_MGR_MAP_TYPE_EGL;
3845     }
3846
3847     /* maximum framerate        */
3848     if (maptype == ICO_WINDOW_MGR_MAP_TYPE_EGL) {
3849         if ((framerate <= 0) || (framerate > _ico_ivi_map_framerate_gpu))
3850             framerate = _ico_ivi_map_framerate_gpu;
3851     }
3852     else if (maptype == ICO_WINDOW_MGR_MAP_TYPE_SHM) {
3853         if ((framerate <= 0) || (framerate > _ico_ivi_map_framerate_shm))
3854             framerate = _ico_ivi_map_framerate_shm;
3855     }
3856     else    {
3857         if ((framerate <= 0) || (framerate > _ico_ivi_map_framerate_pixel))
3858             framerate = _ico_ivi_map_framerate_pixel;
3859     }
3860
3861     /* check same surface       */
3862     wl_list_for_each(sm, &usurf->surf_map, surf_link) {
3863         if ((sm->usurf == usurf) && (sm->uclient == uclient))   {
3864             break;
3865         }
3866     }
3867
3868     if (&sm->surf_link == &usurf->surf_map) {
3869         /* create map table         */
3870         sm = _ico_win_mgr->free_maptable;
3871         if (sm) {
3872             _ico_win_mgr->free_maptable = (struct uifw_surface_map *)sm->usurf;
3873         }
3874         else    {
3875             sm = (struct uifw_surface_map *)malloc(sizeof(struct uifw_surface_map));
3876             if (! sm)   {
3877                 ico_window_mgr_send_map_surface(resource,
3878                                                 ICO_WINDOW_MGR_MAP_SURFACE_EVENT_ERROR,
3879                                                 surfaceid, 6, 0, 0, 0, 0, 0);
3880                 uifw_trace("uifw_map_surface: Leave(malloc error)");
3881                 return;
3882             }
3883         }
3884         memset(sm, 0, sizeof(struct uifw_surface_map));
3885
3886         wl_list_init(&sm->map_link);
3887         wl_list_init(&sm->surf_link);
3888         sm->usurf = usurf;
3889         sm->uclient = uclient;
3890         sm->type = maptype;
3891         sm->framerate = framerate;
3892         sm->interval = (1000 / sm->framerate) - 1;
3893         wl_list_insert(_ico_win_mgr->map_list.next, &sm->map_link);
3894         wl_list_insert(usurf->surf_map.prev, &sm->surf_link);
3895     }
3896     else    {
3897         /* change frame rate    */
3898         uifw_trace("uifw_map_surface: Leave(chagne frame rate %d->%d",
3899                    sm->framerate, framerate);
3900         if (sm->framerate != framerate) {
3901             sm->framerate = framerate;
3902             sm->interval = (1000 / sm->framerate) - 1;
3903             win_mgr_change_mapsurface(sm, 0, weston_compositor_get_time());
3904         }
3905         return;
3906     }
3907
3908     if (buffer != NULL) {
3909         sm->width = buffer->width;
3910         sm->height = buffer->height;
3911         if (maptype != ICO_WINDOW_MGR_MAP_TYPE_EGL) {
3912             sm->stride = sm->width * 4;
3913             sm->format = EGL_TEXTURE_RGBA;
3914             if ((sm->width > 0) && (sm->height > 0))    {
3915                 sm->initflag = 1;
3916             }
3917             uifw_debug("uifw_map_surface: map type=%d,surface=%08x,fps=%d,w/h=%d/%d",
3918                        maptype, surfaceid, framerate, buffer->width, buffer->height);
3919         }
3920         else    {
3921             drm_buffer = (struct uifw_drm_buffer *)wl_resource_get_user_data(
3922                                             (struct wl_resource *)buffer->legacy_buffer);
3923             if (drm_buffer != NULL) {
3924                 sm->stride = drm_buffer->stride[0];
3925                 if (drm_buffer->format == __DRI_IMAGE_FOURCC_XRGB8888)  {
3926                     sm->format = EGL_TEXTURE_RGB;
3927                 }
3928                 else if (drm_buffer->format == __DRI_IMAGE_FOURCC_ARGB8888) {
3929                     sm->format = EGL_TEXTURE_RGBA;
3930                 }
3931                 else    {
3932                     /* unknown format, error    */
3933                     sm->format = EGL_NO_TEXTURE;
3934                 }
3935                 if ((sm->width > 0) && (sm->height > 0) && (sm->stride > 0) &&
3936                     (gl_state != NULL))  {
3937                     sm->initflag = 1;
3938                 }
3939                 uifw_debug("uifw_map_surface: map EGL surface=%08x,fps=%d,w/h=%d/%d",
3940                            surfaceid, framerate, buffer->width, buffer->height);
3941             }
3942             else    {
3943                 uifw_debug("uifw_map_surface: map EGL but no buffer surface=%08x,fps=%d",
3944                            surfaceid, framerate);
3945             }
3946         }
3947     }
3948     else if (maptype != ICO_WINDOW_MGR_MAP_TYPE_EGL)    {
3949         sm->width = usurf->client_width;
3950         sm->height = usurf->client_height;
3951         sm->stride = sm->width * 4;
3952         sm->format = EGL_TEXTURE_RGBA;
3953         if ((sm->width > 0) && (sm->height > 0))    {
3954             sm->initflag = 1;
3955         }
3956         uifw_debug("uifw_map_surface: map type=%d,surface=%08x,fps=%d,w/h=%d/%d",
3957                    maptype, surfaceid, framerate, sm->width, sm->height);
3958     }
3959     else    {
3960         uifw_debug("uifw_map_surface: map EGL but no buffer surface=%08x,fps=%d",
3961                    surfaceid, framerate);
3962     }
3963
3964     /* send map event                       */
3965     if (sm->initflag)   {
3966         win_mgr_change_mapsurface(sm, ICO_WINDOW_MGR_MAP_SURFACE_EVENT_MAP,
3967                                   weston_compositor_get_time());
3968     }
3969     uifw_trace("uifw_map_surface: Leave");
3970 }
3971
3972 /*--------------------------------------------------------------------------*/
3973 /**
3974  * @brief   uifw_unmap_surface: unmap surface buffer
3975  *
3976  * @param[in]   client      Weyland client
3977  * @param[in]   resource    resource of request
3978  * @param[in]   surfaceid   surface id
3979  * @return      none
3980  */
3981 /*--------------------------------------------------------------------------*/
3982 static void
3983 uifw_unmap_surface(struct wl_client *client, struct wl_resource *resource,
3984                    uint32_t surfaceid)
3985 {
3986     struct uifw_win_surface *usurf;
3987     struct uifw_surface_map *sm, *sm_tmp;
3988     struct uifw_client      *uclient;
3989     struct ico_uifw_image_buffer    *p;
3990     int     idx;
3991
3992     uifw_trace("uifw_unmap_surface: Enter(surface=%08x)", surfaceid);
3993
3994     /* check for access control         */
3995     if (resource != NULL)   {
3996         /* resource is NULL, internal use   */
3997         uclient = ico_window_mgr_find_uclient(client);
3998         if (! uclient)  {
3999             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4000                                    "ico_window_mgr_unmap_surface: unknown client");
4001             uifw_trace("uifw_unmap_surface: Leave(unknown client=%08x)", (int)client);
4002             return;
4003         }
4004         if ((uclient->api_access_control & ICO_UIFW_WINDOW_MGR_UNMAP_SURFACE) == 0) {
4005             wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4006                                    "ico_window_mgr_unmap_surface: not permitted");
4007             uifw_trace("uifw_unmap_surface: Leave(%s not permitted)", uclient->appid);
4008             return;
4009         }
4010     }
4011     usurf = ico_window_mgr_get_usurf(surfaceid);
4012     if (! usurf)    {
4013         /* surface dose not exist, error        */
4014         uifw_trace("uifw_unmap_surface: Leave(surface=%08x dose not exist)", surfaceid);
4015         return;
4016     }
4017     if (client) {
4018         uclient = ico_window_mgr_find_uclient(client);
4019         if ((! uclient) || (! uclient->mgr))    {
4020             /* client dose not exist, error         */
4021             uifw_trace("uifw_unmap_surface: Leave(client=%08x dose not exist)", (int)client);
4022             return;
4023         }
4024     }
4025     else    {
4026         uclient = NULL;
4027         wl_list_for_each (sm, &usurf->surf_map, surf_link) {
4028             if (sm->uclient->mgr != NULL) {
4029                 uifw_trace("uifw_unmap_surface: send UNMAP event(ev=%d surf=%08x name=%08x "
4030                            "w/h/s=%d/%d/%d format=%x",
4031                            ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP, surfaceid,
4032                            sm->eglname, sm->width, sm->height, sm->stride, sm->format);
4033                 ico_window_mgr_send_map_surface(sm->uclient->mgr->resource,
4034                                                 ICO_WINDOW_MGR_MAP_SURFACE_EVENT_UNMAP,
4035                                                 surfaceid, sm->type, sm->eglname, sm->width,
4036                                                 sm->height, sm->stride, sm->format);
4037             }
4038         }
4039     }
4040
4041     wl_list_for_each_safe (sm, sm_tmp, &usurf->surf_map, surf_link) {
4042         if (((uclient != NULL) && (sm->uclient != uclient)))   continue;
4043
4044         if ((sm->type != ICO_WINDOW_MGR_MAP_TYPE_EGL) &&
4045             (sm->uclient->shmbuf != NULL))  {
4046             /* reset shared memory buffer   */
4047             for (idx = 0; idx < sm->uclient->bufnum; idx++) {
4048                 p = (struct ico_uifw_image_buffer *)
4049                     (((char *)sm->uclient->shmbuf) + idx * sm->uclient->bufsize);
4050                 if (p->surfaceid == surfaceid)  {
4051                     p->surfaceid = 0;
4052                     p->settime = 0;
4053                     p->reftime = 0;
4054                 }
4055             }
4056         }
4057         wl_list_remove(&sm->surf_link);
4058         wl_list_remove(&sm->map_link);
4059         sm->usurf = (struct uifw_win_surface *)_ico_win_mgr->free_maptable;
4060         _ico_win_mgr->free_maptable = sm;
4061     }
4062     uifw_trace("uifw_unmap_surface: Leave");
4063 }
4064
4065 /*--------------------------------------------------------------------------*/
4066 /**
4067  * @brief   win_mgr_surface_change_mgr: surface chagen from manager(HomeScreen)
4068  *
4069  * @param[in]   surface     Weston surface
4070  * @param[in]   x           X coordinate on screen
4071  * @param[in]   y           Y coordinate on screen
4072  * @param[in]   width       surface width
4073  * @param[in]   height      surface height
4074  * @return      number of managers
4075  * @retval      > 0         number of managers
4076  * @retval      0           manager not exist
4077  */
4078 /*--------------------------------------------------------------------------*/
4079 static int
4080 win_mgr_surface_change_mgr(struct weston_surface *surface,
4081                            const int x, const int y, const int width, const int height)
4082 {
4083     int     num_mgr;
4084     struct uifw_win_surface *usurf;
4085
4086     uifw_trace("win_mgr_surface_change_mgr: Enter(%08x,x/y=%d/%d,w/h=%d/%d)",
4087                (int)surface, x, y, width, height);
4088
4089     usurf = find_uifw_win_surface_by_ws(surface);
4090     if (! usurf) {
4091         uifw_trace("win_mgr_surface_change_mgr: Leave(Not Exist)");
4092         return 0;
4093     }
4094
4095     num_mgr = ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
4096                                       usurf, x, y, width, height, 1);
4097
4098     uifw_trace("win_mgr_surface_change_mgr: Leave(%d)", num_mgr);
4099     return num_mgr;
4100 }
4101
4102 /*--------------------------------------------------------------------------*/
4103 /**
4104  * @brief   win_mgr_change_surface: surface change
4105  *
4106  * @param[in]   surface     Weston surface
4107  * @param[in]   to          destination(0=Client&Manager,1=Client,-1=Manager)
4108  * @param[in]   manager     request from manager(0=Client,1=Manager)
4109  * @return      none
4110  */
4111 /*--------------------------------------------------------------------------*/
4112 static void
4113 win_mgr_change_surface(struct weston_surface *surface, const int to, const int manager)
4114 {
4115     struct uifw_win_surface *usurf;
4116     struct weston_surface   *es;
4117     struct weston_view      *ev;
4118     int     x;
4119     int     y;
4120     int     repaint = 0;
4121
4122     uifw_trace("win_mgr_change_surface: Enter(%08x,%d,%d)", (int)surface, to, manager);
4123
4124     /* Find surface         */
4125     usurf = find_uifw_win_surface_by_ws(surface);
4126     if (! usurf) {
4127         uifw_trace("win_mgr_change_surface: Leave(Not Exist)");
4128         return;
4129     }
4130     es = usurf->surface;
4131     if (! es)   {
4132         uifw_trace("win_mgr_change_surface: Leave(No weston surface)");
4133         return;
4134     }
4135
4136     /* set surface size     */
4137     ev = ico_ivi_get_primary_view(usurf);
4138     uifw_debug("win_mgr_change_surface: set surface x/y=%d/%d=>%d/%d w/h=%d/%d=>%d/%d",
4139                (int)ev->geometry.x, (int)ev->geometry.y, usurf->x, usurf->y,
4140                es->width, es->height, usurf->width, usurf->height);
4141     if ((usurf->width <= 0) || (usurf->height <= 0))    {
4142         usurf->width = es->width;
4143         usurf->height = es->height;
4144     }
4145     win_mgr_set_scale(usurf);
4146     if (usurf->visible) {
4147         weston_view_set_position(ev, (float)(usurf->node_tbl->disp_x +
4148                                              usurf->x + usurf->xadd),
4149                                      (float)(usurf->node_tbl->disp_y +
4150                                              usurf->y + usurf->yadd));
4151         ico_window_mgr_restack_layer(usurf);
4152     }
4153     else    {
4154         weston_view_set_position(ev, (float)(ICO_IVI_MAX_COORDINATE+1),
4155                                      (float)(ICO_IVI_MAX_COORDINATE+1));
4156     }
4157
4158     /* send wayland event to client     */
4159     if ((to >= 0) && (usurf->shsurf != NULL) && (manager !=0) &&
4160         (usurf->width > 0) && (usurf->height > 0))  {
4161         if ((usurf->width != usurf->conf_width) ||
4162             (usurf->height != usurf->conf_height))  {
4163             usurf->conf_width = usurf->width;
4164             usurf->conf_height = usurf->height;
4165             uifw_trace("win_mgr_change_surface: SURFACE_CONFIGURE %08x(%08x),w/h=%d/%d ",
4166                        usurf->surfaceid, (int)es, usurf->width, usurf->height);
4167             ico_ivi_shell_send_configure(es,
4168                                          WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT,
4169                                          usurf->width, usurf->height);
4170         }
4171     }
4172
4173     if (usurf->visible) {
4174         x = usurf->node_tbl->disp_x + usurf->x;
4175         y = usurf->node_tbl->disp_y + usurf->y;
4176     }
4177     else    {
4178         x = ICO_IVI_MAX_COORDINATE+1;
4179         y = ICO_IVI_MAX_COORDINATE+1;
4180     }
4181     /* change geometry if request from manager  */
4182     if (manager)    {
4183         if ((usurf->width != es->width) ||
4184             (usurf->height != es->height) ||
4185             ((int)ev->geometry.x != x) || ((int)ev->geometry.y != y))   {
4186             win_mgr_surface_configure(usurf, (float)x, (float)y, usurf->width, usurf->height);
4187             repaint ++;
4188         }
4189     }
4190
4191     /* send manager event to HomeScreen */
4192     if (to <= 0)    {
4193         if (manager)    {
4194             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
4195                                     usurf, usurf->x, usurf->y,
4196                                     usurf->width, usurf->height, 0);
4197         }
4198         else    {
4199             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
4200                                     usurf, (int)ev->geometry.x, (int)ev->geometry.y,
4201                                     es->width, es->height, 1);
4202         }
4203     }
4204
4205     /* change geometry if request from client   */
4206     if (! manager)  {
4207         if ((usurf->width != es->width) || (usurf->height != es->height) ||
4208             ((int)ev->geometry.x != x) || ((int)ev->geometry.y != y))   {
4209             win_mgr_surface_configure(usurf, x, y, usurf->width, usurf->height);
4210             repaint ++;
4211         }
4212     }
4213
4214     if (repaint)    {
4215         uifw_trace("win_mgr_change_surface: repaint");
4216         weston_compositor_schedule_repaint(_ico_win_mgr->compositor);
4217     }
4218     uifw_trace("win_mgr_change_surface: Leave(OK)");
4219 }
4220
4221 /*--------------------------------------------------------------------------*/
4222 /**
4223  * @brief   win_mgr_surface_configure: UIFW surface configure
4224  *
4225  * @param[in]   usurf       UIFW surface
4226  * @param[in]   x           X coordinate on screen
4227  * @param[in]   y           Y coordinate on screen
4228  * @param[in]   width       surface width
4229  * @param[in]   height      surface height
4230  * @return      none
4231  */
4232 /*--------------------------------------------------------------------------*/
4233 static void
4234 win_mgr_surface_configure(struct uifw_win_surface *usurf,
4235                           int x, int y, int width, int height)
4236 {
4237     struct weston_surface   *es;
4238     struct weston_view      *ev;
4239
4240     es = usurf->surface;
4241     if ((es != NULL) && (es->buffer_ref.buffer))  {
4242         if (usurf->client_width == 0)   {
4243             usurf->client_width = es->width;
4244             if (usurf->client_width == 0)
4245                 usurf->client_width = ico_ivi_surface_buffer_width(es);
4246         }
4247         if (usurf->client_height == 0)  {
4248             usurf->client_height = es->height;
4249             if (usurf->client_height == 0)
4250                 usurf->client_height = ico_ivi_surface_buffer_height(es);
4251         }
4252
4253         /* not set geometry width/height    */
4254         win_mgr_set_scale(usurf);
4255         ev = ico_ivi_get_primary_view(usurf);
4256         weston_view_set_position(ev, x + usurf->xadd, y + usurf->yadd);
4257     }
4258 }
4259
4260 /*--------------------------------------------------------------------------*/
4261 /**
4262  * @brief   win_mgr_shell_configure: shell surface configure
4263  *
4264  * @param[in]   surface     Weston surface
4265  * @return      none
4266  */
4267 /*--------------------------------------------------------------------------*/
4268 static void
4269 win_mgr_shell_configure(struct weston_surface *surface)
4270 {
4271     struct uifw_win_surface *usurf;
4272     int     buf_width;
4273     int     buf_height;
4274
4275     uifw_trace("win_mgr_shell_configure: Enter(%08x)", (int)surface);
4276
4277     /* Find UIFW surface        */
4278     usurf = find_uifw_win_surface_by_ws(surface);
4279     if (! usurf) {
4280         uifw_trace("win_mgr_shell_configure: Leave(Not Exist)");
4281         return;
4282     }
4283
4284     usurf->client_width = surface->width;
4285     usurf->client_height = surface->height;
4286     ico_ivi_surface_buffer_size(surface, &buf_width, &buf_height);
4287     uifw_trace("win_mgr_shell_configure: %08x client w/h=%d/%d buf=%d/%d",
4288                usurf->surfaceid,
4289                usurf->client_width, usurf->client_height, buf_width, buf_height);
4290     if (usurf->client_width > buf_width)    usurf->client_width = buf_width;
4291     if (usurf->client_height > buf_height)  usurf->client_height = buf_height;
4292
4293     /* send event to manager    */
4294     win_mgr_change_surface(surface, -1, 0);
4295
4296     uifw_trace("win_mgr_shell_configure: Leave");
4297 }
4298
4299 /*--------------------------------------------------------------------------*/
4300 /**
4301  * @brief   win_mgr_select_surface: select surface by Bottun/Touch
4302  *
4303  * @param[in]   surface     Weston surface
4304  * @return      none
4305  */
4306 /*--------------------------------------------------------------------------*/
4307 static void
4308 win_mgr_select_surface(struct weston_surface *surface)
4309 {
4310     struct uifw_win_surface *usurf;
4311
4312     uifw_trace("win_mgr_select_surface: Enter(%08x)", (int)surface);
4313
4314     /* find surface         */
4315     usurf = find_uifw_win_surface_by_ws(surface);
4316     if (! usurf) {
4317         uifw_trace("win_mgr_select_surface: Leave(Not Exist)");
4318         return;
4319     }
4320     if (usurf != _ico_win_mgr->active_pointer_usurf)  {
4321
4322         /* send active event to manager     */
4323         if (ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
4324                                     usurf, ICO_WINDOW_MGR_ACTIVE_SELECTED, 0,0,0,0) <= 0) {
4325             uifw_trace("win_mgr_select_surface: not found manager, set active");
4326             win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER |
4327                                         ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
4328         }
4329     }
4330     uifw_trace("win_mgr_select_surface: Leave(OK)");
4331 }
4332
4333 /*--------------------------------------------------------------------------*/
4334 /**
4335  * @brief   win_mgr_set_title: set tile name to surface
4336  *
4337  * @param[in]   surface     weston surface
4338  * @param[in]   title       title name
4339  * @return      title string
4340  * @retval      != NULL     title string
4341  * @retval      NULL        no title string
4342  */
4343 /*--------------------------------------------------------------------------*/
4344 static char *
4345 win_mgr_set_title(struct weston_surface *surface, const char *title)
4346 {
4347     struct uifw_win_surface *usurf;
4348     char    *optpoint, *ppoint, *bpoint, *cpoint;
4349     int     layer, visible, raise, node, x, y, width, height, attribute;
4350     int     rx, ry, rw, rh;
4351     char    wktitle[ICO_IVI_WINNAME_LENGTH + 256];
4352
4353     uifw_trace("win_mgr_set_title: Enter(%08x) name=%s", (int)surface, title);
4354
4355     usurf = find_uifw_win_surface_by_ws(surface);
4356     if (! usurf) {
4357         uifw_trace("win_mgr_set_title: Leave(Not Exist)");
4358         return (char *)title;
4359     }
4360
4361     layer = -1;
4362     visible = -1;
4363     raise = -1;
4364     node = -1;
4365     x = -1;
4366     y = -1;
4367     width = -1;
4368     height = -1;
4369     attribute = -1;
4370
4371     strncpy(wktitle, title, ICO_IVI_WINNAME_LENGTH+256-1);
4372     wktitle[ICO_IVI_WINNAME_LENGTH+256-1] = 0;
4373
4374     /* get option parameters    */
4375     optpoint = strstr(wktitle, "@@");
4376
4377     if (optpoint != NULL)   {
4378         *optpoint = 0;
4379
4380         usurf->uclient->privilege = 1;  /* privilege application    */
4381         ppoint = optpoint + 2;
4382         while (1)   {
4383             bpoint = strtok(ppoint, "=;");
4384             ppoint = NULL;
4385             if (! bpoint)   break;
4386             if (strcasecmp(bpoint, "layer") == 0)   {
4387                 /* layer name or number */
4388                 bpoint = strtok(ppoint, "=;");
4389                 if (bpoint) {
4390                     if (strcasecmp(bpoint, "background") == 0)  {
4391                         layer = _ico_ivi_background_layer;
4392                     }
4393                     else if (strcasecmp(bpoint, "default") == 0)    {
4394                         layer = _ico_ivi_default_layer;
4395                     }
4396                     else if (strcasecmp(bpoint, "touch") == 0)  {
4397                         layer = _ico_ivi_touch_layer;
4398                     }
4399                     else if (strcasecmp(bpoint, "cursor") == 0) {
4400                         layer = _ico_ivi_cursor_layer;
4401                     }
4402                     else if (strcasecmp(bpoint, "startup") == 0)    {
4403                         layer = _ico_ivi_startup_layer;
4404                     }
4405                     else    {
4406                         layer = strtol(bpoint, (char **)0, 0);
4407                     }
4408                 }
4409             }
4410             else if (strcasecmp(bpoint, "show") == 0)   {
4411                 /* show     */
4412                 visible = 1;
4413             }
4414             else if (strcasecmp(bpoint, "hide") == 0)   {
4415                 /* hide     */
4416                 visible = 0;
4417             }
4418             else if (strcasecmp(bpoint, "raise") == 0)  {
4419                 /* raiee    */
4420                 raise = 1;
4421             }
4422             else if (strcasecmp(bpoint, "lower") == 0)  {
4423                 /* lower    */
4424                 raise = 0;
4425             }
4426             else if (strcasecmp(bpoint, "node") == 0)   {
4427                 /* node     */
4428                 bpoint = strtok(ppoint, ",");
4429                 if (bpoint) {
4430                     node = strtol(bpoint, (char **)0, 0);
4431                 }
4432             }
4433             else if (strcasecmp(bpoint, "position") == 0)   {
4434                 /* position */
4435                 bpoint = strtok(ppoint, ",");
4436                 if (bpoint) {
4437                     cpoint = strtok(ppoint, ";");
4438                     if (cpoint) {
4439                         x = strtol(bpoint, (char **)0, 0);
4440                         y = strtol(cpoint, (char **)0, 0);
4441                     }
4442                 }
4443             }
4444             else if (strcasecmp(bpoint, "size") == 0)   {
4445                 /* size     */
4446                 bpoint = strtok(ppoint, ",");
4447                 if (bpoint) {
4448                     cpoint = strtok(ppoint, ";");
4449                     if (cpoint) {
4450                         width = strtol(bpoint, (char **)0, 0);
4451                         height = strtol(cpoint, (char **)0, 0);
4452                     }
4453                 }
4454             }
4455             else if (strcasecmp(bpoint, "attribute") == 0)  {
4456                 /* attribute flags  */
4457                 bpoint = strtok(ppoint, ",");
4458                 if (bpoint) {
4459                     attribute = strtol(bpoint, (char **)0, 0);
4460                 }
4461             }
4462             else if (strcasecmp(bpoint, "region") == 0) {
4463                 /* set input region     */
4464                 bpoint = strtok(ppoint, ",");
4465                 if (bpoint) {
4466                     ry = 0;
4467                     rw = 100;
4468                     rh = 50;
4469                     rx = strtol(bpoint, (char **)0, 0);
4470                     bpoint = strtok(ppoint, ",");
4471                     if (bpoint) {
4472                         ry = strtol(bpoint, (char **)0, 0);
4473                     }
4474                     bpoint = strtok(ppoint, ",");
4475                     if (bpoint) {
4476                         rw = strtol(bpoint, (char **)0, 0);
4477                     }
4478                     bpoint = strtok(ppoint, ";");
4479                     if (bpoint) {
4480                         rh = strtol(bpoint, (char **)0, 0);
4481                     }
4482                     if (win_mgr_hook_inputregion)   {
4483                         (*win_mgr_hook_inputregion)(1, usurf, rx, ry, rw, rh, 0, 0,
4484                                                     0, 0, 0, 0, 0);
4485                     }
4486                 }
4487             }
4488             else if (strcasecmp(bpoint, "unregion") == 0)   {
4489                 /* unset input region   */
4490                 bpoint = strtok(ppoint, ",");
4491                 if (bpoint) {
4492                     ry = 0;
4493                     rw = 100;
4494                     rh = 50;
4495                     rx = strtol(bpoint, (char **)0, 0);
4496                     bpoint = strtok(ppoint, ",");
4497                     if (bpoint) {
4498                         ry = strtol(bpoint, (char **)0, 0);
4499                     }
4500                     bpoint = strtok(ppoint, ",");
4501                     if (bpoint) {
4502                         rw = strtol(bpoint, (char **)0, 0);
4503                     }
4504                     bpoint = strtok(ppoint, ";");
4505                     if (bpoint) {
4506                         rh = strtol(bpoint, (char **)0, 0);
4507                     }
4508                     if (win_mgr_hook_inputregion)   {
4509                         (*win_mgr_hook_inputregion)(0, usurf, rx, ry, rw, rh, 0, 0,
4510                                                     0, 0, 0, 0, 0);
4511                     }
4512                 }
4513             }
4514         }
4515     }
4516
4517     if ((optpoint == NULL) || (wktitle[0] != 0))  {
4518         if (((usurf->width > 0) && (usurf->height > 0)) &&
4519             ((usurf->created == 0) ||
4520              (strncmp(wktitle, usurf->winname, ICO_IVI_WINNAME_LENGTH-1) != 0)))    {
4521             strncpy(usurf->winname, wktitle, ICO_IVI_WINNAME_LENGTH-1);
4522             usurf->winname[ICO_IVI_WINNAME_LENGTH-1] = 0;
4523             if (usurf->created == 0)    {
4524                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CREATED, usurf, 0,0,0,0,0);
4525                 usurf->created = 1;
4526             }
4527             else    {
4528                 ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_NAME, usurf, 0,0,0,0,0);
4529             }
4530         }
4531         else    {
4532             strncpy(usurf->winname, wktitle, ICO_IVI_WINNAME_LENGTH-1);
4533             usurf->winname[ICO_IVI_WINNAME_LENGTH-1] = 0;
4534         }
4535     }
4536
4537     if (optpoint)   {
4538         if (layer >= 0) {
4539             uifw_set_window_layer(usurf->uclient->client, NULL, usurf->surfaceid, layer);
4540         }
4541         if (attribute >= 0) {
4542             uifw_set_attributes(usurf->uclient->client, NULL, usurf->surfaceid, attribute);
4543         }
4544         if ((node >= 0) || (x >=0) || (y >= 0) || (width >=0) || (height >= 0)) {
4545             if (node < 0)   node = usurf->node_tbl->node;
4546             if (x < 0)      x = ICO_IVI_MAX_COORDINATE + 1;
4547             if (y < 0)      y = ICO_IVI_MAX_COORDINATE + 1;
4548             if (width < 0)  width = ICO_IVI_MAX_COORDINATE + 1;
4549             if (height < 0) height = ICO_IVI_MAX_COORDINATE + 1;
4550             uifw_set_positionsize(usurf->uclient->client, NULL, usurf->surfaceid,
4551                                   node, x, y, width, height, 0);
4552         }
4553         if ((visible >= 0)  || (raise >= 0))    {
4554             if (visible < 0)    visible = ICO_WINDOW_MGR_V_NOCHANGE;
4555             if (raise < 0)      raise = ICO_WINDOW_MGR_V_NOCHANGE;
4556             uifw_set_visible(usurf->uclient->client, NULL, usurf->surfaceid,
4557                              visible, raise, 0);
4558         }
4559     }
4560     uifw_trace("win_mgr_set_title: Leave");
4561
4562     return usurf->winname;
4563 }
4564
4565 /*--------------------------------------------------------------------------*/
4566 /**
4567  * @brief   win_mgr_surface_move: set tile name to surface
4568  *
4569  * @param[in]   surface     weston surface
4570  * @param[in]   title       title name
4571  * @return      none
4572  */
4573 /*--------------------------------------------------------------------------*/
4574 static void
4575 win_mgr_surface_move(struct weston_surface *surface, int *dx, int *dy)
4576 {
4577     struct uifw_win_surface *usurf;
4578
4579     uifw_trace("win_mgr_surface_move: Enter(%08x) x/y=%d,%d", (int)surface, *dx, *dy);
4580
4581     usurf = find_uifw_win_surface_by_ws(surface);
4582     if (! usurf) {
4583         uifw_trace("win_mgr_surface_move: Leave(Not Exist)");
4584         return;
4585     }
4586     if (usurf->visible) {
4587         *dx = usurf->node_tbl->disp_x + usurf->x;
4588         *dy = usurf->node_tbl->disp_y + usurf->y;
4589     }
4590     else    {
4591         *dx = ICO_IVI_MAX_COORDINATE+1;
4592         *dy = ICO_IVI_MAX_COORDINATE+1;
4593     }
4594     uifw_trace("win_mgr_surface_move: Leave(change to x/y=%d/%d)", *dx, *dy);
4595 }
4596
4597 /*--------------------------------------------------------------------------*/
4598 /**
4599  * @brief   win_mgr_show_layer: shell layer visible control
4600  *
4601  * @param[in]   layertype   shell layer type
4602  * @param[in]   show        show(1)/hide(0)
4603  * @param[in]   data        requested weston surface in show
4604  * @return      none
4605  */
4606 /*--------------------------------------------------------------------------*/
4607 static void
4608 win_mgr_show_layer(int layertype, int show, void *data)
4609 {
4610     struct uifw_win_layer   *el;
4611     struct uifw_win_surface *usurf;
4612     struct uifw_win_surface *inusurf = NULL;
4613
4614     uifw_trace("win_mgr_show_layer: Enter(type=%d, show=%d, data=%08x)",
4615                layertype, show, (int)data);
4616
4617     if (layertype != LAYER_TYPE_INPUTPANEL) {
4618         uifw_trace("win_mgr_show_layer: Leave(layertype npt InputPanel)");
4619         return;
4620     }
4621     if (show)   {
4622         if (data == NULL)   {
4623             uifw_trace("win_mgr_show_layer: Leave(show but input surface not exist)");
4624             return;
4625         }
4626         inusurf = find_uifw_win_surface_by_ws((struct weston_surface *)data);
4627         if (! inusurf)  {
4628             uifw_trace("win_mgr_show_layer: Leave(show but unknown input surface)");
4629             return;
4630         }
4631     }
4632
4633     /*  all input panel surface show/hide   */
4634     wl_list_for_each (el, &_ico_win_mgr->ivi_layer_list, link)  {
4635         if ((el->layertype == LAYER_TYPE_CURSOR) ||
4636             (el->layertype == LAYER_TYPE_TOUCH))    continue;
4637         wl_list_for_each (usurf, &el->surface_list, ivi_layer) {
4638             if ((usurf->layertype == LAYER_TYPE_INPUTPANEL) &&
4639                 (usurf->surface != NULL) && (usurf->mapped != 0) &&
4640                 (usurf->surface->buffer_ref.buffer != NULL))    {
4641
4642                 if ((inusurf != NULL) && (usurf->win_layer != inusurf->win_layer))  {
4643                     win_mgr_set_layer(usurf, usurf->win_layer->layer);
4644                     usurf->raise = 1;
4645                     win_mgr_change_surface(usurf->surface, -1, 1);
4646                 }
4647                 if ((show == 0) || (ico_ivi_optionflag() & ICO_IVI_OPTION_SHOW_INPUTLAYER)) {
4648                     /* show input panel automatically   */
4649                     ico_window_mgr_set_visible(usurf, show | 2);
4650                 }
4651                 else    {
4652                     /* send hint event to HomeScreen    */
4653                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
4654                                             usurf, show, ICO_WINDOW_MGR_RAISE_RAISE, 1, 0,0);
4655                 }
4656             }
4657         }
4658     }
4659     uifw_trace("win_mgr_show_layer: Leave");
4660 }
4661
4662 /*--------------------------------------------------------------------------*/
4663 /**
4664  * @brief   win_mgr_fullscreen: shell full screen surface control
4665  *
4666  * @param[in]   event       control event
4667  * @param[in]   surface     target weston surface
4668  * @return      result
4669  */
4670 /*--------------------------------------------------------------------------*/
4671 static int
4672 win_mgr_fullscreen(int event, struct weston_surface *surface)
4673 {
4674     struct uifw_win_surface *usurf;
4675     struct uifw_win_surface *tmpusurf;
4676     struct uifw_win_layer   *ulayer;
4677     int     width, height;
4678     int     sx, sy;
4679
4680     uifw_trace("win_mgr_fullscreen: Enter(event=%d, surface=%08x)", event, (int)surface);
4681
4682     if (event == SHELL_FULLSCREEN_HIDEALL)  {
4683         /* hide all fullscreen srface       */
4684         uifw_trace("win_mgr_fullscreen: SHELL_FULLSCREEN_HIDEALL");
4685
4686         wl_list_for_each (ulayer, &_ico_win_mgr->ivi_layer_list, link)  {
4687             if (ulayer->layertype >= LAYER_TYPE_TOUCH)  continue;
4688             wl_list_for_each_safe (usurf, tmpusurf, &ulayer->surface_list, ivi_layer)   {
4689                 if (usurf->layertype == LAYER_TYPE_FULLSCREEN)  {
4690                     ico_window_mgr_set_visible(usurf, 2);
4691                     usurf->layertype = usurf->old_layertype;
4692                     win_mgr_set_layer(usurf, usurf->old_layer->layer);
4693                     win_mgr_change_surface(usurf->surface, -1, 1);
4694                     /* send event to HomeScreen         */
4695                     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
4696                                             usurf, usurf->x, usurf->y,
4697                                             usurf->width, usurf->height, 0);
4698                 }
4699             }
4700         }
4701         uifw_trace("win_mgr_fullscreen: Leave");
4702         return 0;
4703     }
4704
4705     usurf = find_uifw_win_surface_by_ws(surface);
4706     if (! usurf)    {
4707         uifw_trace("win_mgr_fullscreen: Leave(surface dose not exist)");
4708         return -1;
4709     }
4710
4711     switch(event)   {
4712     case SHELL_FULLSCREEN_ISTOP:        /* check if top surface             */
4713         if (usurf->layertype == LAYER_TYPE_FULLSCREEN)  {
4714             wl_list_for_each (ulayer, &_ico_win_mgr->ivi_layer_list, link)  {
4715                 if (ulayer->layertype >= LAYER_TYPE_TOUCH)  continue;
4716                 wl_list_for_each(tmpusurf, &ulayer->surface_list, ivi_layer)    {
4717                     if (usurf == tmpusurf)  {
4718                         uifw_trace("win_mgr_fullscreen: %08x SHELL_FULLSCREEN_ISTOP"
4719                                    "(fullscreen surface)", usurf->surfaceid);
4720                         return 1;
4721                     }
4722                     if (tmpusurf->layertype == LAYER_TYPE_FULLSCREEN)   {
4723                         uifw_trace("win_mgr_fullscreen: %08x SHELL_FULLSCREEN_ISTOP"
4724                                    "(fullscreen surface but not top)", usurf->surfaceid);
4725                         return 0;
4726                     }
4727                 }
4728             }
4729         }
4730         uifw_trace("win_mgr_fullscreen: %08x SHELL_FULLSCREEN_ISTOP"
4731                    "(not fullscreen surface)", usurf->surfaceid);
4732         return 0;
4733     case SHELL_FULLSCREEN_SET:          /* change surface to full screen    */
4734         uifw_trace("win_mgr_fullscreen: %08x SHELL_FULLSCREEN_SET", usurf->surfaceid);
4735         if (usurf->layertype != LAYER_TYPE_FULLSCREEN)  {
4736             usurf->old_layertype = usurf->layertype;
4737             usurf->layertype = LAYER_TYPE_FULLSCREEN;
4738             usurf->old_layer = usurf->win_layer;
4739             /* send hint event to HomeScreen    */
4740             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
4741                                     usurf, usurf->x, usurf->y,
4742                                     usurf->width, usurf->height, 1);
4743         }
4744         break;
4745     case SHELL_FULLSCREEN_UNSET:        /* change surface to normal screen  */
4746         uifw_trace("win_mgr_fullscreen: %08x SHELL_FULLSCREEN_UNSET", usurf->surfaceid);
4747         if (usurf->layertype == LAYER_TYPE_FULLSCREEN)  {
4748             ico_window_mgr_set_visible(usurf, 2);
4749             usurf->layertype = usurf->old_layertype;
4750             win_mgr_set_layer(usurf, usurf->old_layer->layer);
4751             win_mgr_change_surface(usurf->surface, -1, 1);
4752             /* send event to HomeScreen         */
4753             ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_CONFIGURE,
4754                                     usurf, usurf->x, usurf->y,
4755                                     usurf->width, usurf->height, 0);
4756         }
4757         break;
4758     case SHELL_FULLSCREEN_CONF:         /* configure full screen surface    */
4759         uifw_trace("win_mgr_fullscreen: %08x SHELL_FULLSCREEN_CONF", usurf->surfaceid);
4760         if (usurf->mapped == 0) {
4761             width = usurf->node_tbl->disp_width;
4762             height = usurf->node_tbl->disp_height;
4763             sx = 0;
4764             sy = 0;
4765             win_mgr_surface_map(usurf->surface, &width, &height, &sx, &sy);
4766         }
4767         break;
4768     default:
4769         uifw_trace("win_mgr_fullscreen: Leave(unknown event %d)", event);
4770         return -1;
4771     }
4772     uifw_trace("win_mgr_fullscreen: Leave");
4773     return 0;
4774 }
4775
4776 /*--------------------------------------------------------------------------*/
4777 /**
4778  * @brief   win_mgr_reset_focus: reset surface focus
4779  *
4780  * @param[in]   usurf       UIFW surface
4781  * @return      none
4782  */
4783 /*--------------------------------------------------------------------------*/
4784 static void
4785 win_mgr_reset_focus(struct uifw_win_surface *usurf)
4786 {
4787     struct weston_seat      *seat;
4788     struct weston_surface   *surface;
4789
4790     uifw_trace("win_mgr_reset_focus: Enter(%08x)", usurf->surfaceid);
4791
4792     seat = container_of (_ico_win_mgr->compositor->seat_list.next, struct weston_seat, link);
4793     surface = usurf->surface;
4794     if ((seat != NULL) && (surface != NULL))    {
4795 #if 0               /* pointer grab can not release */
4796         /* reset pointer focus          */
4797         if ((seat->pointer != NULL) && (seat->pointer->focus == surface))   {
4798             weston_pointer_set_focus(seat->pointer, NULL,
4799                                      wl_fixed_from_int(0), wl_fixed_from_int(0));
4800         }
4801         /* reset touch focus            */
4802         if ((seat->touch != NULL) && (seat->touch->focus == surface))   {
4803             weston_touch_set_focus(seat, NULL);
4804         }
4805 #endif              /* pointer grab can not release */
4806         /* reset keyboard focus         */
4807         if ((seat->keyboard != NULL) && (seat->keyboard->focus == surface)) {
4808             weston_keyboard_set_focus(seat->keyboard, NULL);
4809         }
4810     }
4811     uifw_trace("win_mgr_reset_focus: Leave");
4812 }
4813
4814 /*--------------------------------------------------------------------------*/
4815 /**
4816  * @brief   ico_window_mgr_set_visible: change surface visibility
4817  *
4818  * @param[in]   usurf       UIFW surface
4819  * @param[in]   visible     bit 0: visible(=1)/unvisible(=0)
4820  *                          bit 1: width anima(=1)/without anima(=0)
4821  * @return      none
4822  */
4823 /*--------------------------------------------------------------------------*/
4824 WL_EXPORT   void
4825 ico_window_mgr_set_visible(struct uifw_win_surface *usurf, const int visible)
4826 {
4827     int     retanima;
4828
4829     if (visible & 1)    {
4830         if ((usurf->visible == 0) ||
4831             (usurf->animation.state != ICO_WINDOW_MGR_ANIMATION_STATE_NONE))    {
4832             uifw_trace("ico_window_mgr_set_visible: Chagne to Visible(%08x) x/y=%d/%d",
4833                        usurf->surfaceid, usurf->x, usurf->y);
4834             usurf->visible = 1;
4835             ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
4836                                               usurf->width, usurf->height);
4837             if ((visible & 2) && (win_mgr_hook_animation != NULL))  {
4838                 usurf->animation.pos_x = usurf->x;
4839                 usurf->animation.pos_y = usurf->y;
4840                 usurf->animation.pos_width = usurf->width;
4841                 usurf->animation.pos_height = usurf->height;
4842                 usurf->animation.no_configure = 0;
4843                 retanima = (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_OPSHOW,
4844                                                  (void *)usurf);
4845                 uifw_trace("ico_window_mgr_set_visible: show animation = %d", retanima);
4846             }
4847             /* change unvisible to visible, restack surface list    */
4848             ico_window_mgr_restack_layer(usurf);
4849         }
4850     }
4851     else    {
4852         if ((usurf->visible != 0) ||
4853             (usurf->animation.state != ICO_WINDOW_MGR_ANIMATION_STATE_NONE))    {
4854
4855             uifw_trace("ico_window_mgr_set_visible: Chagne to Unvisible(%08x)",
4856                        usurf->surfaceid);
4857
4858             /* Reset focus              */
4859             win_mgr_reset_focus(usurf);
4860
4861             retanima = ICO_WINDOW_MGR_ANIMATION_RET_ANIMA;
4862             if ((visible & 2) && (win_mgr_hook_animation != NULL))  {
4863                 usurf->animation.pos_x = usurf->x;
4864                 usurf->animation.pos_y = usurf->y;
4865                 usurf->animation.pos_width = usurf->width;
4866                 usurf->animation.pos_height = usurf->height;
4867                 usurf->animation.no_configure = 0;
4868                 retanima = (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_OPHIDE,
4869                                                     (void *)usurf);
4870                 uifw_trace("ico_window_mgr_set_visible: hide animation = %d", retanima);
4871                 if (retanima != ICO_WINDOW_MGR_ANIMATION_RET_ANIMANOCTL)    {
4872                     usurf->visible = 0;
4873                     /* Weston surface configure                     */
4874                     ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
4875                                                       usurf->width, usurf->height);
4876                 }
4877             }
4878             else    {
4879                 usurf->visible = 0;
4880                 /* Weston surface configure                     */
4881                 ico_window_mgr_set_weston_surface(usurf, usurf->x, usurf->y,
4882                                                   usurf->width, usurf->height);
4883             }
4884             ico_window_mgr_restack_layer(usurf);
4885         }
4886     }
4887     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
4888                             usurf, usurf->visible, usurf->raise, 0, 0, 0);
4889 }
4890
4891 /*--------------------------------------------------------------------------*/
4892 /**
4893  * @brief   win_mgr_set_raise: change surface raise/lower
4894  *
4895  * @param[in]   usurf       UIFW surface
4896  * @param[in]   raise       raise(=1)/lower(0)
4897  * @return      none
4898  */
4899 /*--------------------------------------------------------------------------*/
4900 static void
4901 win_mgr_set_raise(struct uifw_win_surface *usurf, const int raise)
4902 {
4903     struct uifw_win_surface *eu;
4904
4905     uifw_trace("win_mgr_set_raise: Enter(%08x,%d) layer=%d type=%x",
4906                (int)usurf, raise, (int)usurf->win_layer->layer, usurf->layertype);
4907
4908     wl_list_remove(&usurf->ivi_layer);
4909     if (raise & 1)  {
4910         /* raise ... surface stack to top of layer          */
4911         if (usurf->layertype == LAYER_TYPE_INPUTPANEL)  {
4912             /* if input panel, top of surface list          */
4913             uifw_trace("win_mgr_set_raise: Raise Link to Top(InputPanel)");
4914             wl_list_insert(&usurf->win_layer->surface_list, &usurf->ivi_layer);
4915         }
4916         else    {
4917             /* if not input panel, search not input panel   */
4918             wl_list_for_each (eu, &usurf->win_layer->surface_list, ivi_layer)   {
4919                 if (eu->layertype != LAYER_TYPE_INPUTPANEL) break;
4920             }
4921             uifw_trace("win_mgr_set_raise: Raise Link to Top(Normal)");
4922             wl_list_insert(eu->ivi_layer.prev, &usurf->ivi_layer);
4923         }
4924         usurf->raise = 1;
4925     }
4926     else    {
4927         /* Lower ... surface stack to bottom of layer       */
4928         if (usurf->layertype == LAYER_TYPE_INPUTPANEL)  {
4929             /* if input panel, search not input panel       */
4930             uifw_trace("win_mgr_set_raise: Lower Link to Bottom(InputPanel)");
4931             wl_list_for_each (eu, &usurf->win_layer->surface_list, ivi_layer)   {
4932                 if (eu->layertype != LAYER_TYPE_INPUTPANEL) break;
4933             }
4934             wl_list_insert(eu->ivi_layer.prev, &usurf->ivi_layer);
4935         }
4936         else    {
4937             /* if not input panel, bottom of surface list   */
4938             uifw_trace("win_mgr_set_raise: Lower Link to Bottom(Normal)");
4939             wl_list_insert(usurf->win_layer->surface_list.prev, &usurf->ivi_layer);
4940         }
4941         usurf->raise = 0;
4942     }
4943
4944     /* rebild compositor surface list               */
4945     if ((raise & 2) == 0)   {
4946         if (usurf->visible) {
4947             ico_window_mgr_restack_layer(usurf);
4948         }
4949         ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_VISIBLE,
4950                                 usurf, usurf->visible, usurf->raise, 0, 0,0);
4951     }
4952     uifw_trace("win_mgr_set_raise: Leave");
4953 }
4954
4955 /*--------------------------------------------------------------------------*/
4956 /**
4957  * @brief   win_mgr_destroy_surface: surface destroy
4958  *
4959  * @param[in]   surface     Weston surface
4960  * @return      none
4961  */
4962 /*--------------------------------------------------------------------------*/
4963 static void
4964 win_mgr_destroy_surface(struct weston_surface *surface)
4965 {
4966     struct uifw_win_surface *usurf;
4967     struct uifw_win_surface *phash;
4968     struct uifw_win_surface *bhash;
4969     uint32_t    hash;
4970
4971     usurf = find_uifw_win_surface_by_ws(surface);
4972     if (! usurf) {
4973         uifw_trace("win_mgr_destroy_surface: UIFW surface Not Exist");
4974         return;
4975     }
4976     uifw_trace("win_mgr_destroy_surface: Enter(%08x) %08x", (int)surface, usurf->surfaceid);
4977
4978     /* Reset focus                  */
4979     win_mgr_reset_focus(usurf);
4980
4981     /* destory input region         */
4982     if (win_mgr_hook_destory)   {
4983         (*win_mgr_hook_destory)(usurf);
4984     }
4985
4986     /* unmap surface                */
4987     if (&usurf->surf_map != usurf->surf_map.next)   {
4988         uifw_unmap_surface(NULL, NULL, usurf->surfaceid);
4989     }
4990
4991     /* destroy active surface       */
4992     if (usurf == _ico_win_mgr->active_pointer_usurf)  {
4993         _ico_win_mgr->active_pointer_usurf = NULL;
4994     }
4995     if (usurf == _ico_win_mgr->active_keyboard_usurf) {
4996         _ico_win_mgr->active_keyboard_usurf = NULL;
4997     }
4998
4999     /* destroy animation extenson   */
5000     if (win_mgr_hook_animation) {
5001         (*win_mgr_hook_animation)(ICO_WINDOW_MGR_ANIMATION_DESTROY, (void *)usurf);
5002     }
5003
5004     /* delete from layer list       */
5005     wl_list_remove(&usurf->ivi_layer);
5006     ico_window_mgr_restack_layer(NULL);
5007
5008     /* delete from cleint list      */
5009     wl_list_remove(&usurf->client_link);
5010
5011     /* delete from hash table       */
5012     hash = MAKE_IDHASH(usurf->surfaceid);
5013     phash = _ico_win_mgr->idhash[hash];
5014     bhash = NULL;
5015     while ((phash) && (phash != usurf)) {
5016         bhash = phash;
5017         phash = phash->next_idhash;
5018     }
5019     if (bhash)  {
5020         bhash->next_idhash = usurf->next_idhash;
5021     }
5022     else    {
5023         _ico_win_mgr->idhash[hash] = usurf->next_idhash;
5024     }
5025
5026     hash = MAKE_WSHASH(usurf->surface);
5027     phash = _ico_win_mgr->wshash[hash];
5028     bhash = NULL;
5029     while ((phash) && (phash != usurf)) {
5030         bhash = phash;
5031         phash = phash->next_wshash;
5032     }
5033     if (bhash)  {
5034         bhash->next_wshash = usurf->next_wshash;
5035     }
5036     else    {
5037         _ico_win_mgr->wshash[hash] = usurf->next_wshash;
5038     }
5039
5040     ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_DESTROYED,
5041                            usurf, 0,0,0,0,0);
5042
5043     hash = usurf->surfaceid & SURCAFE_ID_MASK;
5044     _ico_win_mgr->surfaceid_map[(hash - 1)/16] &= ~(1 << ((hash - 1) % 16));
5045
5046     free(usurf);
5047     uifw_trace("win_mgr_destroy_surface: Leave(OK)");
5048 }
5049
5050 /*--------------------------------------------------------------------------*/
5051 /**
5052  * @brief   bind_ico_win_mgr: bind Multi Window Manager from client
5053  *
5054  * @param[in]   client      client
5055  * @param[in]   data        user data(unused)
5056  * @param[in]   version     protocol version(unused)
5057  * @param[in]   id          client object id
5058  * @return      none
5059  */
5060 /*--------------------------------------------------------------------------*/
5061 static void
5062 bind_ico_win_mgr(struct wl_client *client,
5063                  void *data, uint32_t version, uint32_t id)
5064 {
5065     struct wl_resource  *add_resource;
5066     struct uifw_manager *mgr;
5067     struct uifw_client  *uclient;
5068
5069     uifw_trace("bind_ico_win_mgr: Enter(client=%08x, id=%x)", (int)client, (int)id);
5070
5071     add_resource = wl_resource_create(client, &ico_window_mgr_interface, 1, id);
5072     if (add_resource)   {
5073         wl_resource_set_implementation(add_resource, &ico_window_mgr_implementation,
5074                                        _ico_win_mgr, unbind_ico_win_mgr);
5075     }
5076
5077     /* Create client management tabel       */
5078     uclient = ico_window_mgr_find_uclient(client);
5079     if (! uclient)  {
5080         win_mgr_bind_client(client, NULL);
5081         uclient = ico_window_mgr_find_uclient(client);
5082     }
5083
5084     /* Manager                              */
5085     mgr = (struct uifw_manager *)malloc(sizeof(struct uifw_manager));
5086     if (! mgr)  {
5087         uifw_error("bind_ico_win_mgr: Error, No Memory");
5088         return;
5089     }
5090     memset(mgr, 0, sizeof(struct uifw_manager));
5091     mgr->resource = add_resource;
5092     if (uclient)    {
5093         uclient->mgr = mgr;
5094     }
5095     wl_list_insert(&_ico_win_mgr->manager_list, &mgr->link);
5096
5097     uifw_trace("bind_ico_win_mgr: Leave");
5098 }
5099
5100 /*--------------------------------------------------------------------------*/
5101 /**
5102  * @brief   unbind_ico_win_mgr: unbind Multi Window Manager from client
5103  *
5104  * @param[in]   resource    client resource
5105  * @return      none
5106  */
5107 /*--------------------------------------------------------------------------*/
5108 static void
5109 unbind_ico_win_mgr(struct wl_resource *resource)
5110 {
5111     struct uifw_manager *mgr, *itmp;
5112
5113     uifw_trace("unbind_ico_win_mgr: Enter");
5114
5115     /* Remove manager from manager list */
5116     _ico_win_mgr->num_manager = 0;
5117     wl_list_for_each_safe (mgr, itmp, &_ico_win_mgr->manager_list, link)    {
5118         if (mgr->resource == resource) {
5119             wl_list_remove(&mgr->link);
5120             free(mgr);
5121         }
5122         else    {
5123             if (mgr->manager)   {
5124                 _ico_win_mgr->num_manager++;
5125             }
5126         }
5127     }
5128     uifw_trace("unbind_ico_win_mgr: Leave");
5129 }
5130
5131 /*--------------------------------------------------------------------------*/
5132 /**
5133  * @brief   ico_win_mgr_send_to_mgr: send event to manager(HomeScreen)
5134  *
5135  * @param[in]   event       event code(if -1, not send event)
5136  * @param[in]   usurf       UIFW surface table
5137  * @param[in]   param1      parameter 1
5138  * @param[in]      :             :
5139  * @param[in]   param5      parameter 5
5140  * @return      number of managers
5141  * @retval      > 0         number of managers
5142  * @retval      0           manager not exist
5143  */
5144 /*--------------------------------------------------------------------------*/
5145 static int
5146 ico_win_mgr_send_to_mgr(const int event, struct uifw_win_surface *usurf,
5147                         const int param1, const int param2, const int param3,
5148                         const int param4, const int param5)
5149 {
5150     int     num_mgr = 0;
5151     struct uifw_manager* mgr;
5152
5153     /* if DESTROY and no send CREATE, Nop       */
5154     if ((event == ICO_WINDOW_MGR_WINDOW_DESTROYED) &&
5155         (usurf != NULL) && (usurf->created == 0))   {
5156         return 0;
5157     }
5158
5159     /* if appid not fix, check and fix appid    */
5160     if ((usurf != NULL) &&
5161         (usurf->uclient->fixed_appid < ICO_WINDOW_MGR_APPID_FIXCOUNT)) {
5162         win_mgr_get_client_appid(usurf->uclient);
5163     }
5164
5165     /* send created if not send created event   */
5166     if ((usurf != NULL) && (usurf->created == 0) &&
5167         (((usurf->width > 0) && (usurf->height > 0)) ||
5168          ((event != ICO_WINDOW_MGR_WINDOW_CREATED) &&
5169           (event != ICO_WINDOW_MGR_WINDOW_NAME))))  {
5170         wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
5171             if (mgr->manager)   {
5172                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) WINDOW_CREATED"
5173                            "(surf=%08x,name=%s,pid=%d,appid=%s,type=%x)", (int)mgr->resource,
5174                            usurf->surfaceid, usurf->winname, usurf->uclient->pid,
5175                            usurf->uclient->appid, usurf->layertype);
5176                 ico_window_mgr_send_window_created(mgr->resource, usurf->surfaceid,
5177                                                    usurf->winname, usurf->uclient->pid,
5178                                                    usurf->uclient->appid,
5179                                                    usurf->layertype << 12);
5180             }
5181         }
5182         usurf->created = 1;
5183     }
5184
5185     wl_list_for_each (mgr, &_ico_win_mgr->manager_list, link)   {
5186         if (mgr->manager)   {
5187             num_mgr ++;
5188
5189             switch(event)   {
5190             case ICO_WINDOW_MGR_WINDOW_CREATED:
5191                 /* Do nothing anymore because sended window create event    */
5192                 break;
5193
5194             case ICO_WINDOW_MGR_WINDOW_NAME:
5195                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) WINDOW_NAME"
5196                            "(surf=%08x,name=%s)", (int)mgr->resource,
5197                            usurf->surfaceid, usurf->winname);
5198                 ico_window_mgr_send_window_name(mgr->resource, usurf->surfaceid,
5199                                                 usurf->winname);
5200                 break;
5201
5202             case ICO_WINDOW_MGR_WINDOW_DESTROYED:
5203                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) DESTROYED"
5204                            "(surf=%08x)", (int)mgr->resource, usurf->surfaceid);
5205                 ico_window_mgr_send_window_destroyed(mgr->resource, usurf->surfaceid);
5206                 break;
5207
5208             case ICO_WINDOW_MGR_WINDOW_VISIBLE:
5209                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) VISIBLE"
5210                            "(surf=%08x,vis=%d,raise=%d,hint=%d)",
5211                            (int)mgr->resource, usurf->surfaceid, param1, param2, param3);
5212                 ico_window_mgr_send_window_visible(mgr->resource,
5213                                                    usurf->surfaceid, param1, param2, param3);
5214                 break;
5215
5216             case ICO_WINDOW_MGR_WINDOW_CONFIGURE:
5217                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) CONFIGURE"
5218                            "(surf=%08x,app=%s,node=%x,type=%x,layer=%d,"
5219                            "x/y=%d/%d,w/h=%d/%d,hint=%d)",
5220                            (int)mgr->resource, usurf->surfaceid, usurf->uclient->appid,
5221                            usurf->node_tbl->node, usurf->layertype,
5222                            usurf->win_layer->layer, param1, param2, param3, param4, param5);
5223                 ico_window_mgr_send_window_configure(mgr->resource, usurf->surfaceid,
5224                                                      usurf->node_tbl->node,
5225                                                      usurf->layertype << 12,
5226                                                      usurf->win_layer->layer,
5227                                                      param1, param2, param3, param4, param5);
5228                 break;
5229
5230             case ICO_WINDOW_MGR_WINDOW_ACTIVE:
5231                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) ACTIVE"
5232                            "(surf=%08x,active=%d)", (int)mgr->resource, usurf->surfaceid,
5233                            param1);
5234                 ico_window_mgr_send_window_active(mgr->resource, usurf->surfaceid,
5235                                                   (uint32_t)param1);
5236                 break;
5237
5238             case ICO_WINDOW_MGR_LAYER_VISIBLE:
5239                 uifw_trace("ico_win_mgr_send_to_mgr: Send Manager(%08x) LAYER_VISIBLE"
5240                            "(layer=%d,visivle=%d)", (int)mgr->resource, param1, param2);
5241                 ico_window_mgr_send_layer_visible(mgr->resource, (uint32_t)param1, param2);
5242                 break;
5243
5244             default:
5245                 uifw_error("ico_win_mgr_send_to_mgr: Illegal event(%08x)", event);
5246                 break;
5247             }
5248         }
5249     }
5250     return num_mgr;
5251 }
5252
5253 /*--------------------------------------------------------------------------*/
5254 /**
5255  * @brief   win_mgr_set_scale: set surface transform scale
5256  *
5257  * @param[in]   usurf       UIFW surface
5258  * @return      chagne display
5259  * @retval      =1          change display
5260  * @retval      =0          no change
5261  */
5262 /*--------------------------------------------------------------------------*/
5263 static int
5264 win_mgr_set_scale(struct uifw_win_surface *usurf)
5265 {
5266     struct weston_surface   *es;
5267     struct weston_view      *ev;
5268     float   scalex;
5269     float   scaley;
5270     int     ret = 0;
5271
5272     es = usurf->surface;
5273     if ((es != NULL) && (es->buffer_ref.buffer))  {
5274         if (usurf->client_width == 0)   usurf->client_width = es->width;
5275         if (usurf->client_height == 0)  usurf->client_height = es->height;
5276         if ((usurf->client_width > 0) && (usurf->client_height > 0))    {
5277             scalex = (float)usurf->width / (float)usurf->client_width;
5278             scaley = (float)usurf->height / (float)usurf->client_height;
5279         }
5280         else    {
5281             scalex = 1.0f;
5282             scaley = 1.0f;
5283         }
5284         uifw_debug("win_mgr_set_scale: %08x X=%4.2f(%d/%d) Y=%4.2f(%d/%d)",
5285                    usurf->surfaceid, scalex, usurf->width, usurf->client_width,
5286                    scaley, usurf->height, usurf->client_height);
5287         usurf->xadd = 0;
5288         usurf->yadd = 0;
5289         if ((ico_ivi_optionflag() & ICO_IVI_OPTION_FIXED_ASPECT) ||
5290             (usurf->attributes & ICO_WINDOW_MGR_ATTR_FIXED_ASPECT)) {
5291             if (scalex > scaley)    {
5292                 scalex = scaley;
5293                 if ((usurf->attributes & ICO_WINDOW_MGR_ATTR_ALIGN_LEFT) == 0)  {
5294                     usurf->xadd = (float)usurf->width - ((float)usurf->client_width * scalex);
5295                     if ((usurf->attributes & ICO_WINDOW_MGR_ATTR_ALIGN_RIGHT) == 0) {
5296                         usurf->xadd /= 2;
5297                     }
5298                 }
5299             }
5300             else if (scalex < scaley)   {
5301                 scaley = scalex;
5302                 if ((usurf->attributes & ICO_WINDOW_MGR_ATTR_ALIGN_TOP) == 0)   {
5303                     usurf->yadd = (float)usurf->height - ((float)usurf->client_height * scaley);
5304                     if ((usurf->attributes & ICO_WINDOW_MGR_ATTR_ALIGN_BOTTOM) == 0)    {
5305                         usurf->yadd /= 2;
5306                     }
5307                 }
5308             }
5309             uifw_debug("win_mgr_set_scale: %08x fixed aspect x/yadd=%d/%d",
5310                        usurf->surfaceid, usurf->xadd, usurf->yadd);
5311         }
5312         if ((scalex != usurf->scalex) || (scaley != usurf->scaley)) {
5313             usurf->scalex = scalex;
5314             usurf->scaley = scaley;
5315             ev = ico_ivi_get_primary_view(usurf);
5316             if ((scalex != 1.0f) || (scaley != 1.0f))   {
5317                 weston_matrix_init(&usurf->transform.matrix);
5318                 weston_matrix_scale(&usurf->transform.matrix, scalex, scaley, 1.0f);
5319                 uifw_trace("win_mgr_set_scale: change scale(%d)", usurf->set_transform);
5320                 if (usurf->set_transform == 0)  {
5321                     usurf->set_transform = 1;
5322                     wl_list_init(&usurf->transform.link);
5323                     wl_list_insert(&ev->geometry.transformation_list, &usurf->transform.link);
5324                 }
5325             }
5326             else if (usurf->set_transform != 0) {
5327                 uifw_trace("win_mgr_set_scale: reset transform");
5328                 usurf->set_transform = 0;
5329                 wl_list_remove(&usurf->transform.link);
5330             }
5331             weston_view_geometry_dirty(ev);
5332             weston_view_update_transform(ev);
5333             weston_view_damage_below(ev);
5334             weston_surface_damage(es);
5335             ret = 1;
5336         }
5337     }
5338     return ret;
5339 }
5340
5341 /*--------------------------------------------------------------------------*/
5342 /**
5343  * @brief   ico_window_mgr_get_uclient: get UIFW client table
5344  *
5345  * @param[in]   appid       application Id
5346  * @return      UIFW client table
5347  * @retval      !=NULL      success(UIFW client table address)
5348  * @retval      = NULL      error(appid not exist)
5349  */
5350 /*--------------------------------------------------------------------------*/
5351 WL_EXPORT   struct uifw_client *
5352 ico_window_mgr_get_uclient(const char *appid)
5353 {
5354     struct uifw_client *uclient;
5355
5356     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
5357         if (strcmp(uclient->appid, appid) == 0) {
5358             return uclient;
5359         }
5360     }
5361     return NULL;
5362 }
5363
5364 /*--------------------------------------------------------------------------*/
5365 /**
5366  * @brief   ico_window_mgr_get_client_usurf: get client UIFW surface table
5367  *
5368  * @param[in]   target      surface window name and application Id(winname@appid)
5369  * @return      UIFW surface table
5370  * @retval      !=NULL      success(UIFW surface table address)
5371  * @retval      = NULL      error(appid or winname not exist)
5372  */
5373 /*--------------------------------------------------------------------------*/
5374 WL_EXPORT   struct uifw_win_surface *
5375 ico_window_mgr_get_client_usurf(const char *target)
5376 {
5377     struct uifw_client      *uclient;
5378     struct uifw_win_surface *usurf;
5379     int     i, j;
5380     char    winname[ICO_IVI_WINNAME_LENGTH];
5381     char    appid[ICO_IVI_APPID_LENGTH];
5382
5383     /* get window name and application id   */
5384     j = 0;
5385     for (i = 0; target[i]; i++) {
5386         if (target[i] == '@')   {
5387             if (target[i+1] != '@') break;
5388             i ++;
5389         }
5390         if (j < (ICO_IVI_WINNAME_LENGTH-1)) {
5391             winname[j++] = target[i];
5392         }
5393     }
5394     winname[j] = 0;
5395     if (target[i] == '@')   {
5396         i ++;
5397     }
5398     else    {
5399         winname[0] = 0;
5400         i = 0;
5401     }
5402     j = 0;
5403     for ( ; target[i]; i++) {
5404         if ((target[i] == '@') && (target[i+1] == '@')) i ++;
5405         if (j < (ICO_IVI_APPID_LENGTH-1))  {
5406             appid[j++] = target[i];
5407         }
5408     }
5409     appid[j] = 0;
5410     uifw_debug("ico_window_mgr_get_client_usurf: target=<%s> appid=<%s> win=<%s>",
5411                target, appid, winname);
5412
5413     wl_list_for_each (uclient, &_ico_win_mgr->client_list, link)    {
5414         if (strcmp(uclient->appid, appid) == 0) {
5415             wl_list_for_each (usurf, &uclient->surface_link, client_link)   {
5416                 if ((winname[0] == 0) ||
5417                     (strcmp(winname, usurf->winname) == 0)) {
5418                     return usurf;
5419                 }
5420             }
5421         }
5422     }
5423     return NULL;
5424 }
5425
5426 /*--------------------------------------------------------------------------*/
5427 /**
5428  * @brief   ico_window_mgr_is_visible: check surface visible
5429  *
5430  * @param[in]   usurf       UIFW surface
5431  * @return      visibility
5432  * @retval      =1          visible
5433  * @retval      =0          not visible
5434  * @retval      =-1         surface visible but layer not vlsible
5435  * @retval      =-2         surface visible but lower(currently not support)
5436  */
5437 /*--------------------------------------------------------------------------*/
5438 WL_EXPORT   int
5439 ico_window_mgr_is_visible(struct uifw_win_surface *usurf)
5440 {
5441     if ((usurf->visible == 0) || (usurf->surface == NULL) || (usurf->mapped == 0))  {
5442         return 0;
5443     }
5444     if (usurf->win_layer->visible == 0) {
5445         return -1;
5446     }
5447     return 1;
5448 }
5449
5450 /*--------------------------------------------------------------------------*/
5451 /**
5452  * @brief   ico_window_mgr_active_surface: set active surface
5453  *
5454  * @param[in]   surface     Weston surface
5455  * @return      none
5456  */
5457 /*--------------------------------------------------------------------------*/
5458 WL_EXPORT   void
5459 ico_window_mgr_active_surface(struct weston_surface *surface)
5460 {
5461     struct uifw_win_surface *usurf;
5462
5463     /* find surface         */
5464     usurf = find_uifw_win_surface_by_ws(surface);
5465     if (! usurf) {
5466         uifw_trace("ico_window_mgr_active_surface: Enter(%08x)", (int)surface);
5467         uifw_trace("ico_window_mgr_active_surface: Leave(Not Exist)");
5468         return;
5469     }
5470     uifw_trace("ico_window_mgr_active_surface: Enter(%08x)", usurf->surfaceid);
5471
5472     if ((usurf != _ico_win_mgr->active_pointer_usurf) ||
5473         (usurf != _ico_win_mgr->active_keyboard_usurf)) {
5474
5475         /* set weston active surface    */
5476         win_mgr_set_active(usurf, ICO_WINDOW_MGR_ACTIVE_POINTER |
5477                            ICO_WINDOW_MGR_ACTIVE_KEYBOARD);
5478         /* send active event to manager     */
5479         (void) ico_win_mgr_send_to_mgr(ICO_WINDOW_MGR_WINDOW_ACTIVE,
5480                                     usurf, ICO_WINDOW_MGR_ACTIVE_SELECTED, 0,0,0,0);
5481     }
5482     uifw_trace("ico_window_mgr_active_surface: Leave(OK)");
5483 }
5484
5485 /*--------------------------------------------------------------------------*/
5486 /**
5487  * @brief   ico_window_mgr_set_hook_animation: set animation hook routine
5488  *
5489  * @param[in]   hook_animation  hook routine
5490  * @return      none
5491  */
5492 /*--------------------------------------------------------------------------*/
5493 WL_EXPORT   void
5494 ico_window_mgr_set_hook_animation(int (*hook_animation)(const int op, void *data))
5495 {
5496     win_mgr_hook_animation = hook_animation;
5497 }
5498
5499 /*--------------------------------------------------------------------------*/
5500 /**
5501  * @brief   ico_window_mgr_set_hook_change: set input region hook routine
5502  *
5503  * @param[in]   hook_change     hook routine
5504  * @return      none
5505  */
5506 /*--------------------------------------------------------------------------*/
5507 WL_EXPORT   void
5508 ico_window_mgr_set_hook_change(void (*hook_change)(struct uifw_win_surface *usurf))
5509 {
5510     win_mgr_hook_change = hook_change;
5511 }
5512
5513 /*--------------------------------------------------------------------------*/
5514 /**
5515  * @brief   ico_window_mgr_set_hook_destory: set input region hook routine
5516  *
5517  * @param[in]   hook_destroy    hook routine
5518  * @return      none
5519  */
5520 /*--------------------------------------------------------------------------*/
5521 WL_EXPORT   void
5522 ico_window_mgr_set_hook_destory(void (*hook_destroy)(struct uifw_win_surface *usurf))
5523 {
5524     win_mgr_hook_destory = hook_destroy;
5525 }
5526
5527 /*--------------------------------------------------------------------------*/
5528 /**
5529  * @brief   ico_window_mgr_set_hook_inputregion: set input region hook routine
5530  *
5531  * @param[in]   hook_inputregion    hook routine
5532  * @return      none
5533  */
5534 /*--------------------------------------------------------------------------*/
5535 WL_EXPORT   void
5536 ico_window_mgr_set_hook_inputregion(
5537         void (*hook_inputregion)(int set, struct uifw_win_surface *usurf,
5538                                  int32_t x, int32_t y, int32_t width,
5539                                  int32_t height, int32_t hotspot_x, int32_t hotspot_y,
5540                                  int32_t cursor_x, int32_t cursor_y, int32_t cursor_width,
5541                                  int32_t cursor_height, uint32_t attr))
5542 {
5543     win_mgr_hook_inputregion = hook_inputregion;
5544 }
5545
5546 /*--------------------------------------------------------------------------*/
5547 /**
5548  * @brief   module_init: initialize ico_window_mgr
5549  *                       this function called from ico_pluign_loader
5550  *
5551  * @param[in]   es          weston compositor
5552  * @param[in]   argc        number of arguments(unused)
5553  * @param[in]   argv        argument list(unused)
5554  * @return      result
5555  * @retval      0           sccess
5556  * @retval      -1          error
5557  */
5558 /*--------------------------------------------------------------------------*/
5559 WL_EXPORT   int
5560 module_init(struct weston_compositor *ec, int *argc, char *argv[])
5561 {
5562     int     nodeId;
5563     int     i;
5564     int     idx;
5565     struct weston_output *output;
5566     struct weston_config_section *section;
5567     char    *displayno = NULL;
5568     char    *p, *pp;
5569     char    *wrkstrp;
5570     char    wkbuf[ICO_UIFW_MAX_SUBJECT_NAME];
5571     struct ico_api_permit   *accp;
5572     struct ico_api_permit   *accp_next;
5573     int     same_accp;
5574     struct wl_event_loop *loop;
5575
5576     uifw_info("ico_window_mgr: Enter(module_init)");
5577
5578     /* get ivi debug level                      */
5579     section = weston_config_get_section(ec->config, "ivi-option", NULL, NULL);
5580     if (section)    {
5581         weston_config_section_get_int(section, "flag", &_ico_ivi_option_flag, 0);
5582         weston_config_section_get_int(section, "log", &_ico_ivi_debug_level, 3);
5583     }
5584
5585     /* get display number                       */
5586     section = weston_config_get_section(ec->config, "ivi-display", NULL, NULL);
5587     if (section)    {
5588         weston_config_section_get_string(section, "displayno", &displayno, NULL);
5589         weston_config_section_get_int(section, "inputpanel",
5590                                       &_ico_ivi_inputpanel_display, 0);
5591     }
5592
5593     /* get layer id                             */
5594     section = weston_config_get_section(ec->config, "ivi-layer", NULL, NULL);
5595     if (section)    {
5596         weston_config_section_get_int(section, "default", &_ico_ivi_default_layer, 1);
5597         weston_config_section_get_int(section, "background", &_ico_ivi_background_layer, 0);
5598         weston_config_section_get_int(section, "touch", &_ico_ivi_touch_layer, 101);
5599         weston_config_section_get_int(section, "cursor", &_ico_ivi_cursor_layer, 102);
5600         weston_config_section_get_int(section, "startup", &_ico_ivi_startup_layer, 103);
5601         weston_config_section_get_string(section, "inputpaneldeco", &wrkstrp, NULL);
5602         if (wrkstrp)  {
5603             p = strtok(wrkstrp, ";,");
5604             if (p)  {
5605                 _ico_ivi_inputdeco_mag = strtol(p, (char **)0, 0);
5606                 p = strtok(NULL, ";,");
5607                 if (p)  {
5608                     _ico_ivi_inputdeco_diff = strtol(p, (char **)0, 0);
5609                 }
5610             }
5611             free(wrkstrp);
5612         }
5613     }
5614     if (_ico_ivi_inputdeco_mag < 20)    _ico_ivi_inputdeco_mag = 100;
5615
5616     /* get animation default                    */
5617     section = weston_config_get_section(ec->config, "ivi-animation", NULL, NULL);
5618     if (section)    {
5619         weston_config_section_get_string(section, "default", &_ico_ivi_animation_name, NULL);
5620         weston_config_section_get_string(section, "inputpanel",
5621                                          &_ico_ivi_inputpanel_animation, NULL);
5622         weston_config_section_get_int(section, "time", &_ico_ivi_animation_time, 500);
5623         weston_config_section_get_int(section, "fps", &_ico_ivi_animation_fps, 30);
5624         if (_ico_ivi_animation_name)    {
5625             p = strtok(_ico_ivi_animation_name, ";,");
5626             if (p)  {
5627                 p = strtok(NULL, ";.");
5628                 if (p)  {
5629                     _ico_ivi_animation_time = strtol(p, (char **)0, 0);
5630                     p = strtok(NULL, ";.");
5631                     if (p)  {
5632                         _ico_ivi_animation_fps = strtol(p, (char **)0, 0);
5633                     }
5634                 }
5635             }
5636         }
5637         if (_ico_ivi_inputpanel_animation)  {
5638             p = strtok(_ico_ivi_inputpanel_animation, ";,");
5639             if (p)  {
5640                 p = strtok(NULL, ",;");
5641                 if (p)  {
5642                     _ico_ivi_inputpanel_anima_time = strtol(p, (char **)0, 0);
5643                 }
5644             }
5645         }
5646     }
5647     if (_ico_ivi_animation_name == NULL)
5648         _ico_ivi_animation_name = (char *)"fade";
5649     if (_ico_ivi_inputpanel_animation == NULL)
5650         _ico_ivi_inputpanel_animation = (char *)"fade";
5651     if (_ico_ivi_animation_time < 100)  _ico_ivi_animation_time = 500;
5652     if (_ico_ivi_animation_fps < 3)     _ico_ivi_animation_fps = 30;
5653     if (_ico_ivi_inputpanel_anima_time < 100)
5654         _ico_ivi_inputpanel_anima_time = _ico_ivi_animation_time;
5655
5656     /* get thumbnail maximum frame rate     */
5657     section = weston_config_get_section(ec->config, "ivi-thumbnail", NULL, NULL);
5658     if (section)    {
5659         weston_config_section_get_int(section, "gpu_accel_fps",
5660                                       &_ico_ivi_map_framerate_gpu, 30);
5661         if ((_ico_ivi_map_framerate_gpu <= 0) || (_ico_ivi_map_framerate_gpu > 60)) {
5662             _ico_ivi_map_framerate_gpu = 30;
5663         }
5664         weston_config_section_get_int(section, "shm_buffer_fps",
5665                                       &_ico_ivi_map_framerate_shm, 5);
5666         if ((_ico_ivi_map_framerate_shm <= 0) || (_ico_ivi_map_framerate_shm > 10)) {
5667             _ico_ivi_map_framerate_shm = 5;
5668         }
5669         weston_config_section_get_int(section, "no_accel_fps",
5670                                       &_ico_ivi_map_framerate_pixel, 10);
5671         if ((_ico_ivi_map_framerate_pixel <= 0) || (_ico_ivi_map_framerate_pixel > 20)) {
5672             _ico_ivi_map_framerate_pixel = 10;
5673         }
5674     }
5675
5676     /* create ico_window_mgr management table   */
5677     _ico_win_mgr = (struct ico_win_mgr *)malloc(sizeof(struct ico_win_mgr));
5678     if (_ico_win_mgr == NULL)   {
5679         uifw_error("ico_window_mgr: malloc failed");
5680         return -1;
5681     }
5682
5683     memset(_ico_win_mgr, 0, sizeof(struct ico_win_mgr));
5684
5685     /* get access control configuration         */
5686     section = weston_config_get_section(ec->config, "ivi-access-control", NULL, NULL);
5687     accp_next = NULL;
5688     if (section)    {
5689         for (idx = 0; ; idx++)  {
5690             sprintf(wkbuf, "access_api.%d", idx);
5691             weston_config_section_get_string(section, wkbuf, &wrkstrp, NULL);
5692             if (wrkstrp == NULL)    {
5693                 if (idx <= 0)   continue;
5694                 break;
5695             }
5696             p = wrkstrp;
5697             for (i = 0; i < (ICO_UIFW_MAX_SUBJECT_NAME-1); i++, p++) {
5698                 if ((*p == 0) || (*p == ';'))   break;
5699                 wkbuf[i] = *p;
5700             }
5701             if ((i <= 0) || (*p == 0))  {
5702                 uifw_error("ico_window_mgr: illegal config format[%s]", wrkstrp);
5703                 free(wrkstrp);
5704                 continue;
5705             }
5706             wkbuf[i] = 0;
5707             accp = _ico_win_mgr->access_config;
5708             while (accp)   {
5709                 if (strcmp(wkbuf, accp->permit.subject) == 0)  break;
5710                 accp = accp->next;
5711             }
5712             if (! accp) {
5713                 accp = (struct ico_api_permit *) malloc(sizeof(struct ico_api_permit));
5714                 if (! accp) {
5715                     uifw_error("ico_window_mgr: malloc failed");
5716                     continue;
5717                 }
5718                 same_accp = 0;
5719                 memset(accp, 0, sizeof(struct ico_api_permit));
5720                 strcpy(accp->permit.subject, wkbuf);
5721             }
5722             else    {
5723                 same_accp = 1;
5724             }
5725
5726             while (1)   {
5727                 pp = p + 1;
5728                 for (p++; (*p != 0) && (*p != ','); p++)    ;
5729                 if ((strcasecmp(pp, "all") == 0) || (strcmp(pp, "*") == 0)) {
5730                     accp->permit.api_permit = 0xffffffff;
5731                     break;
5732                 }
5733                 if (strncasecmp(pp, "ico_", 4) == 0)            pp += 4;
5734                 if (strncasecmp(pp, "window_mgr_", 11) == 0)    pp += 11;
5735                 if (strncasecmp(pp, "input_mgr_", 10) == 0)     pp += 10;
5736                 if (strncasecmp(pp, "exinput_", 8) == 0)        pp += 8;
5737                 for (i = 0; access_api_table[i].name[0]; i++)   {
5738                     if (strncasecmp(pp, access_api_table[i].name, p - pp) == 0) {
5739                         accp->permit.api_permit |= access_api_table[i].api;
5740                         break;
5741                     }
5742                 }
5743                 if (*p == 0)    break;
5744             }
5745             free(wrkstrp);
5746             if (same_accp == 0) {
5747                 if (accp_next)  {
5748                     accp_next->next = accp;
5749                 }
5750                 else    {
5751                     _ico_win_mgr->access_config = accp;
5752                 }
5753                 accp_next = accp;
5754             }
5755             if (strcmp(accp->permit.subject, "*") == 0) {
5756                 _ico_win_mgr->default_access |= accp->permit.api_permit;
5757             }
5758         }
5759         accp = _ico_win_mgr->access_config;
5760         while (accp)   {
5761             accp->permit.api_permit |= _ico_win_mgr->default_access;
5762             accp = accp->next;
5763         }
5764     }
5765     else    {
5766         uifw_trace("ico_window_mgr: no access control in weston.ini");
5767     }
5768
5769     _ico_win_mgr->surfaceid_map = (uint16_t *) malloc(INIT_SURFACE_IDS/8);
5770     if (! _ico_win_mgr->surfaceid_map)  {
5771         uifw_error("ico_window_mgr: malloc failed");
5772         return -1;
5773     }
5774     memset(_ico_win_mgr->surfaceid_map, 0, INIT_SURFACE_IDS/8);
5775
5776     _ico_win_mgr->compositor = ec;
5777
5778     _ico_win_mgr->surfaceid_max = INIT_SURFACE_IDS;
5779     _ico_win_mgr->surfaceid_count = INIT_SURFACE_IDS;
5780
5781     uifw_trace("ico_window_mgr: wl_global_create(bind_ico_win_mgr)");
5782     if (wl_global_create(ec->wl_display, &ico_window_mgr_interface, 1,
5783                          _ico_win_mgr, bind_ico_win_mgr) == NULL)  {
5784         uifw_error("ico_window_mgr: Error(wl_global_create)");
5785         return -1;
5786     }
5787
5788     wl_list_init(&_ico_win_mgr->client_list);
5789     wl_list_init(&_ico_win_mgr->manager_list);
5790     wl_list_init(&_ico_win_mgr->ivi_layer_list);
5791     wl_list_init(&_ico_win_mgr->map_list);
5792
5793     _ico_win_mgr->free_maptable = NULL;
5794
5795     /* create display list                  */
5796     if (displayno != NULL)   {
5797         p = displayno;
5798     }
5799     else    {
5800         p = NULL;
5801     }
5802     _ico_num_nodes = 0;
5803     wl_list_for_each (output, &ec->output_list, link) {
5804         wl_list_init(&_ico_win_mgr->map_animation[_ico_num_nodes].link);
5805         _ico_win_mgr->map_animation[_ico_num_nodes].frame = win_mgr_check_mapsurface;
5806         wl_list_insert(output->animation_list.prev,
5807                        &_ico_win_mgr->map_animation[_ico_num_nodes].link);
5808         _ico_num_nodes++;
5809         if (_ico_num_nodes >= ICO_IVI_MAX_DISPLAY)   break;
5810     }
5811     memset(&_ico_node_table[0], 0, sizeof(_ico_node_table));
5812     i = 0;
5813     wl_list_for_each (output, &ec->output_list, link) {
5814         p = strtok(p, ",");
5815         if (p)  {
5816             idx = strtol(p, (char **)0, 0);
5817             uifw_trace("ico_window_mgr: config Display.%d is weston display.%d", i, idx);
5818             p = NULL;
5819             if ((idx < 0) || (idx >= _ico_num_nodes))   {
5820                 idx = i;
5821             }
5822         }
5823         else    {
5824             idx = i;
5825         }
5826         if (_ico_node_table[idx].node)  {
5827             for (idx = 0; idx < _ico_num_nodes; idx++)  {
5828                 if (_ico_node_table[idx].node == 0) break;
5829             }
5830             if (idx >= _ico_num_nodes)  {
5831                 uifw_error("ico_window_mgr: number of display overflow");
5832                 idx = 0;
5833             }
5834         }
5835         _ico_node_table[idx].node = idx + 0x100;
5836         _ico_node_table[idx].displayno = i;
5837         _ico_node_table[idx].output = output;
5838         _ico_node_table[idx].disp_x = output->x;
5839         _ico_node_table[idx].disp_y = output->y;
5840         _ico_node_table[idx].disp_width = output->width;
5841         _ico_node_table[idx].disp_height = output->height;
5842         i ++;
5843         if (i >= _ico_num_nodes) break;
5844     }
5845     idx = 0;
5846     for (i = 0; i < _ico_num_nodes; i++)    {
5847         _ico_node_table[i].node &= 0x0ff;
5848         uifw_info("ico_window_mgr: Display.%d no=%d x/y=%d/%d w/h=%d/%d",
5849                   i, _ico_node_table[i].displayno,
5850                   _ico_node_table[i].disp_x, _ico_node_table[i].disp_y,
5851                   _ico_node_table[i].disp_width, _ico_node_table[i].disp_height);
5852     }
5853     if (displayno)  free(displayno);
5854
5855     if (_ico_ivi_inputpanel_display >= _ico_num_nodes)  {
5856         _ico_ivi_inputpanel_display = 0;
5857     }
5858     uifw_info("ico_window_mgr: inputpanel_display=%d", _ico_ivi_inputpanel_display);
5859
5860     /* set default display to ico_ivi_shell */
5861     ivi_shell_set_default_display(_ico_node_table[_ico_ivi_inputpanel_display].output);
5862
5863     /* my node Id ... this version fixed 0  */
5864     nodeId = ico_ivi_get_mynode();
5865
5866     _ico_win_mgr->surface_head = ICO_IVI_SURFACEID_BASE(nodeId);
5867     uifw_trace("ico_window_mgr: NoedId=%04x SurfaceIdBase=%08x",
5868                 nodeId, _ico_win_mgr->surface_head);
5869
5870     /* get seat for touch down counter check    */
5871     touch_check_seat = container_of(ec->seat_list.next, struct weston_seat, link);
5872     loop = wl_display_get_event_loop(ec->wl_display);
5873     _ico_win_mgr->wait_mapevent =
5874             wl_event_loop_add_timer(loop, win_mgr_timer_mapsurface, NULL);
5875     wl_event_source_timer_update(_ico_win_mgr->wait_mapevent, 1000);
5876
5877     /* Hook to IVI-Shell                            */
5878     ico_ivi_shell_hook_bind(win_mgr_bind_client);
5879     ico_ivi_shell_hook_unbind(win_mgr_unbind_client);
5880     ico_ivi_shell_hook_create(win_mgr_register_surface);
5881     ico_ivi_shell_hook_destroy(win_mgr_destroy_surface);
5882     ico_ivi_shell_hook_map(win_mgr_surface_map);
5883     ico_ivi_shell_hook_configure(win_mgr_shell_configure);
5884     ico_ivi_shell_hook_select(win_mgr_select_surface);
5885     ico_ivi_shell_hook_title(win_mgr_set_title);
5886     ico_ivi_shell_hook_move(win_mgr_surface_move);
5887     ico_ivi_shell_hook_show_layer(win_mgr_show_layer);
5888     ico_ivi_shell_hook_fullscreen(win_mgr_fullscreen);
5889
5890     uifw_info("ico_window_mgr: animation name=%s/%s time=%d/%d fps=%d",
5891               _ico_ivi_animation_name, _ico_ivi_inputpanel_animation,
5892               _ico_ivi_animation_time, _ico_ivi_inputpanel_anima_time,
5893               _ico_ivi_animation_fps);
5894     uifw_info("ico_window_mgr: input panel mag=%d%% diff=%d",
5895               _ico_ivi_inputdeco_mag,_ico_ivi_inputdeco_diff);
5896     uifw_info("ico_window_mgr: layer default=%d background=%d",
5897               _ico_ivi_default_layer, _ico_ivi_background_layer);
5898     uifw_info("ico_window_mgr: layer touch=%d cursor=%d startup=%d",
5899               _ico_ivi_touch_layer, _ico_ivi_cursor_layer, _ico_ivi_startup_layer);
5900     uifw_info("ico_window_mgr: thumbnail framerate gpu_accel=%d shm_buff=%d no_accel=%d",
5901               _ico_ivi_map_framerate_gpu, _ico_ivi_map_framerate_shm,
5902               _ico_ivi_map_framerate_pixel);
5903     uifw_info("ico_window_mgr: option flag=0x%04x log level=%d debug flag=0x%04x",
5904               _ico_ivi_option_flag, _ico_ivi_debug_level & 0x0ffff,
5905               (_ico_ivi_debug_level >> 16) & 0x0ffff);
5906     accp = _ico_win_mgr->access_config;
5907     while (accp)    {
5908         uifw_info("ico_window_mgr: access_control %s=%08x",
5909                   accp->permit.subject, accp->permit.api_permit);
5910         accp = accp->next;
5911     }
5912
5913     /* get GPU type for H/W support of the thumbnail acquisition    */
5914     if ((ico_ivi_optionflag() & ICO_IVI_OPTION_GPU_DEPEND) == 0)    {
5915         /* can not use GPU H/W dependent acceleration   */
5916         _ico_ivi_gpu_type = ICO_GPUTYPE_NOACCELERATION;
5917         uifw_info("ico_window_mgr: GPU type=No Acceleration by option flag");
5918     }
5919     else    {
5920         _ico_ivi_gpu_type = ICO_GPUTYPE_NOACCELERATION;
5921
5922         p = (char *)glGetString(GL_RENDERER);
5923         if (p)  {
5924             uifw_info("ico_window_mgr: Renderer=<%s>", p);
5925             for (i = 0; ico_window_mgr_gpu_type[i].gpu_type; i++)   {
5926                 if (strncmp(p, ico_window_mgr_gpu_type[i].gpu_name,
5927                             strlen(ico_window_mgr_gpu_type[i].gpu_name)) == 0)  {
5928                     _ico_ivi_gpu_type = ico_window_mgr_gpu_type[i].gpu_type;
5929                     uifw_info("ico_window_mgr: GPU type=Acceleration %d", _ico_ivi_gpu_type);
5930                     break;
5931                 }
5932             }
5933         }
5934         if (_ico_ivi_gpu_type == ICO_GPUTYPE_NOACCELERATION)    {
5935             uifw_info("ico_window_mgr: GPU type=No Acceleration(can not find GPU)");
5936         }
5937     }
5938     uifw_info("ico_window_mgr: Leave(module_init)");
5939
5940     return 0;
5941 }