blob: e2ab44d2d047149a3d5b0d520ad0ba7acb68b8b7 [file] [log] [blame]
Prashant Malani9020e0f2016-03-11 11:57:10 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prashant Malani9020e0f2016-03-11 11:57:10 -080017#include <dirent.h>
18#include <fcntl.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <errno.h>
23#include <string.h>
24
Tianjie Xuc21edd42016-08-05 18:00:04 -070025#include <android-base/logging.h>
Prashant Malani9020e0f2016-03-11 11:57:10 -080026#include <linux/input.h>
27
Tianjie Xuc21edd42016-08-05 18:00:04 -070028#include "wear_touch.h"
29
Prashant Malani9020e0f2016-03-11 11:57:10 -080030#define DEVICE_PATH "/dev/input"
31
32WearSwipeDetector::WearSwipeDetector(int low, int high, OnSwipeCallback callback, void* cookie):
33 mLowThreshold(low),
34 mHighThreshold(high),
35 mCallback(callback),
36 mCookie(cookie),
37 mCurrentSlot(-1) {
38 pthread_create(&mThread, NULL, touch_thread, this);
39}
40
41WearSwipeDetector::~WearSwipeDetector() {
42}
43
44void WearSwipeDetector::detect(int dx, int dy) {
45 enum SwipeDirection direction;
46
47 if (abs(dy) < mLowThreshold && abs(dx) > mHighThreshold) {
48 direction = dx < 0 ? LEFT : RIGHT;
49 } else if (abs(dx) < mLowThreshold && abs(dy) > mHighThreshold) {
50 direction = dy < 0 ? UP : DOWN;
51 } else {
Tianjie Xuc21edd42016-08-05 18:00:04 -070052 LOG(DEBUG) << "Ignore " << dx << " " << dy;
Prashant Malani9020e0f2016-03-11 11:57:10 -080053 return;
54 }
55
Tianjie Xuc21edd42016-08-05 18:00:04 -070056 LOG(DEBUG) << "Swipe direction=" << direction;
Prashant Malani9020e0f2016-03-11 11:57:10 -080057 mCallback(mCookie, direction);
58}
59
60void WearSwipeDetector::process(struct input_event *event) {
61 if (mCurrentSlot < 0) {
62 mCallback(mCookie, UP);
63 mCurrentSlot = 0;
64 }
65
66 if (event->type == EV_ABS) {
67 if (event->code == ABS_MT_SLOT)
68 mCurrentSlot = event->value;
69
70 // Ignore other fingers
71 if (mCurrentSlot > 0) {
72 return;
73 }
74
75 switch (event->code) {
76 case ABS_MT_POSITION_X:
77 mX = event->value;
78 mFingerDown = true;
79 break;
80
81 case ABS_MT_POSITION_Y:
82 mY = event->value;
83 mFingerDown = true;
84 break;
85
86 case ABS_MT_TRACKING_ID:
87 if (event->value < 0)
88 mFingerDown = false;
89 break;
90 }
91 } else if (event->type == EV_SYN) {
92 if (event->code == SYN_REPORT) {
93 if (mFingerDown && !mSwiping) {
94 mStartX = mX;
95 mStartY = mY;
96 mSwiping = true;
97 } else if (!mFingerDown && mSwiping) {
98 mSwiping = false;
99 detect(mX - mStartX, mY - mStartY);
100 }
101 }
102 }
103}
104
105void WearSwipeDetector::run() {
106 int fd = findDevice(DEVICE_PATH);
107 if (fd < 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700108 LOG(ERROR) << "no input devices found";
Prashant Malani9020e0f2016-03-11 11:57:10 -0800109 return;
110 }
111
112 struct input_event event;
113 while (read(fd, &event, sizeof(event)) == sizeof(event)) {
114 process(&event);
115 }
116
117 close(fd);
118}
119
120void* WearSwipeDetector::touch_thread(void* cookie) {
Mikhail Lappo20791bd2017-03-23 21:30:36 +0100121 (static_cast<WearSwipeDetector*>(cookie))->run();
Prashant Malani9020e0f2016-03-11 11:57:10 -0800122 return NULL;
123}
124
Chih-Hung Hsieh8b238112016-08-26 14:54:29 -0700125#define test_bit(bit, array) ((array)[(bit)/8] & (1<<((bit)%8)))
Prashant Malani9020e0f2016-03-11 11:57:10 -0800126
127int WearSwipeDetector::openDevice(const char *device) {
128 int fd = open(device, O_RDONLY);
129 if (fd < 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700130 PLOG(ERROR) << "could not open " << device;
Prashant Malani9020e0f2016-03-11 11:57:10 -0800131 return false;
132 }
133
134 char name[80];
135 name[sizeof(name) - 1] = '\0';
136 if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700137 PLOG(ERROR) << "could not get device name for " << device;
Prashant Malani9020e0f2016-03-11 11:57:10 -0800138 name[0] = '\0';
139 }
140
141 uint8_t bits[512];
142 memset(bits, 0, sizeof(bits));
143 int ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(bits)), bits);
144 if (ret > 0) {
145 if (test_bit(ABS_MT_POSITION_X, bits) && test_bit(ABS_MT_POSITION_Y, bits)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700146 LOG(DEBUG) << "Found " << device << " " << name;
Prashant Malani9020e0f2016-03-11 11:57:10 -0800147 return fd;
148 }
149 }
150
151 close(fd);
152 return -1;
153}
154
155int WearSwipeDetector::findDevice(const char* path) {
156 DIR* dir = opendir(path);
157 if (dir == NULL) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700158 PLOG(ERROR) << "Could not open directory " << path;
Prashant Malani9020e0f2016-03-11 11:57:10 -0800159 return false;
160 }
161
162 struct dirent* entry;
163 int ret = -1;
164 while (ret < 0 && (entry = readdir(dir)) != NULL) {
165 if (entry->d_name[0] == '.') continue;
166
167 char device[PATH_MAX];
168 device[PATH_MAX-1] = '\0';
169 snprintf(device, PATH_MAX-1, "%s/%s", path, entry->d_name);
170
171 ret = openDevice(device);
172 }
173
174 closedir(dir);
175 return ret;
176}
177