c613d0755358d76fdf010665497ff51c8b093df3
[platform/upstream/enlightenment.git] / src / bin / e_devicemgr.c
1 //#include "e.h"
2 #include "e_devicemgr_private.h"
3
4 E_API E_Devicemgr *e_devicemgr;
5
6 static int _e_devicemgr_intercept_hooks_delete = 0;
7 static int _e_devicemgr_intercept_hooks_walking = 0;
8
9 static Eina_Inlist *_e_devicemgr_intercept_hooks[] =
10 {
11    [E_DEVICEMGR_INTERCEPT_HOOK_DETENT] = NULL,
12 };
13
14 int _devicemgr_log_dom = -1;
15
16 EINTERN E_Devicemgr_Intercept_Hook *
17 e_devicemgr_intercept_hook_add(E_Devicemgr_Intercept_Hook_Point hookpoint, E_Devicemgr_Intercept_Hook_Cb func, const void *data)
18 {
19    E_Devicemgr_Intercept_Hook *ch;
20
21    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint < 0 || hookpoint >= E_DEVICEMGR_INTERCEPT_HOOK_LAST,
22                                   EINA_FALSE);
23
24    ch = E_NEW(E_Devicemgr_Intercept_Hook, 1);
25    if (!ch) return NULL;
26    ch->hookpoint = hookpoint;
27    ch->func = func;
28    ch->data = (void*)data;
29    _e_devicemgr_intercept_hooks[hookpoint] = eina_inlist_append(_e_devicemgr_intercept_hooks[hookpoint], EINA_INLIST_GET(ch));
30    return ch;
31 }
32
33 EINTERN void
34 e_devicemgr_intercept_hook_del(E_Devicemgr_Intercept_Hook *ch)
35 {
36    EINA_SAFETY_ON_NULL_RETURN(ch);
37
38    ch->delete_me = 1;
39    if (_e_devicemgr_intercept_hooks_walking == 0)
40      {
41         _e_devicemgr_intercept_hooks[ch->hookpoint] = eina_inlist_remove(_e_devicemgr_intercept_hooks[ch->hookpoint], EINA_INLIST_GET(ch));
42         free(ch);
43      }
44    else
45      _e_devicemgr_intercept_hooks_delete++;
46 }
47
48 static void
49 _e_devicemgr_intercept_hooks_clean(void)
50 {
51    Eina_Inlist *l;
52    E_Devicemgr_Intercept_Hook *ch;
53    unsigned int x;
54    for (x = 0; x < E_DEVICEMGR_INTERCEPT_HOOK_LAST; x++)
55      EINA_INLIST_FOREACH_SAFE(_e_devicemgr_intercept_hooks[x], l, ch)
56        {
57           if (!ch->delete_me) continue;
58           _e_devicemgr_intercept_hooks[x] = eina_inlist_remove(_e_devicemgr_intercept_hooks[x], EINA_INLIST_GET(ch));
59          free(ch);
60        }
61 }
62
63 EINTERN Eina_Bool
64 e_devicemgr_intercept_hook_call(E_Devicemgr_Intercept_Hook_Point hookpoint, void *event)
65 {
66    E_Devicemgr_Intercept_Hook *ch;
67    Eina_Bool res = EINA_TRUE, ret = EINA_TRUE;
68
69    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint < 0 || hookpoint >= E_DEVICEMGR_INTERCEPT_HOOK_LAST,
70                                   EINA_FALSE);
71
72    _e_devicemgr_intercept_hooks_walking++;
73    EINA_INLIST_FOREACH(_e_devicemgr_intercept_hooks[hookpoint], ch)
74      {
75         if (ch->delete_me) continue;
76         res = ch->func(ch->data, hookpoint, event);
77         if (!res) ret = EINA_FALSE;
78      }
79    _e_devicemgr_intercept_hooks_walking--;
80    if ((_e_devicemgr_intercept_hooks_walking == 0) && (_e_devicemgr_intercept_hooks_delete > 0))
81      _e_devicemgr_intercept_hooks_clean();
82
83    return ret;
84 }
85
86 E_API Eina_Bool
87 e_devicemgr_is_blocking_event(Ecore_Device_Class clas)
88 {
89    unsigned int dev_clas = 0x0;
90
91    switch (clas)
92      {
93         case ECORE_DEVICE_CLASS_KEYBOARD:
94           dev_clas = TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD;
95           break;
96         case ECORE_DEVICE_CLASS_MOUSE:
97           dev_clas = TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE;
98           break;
99         case ECORE_DEVICE_CLASS_TOUCH:
100           dev_clas = TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN;
101           break;
102         default:
103           return EINA_FALSE;
104      }
105
106    if (e_devicemgr->block.devtype & dev_clas)
107      return EINA_TRUE;
108
109    return EINA_FALSE;
110 }
111
112 EINTERN int
113 e_devicemgr_init(void)
114 {
115    E_Devicemgr_Config_Data *dconfig = NULL;
116    Eina_Bool res = EINA_FALSE;
117
118    EINA_SAFETY_ON_NULL_GOTO(e_comp, failed);
119
120    _devicemgr_log_dom = eina_log_domain_register("e-devicemgr", EINA_COLOR_BLUE);
121    EINA_SAFETY_ON_FALSE_GOTO(_devicemgr_log_dom >= 0, failed);
122    eina_log_domain_level_set("e-devicemgr", EINA_LOG_LEVEL_INFO);
123
124    e_devicemgr = E_NEW(E_Devicemgr, 1);
125    EINA_SAFETY_ON_NULL_GOTO(e_devicemgr, failed);
126
127    dconfig = E_NEW(E_Devicemgr_Config_Data, 1);
128    EINA_SAFETY_ON_NULL_GOTO(dconfig, failed);
129
130    e_devicemgr_conf_init(dconfig);
131    e_devicemgr->dconfig = dconfig;
132
133    g_rec_mutex_init(&e_devicemgr->device_list_mutex);
134
135    res = e_devicemgr_wl_init();
136    EINA_SAFETY_ON_FALSE_GOTO(res, wl_failed);
137
138    res = e_devicemgr_input_init();
139    EINA_SAFETY_ON_FALSE_GOTO(res, input_failed);
140
141    return EINA_TRUE;
142
143 input_failed:
144    e_devicemgr_input_shutdown();
145
146 wl_failed:
147    e_devicemgr_wl_shutdown();
148
149 failed:
150    if (e_devicemgr)
151      {
152
153         if (e_devicemgr->dconfig)
154           {
155              e_devicemgr_conf_fini(e_devicemgr->dconfig);
156              E_FREE(e_devicemgr->dconfig);
157              e_devicemgr->dconfig = NULL;
158           }
159
160         E_FREE(e_devicemgr);
161         e_devicemgr = NULL;
162      }
163    eina_log_domain_unregister(_devicemgr_log_dom);
164    _devicemgr_log_dom = -1;
165    return EINA_FALSE;
166 }
167
168 EINTERN int
169 e_devicemgr_shutdown(void)
170 {
171    g_rec_mutex_clear(&e_devicemgr->device_list_mutex);
172
173    eina_log_domain_unregister(_devicemgr_log_dom);
174    _devicemgr_log_dom = -1;
175    if (e_devicemgr->dconfig)
176      {
177         e_devicemgr_conf_fini(e_devicemgr->dconfig);
178         E_FREE(e_devicemgr->dconfig);
179         e_devicemgr->dconfig = NULL;
180      }
181    E_FREE(e_devicemgr);
182    return EINA_TRUE;
183 }
184
185 EINTERN Eina_Bool
186 e_devicemgr_block_internal_add(Ecore_Device_Class clas, Eina_Bool all_class, uint32_t duration, E_Devicemgr_Block_Expire_Cb cb_func, void *cb_data)
187 {
188    unsigned int dev_clas = 0x0;
189
190    if (all_class)
191      dev_clas = TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD |
192                 TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE |
193                 TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN;
194    else
195      {
196         switch (clas)
197           {
198            case ECORE_DEVICE_CLASS_KEYBOARD:
199               dev_clas = TIZEN_INPUT_DEVICE_MANAGER_CLAS_KEYBOARD;
200               break;
201            case ECORE_DEVICE_CLASS_MOUSE:
202               dev_clas = TIZEN_INPUT_DEVICE_MANAGER_CLAS_MOUSE;
203               break;
204            case ECORE_DEVICE_CLASS_TOUCH:
205               dev_clas = TIZEN_INPUT_DEVICE_MANAGER_CLAS_TOUCHSCREEN;
206               break;
207            default:
208               return EINA_FALSE;
209           }
210      }
211
212    return e_devicemgr_block_add_internal(dev_clas, duration, cb_func, cb_data);
213 }
214
215 EINTERN Eina_Bool
216 e_devicemgr_block_internal_remove(E_Devicemgr_Block_Expire_Cb cb_func, void *cb_data)
217 {
218    return e_devicemgr_block_remove_internal(cb_func, cb_data);
219 }
220
221 E_API Eina_Bool
222 e_devicemgr_block_reset(void)
223 {
224    int ret;
225
226    if (!e_devicemgr->block.client) return EINA_FALSE;
227    ret = e_devicemgr_block_remove(e_devicemgr->block.client);
228    if (ret == TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE)
229      return EINA_TRUE;
230
231    return EINA_FALSE;
232 }
233