1 /*
2 * Copyright (C) 2012 Invensense, Inc.
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
17 #define LOG_NDEBUG 0
18
19 #include <stdint.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <poll.h>
24
25 #include <sys/cdefs.h>
26 #include <sys/types.h>
27
28 #include <linux/input.h>
29
30 #include <cutils/log.h>
31
32 #include "InputEventReader.h"
33
34 /*****************************************************************************/
35
36 struct input_event;
37
InputEventCircularReader(size_t numEvents)38 InputEventCircularReader::InputEventCircularReader(size_t numEvents)
39 : mBuffer(new input_event[numEvents * 2]),
40 mBufferEnd(mBuffer + numEvents),
41 mHead(mBuffer),
42 mCurr(mBuffer),
43 mFreeSpace(numEvents)
44 {
45 mLastFd = -1;
46 }
47
~InputEventCircularReader()48 InputEventCircularReader::~InputEventCircularReader()
49 {
50 delete [] mBuffer;
51 }
52
53 #define INPUT_EVENT_DEBUG (0)
fill(int fd)54 ssize_t InputEventCircularReader::fill(int fd)
55 {
56 size_t numEventsRead = 0;
57 mLastFd = fd;
58
59 LOGV_IF(INPUT_EVENT_DEBUG,
60 "DEBUG:%s enter, fd=%d\n", __PRETTY_FUNCTION__, fd);
61 if (mFreeSpace) {
62 const ssize_t nread = read(fd, mHead, mFreeSpace * sizeof(input_event));
63 if (nread < 0 || nread % sizeof(input_event)) {
64 //LOGE("Partial event received nread=%d, required=%d",
65 // nread, sizeof(input_event));
66 //LOGE("FD trying to read is: %d");
67 // we got a partial event!!
68 if (INPUT_EVENT_DEBUG) {
69 LOGV_IF(nread < 0, "DEBUG:%s exit nread < 0\n",
70 __PRETTY_FUNCTION__);
71 LOGV_IF(nread % sizeof(input_event),
72 "DEBUG:%s exit nread %% sizeof(input_event)\n",
73 __PRETTY_FUNCTION__);
74 }
75 return (nread < 0 ? -errno : -EINVAL);
76 }
77
78 numEventsRead = nread / sizeof(input_event);
79 if (numEventsRead) {
80 mHead += numEventsRead;
81 mFreeSpace -= numEventsRead;
82 if (mHead > mBufferEnd) {
83 size_t s = mHead - mBufferEnd;
84 memcpy(mBuffer, mBufferEnd, s * sizeof(input_event));
85 mHead = mBuffer + s;
86 }
87 }
88 }
89
90 LOGV_IF(INPUT_EVENT_DEBUG, "DEBUG:%s exit, numEventsRead:%d\n",
91 __PRETTY_FUNCTION__, numEventsRead);
92 return numEventsRead;
93 }
94
readEvent(input_event const ** events)95 ssize_t InputEventCircularReader::readEvent(input_event const** events)
96 {
97 *events = mCurr;
98 ssize_t available = (mBufferEnd - mBuffer) - mFreeSpace;
99 LOGV_IF(INPUT_EVENT_DEBUG, "DEBUG:%s fd:%d, available:%d\n",
100 __PRETTY_FUNCTION__, mLastFd, (int)available);
101 return (available ? 1 : 0);
102 }
103
next()104 void InputEventCircularReader::next()
105 {
106 mCurr++;
107 mFreeSpace++;
108 if (mCurr >= mBufferEnd) {
109 mCurr = mBuffer;
110 }
111 ssize_t available = (mBufferEnd - mBuffer) - mFreeSpace;
112 LOGV_IF(INPUT_EVENT_DEBUG, "DEBUG:%s fd:%d, still available:%d\n",
113 __PRETTY_FUNCTION__, mLastFd, (int)available);
114 }
115
116