e_devicemgr: handle init_generator_with_sync request
[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_inputgen_device_ready_send(dev);
246    e_devicemgr_wl_device_add(dev);
247    e_devicemgr_inputgen_get_device_info(dev);
248    _e_devicemgr_detent_set_info(dev);
249 }
250
251 static void
252 _e_devicemgr_input_device_del(const char *name, const char *identifier, const char *seatname, Ecore_Device_Class clas, Ecore_Device_Subclass subclas)
253 {
254    E_Devicemgr_Input_Device *dev = NULL;
255    Eina_List *l;
256
257    EINA_SAFETY_ON_NULL_RETURN(name);
258    EINA_SAFETY_ON_NULL_RETURN(identifier);
259
260    EINA_LIST_FOREACH(e_devicemgr->device_list, l, dev)
261      {
262         if (!dev->name) continue;
263         if (!dev->identifier) continue;
264         if ((dev->clas == clas) && (dev->subclas == subclas) &&
265             (!strcmp(dev->name, name)) &&
266             (!strcmp(dev->identifier, identifier)))
267           break;
268      }
269    if (!dev)
270      {
271         return;
272      }
273
274    _e_devicemgr_detent_unset_info(dev);
275    e_devicemgr_wl_device_del(dev);
276
277    if (dev->name) eina_stringshare_del(dev->name);
278    if (dev->identifier) eina_stringshare_del(dev->identifier);
279    if (dev->seat_name) eina_stringshare_del(dev->seat_name);
280
281    e_devicemgr->device_list = eina_list_remove(e_devicemgr->device_list, dev);
282
283    if (e_devicemgr->last_device_ptr == dev)
284      e_devicemgr->last_device_ptr = NULL;
285
286    if (e_devicemgr->last_device_touch == dev)
287      e_devicemgr->last_device_touch = NULL;
288
289    if (e_devicemgr->last_device_kbd == dev)
290      e_devicemgr->last_device_kbd = NULL;
291
292    E_FREE(dev);
293 }
294
295 static void
296 _e_devicemgr_input_device_update(Ecore_Device *dev)
297 {
298    Eina_List *l;
299    E_Devicemgr_Input_Device *data;
300    char *dev_identifier;
301
302    EINA_SAFETY_ON_NULL_RETURN(dev);
303
304    dev_identifier = (char *)ecore_device_identifier_get(dev);
305    EINA_SAFETY_ON_NULL_RETURN(dev_identifier);
306
307    EINA_LIST_FOREACH(e_devicemgr->device_list, l, data)
308      {
309         if (data->clas == ecore_device_class_get(dev) && data->identifier)
310           {
311              if (e_devicemgr_strcmp(dev_identifier, data->identifier))
312                {
313                   data->subclas = ecore_device_subclass_get(dev);
314
315                   e_devicemgr_wl_device_update(data);
316                   return;
317                }
318           }
319      }
320 }
321
322 static Eina_Bool
323 _e_devicemgr_input_cb_mouse_button_down(void *data, int type, void *event)
324 {
325    Ecore_Event_Mouse_Button *ev;
326    Eina_Bool res = ECORE_CALLBACK_PASS_ON;
327    E_Comp_Config *comp_conf = NULL;
328
329    EINA_SAFETY_ON_NULL_RETURN_VAL(event, res);
330    ev = (Ecore_Event_Mouse_Button *)event;
331
332    if (e_devicemgr->dconfig->conf->input.button_remap_enable)
333      res = _e_devicemgr_input_mouse_button_remap(ev, EINA_TRUE);
334
335    comp_conf = e_comp_config_get();
336    if (comp_conf && comp_conf->input_log_enable)
337      {
338         ELOGF("DEVMGR", "Mouse Down (id: %d, button: %d, x: %d, y: %d), res: %d", NULL,
339               ev->multi.device, ev->buttons, ev->x, ev->y, res);
340      }
341
342    return res;
343 }
344
345 static Eina_Bool
346 _e_devicemgr_input_cb_mouse_button_up(void *data, int type, void *event)
347 {
348    Ecore_Event_Mouse_Button *ev;
349    Eina_Bool res = ECORE_CALLBACK_PASS_ON;
350    E_Comp_Config *comp_conf = NULL;
351
352    EINA_SAFETY_ON_NULL_RETURN_VAL(event, res);
353    ev = (Ecore_Event_Mouse_Button *)event;
354
355    if (e_devicemgr->dconfig->conf->input.button_remap_enable)
356      res = _e_devicemgr_input_mouse_button_remap(ev, EINA_FALSE);
357
358    comp_conf = e_comp_config_get();
359    if (comp_conf && comp_conf->input_log_enable)
360      {
361         ELOGF("DEVMGR", "Mouse Up (id: %d, button: %d, x: %d, y: %d), res: %d", NULL,
362               ev->multi.device, ev->buttons, ev->x, ev->y, res);
363      }
364
365    return res;
366 }
367
368 static Eina_Bool
369 _e_devicemgr_input_cb_mouse_wheel(void *data, int type, void *event)
370 {
371    Ecore_Event_Mouse_Wheel *ev;
372    int detent;
373    Eina_Bool res = ECORE_CALLBACK_PASS_ON;
374    E_Comp_Config *comp_conf;
375
376    EINA_SAFETY_ON_NULL_RETURN_VAL(event, res);
377    ev = (Ecore_Event_Mouse_Wheel *)event;
378
379    if (!ev->dev) return ECORE_CALLBACK_PASS_ON;
380
381    if (!e_devicemgr_detent_is_detent(ecore_device_name_get(ev->dev)))
382      return ECORE_CALLBACK_PASS_ON;
383
384    detent = ev->z;
385    comp_conf = e_comp_config_get();
386    if (comp_conf && comp_conf->e_wheel_click_angle)
387      {
388         detent = (int)(detent / comp_conf->e_wheel_click_angle);
389      }
390
391    if (detent == 1 || detent == -1)
392      {
393         detent = detent * (-1);
394         if (!e_devicemgr_intercept_hook_call(E_DEVICEMGR_INTERCEPT_HOOK_DETENT, &detent))
395           {
396              DMDBG("Stop propagate detent event. value: %d\n", detent);
397              return ECORE_CALLBACK_DONE;
398           }
399         e_devicemgr_wl_detent_send_event(detent);
400      }
401
402    return ECORE_CALLBACK_DONE;
403 }
404
405
406 static Eina_Bool
407 _e_devicemgr_input_cb_device_add(void *data, int type, void *event)
408 {
409    Ecore_Event_Device_Info *ev;
410
411    EINA_SAFETY_ON_NULL_RETURN_VAL(event, ECORE_CALLBACK_PASS_ON);
412    ev = (Ecore_Event_Device_Info *)event;
413
414    _e_devicemgr_input_device_add(ev->name, ev->identifier, ev->seatname, ev->clas, ev->subclas);
415
416    return ECORE_CALLBACK_PASS_ON;
417 }
418
419 static Eina_Bool
420 _e_devicemgr_input_cb_device_del(void *data, int type, void *event)
421 {
422    Ecore_Event_Device_Info *ev;
423
424    EINA_SAFETY_ON_NULL_RETURN_VAL(event, ECORE_CALLBACK_PASS_ON);
425    ev = (Ecore_Event_Device_Info *)event;
426
427    _e_devicemgr_input_device_del(ev->name, ev->identifier, ev->seatname, ev->clas, ev->subclas);
428
429    return ECORE_CALLBACK_PASS_ON;
430 }
431
432 static Eina_Bool
433 _e_devicemgr_input_cb_device_update(void *data, int type, void *event)
434 {
435    Ecore_Event_Device_Update *ev;
436
437    EINA_SAFETY_ON_NULL_RETURN_VAL(event, ECORE_CALLBACK_PASS_ON);
438
439    ev = (Ecore_Event_Device_Update *)event;
440    EINA_SAFETY_ON_NULL_RETURN_VAL(ev->dev, ECORE_CALLBACK_PASS_ON);
441
442    _e_devicemgr_input_device_update(ev->dev);
443
444    return ECORE_CALLBACK_PASS_ON;
445 }
446
447 Eina_Bool
448 e_devicemgr_input_init(void)
449 {
450    e_devicemgr->virtual_mouse_device_fd = -1;
451    e_devicemgr->virtual_key_device_fd = -1;
452
453    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_MOUSE_BUTTON_DOWN, _e_devicemgr_input_cb_mouse_button_down, NULL);
454    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_MOUSE_BUTTON_UP, _e_devicemgr_input_cb_mouse_button_up, NULL);
455    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_MOUSE_WHEEL, _e_devicemgr_input_cb_mouse_wheel, NULL);
456    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_DEVICE_ADD, _e_devicemgr_input_cb_device_add, NULL);
457    E_LIST_HANDLER_APPEND(e_devicemgr->handlers, ECORE_EVENT_DEVICE_DEL, _e_devicemgr_input_cb_device_del, NULL);
458    E_LIST_HANDLER_PREPEND(e_devicemgr->handlers, ECORE_EVENT_DEVICE_SUBCLASS_UPDATE, _e_devicemgr_input_cb_device_update, NULL);
459
460    if (e_devicemgr->dconfig->conf->input.virtual_key_device_enable)
461      {
462         e_devicemgr->virtual_key_device_fd = e_devicemgr_create_virtual_device(ECORE_DEVICE_CLASS_KEYBOARD, "Virtual Key Device");
463
464         if (e_devicemgr->virtual_key_device_fd >= 0)
465           DMINF("input.virtual_key_device_enable: device fd : %d\n", e_devicemgr->virtual_key_device_fd);
466         else
467           DMWRN("input.virtual_key_device_enable: but failed to create device !\n");
468      }
469
470    if (e_devicemgr->dconfig->conf->input.virtual_mouse_device_enable)
471      {
472         e_devicemgr->virtual_mouse_device_fd = e_devicemgr_create_virtual_device(ECORE_DEVICE_CLASS_MOUSE, "Virtual Mouse Device");
473
474         if (e_devicemgr->virtual_mouse_device_fd >= 0)
475           DMINF("input.virtual_mouse_device_enable: device fd : %d\n", e_devicemgr->virtual_mouse_device_fd);
476         else
477           DMWRN("input.virtual_mouse_device_enable: but failed to create device !\n");
478      }
479
480    e_devicemgr->max_touch_count = 0;
481
482    return EINA_TRUE;
483 }
484
485 void
486 e_devicemgr_input_shutdown(void)
487 {
488    Ecore_Event_Handler *h = NULL;
489
490    EINA_LIST_FREE(e_devicemgr->handlers, h)
491      ecore_event_handler_del(h);
492
493    if (e_devicemgr->virtual_key_device_fd)
494      {
495         e_devicemgr_destroy_virtual_device(e_devicemgr->virtual_key_device_fd);
496         e_devicemgr->virtual_key_device_fd = -1;
497      }
498    if (e_devicemgr->virtual_mouse_device_fd)
499      {
500         e_devicemgr_destroy_virtual_device(e_devicemgr->virtual_mouse_device_fd);
501         e_devicemgr->virtual_mouse_device_fd = -1;
502      }
503
504    if (e_devicemgr->multi)
505      free(e_devicemgr->multi);
506 }