If a client Successfully Grabbed a key in exclusive mode, print information about...
[platform/core/uifw/e-mod-tizen-keyrouter.git] / src / e_mod_main_wl.c
1 #define E_COMP_WL
2 #include "e_mod_main_wl.h"
3 #include <device/power.h>
4 #include <device/callback.h>
5 #include <device/display.h>
6
7 #define KRT_IPD_INPUT_CONFIG          444
8
9 E_KeyrouterPtr krt = NULL;
10 E_API E_Module_Api e_modapi = { E_MODULE_API_VERSION, "Keyrouter Module of Window Manager" };
11
12 static E_Keyrouter_Config_Data *_e_keyrouter_init(E_Module *m);
13 static void _e_keyrouter_init_handlers(void);
14 static void _e_keyrouter_deinit_handlers(void);
15
16 static Eina_Bool _e_keyrouter_query_tizen_key_table(void);
17 static int _e_keyrouter_wl_array_length(const struct wl_array *array);
18
19 static Eina_Bool _e_keyrouter_client_cb_stack(void *data, int type, void *event);
20 static Eina_Bool _e_keyrouter_client_cb_remove(void *data, int type, void *event);
21 static void _e_keyrouter_wl_client_cb_destroy(struct wl_listener *l, void *data);
22 static void _e_keyrouter_wl_surface_cb_destroy(struct wl_listener *l, void *data);
23
24 static int _e_keyrouter_keygrab_set(struct wl_client *client, struct wl_resource *surface, uint32_t key, uint32_t mode);
25 static int _e_keyrouter_keygrab_unset(struct wl_client *client, struct wl_resource *surface, uint32_t key);
26 static Eina_Bool _e_keyrouter_cb_idler(void *data);
27 static void _e_keyrouter_cb_power_change(device_callback_e type, void* value, void* user_data);
28 #ifdef ENABLE_CYNARA
29 static void _e_keyrouter_util_cynara_log(const char *func_name, int err);
30 static Eina_Bool _e_keyrouter_util_do_privilege_check(struct wl_client *client, uint32_t mode, uint32_t keycode);
31
32 #define E_KEYROUTER_CYNARA_ERROR_CHECK_GOTO(func_name, ret, label) \
33   do \
34     { \
35        if (EINA_UNLIKELY(CYNARA_API_SUCCESS != ret)) \
36           { \
37              _e_keyrouter_util_cynara_log(func_name, ret); \
38              goto label; \
39           } \
40     } \
41   while (0)
42 #endif
43
44 int _keyrouter_log_dom = -1;
45
46 static int
47 _e_keyrouter_keygrab_set(struct wl_client *client, struct wl_resource *surface, uint32_t key, uint32_t mode)
48 {
49    int res=0;
50
51 #ifdef ENABLE_CYNARA
52    if (EINA_FALSE == _e_keyrouter_util_do_privilege_check(client, mode, key))
53      {
54         KLINF("No permission for %d grab mode ! (key=%d)", mode, key);
55         return TIZEN_KEYROUTER_ERROR_NO_PERMISSION;
56      }
57 #endif
58
59    if (!surface)
60      {
61         /* Regarding topmost mode, a client must request to grab a key with a valid surface. */
62         if (mode == TIZEN_KEYROUTER_MODE_TOPMOST ||
63             mode == TIZEN_KEYROUTER_MODE_REGISTERED)
64           {
65              KLWRN("Invalid surface for %d grab mode ! (key=%d)", mode, key);
66
67              return TIZEN_KEYROUTER_ERROR_INVALID_SURFACE;
68           }
69      }
70
71    /* Check the given key range */
72    if (krt->max_tizen_hwkeys < key)
73      {
74         KLWRN("Invalid range of key ! (keycode:%d)", key);
75         return TIZEN_KEYROUTER_ERROR_INVALID_KEY;
76      }
77
78    /* Check whether the key can be grabbed or not !
79     * Only key listed in Tizen key layout file can be grabbed. */
80    if (0 == krt->HardKeys[key].keycode)
81      {
82         KLWRN("Invalid key ! Disabled to grab ! (keycode:%d)", key);
83         return TIZEN_KEYROUTER_ERROR_INVALID_KEY;
84      }
85
86    /* Check whether the mode is valid or not */
87    if (TIZEN_KEYROUTER_MODE_REGISTERED < mode)
88      {
89         KLWRN("Invalid range of mode ! (mode:%d)", mode);
90         return  TIZEN_KEYROUTER_ERROR_INVALID_MODE;
91      }
92
93    /* Check whether the request key can be grabbed or not */
94    res = e_keyrouter_set_keygrab_in_list(surface, client, key, mode);
95
96    return res;
97 }
98
99 static void
100 _e_keyrouter_keycancel_send(struct wl_client *client, struct wl_resource *surface, unsigned int key)
101 {
102    Eina_List *l;
103    struct wl_resource *resource = NULL;
104    struct wl_client *wc = NULL;
105    E_Keyrouter_Key_List_NodePtr data;
106
107    if (surface) wc = wl_resource_get_client(surface);
108    else wc = client;
109
110    EINA_SAFETY_ON_NULL_RETURN(wc);
111
112    EINA_LIST_FOREACH(krt->HardKeys[key].press_ptr, l, data)
113      {
114         if (surface)
115           {
116              if (surface == data->surface)
117                {
118                   EINA_LIST_FOREACH(krt->resources, l, resource)
119                     {
120                        if (wl_resource_get_client(resource) != wc) continue;
121
122                        tizen_keyrouter_send_key_cancel(resource, key-8);
123                     }
124                }
125           }
126         else if (client == data->wc)
127           {
128              EINA_LIST_FOREACH(krt->resources, l, resource)
129                {
130                   if (wl_resource_get_client(resource) != wc) continue;
131
132                   tizen_keyrouter_send_key_cancel(resource, key-8);
133                }
134           }
135      }
136 }
137
138 static int
139 _e_keyrouter_keygrab_unset(struct wl_client *client, struct wl_resource *surface, uint32_t key)
140 {
141    /* Ungrab top position grabs first. This grab mode do not need privilege */
142    if (!surface)
143      e_keyrouter_find_and_remove_client_from_list(NULL, client, key, TIZEN_KEYROUTER_MODE_TOPMOST);
144    else
145      e_keyrouter_find_and_remove_client_from_list(surface, client, key, TIZEN_KEYROUTER_MODE_TOPMOST);
146
147 #ifdef ENABLE_CYNARA
148    if (EINA_FALSE == _e_keyrouter_util_do_privilege_check(client, TIZEN_KEYROUTER_MODE_NONE, key))
149      {
150         goto finish;
151      }
152 #endif
153
154    if (!surface)
155      {
156         /* EXCLUSIVE grab */
157         e_keyrouter_find_and_remove_client_from_list(NULL, client, key, TIZEN_KEYROUTER_MODE_EXCLUSIVE);
158
159         /* OVERRIDABLE_EXCLUSIVE grab */
160         e_keyrouter_find_and_remove_client_from_list(NULL, client, key, TIZEN_KEYROUTER_MODE_OVERRIDABLE_EXCLUSIVE);
161
162         /* SHARED grab */
163         e_keyrouter_find_and_remove_client_from_list(NULL, client, key, TIZEN_KEYROUTER_MODE_SHARED);
164
165         /* Press List */
166         e_keyrouter_find_and_remove_client_from_list(NULL, client, key, TIZEN_KEYROUTER_MODE_PRESSED);
167      }
168    else
169      {
170         /* EXCLUSIVE grab */
171         e_keyrouter_find_and_remove_client_from_list(surface, client, key, TIZEN_KEYROUTER_MODE_EXCLUSIVE);
172
173         /* OVERRIDABLE_EXCLUSIVE grab */
174         e_keyrouter_find_and_remove_client_from_list(surface, client, key, TIZEN_KEYROUTER_MODE_OVERRIDABLE_EXCLUSIVE);
175
176         /* SHARED grab */
177         e_keyrouter_find_and_remove_client_from_list(surface, client, key, TIZEN_KEYROUTER_MODE_SHARED);
178
179         /* REGISTERED grab */
180         e_keyrouter_unset_keyregister(surface, client, key);
181
182         /* Press List */
183         e_keyrouter_find_and_remove_client_from_list(surface, client, key, TIZEN_KEYROUTER_MODE_PRESSED);
184      }
185
186 finish:
187    _e_keyrouter_keycancel_send(client, surface, key);
188
189    return TIZEN_KEYROUTER_ERROR_NONE;
190 }
191
192 /* tizen_keyrouter_set_keygrab request handler */
193 static void
194 _e_keyrouter_cb_keygrab_set(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, uint32_t key, uint32_t mode)
195 {
196    int res = 0;
197
198    TRACE_INPUT_BEGIN(_e_keyrouter_cb_keygrab_set);
199
200    res = _e_keyrouter_keygrab_set(client, surface, key, mode);
201
202    TRACE_INPUT_END();
203
204    if (res == TIZEN_KEYROUTER_ERROR_NONE)
205      {
206         if (mode == TIZEN_KEYROUTER_MODE_EXCLUSIVE)
207           {
208              KLINF("Success to %d key %s grab request (wl_client: %p, wl_surface: %p, pid: %d)", key, e_keyrouter_mode_to_string(mode),
209                 client, surface, e_keyrouter_util_get_pid(client, surface));
210           }
211         else
212           {
213              KLDBG("Success to %d key %s grab request (wl_client: %p, wl_surface: %p, pid: %d)", key, e_keyrouter_mode_to_string(mode),
214                 client, surface, e_keyrouter_util_get_pid(client, surface));
215           }
216      }
217    else
218      KLINF("Failed to %d key %s grab request (wl_client: %p, wl_surface: %p, pid: %d): res: %d", key, e_keyrouter_mode_to_string(mode),
219         client, surface, e_keyrouter_util_get_pid(client, surface), res);
220    tizen_keyrouter_send_keygrab_notify(resource, surface, key, mode, res);
221 }
222
223 /* tizen_keyrouter unset_keygrab request handler */
224 static void
225 _e_keyrouter_cb_keygrab_unset(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, uint32_t key)
226 {
227    int res = 0;
228
229    TRACE_INPUT_BEGIN(_e_keyrouter_cb_keygrab_unset);
230
231    res = _e_keyrouter_keygrab_unset(client, surface, key);
232
233    TRACE_INPUT_END();
234
235    if (res == TIZEN_KEYROUTER_ERROR_NONE)
236      KLDBG("Success to %d key ungrab request (wl_client: %p, wl_surface: %p, pid: %d)", key, client, surface,
237            e_keyrouter_util_get_pid(client, surface));
238    else
239      KLINF("Failed to %d key ungrab request (wl_client: %p, wl_surface: %p, pid: %d): res: %d", key, client, surface,
240            e_keyrouter_util_get_pid(client, surface), res);
241    tizen_keyrouter_send_keygrab_notify(resource, surface, key, TIZEN_KEYROUTER_MODE_NONE, res);
242 }
243
244 /* tizen_keyrouter get_keygrab_status request handler */
245 static void
246 _e_keyrouter_cb_get_keygrab_status(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, uint32_t key)
247 {
248    (void) client;
249    (void) resource;
250    (void) surface;
251    (void) key;
252    int mode = TIZEN_KEYROUTER_MODE_NONE;
253
254    TRACE_INPUT_BEGIN(_e_keyrouter_cb_get_keygrab_status);
255    mode = e_keyrouter_find_key_in_list(surface, client, key);
256
257    TRACE_INPUT_END();
258    tizen_keyrouter_send_keygrab_notify(resource, surface, key, mode, TIZEN_KEYROUTER_ERROR_NONE);
259 }
260
261 static void
262 _e_keyrouter_cb_keygrab_set_list(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, struct wl_array *grab_list)
263 {
264    E_Keyrouter_Grab_Request *grab_request = NULL;
265    int res = TIZEN_KEYROUTER_ERROR_NONE;
266    int array_len = 0;
267
268    TRACE_INPUT_BEGIN(_e_keyrouter_cb_keygrab_set_list);
269
270    array_len = _e_keyrouter_wl_array_length(grab_list);
271
272    if (0 != (array_len % 3))
273      {
274         /* FIX ME: Which way is effectively to notify invalid pair to client */
275         KLWRN("Invalid keycode and grab mode pair. Check arguments in a list");
276         TRACE_INPUT_END();
277         tizen_keyrouter_send_keygrab_notify_list(resource, surface, NULL);
278         return;
279      }
280
281    wl_array_for_each(grab_request, grab_list)
282      {
283         res = _e_keyrouter_keygrab_set(client, surface, grab_request->key, grab_request->mode);
284         grab_request->err = res;
285         if (res == TIZEN_KEYROUTER_ERROR_NONE)
286           KLDBG("Success to %d key %s grab using list(wl_client: %p, wl_surface: %p, pid: %d)",
287                 grab_request->key, e_keyrouter_mode_to_string(grab_request->mode),
288                 client, surface, e_keyrouter_util_get_pid(client, surface));
289         else
290           KLINF("Failed to %d key %s grab using list(wl_client: %p, wl_surface: %p, pid: %d): res: %d",
291                 grab_request->key, e_keyrouter_mode_to_string(grab_request->mode),
292                 client, surface, e_keyrouter_util_get_pid(client, surface), grab_request->err);
293      }
294
295
296    TRACE_INPUT_END();
297    tizen_keyrouter_send_keygrab_notify_list(resource, surface, grab_list);
298 }
299
300 static void
301 _e_keyrouter_cb_keygrab_unset_list(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, struct wl_array *ungrab_list)
302 {
303    E_Keyrouter_Ungrab_Request *ungrab_request = NULL;
304    int res = TIZEN_KEYROUTER_ERROR_NONE;
305    int array_len = 0;
306
307    TRACE_INPUT_BEGIN(_e_keyrouter_cb_keygrab_unset_list);
308
309    array_len = _e_keyrouter_wl_array_length(ungrab_list);
310
311    if (0 != (array_len % 2))
312      {
313         /* FIX ME: Which way is effectively to notify invalid pair to client */
314         KLWRN("Invalid keycode and error pair. Check arguments in a list");
315         TRACE_INPUT_END();
316         tizen_keyrouter_send_keygrab_notify_list(resource, surface, ungrab_list);
317         return;
318      }
319
320    wl_array_for_each(ungrab_request, ungrab_list)
321      {
322         res = _e_keyrouter_keygrab_unset(client, surface, ungrab_request->key);
323         ungrab_request->err = res;
324         if (res == TIZEN_KEYROUTER_ERROR_NONE)
325           KLDBG("Success to ungrab using list: %d key (wl_client: %p, wl_surface: %p, pid: %d)",
326                 ungrab_request->key, client, surface, e_keyrouter_util_get_pid(client, surface));
327         else
328           KLINF("Failed to ungrab using list: %d key (wl_client: %p, wl_surface: %p, pid: %d): res: %d",
329                 ungrab_request->key, client, surface, e_keyrouter_util_get_pid(client, surface), ungrab_request->err);
330      }
331
332    TRACE_INPUT_END();
333    tizen_keyrouter_send_keygrab_notify_list(resource, surface, ungrab_list);
334 }
335
336 static void
337 _e_keyrouter_cb_get_keyregister_status(struct wl_client *client, struct wl_resource *resource, uint32_t key)
338 {
339    (void) client;
340    (void) key;
341
342    int delivery_mode = TIZEN_KEYROUTER_MODE_NONE;
343    Eina_Bool below_focus = EINA_FALSE;
344    E_Client *ec_top = NULL, *ec_focus = NULL;
345    struct wl_resource *surface = NULL, *surface_focus = NULL;
346    Eina_List* key_list = NULL, *l = NULL, *l_next = NULL;
347    E_Keyrouter_Key_List_NodePtr key_node_data = NULL;
348
349    int *key_data = NULL;
350    int deliver_invisible = 0;
351
352     // Check for exclusive & or_exclusive mode delivery
353    if (krt->HardKeys[key].excl_ptr)
354      {
355         delivery_mode = TIZEN_KEYROUTER_MODE_EXCLUSIVE;
356         goto finish;
357      }
358    if (krt->HardKeys[key].or_excl_ptr)
359       {
360          delivery_mode = TIZEN_KEYROUTER_MODE_OVERRIDABLE_EXCLUSIVE;
361          goto finish;
362       }
363
364    ec_top = e_client_top_get();
365    ec_focus = e_client_focused_get();
366    surface_focus = e_keyrouter_util_get_surface_from_eclient(ec_focus);
367
368    for (; ec_top != NULL; ec_top = e_client_below_get(ec_top))
369      {
370         surface = e_keyrouter_util_get_surface_from_eclient(ec_top);
371
372         if (surface == NULL)
373           {
374              // Not a valid surface.
375              continue;
376           }
377
378         if (ec_top->is_cursor) continue;
379
380         //Check Top-Most Delivery
381         if( below_focus == EINA_FALSE)
382           {
383              EINA_LIST_FOREACH_SAFE(krt->HardKeys[key].top_ptr, l, l_next, key_node_data)
384                {
385                   if (!key_node_data) continue;
386                   if ((ec_top->visible) && (ec_top == wl_resource_get_user_data(key_node_data->surface)))
387                     {
388                        delivery_mode = TIZEN_KEYROUTER_MODE_TOPMOST;
389                        goto finish;
390                     }
391                }
392           }
393
394         // Check if window stack reaches to focus window
395         if (ec_top == ec_focus)
396           {
397              below_focus = EINA_TRUE;
398           }
399
400         // Check for FORCE DELIVER to INVISIBLE WINDOW
401         if (deliver_invisible && IsInvisibleGetWindow(surface))
402           {
403              goto finish;
404           }
405
406         // Check for visible window first <Consider VISIBILITY>
407         // return if not visible
408         if (ec_top->visibility.obscured == E_VISIBILITY_FULLY_OBSCURED || ec_top->visibility.obscured == E_VISIBILITY_UNKNOWN)
409           {
410              continue;
411           }
412
413         // Set key Event Delivery for INVISIBLE WINDOW
414         if (IsInvisibleSetWindow(surface))
415           {
416              deliver_invisible = 1;
417           }
418
419         if (IsNoneKeyRegisterWindow(surface))
420           {
421              continue;
422           }
423
424         if (e_keyrouter_is_registered_window(surface))
425           {
426              // get the key list and deliver events if it has registered for that key
427              key_list = _e_keyrouter_registered_window_key_list(surface);
428              if (key_list)
429                {
430                   EINA_LIST_FOREACH(key_list, l, key_data)
431                     {
432                        if(!key_data)
433                          {
434                             continue;
435                          }
436
437                        if(*key_data == key)
438                          {
439                             delivery_mode = TIZEN_KEYROUTER_MODE_REGISTERED;
440                             goto finish;
441                          }
442                     }
443                }
444           }
445
446         if (surface != surface_focus)
447           {
448              if (below_focus == EINA_FALSE)
449                {
450                   continue;
451                }
452
453              // Deliver to below Non Registered window
454              else if (!e_keyrouter_is_registered_window(surface))
455                {
456                   goto finish;
457                }
458              else
459                {
460                   continue;
461                }
462           }
463         else
464           {
465              // Deliver to Focus Surface window from FOCUS MODE
466              if (!e_keyrouter_is_registered_window(surface))
467                {
468                   goto finish;
469                }
470              else
471                {
472                   continue;
473                }
474           }
475     }
476
477    finish:
478    tizen_keyrouter_send_keyregister_notify(resource, (int)delivery_mode);
479 }
480
481 static void
482 _e_keyrouter_cb_set_input_config(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, uint32_t config_mode, uint32_t value)
483 {
484    Eina_Bool res = EINA_TRUE;
485
486    if (surface == NULL && config_mode != TIZEN_KEYROUTER_CONFIG_MODE_PICTURE_OFF)
487      {
488         KLWRN("Error Surface is NULL");
489         res = EINA_FALSE;
490         goto send_input_config_notify;
491      }
492
493    switch (config_mode)
494      {
495         case TIZEN_KEYROUTER_CONFIG_MODE_INVISIBLE_SET:
496            if (value)
497              {
498                 krt->invisible_set_window_list= eina_list_append(krt->invisible_set_window_list, surface);
499              }
500            else
501              {
502                 krt->invisible_set_window_list= eina_list_remove(krt->invisible_set_window_list, surface);
503              }
504            break;
505
506         case KRT_IPD_INPUT_CONFIG:
507            krt->playback_daemon_surface = surface;
508            KLINF("Registered playback daemon wl_surface: %p",surface);
509            break;
510
511         case TIZEN_KEYROUTER_CONFIG_MODE_INVISIBLE_GET:
512            if (value)
513              {
514                 krt->invisible_get_window_list= eina_list_append(krt->invisible_get_window_list, surface);
515              }
516            else
517              {
518                 krt->invisible_get_window_list= eina_list_remove(krt->invisible_get_window_list, surface);
519              }
520            break;
521
522         case TIZEN_KEYROUTER_CONFIG_MODE_NUM_KEY_FOCUS:
523             // to do ;
524             break;
525
526         case TIZEN_KEYROUTER_CONFIG_MODE_PICTURE_OFF:
527             res = e_keyrouter_prepend_to_keylist(surface, surface ? NULL : client, value, TIZEN_KEYROUTER_MODE_PICTURE_OFF, EINA_FALSE);
528             /* As surface/client destroy listener got added in e_keyrouter_prepend_to_keylist() function already */
529             value = 0;
530             break;
531
532         default:
533             KLWRN("Invalid mode: %d", config_mode);
534             res= EINA_FALSE;
535             goto send_input_config_notify;
536      }
537
538    if (value)
539      {
540         KLDBG("Add a wl_surface(%p) to destory listener", surface);
541         e_keyrouter_add_surface_destroy_listener(surface);
542      }
543
544 send_input_config_notify:
545    if (res == TIZEN_KEYROUTER_ERROR_NONE)
546      {
547         KLDBG("Success to set input config: wl_surface (%p) for mode %d with value (%d)", surface, config_mode, value);
548      }
549    else
550      {
551         KLINF("Failed to set input config (res: %d): wl_surface (%p) for mode %d with value (%d)", res, surface, config_mode, value);
552      }
553    tizen_keyrouter_send_set_input_config_notify(resource, (int)res);
554 }
555
556 /* tizen_keyrouter check if given surface in register none key list */
557 Eina_Bool
558 IsNoneKeyRegisterWindow(struct wl_resource *surface)
559 {
560    struct wl_resource *surface_ldata = NULL;
561    Eina_List *l = NULL, *l_next = NULL;
562
563    EINA_LIST_FOREACH_SAFE (krt->registered_none_key_window_list, l, l_next, surface_ldata)
564      {
565         if (surface_ldata == surface)
566           {
567              KLDBG("Given wl_surface(%p) is in NoneKeyRegisterWindow", surface);
568              return EINA_TRUE;
569           }
570      }
571    return EINA_FALSE;
572 }
573
574 /* tizen_keyrouter check if given surface in register invisible set list */
575 Eina_Bool
576 IsInvisibleSetWindow(struct wl_resource *surface)
577 {
578    struct wl_resource *surface_ldata = NULL;
579    Eina_List *l = NULL, *l_next = NULL;
580
581    EINA_LIST_FOREACH_SAFE(krt->invisible_set_window_list, l, l_next, surface_ldata)
582      {
583         if (surface_ldata == surface)
584           {
585              return EINA_TRUE;
586           }
587      }
588    return EINA_FALSE;
589 }
590
591 /* tizen_keyrouter check if given surface in register invisible get list */
592 Eina_Bool
593 IsInvisibleGetWindow(struct wl_resource *surface)
594 {
595    struct wl_resource *surface_ldata = NULL;
596    Eina_List *l = NULL, *l_next = NULL;
597
598    EINA_LIST_FOREACH_SAFE(krt->invisible_get_window_list, l, l_next, surface_ldata)
599      {
600         if (surface_ldata == surface)
601           {
602              return EINA_TRUE;
603           }
604      }
605    return EINA_FALSE;
606 }
607
608 static void
609 _e_keyrouter_cb_set_register_none_key(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, uint32_t data)
610 {
611    (void) client;
612
613    // Register None key set/get
614    krt->register_none_key = data;
615    if (krt->register_none_key)
616      {
617         krt->registered_none_key_window_list = eina_list_append(krt->registered_none_key_window_list, surface);
618         if (surface)
619           {
620              e_keyrouter_add_surface_destroy_listener(surface);
621              /* TODO: if failed add surface_destory_listener, remove keygrabs */
622           }
623      }
624    else
625      {
626         krt->registered_none_key_window_list = eina_list_remove(krt->registered_none_key_window_list, surface);
627      }
628
629    KLDBG("Set Registered None Key called on wl_surface (%p) with data (%d)", surface, krt->register_none_key);
630    tizen_keyrouter_send_set_register_none_key_notify(resource, NULL, krt->register_none_key);
631 }
632
633 static void
634 _e_keyrouter_cb_keygrab_get_list(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface)
635 {
636    E_Keyrouter_Key_List_NodePtr key_node_data = NULL;
637    struct wl_array grab_result_list = {0,};
638    E_Keyrouter_Grab_Request *grab_result = NULL;
639    E_Keyrouter_Registered_Window_Info *rwin_info = NULL;
640    Eina_List *l = NULL, *ll = NULL, *l_next = NULL;
641    int *key_data;
642    int i;
643
644    wl_array_init(&grab_result_list);
645
646    for (i = 0; i < krt->max_tizen_hwkeys; i++)
647      {
648         if (0 == krt->HardKeys[i].keycode) continue;
649
650         EINA_LIST_FOREACH_SAFE(krt->HardKeys[i].excl_ptr, l, l_next, key_node_data)
651           {
652              if (surface == key_node_data->surface)
653                {
654                   grab_result = wl_array_add(&grab_result_list, sizeof(E_Keyrouter_Grab_Request));
655                   if (grab_result)
656                     {
657                        grab_result->key = i;
658                        grab_result->mode = TIZEN_KEYROUTER_MODE_EXCLUSIVE;
659                        grab_result->err = TIZEN_KEYROUTER_ERROR_NONE;
660                     }
661                }
662           }
663         EINA_LIST_FOREACH_SAFE(krt->HardKeys[i].or_excl_ptr, l, l_next, key_node_data)
664           {
665              if (surface == key_node_data->surface)
666                {
667                   grab_result = wl_array_add(&grab_result_list, sizeof(E_Keyrouter_Grab_Request));
668                   if (grab_result)
669                     {
670                        grab_result->key = i;
671                        grab_result->mode = TIZEN_KEYROUTER_MODE_OVERRIDABLE_EXCLUSIVE;
672                        grab_result->err = TIZEN_KEYROUTER_ERROR_NONE;
673                     }
674                }
675           }
676         EINA_LIST_FOREACH_SAFE(krt->HardKeys[i].top_ptr, l, l_next, key_node_data)
677           {
678              if (surface == key_node_data->surface)
679                {
680                   grab_result = wl_array_add(&grab_result_list, sizeof(E_Keyrouter_Grab_Request));
681                   if (grab_result)
682                     {
683                        grab_result->key = i;
684                        grab_result->mode = TIZEN_KEYROUTER_MODE_TOPMOST;
685                        grab_result->err = TIZEN_KEYROUTER_ERROR_NONE;
686                     }
687                }
688           }
689         EINA_LIST_FOREACH_SAFE(krt->HardKeys[i].shared_ptr, l, l_next, key_node_data)
690           {
691              if (surface == key_node_data->surface)
692                {
693                   grab_result = wl_array_add(&grab_result_list, sizeof(E_Keyrouter_Grab_Request));
694                   if (grab_result)
695                     {
696                        grab_result->key = i;
697                        grab_result->mode = TIZEN_KEYROUTER_MODE_SHARED;
698                        grab_result->err = TIZEN_KEYROUTER_ERROR_NONE;
699                     }
700                }
701           }
702      }
703    // handle register mode here
704    EINA_LIST_FOREACH(krt->registered_window_list, l, rwin_info)
705      {
706         if (rwin_info->surface == surface)
707           {
708              EINA_LIST_FOREACH(rwin_info->keys, ll, key_data)
709                {
710                   grab_result = wl_array_add(&grab_result_list, sizeof(E_Keyrouter_Grab_Request));
711                   if (grab_result)
712                     {
713                        grab_result->key = *key_data;
714                        grab_result->mode = TIZEN_KEYROUTER_MODE_REGISTERED;
715                        grab_result->err = TIZEN_KEYROUTER_ERROR_NONE;
716                     }
717                }
718           }
719      }
720
721    tizen_keyrouter_send_getgrab_notify_list(resource, surface, &grab_result_list);
722    wl_array_release(&grab_result_list);
723 }
724
725 /* Function for registering wl_client destroy listener */
726 int
727 e_keyrouter_add_client_destroy_listener(struct wl_client *client)
728 {
729    struct wl_listener *destroy_listener = NULL;
730    Eina_List *l;
731    struct wl_client *wc_data;
732
733    if (!client) return TIZEN_KEYROUTER_ERROR_NONE;
734
735    EINA_LIST_FOREACH(krt->grab_client_list, l, wc_data)
736      {
737         if (wc_data)
738           {
739              if (wc_data == client)
740                {
741                   return TIZEN_KEYROUTER_ERROR_NONE;
742                }
743           }
744      }
745
746    destroy_listener = E_NEW(struct wl_listener, 1);
747
748    if (!destroy_listener)
749      {
750         KLERR("Failed to allocate memory for wl_client destroy listener !");
751         return TIZEN_KEYROUTER_ERROR_NO_SYSTEM_RESOURCES;
752      }
753
754    destroy_listener->notify = _e_keyrouter_wl_client_cb_destroy;
755    wl_client_add_destroy_listener(client, destroy_listener);
756    krt->grab_client_list = eina_list_append(krt->grab_client_list, client);
757
758    return TIZEN_KEYROUTER_ERROR_NONE;
759 }
760
761 /* Function for registering wl_surface destroy listener */
762 int
763 e_keyrouter_add_surface_destroy_listener(struct wl_resource *surface)
764 {
765    struct wl_listener *destroy_listener = NULL;
766    Eina_List *l;
767    struct wl_resource *surface_data;
768
769    if (!surface) return TIZEN_KEYROUTER_ERROR_NONE;
770
771    EINA_LIST_FOREACH(krt->grab_surface_list, l, surface_data)
772      {
773         if (surface_data)
774           {
775              if (surface_data == surface)
776                {
777                   return TIZEN_KEYROUTER_ERROR_NONE;
778                }
779           }
780      }
781
782    destroy_listener = E_NEW(struct wl_listener, 1);
783
784    if (!destroy_listener)
785      {
786         KLERR("Failed to allocate memory for wl_surface destroy listener !");
787         return TIZEN_KEYROUTER_ERROR_NO_SYSTEM_RESOURCES;
788      }
789
790    destroy_listener->notify = _e_keyrouter_wl_surface_cb_destroy;
791    wl_resource_add_destroy_listener(surface, destroy_listener);
792    krt->grab_surface_list = eina_list_append(krt->grab_surface_list, surface);
793
794    return TIZEN_KEYROUTER_ERROR_NONE;
795 }
796
797
798 static const struct tizen_keyrouter_interface _e_keyrouter_implementation = {
799    _e_keyrouter_cb_keygrab_set,
800    _e_keyrouter_cb_keygrab_unset,
801    _e_keyrouter_cb_get_keygrab_status,
802    _e_keyrouter_cb_keygrab_set_list,
803    _e_keyrouter_cb_keygrab_unset_list,
804    _e_keyrouter_cb_keygrab_get_list,
805    _e_keyrouter_cb_set_register_none_key,
806    _e_keyrouter_cb_get_keyregister_status,
807    _e_keyrouter_cb_set_input_config
808 };
809
810 /* tizen_keyrouter global object destroy function */
811 static void
812 _e_keyrouter_cb_destory(struct wl_resource *resource)
813 {
814    krt->resources = eina_list_remove(krt->resources, resource);
815 }
816
817 /* tizen_keyrouter global object bind function */
818 static void
819 _e_keyrouter_cb_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
820 {
821    E_KeyrouterPtr krt_instance = data;
822    struct wl_resource *resource;
823
824    resource = wl_resource_create(client, &tizen_keyrouter_interface, MIN(version, 1), id);
825
826    KLDBG("wl_resource_create(...,&tizen_keyrouter_interface,...)");
827
828    if (!resource)
829      {
830         KLERR("Failed to create resource ! (version :%d, id:%d)", version, id);
831         wl_client_post_no_memory(client);
832          return;
833      }
834
835    krt->resources = eina_list_append(krt->resources, resource);
836
837    wl_resource_set_implementation(resource, &_e_keyrouter_implementation, krt_instance, _e_keyrouter_cb_destory);
838 }
839
840 static void
841 _e_keyrouter_keygrab_status_print(FILE *log_fl, Eina_List *list)
842 {
843    Eina_List *l;
844    E_Keyrouter_Key_List_NodePtr kdata;
845    int pid;
846    char *cmd;
847
848    EINA_LIST_FOREACH(list, l, kdata)
849      {
850         pid = e_keyrouter_util_get_pid(kdata->wc, kdata->surface);
851         cmd = e_keyrouter_util_cmd_get_from_pid(pid);
852         fprintf(log_fl, "                [surface: %p, client: %p, pid: %d(%s)]\n", kdata->surface, kdata->wc, pid, cmd ?: "Unknown");
853         if(cmd) E_FREE(cmd);
854         if (kdata->surface)
855           {
856              fprintf(log_fl, "                    -- Surface Information --\n");
857              fprintf(log_fl, "                        = client: %p\n", wl_resource_get_client(kdata->surface));
858              fprintf(log_fl, "                        = resource: %s(%d)\n", wl_resource_get_name(kdata->surface), wl_resource_get_id(kdata->surface));
859           }
860         else
861           {
862              fprintf(log_fl, "                    -- Client Information --\n");
863              fprintf(log_fl, "                        = connected fd: %d\n", wl_client_get_fd(kdata->wc));
864           }
865      }
866 }
867
868
869 static void
870 _e_keyrouter_info_print(void *data, const char *log_path)
871 {
872    char *keyname, *cmd;
873    int  i, c, pid, *idata;
874    FILE *log_fl;
875    Eina_List *l, *ll;
876    E_Keyrouter_Registered_Window_Info *rdata;
877
878    log_fl = fopen(log_path, "a");
879    if (!log_fl)
880      {
881         KLERR("failed: open file(%s)", log_path);
882         return;
883      }
884
885    setvbuf(log_fl, NULL, _IOLBF, 512);
886
887    fprintf(log_fl, "\n===== Keyrouter Information =====\n");
888    fprintf(log_fl, "    ----- Grabbable Keys -----\n");
889    for (i = 8; i < krt->max_tizen_hwkeys; i++)
890      {
891         if (!krt->HardKeys[i].keycode) continue;
892
893         keyname = e_keyrouter_util_keyname_get_from_keycode(i);
894
895         fprintf(log_fl, "         Key [%3d], Keyname: %s\n", i, keyname);
896
897         free(keyname);
898         keyname = NULL;
899      }
900    fprintf(log_fl, "    ----- End -----\n\n");
901
902    fprintf(log_fl, "    ----- Register Window List -----\n");
903    EINA_LIST_FOREACH(krt->registered_window_list, l, rdata)
904      {
905         pid = e_keyrouter_util_get_pid(NULL, rdata->surface);
906         cmd = e_keyrouter_util_cmd_get_from_pid(pid);
907         fprintf(log_fl, "        [ surface: %p, client: %p, pid: %d(%s) ]\n",
908                          rdata->surface, wl_resource_get_client(rdata->surface), pid, cmd ?: "Unknown");
909         if(cmd) E_FREE(cmd);
910         c = 0;
911         EINA_LIST_FOREACH(rdata->keys, ll, idata)
912           {
913              keyname = e_keyrouter_util_keyname_get_from_keycode(*idata);
914              if (c == 0)
915                fprintf(log_fl, "            registered key: Key [%3d], Keyname: %s\n", *idata, keyname);
916              else
917                fprintf(log_fl, "                            Key [%3d], Keyname: %s\n", *idata, keyname);
918              c++;
919              free(keyname);
920              keyname = NULL;
921           }
922      }
923    fprintf(log_fl, "    ----- End -----\n\n");
924
925    fclose(log_fl);
926    log_fl = NULL;
927 }
928
929 static void
930 _e_keyrouter_keygrab_print(void *data, const char *log_path)
931 {
932    Eina_List *l;
933    E_Keyrouter_Key_List_NodePtr kdata;
934    E_Client *ec_focus;
935    struct wl_resource *surface_focus;
936    struct wl_client *wc_focus;
937    int pid_focus, pid, i;
938    char *cmd_focus, *cmd, *keyname;
939    FILE *log_fl;
940
941    (void) data;
942
943    log_fl = fopen(log_path, "a");
944    if (!log_fl)
945      {
946         KLERR("failed: open file(%s)", log_path);
947         return;
948      }
949
950    setvbuf(log_fl, NULL, _IOLBF, 512);
951
952    fprintf(log_fl, "\n===== Keygrab Status =====\n");
953
954    ec_focus = e_client_focused_get();
955    fprintf(log_fl, "    ----- Focus Window Info -----\n");
956    if (ec_focus)
957      {
958         surface_focus = e_keyrouter_util_get_surface_from_eclient(ec_focus);
959         wc_focus = wl_resource_get_client(surface_focus);
960         pid_focus = e_keyrouter_util_get_pid(NULL, surface_focus);
961         cmd_focus = e_keyrouter_util_cmd_get_from_pid(pid_focus);
962
963         fprintf(log_fl, "        Focus Client: E_Client: %p\n", ec_focus);
964         fprintf(log_fl, "                      Surface: %p, Client: %p\n", surface_focus, wc_focus);
965         fprintf(log_fl, "                      pid: %d, cmd: %s\n", pid_focus, cmd_focus ?: "Unknown");
966         if(cmd_focus) E_FREE(cmd_focus);
967      }
968    else
969      {
970         fprintf(log_fl, "        No Focus Client\n");
971      }
972    fprintf(log_fl, "    ----- End -----\n\n");
973
974    fprintf(log_fl, "    ----- Grabbed keys Info -----\n\n");
975    for (i = 8; i < krt->max_tizen_hwkeys; i++)
976      {
977         if (!krt->HardKeys[i].keycode) continue;
978         if (!krt->HardKeys[i].excl_ptr &&
979             !krt->HardKeys[i].or_excl_ptr &&
980             !krt->HardKeys[i].top_ptr &&
981             !krt->HardKeys[i].shared_ptr &&
982             !krt->HardKeys[i].registered_ptr)
983           continue;
984
985         keyname = e_keyrouter_util_keyname_get_from_keycode(i);
986
987         fprintf(log_fl, "        [ Keycode: %d, Keyname: %s ]\n", i, keyname);
988
989         free(keyname);
990         keyname = NULL;
991
992         if (krt->HardKeys[i].excl_ptr)
993           {
994              fprintf(log_fl, "            == Exclusive Grab ==\n");
995              EINA_LIST_FOREACH(krt->HardKeys[i].excl_ptr, l, kdata)
996                {
997                   pid = e_keyrouter_util_get_pid(kdata->wc, kdata->surface);
998                   cmd = e_keyrouter_util_cmd_get_from_pid(pid);
999                   fprintf(log_fl, "                [surface: %p, client: %p, pid: %d(%s)]\n", kdata->surface, kdata->wc, pid, cmd ?: "Unknown");
1000                   if(cmd) E_FREE(cmd);
1001                   if (kdata->surface)
1002                     {
1003                        fprintf(log_fl, "                    -- Surface Information --\n");
1004                        fprintf(log_fl, "                        = wl_client: %p\n", wl_resource_get_client(kdata->surface));
1005                        fprintf(log_fl, "                        = resource: %s(%d)\n", wl_resource_get_name(kdata->surface), wl_resource_get_id(kdata->surface));
1006                     }
1007                   else
1008                     {
1009                        fprintf(log_fl, "                    -- Client Information --\n");
1010                        fprintf(log_fl, "                        = connected fd: %d\n", wl_client_get_fd(kdata->wc));
1011                     }
1012                   break;
1013                }
1014             }
1015
1016         if (krt->HardKeys[i].or_excl_ptr)
1017           {
1018              fprintf(log_fl, "            == Overidable Exclusive Grab ==\n");
1019              _e_keyrouter_keygrab_status_print(log_fl, krt->HardKeys[i].or_excl_ptr);
1020           }
1021
1022         if (krt->HardKeys[i].top_ptr)
1023           {
1024              fprintf(log_fl, "            == Top Position Grab ==\n");
1025              _e_keyrouter_keygrab_status_print(log_fl, krt->HardKeys[i].top_ptr);
1026           }
1027
1028         if (krt->HardKeys[i].shared_ptr)
1029           {
1030              fprintf(log_fl, "            == Shared Grab ==\n");
1031              _e_keyrouter_keygrab_status_print(log_fl, krt->HardKeys[i].shared_ptr);
1032           }
1033
1034         fprintf(log_fl, "\n");
1035      }
1036
1037    fprintf(log_fl, "    ----- End -----\n\n");
1038
1039    fclose(log_fl);
1040    log_fl = NULL;
1041 }
1042
1043 static Eina_Bool
1044 _event_filter(void *data, void *loop_data EINA_UNUSED, int type, void *event)
1045 {
1046    (void) data;
1047    (void) type;
1048    (void) event;
1049
1050    Ecore_Event_Key *ev;
1051
1052    /* Filter only for key down/up event */
1053    if (ECORE_EVENT_KEY_DOWN == type || ECORE_EVENT_KEY_UP == type)
1054      {
1055         ev = event;
1056
1057         if (ECORE_EVENT_KEY_DOWN == type)
1058           {
1059              if (event)
1060                {
1061                   TRACE_INPUT_BEGIN(event_filter:KEY_PRESS(%d), ev->keycode);
1062                   TRACE_INPUT_END();
1063                }
1064              else
1065                {
1066                   TRACE_INPUT_BEGIN(event_filter:KEY_PRESS);
1067                   TRACE_INPUT_END();
1068                }
1069
1070           }
1071         else if (ECORE_EVENT_KEY_UP == type)
1072           {
1073
1074              if (event)
1075                {
1076                   TRACE_INPUT_BEGIN(event_filter:KEY_RELEASE(%d), ev->keycode);
1077                   TRACE_INPUT_END();
1078                }
1079              else
1080                {
1081                   TRACE_INPUT_BEGIN(event_filter:KEY_RELEASE);
1082                   TRACE_INPUT_END();
1083                }
1084
1085           }
1086         return e_keyrouter_process_key_event(event, type);
1087      }
1088
1089    return EINA_TRUE;
1090 }
1091
1092 static void
1093 _e_keyrouter_cb_power_change(device_callback_e type, void* value, void* user_data)
1094 {
1095    if (type != DEVICE_CALLBACK_DISPLAY_STATE)
1096      {
1097         KLWRN("type is not DISPLAY_STATE");
1098         return;
1099      }
1100
1101    display_state_e state = (display_state_e)value;
1102    switch (state)
1103      {
1104         case DISPLAY_STATE_SCREEN_OFF:
1105            krt->isPictureOffEnabled = 1;
1106            break;
1107         case DISPLAY_STATE_NORMAL:
1108            krt->isPictureOffEnabled = 0;
1109            break;
1110         case DISPLAY_STATE_SCREEN_DIM:
1111            krt->isPictureOffEnabled = 0;
1112            break;
1113         default:
1114            krt->isPictureOffEnabled = 0;
1115            break;
1116      }
1117    KLDBG("Picture off flag changed to %d", krt->isPictureOffEnabled);
1118 }
1119
1120 static Eina_Bool _e_keyrouter_cb_idler(void *data)
1121 {
1122     device_add_callback(DEVICE_CALLBACK_DISPLAY_STATE,_e_keyrouter_cb_power_change,NULL);
1123     return ECORE_CALLBACK_CANCEL;
1124 }
1125
1126
1127 static E_Keyrouter_Config_Data *
1128 _e_keyrouter_init(E_Module *m)
1129 {
1130    E_Keyrouter_Config_Data *kconfig = NULL;
1131    krt = E_NEW(E_Keyrouter, 1);
1132    Eina_Bool res = EINA_FALSE;
1133    int ret;
1134
1135    TRACE_INPUT_BEGIN(_e_keyrouter_init);
1136
1137    _keyrouter_log_dom = eina_log_domain_register("e-keyrouter", EINA_COLOR_RED);
1138    if (_keyrouter_log_dom < 0)
1139      {
1140         KLERR("Failed to set eina_log_domain (%d)\n", _keyrouter_log_dom);
1141         return NULL;
1142      }
1143    eina_log_domain_level_set("e-keyrouter", EINA_LOG_LEVEL_INFO);
1144
1145    if (!krt)
1146      {
1147         KLERR("Failed to allocate memory for krt !");
1148         TRACE_INPUT_END();
1149         return NULL;
1150      }
1151
1152    if (!e_comp)
1153      {
1154         KLERR("Failed to initialize keyrouter module ! (e_comp == NULL)");
1155         goto err;
1156      }
1157
1158    kconfig = E_NEW(E_Keyrouter_Config_Data, 1);
1159    EINA_SAFETY_ON_NULL_GOTO(kconfig, err);
1160
1161    kconfig->module = m;
1162
1163    e_keyrouter_conf_init(kconfig);
1164    EINA_SAFETY_ON_NULL_GOTO(kconfig->conf, err);
1165    krt->conf = kconfig;
1166    krt->pictureoff_disabled = !!kconfig->conf->pictureoff_disabled;
1167
1168    /* Get keyname and keycode pair from Tizen Key Layout file */
1169    res = _e_keyrouter_query_tizen_key_table();
1170    EINA_SAFETY_ON_FALSE_GOTO(res, err);
1171
1172    /* Add filtering mechanism */
1173    krt->ef_handler = ecore_event_filter_add(NULL, _event_filter, NULL, NULL);
1174    //ecore handler add for power callback registration
1175    if (!krt->pictureoff_disabled)
1176      ecore_idle_enterer_add(_e_keyrouter_cb_idler, NULL);
1177    _e_keyrouter_init_handlers();
1178
1179    krt->global = wl_global_create(e_comp_wl->wl.disp, &tizen_keyrouter_interface, 1, krt, _e_keyrouter_cb_bind);
1180    if (!krt->global)
1181      {
1182         KLERR("Failed to create global !");
1183         goto err;
1184      }
1185
1186 #ifdef ENABLE_CYNARA
1187    ret = cynara_initialize(&krt->p_cynara, NULL);
1188    if (EINA_UNLIKELY(CYNARA_API_SUCCESS != ret))
1189      {
1190         _e_keyrouter_util_cynara_log("cynara_initialize", ret);
1191         krt->p_cynara = NULL;
1192      }
1193 #endif
1194
1195    TRACE_INPUT_END();
1196    return kconfig;
1197
1198 err:
1199    if (kconfig)
1200      {
1201         e_keyrouter_conf_deinit(kconfig);
1202         E_FREE(kconfig);
1203      }
1204    _e_keyrouter_deinit_handlers();
1205    if (krt && krt->ef_handler) ecore_event_filter_del(krt->ef_handler);
1206    if (krt) E_FREE(krt);
1207
1208    TRACE_INPUT_END();
1209    return NULL;
1210 }
1211
1212 E_API void *
1213 e_modapi_init(E_Module *m)
1214 {
1215    return _e_keyrouter_init(m);
1216 }
1217
1218 E_API int
1219 e_modapi_shutdown(E_Module *m)
1220 {
1221    int i;
1222    Eina_List *l, *l_next;
1223    E_Keyrouter_Config_Data *kconfig = m->data;
1224    struct wl_resource *resource;
1225    struct wl_client *client;
1226    struct wl_listener *destroy_listener;
1227
1228    e_keyrouter_conf_deinit(kconfig);
1229    E_FREE(kconfig);
1230
1231    _e_keyrouter_deinit_handlers();
1232
1233    for (i = 0; i < krt->max_tizen_hwkeys+1; i++)
1234      {
1235         if (krt->HardKeys[i].keyname)
1236           eina_stringshare_del(krt->HardKeys[i].keyname);
1237      }
1238    E_FREE(krt->HardKeys);
1239
1240    EINA_LIST_FOREACH_SAFE(krt->grab_client_list, l, l_next, client)
1241      {
1242         destroy_listener = wl_client_get_destroy_listener(client, _e_keyrouter_wl_client_cb_destroy);
1243         if (destroy_listener)
1244           {
1245              wl_list_remove(&destroy_listener->link);
1246              E_FREE(destroy_listener);
1247           }
1248         krt->grab_client_list = eina_list_remove(krt->grab_client_list, client);
1249      }
1250    EINA_LIST_FOREACH_SAFE(krt->grab_surface_list, l, l_next, resource)
1251      {
1252         destroy_listener = wl_resource_get_destroy_listener(resource, _e_keyrouter_wl_surface_cb_destroy);
1253         if (destroy_listener)
1254           {
1255              wl_list_remove(&destroy_listener->link);
1256              E_FREE(destroy_listener);
1257           }
1258         krt->grab_surface_list = eina_list_remove(krt->grab_surface_list, client);
1259      }
1260
1261    EINA_LIST_FREE(krt->registered_window_list, resource);
1262
1263    EINA_LIST_FREE(krt->resources, resource)
1264      wl_resource_destroy(resource);
1265
1266    wl_global_destroy(krt->global);
1267
1268 #ifdef ENABLE_CYNARA
1269    if (krt->p_cynara) cynara_finish(krt->p_cynara);
1270 #endif
1271
1272    E_FREE(krt);
1273    /* TODO: free allocated memory */
1274
1275    eina_log_domain_unregister(_keyrouter_log_dom);
1276
1277    return 1;
1278 }
1279
1280 E_API int
1281 e_modapi_save(E_Module *m)
1282 {
1283    /* Save something to be kept */
1284    E_Keyrouter_Config_Data *kconfig = m->data;
1285    e_config_domain_save("module.keyrouter",
1286                         kconfig->conf_edd,
1287                         kconfig->conf);
1288
1289    return 1;
1290 }
1291
1292 /* Function for getting keyname/keycode information from a key layout file */
1293 static Eina_Bool
1294 _e_keyrouter_query_tizen_key_table(void)
1295 {
1296    E_Keyrouter_Conf_Edd *kconf = krt->conf->conf;
1297    Eina_List *l;
1298    E_Keyrouter_Tizen_HWKey *data;
1299    int res;
1300    struct xkb_rule_names names={0,};
1301
1302    /* TODO: Make struct in HardKeys to pointer.
1303                   If a key is defined, allocate memory to pointer,
1304                   that makes to save unnecessary memory */
1305    krt->HardKeys = E_NEW(E_Keyrouter_Grabbed_Key, kconf->max_keycode + 1);
1306    EINA_SAFETY_ON_NULL_RETURN_VAL(krt->HardKeys, EINA_FALSE);
1307
1308    krt->numTizenHWKeys = kconf->num_keycode;
1309    krt->max_tizen_hwkeys = kconf->max_keycode;
1310
1311    EINA_LIST_FOREACH(kconf->KeyList, l, data)
1312      {
1313         if (!data) continue;
1314
1315         if (0 > data->keycode || krt->max_tizen_hwkeys < data->keycode)
1316           {
1317              KLWRN("Given keycode(%d) is invalid. It must be bigger than zero, smaller than the maximum value(%d) or equal to it.", data->keycode, kconf->max_keycode);
1318              continue;
1319           }
1320
1321         krt->HardKeys[data->keycode].keycode = data->keycode;
1322         krt->HardKeys[data->keycode].keyname = (char *)eina_stringshare_add(data->name);
1323         krt->HardKeys[data->keycode].no_privcheck = data->no_privcheck ? EINA_TRUE : EINA_FALSE;
1324         krt->HardKeys[data->keycode].repeat = data->repeat ? EINA_TRUE : EINA_FALSE;
1325
1326         if (e_comp_wl_input_keymap_cache_file_use_get() == EINA_FALSE)
1327           {
1328              if (krt->HardKeys[data->keycode].repeat == EINA_FALSE)
1329                {
1330                   res = xkb_keymap_key_set_repeats(e_comp_wl->xkb.keymap, data->keycode, 0);
1331                   if (!res)
1332                     {
1333                        KLWRN("Failed to set repeat key(%d), value(%d)", data->keycode, 0);
1334                     }
1335                }
1336           }
1337      }
1338
1339    if (e_comp_wl_input_keymap_cache_file_use_get() == EINA_FALSE)
1340      {
1341         KLINF("Server create a new cache file: %s", e_comp_wl_input_keymap_path_get(names));
1342         res = unlink(e_comp_wl_input_keymap_path_get(names));
1343
1344         e_comp_wl_input_keymap_set(NULL, NULL, NULL, NULL, NULL, xkb_context_ref(e_comp_wl->xkb.context), xkb_keymap_ref(e_comp_wl->xkb.keymap));
1345      }
1346    else
1347      KLINF("Currently cache file is exist. Do not change it.");
1348
1349    return EINA_TRUE;
1350 }
1351
1352 static int
1353 _e_keyrouter_wl_array_length(const struct wl_array *array)
1354 {
1355    int *data = NULL;
1356    int count = 0;
1357
1358    wl_array_for_each(data, array)
1359      {
1360         count++;
1361      }
1362
1363    return count;
1364 }
1365
1366 static void
1367 _e_keyrouter_deinit_handlers(void)
1368 {
1369    Ecore_Event_Handler *h = NULL;
1370
1371    if (!krt ||  !krt->handlers) return;
1372
1373    EINA_LIST_FREE(krt->handlers, h)
1374      ecore_event_handler_del(h);
1375
1376    e_info_server_hook_set("keyrouter", NULL, NULL);
1377    e_info_server_hook_set("keygrab", NULL, NULL);
1378 }
1379
1380 static void
1381 _e_keyrouter_init_handlers(void)
1382 {
1383    E_LIST_HANDLER_APPEND(krt->handlers, E_EVENT_CLIENT_STACK, _e_keyrouter_client_cb_stack, NULL);
1384    E_LIST_HANDLER_APPEND(krt->handlers, E_EVENT_CLIENT_REMOVE, _e_keyrouter_client_cb_remove, NULL);
1385
1386    e_info_server_hook_set("keyrouter", _e_keyrouter_info_print, NULL);
1387    e_info_server_hook_set("keygrab", _e_keyrouter_keygrab_print, NULL);
1388 }
1389
1390 static Eina_Bool
1391 _e_keyrouter_client_cb_stack(void *data, int type, void *event)
1392 {
1393    E_Event_Client *ev = event;
1394    E_Client *ec = ev->ec;
1395
1396    (void) data;
1397    (void) type;
1398    (void) event;
1399    (void) ev;
1400    (void) ec;
1401
1402    //KLDBG("ec: %p, visibile: %d, focused: %d, take_focus: %d, want_focus: %d, bordername: %s, input_only: %d",
1403    //        ec, ec->visible, ec->focused, ec->take_focus, ec->want_focus, ec->bordername, ec->input_only);
1404
1405    krt->isWindowStackChanged = EINA_TRUE;
1406    e_keyrouter_clear_registered_window();
1407
1408    return ECORE_CALLBACK_PASS_ON;
1409 }
1410
1411 static Eina_Bool
1412 _e_keyrouter_client_cb_remove(void *data, int type, void *event)
1413 {
1414    E_Event_Client *ev = event;
1415    E_Client *ec = ev->ec;
1416
1417    (void) data;
1418    (void) type;
1419    (void) ev;
1420    (void) ec;
1421
1422    /* FIXME: Remove this callback or do something others.
1423     *             It was moved to _e_keyrouter_wl_surface_cb_destroy() where it had here.
1424     */
1425
1426    return ECORE_CALLBACK_PASS_ON;
1427 }
1428
1429 static void
1430 _e_keyrouter_wl_client_cb_destroy(struct wl_listener *l, void *data)
1431 {
1432    struct wl_client *client = data;
1433
1434    KLDBG("Listener(%p) called: wl_client: %p is died", l, client);
1435    e_keyrouter_remove_client_from_list(NULL, client);
1436
1437    wl_list_remove(&l->link);
1438    E_FREE(l);
1439
1440    krt->grab_client_list = eina_list_remove(krt->grab_client_list, client);
1441 }
1442
1443 static void
1444 _e_keyrouter_wl_surface_cb_destroy(struct wl_listener *l, void *data)
1445 {
1446    struct wl_resource *surface = (struct wl_resource *)data;
1447
1448    KLDBG("Listener(%p) called: surface: %p is died", l, surface);
1449    e_keyrouter_remove_client_from_list(surface, NULL);
1450
1451    wl_list_remove(&l->link);
1452    E_FREE(l);
1453
1454    krt->grab_surface_list = eina_list_remove(krt->grab_surface_list, surface);
1455    krt->registered_none_key_window_list = eina_list_remove(krt->registered_none_key_window_list, surface);
1456    krt->invisible_set_window_list= eina_list_remove(krt->invisible_set_window_list, surface);
1457    krt->invisible_get_window_list= eina_list_remove(krt->invisible_get_window_list, surface);
1458    if (surface == krt->playback_daemon_surface)
1459      {
1460         krt->playback_daemon_surface = NULL;
1461         KLDBG("playback daemon surface destroyed!");
1462      }
1463 }
1464
1465 #ifdef ENABLE_CYNARA
1466 static void
1467 _e_keyrouter_util_cynara_log(const char *func_name, int err)
1468 {
1469 #define CYNARA_BUFSIZE 128
1470    char buf[CYNARA_BUFSIZE] = "\0";
1471    int ret;
1472
1473    ret = cynara_strerror(err, buf, CYNARA_BUFSIZE);
1474    if (ret != CYNARA_API_SUCCESS)
1475      {
1476         KLWRN("Failed to cynara_strerror: %d (error log about %s: %d)", ret, func_name, err);
1477         return;
1478      }
1479    KLWRN("%s is failed: %s", func_name, buf);
1480 }
1481
1482 static Eina_Bool
1483 _e_keyrouter_util_do_privilege_check(struct wl_client *client, uint32_t mode, uint32_t keycode)
1484 {
1485    int ret, retry_cnt=0, len=0;
1486    char *clientSmack=NULL, *client_session=NULL, uid2[16]={0, };
1487    Eina_Bool res = EINA_FALSE;
1488    Eina_List *l;
1489    struct wl_client *wc_data;
1490    static Eina_Bool retried = EINA_FALSE;
1491    pid_t pid = 0;
1492    uid_t uid = 0;
1493    gid_t gid = 0;
1494
1495    /* Top position grab is always allowed. This mode do not need privilege.*/
1496    if (mode == TIZEN_KEYROUTER_MODE_TOPMOST)
1497      return EINA_TRUE;
1498
1499    if (krt->HardKeys[keycode].no_privcheck == EINA_TRUE)
1500      return EINA_TRUE;
1501
1502    if (!client) return EINA_FALSE;
1503
1504    /* If initialize cynara is failed, allow keygrabs regardless of the previlege permition. */
1505    if (krt->p_cynara == NULL)
1506      {
1507         if (retried == EINA_FALSE)
1508           {
1509              retried = EINA_TRUE;
1510              for(retry_cnt = 0; retry_cnt < 5; retry_cnt++)
1511                {
1512                   KLDBG("Retry cynara initialize: %d", retry_cnt+1);
1513                   ret = cynara_initialize(&krt->p_cynara, NULL);
1514                   if (EINA_UNLIKELY(CYNARA_API_SUCCESS != ret))
1515                     {
1516                       _e_keyrouter_util_cynara_log("cynara_initialize", ret);
1517                        krt->p_cynara = NULL;
1518                     }
1519                   else
1520                     {
1521                        KLDBG("Success cynara initialize to try %d times", retry_cnt+1);
1522                        break;
1523                     }
1524                }
1525           }
1526         return EINA_TRUE;
1527      }
1528
1529    EINA_LIST_FOREACH(krt->grab_client_list, l, wc_data)
1530      {
1531         if (wc_data == client)
1532           {
1533              res = EINA_TRUE;
1534              goto finish;
1535           }
1536      }
1537
1538    wl_client_get_credentials(client, &pid, &uid, &gid);
1539
1540    len = smack_new_label_from_process((int)pid, &clientSmack);
1541    if (len <= 0) goto finish;
1542
1543    snprintf(uid2, 15, "%d", (int)uid);
1544    client_session = cynara_session_from_pid(pid);
1545
1546    ret = cynara_check(krt->p_cynara, clientSmack, client_session, uid2, "http://tizen.org/privilege/keygrab");
1547    if (CYNARA_API_ACCESS_ALLOWED == ret)
1548      {
1549         res = EINA_TRUE;
1550      }
1551    else
1552      {
1553         KLINF("Fail to check cynara,  error : %d (pid : %d)", ret, pid);
1554      }
1555 finish:
1556    if (client_session) E_FREE(client_session);
1557    if (clientSmack) E_FREE(clientSmack);
1558
1559    return res;
1560 }
1561 #endif