aboutsummaryrefslogtreecommitdiffstats
path: root/libsensors/input.c
diff options
context:
space:
mode:
authorPaul Kocialkowski <contact@paulk.fr>2015-06-15 16:22:16 +0200
committerAndreas Blaesius <skate4life@gmx.de>2015-08-15 14:21:25 -0700
commitaf6a75109ed68e89998f221ba27fe15980f01714 (patch)
treeae474f87650bcdc8be89f53c26bfcd0a05b77175 /libsensors/input.c
parentc9541c3d21b7f1ada9c37fb65d9abace51cdcdc5 (diff)
downloaddevice_samsung_espressowifi-af6a75109ed68e89998f221ba27fe15980f01714.zip
device_samsung_espressowifi-af6a75109ed68e89998f221ba27fe15980f01714.tar.gz
device_samsung_espressowifi-af6a75109ed68e89998f221ba27fe15980f01714.tar.bz2
espresso-common: libsensors: sync with replicant
Change-Id: Idcb9a57af699833c504f236692a6bdb1ef33c98e
Diffstat (limited to 'libsensors/input.c')
-rw-r--r--libsensors/input.c216
1 files changed, 213 insertions, 3 deletions
diff --git a/libsensors/input.c b/libsensors/input.c
index f82cfd2..c48f17f 100644
--- a/libsensors/input.c
+++ b/libsensors/input.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013 Paul Kocialkowski
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -23,18 +23,107 @@
#include <dirent.h>
#include <linux/ioctl.h>
#include <linux/input.h>
+#include <linux/uinput.h>
#define LOG_TAG "piranha_sensors"
#include <utils/Log.h>
#include "piranha_sensors.h"
-int64_t input_timestamp(struct input_event *event)
+void input_event_set(struct input_event *event, int type, int code, int value)
+{
+ if (event == NULL)
+ return;
+
+ memset(event, 0, sizeof(struct input_event));
+
+ event->type = type,
+ event->code = code;
+ event->value = value;
+
+ gettimeofday(&event->time, NULL);
+}
+
+long int timestamp(struct timeval *time)
+{
+ if (time == NULL)
+ return -1;
+
+ return time->tv_sec * 1000000000LL + time->tv_usec * 1000;
+}
+
+long int input_timestamp(struct input_event *event)
{
if (event == NULL)
return -1;
- return event->time.tv_sec*1000000000LL + event->time.tv_usec*1000;
+ return timestamp(&event->time);
+}
+
+int uinput_rel_create(const char *name)
+{
+ struct uinput_user_dev uinput_dev;
+ int uinput_fd;
+ int rc;
+
+ if (name == NULL)
+ return -1;
+
+ uinput_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
+ if (uinput_fd < 0) {
+ ALOGE("%s: Unable to open uinput device", __func__);
+ goto error;
+ }
+
+ memset(&uinput_dev, 0, sizeof(uinput_dev));
+
+ strncpy(uinput_dev.name, name, sizeof(uinput_dev.name));
+ uinput_dev.id.bustype = BUS_I2C;
+ uinput_dev.id.vendor = 0;
+ uinput_dev.id.product = 0;
+ uinput_dev.id.version = 0;
+
+ rc = 0;
+ rc |= ioctl(uinput_fd, UI_SET_EVBIT, EV_REL);
+ rc |= ioctl(uinput_fd, UI_SET_RELBIT, REL_X);
+ rc |= ioctl(uinput_fd, UI_SET_RELBIT, REL_Y);
+ rc |= ioctl(uinput_fd, UI_SET_RELBIT, REL_Z);
+ rc |= ioctl(uinput_fd, UI_SET_EVBIT, EV_SYN);
+
+ if (rc < 0) {
+ ALOGE("%s: Unable to set uinput bits", __func__);
+ goto error;
+ }
+
+ rc = write(uinput_fd, &uinput_dev, sizeof(uinput_dev));
+ if (rc < 0) {
+ ALOGE("%s: Unable to write uinput device", __func__);
+ goto error;
+ }
+
+ rc = ioctl(uinput_fd, UI_DEV_CREATE);
+ if (rc < 0) {
+ ALOGE("%s: Unable to create uinput device", __func__);
+ goto error;
+ }
+
+ usleep(3000);
+
+ return uinput_fd;
+
+error:
+ if (uinput_fd >= 0)
+ close(uinput_fd);
+
+ return -1;
+}
+
+void uinput_destroy(int uinput_fd)
+{
+ if (uinput_fd < 0)
+ return;
+
+ ioctl(uinput_fd, UI_DEV_DESTROY);
}
int input_open(char *name)
@@ -123,3 +212,124 @@ int sysfs_path_prefix(char *name, char *path_prefix)
return -1;
}
+
+int sysfs_value_read(char *path)
+{
+ char buffer[100];
+ int value;
+ int fd = -1;
+ int rc;
+
+ if (path == NULL)
+ return -1;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto error;
+
+ rc = read(fd, &buffer, sizeof(buffer));
+ if (rc <= 0)
+ goto error;
+
+ value = atoi(buffer);
+ goto complete;
+
+error:
+ value = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return value;
+}
+
+int sysfs_value_write(char *path, int value)
+{
+ char buffer[100];
+ int fd = -1;
+ int rc;
+
+ if (path == NULL)
+ return -1;
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ goto error;
+
+ snprintf((char *) &buffer, sizeof(buffer), "%d\n", value);
+
+ rc = write(fd, buffer, strlen(buffer));
+ if (rc < (int) strlen(buffer))
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+int sysfs_string_read(char *path, char *buffer, size_t length)
+{
+ int fd = -1;
+ int rc;
+
+ if (path == NULL || buffer == NULL || length == 0)
+ return -1;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto error;
+
+ rc = read(fd, buffer, length);
+ if (rc <= 0)
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+int sysfs_string_write(char *path, char *buffer, size_t length)
+{
+ int fd = -1;
+ int rc;
+
+ if (path == NULL || buffer == NULL || length == 0)
+ return -1;
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ goto error;
+
+ rc = write(fd, buffer, length);
+ if (rc <= 0)
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}