Bugfix for TIVI-1997, TIVI-2244, TIVI-2256, A ivi cursor layer may pick up a touch...
[profile/ivi/ico-uxf-weston-plugin.git] / src / ico_input_mgr.c
1 /*
2  * Copyright © 2010-2011 Intel Corporation
3  * Copyright © 2008-2011 Kristian Høgsberg
4  * Copyright © 2013 TOYOTA MOTOR CORPORATION.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the copyright holders not be used in
11  * advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission.  The copyright holders make
13  * no representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24 /**
25  * @brief   Multi Input Manager (Weston(Wayland) PlugIn)
26  *
27  * @date    Jul-26-2013
28  */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdbool.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <linux/input.h>
36 #include <assert.h>
37 #include <signal.h>
38 #include <math.h>
39 #include <time.h>
40 #include <sys/types.h>
41
42 #include <pixman.h>
43 #include <wayland-server.h>
44 #include <weston/compositor.h>
45 #include "ico_ivi_common_private.h"
46 #include "ico_ivi_shell_private.h"
47 #include "ico_input_mgr.h"
48 #include "ico_window_mgr_private.h"
49 #include "ico_window_mgr-server-protocol.h"
50 #include "ico_input_mgr-server-protocol.h"
51
52 /* degine maximum length                */
53 #define ICO_MINPUT_DEVICE_LEN           32
54 #define ICO_MINPUT_SW_LEN               20
55 #define ICO_MINPUT_MAX_CODES            20
56
57 /* structure definition */
58 struct uifw_region_mng;
59
60 /* working table of Multi Input Manager */
61 struct ico_input_mgr {
62     struct weston_compositor *compositor;   /* Weston Compositor                    */
63     struct wl_list  ictl_list;              /* Input Controller List                */
64     struct wl_list  app_list;               /* application List                     */
65     struct wl_list  dev_list;               /* pseudo device List                   */
66     struct wl_list  free_region;            /* free input region table list         */
67     struct weston_seat *seat;               /* input seat                           */
68     struct wl_resource *inputmgr;
69 };
70
71 /* Input Switch Table                   */
72 struct ico_ictl_code {
73     uint16_t    code;                       /* input code numner                    */
74     char        name[ICO_MINPUT_SW_LEN];    /* input code name                      */
75 };
76
77 struct ico_ictl_input {
78     struct wl_list link;                    /* link                                 */
79     char        swname[ICO_MINPUT_SW_LEN];  /* input switch name                    */
80     int32_t     input;                      /* input Id                             */
81     uint16_t    fix;                        /* fixed assign to application          */
82     uint16_t    ncode;                      /* number of codes                      */
83     struct ico_ictl_code code[ICO_MINPUT_MAX_CODES];   /* codes                     */
84     struct ico_app_mgr  *app;               /* send event tagret application        */
85 };
86
87 /* Input Controller Management Table    */
88 struct ico_ictl_mgr {
89     struct wl_list link;                    /* link                                 */
90     struct wl_client    *client;            /* client                               */
91     struct wl_resource  *mgr_resource;      /* resource as manager                  */
92     char    device[ICO_MINPUT_DEVICE_LEN];  /* device name                          */
93     int     type;                           /* device type                          */
94     struct wl_list ico_ictl_input;          /* list of input switchs                */
95 };
96
97 /* Application Management Table */
98 struct ico_app_mgr {
99     struct wl_list link;                    /* link                                 */
100     struct wl_client    *client;            /* client                               */
101     struct wl_resource  *resource;          /* resource for send event              */
102     struct wl_resource  *mgr_resource;      /* resource as manager(if NULL, client) */
103     char    appid[ICO_IVI_APPID_LENGTH];    /* application id                       */
104 };
105
106 /* Pseudo Input Device Control Flags    */
107 #define EVENT_MOTION        0x01            /* motion event                         */
108 #define EVENT_BUTTON        0x02            /* button event                         */
109 #define EVENT_TOUCH         0x03            /* touch event                          */
110 #define EVENT_KEY           0x04            /* key event                            */
111 #define EVENT_PENDING       0xff            /* pending event input                  */
112
113 #define PENDING_X           0x01            /* pending X coordinate                 */
114 #define PENDING_Y           0x02            /* pending Y coordinate                 */
115
116 /* Pseudo Input Device Table    */
117 struct uifw_input_device    {
118     struct wl_list  link;                   /* link to next device                  */
119     uint16_t    type;                       /* device type                          */
120     uint16_t    no;                         /* device number                        */
121     int         disp_x;                     /* display X coordinate                 */
122     int         disp_y;                     /* display Y coordinate                 */
123     int         x;                          /* current X coordinate                 */
124     int         y;                          /* current Y coordinate                 */
125     int         pend_x;                     /* pending X coordinate                 */
126     int         pend_y;                     /* pending Y coordinate                 */
127     uint16_t    node;                       /* display number                       */
128     uint16_t    pending;                    /* pending flag                         */
129     struct weston_surface *grab;            /* current grab surface                 */
130 };
131
132 /* Input Region Table           */
133 struct uifw_region_mng  {
134     struct wl_list  link;                   /* link pointer                         */
135     struct ico_uifw_input_region region;    /* input region                         */
136 };
137
138 /* prototype of static function */
139                                             /* bind input manager form manager      */
140 static void ico_control_bind(struct wl_client *client, void *data,
141                              uint32_t version, uint32_t id);
142                                             /* unbind input manager form manager    */
143 static void ico_control_unbind(struct wl_resource *resource);
144                                             /* bind input manager form input controller*/
145 static void ico_device_bind(struct wl_client *client, void *data,
146                             uint32_t version, uint32_t id);
147                                             /* unbind input manager form input controller*/
148 static void ico_device_unbind(struct wl_resource *resource);
149                                             /* bind input manager(form application) */
150 static void ico_exinput_bind(struct wl_client *client, void *data,
151                              uint32_t version, uint32_t id);
152                                             /* unbind input manager(form application)*/
153 static void ico_exinput_unbind(struct wl_resource *resource);
154
155                                             /* find ictl manager by device name     */
156 static struct ico_ictl_mgr *find_ictlmgr_by_device(const char *device);
157                                             /* find ictl input switch by input Id   */
158 static struct ico_ictl_input *find_ictlinput_by_input(struct ico_ictl_mgr *pIctlMgr,
159                                                       const int32_t input);
160                                             /* find app manager by application Id   */
161 static struct ico_app_mgr *find_app_by_appid(const char *appid);
162                                             /* add input event to application       */
163 static void ico_mgr_add_input_app(struct wl_client *client, struct wl_resource *resource,
164                                   const char *appid, const char *device, int32_t input,
165                                   int32_t fix, int32_t keycode);
166                                             /* delete input event to application    */
167 static void ico_mgr_del_input_app(struct wl_client *client, struct wl_resource *resource,
168                                   const char *appid, const char *device, int32_t input);
169                                             /* send input event from manager        */
170 static void ico_mgr_send_input_event(struct wl_client *client, struct wl_resource *resource,
171                                      const char *target, uint32_t surfaceid, int32_t type,
172                                      int32_t deviceno, uint32_t time,
173                                      int32_t code, int32_t value);
174                                             /* set input region                     */
175 static void ico_mgr_set_input_region(struct wl_client *client, struct wl_resource *resource,
176                                      const char *target, int32_t x, int32_t y,
177                                      int32_t width, int32_t height, int32_t hotspot_x,
178                                      int32_t hotspot_y, int32_t cursor_x, int32_t cursor_y,
179                                      int32_t cursor_width, int32_t cursor_height,
180                                      uint32_t attr);
181                                             /* unset input region                   */
182 static void ico_mgr_unset_input_region(struct wl_client *client,
183                                        struct wl_resource *resource,
184                                        const char *taret, int32_t x, int32_t y,
185                                        int32_t width, int32_t height);
186                                             /* input region set/unset               */
187 static void ico_set_input_region(int set, struct uifw_win_surface *usurf,
188                                  int32_t x, int32_t y, int32_t width, int32_t height,
189                                  int32_t hotspot_x, int32_t hotspot_y, int32_t cursor_x,
190                                  int32_t cursor_y, int32_t cursor_width,
191                                  int32_t cursor_height, uint32_t attr);
192                                             /* create and regist Input Controller table*/
193 static void ico_device_configure_input(struct wl_client *client,
194                                        struct wl_resource *resource, const char *device,
195                                        int32_t type, const char *swname, int32_t input,
196                                        const char *codename, int32_t code);
197                                             /* add input to from Input Controller table*/
198 static void ico_device_configure_code(struct wl_client *client,
199                                       struct wl_resource *resource, const char *device,
200                                       int32_t input, const char *codename, int32_t code);
201                                             /* device input event                   */
202 static void ico_device_input_event(struct wl_client *client, struct wl_resource *resource,
203                                    uint32_t time, const char *device,
204                                    int32_t input, int32_t code, int32_t state);
205 static void ico_input_send_region_event(struct wl_array *array);
206
207 /* definition of Wayland protocol       */
208 /* Input Manager Control interface      */
209 static const struct ico_input_mgr_control_interface ico_input_mgr_implementation = {
210     ico_mgr_add_input_app,
211     ico_mgr_del_input_app,
212     ico_mgr_send_input_event
213 };
214
215 /* Extended Input interface             */
216 static const struct ico_exinput_interface ico_exinput_implementation = {
217     ico_mgr_set_input_region,
218     ico_mgr_unset_input_region
219 };
220
221 /* Input Controller Device interface    */
222 static const struct ico_input_mgr_device_interface input_mgr_ictl_implementation = {
223     ico_device_configure_input,
224     ico_device_configure_code,
225     ico_device_input_event
226 };
227
228 /* definition of class variable */
229 struct ico_input_mgr    *pInputMgr = NULL;
230
231 /* implementation */
232 /*--------------------------------------------------------------------------*/
233 /**
234  * @brief   ico_mgr_add_input_app: add input event to application from HomeScreen.
235  *
236  * @param[in]   client          client(HomeScreen)
237  * @param[in]   resource        resource of request
238  * @param[in]   appid           target application id
239  * @param[in]   device          device name
240  * @param[in]   input           input switch number
241  * @param[in]   fix             fix to application(1=fix,0=general)
242  * @param[in]   keycode         switch map to keyboard operation(0=not map to keyboard)
243  * @return      none
244  */
245 /*--------------------------------------------------------------------------*/
246 static void
247 ico_mgr_add_input_app(struct wl_client *client, struct wl_resource *resource,
248                       const char *appid, const char *device, int32_t input,
249                       int32_t fix, int32_t keycode)
250 {
251     uifw_trace("ico_mgr_add_input_app: Enter(appid=%s,dev=%s,input=%d,fix=%d,key=%d)",
252                appid, device, input, fix, keycode);
253
254     struct ico_ictl_mgr     *pIctlMgr;
255     struct ico_ictl_input   *pInput;
256     struct ico_app_mgr      *pAppMgr;
257
258     pIctlMgr = find_ictlmgr_by_device(device);
259     if (! pIctlMgr) {
260         /* not configure input controller, create   */
261         ico_device_configure_input(NULL, NULL, device, 0, NULL, input, NULL, 0);
262         pIctlMgr = find_ictlmgr_by_device(device);
263         if (! pIctlMgr) {
264             uifw_error("ico_mgr_add_input_app: Leave(No Memory)");
265             return;
266         }
267     }
268     pInput = find_ictlinput_by_input(pIctlMgr, input);
269     if (! pInput)   {
270         /* not configure input switch, create   */
271         ico_device_configure_input(NULL, NULL, device, 0, NULL, input, NULL, 0);
272         pInput = find_ictlinput_by_input(pIctlMgr, input);
273         if (! pInput)   {
274             uifw_error("ico_mgr_add_input_app: Leave(No Memory)");
275             return;
276         }
277     }
278
279     /* find application         */
280     pAppMgr = find_app_by_appid(appid);
281     if (! pAppMgr)  {
282         /* create Application Management Table  */
283         pAppMgr = (struct ico_app_mgr *)malloc(sizeof(struct ico_app_mgr));
284         if (! pAppMgr)  {
285             uifw_error("ico_mgr_add_input_app: Leave(No Memory)");
286             return;
287         }
288         memset(pAppMgr, 0, sizeof(struct ico_app_mgr));
289         strncpy(pAppMgr->appid, appid, sizeof(pAppMgr->appid)-1);
290         wl_list_insert(pInputMgr->app_list.prev, &pAppMgr->link);
291     }
292
293     pInput->app = pAppMgr;
294     pInput->fix = fix;
295     uifw_trace("ico_mgr_add_input_app: Leave(%s.%s[%d] assign to %s)",
296                pIctlMgr->device, pInput->swname ? pInput->swname : "(NULL)", input,
297                pAppMgr->appid);
298 }
299
300 /*--------------------------------------------------------------------------*/
301 /**
302  * @brief   ico_mgr_del_input_app: delete input event at application from HomeScreen.
303  *
304  * @param[in]   client          client(HomeScreen)
305  * @param[in]   resource        resource of request
306  * @param[in]   appid           target application id,
307  *                              if NULL, all applictions without fixed assign switch
308  * @param[in]   device          device name
309  *                              if NULL, all device without fixed assign switch
310  * @param[in]   input           input switch number
311  *                              if -1, all input without fixed assign switch
312  * @return      none
313  */
314 /*--------------------------------------------------------------------------*/
315 static void
316 ico_mgr_del_input_app(struct wl_client *client, struct wl_resource *resource,
317                       const char *appid, const char *device, int32_t input)
318 {
319     uifw_trace("ico_mgr_del_input_app: Enter(appid=%s,dev=%s,input=%d)",
320                appid ? appid : "(NULL)", device ? device : "(NULL)", input);
321
322     int     alldev = 0;
323     struct ico_ictl_mgr     *pIctlMgr = NULL;
324     struct ico_ictl_input   *pInput = NULL;
325     struct ico_app_mgr      *pAppMgr;
326
327     if ((device != NULL) && (*device != 0)) {
328         pIctlMgr = find_ictlmgr_by_device(device);
329         if (! pIctlMgr) {
330             /* not configure input controller, NOP  */
331             uifw_trace("ico_mgr_del_input_app: Leave(%s dose not exist)", device);
332             return;
333         }
334         if (input >= 0) {
335             pInput = find_ictlinput_by_input(pIctlMgr, input);
336             if (! pInput)   {
337                 /* not configure input switch, NOP  */
338                 uifw_trace("ico_mgr_del_input_app: Leave(%s.%d dose not exist)",
339                            device, input);
340                 return;
341             }
342         }
343     }
344     else    {
345         alldev = 1;
346     }
347
348     /* find application         */
349     if ((appid != NULL) && (*appid != 0))   {
350         pAppMgr = find_app_by_appid(appid);
351         if (! pAppMgr)  {
352             /* application dose not exist, NOP  */
353             uifw_trace("ico_mgr_del_input_app: Leave(app.%s dose not exist)", appid);
354             return;
355         }
356         if (alldev == 0)    {
357             if (input >= 0) {
358                 if (pInput->app != pAppMgr) {
359                     /* not same application, NOP        */
360                     uifw_trace("ico_mgr_del_input_app: Leave(%s.%d not app.%s, current %s)",
361                                device, input, appid,
362                                pInput->app ? pInput->app->appid : "(NULL)");
363                     return;
364                 }
365                 uifw_trace("ico_mgr_del_input_app: Leave(%s.%d app.%s deleted)",
366                            device, input, appid);
367                 pInput->app = NULL;
368                 return;
369             }
370             else    {
371                 wl_list_for_each (pInput, &pIctlMgr->ico_ictl_input, link)  {
372                     if ((pInput->fix == 0) && (pInput->app == pAppMgr))   {
373                         uifw_trace("ico_mgr_del_input_app: %s.%d app.%s deleted",
374                                    pIctlMgr->device, pInput->input, appid);
375                         pInput->app = NULL;
376                     }
377                 }
378             }
379         }
380         else    {
381             /* reset all device without fixed assign    */
382             wl_list_for_each (pIctlMgr, &pInputMgr->ictl_list, link)    {
383                 wl_list_for_each (pInput, &pIctlMgr->ico_ictl_input, link)  {
384                     if ((pInput->fix == 0) && (pInput->app == pAppMgr))   {
385                         uifw_trace("ico_mgr_del_input_app: %s.%d app.%s deleted",
386                                    pIctlMgr->device, pInput->input, pInput->app->appid);
387                         pInput->app = NULL;
388                     }
389                 }
390             }
391         }
392     }
393     else    {
394         if (alldev == 0)    {
395             if (input >= 0) {
396                 if ((pInput->fix == 0) && (pInput->app != NULL))    {
397                     uifw_trace("ico_mgr_del_input_app: %s.%d app.%s deleted",
398                                pIctlMgr->device, pInput->input, pInput->app->appid);
399                     pInput->app = NULL;
400                 }
401             }
402             else    {
403                 wl_list_for_each (pInput, &pIctlMgr->ico_ictl_input, link)  {
404                     if ((pInput->fix == 0) && (pInput->app != NULL))    {
405                         uifw_trace("ico_mgr_del_input_app: %s.%d app.%s deleted",
406                                pIctlMgr->device, pInput->input, pInput->app->appid);
407                         pInput->app = NULL;
408                     }
409                 }
410             }
411         }
412         else    {
413             /* reset all application without fixed assign       */
414             wl_list_for_each (pIctlMgr, &pInputMgr->ictl_list, link)    {
415                 wl_list_for_each (pInput, &pIctlMgr->ico_ictl_input, link)  {
416                     if ((pInput->fix == 0) && (pInput->app != NULL))    {
417                         uifw_trace("ico_mgr_del_input_app: %s.%d app.%s deleted",
418                                    pIctlMgr->device, pInput->input, pInput->app->appid);
419                         pInput->app = NULL;
420                     }
421                 }
422             }
423         }
424     }
425     uifw_trace("ico_mgr_del_input_app: Leave");
426 }
427
428 /*--------------------------------------------------------------------------*/
429 /**
430  * @brief   ico_mgr_send_input_event: send input event from manager
431  *
432  * @param[in]   client          client(HomeScreen)
433  * @param[in]   resource        resource of request
434  * @param[in]   target          target window name and application id
435  * @param[in]   surfaceid       target surface id
436  * @param[in]   type            event device type
437  * @param[in]   deviceno        device number
438  * @param[in]   time            event time (if 0, generate)
439  * @param[in]   code            event code
440  * @param[in]   value           event value
441  * @return      none
442  */
443 /*--------------------------------------------------------------------------*/
444 static void
445 ico_mgr_send_input_event(struct wl_client *client, struct wl_resource *resource,
446                          const char *target, uint32_t surfaceid, int32_t type,
447                          int32_t deviceno, uint32_t time, int32_t code, int32_t value)
448 {
449     struct uifw_win_surface *usurf;         /* UIFW surface                 */
450     struct uifw_input_device *dev;          /* device control table         */
451     struct wl_resource      *cres;          /* event send client resource   */
452     struct wl_array dummy_array;            /* dummy array for wayland API  */
453     uint32_t    ctime;                      /* current time(ms)             */
454     uint32_t    serial;                     /* event serial number          */
455     int         event;                      /* event flag                   */
456     wl_fixed_t  fix_x;                      /* wayland X coordinate         */
457     wl_fixed_t  fix_y;                      /* wayland Y coordinate         */
458     wl_fixed_t  dx, dy;                     /* relative coordinate (dummy)  */
459     struct weston_surface   *grabsave;      /* real grab surface            */
460     int         keyboard_active;            /* keyborad active surface flag */
461
462 #if 0           /* too many log */
463     uifw_debug("ico_mgr_send_input_event: Enter(target=%s surf=%x dev=%d.%d "
464                "time=%d code=%x value=%d)",
465                target ? target : "(NULL)", surfaceid, type, deviceno,
466                time, code, value);
467 #endif
468
469     /* search pseudo input device           */
470     wl_list_for_each (dev, &pInputMgr->dev_list, link)  {
471         if ((dev->type == type) && (dev->no == deviceno))   break;
472     }
473     if (&dev->link == &pInputMgr->dev_list) {
474         /* device not exist, create new device  */
475         uifw_trace("ico_mgr_send_input_event: new device=%d no=%d", type, deviceno);
476         dev = malloc(sizeof(struct uifw_input_device));
477         if (! dev)  {
478             uifw_error("ico_mgr_send_input_event: Leave(No Memory)");
479             return;
480         }
481         memset(dev, 0, sizeof(struct uifw_input_device));
482         dev->type = type;
483         dev->no = deviceno;
484         if ((type == ICO_INPUT_MGR_DEVICE_TYPE_POINTER) ||
485             (type == ICO_INPUT_MGR_DEVICE_TYPE_TOUCH) ||
486             (type == ICO_INPUT_MGR_DEVICE_TYPE_HAPTIC)) {
487             ico_window_mgr_get_display_coordinate(deviceno, &dev->disp_x, &dev->disp_y);
488         }
489         wl_list_insert(pInputMgr->dev_list.prev, &dev->link);
490     }
491
492     /* convert pending event            */
493     event = 0;
494     if ((code & 0xffff0000) != (EV_REL << 16))  {
495         code &= 0x0000ffff;
496     }
497     switch (type)   {
498     case ICO_INPUT_MGR_DEVICE_TYPE_POINTER:         /* mouse        */
499     case ICO_INPUT_MGR_DEVICE_TYPE_TOUCH:           /* touch panel  */
500     case ICO_INPUT_MGR_DEVICE_TYPE_HAPTIC:          /* haptic       */
501         switch (code)   {
502         case ABS_X:
503             if (dev->pending & PENDING_Y)   {
504                 dev->x = value;
505                 dev->y = dev->pend_y;
506                 dev->pending = 0;
507                 dev->pend_x = 0;
508                 dev->pend_y = 0;
509                 event = EVENT_MOTION;
510             }
511             else    {
512                 dev->pend_x = value;
513                 dev->pending |= PENDING_X;
514                 event = EVENT_PENDING;
515             }
516             break;
517         case ABS_Y:
518             if (dev->pending & PENDING_X)   {
519                 dev->x = dev->pend_x;
520                 dev->y = value;
521                 dev->pending = 0;
522                 dev->pend_x = 0;
523                 dev->pend_y = 0;
524                 event = EVENT_MOTION;
525             }
526             else    {
527                 dev->pend_y = value;
528                 dev->pending |= PENDING_Y;
529                 event = EVENT_PENDING;
530             }
531             break;
532         case ABS_Z:
533             dev->x = (short)(value >> 16);
534             dev->y = (short)(value & 0x0ffff);
535             dev->pending = 0;
536             dev->pend_x = 0;
537             dev->pend_y = 0;
538             event = EVENT_MOTION;
539             break;
540         case ((EV_REL << 16) | REL_X):
541             if (dev->pending & PENDING_Y)   {
542                 dev->x += value;
543                 dev->y = dev->pend_y;
544                 dev->pending = 0;
545                 dev->pend_x = 0;
546                 dev->pend_y = 0;
547                 event = EVENT_MOTION;
548             }
549             else    {
550                 dev->pend_x = dev->x + value;
551                 dev->pending |= PENDING_X;
552                 event = EVENT_PENDING;
553             }
554             break;
555         case ((EV_REL << 16) | REL_Y):
556             if (dev->pending & PENDING_X)   {
557                 dev->x = dev->pend_x;
558                 dev->y += value;
559                 dev->pending = 0;
560                 dev->pend_x = 0;
561                 dev->pend_y = 0;
562                 event = EVENT_MOTION;
563             }
564             else    {
565                 dev->pend_x = dev->y + value;
566                 dev->pending |= PENDING_Y;
567                 event = EVENT_PENDING;
568             }
569             break;
570         case ((EV_REL << 16) | REL_Z):
571             dev->x += (short)(value >> 16);
572             dev->y += (short)(value & 0x0ffff);
573             dev->pending = 0;
574             dev->pend_x = 0;
575             dev->pend_y = 0;
576             event = EVENT_MOTION;
577             break;
578         default:
579             if (type == ICO_INPUT_MGR_DEVICE_TYPE_TOUCH)    {
580                 event = EVENT_TOUCH;
581             }
582             else    {
583                 event = EVENT_BUTTON;
584             }
585             break;
586         }
587         break;
588     default:
589         event = EVENT_KEY;
590         break;
591     }
592
593     if (event == EVENT_PENDING)   {
594 #if 0           /* too many log */
595         uifw_debug("ico_mgr_send_input_event: Leave(event pending)");
596 #endif
597         return;
598     }
599
600     if (time)   {
601         ctime = time;
602     }
603     else    {
604         ctime = weston_compositor_get_time();
605     }
606     fix_x = wl_fixed_from_int(dev->x + dev->disp_x);
607     fix_y = wl_fixed_from_int(dev->y + dev->disp_y);
608
609     if ((surfaceid == 0) && ((target == NULL) || (*target == 0) || (*target == ' ')))  {
610         /* send event to surface via weston */
611
612         /* disable the event transmission to a input layer  */
613         if (type == ICO_INPUT_MGR_DEVICE_TYPE_TOUCH)    {
614             ico_window_mgr_touch_layer(TRUE);
615         }
616
617         if ((event == EVENT_TOUCH) && (pInputMgr->seat->touch == NULL)) {
618             /* system has no touch, change to pointer event */
619             if (pInputMgr->seat->pointer == NULL)   {
620                 uifw_trace("ico_mgr_send_input_event: Leave(no touch & no pointerr)");
621                 return;
622             }
623             event = EVENT_BUTTON;
624             code = BTN_LEFT;
625         }
626         else if ((event == EVENT_BUTTON) && (pInputMgr->seat->pointer == NULL)) {
627             /* system has no pointer, change to touch event */
628             if (pInputMgr->seat->touch == NULL) {
629                 uifw_trace("ico_mgr_send_input_event: Leave(no touch & no pointerr)");
630                 return;
631             }
632             event = EVENT_TOUCH;
633         }
634
635         switch (event)    {
636         case EVENT_MOTION:
637             if ((type == ICO_INPUT_MGR_DEVICE_TYPE_TOUCH) &&
638                 (pInputMgr->seat->touch))   {
639                 if (pInputMgr->seat->num_tp > 10)   {
640                     uifw_debug("ico_mgr_send_input_event: num=%d reset",
641                                pInputMgr->seat->num_tp);
642                     pInputMgr->seat->num_tp = 0;        /* safty gard   */
643                 }
644                 grabsave = pInputMgr->seat->touch->focus;
645                 uifw_debug("ico_mgr_send_input_event: MOTION(%d/%d) grab %08x org %08x",
646                            fix_x/256, fix_y/256, (int)dev->grab, (int)grabsave);
647                 if ((grabsave != dev->grab) && (dev->grab != NULL)) {
648                     weston_touch_set_focus(pInputMgr->seat, dev->grab);
649                 }
650                 notify_touch(pInputMgr->seat, ctime, 0, fix_x, fix_y, WL_TOUCH_MOTION);
651                 if ((grabsave != dev->grab) && (dev->grab != NULL)) {
652                     weston_touch_set_focus(pInputMgr->seat, grabsave);
653                 }
654             }
655             else if (pInputMgr->seat->pointer)  {
656 #if 0           /* too many log */
657                 uifw_debug("ico_mgr_send_input_event: notify_motion_absolute(%d/%d)",
658                            fix_x/256, fix_y/256);
659 #endif
660                 notify_motion_absolute(pInputMgr->seat, ctime, fix_x, fix_y);
661             }
662             break;
663         case EVENT_BUTTON:
664             uifw_trace("ico_mgr_send_input_event: notify_button(%d,%d)", code, value);
665             if (pInputMgr->seat->pointer)   {
666                 if (value)  {
667                     dev->grab = weston_compositor_pick_surface(
668                                     pInputMgr->compositor, fix_x, fix_y, &dx, &dy);
669                     weston_pointer_set_focus(pInputMgr->seat->pointer, dev->grab, dx, dy);
670                     ico_window_mgr_active_surface(dev->grab);
671                 }
672                 else    {
673                     dev->grab = NULL;
674                 }
675                 notify_button(pInputMgr->seat, ctime, code,
676                               value ? WL_POINTER_BUTTON_STATE_PRESSED :
677                                       WL_POINTER_BUTTON_STATE_RELEASED);
678             }
679             break;
680         case EVENT_TOUCH:
681             if (value == ICO_INPUT_MGR_CONTROL_TOUCH_EVENT_RESET)   {
682                 /* reset touch focus    */
683                 grabsave = pInputMgr->seat->touch->focus;
684                 uifw_trace("ico_mgr_send_input_event: notify_touch(UnGrab dev=%08x sys=%08x)",
685                            (int)dev->grab, (int)grabsave);
686                 dev->grab = NULL;
687                 if (grabsave)   {
688                     weston_touch_set_focus(pInputMgr->seat, NULL);
689                     if (pInputMgr->seat->num_tp > 0)   {
690                         uifw_debug("ico_mgr_send_input_event: num=%d reset for reset focuse",
691                                    pInputMgr->seat->num_tp);
692                         pInputMgr->seat->num_tp = 0;
693                     }
694                 }
695             }
696             else if (value == ICO_INPUT_MGR_CONTROL_TOUCH_EVENT_DOWN)   {
697                 grabsave = pInputMgr->seat->touch->focus;
698                 dev->grab = weston_compositor_pick_surface(
699                                 pInputMgr->compositor, fix_x, fix_y, &dx, &dy);
700                 uifw_trace("ico_mgr_send_input_event: notify_touch(DOWN=%d/%d) "
701                            "grab=%08x org=%08x", fix_x/256, fix_y/256,
702                            (int)dev->grab, (int)grabsave);
703                 if (grabsave != dev->grab)  {
704                     weston_touch_set_focus(pInputMgr->seat, dev->grab);
705                 }
706                 if (pInputMgr->seat->num_tp > 0)    {
707                     uifw_debug("ico_mgr_send_input_event: touch_down illegal num, modify");
708                     weston_touch_set_focus(pInputMgr->seat, NULL);
709                     pInputMgr->seat->num_tp = 0;
710                 }
711                 notify_touch(pInputMgr->seat, ctime, 0, fix_x, fix_y, WL_TOUCH_DOWN);
712                 ico_window_mgr_active_surface(dev->grab);
713             }
714             else    {
715                 grabsave = pInputMgr->seat->touch->focus;
716                 uifw_trace("ico_mgr_send_input_event: notify_touch(UP) org=%08x",
717                            (int)grabsave);
718                 if ((grabsave != dev->grab) && (dev->grab != NULL)) {
719                     weston_touch_set_focus(pInputMgr->seat, dev->grab);
720                 }
721                 if ((pInputMgr->seat->num_tp == 0) || (pInputMgr->seat->num_tp > 10))   {
722                     uifw_debug("ico_mgr_send_input_event: num=%d reset",
723                                pInputMgr->seat->num_tp);
724                     pInputMgr->seat->num_tp = 1;
725                 }
726                 notify_touch(pInputMgr->seat, ctime, 0, 0, 0, WL_TOUCH_UP);
727                 if (grabsave == dev->grab)  grabsave = NULL;
728                 weston_touch_set_focus(pInputMgr->seat, grabsave);
729                 dev->grab = NULL;
730             }
731             break;
732         case EVENT_KEY:
733             uifw_trace("ico_mgr_send_input_event: notify_key(%d,%d)", code, value);
734             notify_key(pInputMgr->seat, ctime, code,
735                        value ? WL_KEYBOARD_KEY_STATE_PRESSED :
736                                WL_KEYBOARD_KEY_STATE_RELEASED, STATE_UPDATE_NONE);
737             break;
738         default:
739             uifw_trace("ico_mgr_send_input_event: unknown event=%d", event);
740             break;
741         }
742         /* enable the event transmission to a input layer   */
743         if (type == ICO_INPUT_MGR_DEVICE_TYPE_TOUCH)    {
744             ico_window_mgr_touch_layer(FALSE);
745         }
746     }
747     else    {
748         if ((target != NULL) && (*target != 0) && (*target != ' '))    {
749             /* send event to fixed application  */
750
751             /* get application surface       */
752             usurf = ico_window_mgr_get_client_usurf(target);
753             if (! usurf)  {
754                 uifw_trace("ico_mgr_send_input_event: Leave(window=%s dose not exist)",
755                            target);
756                 return;
757             }
758         }
759         else    {
760             /* get UIFW surface             */
761             usurf = ico_window_mgr_get_usurf(surfaceid);
762             if (! usurf)    {
763                 uifw_trace("ico_mgr_send_input_event: Leave(surface dose not exist)");
764                 return;
765             }
766         }
767
768         /* send event                   */
769         switch (event)    {
770         case EVENT_MOTION:
771             if (type == ICO_INPUT_MGR_DEVICE_TYPE_TOUCH)    {
772                 cres = wl_resource_find_for_client(
773                                     &pInputMgr->seat->touch->resource_list,
774                                     wl_resource_get_client(usurf->surface->resource));
775                 if (cres)   {
776                     wl_touch_send_motion(cres, ctime, 0, fix_x, fix_y);
777                 }
778             }
779             else    {
780                 cres = wl_resource_find_for_client(
781                                     &pInputMgr->seat->pointer->resource_list,
782                                     wl_resource_get_client(usurf->surface->resource));
783                 if (cres)   {
784                     wl_pointer_send_motion(cres, ctime, fix_x, fix_y);
785                 }
786             }
787             break;
788         case EVENT_BUTTON:
789             cres = wl_resource_find_for_client(
790                                 &pInputMgr->seat->pointer->resource_list,
791                                 wl_resource_get_client(usurf->surface->resource));
792             if (cres)   {
793                 serial = wl_display_next_serial(pInputMgr->compositor->wl_display);
794                 wl_pointer_send_button(cres, serial, ctime, code,
795                                        value ? WL_POINTER_BUTTON_STATE_PRESSED :
796                                                WL_POINTER_BUTTON_STATE_RELEASED);
797             }
798             break;
799         case EVENT_TOUCH:
800             cres = wl_resource_find_for_client(
801                                 &pInputMgr->seat->touch->resource_list,
802                                 wl_resource_get_client(usurf->surface->resource));
803             if (cres)   {
804                 serial = wl_display_next_serial(pInputMgr->compositor->wl_display);
805                 if (value)  {
806                     wl_touch_send_down(cres, serial, ctime, usurf->surface->resource, 0,
807                                        fix_x, fix_y);
808                 }
809                 else    {
810                     wl_touch_send_up(cres, serial, ctime, 0);
811                 }
812             }
813             break;
814         case EVENT_KEY:
815             cres = wl_resource_find_for_client(
816                                 &pInputMgr->seat->keyboard->resource_list,
817                                 wl_resource_get_client(usurf->surface->resource));
818             if (cres)   {
819                 keyboard_active = ico_window_mgr_ismykeyboard(usurf);
820                 if (! keyboard_active)  {
821                     wl_array_init(&dummy_array);
822                     serial = wl_display_next_serial(pInputMgr->compositor->wl_display);
823                     wl_keyboard_send_enter(cres, serial,
824                                            usurf->surface->resource, &dummy_array);
825                 }
826                 serial = wl_display_next_serial(pInputMgr->compositor->wl_display);
827                 uifw_trace("ico_mgr_send_input_event: send Key (%d, %d) to %08x",
828                            code, value, usurf->surfaceid);
829                 wl_keyboard_send_key(cres, serial, ctime, code,
830                                      value ? WL_KEYBOARD_KEY_STATE_PRESSED :
831                                              WL_KEYBOARD_KEY_STATE_RELEASED);
832                 if (! keyboard_active)  {
833                     serial = wl_display_next_serial(pInputMgr->compositor->wl_display);
834                     wl_keyboard_send_leave(cres, serial, usurf->surface->resource);
835                 }
836             }
837             else    {
838                 uifw_trace("ico_mgr_send_input_event: Key client %08x dose not exist",
839                            (int)usurf->surface->resource);
840             }
841             break;
842         default:
843             break;
844         }
845     }
846 #if 0           /* too many log */
847     uifw_debug("ico_mgr_send_input_event: Leave");
848 #endif
849 }
850
851 /*--------------------------------------------------------------------------*/
852 /**
853  * @brief   ico_mgr_set_input_region: set input region for haptic devcie
854  *
855  * @param[in]   client          client(Device Input Controller)
856  * @param[in]   resource        resource of request
857  * @param[in]   target          target window (winname@appid)
858  * @param[in]   x               input region X coordinate
859  * @param[in]   y               input region X coordinate
860  * @param[in]   width           input region width
861  * @param[in]   height          input region height
862  * @param[in]   hotspot_x       hotspot of X relative coordinate
863  * @param[in]   hotspot_y       hotspot of Y relative coordinate
864  * @param[in]   cursor_x        cursor region X coordinate
865  * @param[in]   cursor_y        cursor region X coordinate
866  * @param[in]   cursor_width    cursor region width
867  * @param[in]   cursor_height   cursor region height
868  * @param[in]   attr            region attributes(currently unused)
869  * @return      none
870  */
871 /*--------------------------------------------------------------------------*/
872 static void
873 ico_mgr_set_input_region(struct wl_client *client, struct wl_resource *resource,
874                          const char *target, int32_t x, int32_t y,
875                          int32_t width, int32_t height, int32_t hotspot_x,
876                          int32_t hotspot_y, int32_t cursor_x, int32_t cursor_y,
877                          int32_t cursor_width, int32_t cursor_height, uint32_t attr)
878 {
879     struct uifw_win_surface *usurf;         /* UIFW surface                 */
880
881     uifw_trace("ico_mgr_set_input_region: Enter(%s %d/%d-%d/%d(%d/%d) %d/%d-%d/%d)",
882                target, x, y, width, height, hotspot_x, hotspot_y,
883                cursor_x, cursor_y, cursor_width, cursor_height);
884
885     /* get target surface           */
886     usurf = ico_window_mgr_get_client_usurf(target);
887     if (! usurf)    {
888         uifw_warn("ico_mgr_set_input_region: Leave(target<%s> dose not exist)", target);
889         return;
890     }
891
892     ico_set_input_region(1, usurf, x, y, width, height, hotspot_x, hotspot_y,
893                          cursor_x, cursor_y, cursor_width, cursor_height, attr);
894
895     uifw_trace("ico_mgr_set_input_region: Leave");
896 }
897
898 /*--------------------------------------------------------------------------*/
899 /**
900  * @brief   ico_mgr_unset_input_region: unset input region for haptic devcie
901  *
902  * @param[in]   client          client(Device Input Controller)
903  * @param[in]   resource        resource of request
904  * @param[in]   target          target window (winname@appid)
905  * @param[in]   x               input region X coordinate
906  * @param[in]   y               input region X coordinate
907  * @param[in]   width           input region width
908  * @param[in]   height          input region height
909  * @return      none
910  */
911 /*--------------------------------------------------------------------------*/
912 static void
913 ico_mgr_unset_input_region(struct wl_client *client, struct wl_resource *resource,
914                            const char *target, int32_t x, int32_t y,
915                            int32_t width, int32_t height)
916 {
917     struct uifw_win_surface     *usurf;     /* UIFW surface                 */
918
919     uifw_trace("ico_mgr_unset_input_region: Enter(%s %d/%d-%d/%d)",
920                target, x, y, width, height);
921
922     /* get target surface           */
923     usurf = ico_window_mgr_get_client_usurf(target);
924     if (! usurf)    {
925         uifw_warn("ico_mgr_unset_input_region: Leave(target<%s> dose not exist)", target);
926         return;
927     }
928
929     ico_set_input_region(0, usurf, x, y, width, height, 0, 0, 0, 0, 0, 0, 0);
930
931     uifw_trace("ico_mgr_unset_input_region: Leave");
932 }
933
934 /*--------------------------------------------------------------------------*/
935 /**
936  * @brief   ico_input_hook_region_change: change surface attribute
937  *
938  * @param[in]   usurf           UIFW surface
939  * @return      none
940  */
941 /*--------------------------------------------------------------------------*/
942 static void
943 ico_input_hook_region_change(struct uifw_win_surface *usurf)
944 {
945     struct uifw_region_mng      *p;         /* input region mamagement table*/
946     struct ico_uifw_input_region *rp;       /* input region                 */
947     struct wl_array             array;
948     int                         chgcount = 0;
949     int                         visible;
950
951     visible = ico_window_mgr_is_visible(usurf);
952
953     uifw_trace("ico_input_hook_region_change: Entery(surf=%08x, visible=%d)",
954                usurf->surfaceid, visible);
955
956     wl_array_init(&array);
957
958     wl_list_for_each(p, &usurf->input_region, link) {
959         if (((p->region.change > 0) && (visible <= 0)) ||
960             ((p->region.change <= 0) && (visible > 0))) {
961             /* visible change, send add/remove event    */
962             rp = (struct ico_uifw_input_region *)
963                  wl_array_add(&array, sizeof(struct ico_uifw_input_region));
964             if (rp) {
965                 chgcount ++;
966                 memcpy(rp, &p->region, sizeof(struct ico_uifw_input_region));
967                 if (visible > 0)    {
968                     rp->change = ICO_INPUT_MGR_DEVICE_REGION_ADD;
969                 }
970                 else    {
971                     rp->change = ICO_INPUT_MGR_DEVICE_REGION_REMOVE;
972                 }
973             }
974             p->region.change = visible;
975             p->region.node = usurf->node_tbl->node;
976             p->region.surface_x = usurf->x;
977             p->region.surface_y = usurf->y;
978         }
979         else if ((p->region.node != usurf->node_tbl->node) ||
980                  (p->region.surface_x != usurf->x) ||
981                  (p->region.surface_y != usurf->y)) {
982             /* surface position change, send change event   */
983             p->region.node = usurf->node_tbl->node;
984             p->region.surface_x = usurf->x;
985             p->region.surface_y = usurf->y;
986
987             rp = (struct ico_uifw_input_region *)
988                  wl_array_add(&array, sizeof(struct ico_uifw_input_region));
989             if (rp) {
990                 chgcount ++;
991                 memcpy(rp, &p->region, sizeof(struct ico_uifw_input_region));
992                 rp->change = ICO_INPUT_MGR_DEVICE_REGION_CHANGE;
993             }
994         }
995     }
996     if (chgcount > 0)   {
997         /* send region delete to haptic device input controller */
998         ico_input_send_region_event(&array);
999     }
1000     uifw_trace("ico_input_hook_region_change: Leave");
1001 }
1002
1003 /*--------------------------------------------------------------------------*/
1004 /**
1005  * @brief   ico_input_hook_region_destroy: destory surface
1006  *
1007  * @param[in]   usurf           UIFW surface
1008  * @return      none
1009  */
1010 /*--------------------------------------------------------------------------*/
1011 static void
1012 ico_input_hook_region_destroy(struct uifw_win_surface *usurf)
1013 {
1014     struct uifw_region_mng      *p;         /* input region mamagement table*/
1015     struct uifw_region_mng      *np;        /* next region mamagement table */
1016     struct ico_uifw_input_region *rp;       /* input region                 */
1017     struct wl_array             array;
1018     int                         delcount = 0;
1019
1020     uifw_trace("ico_input_hook_region_destroy: Entery(surf=%08x)", usurf->surfaceid);
1021
1022     wl_array_init(&array);
1023
1024     wl_list_for_each_safe(p, np, &usurf->input_region, link)    {
1025         if (p->region.change > 0)   {
1026             /* visible, send remove event   */
1027             rp = (struct ico_uifw_input_region *)
1028                  wl_array_add(&array, sizeof(struct ico_uifw_input_region));
1029             if (rp) {
1030                 delcount ++;
1031                 memcpy(rp, &p->region, sizeof(struct ico_uifw_input_region));
1032                 rp->change = ICO_INPUT_MGR_DEVICE_REGION_REMOVE;
1033             }
1034         }
1035         wl_list_remove(&p->link);
1036         wl_list_insert(pInputMgr->free_region.prev, &p->link);
1037     }
1038     if (delcount > 0)   {
1039         /* send region delete to haptic device input controller */
1040         ico_input_send_region_event(&array);
1041     }
1042     uifw_trace("ico_input_hook_region_destroy: Leave");
1043 }
1044
1045 /*--------------------------------------------------------------------------*/
1046 /**
1047  * @brief   ico_set_input_region: input region set/unset
1048  *
1049  * @param[in]   set             set(1)/unset(0)
1050  * @param[in]   usurf           UIFW surface
1051  * @param[in]   x               input region X coordinate
1052  * @param[in]   y               input region X coordinate
1053  * @param[in]   width           input region width
1054  * @param[in]   height          input region height
1055  * @param[in]   hotspot_x       hotspot of X relative coordinate
1056  * @param[in]   hotspot_y       hotspot of Y relative coordinate
1057  * @param[in]   cursor_x        cursor region X coordinate
1058  * @param[in]   cursor_y        cursor region X coordinate
1059  * @param[in]   cursor_width    cursor region width
1060  * @param[in]   cursor_height   cursor region height
1061  * @param[in]   attr            region attributes(currently unused)
1062  * @return      none
1063  */
1064 /*--------------------------------------------------------------------------*/
1065 static void
1066 ico_set_input_region(int set, struct uifw_win_surface *usurf,
1067                      int32_t x, int32_t y, int32_t width, int32_t height,
1068                      int32_t hotspot_x, int32_t hotspot_y, int32_t cursor_x, int32_t cursor_y,
1069                      int32_t cursor_width, int32_t cursor_height, uint32_t attr)
1070 {
1071     struct uifw_region_mng      *p;         /* input region mamagement table*/
1072     struct uifw_region_mng      *np;        /* next region mamagement table */
1073     struct ico_uifw_input_region *rp;       /* input region                 */
1074     struct wl_array             array;
1075     int                         i;
1076     int                         alldel;
1077     int                         delcount;
1078
1079     uifw_trace("ico_set_input_region: Enter(%s %d/%d-%d/%d(%d/%d) %d/%d-%d/%d)",
1080                set ? "Set" : "Unset", x, y, width, height, hotspot_x, hotspot_y,
1081                cursor_x, cursor_y, cursor_width, cursor_height);
1082
1083     if (set)    {
1084         if (wl_list_empty(&pInputMgr->free_region)) {
1085             p = malloc(sizeof(struct uifw_region_mng) * 50);
1086             if (! p)    {
1087                 uifw_error("ico_set_input_region: No Memory");
1088                 return;
1089             }
1090             memset(p, 0, sizeof(struct uifw_region_mng)*50);
1091             for (i = 0; i < 50; i++, p++)  {
1092                 wl_list_insert(pInputMgr->free_region.prev, &p->link);
1093             }
1094         }
1095         p = container_of(pInputMgr->free_region.next, struct uifw_region_mng, link);
1096         wl_list_remove(&p->link);
1097         p->region.node = usurf->node_tbl->node;
1098         p->region.surfaceid = usurf->surfaceid;
1099         p->region.surface_x = usurf->x;
1100         p->region.surface_y = usurf->y;
1101         p->region.x = x;
1102         p->region.y = y;
1103         p->region.width = width;
1104         p->region.height = height;
1105         if ((hotspot_x <= 0) && (hotspot_y <= 0))   {
1106             p->region.hotspot_x = width / 2;
1107             p->region.hotspot_y = height / 2;
1108         }
1109         else    {
1110             p->region.hotspot_x = hotspot_x;
1111             p->region.hotspot_y = hotspot_y;
1112         }
1113         if ((cursor_width <= 0) && (cursor_height <= 0))    {
1114             p->region.cursor_x = x;
1115             p->region.cursor_y = y;
1116             p->region.cursor_width = width;
1117             p->region.cursor_height = height;
1118         }
1119         else    {
1120             p->region.cursor_x = cursor_x;
1121             p->region.cursor_y = cursor_y;
1122             p->region.cursor_width = cursor_width;
1123             p->region.cursor_height = cursor_height;
1124         }
1125         p->region.change = ico_window_mgr_is_visible(usurf);
1126         wl_list_insert(usurf->input_region.prev, &p->link);
1127
1128         /* send input region to haptic device input controller  */
1129         if (p->region.change > 0)   {
1130             wl_array_init(&array);
1131             rp = (struct ico_uifw_input_region *)
1132                      wl_array_add(&array, sizeof(struct ico_uifw_input_region));
1133             if (rp) {
1134                 memcpy(rp, &p->region, sizeof(struct ico_uifw_input_region));
1135                 rp->change = ICO_INPUT_MGR_DEVICE_REGION_ADD;
1136                 ico_input_send_region_event(&array);
1137             }
1138             uifw_trace("ico_set_input_region: Leave(Set)");
1139         }
1140         else    {
1141             uifw_trace("ico_set_input_region: Leave(Set but Unvisible)");
1142         }
1143     }
1144     else    {
1145         delcount = 0;
1146
1147         if ((x <= 0) && (y <= 0) && (width <= 0) && (height <= 0))  {
1148             alldel = 1;
1149         }
1150         else    {
1151             alldel = 0;
1152         }
1153
1154         wl_array_init(&array);
1155
1156         wl_list_for_each_safe(p, np, &usurf->input_region, link)    {
1157             if ((alldel != 0) ||
1158                 ((x == p->region.x) && (y == p->region.y) &&
1159                  (width == p->region.width) && (height == p->region.height)))   {
1160                 if (p->region.change > 0)   {
1161                     /* visible, send remove event   */
1162                     rp = (struct ico_uifw_input_region *)
1163                          wl_array_add(&array, sizeof(struct ico_uifw_input_region));
1164                     if (rp) {
1165                         delcount ++;
1166                         memcpy(rp, &p->region, sizeof(struct ico_uifw_input_region));
1167                         rp->change = ICO_INPUT_MGR_DEVICE_REGION_REMOVE;
1168                     }
1169                 }
1170                 wl_list_remove(&p->link);
1171                 wl_list_insert(pInputMgr->free_region.prev, &p->link);
1172             }
1173         }
1174         if (delcount > 0)   {
1175             /* send region delete to haptic device input controller */
1176             ico_input_send_region_event(&array);
1177         }
1178         uifw_trace("ico_set_input_region: Leave(Unset)");
1179     }
1180 }
1181
1182 /*--------------------------------------------------------------------------*/
1183 /**
1184  * @brief   ico_input_send_region_event: send region event to Haptic dic
1185  *
1186  * @param[in]   usurf           UIFW surface
1187  * @return      none
1188  */
1189 /*--------------------------------------------------------------------------*/
1190 static void
1191 ico_input_send_region_event(struct wl_array *array)
1192 {
1193     struct ico_ictl_mgr     *pIctlMgr;
1194
1195     wl_list_for_each (pIctlMgr, &pInputMgr->ictl_list, link)   {
1196         if ((pIctlMgr->type == ICO_INPUT_MGR_DEVICE_TYPE_HAPTIC) &&
1197             (pIctlMgr->mgr_resource != NULL))   {
1198             uifw_trace("ico_input_send_region_event: send event to Hapfic");
1199             ico_input_mgr_device_send_input_regions(pIctlMgr->mgr_resource, array);
1200         }
1201     }
1202 }
1203
1204 /*--------------------------------------------------------------------------*/
1205 /**
1206  * @brief   ico_device_configure_input: configure input device and input switch
1207  *          from Device Input Controller.
1208  *
1209  * @param[in]   client          client(Device Input Controller)
1210  * @param[in]   resource        resource of request
1211  * @param[in]   device          device name
1212  * @param[in]   type            device type(saved but unused)
1213  * @param[in]   swname          input switch name
1214  * @param[in]   input           input switch number
1215  * @param[in]   codename        input code name
1216  * @param[in]   code            input code number
1217  * @return      none
1218  */
1219 /*--------------------------------------------------------------------------*/
1220 static void
1221 ico_device_configure_input(struct wl_client *client, struct wl_resource *resource,
1222                            const char *device, int32_t type, const char *swname,
1223                            int32_t input, const char *codename, int32_t code)
1224 {
1225     uifw_trace("ico_device_configure_input: Enter(client=%08x,dev=%s,type=%d,swname=%s,"
1226                "input=%d,code=%d[%s])", (int)client, device, type,
1227                swname ? swname : "(NULL)", input, code, codename ? codename : " ");
1228
1229     struct ico_ictl_mgr     *pIctlMgr;
1230     struct ico_ictl_mgr     *psameIctlMgr;
1231     struct ico_ictl_input   *pInput;
1232     struct ico_app_mgr      *pAppMgr;
1233
1234     pIctlMgr = find_ictlmgr_by_device(device);
1235     if (! pIctlMgr) {
1236         /* search binded table      */
1237         psameIctlMgr = NULL;
1238         wl_list_for_each (pIctlMgr, &pInputMgr->ictl_list, link)    {
1239             if (pIctlMgr->client == client) {
1240                 uifw_trace("ico_device_configure_input: set pIctlMgr"
1241                            "(mgr=%08x,input=%d,dev=%s)", (int)pIctlMgr,
1242                            input, pIctlMgr->device);
1243                 if (pIctlMgr->device[0] != 0)   {
1244                     /* save same device         */
1245                     psameIctlMgr = pIctlMgr;
1246                 }
1247                 else    {
1248                     /* first device             */
1249                     strncpy(pIctlMgr->device, device, sizeof(pIctlMgr->device)-1);
1250                     psameIctlMgr = NULL;
1251                     break;
1252                 }
1253             }
1254         }
1255         if (psameIctlMgr)   {
1256             /* client(device input controller) exist, but other device  */
1257             pIctlMgr = (struct ico_ictl_mgr *)malloc(sizeof(struct ico_ictl_mgr));
1258             if (pIctlMgr == NULL) {
1259                 uifw_error("ico_device_bind: Leave(No Memory)");
1260                 return;
1261             }
1262             memset(pIctlMgr, 0, sizeof(struct ico_ictl_mgr));
1263             wl_list_init(&pIctlMgr->ico_ictl_input);
1264             pIctlMgr->client = psameIctlMgr->client;
1265             pIctlMgr->mgr_resource = psameIctlMgr->mgr_resource;
1266
1267             wl_list_insert(pInputMgr->ictl_list.prev, &pIctlMgr->link);
1268         }
1269     }
1270
1271     if (type)   {
1272         pIctlMgr->type = type;
1273     }
1274
1275     /* search and add input switch  */
1276     wl_list_for_each (pInput, &pIctlMgr->ico_ictl_input, link)  {
1277         if (pInput->input == input)     break;
1278     }
1279     if (&pInput->link == &pIctlMgr->ico_ictl_input)    {
1280         uifw_trace("ico_device_configure_input: create %s.%s(%d) switch",
1281                    device, swname, input);
1282         pInput = (struct ico_ictl_input *)malloc(sizeof(struct ico_ictl_input));
1283         if (pInput == NULL) {
1284             uifw_error("ico_device_configure_input: Leave(No Memory)");
1285             return;
1286         }
1287         memset(pInput, 0, sizeof(struct ico_ictl_input));
1288         if (swname) {
1289             strncpy(pInput->swname, swname, sizeof(pInput->swname)-1);
1290         }
1291         else    {
1292             strcpy(pInput->swname, "(Unknown)");
1293         }
1294         wl_list_insert(pIctlMgr->ico_ictl_input.prev, &pInput->link);
1295     }
1296     if (swname) {
1297         strncpy(pInput->swname, swname, sizeof(pInput->swname)-1);
1298     }
1299     pInput->input = input;
1300     memset(pInput->code, 0, sizeof(pInput->code));
1301     pInput->ncode = 1;
1302     pInput->code[0].code = code;
1303     if (codename)   {
1304         strncpy(pInput->code[0].name, codename, sizeof(pInput->code[0].name)-1);
1305     }
1306
1307     if (client == NULL) {
1308         /* internal call for table create   */
1309         uifw_trace("ico_device_configure_input: Leave(table create)");
1310         return;
1311     }
1312     pIctlMgr->client = client;
1313
1314     /* send to application and manager(ex.HomeScreen)   */
1315     wl_list_for_each (pAppMgr, &pInputMgr->app_list, link)  {
1316         if (pAppMgr->resource == NULL)  continue;
1317         if ((pInput->app != NULL) && (pInput->app != pAppMgr) && (pInput->fix)) continue;
1318
1319         uifw_trace("ico_device_configure_input: send capabilities to app(%s) %s.%s[%d]",
1320                    pAppMgr->appid, device, pInput->swname, input);
1321         ico_exinput_send_capabilities(pAppMgr->resource, device, pIctlMgr->type,
1322                                       pInput->swname, input,
1323                                       pInput->code[0].name, pInput->code[0].code);
1324     }
1325     uifw_trace("ico_device_configure_input: Leave");
1326 }
1327
1328 /*--------------------------------------------------------------------------*/
1329 /**
1330  * @brief   ico_device_configure_code: add input switch from Device Input Controller.
1331  *
1332  * @param[in]   client          client(Device Input Controller)
1333  * @param[in]   resource        resource of request
1334  * @param[in]   device          device name
1335  * @param[in]   input           input switch number
1336  * @param[in]   codename        input code name
1337  * @param[in]   code            input code number
1338  * @return      none
1339  */
1340 /*--------------------------------------------------------------------------*/
1341 static void
1342 ico_device_configure_code(struct wl_client *client, struct wl_resource *resource,
1343                           const char *device, int32_t input,
1344                           const char *codename, int32_t code)
1345 {
1346     uifw_trace("ico_device_configure_code: Enter(client=%08x,dev=%s,input=%d,code=%d[%s])",
1347                (int)client, device, input, code, codename ? codename : " ");
1348
1349     int     i;
1350     struct ico_ictl_mgr     *pIctlMgr;
1351     struct ico_ictl_input   *pInput;
1352     struct ico_app_mgr      *pAppMgr;
1353
1354     pIctlMgr = find_ictlmgr_by_device(device);
1355     if (! pIctlMgr) {
1356         uifw_warn("ico_device_configure_code: Leave(dev=%s dose not exist)", device);
1357         return;
1358     }
1359     /* search input switch      */
1360     wl_list_for_each (pInput, &pIctlMgr->ico_ictl_input, link)  {
1361         if (pInput->input == input)     break;
1362     }
1363     if (&pInput->link == &pIctlMgr->ico_ictl_input)    {
1364         uifw_warn("ico_device_configure_code: Leave(input=%s.%d dose not exist)",
1365                   device, input);
1366         return;
1367     }
1368
1369     /* search input code        */
1370     for (i = 0; i < pInput->ncode; i++) {
1371         if (pInput->code[i].code == code)   break;
1372     }
1373     if (i >= pInput->ncode) {
1374         /* code dose not exist, add */
1375         if (pInput->ncode >= ICO_MINPUT_MAX_CODES) {
1376             uifw_warn("ico_device_configure_code: Leave(input=%s.%d code overflow)",
1377                       device, input);
1378             return;
1379         }
1380         i = pInput->ncode;
1381         pInput->ncode ++;
1382         pInput->code[i].code = code;
1383     }
1384     memset(pInput->code[i].name, 0, sizeof(pInput->code[i].name));
1385     strncpy(pInput->code[i].name, codename, sizeof(pInput->code[i].name)-1);
1386
1387     /* send to application and manager(ex.HomeScreen)   */
1388     wl_list_for_each (pAppMgr, &pInputMgr->app_list, link)  {
1389         if (pAppMgr->resource == NULL)  continue;
1390         if ((pInput->app != NULL) && (pInput->app != pAppMgr) && (pInput->fix)) continue;
1391         uifw_trace("ico_device_configure_input: send code to app(%s) %s.%s[%d]",
1392                    pAppMgr->appid, device, pInput->swname, input);
1393         ico_exinput_send_code(pAppMgr->resource, device, input,
1394                               pInput->code[i].name, pInput->code[i].code);
1395     }
1396     uifw_trace("ico_device_configure_code: Leave");
1397 }
1398
1399 /*--------------------------------------------------------------------------*/
1400 /**
1401  * @brief   ico_device_input_event: device input event from Device Input Controller.
1402  *
1403  * @param[in]   client          client(Device Input Controller)
1404  * @param[in]   resource        resource of request
1405  * @param[in]   time            device input time(miri-sec)
1406  * @param[in]   device          device name
1407  * @param[in]   input           input switch number
1408  * @param[in]   code            input code number
1409  * @param[in]   state           input state(1=On, 0=Off)
1410  * @return      none
1411  */
1412 /*--------------------------------------------------------------------------*/
1413 static void
1414 ico_device_input_event(struct wl_client *client, struct wl_resource *resource,
1415                        uint32_t time, const char *device,
1416                        int32_t input, int32_t code, int32_t state)
1417 {
1418     uifw_trace("ico_device_input_event: Enter(time=%d,dev=%s,input=%d,code=%d,state=%d)",
1419                time, device, input, code, state);
1420
1421     struct ico_ictl_mgr     *pIctlMgr;
1422     struct ico_ictl_input   *pInput;
1423
1424     /* find input devcie by client      */
1425     pIctlMgr = find_ictlmgr_by_device(device);
1426     if (! pIctlMgr) {
1427         uifw_error("ico_device_input_event: Leave(Unknown device(%s))", device);
1428         return;
1429     }
1430     /* find input switch by input Id    */
1431     pInput = find_ictlinput_by_input(pIctlMgr, input);
1432     if (! pInput) {
1433         uifw_warn("ico_device_input_event: Leave(Unknown input(%s,%d))",
1434                   pIctlMgr->device, input);
1435         return;
1436     }
1437
1438     if (! pInput->app)  {
1439         uifw_trace("ico_device_input_event: Leave(%s.%s not assign)",
1440                   pIctlMgr->device, pInput->swname);
1441         return;
1442     }
1443
1444     /* send event to application        */
1445     uifw_trace("ico_device_input_event: send event=%s.%s[%d],%d,%d to App.%s",
1446                pIctlMgr->device, pInput->swname, input, code, state, pInput->app->appid);
1447     ico_exinput_send_input(pInput->app->resource, time, pIctlMgr->device,
1448                            input, code, state);
1449
1450     uifw_trace("ico_device_input_event: Leave");
1451 }
1452
1453 /*--------------------------------------------------------------------------*/
1454 /**
1455  * @brief   ico_control_bind: ico_input_mgr_control bind from HomeScreen
1456  *
1457  * @param[in]   client          client(HomeScreen)
1458  * @param[in]   data            data(unused)
1459  * @param[in]   version         protocol version(unused)
1460  * @param[in]   id              client object id
1461  * @return      none
1462  */
1463 /*--------------------------------------------------------------------------*/
1464 static void
1465 ico_control_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1466 {
1467     char                    *appid;
1468     struct ico_app_mgr      *pAppMgr;
1469
1470     uifw_trace("ico_control_bind: Enter(client=%08x)", (int)client);
1471     appid = ico_window_mgr_get_appid(client);
1472
1473     if (! appid)    {
1474         /* client dose not exist        */
1475         uifw_warn("ico_control_bind: Leave(client=%08x dose not exist)", (int)client);
1476         return;
1477     }
1478
1479     /* find application         */
1480     pAppMgr = find_app_by_appid(appid);
1481     if (! pAppMgr)  {
1482         /* create Application Management Table  */
1483         pAppMgr = (struct ico_app_mgr *)malloc(sizeof(struct ico_app_mgr));
1484         if (! pAppMgr)  {
1485             uifw_error("ico_control_bind: Leave(No Memory)");
1486             return;
1487         }
1488         memset(pAppMgr, 0, sizeof(struct ico_app_mgr));
1489         strncpy(pAppMgr->appid, appid, sizeof(pAppMgr->appid)-1);
1490         wl_list_insert(pInputMgr->app_list.prev, &pAppMgr->link);
1491     }
1492     pAppMgr->client = client;
1493     if (! pAppMgr->mgr_resource)    {
1494         pAppMgr->mgr_resource = wl_resource_create(client,
1495                                                    &ico_input_mgr_control_interface, 1, id);
1496         if (pAppMgr->mgr_resource)  {
1497             wl_resource_set_implementation(pAppMgr->mgr_resource,
1498                                            &ico_input_mgr_implementation,
1499                                            pInputMgr, ico_control_unbind);
1500         }
1501     }
1502     uifw_trace("ico_control_bind: Leave");
1503 }
1504
1505 /*--------------------------------------------------------------------------*/
1506 /**
1507  * @brief   ico_control_unbind: ico_input_mgr_control unbind from HomeScreen
1508  *
1509  * @param[in]   resource        client resource(HomeScreen)
1510  * @return      none
1511  */
1512 /*--------------------------------------------------------------------------*/
1513 static void
1514 ico_control_unbind(struct wl_resource *resource)
1515 {
1516     struct ico_app_mgr  *pAppMgr;
1517
1518     uifw_trace("ico_control_unbind: Enter(resource=%08x)", (int)resource);
1519
1520     wl_list_for_each (pAppMgr, &pInputMgr->app_list, link)  {
1521         if (pAppMgr->mgr_resource == resource)  {
1522             uifw_trace("ico_control_unbind: find app.%s", pAppMgr->appid);
1523             pAppMgr->mgr_resource = NULL;
1524             break;
1525         }
1526     }
1527     uifw_trace("ico_control_unbind: Leave");
1528 }
1529
1530 /*--------------------------------------------------------------------------*/
1531 /**
1532  * @brief   ico_device_bind: ico_input_mgr_device bind from Device Input Controller
1533  *
1534  * @param[in]   client          client(Device Input Controller)
1535  * @param[in]   data            data(unused)
1536  * @param[in]   version         protocol version
1537  * @param[in]   id              client object id
1538  * @return      none
1539  */
1540 /*--------------------------------------------------------------------------*/
1541 static void
1542 ico_device_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1543 {
1544     struct ico_ictl_mgr *pIctlMgr;
1545     struct wl_resource  *mgr_resource;
1546
1547     uifw_trace("ico_device_bind: Enter(client=%08x)", (int)client);
1548
1549     /* create ictl mgr table */
1550     pIctlMgr = (struct ico_ictl_mgr *)malloc(sizeof(struct ico_ictl_mgr));
1551     if (pIctlMgr == NULL) {
1552         uifw_error("ico_device_bind: Leave(No Memory)");
1553         return;
1554     }
1555     memset(pIctlMgr, 0, sizeof(struct ico_ictl_mgr));
1556     wl_list_init(&pIctlMgr->ico_ictl_input);
1557     pIctlMgr->client = client;
1558
1559     /* add list */
1560     wl_list_insert(pInputMgr->ictl_list.prev, &pIctlMgr->link);
1561
1562     mgr_resource = wl_resource_create(client, &ico_input_mgr_device_interface, 1, id);
1563     if (mgr_resource)   {
1564         pIctlMgr->mgr_resource = mgr_resource;
1565         wl_resource_set_implementation(mgr_resource, &input_mgr_ictl_implementation,
1566                                        pIctlMgr, ico_device_unbind);
1567     }
1568     uifw_trace("ico_device_bind: Leave");
1569 }
1570
1571 /*--------------------------------------------------------------------------*/
1572 /**
1573  * @brief   ico_device_unbind: ico_input_mgr_device unbind from Device Input Controller
1574  *
1575  * @param[in]   resource        client resource(Device Input Controller)
1576  * @return      none
1577  */
1578 /*--------------------------------------------------------------------------*/
1579 static void
1580 ico_device_unbind(struct wl_resource *resource)
1581 {
1582     uifw_trace("ico_device_unbind: Enter(resource=%08x)", (int)resource);
1583     uifw_trace("ico_device_unbind: Leave");
1584 }
1585
1586 /*--------------------------------------------------------------------------*/
1587 /**
1588  * @brief   ico_exinput_bind: ico_exinput bind from Application
1589  *
1590  * @param[in]   client          client(Application)
1591  * @param[in]   data            data(unused)
1592  * @param[in]   version         protocol version(unused)
1593  * @param[in]   id              client object id
1594  * @return      none
1595  */
1596 /*--------------------------------------------------------------------------*/
1597 static void
1598 ico_exinput_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1599 {
1600     int                     i;
1601     char                    *appid;
1602     struct ico_app_mgr      *pAppMgr;
1603     struct ico_ictl_mgr     *pIctlMgr;
1604     struct ico_ictl_input   *pInput;
1605
1606     appid = ico_window_mgr_get_appid(client);
1607     uifw_trace("ico_exinput_bind: Enter(client=%08x,%s)", (int)client,
1608                appid ? appid : "(NULL)");
1609
1610     if (! appid)    {
1611         /* client dose not exist        */
1612         uifw_warn("ico_exinput_bind: Leave(client=%08x dose not exist)", (int)client);
1613         return;
1614     }
1615
1616     /* find application         */
1617     pAppMgr = find_app_by_appid(appid);
1618     if (! pAppMgr)  {
1619         /* create Application Management Table  */
1620         pAppMgr = (struct ico_app_mgr *)malloc(sizeof(struct ico_app_mgr));
1621         if (! pAppMgr)  {
1622             uifw_error("ico_exinput_bind: Leave(No Memory)");
1623             return;
1624         }
1625         memset(pAppMgr, 0, sizeof(struct ico_app_mgr));
1626         strncpy(pAppMgr->appid, appid, sizeof(pAppMgr->appid)-1);
1627         wl_list_insert(pInputMgr->app_list.prev, &pAppMgr->link);
1628         uifw_trace("ico_exinput_bind: Create App.%s table", appid);
1629     }
1630     pAppMgr->client = client;
1631     if (! pAppMgr->resource)    {
1632         pAppMgr->resource = wl_resource_create(client, &ico_exinput_interface, 1, id);
1633         if (pAppMgr->resource)  {
1634             wl_resource_set_implementation(pAppMgr->resource, &ico_exinput_implementation,
1635                                            pInputMgr, ico_exinput_unbind);
1636         }
1637     }
1638
1639     /* send all capabilities    */
1640     wl_list_for_each (pIctlMgr, &pInputMgr->ictl_list, link)    {
1641         if (pIctlMgr->client == NULL)   {
1642             uifw_trace("ico_exinput_bind: Input controller.%s not initialized",
1643                        pIctlMgr->device);
1644             continue;
1645         }
1646
1647         wl_list_for_each (pInput, &pIctlMgr->ico_ictl_input, link)  {
1648             if (pInput->swname[0] == 0) {
1649                 uifw_trace("ico_exinput_bind: Input %s not initialized", pIctlMgr->device);
1650                 continue;
1651             }
1652             if ((pInput->app != NULL) && (pInput->app != pAppMgr) && (pInput->fix)) {
1653                 uifw_trace("ico_exinput_bind: Input %s.%s fixed assign to App.%s",
1654                            pIctlMgr->device, pInput->swname, pInput->app->appid);
1655                 continue;
1656             }
1657             uifw_trace("ico_exinput_bind: send capabilities to app(%s) %s.%s[%d]",
1658                        pAppMgr->appid, pIctlMgr->device, pInput->swname, pInput->input);
1659             ico_exinput_send_capabilities(pAppMgr->resource, pIctlMgr->device,
1660                                           pIctlMgr->type, pInput->swname, pInput->input,
1661                                           pInput->code[0].name, pInput->code[0].code);
1662             for (i = 1; i < pInput->ncode; i++) {
1663                 ico_exinput_send_code(pAppMgr->resource, pIctlMgr->device, pInput->input,
1664                                       pInput->code[i].name, pInput->code[i].code);
1665             }
1666         }
1667     }
1668     uifw_trace("ico_exinput_bind: Leave");
1669 }
1670
1671 /*--------------------------------------------------------------------------*/
1672 /**
1673  * @brief   ico_exinput_unbind: ico_exinput unbind from Application
1674  *
1675  * @param[in]   resource        client resource(Application)
1676  * @return      none
1677  */
1678 /*--------------------------------------------------------------------------*/
1679 static void
1680 ico_exinput_unbind(struct wl_resource *resource)
1681 {
1682     struct ico_app_mgr      *pAppMgr;
1683     struct ico_app_mgr      *pAppMgrTmp;
1684     struct ico_ictl_mgr     *pIctlMgr;
1685     struct ico_ictl_input   *pInput;
1686     int                     fix = 0;
1687
1688     uifw_trace("ico_exinput_unbind: Enter(resource=%08x)", (int)resource);
1689
1690     wl_list_for_each_safe (pAppMgr, pAppMgrTmp, &pInputMgr->app_list, link) {
1691         if (pAppMgr->resource == resource)  {
1692             uifw_trace("ico_exinput_unbind: find app.%s", pAppMgr->appid);
1693
1694             /* release application from input switch    */
1695             wl_list_for_each (pIctlMgr, &pInputMgr->ictl_list, link)    {
1696                 wl_list_for_each (pInput, &pIctlMgr->ico_ictl_input, link)  {
1697                     if (pInput->app == pAppMgr) {
1698                         if (pInput->fix == 0)   {
1699                             uifw_trace("ico_exinput_unbind: app.%s remove %s.%s",
1700                                        pAppMgr->appid, pIctlMgr->device, pInput->swname);
1701                             pInput->app = NULL;
1702                         }
1703                         else    {
1704                             uifw_trace("ico_exinput_unbind: app.%s fix assign %s.%s",
1705                                        pAppMgr->appid, pIctlMgr->device, pInput->swname);
1706                             fix ++;
1707                         }
1708                     }
1709                 }
1710             }
1711             if (fix == 0)   {
1712                 wl_list_remove(&pAppMgr->link);
1713                 free(pAppMgr);
1714             }
1715             else    {
1716                 pAppMgr->client = NULL;
1717                 pAppMgr->resource = NULL;
1718             }
1719         }
1720     }
1721     uifw_trace("ico_exinput_unbind: Leave");
1722 }
1723
1724 /*--------------------------------------------------------------------------*/
1725 /**
1726  * @brief   find_ictlmgr_by_device: find Input Controller by device name
1727  *
1728  * @param[in]   device          device name
1729  * @return      Input Controller Manager table address
1730  * @retval      !=NULL          address
1731  * @retval      ==NULL          not exist
1732  */
1733 /*--------------------------------------------------------------------------*/
1734 static struct ico_ictl_mgr *
1735 find_ictlmgr_by_device(const char *device)
1736 {
1737     struct ico_ictl_mgr     *pIctlMgr;
1738
1739     wl_list_for_each (pIctlMgr, &pInputMgr->ictl_list, link)    {
1740         uifw_debug("find_ictlmgr_by_device: <%s> vs <%s>", device, pIctlMgr->device);
1741         if (strcmp(pIctlMgr->device, device) == 0)  {
1742             return pIctlMgr;
1743         }
1744     }
1745     return NULL;
1746 }
1747
1748 /*--------------------------------------------------------------------------*/
1749 /**
1750  * @brief   find_ictlinput_by_input: find Input Switch by input Id
1751  *
1752  * @param[in]   pIctlMgr        Input Controller device
1753  * @param[in]   input           Input Id
1754  * @return      Input Switch table address
1755  * @retval      !=NULL          address
1756  * @retval      ==NULL          not exist
1757  */
1758 /*--------------------------------------------------------------------------*/
1759 static struct ico_ictl_input *
1760 find_ictlinput_by_input(struct ico_ictl_mgr *pIctlMgr, const int32_t input)
1761 {
1762     struct ico_ictl_input   *pInput;
1763
1764     wl_list_for_each (pInput, &pIctlMgr->ico_ictl_input, link)  {
1765         if (pInput->input == input) {
1766             return pInput;
1767         }
1768     }
1769     return NULL;
1770 }
1771
1772 /*--------------------------------------------------------------------------*/
1773 /**
1774  * @brief   find_app_by_appid: find Application by application Id
1775  *
1776  * @param[in]   appid           application Id
1777  * @return      Application Management table address
1778  * @retval      !=NULL          address
1779  * @retval      ==NULL          not exist
1780  */
1781 /*--------------------------------------------------------------------------*/
1782 static struct ico_app_mgr *
1783 find_app_by_appid(const char *appid)
1784 {
1785     struct ico_app_mgr      *pAppMgr;
1786
1787     wl_list_for_each (pAppMgr, &pInputMgr->app_list, link)  {
1788         if (strcmp(pAppMgr->appid, appid) == 0) {
1789             return pAppMgr;
1790         }
1791     }
1792     return NULL;
1793 }
1794
1795 /*--------------------------------------------------------------------------*/
1796 /**
1797  * @brief   module_init: initialization of this plugin
1798  *
1799  * @param[in]   ec          weston compositor
1800  * @param[in]   argc        number of arguments(unused)
1801  * @param[in]   argv        argument list(unused)
1802  * @return      result
1803  * @retval      0           OK
1804  * @retval      -1          error
1805  */
1806 /*--------------------------------------------------------------------------*/
1807 WL_EXPORT int
1808 module_init(struct weston_compositor *ec, int *argc, char *argv[])
1809 {
1810     struct uifw_region_mng  *p;
1811     int     i;
1812
1813     uifw_info("ico_input_mgr: Enter(module_init)");
1814
1815     /* initialize management table */
1816     pInputMgr = (struct ico_input_mgr *)malloc(sizeof(struct ico_input_mgr));
1817     if (pInputMgr == NULL) {
1818         uifw_trace("ico_input_mgr: malloc failed");
1819         return -1;
1820     }
1821     memset(pInputMgr, 0, sizeof(struct ico_input_mgr));
1822     pInputMgr->compositor = ec;
1823
1824     /* interface to desktop manager(ex.HomeScreen)  */
1825     if (wl_global_create(ec->wl_display, &ico_input_mgr_control_interface, 1,
1826                          pInputMgr, ico_control_bind) == NULL) {
1827         uifw_trace("ico_input_mgr: wl_global_create mgr failed");
1828         return -1;
1829     }
1830
1831     /* interface to Input Controller(ictl) */
1832     if (wl_global_create(ec->wl_display, &ico_input_mgr_device_interface, 1,
1833                          pInputMgr, ico_device_bind) == NULL) {
1834         uifw_trace("ico_input_mgr: wl_global_create ictl failed");
1835         return -1;
1836     }
1837
1838     /* interface to App(exinput) */
1839     if (wl_global_create(ec->wl_display, &ico_exinput_interface, 1,
1840                          pInputMgr, ico_exinput_bind) == NULL) {
1841         uifw_trace("ico_input_mgr: wl_global_create exseat failed");
1842         return -1;
1843     }
1844
1845     /* initialize list */
1846     wl_list_init(&pInputMgr->ictl_list);
1847     wl_list_init(&pInputMgr->app_list);
1848     wl_list_init(&pInputMgr->dev_list);
1849     wl_list_init(&pInputMgr->free_region);
1850     p = malloc(sizeof(struct uifw_region_mng)*100);
1851     if (p)  {
1852         memset(p, 0, sizeof(struct uifw_region_mng)*100);
1853         for (i = 0; i < 100; i++, p++)  {
1854             wl_list_insert(pInputMgr->free_region.prev, &p->link);
1855         }
1856     }
1857
1858     /* found input seat */
1859     pInputMgr->seat = container_of(ec->seat_list.next, struct weston_seat, link);
1860
1861     /* set hook for input region control    */
1862     ico_window_mgr_set_hook_change(ico_input_hook_region_change);
1863     ico_window_mgr_set_hook_destory(ico_input_hook_region_destroy);
1864     ico_window_mgr_set_hook_inputregion(ico_set_input_region);
1865
1866     uifw_info("ico_input_mgr: Leave(module_init)");
1867     return 0;
1868 }
1869