2 * Copyright (C) 2008 The Android Open Source Project
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.
22 #include <sys/cdefs.h>
23 #include <sys/types.h>
25 #include <linux/input.h>
27 #include <cutils/log.h>
29 #include "InputEventReader.h"
31 /*****************************************************************************/
35 InputEventCircularReader::InputEventCircularReader(size_t numEvents)
36 : mBuffer(new input_event[numEvents * 2]),
37 mBufferEnd(mBuffer + numEvents),
44 InputEventCircularReader::~InputEventCircularReader()
49 ssize_t InputEventCircularReader::fill(int fd)
51 size_t numEventsRead = 0;
53 const ssize_t nread = read(fd, mHead, mFreeSpace * sizeof(input_event));
54 if (nread<0 || nread % sizeof(input_event)) {
55 // we got a partial event!!
56 return nread<0 ? -errno : -EINVAL;
59 numEventsRead = nread / sizeof(input_event);
61 mHead += numEventsRead;
62 mFreeSpace -= numEventsRead;
63 if (mHead > mBufferEnd) {
64 size_t s = mHead - mBufferEnd;
65 memcpy(mBuffer, mBufferEnd, s * sizeof(input_event));
74 ssize_t InputEventCircularReader::readEvent(input_event const** events)
77 ssize_t available = (mBufferEnd - mBuffer) - mFreeSpace;
78 return available ? 1 : 0;
81 void InputEventCircularReader::next()
85 if (mCurr >= mBufferEnd) {