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