Check grabbed edge when process a edge_swipe gesture
[platform/core/uifw/e-mod-tizen-gesture.git] / src / e_mod_gesture_device.c
1 #define E_COMP_WL
2 #include "e_mod_main.h"
3 #include <string.h>
4 #include <linux/uinput.h>
5
6 static void
7 _e_gesture_device_keydev_create(void)
8 {
9    int uinp_fd = -1;
10    struct uinput_user_dev uinp;
11    int ret = 0;
12
13    uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY);
14    if ( uinp_fd < 0)
15      {
16         GTWRN("Failed to open /dev/uinput: (%d)\n", uinp_fd);
17         return;
18      }
19
20    memset(&uinp, 0, sizeof(struct uinput_user_dev));
21    strncpy(uinp.name, E_GESTURE_KEYBOARD_NAME, UINPUT_MAX_NAME_SIZE);
22    uinp.id.version = 4;
23    uinp.id.bustype = BUS_USB;
24
25    ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
26    ioctl(uinp_fd, UI_SET_EVBIT, EV_SYN);
27    ioctl(uinp_fd, UI_SET_EVBIT, EV_MSC);
28
29    ioctl(uinp_fd, UI_SET_KEYBIT, KEY_BACK);
30
31    ret = write(uinp_fd, &uinp, sizeof(struct uinput_user_dev));
32    if (ret < 0)
33      {
34         GTWRN("Failed to write UINPUT device\n");
35         close(uinp_fd);
36         return;
37      }
38    if (ioctl(uinp_fd, UI_DEV_CREATE))
39      {
40         GTWRN("Unable to create UINPUT device\n");
41         close(uinp_fd);
42         return;
43      }
44
45    gesture->device.uinp_fd = uinp_fd;
46 }
47
48 void
49 e_gesture_device_keydev_set(char *option)
50 {
51    if (!option)
52      {
53         _e_gesture_device_keydev_create();
54         gesture->device.kbd_name = strdup(E_GESTURE_KEYBOARD_NAME);
55      }
56    else if (strncmp(option, "Any", sizeof("Any")))
57      {
58         gesture->device.kbd_name = strdup(option);
59      }
60 }
61
62 Ecore_Device *
63 _e_gesture_device_ecore_device_get(char *path, unsigned int clas)
64 {
65    const Eina_List *dev_list = NULL;
66    const Eina_List *l;
67    Ecore_Device *dev = NULL;
68    const char *identifier;
69
70    if (!path) return NULL;
71
72    dev_list = ecore_device_list();
73    if (!dev_list) return NULL;
74    EINA_LIST_FOREACH(dev_list, l, dev)
75      {
76         if (!dev) continue;
77         GTINF("dev: %s\n", ecore_device_name_get(dev));
78         identifier = ecore_device_identifier_get(dev);
79         if (!identifier) continue;
80         if ((ecore_device_class_get(dev) == clas) && !(strcmp(identifier, path)))
81           return dev;
82      }
83
84    return NULL;
85 }
86
87 E_Gesture_Event_State
88 e_gesture_device_add(Ecore_Event_Device_Info *ev)
89 {
90    if (ev->clas == ECORE_DEVICE_CLASS_TOUCH)
91      {
92         char *id;
93         id = strdup(ev->identifier);
94         gesture->device.touch_devices = eina_list_append(gesture->device.touch_devices, id);
95         GTINF("%s(%s) device is touch device: add list\n", ev->name, ev->identifier);
96      }
97    if ((!gesture->device.kbd_identifier) &&
98        (ev->clas == ECORE_DEVICE_CLASS_KEYBOARD))
99      {
100         if (gesture->device.kbd_name)
101           {
102              if (!strncmp(ev->name, gesture->device.kbd_name, strlen(gesture->device.kbd_name)))
103                {
104                   GTINF("%s(%s) device is key generated device in gesture\n", ev->name, ev->identifier);
105                   gesture->device.kbd_identifier = strdup(ev->identifier);
106                   gesture->device.kbd_device = _e_gesture_device_ecore_device_get(gesture->device.kbd_identifier, ECORE_DEVICE_CLASS_KEYBOARD);
107                }
108           }
109         else
110           {
111              GTINF("%s(%s) device is key generated device in gesture\n", ev->name, ev->identifier);
112              gesture->device.kbd_name = strdup(ev->name);
113              gesture->device.kbd_identifier = strdup(ev->identifier);
114              gesture->device.kbd_device = _e_gesture_device_ecore_device_get(gesture->device.kbd_identifier, ECORE_DEVICE_CLASS_KEYBOARD);
115           }
116      }
117    return E_GESTURE_EVENT_STATE_PROPAGATE;
118 }
119
120 E_Gesture_Event_State
121 e_gesture_device_del(Ecore_Event_Device_Info *ev)
122 {
123    Eina_List *l, *l_next;
124    char *data;
125
126    if (ev->clas == ECORE_DEVICE_CLASS_TOUCH)
127      {
128         EINA_LIST_FOREACH_SAFE(gesture->device.touch_devices, l, l_next, data)
129           {
130              if (!strncmp(data, ev->identifier, strlen(ev->identifier)))
131                {
132                   GTINF("%s(%s) device is touch device: remove list\n", ev->name, ev->identifier);
133                   gesture->device.touch_devices = eina_list_remove(gesture->device.touch_devices, data);
134                   E_FREE(data);
135                }
136           }
137      }
138    if ((gesture->device.kbd_identifier) &&
139        (ev->clas == ECORE_DEVICE_CLASS_KEYBOARD))
140      {
141         if (!strncmp(ev->name, gesture->device.kbd_name, strlen(gesture->device.kbd_name)))
142           {
143              GTWRN("Gesture keyboard device(%s) is disconnected. Gesture cannot create key events\n", gesture->device.kbd_name);
144              E_FREE(gesture->device.kbd_identifier);
145              E_FREE(gesture->device.kbd_name);
146           }
147      }
148    return E_GESTURE_EVENT_STATE_PROPAGATE;
149 }
150
151 Eina_Bool
152 e_gesture_is_touch_device(const Ecore_Device *dev)
153 {
154    if (ecore_device_class_get(dev) == ECORE_DEVICE_CLASS_TOUCH)
155      return EINA_TRUE;
156
157    return EINA_FALSE;
158 }
159
160 void
161 e_gesture_device_shutdown(void)
162 {
163    E_FREE(gesture->device.kbd_identifier);
164    E_FREE(gesture->device.kbd_name);
165    gesture->device.touch_devices = eina_list_free(gesture->device.touch_devices);
166 }