26053be84db8130386cec46fea5bf2436f16effd
[platform/upstream/enlightenment.git] / src / bin / e_devicemgr_input.c
1 #include "e_devicemgr_private.h"
2 #include "e_input_event.h"
3 #include "e_comp_wl_intern.h"
4
5 Eina_Bool
6 e_devicemgr_strcmp(const char *dst, const char *src)
7 {
8    int dst_len, src_len, str_len;
9
10    dst_len = strlen(dst);
11    src_len = strlen(src);
12
13    if (src_len > dst_len) str_len = src_len;
14    else str_len = dst_len;
15
16    if (!strncmp(dst, src, str_len))
17      return EINA_TRUE;
18    else
19      return EINA_FALSE;
20 }
21
22 static int
23 _e_devicemgr_input_pointer_warp(int x, int y)
24 {
25    e_input_device_pointer_warp(NULL, x, y);
26    DMWRN("The pointer warped to (%d, %d) !\n", x, y);
27
28    return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
29 }
30
31 int
32 e_devicemgr_input_pointer_warp(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, wl_fixed_t x, wl_fixed_t y)
33 {
34    E_Client *ec;
35    E_Map *map;
36    int new_x, new_y, ret;
37    int i, min_x, min_y, max_x, max_y, tmp_x, tmp_y, map_x = 0, map_y = 0;
38
39    if (!(ec = e_client_from_surface_resource(surface)) || !ec->visible)
40      {
41         DMERR("The given surface is invalid or invisible !\n");
42         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_SURFACE;
43      }
44
45    if (ec != e_comp_wl->ptr.ec)
46      {
47         DMERR("Pointer is not on the given surface  !\n");
48         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_SURFACE;
49      }
50
51    if ((!e_config->show_cursor) || (!e_comp_wl->ptr.enabled))
52      {
53         DMERR("Pointer is not available");
54         return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_POINTER_AVAILABLE;
55      }
56
57    new_x = wl_fixed_to_int(x);
58    new_y = wl_fixed_to_int(y);
59
60    if (new_x < 0) new_x = 0;
61    if ((ec->w != 0) && (new_x > ec->w - 1)) new_x = ec->w - 1;
62
63    if (new_y < 0) new_y = 0;
64    if ((ec->h != 0) && (new_y > ec->h - 1)) new_y = ec->h - 1;
65
66    new_x = ec->client.x + new_x;
67    new_y = ec->client.y + new_y;
68
69    if (e_client_transform_core_enable_get(ec))
70      {
71         e_comp_wl_map_inv_coord_get(ec, new_x, new_y, &map_x, &map_y);
72         DMWRN("Pointer warp. (%d, %d) -> map_inv_coord (%d, %d)",
73               new_x, new_y, map_x, map_y);
74
75         map = e_client_map_get(ec);
76         e_map_point_coord_get(map, 0, &min_x, &min_y, NULL);
77         max_x = min_x;
78         max_y = min_y;
79
80         for (i = 1; i < e_map_count_get(map); i++)
81           {
82              e_map_point_coord_get(map, i, &tmp_x, &tmp_y, NULL);
83
84              if (tmp_x < min_x) min_x = tmp_x;
85              else if (tmp_x > max_x) max_x = tmp_x;
86
87              if (tmp_y < min_y) min_y = tmp_y;
88              else if (tmp_y > max_y) max_y = tmp_y;
89           }
90
91         if (min_x != max_x)
92           {
93              if (map_x < min_x) map_x = min_x;
94              if (map_x > max_x - 1) map_x = max_x - 1;
95           }
96         if (min_y != max_y)
97           {
98              if (map_y < min_y) map_y = min_y;
99              if (map_y > max_y - 1) map_y = max_y - 1;
100           }
101         e_map_free(map);
102         new_x = map_x;
103         new_y = map_y;
104      }
105    ret = _e_devicemgr_input_pointer_warp(new_x, new_y);
106    return ret;
107 }
108
109 EINTERN Eina_Bool
110 e_devicemgr_detent_is_detent(const char *name)
111 {
112    EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE);
113
114    if (!strncmp(name, DETENT_DEVICE_NAME, sizeof(DETENT_DEVICE_NAME)))
115      return EINA_TRUE;
116
117    return EINA_FALSE;
118 }
119
120 static void
121 _e_devicemgr_detent_set_info(E_Devicemgr_Input_Device *dev)
122 {
123    if (dev->name && !strncmp(dev->name, DETENT_DEVICE_NAME, sizeof(DETENT_DEVICE_NAME)))
124      {
125         E_FREE(e_devicemgr->detent.identifier);
126         if (dev->identifier) e_devicemgr->detent.identifier = strdup(dev->identifier);
127      }
128 }
129
130 static void
131 _e_devicemgr_detent_unset_info(E_Devicemgr_Input_Device *dev)
132 {
133    if (dev->name && !strncmp(dev->name, DETENT_DEVICE_NAME, sizeof(DETENT_DEVICE_NAME)))
134      {
135         E_FREE(e_devicemgr->detent.identifier);
136      }
137 }
138
139 static void
140 _e_devicemgr_input_keyevent_free(void *data EINA_UNUSED, void *ev)
141 {
142    Ecore_Event_Key *e = ev;
143
144    eina_stringshare_del(e->keyname);
145    eina_stringshare_del(e->key);
146    eina_stringshare_del(e->compose);
147
148    E_FREE(e->data);
149    E_FREE(e);
150 }
151
152 static Eina_Bool
153 _e_devicemgr_input_mouse_button_remap(Ecore_Event_Mouse_Button *ev, Eina_Bool pressed)
154 {
155    Ecore_Event_Key *ev_key;
156    E_Keyrouter_Event_Data *key_data;
157    struct xkb_keymap *keymap = NULL;
158
159    g_mutex_lock(&e_comp_wl->xkb.keymap_mutex);
160    keymap = e_comp_wl->xkb.keymap;
161    g_mutex_unlock(&e_comp_wl->xkb.keymap_mutex);
162
163    EINA_SAFETY_ON_NULL_RETURN_VAL(keymap, ECORE_CALLBACK_PASS_ON);
164    EINA_SAFETY_ON_NULL_RETURN_VAL(ev, ECORE_CALLBACK_PASS_ON);
165
166    if (ev->buttons != 3) return ECORE_CALLBACK_PASS_ON;
167
168    ev_key = E_NEW(Ecore_Event_Key, 1);
169    EINA_SAFETY_ON_NULL_RETURN_VAL(ev_key, ECORE_CALLBACK_PASS_ON);
170
171    key_data = E_NEW(E_Keyrouter_Event_Data, 1);
172    EINA_SAFETY_ON_NULL_GOTO(key_data, failed);
173
174    ev_key->key = (char *)eina_stringshare_add("XF86Back");
175    ev_key->keyname = (char *)eina_stringshare_add(ev_key->key);
176    ev_key->compose = (char *)eina_stringshare_add(ev_key->key);
177    ev_key->timestamp = e_util_timestamp_get();
178    ev_key->same_screen = 1;
179
180    ev_key->window = e_comp->ee_win;
181    ev_key->event_window = e_comp->ee_win;
182    ev_key->root_window = e_comp->ee_win;
183    ev_key->keycode = e_devicemgr->dconfig->conf->input.back_keycode;
184    ev_key->data = key_data;
185
186    if (e_input_thread_mode_get())
187      {
188         E_Input_Event_Source *input_event_source = e_input_event_source_get();
189         e_input_event_add(input_event_source, pressed ? ECORE_EVENT_KEY_DOWN : ECORE_EVENT_KEY_UP, ev_key, _e_devicemgr_input_keyevent_free, NULL);
190      }
191    else
192      {
193         ecore_event_add(pressed ? ECORE_EVENT_KEY_DOWN : ECORE_EVENT_KEY_UP, ev_key, _e_devicemgr_input_keyevent_free, NULL);
194      }
195
196    return ECORE_CALLBACK_DONE;
197
198 failed:
199    E_FREE(ev_key);
200    return ECORE_CALLBACK_PASS_ON;
201 }
202
203 static void
204 _e_devicemgr_input_device_add(const char *name, const char *identifier, const char *seatname, Ecore_Device_Class clas, Ecore_Device_Subclass subclas)
205 {
206    E_Devicemgr_Input_Device *dev = NULL;
207    Eina_List *l;
208    int current_touch_count = -1;
209
210    EINA_SAFETY_ON_NULL_RETURN(name);
211    EINA_SAFETY_ON_NULL_RETURN(identifier);
212    EINA_SAFETY_ON_NULL_RETURN(e_devicemgr);
213
214    g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
215    EINA_LIST_FOREACH(e_devicemgr->device_list, l, dev)
216      {
217         if (!dev->name) continue;
218         if (!dev->identifier) continue;
219         if ((dev->clas == clas) && (dev->subclas == subclas) &&
220             (!strcmp(dev->name, name)) &&
221             (!strcmp(dev->identifier, identifier)))
222           {
223              g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
224              return;
225           }
226      }
227    g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
228
229    if (!(dev = E_NEW(E_Devicemgr_Input_Device, 1))) return;
230    dev->name = eina_stringshare_add(name);
231    dev->identifier = eina_stringshare_add(identifier);
232    dev->seat_name = eina_stringshare_add(seatname);
233    dev->clas = clas;
234    dev->subclas = subclas;
235
236    g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
237    e_devicemgr->device_list = eina_list_append(e_devicemgr->device_list, dev);
238    g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
239
240    if (dev->clas == ECORE_DEVICE_CLASS_MOUSE)
241      e_devicemgr->last_device_ptr = dev;
242
243    if (dev->clas == ECORE_DEVICE_CLASS_TOUCH)
244      {
245         if (!e_devicemgr->last_device_touch)
246           e_devicemgr->last_device_touch = dev;
247
248         current_touch_count = e_input_touch_max_count_get();
249         if (e_devicemgr->max_touch_count < current_touch_count)
250           {
251              e_devicemgr->max_touch_count = current_touch_count;
252              if (e_devicemgr->multi)
253                {
254                   ELOGF("DEVMGR_TOUCH", "Multi pointer is NOT NULL. It'll be updated to %d finger of touch.\n", NULL, current_touch_count);
255                   free (e_devicemgr->multi);
256                }
257
258              e_devicemgr->multi = calloc(1, sizeof(E_Devicemgr_Input_Device_Multi)*current_touch_count);
259
260              if (!e_devicemgr->multi)
261                ELOGF("DEVMGR_TOUCH", "Failed to allocate memory for multi ptr. (finger=%d)\n", NULL, current_touch_count);
262
263              e_devicemgr_wl_touch_max_count_send(e_devicemgr->max_touch_count, NULL, NULL);
264           }
265      }
266
267    E_Devicemgr_Input_Device *last_device_kbd = atomic_load(&e_devicemgr->last_device_kbd);
268    if (!last_device_kbd && dev->clas == ECORE_DEVICE_CLASS_KEYBOARD)
269      atomic_store(&e_devicemgr->last_device_kbd, dev);
270
271    e_devicemgr_inputgen_device_ready_send(dev);
272    e_devicemgr_wl_device_add(dev);
273    e_devicemgr_inputgen_get_device_info(dev);
274    _e_devicemgr_detent_set_info(dev);
275 }
276
277 static void
278 _e_devicemgr_input_device_del(const char *name, const char *identifier, const char *seatname, Ecore_Device_Class clas, Ecore_Device_Subclass subclas)
279 {
280    E_Devicemgr_Input_Device *dev = NULL;
281    E_Devicemgr_Input_Device *last_device_kbd = NULL;
282    Eina_List *l;
283
284    EINA_SAFETY_ON_NULL_RETURN(name);
285    EINA_SAFETY_ON_NULL_RETURN(identifier);
286
287    g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
288    EINA_LIST_FOREACH(e_devicemgr->device_list, l, dev)
289      {
290         if (!dev->name) continue;
291         if (!dev->identifier) continue;
292         if ((dev->clas == clas) && (dev->subclas == subclas) &&
293             (!strcmp(dev->name, name)) &&
294             (!strcmp(dev->identifier, identifier)))
295           break;
296      }
297
298    if (!dev)
299      {
300         g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
301         return;
302      }
303
304    _e_devicemgr_detent_unset_info(dev);
305    e_devicemgr_wl_device_del(dev);
306
307    if (dev->name) eina_stringshare_del(dev->name);
308    if (dev->identifier) eina_stringshare_del(dev->identifier);
309    if (dev->seat_name) eina_stringshare_del(dev->seat_name);
310
311    e_devicemgr->device_list = eina_list_remove(e_devicemgr->device_list, dev);
312
313    if (e_devicemgr->last_device_ptr == dev)
314      e_devicemgr->last_device_ptr = NULL;
315
316    if (e_devicemgr->last_device_touch == dev)
317      e_devicemgr->last_device_touch = NULL;
318
319    last_device_kbd = atomic_load(&e_devicemgr->last_device_kbd);
320
321    if (last_device_kbd == dev)
322      atomic_store(&e_devicemgr->last_device_kbd, NULL);
323
324    E_FREE(dev);
325
326    g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
327 }
328
329 static void
330 _e_devicemgr_input_device_update(Ecore_Device *dev)
331 {
332    Eina_List *l;
333    E_Devicemgr_Input_Device *data;
334    char *dev_identifier;
335
336    EINA_SAFETY_ON_NULL_RETURN(dev);
337
338    dev_identifier = (char *)ecore_device_identifier_get(dev);
339    EINA_SAFETY_ON_NULL_RETURN(dev_identifier);
340
341    g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
342    EINA_LIST_FOREACH(e_devicemgr->device_list, l, data)
343      {
344         if (data->clas == ecore_device_class_get(dev) && data->identifier)
345           {
346              if (e_devicemgr_strcmp(dev_identifier, data->identifier))
347                {
348                   data->subclas = ecore_device_subclass_get(dev);
349
350                   e_devicemgr_wl_device_update(data);
351                   g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
352                   return;
353                }
354           }
355      }
356    g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
357 }
358
359 static Eina_Bool
360 _e_devicemgr_input_cb_mouse_button_down(void *data, int type, void *event)
361 {
362    Ecore_Event_Mouse_Button *ev;
363    Eina_Bool res = ECORE_CALLBACK_PASS_ON;
364    E_Comp_Config *comp_conf = NULL;
365
366    EINA_SAFETY_ON_NULL_RETURN_VAL(event, res);
367    ev = (Ecore_Event_Mouse_Button *)event;
368
369    if (e_devicemgr->dconfig->conf->input.button_remap_enable)
370      res = _e_devicemgr_input_mouse_button_remap(ev, EINA_TRUE);
371
372    comp_conf = e_comp_config_get();
373    if (comp_conf && comp_conf->input_log_enable)
374      {
375         ELOGF("DEVMGR", "Mouse Down (id: %d, button: %d, x: %d, y: %d), res: %d", NULL,
376               ev->multi.device, ev->buttons, ev->x, ev->y, res);
377      }
378
379    return res;
380 }
381
382 static Eina_Bool
383 _e_devicemgr_input_cb_mouse_button_up(void *data, int type, void *event)
384 {
385    Ecore_Event_Mouse_Button *ev;
386    Eina_Bool res = ECORE_CALLBACK_PASS_ON;
387    E_Comp_Config *comp_conf = NULL;
388
389    EINA_SAFETY_ON_NULL_RETURN_VAL(event, res);
390    ev = (Ecore_Event_Mouse_Button *)event;
391
392    if (e_devicemgr->dconfig->conf->input.button_remap_enable)
393      res = _e_devicemgr_input_mouse_button_remap(ev, EINA_FALSE);
394
395    comp_conf = e_comp_config_get();
396    if (comp_conf && comp_conf->input_log_enable)
397      {
398         ELOGF("DEVMGR", "Mouse Up (id: %d, button: %d, x: %d, y: %d), res: %d", NULL,
399               ev->multi.device, ev->buttons, ev->x, ev->y, res);
400      }
401
402    return res;
403 }
404
405 static Eina_Bool
406 _e_devicemgr_input_cb_mouse_wheel(void *data, int type, void *event)
407 {
408    Ecore_Event_Mouse_Wheel *ev;
409    int detent;
410    Eina_Bool res = ECORE_CALLBACK_PASS_ON;
411    E_Comp_Config *comp_conf;
412
413    EINA_SAFETY_ON_NULL_RETURN_VAL(event, res);
414    ev = (Ecore_Event_Mouse_Wheel *)event;
415
416    if (!ev->dev) return ECORE_CALLBACK_PASS_ON;
417
418    if (!e_devicemgr_detent_is_detent(ecore_device_name_get(ev->dev)))
419      return ECORE_CALLBACK_PASS_ON;
420
421    detent = ev->z;
422    comp_conf = e_comp_config_get();
423    if (comp_conf && comp_conf->e_wheel_click_angle)
424      {
425         detent = (int)(detent / comp_conf->e_wheel_click_angle);
426      }
427
428    if (detent == 1 || detent == -1)
429      {
430         detent = detent * (-1);
431         if (!e_devicemgr_intercept_hook_call(E_DEVICEMGR_INTERCEPT_HOOK_DETENT, &detent))
432           {
433              DMDBG("Stop propagate detent event. value: %d\n", detent);
434              return ECORE_CALLBACK_DONE;
435           }
436         e_devicemgr_wl_detent_send_event(detent);
437      }
438
439    return ECORE_CALLBACK_DONE;
440 }
441
442
443 static Eina_Bool
444 _e_devicemgr_input_cb_device_add(void *data, int type, void *event)
445 {
446    Ecore_Event_Device_Info *ev;
447
448    EINA_SAFETY_ON_NULL_RETURN_VAL(event, ECORE_CALLBACK_PASS_ON);
449    ev = (Ecore_Event_Device_Info *)event;
450
451    _e_devicemgr_input_device_add(ev->name, ev->identifier, ev->seatname, ev->clas, ev->subclas);
452
453    return ECORE_CALLBACK_PASS_ON;
454 }
455
456 static Eina_Bool
457 _e_devicemgr_input_cb_device_del(void *data, int type, void *event)
458 {
459    Ecore_Event_Device_Info *ev;
460
461    EINA_SAFETY_ON_NULL_RETURN_VAL(event, ECORE_CALLBACK_PASS_ON);
462    ev = (Ecore_Event_Device_Info *)event;
463
464    _e_devicemgr_input_device_del(ev->name, ev->identifier, ev->seatname, ev->clas, ev->subclas);
465
466    return ECORE_CALLBACK_PASS_ON;
467 }
468
469 static Eina_Bool
470 _e_devicemgr_input_cb_device_update(void *data, int type, void *event)
471 {
472    Ecore_Event_Device_Update *ev;
473
474    EINA_SAFETY_ON_NULL_RETURN_VAL(event, ECORE_CALLBACK_PASS_ON);
475
476    ev = (Ecore_Event_Device_Update *)event;
477    EINA_SAFETY_ON_NULL_RETURN_VAL(ev->dev, ECORE_CALLBACK_PASS_ON);
478
479    _e_devicemgr_input_device_update(ev->dev);
480
481    return ECORE_CALLBACK_PASS_ON;
482 }
483
484 Eina_Bool
485 e_devicemgr_input_init(void)
486 {
487    e_devicemgr->virtual_mouse_device_fd = -1;
488    e_devicemgr->virtual_key_device_fd = -1;
489
490    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_MOUSE_BUTTON_DOWN, _e_devicemgr_input_cb_mouse_button_down, NULL);
491    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_MOUSE_BUTTON_UP, _e_devicemgr_input_cb_mouse_button_up, NULL);
492    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_MOUSE_WHEEL, _e_devicemgr_input_cb_mouse_wheel, NULL);
493    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_DEVICE_ADD, _e_devicemgr_input_cb_device_add, NULL);
494    E_LIST_HANDLER_APPEND(e_devicemgr->handlers, ECORE_EVENT_DEVICE_DEL, _e_devicemgr_input_cb_device_del, NULL);
495    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_DEVICE_SUBCLASS_UPDATE, _e_devicemgr_input_cb_device_update, NULL);
496
497    if (e_devicemgr->dconfig->conf->input.virtual_key_device_enable)
498      {
499         e_devicemgr->virtual_key_device_fd = e_devicemgr_create_virtual_device(ECORE_DEVICE_CLASS_KEYBOARD, "Virtual Key Device");
500
501         if (e_devicemgr->virtual_key_device_fd >= 0)
502           DMINF("input.virtual_key_device_enable: device fd : %d\n", e_devicemgr->virtual_key_device_fd);
503         else
504           DMWRN("input.virtual_key_device_enable: but failed to create device !\n");
505      }
506
507    if (e_devicemgr->dconfig->conf->input.virtual_mouse_device_enable)
508      {
509         e_devicemgr->virtual_mouse_device_fd = e_devicemgr_create_virtual_device(ECORE_DEVICE_CLASS_MOUSE, "Virtual Mouse Device");
510
511         if (e_devicemgr->virtual_mouse_device_fd >= 0)
512           DMINF("input.virtual_mouse_device_enable: device fd : %d\n", e_devicemgr->virtual_mouse_device_fd);
513         else
514           DMWRN("input.virtual_mouse_device_enable: but failed to create device !\n");
515      }
516
517    e_devicemgr->max_touch_count = 0;
518
519    return EINA_TRUE;
520 }
521
522 void
523 e_devicemgr_input_shutdown(void)
524 {
525    Ecore_Event_Handler *h = NULL;
526
527    EINA_LIST_FREE(e_devicemgr->handlers, h)
528      ecore_event_handler_del(h);
529
530    if (e_devicemgr->virtual_key_device_fd)
531      {
532         e_devicemgr_destroy_virtual_device(e_devicemgr->virtual_key_device_fd);
533         e_devicemgr->virtual_key_device_fd = -1;
534      }
535    if (e_devicemgr->virtual_mouse_device_fd)
536      {
537         e_devicemgr_destroy_virtual_device(e_devicemgr->virtual_mouse_device_fd);
538         e_devicemgr->virtual_mouse_device_fd = -1;
539      }
540
541    if (e_devicemgr->multi)
542      free(e_devicemgr->multi);
543 }