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