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#include "local_log_def.h"
33
34/*****************************************************************************/
35
36struct input_event;
37
38InputEventCircularReader::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}
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    LOGV_IF(INPUT_EVENT_DEBUG,
57            "DEBUG:%s enter, fd=%d\n", __PRETTY_FUNCTION__, fd);
58    if (mFreeSpace) {
59        const ssize_t nread = read(fd, mHead, mFreeSpace * sizeof(input_event));
60        if (nread < 0 || nread % sizeof(input_event)) {
61            //LOGE("Partial event received nread=%d, required=%d",
62            //     nread, sizeof(input_event));
63            //LOGE("FD trying to read is: %d");
64            // we got a partial event!!
65            if (INPUT_EVENT_DEBUG) {
66                LOGV_IF(nread < 0, "DEBUG:%s exit nread < 0\n",
67                        __PRETTY_FUNCTION__);
68                LOGV_IF(nread % sizeof(input_event),
69                        "DEBUG:%s exit nread %% sizeof(input_event)\n",
70                        __PRETTY_FUNCTION__);
71            }
72            return (nread < 0 ? -errno : -EINVAL);
73        }
74
75        numEventsRead = nread / sizeof(input_event);
76        if (numEventsRead) {
77            mHead += numEventsRead;
78            mFreeSpace -= numEventsRead;
79            if (mHead > mBufferEnd) {
80                size_t s = mHead - mBufferEnd;
81                memcpy(mBuffer, mBufferEnd, s * sizeof(input_event));
82                mHead = mBuffer + s;
83            }
84        }
85    }
86
87    LOGV_IF(INPUT_EVENT_DEBUG, "DEBUG:%s exit\n", __PRETTY_FUNCTION__);
88    return numEventsRead;
89}
90
91ssize_t InputEventCircularReader::readEvent(input_event const** events)
92{
93    *events = mCurr;
94    ssize_t available = (mBufferEnd - mBuffer) - mFreeSpace;
95    return available ? 1 : 0;
96}
97
98void InputEventCircularReader::next()
99{
100    mCurr++;
101    mFreeSpace++;
102    if (mCurr >= mBufferEnd) {
103        mCurr = mBuffer;
104    }
105}
106