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