2 * Copyright (C) 2010-2012 Intel Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <linux/input.h>
19 #include <cutils/log.h>
21 #include "SensorInputDev.h"
23 SensorInputDev::SensorInputDev(std::string dev)
27 findInputDev(dev, mDevPath);
30 int SensorInputDev::readEvents(sensors_event_t* data, int count)
35 if (mHasPendingEvent) {
36 mHasPendingEvent = false;
37 mPendingEvent.timestamp = getTimestamp();
38 *data = mPendingEvent;
45 ssize_t n = mInputReader.fill(mFd);
49 int numEventReceived = 0;
50 input_event const* event;
52 #if FETCH_FULL_EVENT_BEFORE_RETURN
55 while (count && mInputReader.readEvent(&event)) {
56 /* do sensor specific stuff */
59 if (event->type == EV_SYN) {
60 mPendingEvent.timestamp = timevalToNano(event->time);
62 *data++ = mPendingEvent;
66 ALOGV("%s:%s:, in type = EV_SYN, after, count = %d, numEventReceived = %d, mEnabled = %d",
67 __func__, mDevPath.c_str(), count, numEventReceived, mEnabled);
73 #if FETCH_FULL_EVENT_BEFORE_RETURN
74 /* if we didn't read a complete event, see if we can fill and
75 try again instead of returning with nothing and redoing poll. */
76 if (numEventReceived == 0 && mEnabled == 1) {
77 n = mInputReader.fill(mFd);
83 return numEventReceived;
86 bool SensorInputDev::findInputDev(const std::string &inputName,
87 std::string &foundPath)
90 std::string dev = "/dev/input";
91 DIR *dir = opendir(dev.c_str());
96 while ((de = readdir(dir))) {
97 if(de->d_name[0] == '.' &&
98 (de->d_name[1] == '\0' ||
99 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
102 int fd = ::open((dev + "/" + de->d_name).c_str(), O_RDONLY);
106 ALOGV("%s: probing path %s against %s", __func__, (dev + "/" + de->d_name).c_str(), inputName.c_str());
108 if (ioctl(fd, EVIOCGNAME(sizeof(readName) - 1), &readName) < 1)
113 if (inputName.compare(readName) == 0) {
114 foundPath = (dev + "/" + de->d_name);
116 ALOGV("%s: found %s", __func__, foundPath.c_str());
123 ALOGE_IF(!found, "couldn't find '%s' input device", inputName.c_str());