replicant-frameworks_native/libs/gui/SensorEventQueue.cpp
Jeff Brown 59abe7e090 Replace epoll() with poll() and rename PollLoop to Looper.
As part of this change, consolidated and cleaned up the Looper API so
that there are fewer distinctions between the NDK and non-NDK declarations
(no need for two callback types, etc.).

Removed the dependence on specific constants from sys/poll.h such as
POLLIN.  Instead looper.h defines events like LOOPER_EVENT_INPUT for
the events that it supports.  That should help make any future
under-the-hood implementation changes easier.

Fixed a couple of compiler warnings along the way.

Change-Id: I449a7ec780bf061bdd325452f823673e2b39b6ae
2010-09-14 01:59:45 -07:00

136 lines
3.8 KiB
C++

/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "Sensors"
#include <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/Looper.h>
#include <gui/Sensor.h>
#include <gui/SensorChannel.h>
#include <gui/SensorEventQueue.h>
#include <gui/ISensorEventConnection.h>
#include <android/sensor.h>
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
: mSensorEventConnection(connection)
{
}
SensorEventQueue::~SensorEventQueue()
{
}
void SensorEventQueue::onFirstRef()
{
mSensorChannel = mSensorEventConnection->getSensorChannel();
}
int SensorEventQueue::getFd() const
{
return mSensorChannel->getFd();
}
ssize_t SensorEventQueue::write(ASensorEvent const* events, size_t numEvents)
{
ssize_t size = mSensorChannel->write(events, numEvents * sizeof(events[0]));
if (size >= 0) {
if (size % sizeof(events[0])) {
// partial write!!! should never happen.
return -EINVAL;
}
// returns number of events written
size /= sizeof(events[0]);
}
return size;
}
ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents)
{
ssize_t size = mSensorChannel->read(events, numEvents*sizeof(events[0]));
if (size >= 0) {
if (size % sizeof(events[0])) {
// partial read!!! should never happen.
return -EINVAL;
}
// returns number of events read
size /= sizeof(events[0]);
}
return size;
}
sp<Looper> SensorEventQueue::getLooper() const
{
Mutex::Autolock _l(mLock);
if (mLooper == 0) {
mLooper = new Looper(true);
mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
}
return mLooper;
}
status_t SensorEventQueue::waitForEvent() const
{
const int fd = getFd();
sp<Looper> looper(getLooper());
int32_t result = looper->pollOnce(-1);
return (result == fd) ? status_t(NO_ERROR) : status_t(-1);
}
status_t SensorEventQueue::wake() const
{
sp<Looper> looper(getLooper());
looper->wake();
return NO_ERROR;
}
status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
return mSensorEventConnection->enableDisable(sensor->getHandle(), true);
}
status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
return mSensorEventConnection->enableDisable(sensor->getHandle(), false);
}
status_t SensorEventQueue::enableSensor(int32_t handle, int32_t us) const {
status_t err = mSensorEventConnection->enableDisable(handle, true);
if (err == NO_ERROR) {
mSensorEventConnection->setEventRate(handle, us2ns(us));
}
return err;
}
status_t SensorEventQueue::disableSensor(int32_t handle) const {
return mSensorEventConnection->enableDisable(handle, false);
}
status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
}
// ----------------------------------------------------------------------------
}; // namespace android