5 * Copyright (C) 2011 The Android-x86 Open Source Project
7 * by Chih-Wei Huang <cwhuang@linux.org.tw>
9 * Licensed under GPLv2 or later
13 #define LOG_TAG "KbdSensor"
22 #include <cutils/log.h>
23 #include <linux/input.h>
24 #include <linux/uinput.h>
25 #include <hardware/sensors.h>
27 const int ID_ACCELERATION = (SENSORS_HANDLE_BASE + 0);
29 template <typename T> struct SensorFd : T {
32 SensorFd(const struct hw_module_t *module, struct hw_device_t **device);
36 template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module, struct hw_device_t **device) : ufd(-1)
38 this->common.tag = HARDWARE_DEVICE_TAG;
39 this->common.version = 0;
40 this->common.module = const_cast<struct hw_module_t *>(module);
41 *device = &this->common;
42 LOGD("%s: module=%p dev=%p", __FUNCTION__, module, *device);
45 template <typename T> SensorFd<T>::~SensorFd()
50 struct SensorPollContext : SensorFd<sensors_poll_device_t> {
52 SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device);
56 static int poll_close(struct hw_device_t *dev);
57 static int poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled);
58 static int poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns);
59 static int poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count);
71 sensors_event_t orients[4];
74 SensorPollContext::SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device)
75 : SensorFd<sensors_poll_device_t>(module, device), enabled(false), rotation(ROT_0)
77 common.close = poll_close;
78 activate = poll_activate;
79 setDelay = poll_setDelay;
83 const char *dirname = "/dev/input";
84 if (DIR *dir = opendir(dirname)) {
85 while (struct dirent *de = readdir(dir)) {
86 if (de->d_name[0] != 'e') // eventX
89 snprintf(name, PATH_MAX, "%s/%s", dirname, de->d_name);
90 fd = open(name, O_RDWR);
92 LOGE("could not open %s, %s", name, strerror(errno));
95 name[sizeof(name) - 1] = '\0';
96 if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
97 LOGE("could not get device name for %s, %s\n", name, strerror(errno));
101 // TODO: parse /etc/excluded-input-devices.xml
102 if (!strcmp(name, "AT Translated Set 2 keyboard")) {
103 LOGI("open %s ok", name);
111 ufd = open("/dev/uinput", O_WRONLY | O_NDELAY);
113 struct uinput_user_dev ud;
114 memset(&ud, 0, sizeof(ud));
115 strcpy(ud.name, "Tega V2 Buttons");
116 write(ufd, &ud, sizeof(ud));
117 ioctl(ufd, UI_SET_EVBIT, EV_KEY);
118 ioctl(ufd, UI_SET_EVBIT, EV_REP);
119 ioctl(ufd, UI_SET_KEYBIT, KEY_ESC);
120 ioctl(ufd, UI_SET_KEYBIT, KEY_COMPOSE);
121 ioctl(ufd, UI_SET_KEYBIT, KEY_LEFTMETA);
122 ioctl(ufd, UI_DEV_CREATE, 0);
124 LOGE("could not open uinput device: %s", strerror(errno));
128 orients[ROT_0].version = sizeof(sensors_event_t);
129 orients[ROT_0].sensor = ID_ACCELERATION;
130 orients[ROT_0].type = SENSOR_TYPE_ACCELEROMETER;
131 orients[ROT_0].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
132 orients[ROT_270] = orients[ROT_180] = orients[ROT_90] = orients[ROT_0];
133 const double angle = 20.0;
134 const double cos_angle = GRAVITY_EARTH * cos(angle / M_PI);
135 const double sin_angle = GRAVITY_EARTH * sin(angle / M_PI);
136 orients[ROT_0].acceleration.x = 0.0;
137 orients[ROT_0].acceleration.y = cos_angle;
138 orients[ROT_0].acceleration.z = sin_angle;
139 orients[ROT_90].acceleration.x = cos_angle;
140 orients[ROT_90].acceleration.y = 0.0;
141 orients[ROT_90].acceleration.z = sin_angle;
142 orients[ROT_180].acceleration.x = 0.0;
143 orients[ROT_180].acceleration.y = +cos_angle;
144 orients[ROT_180].acceleration.z = -sin_angle;
145 orients[ROT_270].acceleration.x = -cos_angle;
146 orients[ROT_270].acceleration.y = 0.0;
147 orients[ROT_270].acceleration.z = -sin_angle;
149 LOGD("%s: dev=%p ufd=%d fd=%d", __FUNCTION__, this, ufd, fd);
152 SensorPollContext::~SensorPollContext()
157 int SensorPollContext::poll_close(struct hw_device_t *dev)
159 LOGD("%s: dev=%p", __FUNCTION__, dev);
160 delete reinterpret_cast<SensorPollContext *>(dev);
163 int SensorPollContext::poll_activate(struct sensors_poll_device_t *dev, int handle, int enabled)
165 LOGD("%s: dev=%p handle=%d enabled=%d", __FUNCTION__, dev, handle, enabled);
166 SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
167 ctx->enabled = enabled;
170 int SensorPollContext::poll_setDelay(struct sensors_poll_device_t *dev, int handle, int64_t ns)
172 LOGD("%s: dev=%p delay-ns=%lld", __FUNCTION__, dev, ns);
175 int SensorPollContext::poll_poll(struct sensors_poll_device_t *dev, sensors_event_t *data, int count)
177 LOGD("%s: dev=%p data=%p count=%d", __FUNCTION__, dev, data, count);
178 SensorPollContext *ctx = reinterpret_cast<SensorPollContext *>(dev);
180 struct pollfd &pfd = ctx->pfd;
181 while (int pollres = ::poll(&pfd, 1, -1)) {
183 LOGE("%s: poll %d error: %s", __FUNCTION__, pfd.fd, strerror(errno));
186 if (!(pfd.revents & POLLIN)) {
187 LOGW("%s: ignore revents %d", __FUNCTION__, pfd.revents);
191 struct input_event iev;
192 size_t res = ::read(pfd.fd, &iev, sizeof(iev));
193 if (res < sizeof(iev)) {
194 LOGW("insufficient input data(%d)? fd=%d", res, pfd.fd);
197 LOGD("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, pfd.fd);
198 if (iev.type == EV_KEY) {
222 iev.code = KEY_LEFTMETA;
230 if (rot != ctx->rotation) {
231 LOGI("orientation changed from %d to %d", ctx->rotation * 90, rot * 90);
234 if (ctx->enabled && count > 0)
240 write(ctx->ufd, &iev, sizeof(iev));
243 LOGD("%s: dev=%p ufd=%d fd=%d rotation=%d", __FUNCTION__, dev, ctx->ufd, pfd.fd, ctx->rotation * 90);
244 data[0] = ctx->orients[ctx->rotation];
248 static int open_kbd_sensor(const struct hw_module_t *module, const char *id, struct hw_device_t **device)
250 LOGD("%s: id=%s", __FUNCTION__, id);
251 return new SensorPollContext(module, device) ? 0 : -EINVAL;
254 static struct sensor_t sSensorListInit[] = {
256 name: "Kbd Orientation Sensor",
257 vendor: "Android-x86 Open Source Project",
259 handle: ID_ACCELERATION,
260 type: SENSOR_TYPE_ACCELEROMETER,
262 resolution: 1.0f/4032.0f,
269 static int sensors_get_sensors_list(struct sensors_module_t *module, struct sensor_t const **list)
271 *list = sSensorListInit;
272 return sizeof(sSensorListInit) / sizeof(struct sensor_t);
275 static struct hw_module_methods_t sensors_methods = {
276 open: open_kbd_sensor
279 struct sensors_module_t HAL_MODULE_INFO_SYM = {
281 tag: HARDWARE_MODULE_TAG,
284 id: SENSORS_HARDWARE_MODULE_ID,
285 name: "Kbd Orientation Sensor",
286 author: "Chih-Wei Huang",
287 methods: &sensors_methods,
291 get_sensors_list: sensors_get_sensors_list