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.
23 #include <sys/cdefs.h>
24 #include <sys/types.h>
26 #include <linux/input.h>
28 #include <cutils/log.h>
30 #include "InputEventReader.h"
32 /*****************************************************************************/
36 InputEventCircularReader::InputEventCircularReader(size_t numEvents)
37 : mBuffer(new input_event[numEvents * 2]),
38 mBufferEnd(mBuffer + numEvents),
45 InputEventCircularReader::~InputEventCircularReader()
50 ssize_t InputEventCircularReader::fill(int fd)
52 size_t numEventsRead = 0;
54 const ssize_t nread = read(fd, mHead, mFreeSpace * sizeof(input_event));
55 if (nread<0 || nread % sizeof(input_event)) {
56 // we got a partial event!!
57 return nread<0 ? -errno : -EINVAL;
60 numEventsRead = nread / sizeof(input_event);
62 mHead += numEventsRead;
63 mFreeSpace -= numEventsRead;
64 if (mHead > mBufferEnd) {
65 size_t s = mHead - mBufferEnd;
66 memcpy(mBuffer, mBufferEnd, s * sizeof(input_event));
75 ssize_t InputEventCircularReader::readEvent(input_event const** events)
78 ssize_t available = (mBufferEnd - mBuffer) - mFreeSpace;
79 return available ? 1 : 0;
82 void InputEventCircularReader::next()
86 if (mCurr >= mBufferEnd) {