summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-09-23 15:08:33 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-09-23 15:08:33 +0200
commit45e71e07bbc891e552de58d5ba827ad719d90662 (patch)
treeb9741ac85661e397213923b1ff60bf862a1fb5a0
parentb1525c35503cafafd69aa63dedddf49bd00bac5f (diff)
Currently facing "Bad file descriptor" errors...
-rw-r--r--conf/space_navigator.conf2
-rw-r--r--src/config.c31
-rw-r--r--src/config.h10
-rw-r--r--src/main.c16
-rw-r--r--src/relabsd_device.c106
-rw-r--r--src/relabsd_device.h17
6 files changed, 164 insertions, 18 deletions
diff --git a/conf/space_navigator.conf b/conf/space_navigator.conf
index 788ee70..fe6a54c 100644
--- a/conf/space_navigator.conf
+++ b/conf/space_navigator.conf
@@ -1,5 +1,5 @@
# 3DConnexion SpaceNavigator
-timeout 100
+#to 100
# AXIS MIN MAX FUZZ FLAT RESOLUTION OPTIONS
X -350 350 0 0 1 direct,real_fuzz
Y -350 350 0 0 1 direct,real_fuzz
diff --git a/src/config.c b/src/config.c
index 3b8eb0a..bb78779 100644
--- a/src/config.c
+++ b/src/config.c
@@ -244,6 +244,7 @@ static int read_axis_options
return -1;
}
+/*
static int parse_timeout_option
(
struct relabsd_config * const conf,
@@ -295,6 +296,8 @@ static int parse_timeout_option
memset((void *) &(conf->timeout), 0, sizeof(struct timeval));
+ conf->timeout.tv_sec = (time_t) (timeout_msec / 1000);
+
conf->timeout.tv_usec =
(
((suseconds_t) timeout_msec)
@@ -303,6 +306,7 @@ static int parse_timeout_option
return 0;
}
+*/
/*
* Returns -1 on (fatal) error,
@@ -322,22 +326,6 @@ static int parse_axis_configuration_line
if (axis == RELABSD_UNKNOWN)
{
- if (RELABSD_STRING_EQUALS("timeout", buffer))
- {
- if (parse_timeout_option(conf, f) < 0)
- {
- RELABSD_FATAL
- (
- "[CONFIG] Issue while parsing timeout option '%s'.",
- buffer
- );
-
- return -1;
- }
-
- return 0;
- }
-
RELABSD_FATAL
(
"[CONFIG] Unknown axis '%s'.",
@@ -468,7 +456,6 @@ static int read_config_file
return -1;
}
- conf->enable_timeout = 0;
prev_errno = errno;
errno = 0;
@@ -620,6 +607,16 @@ int relabsd_config_parse
return -1;
}
+ conf->enable_timeout = RELABSD_ENABLE_TIMEOUT;
+
+ conf->timeout.tv_sec = (time_t) (RELABSD_TIMEOUT_MSEC / 1000);
+
+ conf->timeout.tv_usec =
+ (
+ ((suseconds_t) RELABSD_TIMEOUT_MSEC)
+ * ((suseconds_t) 1000)
+ );
+
return 0;
}
diff --git a/src/config.h b/src/config.h
index 3aa1a13..92852d9 100644
--- a/src/config.h
+++ b/src/config.h
@@ -11,6 +11,16 @@
/* Number of options that can be configured. */
#define RELABSD_OPTIONS_COUNT 3
+/* TODO: turn this into a runtime parameter */
+#ifndef RELABSD_ENABLE_TIMEOUT
+ #define RELABSD_ENABLE_TIMEOUT 1
+#endif
+
+/* TODO: turn this into a runtime parameter */
+#ifndef RELABSD_TIMEOUT_MSEC
+ #define RELABSD_TIMEOUT_MSEC 100
+#endif
+
enum relabsd_option
{
RELABSD_DIRECT_OPTION,
diff --git a/src/main.c b/src/main.c
index 3658adb..6d59395 100644
--- a/src/main.c
+++ b/src/main.c
@@ -65,6 +65,22 @@ static void convert_input
while (RELABSD_RUN == 1)
{
+ if (conf->enable_timeout)
+ {
+ switch (relabsd_device_wait_next_event(dev, conf))
+ {
+ case 1:
+ break;
+
+ case 0:
+ relabsd_device_set_axes_to_zero(dev, conf);
+ break;
+
+ case -1:
+ continue;
+ }
+ }
+
if (relabsd_input_read(input, &input_type, &input_code, &value) < 0)
{
/*
diff --git a/src/relabsd_device.c b/src/relabsd_device.c
index b093203..4af52c8 100644
--- a/src/relabsd_device.c
+++ b/src/relabsd_device.c
@@ -205,6 +205,8 @@ int relabsd_device_create
close(fd);
+ dev->fd = fd;
+
return 0;
}
@@ -246,3 +248,107 @@ int relabsd_device_write_evdev_event
return -1;
}
+
+
+void relabsd_device_set_axes_to_zero
+(
+ const struct relabsd_device * const dev,
+ const struct relabsd_config * const config
+)
+{
+ int i;
+
+ for (i = 0; i < RELABSD_VALID_AXES_COUNT; ++i)
+ {
+ if (config->axis[i].enabled)
+ {
+ relabsd_device_write_evdev_event
+ (
+ dev,
+ relabsd_axis_to_abs((enum relabsd_axis) i),
+ EV_ABS,
+ 0
+ );
+ }
+ }
+
+ /*
+ * Also send a SYN event when the axes have been modified.
+ */
+ libevdev_uinput_write_event(dev->uidev, EV_SYN, SYN_REPORT, 0);
+}
+
+int relabsd_device_wait_next_event
+(
+ const struct relabsd_device * const dev,
+ const struct relabsd_config * const config
+)
+{
+ int ready_fds;
+ const int old_errno = errno;
+ fd_set ready_to_read;
+ struct timeval curr_timeout;
+
+ FD_ZERO(&ready_to_read);
+ FD_SET(dev->fd, &ready_to_read);
+
+ /* call to select may alter timeout */
+ memcpy
+ (
+ (void *) &(curr_timeout),
+ (const void *) &(config->timeout),
+ sizeof(struct timeval)
+ );
+
+ errno = 0;
+
+ ready_fds = select
+ (
+ (dev->fd + 1),
+ &ready_to_read,
+ (fd_set *) NULL,
+ (fd_set *) NULL,
+ &(curr_timeout)
+ );
+
+ if (errno != 0)
+ {
+ RELABSD_ERROR
+ (
+ "Unable to wait for timeout: %s (errno: %d).",
+ strerror(errno),
+ errno
+ );
+
+ if (errno == EINTR)
+ {
+ /* Signal interruption? */
+ }
+ else
+ {
+ /* TODO: error message */
+ }
+
+ errno = old_errno;
+
+ return -1;
+ }
+
+ if (ready_fds == -1)
+ {
+ /* TODO: error message */
+
+ RELABSD_S_ERROR
+ (
+ "Unable to wait for timeout, yet errno was not set to anything."
+ );
+
+ errno = old_errno;
+
+ return -1;
+ }
+
+ errno = old_errno;
+
+ return ready_fds;
+}
diff --git a/src/relabsd_device.h b/src/relabsd_device.h
index b3f2d51..8e83196 100644
--- a/src/relabsd_device.h
+++ b/src/relabsd_device.h
@@ -10,6 +10,7 @@ struct relabsd_device
{
struct libevdev * dev;
struct libevdev_uinput * uidev;
+ int fd;
};
/*
@@ -50,4 +51,20 @@ int relabsd_device_write_evdev_event
int const value
);
+/*
+ * Send an event for each enabled axis, setting it to zero.
+ * An EV_SYN event is sent afterwards.
+ */
+void relabsd_device_set_axes_to_zero
+(
+ const struct relabsd_device * const dev,
+ const struct relabsd_config * const config
+);
+
+int relabsd_device_wait_next_event
+(
+ const struct relabsd_device * const dev,
+ const struct relabsd_config * const config
+);
+
#endif