Enable default single finger 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 Eina_Bool
88 e_gesture_device_add(Ecore_Event_Device_Info *ev)
89 {
90    if (ev->clas == ECORE_DEVICE_CLASS_TOUCH)
91      {
92         gesture->device.touch_devices = eina_list_append(gesture->device.touch_devices, ev->identifier);
93         GTINF("%s(%s) device is touch device: add list\n", ev->name, ev->identifier);
94      }
95    if ((!gesture->device.kbd_identifier) &&
96        (ev->clas == ECORE_DEVICE_CLASS_KEYBOARD))
97      {
98         if (gesture->device.kbd_name)
99           {
100              if (!strncmp(ev->name, gesture->device.kbd_name, strlen(gesture->device.kbd_name)))
101                {
102                   GTINF("%s(%s) device is key generated device in gesture\n", ev->name, ev->identifier);
103                   gesture->device.kbd_identifier = strdup(ev->identifier);
104                   gesture->device.kbd_device = _e_gesture_device_ecore_device_get(gesture->device.kbd_identifier, ECORE_DEVICE_CLASS_KEYBOARD);
105                }
106           }
107         else
108           {
109              GTINF("%s(%s) device is key generated device in gesture\n", ev->name, ev->identifier);
110              gesture->device.kbd_name = strdup(ev->name);
111              gesture->device.kbd_identifier = strdup(ev->identifier);
112              gesture->device.kbd_device = _e_gesture_device_ecore_device_get(gesture->device.kbd_identifier, ECORE_DEVICE_CLASS_KEYBOARD);
113           }
114      }
115    return EINA_TRUE;
116 }
117
118 Eina_Bool
119 e_gesture_device_del(Ecore_Event_Device_Info *ev)
120 {
121    Eina_List *l, *l_next;
122    char *data;
123
124    if (ev->clas == ECORE_DEVICE_CLASS_TOUCH)
125      {
126         EINA_LIST_FOREACH_SAFE(gesture->device.touch_devices, l, l_next, data)
127           {
128              if (!strncmp(data, ev->identifier, strlen(ev->identifier)))
129                {
130                   GTINF("%s(%s) device is touch device: remove list\n", ev->name, ev->identifier);
131                   gesture->device.touch_devices = eina_list_remove(gesture->device.touch_devices, data);
132                   E_FREE(data);
133                }
134           }
135      }
136    if ((gesture->device.kbd_identifier) &&
137        (ev->clas == ECORE_DEVICE_CLASS_KEYBOARD))
138      {
139         if (!strncmp(ev->name, gesture->device.kbd_name, strlen(gesture->device.kbd_name)))
140           {
141              GTWRN("Gesture keyboard device(%s) is disconnected. Gesture cannot create key events\n", gesture->device.kbd_name);
142              E_FREE(gesture->device.kbd_identifier);
143              E_FREE(gesture->device.kbd_name);
144           }
145      }
146    return EINA_TRUE;
147 }
148
149 Eina_Bool
150 e_gesture_is_touch_device(const Ecore_Device *dev)
151 {
152    if (ecore_device_class_get(dev) == ECORE_DEVICE_CLASS_TOUCH)
153      return EINA_TRUE;
154
155    return EINA_FALSE;
156 }
157
158 void
159 e_gesture_device_shutdown(void)
160 {
161    E_FREE(gesture->device.kbd_identifier);
162    E_FREE(gesture->device.kbd_name);
163    gesture->device.touch_devices = eina_list_free(gesture->device.touch_devices);
164 }