4da5d38e8e0fb0e3b8da9918e1daa77ae16a1908
[platform/upstream/enlightenment.git] / src / bin / e_input.c
1 #include "e.h"
2 #include "e_input_private.h"
3 #include <Ecore_Input_Evas.h>
4
5 int _e_input_init_count;
6 int _e_input_log_dom = -1;
7
8 E_API int E_INPUT_EVENT_SEAT_ADD = -1;
9 E_API int E_EVENT_INPUT_ENABLED = -1;
10 E_API int E_EVENT_INPUT_DISABLED = -1;
11
12 E_API E_Input *e_input = NULL;
13
14 static Eina_Bool
15 _e_input_cb_screen_change(void *data EINA_UNUSED, int type EINA_UNUSED, void *event EINA_UNUSED)
16 {
17    Eina_List *l;
18    Eina_List *devices;
19    E_Input_Device *dev_data;
20
21    devices = (Eina_List *)e_input_devices_get();
22
23    EINA_LIST_FOREACH(devices, l, dev_data)
24      {
25         e_input_device_output_changed(dev_data);
26      }
27    return ECORE_CALLBACK_PASS_ON;
28 }
29
30 EINTERN const char *
31 e_input_base_dir_get(void)
32 {
33    return e_input->input_base_dir;
34 }
35
36 EINTERN Eina_Bool
37 e_input_thread_enabled_get(void)
38 {
39    return e_input->use_thread;
40 }
41
42 EINTERN int
43 e_input_init(Ecore_Evas *ee)
44 {
45    char *env = NULL;
46    unsigned int seat_caps = 0;
47
48    E_Input_Device *dev;
49
50    Eina_Bool use_udev_backend = EINA_FALSE;
51    Eina_Bool use_path_backend = EINA_FALSE;
52    Eina_Bool skip_udev_enumeration = EINA_FALSE;
53
54    EINA_SAFETY_ON_NULL_RETURN_VAL(ee, _e_input_init_count);
55
56    TRACE_INPUT_BEGIN(e_input_init);
57
58    if (++_e_input_init_count != 1) return _e_input_init_count;
59    if (!ecore_event_evas_init()) goto ecore_event_evas_err;
60    if (!eeze_init()) goto eeze_err;
61
62    _e_input_log_dom = eina_log_domain_register("e_input", EINA_COLOR_GREEN);
63    if (!_e_input_log_dom)
64      {
65         EINA_LOG_ERR("Could not create logging domain for E_Input");
66         goto log_err;
67      }
68
69    E_INPUT_EVENT_SEAT_ADD = ecore_event_type_new();
70    E_EVENT_INPUT_ENABLED = ecore_event_type_new();
71    E_EVENT_INPUT_DISABLED = ecore_event_type_new();
72
73    ecore_event_add(E_EVENT_INPUT_ENABLED, NULL, NULL, NULL);
74
75    ecore_evas_input_event_register_with_multi2(ee);
76
77    if (!e_input)
78      {
79         e_input = (E_Input *)calloc(1, sizeof(E_Input));
80      }
81
82    if (!e_input)
83      {
84         EINA_LOG_ERR("Failed to alloc memory for e_input\n");
85         goto log_err;
86      }
87
88    // TODO : make this variable configurable e.g. e.cfg
89    e_input->input_base_dir = eina_stringshare_add("/dev/input");
90    e_input->use_thread = EINA_FALSE;
91
92    e_input->touch_device_count = 0;
93    e_input->touch_max_count = 1;//This is going to updated when a touch device is attached.
94
95    dev = e_input_device_open();
96
97    e_input->ee = ee;
98    e_input->dev = dev;
99
100    if (!dev)
101      {
102         EINA_LOG_ERR("Failed to open device\n");
103         goto log_err;
104      }
105
106    e_input->window = ecore_evas_window_get(ee);
107    e_input_device_window_set(dev, e_input->window);
108
109    env = e_util_env_get("E_INPUT_USE_THREAD_INIT");
110
111    if (env)
112      {
113         e_input->use_thread = EINA_TRUE;
114         E_FREE(env);
115      }
116
117    env = e_util_env_get("LIBINPUT_UDEV_SKIP_INITIAL_ENUMERATION");
118    if (env)
119      {
120         skip_udev_enumeration = EINA_TRUE;
121         E_FREE(env);
122      }
123
124    env = e_util_env_get("E_INPUT_USE_LIBINPUT_UDEV_BACKEND");
125    if (env)
126      {
127         use_udev_backend = EINA_TRUE;
128         E_FREE(env);
129      }
130
131    env = e_util_env_get("E_INPUT_USE_LIBINPUT_PATH_BACKEND");
132    if (env)
133      {
134         use_path_backend = EINA_TRUE;
135         E_FREE(env);
136      }
137
138    TRACE_INPUT_BEGIN(e_input_device_input_backend_create);
139
140    /* FIXME: we need to select default backend udev or path.
141     *        Input system will not be initialized, if there are no enviroment is set.
142     */
143    if ((!use_udev_backend) && (!use_path_backend))
144      {
145         EINA_LOG_INFO("This system doesn't select input backend. We use udev backend defaultly\n");
146         use_udev_backend = EINA_TRUE;
147      }
148
149    if ((use_udev_backend) &&
150        (!e_input_device_input_backend_create(dev, E_INPUT_LIBINPUT_BACKEND_UDEV)))
151      {
152         EINA_LOG_ERR("Failed to create e_input_device\n");
153         TRACE_INPUT_END();
154         goto device_create_err;
155      }
156
157    if ((use_path_backend) && (!use_udev_backend || skip_udev_enumeration) &&
158        (!e_input_device_input_backend_create(dev, E_INPUT_LIBINPUT_BACKEND_PATH)))
159      {
160         EINA_LOG_ERR("Failed to create e_input_device\n");
161         TRACE_INPUT_END();
162         goto device_create_err;
163      }
164
165    TRACE_INPUT_END();
166
167    if (use_udev_backend && skip_udev_enumeration && !use_path_backend)
168      {
169         /* Enable some of keyboard, touch devices temporarily */
170         /* FIXME : get seat caps from e_input configuration or env */
171         seat_caps = E_INPUT_SEAT_KEYBOARD | E_INPUT_SEAT_TOUCH;
172         e_comp_wl_input_seat_caps_set(seat_caps);
173      }
174
175    E_LIST_HANDLER_APPEND(e_input->handlers, E_EVENT_SCREEN_CHANGE, _e_input_cb_screen_change, NULL);
176
177    TRACE_INPUT_END();
178
179    return _e_input_init_count;
180
181 device_create_err:
182    e_input_device_close(dev);
183
184 log_err:
185    if (e_input && e_input->input_base_dir)
186      {
187         eina_stringshare_del(e_input->input_base_dir);
188         e_input->input_base_dir = NULL;
189      }
190
191    eeze_shutdown();
192
193 eeze_err:
194    ecore_event_evas_shutdown();
195
196 ecore_event_evas_err:
197
198    TRACE_INPUT_END();
199
200    return --_e_input_init_count;
201 }
202
203 EINTERN int
204 e_input_shutdown(void)
205 {
206    Ecore_Event_Handler *h = NULL;
207
208    if (_e_input_init_count < 1) return 0;
209    if (--_e_input_init_count != 0) return _e_input_init_count;
210
211    ecore_event_add(E_EVENT_INPUT_DISABLED, NULL, NULL, NULL);
212
213    E_INPUT_EVENT_SEAT_ADD = -1;
214    E_EVENT_INPUT_ENABLED = -1;
215    E_EVENT_INPUT_DISABLED = -1;
216
217    EINA_LIST_FREE(e_input->handlers, h)
218      ecore_event_handler_del(h);
219
220    if (e_input->input_base_dir)
221      eina_stringshare_del(e_input->input_base_dir);
222    e_input_device_close(e_input->dev);
223    free(e_input);
224
225    ecore_event_evas_shutdown();
226    eeze_shutdown();
227    ecore_event_shutdown();
228    ecore_shutdown();
229    eina_shutdown();
230
231    return _e_input_init_count;
232 }
233
234 EINTERN E_Input *
235 e_input_get()
236 {
237    if (e_input) return e_input;
238    return NULL;
239 }
240
241 EINTERN Ecore_Evas *
242 e_input_ecore_evas_get(E_Input *ei)
243 {
244    if (ei) return ei->ee;
245    return NULL;
246 }
247
248 E_API unsigned int
249 e_input_touch_max_count_get()
250 {
251    if (e_input)
252      return e_input->touch_max_count;
253    else
254      return 0;
255 }
256
257 E_API void
258 e_input_touch_max_count_set(unsigned int max_count)
259 {
260    if (e_input)
261      e_input->touch_max_count = max_count;
262 }