656a5be2348f76b561c06b9bf34892d0c99431d7
[platform/upstream/enlightenment.git] / src / bin / e_devicemgr_keyboard_grab.c
1 #include "e_devicemgr_private.h"
2 #include "e_input_intern.h"
3 #include "e_comp_wl_intern.h"
4
5 static void _e_devicemgr_keyboard_grab_client_cb_destroy(struct wl_listener *l, void *data);
6
7 EINTERN Eina_Bool
8 e_devicemgr_keyboard_grab_subtype_is_grabbed(Ecore_Device_Subclass subclas)
9 {
10    if ((subclas == ECORE_DEVICE_SUBCLASS_NONE) &&
11        (e_devicemgr->keyboard_grab.subtype & TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_NONE))
12        return EINA_TRUE;
13
14    if ((subclas == ECORE_DEVICE_SUBCLASS_REMOCON) &&
15        (e_devicemgr->keyboard_grab.subtype & TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_REMOCON))
16        return EINA_TRUE;
17
18    if ((subclas == ECORE_DEVICE_SUBCLASS_VIRTUAL_KEYBOARD) &&
19        (e_devicemgr->keyboard_grab.subtype & TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_VIRTUAL_KEYBOARD))
20        return EINA_TRUE;
21
22    DMWRN("Keyboard Grab. device subclas(%d) is not grabbed", subclas);
23    return EINA_FALSE;
24 }
25
26 static void
27 _e_devicemgr_keyboard_grab_key_handler(uint32_t keycode, uint32_t state,
28                                            uint32_t timestamp, void *dev)
29 {
30     DMINF("key(%d-%s) event is delivered to grab ec(%p)",
31           keycode, state ? "Pressed" : "Released", e_devicemgr->keyboard_grab.ec);
32
33    if (!e_comp_wl_key_send(e_devicemgr->keyboard_grab.ec, keycode, state ? EINA_TRUE : EINA_FALSE,
34                       dev, timestamp))
35       {
36          DMERR("Could not send keyboard_grab_key to client(%p)", e_devicemgr->keyboard_grab.client);
37       }
38
39    e_input_flush_keyboard_share_events();
40 }
41
42 static Eina_Bool
43 _e_devicemgr_keyboard_grab_client_remove(struct wl_client *client)
44 {
45    struct wl_listener *destroy_listener = NULL;
46
47    if (client != e_devicemgr->keyboard_grab.client)
48      return EINA_FALSE;
49
50    e_devicemgr->keyboard_grab.subtype = 0x0;
51    e_devicemgr->keyboard_grab.client = NULL;
52    e_devicemgr->keyboard_grab.ec = NULL;
53
54    if (!e_input_keyboard_grab_key_handler_set(NULL))
55      {
56         DMERR("ERROR! Could not set keyboard_grab key handler to NULL!");
57         return EINA_FALSE;
58      }
59
60    destroy_listener = wl_client_get_destroy_listener(client, _e_devicemgr_keyboard_grab_client_cb_destroy);
61    if (destroy_listener)
62      {
63         wl_list_remove(&destroy_listener->link);
64         E_FREE(destroy_listener);
65      }
66
67    return EINA_TRUE;
68 }
69
70 static void
71 _e_devicemgr_keyboard_grab_client_cb_destroy(struct wl_listener *l, void *data)
72 {
73    struct wl_client *client = (struct wl_client *)data;
74
75    if (!e_devicemgr->keyboard_grab.client) return;
76
77    wl_list_remove(&l->link);
78    E_FREE(l);
79
80    _e_devicemgr_keyboard_grab_client_remove(client);
81 }
82
83 static Eina_Bool
84 _e_devicemgr_keyboard_grab_client_add(struct wl_client *client, E_Client *ec, uint32_t subclas)
85 {
86    struct wl_listener *destroy_listener = NULL;
87
88    e_devicemgr->keyboard_grab.subtype |= subclas;
89
90    if (e_devicemgr->keyboard_grab.client == client)
91      return EINA_TRUE;
92
93    e_devicemgr->keyboard_grab.client = client;
94    e_devicemgr->keyboard_grab.ec = ec;
95
96    if (!e_input_keyboard_grab_key_handler_set(_e_devicemgr_keyboard_grab_key_handler))
97      {
98         DMERR("ERROR! Could not set keyboard_grab key handler to func!");
99         return EINA_FALSE;
100      }
101
102    destroy_listener = E_NEW(struct wl_listener, 1);
103    EINA_SAFETY_ON_NULL_GOTO(destroy_listener, failed);
104    destroy_listener->notify = _e_devicemgr_keyboard_grab_client_cb_destroy;
105    wl_client_add_destroy_listener(client, destroy_listener);
106
107    return EINA_TRUE;
108
109 failed:
110    e_devicemgr->keyboard_grab.client = NULL;
111    e_devicemgr->keyboard_grab.ec = NULL;
112    e_devicemgr->keyboard_grab.subtype = 0x0;
113
114    return EINA_FALSE;
115 }
116
117 int
118 e_devicemgr_keyboard_grab(struct wl_client *client, struct wl_resource *surface, uint32_t subclas)
119 {
120    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
121    E_Client *ec;
122    uint32_t all_subclass = TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_NONE |
123                         TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_REMOCON |
124                         TIZEN_INPUT_DEVICE_MANAGER_SUBCLAS_VIRTUAL_KEYBOARD;
125
126    if ((e_devicemgr->keyboard_grab.client) && (e_devicemgr->keyboard_grab.client != client))
127      {
128         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED;
129      }
130    if (!(subclas & all_subclass))
131      {
132         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_CLASS;
133      }
134
135    ec = e_client_from_surface_resource(surface);
136    if (!ec)
137      return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_SURFACE;
138
139    if (!_e_devicemgr_keyboard_grab_client_add(client, ec, subclas))
140      ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED;
141
142    return ret;
143 }
144
145 int
146 e_devicemgr_keyboard_ungrab(struct wl_client *client, struct wl_resource *surface)
147 {
148    int ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
149    E_Client *ec;
150
151    if ((e_devicemgr->keyboard_grab.client) && (e_devicemgr->keyboard_grab.client != client))
152     {
153        return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED;
154     }
155
156    ec = e_client_from_surface_resource(surface);
157    if (!ec)
158      return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_SURFACE;
159
160    if (!_e_devicemgr_keyboard_grab_client_remove(client))
161      ret = TIZEN_INPUT_DEVICE_MANAGER_ERROR_NOT_ALLOWED;
162
163    return ret;
164 }