*
* Atkbd style sensor
*
- * Copyright (C) 2011 The Android-x86 Open Source Project
+ * Copyright (C) 2011-2013 The Android-x86 Open Source Project
*
* by Chih-Wei Huang <cwhuang@linux.org.tw>
*
#include <cmath>
#include <cerrno>
+#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <poll.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <hardware/sensors.h>
+#include <cutils/properties.h>
+
+struct KbdSensorKeys {
+ char name[64];
+ int keys[8];
+} KeysType[] = {
+ { "", { } },
+ { "AT Translated Set 2 keyboard", { EV_KEY, KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_LEFT, KEY_LEFTALT, KEY_LEFTCTRL, 1 } },
+ { "AT Translated Set 2 keyboard", { EV_MSC, 91, 115, 123, 109, KEY_LEFTALT, KEY_LEFTCTRL, 3 } },
+ { "AT Translated Set 2 keyboard", { EV_KEY, KEY_F5, KEY_F8, KEY_F6, KEY_F7, KEY_LEFTALT, KEY_LEFTCTRL, 1 } },
+ { "AT Translated Set 2 keyboard", { EV_KEY, KEY_F9, KEY_F12, KEY_F10, KEY_F11, KEY_LEFTALT, KEY_LEFTCTRL, 1 } },
+ { "Asus Laptop extra buttons", { EV_KEY, KEY_F9, KEY_F12, KEY_F10, KEY_F11, KEY_LEFTALT, KEY_LEFTCTRL, 2 } },
+ { "HP WMI hotkeys", { -1, KEY_DIRECTION, 0, 0, 0, 0, 0, 3 } },
+};
const int ID_ACCELERATION = (SENSORS_HANDLE_BASE + 0);
template <typename T> struct SensorFd : T {
- int ufd;
-
SensorFd(const struct hw_module_t *module, struct hw_device_t **device);
- ~SensorFd();
-
- static int common_close(struct hw_device_t *dev);
};
-template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module, struct hw_device_t **device) : ufd(-1)
+template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module, struct hw_device_t **device)
{
this->common.tag = HARDWARE_DEVICE_TAG;
this->common.version = 0;
this->common.module = const_cast<struct hw_module_t *>(module);
- this->common.close = common_close;
*device = &this->common;
- LOGD("%s: module=%p dev=%p", __FUNCTION__, module, *device);
-}
-
-template <typename T> SensorFd<T>::~SensorFd()
-{
- close(ufd);
+ ALOGD("%s: module=%p dev=%p", __FUNCTION__, module, *device);
}
-template <typename T> int SensorFd<T>::common_close(struct hw_device_t *dev)
-{
- LOGD("%s: dev=%p", __FUNCTION__, dev);
- delete reinterpret_cast<SensorFd<T> *>(dev);
- return 0;
-}
-
-/**
- ** SENSORS CONTROL DEVICE -- used to send commands to the sensors drivers
- **/
-struct SensorControl : SensorFd<sensors_control_device_t> {
+struct SensorPollContext : SensorFd<sensors_poll_device_t> {
public:
- SensorControl(const struct hw_module_t *module, struct hw_device_t **device);
+ SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device);
+ ~SensorPollContext();
private:
- static native_handle_t *control_open_data_source(struct sensors_control_device_t *dev);
- static int control_activate(struct sensors_control_device_t *dev, int handle, int enabled);
- static int control_set_delay(struct sensors_control_device_t *dev, int32_t ms);
- static int control_wake(struct sensors_control_device_t *dev);
+ static int poll_close(struct hw_device_t *dev);
+ static int poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled);
+ static int poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns);
+ static int poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count);
+
+ int doPoll(sensors_event_t *data, int count);
+
+ enum {
+ ROT_0,
+ ROT_90,
+ ROT_180,
+ ROT_270
+ };
+
+ bool enabled;
+ int rotation;
+ struct timespec delay;
+ struct pollfd pfd;
+ sensors_event_t orients[4];
+ KbdSensorKeys *ktype;
};
-SensorControl::SensorControl(const struct hw_module_t *module, struct hw_device_t **device)
- : SensorFd<sensors_control_device_t>(module, device)
+SensorPollContext::SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device)
+ : SensorFd<sensors_poll_device_t>(module, device), enabled(false), rotation(ROT_0), ktype(KeysType)
{
- open_data_source = control_open_data_source;
- activate = control_activate;
- set_delay = control_set_delay;
- wake = control_wake;
-}
+ common.close = poll_close;
+ activate = poll_activate;
+ setDelay = poll_setDelay;
+ poll = poll_poll;
-native_handle_t* SensorControl::control_open_data_source(struct sensors_control_device_t *dev)
-{
- SensorControl *ctl = reinterpret_cast<SensorControl *>(dev);
- native_handle_t *handle;
- handle = native_handle_create(1, 1);
- int fd = -1;
+ int &fd = pfd.fd;
const char *dirname = "/dev/input";
+ char prop[PROPERTY_VALUE_MAX];
+ if (property_get("hal.sensors.kbd.keys", prop, 0))
+ sscanf(prop, "%s,%d,%d,%d,%d,%d,%d,%d,%d", ktype->name, ktype->keys,
+ ktype->keys + 1, ktype->keys + 2, ktype->keys + 3, ktype->keys + 4, ktype->keys + 5, ktype->keys + 6, ktype->keys + 7);
+ else if (property_get("hal.sensors.kbd.type", prop, 0))
+ ktype = &KeysType[atoi(prop)];
+ else
+ ktype = 0;
if (DIR *dir = opendir(dirname)) {
+ char name[PATH_MAX];
while (struct dirent *de = readdir(dir)) {
- if (de->d_name[0] != 'e') // eventX
+ if (de->d_name[0] != 'e') // not eventX
continue;
- char name[PATH_MAX];
snprintf(name, PATH_MAX, "%s/%s", dirname, de->d_name);
fd = open(name, O_RDWR);
if (fd < 0) {
- LOGE("could not open %s, %s", name, strerror(errno));
+ ALOGE("could not open %s, %s", name, strerror(errno));
continue;
}
name[sizeof(name) - 1] = '\0';
if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
- LOGE("could not get device name for %s, %s\n", name, strerror(errno));
+ ALOGE("could not get device name for %s, %s\n", name, strerror(errno));
name[0] = '\0';
}
- // TODO: parse /etc/excluded-input-devices.xml
- if (!strcmp(name, "AT Translated Set 2 keyboard")) {
- LOGI("open %s ok", name);
- break;
+ if (ktype) {
+ if (!strcmp(name, ktype->name))
+ break;
+ } else {
+ ktype = KeysType + (sizeof(KeysType) / sizeof(KeysType[0]));
+ while (--ktype != KeysType)
+ if (!strcmp(name, ktype->name))
+ break;
+ if (ktype != KeysType)
+ break;
+ else
+ ktype = 0;
}
close(fd);
+ fd = -1;
}
+ ALOGI_IF(fd >= 0, "Open %s ok, fd=%d", name, fd);
closedir(dir);
}
- handle->data[0] = fd;
-
- handle->data[1] = -1;
- if (ctl->ufd < 0) {
- fd = open("/dev/uinput", O_WRONLY | O_NDELAY);
- if (fd >= 0) {
- struct uinput_user_dev ud;
- memset(&ud, 0, sizeof(ud));
- strcpy(ud.name, "Tega V2 Buttons");
- write(fd, &ud, sizeof(ud));
- ioctl(fd, UI_SET_EVBIT, EV_KEY);
- ioctl(fd, UI_SET_EVBIT, EV_REP);
- ioctl(fd, UI_SET_KEYBIT, KEY_ESC);
- ioctl(fd, UI_SET_KEYBIT, KEY_COMPOSE);
- ioctl(fd, UI_SET_KEYBIT, KEY_LEFTMETA);
- ioctl(fd, UI_DEV_CREATE, 0);
- } else {
- LOGE("could not open uinput device: %s", strerror(errno));
- }
- handle->data[1] = ctl->ufd = fd;
- }
-
- LOGD("%s: dev=%p handle=%p data[0]=%d data[1]=%d", __FUNCTION__, dev, handle, handle->data[0], handle->data[1]);
- return handle;
-}
-
-int SensorControl::control_activate(struct sensors_control_device_t *dev, int handle, int enabled)
-{
- LOGD("%s: dev=%p handle=%d enabled=%d", __FUNCTION__, dev, handle, enabled);
- return 0;
-}
-
-int SensorControl::control_set_delay(struct sensors_control_device_t *dev, int32_t ms)
-{
- LOGD("%s: dev=%p delay-ms=%d", __FUNCTION__, dev, ms);
- return 0;
-}
-
-int SensorControl::control_wake(struct sensors_control_device_t *dev)
-{
- LOGD("%s: dev=%p", __FUNCTION__, dev);
- return 0;
-}
-
-/**
- ** SENSORS DATA DEVICE -- used to read sensor data from the hardware.
- **/
-class SensorData : SensorFd<sensors_data_device_t> {
- public:
- SensorData(const struct hw_module_t *module, struct hw_device_t **device);
-
- private:
- static int data_data_open(struct sensors_data_device_t *dev, native_handle_t *handle);
- static int data_data_close(struct sensors_data_device_t *dev);
- static int data_poll(struct sensors_data_device_t *dev, sensors_data_t *values);
-
- enum {
- ROT_0,
- ROT_90,
- ROT_180,
- ROT_270
- };
-
- int rotation;
- struct pollfd pfd;
- sensors_data_t orients[4];
-};
-
-SensorData::SensorData(const struct hw_module_t *module, struct hw_device_t **device)
- : SensorFd<sensors_data_device_t>(module, device), rotation(ROT_0)
-{
- data_open = data_data_open;
- data_close = data_data_close;
- poll = data_poll;
pfd.events = POLLIN;
+ orients[ROT_0].version = sizeof(sensors_event_t);
orients[ROT_0].sensor = ID_ACCELERATION;
+ orients[ROT_0].type = SENSOR_TYPE_ACCELEROMETER;
orients[ROT_0].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
orients[ROT_270] = orients[ROT_180] = orients[ROT_90] = orients[ROT_0];
const double angle = 20.0;
orients[ROT_90].acceleration.y = 0.0;
orients[ROT_90].acceleration.z = sin_angle;
orients[ROT_180].acceleration.x = 0.0;
- orients[ROT_180].acceleration.y = +cos_angle;
+ orients[ROT_180].acceleration.y = -cos_angle;
orients[ROT_180].acceleration.z = -sin_angle;
orients[ROT_270].acceleration.x = -cos_angle;
orients[ROT_270].acceleration.y = 0.0;
orients[ROT_270].acceleration.z = -sin_angle;
+
+ delay.tv_sec = 0;
+ delay.tv_nsec = 200000000L;
+
+ ALOGD("%s: dev=%p fd=%d", __FUNCTION__, this, fd);
}
-int SensorData::data_data_open(struct sensors_data_device_t *dev, native_handle_t *handle)
+SensorPollContext::~SensorPollContext()
{
- SensorData *data = reinterpret_cast<SensorData *>(dev);
+ close(pfd.fd);
+}
- data->ufd = handle->data[1];
- data->pfd.fd = dup(handle->data[0]);
- LOGD("%s: dev=%p ufd=%d fd=%d(%d) handle=%p)", __FUNCTION__, dev, data->ufd, data->pfd.fd, handle->data[0], handle);
- native_handle_close(handle);
- native_handle_delete(handle);
+int SensorPollContext::poll_close(struct hw_device_t *dev)
+{
+ ALOGD("%s: dev=%p", __FUNCTION__, dev);
+ delete reinterpret_cast<SensorPollContext *>(dev);
return 0;
}
-int SensorData::data_data_close(struct sensors_data_device_t *dev)
+int SensorPollContext::poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled)
{
- LOGD("%s: dev=%p", __FUNCTION__, dev);
- SensorData *data = reinterpret_cast<SensorData *>(dev);
- if (data) {
- close(data->ufd);
- data->ufd = -1;
- close(data->pfd.fd);
- data->pfd.fd = -1;
- }
+ ALOGD("%s: dev=%p handle=%d enabled=%d", __FUNCTION__, dev, handle, enabled);
+ SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
+ ctx->enabled = enabled;
return 0;
}
-int SensorData::data_poll(struct sensors_data_device_t *dev, sensors_data_t *values)
+int SensorPollContext::poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns)
{
- SensorData *data = reinterpret_cast<SensorData *>(dev);
- LOGV("%s: dev=%p fd=%d,%d", __FUNCTION__, dev, data->fd[0], data->fd[1]);
+ ALOGD("%s: dev=%p delay-ns=%lld", __FUNCTION__, dev, ns);
+ return 0;
+}
+
+int SensorPollContext::poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count)
+{
+ ALOGV("%s: dev=%p data=%p count=%d", __FUNCTION__, dev, data, count);
+ SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
+ return ctx->doPoll(data, count);
+}
- struct pollfd &pfd = data->pfd;
+int SensorPollContext::doPoll(sensors_event_t *data, int count)
+{
+ nanosleep(&delay, 0);
+ int *keys = ktype->keys;
while (int pollres = ::poll(&pfd, 1, -1)) {
if (pollres < 0) {
- LOGE("%s: poll %d error: %s", __FUNCTION__, pfd.fd, strerror(errno));
+ ALOGE("%s: poll %d error: %s", __FUNCTION__, pfd.fd, strerror(errno));
break;
}
if (!(pfd.revents & POLLIN)) {
- LOGW("%s: ignore revents %d", __FUNCTION__, pfd.revents);
+ ALOGW("%s: ignore revents %d", __FUNCTION__, pfd.revents);
continue;
}
struct input_event iev;
size_t res = ::read(pfd.fd, &iev, sizeof(iev));
if (res < sizeof(iev)) {
- LOGW("insufficient input data(%d)? fd=%d", res, pfd.fd);
+ ALOGW("insufficient input data(%d)? fd=%d", res, pfd.fd);
continue;
}
- LOGD("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, pfd.fd);
- if (iev.type == EV_KEY) {
- int rot = -1;
- switch (iev.code)
- {
- case KEY_LEFTCTRL:
- case KEY_LEFTALT:
- if (iev.value)
- continue;
- rot = data->rotation;
- break;
- case KEY_UP:
- rot = ROT_0;
- break;
- case KEY_RIGHT:
- rot = ROT_90;
- break;
- case KEY_DOWN:
- rot = ROT_180;
- break;
- case KEY_LEFT:
- rot = ROT_270;
- break;
-#if 0
- case KEY_ESC:
- iev.code = KEY_LEFTMETA;
- break;
- case KEY_COMPOSE:
- iev.code = KEY_ESC;
- break;
-#endif
- }
+ ALOGV("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, pfd.fd);
+ if (iev.type == keys[0]) {
+ int rot;
+ int input = (keys[0] == EV_MSC) ? iev.value : iev.code;
+ if (input == keys[1])
+ rot = ROT_0;
+ else if (input == keys[2])
+ rot = ROT_90;
+ else if (input == keys[3])
+ rot = ROT_180;
+ else if (input == keys[4])
+ rot = ROT_270;
+ else if (input == keys[5] || input == keys[6])
+ rot = rotation;
+ else
+ rot = -1;
+
if (rot >= 0) {
- if (rot != data->rotation) {
- LOGI("orientation changed from %d to %d", data->rotation * 90, rot * 90);
- data->rotation = rot;
+ if (rot != rotation) {
+ ALOGI("orientation changed from %d to %d", rotation * 90, rot * 90);
+ rotation = rot;
}
- break;
+ if (enabled && count > 0)
+ break;
}
+ } else if (iev.type == EV_KEY) {
+ if (iev.code == keys[1] && iev.value) {
+ if (rotation == ROT_270)
+ rotation = ROT_0;
+ else
+ rotation++;
+ }
+ if (iev.code == keys[2] && iev.value) {
+ if (rotation == ROT_0)
+ rotation = ROT_270;
+ else
+ rotation--;
+ }
+ break;
+ } else if (iev.type == EV_SW && iev.code == SW_TABLET_MODE) {
+ if (!iev.value)
+ rotation = ROT_0;
+ else if (rotation == ROT_0)
+ rotation = ROT_90;
+ break;
}
-
- if (data->ufd >= 0)
- write(data->ufd, &iev, sizeof(iev));
}
- *values = data->orients[data->rotation];
- LOGD("%s: dev=%p ufd=%d fd=%d rotation=%d", __FUNCTION__, dev, data->ufd, pfd.fd, data->rotation * 90);
- return 0;
+ int cnt;
+ struct timespec t;
+ data[0] = orients[rotation];
+ t.tv_sec = t.tv_nsec = 0;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ data[0].timestamp = int64_t(t.tv_sec) * 1000000000LL + t.tv_nsec;
+ for (cnt = 1; cnt < keys[7] && cnt < count; ++cnt) {
+ data[cnt] = data[cnt - 1];
+ data[cnt].timestamp += delay.tv_nsec;
+ nanosleep(&delay, 0);
+ }
+ ALOGV("%s: dev=%p fd=%d rotation=%d cnt=%d", __FUNCTION__, this, pfd.fd, rotation * 90, cnt);
+ return cnt;
}
static int open_kbd_sensor(const struct hw_module_t *module, const char *id, struct hw_device_t **device)
{
- LOGD("%s: id=%s", __FUNCTION__, id);
- void *dev = 0;
- if (!strcmp(id, SENSORS_HARDWARE_CONTROL))
- dev = new SensorControl(module, device);
- else if (!strcmp(id, SENSORS_HARDWARE_DATA))
- dev = new SensorData(module, device);
- return dev ? 0 : -1;
+ ALOGD("%s: id=%s", __FUNCTION__, id);
+ return new SensorPollContext(module, device) ? 0 : -EINVAL;
}
static struct sensor_t sSensorListInit[] = {
maxRange: 2.8f,
resolution: 1.0f/4032.0f,
power: 3.0f,
+ minDelay: 0,
reserved: { }
}
};
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 2,
- version_minor: 2,
+ version_minor: 3,
id: SENSORS_HARDWARE_MODULE_ID,
name: "Kbd Orientation Sensor",
author: "Chih-Wei Huang",