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