c90d1a30db51efda1e6a3d194e310ccbf0b1ce8c
[platform/upstream/enlightenment.git] / src / bin / e_devicemgr_block.c
1 #include "e_devicemgr_private.h"
2 #include "e_input_intern.h"
3
4 static void _e_devicemgr_block_client_cb_destroy(struct wl_listener *l, void *data);
5
6 static void
7 _e_devicemgr_block_client_remove(struct wl_client *client)
8 {
9    struct wl_listener *destroy_listener = NULL;
10    Eina_List *dev_list, *l;
11    E_Input_Device *device_data;
12    Eina_Bool res = EINA_TRUE;
13
14    if (client != e_devicemgr->block.client) return;
15
16    e_devicemgr->block.devtype = 0x0;
17    if (e_devicemgr->block.duration_timer)
18      {
19         ecore_timer_del(e_devicemgr->block.duration_timer);
20         e_devicemgr->block.duration_timer = NULL;
21      }
22    e_devicemgr->block.client = NULL;
23    destroy_listener = wl_client_get_destroy_listener(client, _e_devicemgr_block_client_cb_destroy);
24    if (destroy_listener)
25      {
26         wl_list_remove(&destroy_listener->link);
27         E_FREE(destroy_listener);
28      }
29
30    dev_list = (Eina_List *)e_input_devices_get();
31    EINA_LIST_FOREACH(dev_list, l, device_data)
32      {
33         res = e_input_device_unblock(device_data, client);
34         if (!res) DMWRN("Failed to request unblock client: %p", client);
35      }
36 }
37
38 static Eina_Bool
39 _e_devicemgr_block_timer(void *data)
40 {
41    struct wl_resource *resource = (struct wl_resource *)data;
42    struct wl_client *client = wl_resource_get_client(resource);
43
44    if ((e_devicemgr->block.client) && (e_devicemgr->block.client != client))
45      {
46         return ECORE_CALLBACK_CANCEL;
47      }
48
49    _e_devicemgr_block_client_remove(client);
50    e_devicemgr_wl_block_send_expired(resource);
51
52    return ECORE_CALLBACK_CANCEL;
53 }
54
55 static void
56 _e_devicemgr_block_client_cb_destroy(struct wl_listener *l, void *data)
57 {
58    struct wl_client *client = (struct wl_client *)data;
59
60    if (!e_devicemgr->block.client) return;
61
62    wl_list_remove(&l->link);
63    E_FREE(l);
64
65    _e_devicemgr_block_client_remove(client);
66 }
67
68 static void
69 _e_devicemgr_block_client_add(struct wl_client *client, struct wl_resource *resource, uint32_t clas, uint32_t duration)
70 {
71    struct wl_listener *destroy_listener = NULL;
72    double milli_duration = (double)(duration) / 1000.0;
73    unsigned int block_type = 0x0;
74    Eina_List *dev_list, *l;
75    E_Input_Device *device_data;
76    Eina_Bool res = EINA_TRUE;
77
78    /* Last request of block can renew timer time */
79    if (e_devicemgr->block.duration_timer)
80      ecore_timer_del(e_devicemgr->block.duration_timer);
81    e_devicemgr->block.duration_timer = ecore_timer_add(milli_duration, _e_devicemgr_block_timer, resource);
82
83    e_devicemgr->block.devtype |= clas;
84
85    if (e_devicemgr->block.client) return;
86    e_devicemgr->block.client = client;
87
88    destroy_listener = E_NEW(struct wl_listener, 1);
89    EINA_SAFETY_ON_NULL_GOTO(destroy_listener, failed);
90    destroy_listener->notify = _e_devicemgr_block_client_cb_destroy;
91    wl_client_add_destroy_listener(client, destroy_listener);
92
93    if (e_devicemgr->block.devtype & TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE)
94      {
95         block_type |= E_INPUT_SEAT_POINTER;
96      }
97    if (e_devicemgr->block.devtype & TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD)
98      {
99         block_type |= E_INPUT_SEAT_KEYBOARD;
100      }
101    if (e_devicemgr->block.devtype & TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN)
102      {
103         block_type |= E_INPUT_SEAT_TOUCH;
104      }
105
106    dev_list = (Eina_List *)e_input_devices_get();
107    EINA_LIST_FOREACH(dev_list, l, device_data)
108      {
109         res = e_input_device_block(device_data, block_type, client);
110         if (!res) DMWRN("Failed to request block block_type: 0x%x, client: %p", block_type, client);
111      }
112
113    return;
114
115 failed:
116    ecore_timer_del(e_devicemgr->block.duration_timer);
117    e_devicemgr->block.duration_timer = NULL;
118    e_devicemgr->block.client = NULL;
119 }
120
121 int
122 e_devicemgr_block_add(struct wl_client *client, struct wl_resource *resource, uint32_t clas, uint32_t duration)
123 {
124    uint32_t all_class = TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE |
125                         TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD |
126                         TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN;
127
128    if ((e_devicemgr->block.client) && (e_devicemgr->block.client != client))
129      {
130         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_BLOCKED_ALREADY;
131      }
132    if (!(clas & all_class))
133      {
134         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_CLASS;
135      }
136
137    _e_devicemgr_block_client_add(client, resource, clas, duration);
138
139    /* TODO: Release pressed button or key */
140
141    return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
142 }
143
144 int
145 e_devicemgr_block_remove(struct wl_client *client)
146 {
147    if ((e_devicemgr->block.client) && (e_devicemgr->block.client != client))
148     {
149        return TIZEN_INPUT_DEVICE_MANAGER_ERROR_BLOCKED_ALREADY;
150     }
151
152    _e_devicemgr_block_client_remove(client);
153
154    return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
155 }
156
157 static Eina_Bool
158 _e_devicemgr_block_timer_internal(void *data)
159 {
160    Eina_List *dev_list, *l;
161    E_Input_Device *device_data;
162    Eina_Bool res;
163
164    e_devicemgr->block.internal_devtype = 0x0;
165    e_devicemgr->block.internal_duration_timer = NULL;
166
167    if (!e_devicemgr->block.internal_cb_func) return ECORE_CALLBACK_CANCEL;
168
169    //Call expiration cb for internal block
170    e_devicemgr->block.internal_cb_func(e_devicemgr->block.internal_cb_data);
171
172    e_devicemgr->block.internal_cb_func = NULL;
173    e_devicemgr->block.internal_cb_data = NULL;
174
175    dev_list = (Eina_List *)e_input_devices_get();
176    EINA_LIST_FOREACH(dev_list, l, device_data)
177      {
178         res = e_input_device_unblock(device_data, E_INPUT_REQUEST_SERVER);
179         if (!res) DMWRN("Failed to request unblock by server");
180      }
181
182    return ECORE_CALLBACK_DONE;
183 }
184
185
186
187 Eina_Bool
188 e_devicemgr_block_add_internal(uint32_t clas, uint32_t duration, E_Devicemgr_Block_Expire_Cb cb_func, void *cb_data)
189 {
190    uint32_t all_class = TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE |
191                         TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD |
192                         TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN;
193    unsigned int block_type = 0x0;
194    Eina_List *dev_list, *l;
195    E_Input_Device *device_data;
196    Eina_Bool res;
197
198    if (!(clas & all_class)) return EINA_FALSE;
199
200    if (e_devicemgr->block.internal_duration_timer)
201      ecore_timer_del(e_devicemgr->block.internal_duration_timer);
202    e_devicemgr->block.internal_duration_timer = ecore_timer_add(duration / 1000.0, _e_devicemgr_block_timer_internal, NULL);
203
204    e_devicemgr->block.internal_devtype |= clas;
205
206    e_devicemgr->block.internal_cb_func = cb_func;
207    e_devicemgr->block.internal_cb_data = cb_data;
208
209    if (e_devicemgr->block.internal_devtype & TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE)
210      {
211         block_type |= E_INPUT_SEAT_POINTER;
212      }
213    if (e_devicemgr->block.internal_devtype & TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD)
214      {
215         block_type |= E_INPUT_SEAT_KEYBOARD;
216      }
217    if (e_devicemgr->block.internal_devtype & TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN)
218      {
219         block_type |= E_INPUT_SEAT_TOUCH;
220      }
221
222    dev_list = (Eina_List *)e_input_devices_get();
223    EINA_LIST_FOREACH(dev_list, l, device_data)
224      {
225         res = e_input_device_block(device_data, block_type, E_INPUT_REQUEST_SERVER);
226         if (!res) DMWRN("Failed to request block block_type: 0x%x by server", block_type);
227      }
228
229    return EINA_TRUE;
230 }
231
232 Eina_Bool
233 e_devicemgr_block_remove_internal(E_Devicemgr_Block_Expire_Cb cb_func, void *cb_data)
234 {
235    Eina_List *dev_list, *l;
236    E_Input_Device *device_data;
237    Eina_Bool res;
238
239    if ((e_devicemgr->block.internal_cb_func != cb_func) ||
240        (e_devicemgr->block.internal_cb_data != cb_data)) return EINA_FALSE;
241
242    e_devicemgr->block.internal_devtype = 0x0;
243    if (e_devicemgr->block.internal_duration_timer)
244      {
245         ecore_timer_del(e_devicemgr->block.internal_duration_timer);
246         e_devicemgr->block.internal_duration_timer = NULL;
247      }
248
249    e_devicemgr->block.internal_cb_func = NULL;
250    e_devicemgr->block.internal_cb_data = NULL;
251
252    dev_list = (Eina_List *)e_input_devices_get();
253    EINA_LIST_FOREACH(dev_list, l, device_data)
254      {
255         res = e_input_device_unblock(device_data, E_INPUT_REQUEST_SERVER);
256         if (!res) DMWRN("Failed to request unblock by server");
257      }
258
259    return EINA_TRUE;
260 }