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"
8 e_devicemgr_strcmp(const char *dst, const char *src)
10 int dst_len, src_len, str_len;
12 dst_len = strlen(dst);
13 src_len = strlen(src);
15 if (src_len > dst_len) str_len = src_len;
16 else str_len = dst_len;
18 if (!strncmp(dst, src, str_len))
25 _e_devicemgr_input_pointer_warp(int x, int y)
27 e_input_device_pointer_warp(NULL, x, y);
28 DMWRN("The pointer warped to (%d, %d) !\n", x, y);
30 return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NONE;
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)
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;
41 if (!(ec = e_client_from_surface_resource(surface)) || !ec->visible)
43 DMERR("The given surface is invalid or invisible !\n");
44 return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_SURFACE;
47 if (ec != e_comp_wl->ptr.ec)
49 DMERR("Pointer is not on the given surface !\n");
50 return TIZEN_INPUT_DEVICE_MANAGER_ERROR_INVALID_SURFACE;
53 if ((!e_config->show_cursor) || (!e_comp_wl->ptr.enabled))
55 DMERR("Pointer is not available");
56 return TIZEN_INPUT_DEVICE_MANAGER_ERROR_NO_POINTER_AVAILABLE;
59 new_x = wl_fixed_to_int(x);
60 new_y = wl_fixed_to_int(y);
62 if (new_x < 0) new_x = 0;
63 if ((ec->w != 0) && (new_x > ec->w - 1)) new_x = ec->w - 1;
65 if (new_y < 0) new_y = 0;
66 if ((ec->h != 0) && (new_y > ec->h - 1)) new_y = ec->h - 1;
68 new_x = ec->client.x + new_x;
69 new_y = ec->client.y + new_y;
71 if (e_client_transform_core_enable_get(ec))
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);
77 map = e_client_map_get(ec);
78 e_map_point_coord_get(map, 0, &min_x, &min_y, NULL);
82 for (i = 1; i < e_map_count_get(map); i++)
84 e_map_point_coord_get(map, i, &tmp_x, &tmp_y, NULL);
86 if (tmp_x < min_x) min_x = tmp_x;
87 else if (tmp_x > max_x) max_x = tmp_x;
89 if (tmp_y < min_y) min_y = tmp_y;
90 else if (tmp_y > max_y) max_y = tmp_y;
95 if (map_x < min_x) map_x = min_x;
96 if (map_x > max_x - 1) map_x = max_x - 1;
100 if (map_y < min_y) map_y = min_y;
101 if (map_y > max_y - 1) map_y = max_y - 1;
107 ret = _e_devicemgr_input_pointer_warp(new_x, new_y);
112 e_devicemgr_detent_is_detent(const char *name)
114 EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE);
116 if (!strncmp(name, DETENT_DEVICE_NAME, sizeof(DETENT_DEVICE_NAME)))
123 _e_devicemgr_detent_set_info(E_Devicemgr_Input_Device *dev)
125 if (dev->name && !strncmp(dev->name, DETENT_DEVICE_NAME, sizeof(DETENT_DEVICE_NAME)))
127 E_FREE(e_devicemgr->detent.identifier);
128 if (dev->identifier) e_devicemgr->detent.identifier = strdup(dev->identifier);
133 _e_devicemgr_detent_unset_info(E_Devicemgr_Input_Device *dev)
135 if (dev->name && !strncmp(dev->name, DETENT_DEVICE_NAME, sizeof(DETENT_DEVICE_NAME)))
137 E_FREE(e_devicemgr->detent.identifier);
142 _e_devicemgr_input_keyevent_free(void *data EINA_UNUSED, void *ev)
144 Ecore_Event_Key *e = ev;
146 eina_stringshare_del(e->keyname);
147 eina_stringshare_del(e->key);
148 eina_stringshare_del(e->compose);
155 _e_devicemgr_input_mouse_button_remap(Ecore_Event_Mouse_Button *ev, Eina_Bool pressed)
157 Ecore_Event_Key *ev_key;
158 E_Keyrouter_Event_Data *key_data;
159 struct xkb_keymap *keymap = NULL;
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);
165 EINA_SAFETY_ON_NULL_RETURN_VAL(keymap, ECORE_CALLBACK_PASS_ON);
166 EINA_SAFETY_ON_NULL_RETURN_VAL(ev, ECORE_CALLBACK_PASS_ON);
168 if (ev->buttons != 3) return ECORE_CALLBACK_PASS_ON;
170 ev_key = E_NEW(Ecore_Event_Key, 1);
171 EINA_SAFETY_ON_NULL_RETURN_VAL(ev_key, ECORE_CALLBACK_PASS_ON);
173 key_data = E_NEW(E_Keyrouter_Event_Data, 1);
174 EINA_SAFETY_ON_NULL_GOTO(key_data, failed);
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;
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;
188 if (e_input_thread_mode_get())
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);
195 ecore_event_add(pressed ? ECORE_EVENT_KEY_DOWN : ECORE_EVENT_KEY_UP, ev_key, _e_devicemgr_input_keyevent_free, NULL);
198 return ECORE_CALLBACK_DONE;
202 return ECORE_CALLBACK_PASS_ON;
206 _e_devicemgr_input_device_add(const char *name, const char *identifier, const char *seatname, Ecore_Device_Class clas, Ecore_Device_Subclass subclas)
208 E_Devicemgr_Input_Device *dev = NULL;
210 int current_touch_count = -1;
212 EINA_SAFETY_ON_NULL_RETURN(name);
213 EINA_SAFETY_ON_NULL_RETURN(identifier);
214 EINA_SAFETY_ON_NULL_RETURN(e_devicemgr);
216 g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
217 EINA_LIST_FOREACH(e_devicemgr->device_list, l, dev)
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)))
225 g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
229 g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
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);
236 dev->subclas = subclas;
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);
242 if (dev->clas == ECORE_DEVICE_CLASS_MOUSE)
243 e_devicemgr->last_device_ptr = dev;
245 if (dev->clas == ECORE_DEVICE_CLASS_TOUCH)
247 if (!e_devicemgr->last_device_touch)
248 e_devicemgr->last_device_touch = dev;
250 current_touch_count = e_input_touch_max_count_get();
251 if (e_devicemgr->max_touch_count < current_touch_count)
253 e_devicemgr->max_touch_count = current_touch_count;
254 if (e_devicemgr->multi)
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);
260 e_devicemgr->multi = calloc(1, sizeof(E_Devicemgr_Input_Device_Multi)*current_touch_count);
262 if (!e_devicemgr->multi)
263 ELOGF("DEVMGR_TOUCH", "Failed to allocate memory for multi ptr. (finger=%d)\n", NULL, current_touch_count);
265 e_devicemgr_wl_touch_max_count_send(e_devicemgr->max_touch_count, NULL, NULL);
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);
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);
280 _e_devicemgr_input_device_del(const char *name, const char *identifier, const char *seatname, Ecore_Device_Class clas, Ecore_Device_Subclass subclas)
282 E_Devicemgr_Input_Device *dev = NULL;
283 E_Devicemgr_Input_Device *last_device_kbd = NULL;
286 EINA_SAFETY_ON_NULL_RETURN(name);
287 EINA_SAFETY_ON_NULL_RETURN(identifier);
289 g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
290 EINA_LIST_FOREACH(e_devicemgr->device_list, l, dev)
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)))
302 g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
306 _e_devicemgr_detent_unset_info(dev);
307 e_devicemgr_wl_device_del(dev);
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);
313 e_devicemgr->device_list = eina_list_remove(e_devicemgr->device_list, dev);
315 if (e_devicemgr->last_device_ptr == dev)
316 e_devicemgr->last_device_ptr = NULL;
318 if (e_devicemgr->last_device_touch == dev)
319 e_devicemgr->last_device_touch = NULL;
321 last_device_kbd = atomic_load(&e_devicemgr->last_device_kbd);
323 if (last_device_kbd == dev)
324 atomic_store(&e_devicemgr->last_device_kbd, NULL);
328 g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
332 _e_devicemgr_input_device_update(Ecore_Device *dev)
335 E_Devicemgr_Input_Device *data;
336 char *dev_identifier;
338 EINA_SAFETY_ON_NULL_RETURN(dev);
340 dev_identifier = (char *)ecore_device_identifier_get(dev);
341 EINA_SAFETY_ON_NULL_RETURN(dev_identifier);
343 g_rec_mutex_lock(&e_devicemgr->device_list_mutex);
344 EINA_LIST_FOREACH(e_devicemgr->device_list, l, data)
346 if (data->clas == ecore_device_class_get(dev) && data->identifier)
348 if (e_devicemgr_strcmp(dev_identifier, data->identifier))
350 data->subclas = ecore_device_subclass_get(dev);
352 e_devicemgr_wl_device_update(data);
353 g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
358 g_rec_mutex_unlock(&e_devicemgr->device_list_mutex);
362 _e_devicemgr_input_cb_mouse_button_down(void *data, int type, void *event)
364 Ecore_Event_Mouse_Button *ev;
365 Eina_Bool res = ECORE_CALLBACK_PASS_ON;
366 E_Comp_Config *comp_conf = NULL;
368 EINA_SAFETY_ON_NULL_RETURN_VAL(event, res);
369 ev = (Ecore_Event_Mouse_Button *)event;
371 if (e_devicemgr->dconfig->conf->input.button_remap_enable)
372 res = _e_devicemgr_input_mouse_button_remap(ev, EINA_TRUE);
374 comp_conf = e_comp_config_get();
375 if (comp_conf && comp_conf->input_log_enable)
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);
385 _e_devicemgr_input_cb_mouse_button_up(void *data, int type, void *event)
387 Ecore_Event_Mouse_Button *ev;
388 Eina_Bool res = ECORE_CALLBACK_PASS_ON;
389 E_Comp_Config *comp_conf = NULL;
391 EINA_SAFETY_ON_NULL_RETURN_VAL(event, res);
392 ev = (Ecore_Event_Mouse_Button *)event;
394 if (e_devicemgr->dconfig->conf->input.button_remap_enable)
395 res = _e_devicemgr_input_mouse_button_remap(ev, EINA_FALSE);
397 comp_conf = e_comp_config_get();
398 if (comp_conf && comp_conf->input_log_enable)
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);
408 _e_devicemgr_input_cb_mouse_wheel(void *data, int type, void *event)
410 Ecore_Event_Mouse_Wheel *ev;
412 Eina_Bool res = ECORE_CALLBACK_PASS_ON;
413 E_Comp_Config *comp_conf;
415 EINA_SAFETY_ON_NULL_RETURN_VAL(event, res);
416 ev = (Ecore_Event_Mouse_Wheel *)event;
418 if (!ev->dev) return ECORE_CALLBACK_PASS_ON;
420 if (!e_devicemgr_detent_is_detent(ecore_device_name_get(ev->dev)))
421 return ECORE_CALLBACK_PASS_ON;
424 comp_conf = e_comp_config_get();
425 if (comp_conf && comp_conf->e_wheel_click_angle)
427 detent = (int)(detent / comp_conf->e_wheel_click_angle);
430 if (detent == 1 || detent == -1)
432 detent = detent * (-1);
433 if (!e_devicemgr_intercept_hook_call(E_DEVICEMGR_INTERCEPT_HOOK_DETENT, &detent))
435 DMDBG("Stop propagate detent event. value: %d\n", detent);
436 return ECORE_CALLBACK_DONE;
438 e_devicemgr_wl_detent_send_event(detent);
441 return ECORE_CALLBACK_DONE;
446 _e_devicemgr_input_cb_device_add(void *data, int type, void *event)
448 Ecore_Event_Device_Info *ev;
450 EINA_SAFETY_ON_NULL_RETURN_VAL(event, ECORE_CALLBACK_PASS_ON);
451 ev = (Ecore_Event_Device_Info *)event;
453 _e_devicemgr_input_device_add(ev->name, ev->identifier, ev->seatname, ev->clas, ev->subclas);
455 return ECORE_CALLBACK_PASS_ON;
459 _e_devicemgr_input_cb_device_del(void *data, int type, void *event)
461 Ecore_Event_Device_Info *ev;
463 EINA_SAFETY_ON_NULL_RETURN_VAL(event, ECORE_CALLBACK_PASS_ON);
464 ev = (Ecore_Event_Device_Info *)event;
466 _e_devicemgr_input_device_del(ev->name, ev->identifier, ev->seatname, ev->clas, ev->subclas);
468 return ECORE_CALLBACK_PASS_ON;
472 _e_devicemgr_input_cb_device_update(void *data, int type, void *event)
474 Ecore_Event_Device_Update *ev;
476 EINA_SAFETY_ON_NULL_RETURN_VAL(event, ECORE_CALLBACK_PASS_ON);
478 ev = (Ecore_Event_Device_Update *)event;
479 EINA_SAFETY_ON_NULL_RETURN_VAL(ev->dev, ECORE_CALLBACK_PASS_ON);
481 _e_devicemgr_input_device_update(ev->dev);
483 return ECORE_CALLBACK_PASS_ON;
487 e_devicemgr_input_init(void)
489 e_devicemgr->virtual_mouse_device_fd = -1;
490 e_devicemgr->virtual_key_device_fd = -1;
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);
499 if (e_devicemgr->dconfig->conf->input.virtual_key_device_enable)
501 e_devicemgr->virtual_key_device_fd = e_devicemgr_create_virtual_device(ECORE_DEVICE_CLASS_KEYBOARD, "Virtual Key Device");
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);
506 DMWRN("input.virtual_key_device_enable: but failed to create device !\n");
509 if (e_devicemgr->dconfig->conf->input.virtual_mouse_device_enable)
511 e_devicemgr->virtual_mouse_device_fd = e_devicemgr_create_virtual_device(ECORE_DEVICE_CLASS_MOUSE, "Virtual Mouse Device");
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);
516 DMWRN("input.virtual_mouse_device_enable: but failed to create device !\n");
519 e_devicemgr->max_touch_count = 0;
525 e_devicemgr_input_shutdown(void)
527 Ecore_Event_Handler *h = NULL;
529 EINA_LIST_FREE(e_devicemgr->handlers, h)
530 ecore_event_handler_del(h);
532 if (e_devicemgr->virtual_key_device_fd)
534 e_devicemgr_destroy_virtual_device(e_devicemgr->virtual_key_device_fd);
535 e_devicemgr->virtual_key_device_fd = -1;
537 if (e_devicemgr->virtual_mouse_device_fd)
539 e_devicemgr_destroy_virtual_device(e_devicemgr->virtual_mouse_device_fd);
540 e_devicemgr->virtual_mouse_device_fd = -1;
543 if (e_devicemgr->multi)
544 free(e_devicemgr->multi);