disable a keyrotuer module
[platform/core/uifw/e-mod-tizen-keyrouter.git] / src / e_mod_keyrouter_events.c
1 #ifndef _F_ENABLE_KEYROUTER_CORE
2
3 #define E_COMP_WL
4 #include "e_mod_main_wl.h"
5
6 static Eina_Bool _e_keyrouter_send_key_events(int type, Ecore_Event_Key *ev);
7 static Eina_Bool _e_keyrouter_send_key_events_press(int type, Ecore_Event_Key *ev);
8 static Eina_Bool _e_keyrouter_send_key_events_release(int type, Ecore_Event_Key *ev);
9 static Eina_Bool _e_keyrouter_send_key_event(int type, struct wl_resource *surface, struct wl_client *wc, Ecore_Event_Key *ev, Eina_Bool focused, unsigned int mode);
10
11 static Eina_Bool _e_keyrouter_send_key_events_focus(int type, struct wl_resource *surface, Ecore_Event_Key *ev, struct wl_resource **delivered_surface);
12 static void _e_keyrouter_event_generate_key(Ecore_Event_Key *ev, int type, struct wl_client *send_surface);
13
14 static Eina_Bool _e_keyrouter_is_key_grabbed(int key);
15 static Eina_Bool _e_keyrouter_check_top_visible_window(E_Client *ec_focus, int arr_idx);
16
17 static Eina_Bool
18 _e_keyrouter_is_key_grabbed(int key)
19 {
20    if (!krt->HardKeys[key].keycode)
21      {
22         return EINA_FALSE;
23      }
24    if (krt->HardKeys[key].excl_ptr ||
25         krt->HardKeys[key].or_excl_ptr ||
26         krt->HardKeys[key].top_ptr ||
27         krt->HardKeys[key].shared_ptr)
28      {
29         return EINA_TRUE;
30      }
31
32    return EINA_FALSE;
33 }
34
35 static void
36 _e_keyrouter_event_key_free(void *data EINA_UNUSED, void *ev)
37 {
38    Ecore_Event_Key *e = ev;
39
40    eina_stringshare_del(e->keyname);
41    eina_stringshare_del(e->key);
42    eina_stringshare_del(e->string);
43    eina_stringshare_del(e->compose);
44
45    if (e->dev) ecore_device_unref(e->dev);
46
47    E_FREE(e);
48 }
49
50 static void
51 _e_keyrouter_event_generate_key(Ecore_Event_Key *ev, int type, struct wl_client *send_surface)
52 {
53    Ecore_Event_Key *ev_cpy = NULL;
54
55    ev_cpy = E_NEW(Ecore_Event_Key, 1);
56    EINA_SAFETY_ON_NULL_RETURN(ev_cpy);
57
58    KLDBG("Generate new key event! send to wl_surface: %p (pid: %d)", send_surface, e_keyrouter_util_get_pid(send_surface, NULL));
59
60    ev_cpy->keyname = (char *)eina_stringshare_add(ev->keyname);
61    ev_cpy->key = (char *)eina_stringshare_add(ev->key);
62    ev_cpy->string = (char *)eina_stringshare_add(ev->string);
63    ev_cpy->compose = (char *)eina_stringshare_add(ev->compose);
64
65    ev_cpy->window = ev->window;
66    ev_cpy->root_window = ev->root_window;
67    ev_cpy->event_window = ev->event_window;
68
69    ev_cpy->timestamp = (int)(ecore_time_get()*1000);
70    ev_cpy->modifiers = ev->modifiers;
71
72    ev_cpy->same_screen = ev->same_screen;
73    ev_cpy->keycode = ev->keycode;
74
75    ev_cpy->data = send_surface;
76    ev_cpy->dev = ecore_device_ref(ev->dev);
77
78    if (ECORE_EVENT_KEY_DOWN == type)
79      ecore_event_add(ECORE_EVENT_KEY_DOWN, ev_cpy, _e_keyrouter_event_key_free, NULL);
80    else
81      ecore_event_add(ECORE_EVENT_KEY_UP, ev_cpy, _e_keyrouter_event_key_free, NULL);
82 }
83
84 /* Function for checking the existing grab for a key and sending key event(s) */
85 Eina_Bool
86 e_keyrouter_process_key_event(void *event, int type)
87 {
88    Eina_Bool res = EINA_TRUE;
89    Ecore_Event_Key *ev = event;
90    struct wl_client *wc;
91
92    if (!ev) goto finish;
93
94    KLDBG("[%s] keyname: %s, key: %s, keycode: %d", (type == ECORE_EVENT_KEY_DOWN) ? "KEY_PRESS" : "KEY_RELEASE", ev->keyname, ev->key, ev->keycode);
95
96    if (ev->data)
97      {
98         KLDBG("data is exist send to compositor: %p", ev->data);
99         goto finish;
100      }
101
102    if (ev->modifiers != 0)
103      {
104         KLDBG("Modifier key delivered to Focus window : Key %s(%d)", ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keycode);
105         goto finish;
106      }
107
108    if (krt->playback_daemon_surface)
109      {
110        wc = wl_resource_get_client(krt->playback_daemon_surface);
111        if (wc)
112          {
113             _e_keyrouter_event_generate_key(ev, type, wc);
114             KLDBG("Sent key to playback-daemon");
115          }
116      }
117
118    if (krt->max_tizen_hwkeys < ev->keycode)
119      {
120         KLWRN("The key(%d) is too larger to process keyrouting: Invalid keycode", ev->keycode);
121         goto finish;
122      }
123
124    if (!krt->HardKeys[ev->keycode].keycode) goto finish;
125
126    if (!e_keyrouter_intercept_hook_call(E_KEYROUTER_INTERCEPT_HOOK_BEFORE_KEYROUTING, type, ev))
127      {
128         goto finish;
129      }
130
131    if ((ECORE_EVENT_KEY_UP == type) && (!krt->HardKeys[ev->keycode].press_ptr))
132      {
133         KLDBG("The release key(%d) isn't a processed by keyrouter!", ev->keycode);
134         res = EINA_FALSE;
135         goto finish;
136      }
137
138    //KLDBG("The key(%d) is going to be sent to the proper wl client(s) !", ev->keycode);
139    KLDBG("[%s] keyname: %s, key: %s, keycode: %d", (type == ECORE_EVENT_KEY_DOWN) ? "KEY_PRESS" : "KEY_RELEASE", ev->keyname, ev->key, ev->keycode);
140    if (_e_keyrouter_send_key_events(type, ev))
141      res = EINA_FALSE;
142
143 finish:
144    return res;
145 }
146
147 /* Function for sending key events to wl_client(s) */
148 static Eina_Bool
149 _e_keyrouter_send_key_events(int type, Ecore_Event_Key *ev)
150 {
151    Eina_Bool res;
152    if (ECORE_EVENT_KEY_DOWN == type)
153      {
154         res = _e_keyrouter_send_key_events_press(type, ev);
155      }
156   else
157      {
158         res = _e_keyrouter_send_key_events_release(type, ev);
159      }
160   return res;
161 }
162
163 static Eina_Bool
164 _e_keyrouter_send_key_events_release(int type, Ecore_Event_Key *ev)
165 {
166    int pid = 0;
167    char *pname = NULL, *cmd = NULL;
168    E_Keyrouter_Key_List_NodePtr key_node_data;
169    Eina_Bool res = EINA_TRUE, ret = EINA_TRUE;
170
171    /* Deliver release  clean up pressed key list */
172    EINA_LIST_FREE(krt->HardKeys[ev->keycode].press_ptr, key_node_data)
173      {
174         if (key_node_data->status == E_KRT_CSTAT_ALIVE)
175           {
176              res = _e_keyrouter_send_key_event(type, key_node_data->surface, key_node_data->wc, ev,
177                                                key_node_data->focused, TIZEN_KEYROUTER_MODE_PRESSED);
178
179              pid = e_keyrouter_util_get_pid(key_node_data->wc, key_node_data->surface);
180              cmd = e_keyrouter_util_cmd_get_from_pid(pid);
181              pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
182              KLINF("Release Pair : %s(%s:%d)(Focus: %d)(Status: %d) => wl_surface (%p) wl_client (%p) (pid: %d) (pname: %s)",
183                       ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode, key_node_data->focused,
184                       key_node_data->status, key_node_data->surface, key_node_data->wc, pid, pname ?: "Unknown");
185              if(pname) E_FREE(pname);
186              if(cmd) E_FREE(cmd);
187           }
188         else
189           {
190              if (key_node_data->focused == EINA_TRUE)
191                {
192                   res = EINA_FALSE;
193                   if (key_node_data->status == E_KRT_CSTAT_DEAD)
194                     {
195                        ev->data = key_node_data->wc;
196                     }
197                   else
198                     {
199                        ev->data = (void *)0x1;
200                     }
201                }
202              KLINF("Release Pair : %s(%s:%d)(Focus: %d)(Status: %d) => wl_surface (%p) wl_client (%p) process is ungrabbed / dead",
203                       ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode, key_node_data->focused,
204                       key_node_data->status, key_node_data->surface, key_node_data->wc);
205           }
206
207         E_FREE(key_node_data);
208         if (res == EINA_FALSE) ret = EINA_FALSE;
209      }
210    krt->HardKeys[ev->keycode].press_ptr = NULL;
211
212    return ret;
213 }
214
215 static Eina_Bool
216 _e_keyrouter_send_key_events_press(int type, Ecore_Event_Key *ev)
217 {
218    unsigned int keycode = ev->keycode;
219    struct wl_resource *surface_focus = NULL;
220    E_Client *ec_focus = NULL;
221    struct wl_resource *delivered_surface = NULL;
222    Eina_Bool res = EINA_TRUE;
223    int pid = 0;
224    char *pname = NULL, *cmd = NULL;
225
226    E_Keyrouter_Key_List_NodePtr key_node_data;
227    Eina_List *l = NULL;
228
229    ec_focus = e_client_focused_get();
230    surface_focus = e_keyrouter_util_get_surface_from_eclient(ec_focus);
231
232    if (krt->isPictureOffEnabled == 1)
233      {
234        EINA_LIST_FOREACH(krt->HardKeys[keycode].pic_off_ptr, l, key_node_data)
235           {
236             if (key_node_data)
237                 {
238                  res = _e_keyrouter_send_key_event(type, key_node_data->surface, key_node_data->wc, ev, key_node_data->focused, TIZEN_KEYROUTER_MODE_SHARED);
239
240                  pid = e_keyrouter_util_get_pid(key_node_data->wc, key_node_data->surface);
241                  cmd = e_keyrouter_util_cmd_get_from_pid(pid);
242                  pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
243                  KLINF("PICTURE OFF : %s(%d) => wl_surface (%p) wl_client (%p) (pid: %d) (pname: %s)",
244                        ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keycode, key_node_data->surface, key_node_data->wc, pid, pname ?: "Unknown");
245                  if(pname) E_FREE(pname);
246                  if(cmd) E_FREE(cmd);
247                 }
248           }
249        return res;
250      }
251    if (!_e_keyrouter_is_key_grabbed(ev->keycode))
252      {
253        res = _e_keyrouter_send_key_events_focus(type, surface_focus, ev, &delivered_surface);
254        if (delivered_surface)
255          {
256             res = e_keyrouter_add_surface_destroy_listener(delivered_surface);
257             if (res != TIZEN_KEYROUTER_ERROR_NONE)
258               {
259                  KLWRN("Failed to add surface to destroy listener (res: %d)", res);
260               }
261          }
262        return res;
263      }
264
265    EINA_LIST_FOREACH(krt->HardKeys[keycode].excl_ptr, l, key_node_data)
266      {
267         if (key_node_data)
268           {
269              res = _e_keyrouter_send_key_event(type, key_node_data->surface, key_node_data->wc, ev,
270                                                key_node_data->focused, TIZEN_KEYROUTER_MODE_EXCLUSIVE);
271
272              pid = e_keyrouter_util_get_pid(key_node_data->wc, key_node_data->surface);
273              cmd = e_keyrouter_util_cmd_get_from_pid(pid);
274              pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
275              KLINF("EXCLUSIVE : %s(%s:%d) => wl_surface (%p) wl_client (%p) (pid: %d) (pname: %s)",
276                       ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode,
277                       key_node_data->surface, key_node_data->wc, pid, pname ?: "Unknown");
278              if(pname) E_FREE(pname);
279              if(cmd) E_FREE(cmd);
280              return res;
281           }
282      }
283
284    EINA_LIST_FOREACH(krt->HardKeys[keycode].or_excl_ptr, l, key_node_data)
285      {
286         if (key_node_data)
287           {
288              res = _e_keyrouter_send_key_event(type, key_node_data->surface, key_node_data->wc, ev,
289                                                key_node_data->focused, TIZEN_KEYROUTER_MODE_OVERRIDABLE_EXCLUSIVE);
290
291              pid = e_keyrouter_util_get_pid(key_node_data->wc, key_node_data->surface);
292              cmd = e_keyrouter_util_cmd_get_from_pid(pid);
293              pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
294              KLINF("OVERRIDABLE_EXCLUSIVE : %s(%s:%d) => wl_surface (%p) wl_client (%p) (pid: %d) (pname: %s)",
295                      ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode,
296                      key_node_data->surface, key_node_data->wc, pid, pname ?: "Unknown");
297              if(pname) E_FREE(pname);
298              if(cmd) E_FREE(cmd);
299
300              return res;
301           }
302      }
303
304    // Top position grab must need a focus surface.
305    if (surface_focus)
306      {
307         EINA_LIST_FOREACH(krt->HardKeys[keycode].top_ptr, l, key_node_data)
308           {
309              if (key_node_data)
310                {
311                   if ((EINA_FALSE == krt->isWindowStackChanged) && (surface_focus == key_node_data->surface))
312                     {
313                        pid = e_keyrouter_util_get_pid(key_node_data->wc, key_node_data->surface);
314                        cmd = e_keyrouter_util_cmd_get_from_pid(pid);
315                        pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
316
317                        res = _e_keyrouter_send_key_event(type, key_node_data->surface, NULL, ev, key_node_data->focused,
318                                                          TIZEN_KEYROUTER_MODE_TOPMOST);
319                        KLINF("TOPMOST (TOP_POSITION) : %s (%s:%d) => wl_surface (%p) (pid: %d) (pname: %s)",
320                                 ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode,
321                                 key_node_data->surface, pid, pname ?: "Unknown");
322
323                        if(pname) E_FREE(pname);
324                        if(cmd) E_FREE(cmd);
325                        return res;
326                     }
327                   krt->isWindowStackChanged = EINA_FALSE;
328
329                   if (_e_keyrouter_check_top_visible_window(ec_focus, keycode))
330                     {
331                        E_Keyrouter_Key_List_NodePtr top_key_node_data = eina_list_data_get(krt->HardKeys[keycode].top_ptr);
332                        pid = e_keyrouter_util_get_pid(top_key_node_data->wc, top_key_node_data->surface);
333                        cmd = e_keyrouter_util_cmd_get_from_pid(pid);
334                        pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
335
336                        res = _e_keyrouter_send_key_event(type, top_key_node_data->surface, NULL, ev, top_key_node_data->focused,
337                                                          TIZEN_KEYROUTER_MODE_TOPMOST);
338                        KLINF("TOPMOST (TOP_POSITION) : %s (%s:%d) => wl_surface (%p) (pid: %d) (pname: %s)",
339                              ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode,
340                              top_key_node_data->surface, pid, pname ?: "Unknown");
341
342                        if(pname) E_FREE(pname);
343                        if(cmd) E_FREE(cmd);
344                        return res;
345                     }
346                   break;
347                }
348           }
349        goto need_shared;
350      }
351
352    if (krt->HardKeys[keycode].shared_ptr)
353      {
354 need_shared:
355         res = _e_keyrouter_send_key_events_focus(type, surface_focus, ev, &delivered_surface);
356         if (delivered_surface)
357           {
358              res = e_keyrouter_add_surface_destroy_listener(delivered_surface);
359              if (res != TIZEN_KEYROUTER_ERROR_NONE)
360                {
361                   KLWRN("Failed to add wl_surface to destroy listener (res: %d)", res);
362                }
363           }
364         EINA_LIST_FOREACH(krt->HardKeys[keycode].shared_ptr, l, key_node_data)
365           {
366              if (key_node_data)
367                {
368                   if (delivered_surface && key_node_data->surface == delivered_surface)
369                     {
370                        // Check for already delivered surface
371                        // do not deliver double events in this case.
372                        continue;
373                     }
374                   else
375                     {
376                        _e_keyrouter_send_key_event(type, key_node_data->surface, key_node_data->wc, ev, key_node_data->focused, TIZEN_KEYROUTER_MODE_SHARED);
377                        pid = e_keyrouter_util_get_pid(key_node_data->wc, key_node_data->surface);
378                        cmd = e_keyrouter_util_cmd_get_from_pid(pid);
379                        pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
380                        KLINF("SHARED : %s(%s:%d) => wl_surface (%p) wl_client (%p) (pid: %d) (pname: %s)",
381                              ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode, key_node_data->surface, key_node_data->wc, pid, pname ?: "Unknown");
382                        if(pname) E_FREE(pname);
383                        if(cmd) E_FREE(cmd);
384                     }
385                }
386           }
387         return res;
388      }
389
390    return EINA_FALSE;
391 }
392
393 static Eina_Bool
394 _e_keyrouter_send_key_events_focus(int type, struct wl_resource *surface_focus,  Ecore_Event_Key *ev, struct wl_resource **delivered_surface)
395 {
396    E_Client *ec_top = NULL, *ec_focus = NULL;
397    Eina_Bool below_focus = EINA_FALSE;
398    struct wl_resource *surface = NULL;
399    Eina_List* key_list = NULL;
400    int *key_data = NULL;
401    Eina_List *ll = NULL;
402    int deliver_invisible = 0;
403    Eina_Bool res = EINA_TRUE;
404    int pid = 0;
405    char *pname = NULL, *cmd = NULL;
406
407    if (!e_keyrouter_intercept_hook_call(E_KEYROUTER_INTERCEPT_HOOK_DELIVER_FOCUS, type, ev))
408      {
409         if (ev->data && ev->data != (void *)0x1)
410           {
411              *delivered_surface = ev->data;
412              ev->data = wl_resource_get_client(ev->data);
413           }
414         return res;
415      }
416
417    ec_top = e_client_top_get();
418    ec_focus = e_client_focused_get();
419
420    if (!krt->HardKeys[ev->keycode].registered_ptr && !e_keyrouter_is_registered_window(surface_focus) &&
421          !IsNoneKeyRegisterWindow(surface_focus) && !krt->invisible_set_window_list)
422      {
423         pid = e_keyrouter_util_get_pid(NULL, surface_focus);
424         cmd = e_keyrouter_util_cmd_get_from_pid(pid);
425         pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
426
427         res = _e_keyrouter_send_key_event(type, surface_focus, NULL,ev, EINA_TRUE, TIZEN_KEYROUTER_MODE_SHARED);
428         KLINF("FOCUS DIRECT : %s(%s:%d) => wl_surface (%p) (pid: %d) (pname: %s)",
429                ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode, surface_focus, pid, pname ?: "Unknown");
430         *delivered_surface = surface_focus;
431         if(pname) E_FREE(pname);
432         if(cmd) E_FREE(cmd);
433         return res;
434      }
435
436    // loop over to next window from top of window stack
437    for (; ec_top != NULL; ec_top = e_client_below_get(ec_top))
438      {
439         surface = e_keyrouter_util_get_surface_from_eclient(ec_top);
440
441         if(surface == NULL)
442           {
443              // Not a valid surface.
444              continue;
445           }
446         if (ec_top->is_cursor) continue;
447
448         // Check if window stack reaches to focus window
449         if (ec_top == ec_focus)
450           {
451              KLINF("%p is focus e_client & wl_surface: focus %p ==> %p", ec_top, surface_focus, surface);
452              below_focus = EINA_TRUE;
453           }
454
455         // Check for FORCE DELIVER to INVISIBLE WINDOW
456         if (deliver_invisible && IsInvisibleGetWindow(surface))
457           {
458              pid = e_keyrouter_util_get_pid(NULL, surface);
459              cmd = e_keyrouter_util_cmd_get_from_pid(pid);
460              pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
461
462              res = _e_keyrouter_send_key_event(type, surface, NULL, ev, EINA_TRUE, TIZEN_KEYROUTER_MODE_REGISTERED);
463              KLINF("FORCE DELIVER : %s(%s:%d) => wl_surface (%p) (pid: %d) (pname: %s)",
464                    ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode,
465                    surface, pid, pname ?: "Unknown");
466              *delivered_surface = surface;
467              if(pname) E_FREE(pname);
468              if(cmd) E_FREE(cmd);
469              return res;
470           }
471
472         // Check for visible window first <Consider VISIBILITY>
473         // return if not visible
474         if (ec_top->visibility.obscured == E_VISIBILITY_FULLY_OBSCURED || ec_top->visibility.obscured == E_VISIBILITY_UNKNOWN)
475           {
476              continue;
477           }
478
479         // Set key Event Delivery for INVISIBLE WINDOW
480         if (IsInvisibleSetWindow(surface))
481           {
482              deliver_invisible = 1;
483           }
484
485         if (IsNoneKeyRegisterWindow(surface))
486           {
487              // Registered None property is set for this surface
488              // No event will be delivered to this surface.
489              KLINF("wl_surface (%p) is a none register window.", surface);
490              continue;
491           }
492
493         if (e_keyrouter_is_registered_window(surface))
494           {
495              // get the key list and deliver events if it has registered for that key
496              // Write a function to get the key list for register window.
497              key_list = _e_keyrouter_registered_window_key_list(surface);
498              if (key_list)
499                {
500                   EINA_LIST_FOREACH(key_list, ll, key_data)
501                     {
502                        if(!key_data) continue;
503
504                        if(*key_data == ev->keycode)
505                          {
506                             pid = e_keyrouter_util_get_pid(NULL, surface);
507                             cmd = e_keyrouter_util_cmd_get_from_pid(pid);
508                             pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
509
510                             res = _e_keyrouter_send_key_event(type, surface, NULL, ev, EINA_TRUE, TIZEN_KEYROUTER_MODE_REGISTERED);
511                             KLINF("REGISTER : %s(%s:%d) => wl_surface (%p) (pid: %d) (pname: %s)",
512                                   ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode, surface, pid, pname ?: "Unknown");
513                             *delivered_surface = surface;
514                             if(pname) E_FREE(pname);
515                             if(cmd) E_FREE(cmd);
516                             return res;
517                          }
518                     }
519                }
520              else
521                {
522                   KLDBG("Key_list is Null for registered wl_surface %p", surface);
523                }
524           }
525
526         if (surface != surface_focus)
527           {
528              if (below_focus == EINA_FALSE) continue;
529
530              // Deliver to below Non Registered window
531              else if (!e_keyrouter_is_registered_window(surface))
532                {
533                   pid = e_keyrouter_util_get_pid(NULL, surface);
534                   cmd = e_keyrouter_util_cmd_get_from_pid(pid);
535                   pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
536
537                   res = _e_keyrouter_send_key_event(type, surface, NULL, ev, EINA_TRUE, TIZEN_KEYROUTER_MODE_SHARED);
538                   KLINF("NOT REGISTER : %s(%s:%d) => wl_surface (%p) (pid: %d) (pname: %s)",
539                         ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode, surface, pid, pname ?: "Unknown");
540                   *delivered_surface = surface;
541                   if(pname) E_FREE(pname);
542                   if(cmd) E_FREE(cmd);
543                   return res;
544                }
545              else continue;
546           }
547         else
548           {
549              // Deliver to Focus if Non Registered window
550              if (!e_keyrouter_is_registered_window(surface))
551                {
552                   pid = e_keyrouter_util_get_pid(NULL, surface);
553                   cmd = e_keyrouter_util_cmd_get_from_pid(pid);
554                   pname = e_keyrouter_util_process_name_get_from_cmd(cmd);
555
556                   res = _e_keyrouter_send_key_event(type, surface, NULL,ev, EINA_TRUE, TIZEN_KEYROUTER_MODE_SHARED);
557                   KLINF("FOCUS : %s(%s:%d) => wl_surface (%p) (pid: %d) (pname: %s)",
558                         ((ECORE_EVENT_KEY_DOWN == type) ? "Down" : "Up"), ev->keyname, ev->keycode, surface, pid, pname ?: "Unknown");
559                   *delivered_surface = surface;
560                   if(pname) E_FREE(pname);
561                   if(cmd) E_FREE(cmd);
562                   return res;
563                }
564              else continue;
565           }
566     }
567
568     KLINF("Couldnt Deliver key:(%s:%d) to any window. Focused wl_surface: %p", ev->keyname, ev->keycode, surface_focus);
569     return res;
570 }
571
572 static Eina_Bool
573 _e_keyrouter_check_top_visible_window(E_Client *ec_focus, int arr_idx)
574 {
575    E_Client *ec_top = NULL;
576    Eina_List *l = NULL, *l_next = NULL;
577    E_Keyrouter_Key_List_NodePtr key_node_data = NULL;
578
579    ec_top = e_client_top_get();
580
581    while (ec_top)
582      {
583         if (!ec_top->visible && ec_top == ec_focus)
584           {
585              KLDBG("Top e_client (%p) is invisible(%d) but focus client", ec_top, ec_top->visible);
586              return EINA_FALSE;
587           }
588         if (!ec_top->visible)
589           {
590              ec_top = e_client_below_get(ec_top);
591              continue;
592           }
593
594         /* TODO: Check this client is located inside a display boundary */
595
596         EINA_LIST_FOREACH_SAFE(krt->HardKeys[arr_idx].top_ptr, l, l_next, key_node_data)
597           {
598              if (key_node_data)
599                {
600                   if (ec_top == wl_resource_get_user_data(key_node_data->surface))
601                     {
602                        krt->HardKeys[arr_idx].top_ptr = eina_list_promote_list(krt->HardKeys[arr_idx].top_ptr, l);
603                        KLDBG("Move a client(e_client: %p, wl_surface: %p) to first index of list(key: %d)",
604                                 ec_top, key_node_data->surface, arr_idx);
605                        return EINA_TRUE;
606                     }
607                }
608           }
609
610         if (ec_top == ec_focus)
611           {
612              KLDBG("The e_client(%p) is a focus client", ec_top);
613              return EINA_FALSE;
614           }
615
616         ec_top = e_client_below_get(ec_top);
617      }
618    return EINA_FALSE;
619 }
620
621 /* Function for sending key event to wl_client(s) */
622 static Eina_Bool
623 _e_keyrouter_send_key_event(int type, struct wl_resource *surface, struct wl_client *wc, Ecore_Event_Key *ev, Eina_Bool focused, unsigned int mode)
624 {
625    struct wl_client *wc_send;
626
627    if (surface == NULL)
628      {
629         wc_send = wc;
630      }
631    else
632      {
633         wc_send = wl_resource_get_client(surface);
634      }
635
636    if (!wc_send)
637      {
638         KLWRN("wl_surface: %p or wl_client: %p returns null wayland client", surface, wc);
639         return EINA_FALSE;
640      }
641
642    if (ECORE_EVENT_KEY_DOWN == type)
643      {
644         if (mode == TIZEN_KEYROUTER_MODE_EXCLUSIVE ||
645             mode == TIZEN_KEYROUTER_MODE_OVERRIDABLE_EXCLUSIVE ||
646             mode == TIZEN_KEYROUTER_MODE_TOPMOST ||
647             mode == TIZEN_KEYROUTER_MODE_REGISTERED)
648           {
649              focused = EINA_TRUE;
650              ev->data = wc_send;
651              KLDBG("Send only one key! wl_client: %p(%d)", wc_send, e_keyrouter_util_get_pid(wc_send, NULL));
652           }
653         else if (focused == EINA_TRUE)
654           {
655              ev->data = wc_send;
656           }
657         e_keyrouter_prepend_to_keylist(surface, wc, ev->keycode, TIZEN_KEYROUTER_MODE_PRESSED, focused);
658      }
659    else
660      {
661         if (focused == EINA_TRUE) ev->data = wc_send;
662      }
663
664    if (focused == EINA_TRUE) return EINA_FALSE;
665
666    _e_keyrouter_event_generate_key(ev, type, wc_send);
667
668    return EINA_TRUE;
669 }
670
671 struct wl_resource *
672 e_keyrouter_util_get_surface_from_eclient(E_Client *client)
673 {
674    EINA_SAFETY_ON_NULL_RETURN_VAL
675      (client, NULL);
676    EINA_SAFETY_ON_NULL_RETURN_VAL
677      (client->comp_data, NULL);
678
679    return client->comp_data->wl_surface;
680 }
681
682 int
683 e_keyrouter_util_get_pid(struct wl_client *client, struct wl_resource *surface)
684 {
685    pid_t pid = 0;
686    uid_t uid = 0;
687    gid_t gid = 0;
688    struct wl_client *cur_client = NULL;
689
690    if (client) cur_client = client;
691    else if (surface) cur_client = wl_resource_get_client(surface);
692    EINA_SAFETY_ON_NULL_RETURN_VAL(cur_client, 0);
693
694    wl_client_get_credentials(cur_client, &pid, &uid, &gid);
695
696    return pid;
697 }
698
699 char *
700 e_keyrouter_util_cmd_get_from_pid(int pid)
701 {
702    Eina_List *l;
703    E_Comp_Connected_Client_Info *cdata;
704
705    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp, NULL);
706
707    EINA_LIST_FOREACH(e_comp->connected_clients, l, cdata)
708      {
709         if (cdata->pid == pid) return strdup(cdata->name);
710      }
711
712    return NULL;
713 }
714
715 typedef struct _keycode_map{
716     xkb_keysym_t keysym;
717     xkb_keycode_t keycode;
718 }keycode_map;
719
720 static void
721 find_keycode(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
722 {
723    keycode_map *found_keycodes = (keycode_map *)data;
724    xkb_keysym_t keysym = found_keycodes->keysym;
725    int nsyms = 0;
726    const xkb_keysym_t *syms_out = NULL;
727
728    nsyms = xkb_keymap_key_get_syms_by_level(keymap, key, 0, 0, &syms_out);
729    if (nsyms && syms_out)
730      {
731         if (*syms_out == keysym)
732           {
733              found_keycodes->keycode = key;
734           }
735      }
736 }
737
738 int
739 _e_keyrouter_keycode_get_from_keysym(struct xkb_keymap *keymap, xkb_keysym_t keysym)
740 {
741    keycode_map found_keycodes = {0,};
742    found_keycodes.keysym = keysym;
743    xkb_keymap_key_for_each(keymap, find_keycode, &found_keycodes);
744
745    return found_keycodes.keycode;
746 }
747
748 int
749 e_keyrouter_util_keycode_get_from_string(char * name)
750 {
751    struct xkb_keymap *keymap = NULL;
752    xkb_keysym_t keysym = 0x0;
753    int keycode = 0;
754
755    keymap = e_comp_wl->xkb.keymap;
756    EINA_SAFETY_ON_NULL_GOTO(keymap, finish);
757
758    keysym = xkb_keysym_from_name(name, XKB_KEYSYM_NO_FLAGS);
759    EINA_SAFETY_ON_FALSE_GOTO(keysym != XKB_KEY_NoSymbol, finish);
760
761    keycode = _e_keyrouter_keycode_get_from_keysym(keymap, keysym);
762
763    KLDBG("request name: %s, return value: %d", name, keycode);
764
765    return keycode;
766
767 finish:
768    return 0;
769 }
770
771 char *
772 e_keyrouter_util_keyname_get_from_keycode(int keycode)
773 {
774    struct xkb_state *state;
775    xkb_keysym_t sym = XKB_KEY_NoSymbol;
776    char name[256] = {0, };
777
778    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, NULL);
779    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl->xkb.state, NULL);
780
781    state = e_comp_wl->xkb.state;
782    sym = xkb_state_key_get_one_sym(state, keycode);
783    xkb_keysym_get_name(sym, name, sizeof(name));
784
785    return strdup(name);
786 }
787
788 char *
789 e_keyrouter_util_process_name_get_from_cmd(char *cmd)
790 {
791    int len, i;
792    char pbuf = '\0';
793    char *pname = NULL;
794    if (cmd)
795      {
796         len = strlen(cmd);
797         for (i = 0; i < len; i++)
798           {
799              pbuf = cmd[len - i - 1];
800              if (pbuf == '/')
801                {
802                   pname = &cmd[len - i];
803                   return strdup(pname);
804                }
805           }
806      }
807    return NULL;
808 }
809 #endif