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