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