summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2019-12-23 15:44:19 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2019-12-23 15:44:19 +0100
commit390576c3839ee7abb845e27b7267de45495e6b2f (patch)
treec481c37c868ccc65a3476f60b17369b21a90b79b /include
parent4355548f79375a62bb5e3bb5695190d48e4c0bc3 (diff)
Starting to turn relabsd into a proper daemon...
Diffstat (limited to 'include')
-rw-r--r--include/relabsd/client.h0
-rw-r--r--include/relabsd/config.h0
-rw-r--r--include/relabsd/config/config_file.h100
-rw-r--r--include/relabsd/config/parameters.h0
-rw-r--r--include/relabsd/debug.h145
-rw-r--r--include/relabsd/device/axis.h78
-rw-r--r--include/relabsd/device/physical_device.h50
-rw-r--r--include/relabsd/device/virtual_device.h71
-rw-r--r--include/relabsd/server.h0
-rw-r--r--include/relabsd/util/macro.h6
-rw-r--r--include/relabsd/util/string.h18
11 files changed, 468 insertions, 0 deletions
diff --git a/include/relabsd/client.h b/include/relabsd/client.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/include/relabsd/client.h
diff --git a/include/relabsd/config.h b/include/relabsd/config.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/include/relabsd/config.h
diff --git a/include/relabsd/config/config_file.h b/include/relabsd/config/config_file.h
new file mode 100644
index 0000000..3aa1a13
--- /dev/null
+++ b/include/relabsd/config/config_file.h
@@ -0,0 +1,100 @@
+#ifndef RELABSD_CONFIG_H
+#define RELABSD_CONFIG_H
+
+#include <libevdev/libevdev.h>
+
+#include "axis.h"
+
+/* Maximum length for a axis code. */
+#define RELABSD_CONF_AXIS_CODE_SIZE 2
+
+/* Number of options that can be configured. */
+#define RELABSD_OPTIONS_COUNT 3
+
+enum relabsd_option
+{
+ RELABSD_DIRECT_OPTION,
+ RELABSD_REAL_FUZZ_OPTION,
+ RELABSD_FRAMED_OPTION
+};
+
+struct relabsd_config_axis
+{
+/* relabsd axis properties */
+ int enabled;
+ int previous_value;
+ int option[RELABSD_OPTIONS_COUNT];
+
+/* Absolute axis properties */
+ int min;
+ int max;
+ int fuzz;
+ int flat;
+ int resolution;
+};
+
+/*
+ * There is no relabsd_config_free function, so be careful about using dynamic
+ * memory for input_file or device_name.
+ */
+struct relabsd_config
+{
+ const char * input_file;
+ const char * device_name;
+ struct timeval timeout;
+ int enable_timeout;
+ struct relabsd_config_axis axis[RELABSD_VALID_AXES_COUNT];
+};
+
+/*
+ * Parses the invocation parameters and the config file into 'conf'.
+ *
+ * Returns -1 on (fatal) error,
+ * 0 on successfully parsed config.
+ *
+ * 'conf' does not need to be initialized, as the function will only write to
+ * it.
+ * As one would expect, argc is the number of elements in argv.
+ */
+int relabsd_config_parse
+(
+ struct relabsd_config * const conf,
+ int const argc,
+ char * const * const argv
+);
+
+/*
+ * This function aims at preventing us from emitting values that are incoherent
+ * with our REV_ABS axis configuration, such as the axis' minimum or maximum
+ * values.
+ *
+ * Returns 1 if 'conf' allows the value to be emitted,
+ * 0 if 'conf' wants the event to be transmitted as is.
+ * -1 if 'conf' doesn't want the event to be transmitted.
+ *
+ * If the return value is 0, this function will not have altered the value at
+ * 'value'. Otherwise, this function can have altered it to match its
+ * requirements.
+ * If the return value is either 0 or -1, the 'previous_value' of the axis
+ * has been updated.
+ */
+int relabsd_config_filter
+(
+ struct relabsd_config * const conf,
+ enum relabsd_axis const axis,
+ int * const value
+);
+
+/*
+ * Copies all the ABS event parameters of 'axis' into 'absinfo'.
+ * 'absinfo' does not need to be initialized, as the function will only write to
+ * it.
+ */
+void relabsd_config_get_absinfo
+(
+ const struct relabsd_config * const conf,
+ enum relabsd_axis const axis,
+ struct input_absinfo * const absinfo
+);
+
+#endif
diff --git a/include/relabsd/config/parameters.h b/include/relabsd/config/parameters.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/include/relabsd/config/parameters.h
diff --git a/include/relabsd/debug.h b/include/relabsd/debug.h
new file mode 100644
index 0000000..025c312
--- /dev/null
+++ b/include/relabsd/debug.h
@@ -0,0 +1,145 @@
+#pragma once
+
+#include <stdio.h>
+
+#include <relabsd/util/macro.h>
+
+#ifndef RELABSD_DEBUG_PROGRAM_FLOW
+ #define RELABSD_DEBUG_PROGRAM_FLOW 0
+#endif
+
+#ifndef RELABSD_DEBUG_CONFIG
+ #define RELABSD_DEBUG_CONFIG 0
+#endif
+
+#ifndef RELABSD_DEBUG_REAL_EVENTS
+ #define RELABSD_DEBUG_REAL_EVENTS 0
+#endif
+
+#ifndef RELABSD_DEBUG_VIRTUAL_EVENTS
+ #define RELABSD_DEBUG_VIRTUAL_EVENTS 0
+#endif
+
+#ifndef RELABSD_ENABLE_WARNINGS_OUTPUT
+#define RELABSD_ENABLE_WARNINGS_OUTPUT 1
+#endif
+#ifndef RELABSD_ENABLE_RUNTIME_ERRORS_OUTPUT
+#define RELABSD_ENABLE_RUNTIME_ERRORS_OUTPUT 1
+#endif
+#ifndef RELABSD_ENABLE_PROGRAMMING_ERRORS_OUTPUT
+#define RELABSD_ENABLE_PROGRAMMING_ERRORS_OUTPUT 1
+#endif
+#ifndef RELABSD_ENABLE_FATAL_ERROR_OUTPUT
+#define RELABSD_ENABLE_FATAL_ERROR_OUTPUT 1
+#endif
+
+#ifdef RELABSD_ENABLE_ERROR_LOCATION
+ #define RELABSD_LOCATION "[" __FILE__ "][" RELABSD_TO_STRING(__LINE__) "]"
+#else
+ #define RELABSD_LOCATION ""
+#endif
+
+#define RELABSD_PRINT_STDERR(symbol, str, ...)\
+ fprintf(stderr, "[" symbol "]" RELABSD_LOCATION " " str "\n", __VA_ARGS__);
+
+/*
+ * Given that we use preprocessor contants as flags, we can expect the compilers
+ * to remove the test condition for disabled flags. No need to be shy about
+ * allowing many debug options.
+ */
+
+#define RELABSD_DEBUG(flag, str, ...)\
+ RELABSD_ISOLATE\
+ (\
+ if (flag)\
+ {\
+ RELABSD_PRINT_STDERR("D", str, __VA_ARGS__);\
+ }\
+ )
+
+
+#define RELABSD_WARNING(str, ...)\
+ RELABSD_ISOLATE\
+ (\
+ if (RELABSD_ENABLE_WARNINGS_OUTPUT)\
+ {\
+ RELABSD_PRINT_STDERR("W", str, __VA_ARGS__);\
+ }\
+ )
+
+#define RELABSD_ERROR(str, ...)\
+ RELABSD_ISOLATE\
+ (\
+ if (RELABSD_ENABLE_RUNTIME_ERRORS_OUTPUT)\
+ {\
+ RELABSD_PRINT_STDERR("E", str, __VA_ARGS__);\
+ }\
+ )
+
+#define RELABSD_PROG_ERROR(str, ...)\
+ RELABSD_ISOLATE\
+ (\
+ if (RELABSD_ENABLE_PROGRAMMING_ERRORS_OUTPUT)\
+ {\
+ RELABSD_PRINT_STDERR("P", str, __VA_ARGS__);\
+ }\
+ )
+
+#define RELABSD_FATAL(str, ...)\
+ RELABSD_ISOLATE\
+ (\
+ if (RELABSD_ENABLE_FATAL_ERROR_OUTPUT)\
+ {\
+ RELABSD_PRINT_STDERR("F", str, __VA_ARGS__);\
+ }\
+ )
+
+/* For outputs without dynamic content (static). ******************************/
+
+#define RELABSD_PRINT_S_STDERR(symbol, str)\
+ fprintf(stderr, "[" symbol "]" RELABSD_LOCATION " " str "\n");
+
+#define RELABSD_S_DEBUG(flag, str)\
+ RELABSD_ISOLATE\
+ (\
+ if (flag)\
+ {\
+ RELABSD_PRINT_S_STDERR("D", str);\
+ }\
+ )
+
+#define RELABSD_S_WARNING(str)\
+ RELABSD_ISOLATE\
+ (\
+ if (RELABSD_ENABLE_WARNINGS_OUTPUT)\
+ {\
+ RELABSD_PRINT_S_STDERR("W", str);\
+ }\
+ )
+
+#define RELABSD_S_ERROR(str)\
+ RELABSD_ISOLATE\
+ (\
+ if (RELABSD_ENABLE_RUNTIME_ERRORS_OUTPUT)\
+ {\
+ RELABSD_PRINT_S_STDERR("E", str);\
+ }\
+ )
+
+#define RELABSD_S_PROG_ERROR(str)\
+ RELABSD_ISOLATE\
+ (\
+ if (RELABSD_ENABLE_PROGRAMMING_ERRORS_OUTPUT)\
+ {\
+ RELABSD_PRINT_S_STDERR("P", str);\
+ }\
+ )
+
+#define RELABSD_S_FATAL(str)\
+ RELABSD_ISOLATE\
+ (\
+ if (RELABSD_ENABLE_FATAL_ERROR_OUTPUT)\
+ {\
+ RELABSD_PRINT_S_STDERR("F", str);\
+ }\
+ )
diff --git a/include/relabsd/device/axis.h b/include/relabsd/device/axis.h
new file mode 100644
index 0000000..3829b51
--- /dev/null
+++ b/include/relabsd/device/axis.h
@@ -0,0 +1,78 @@
+#pragma once
+
+/* Number of axes that can be configured. */
+#define RELABSD_AXIS_AXES_COUNT 8
+
+enum relabsd_axis_name
+{
+ RELABSD_X,
+ RELABSD_Y,
+ RELABSD_Z,
+ RELABSD_RX,
+ RELABSD_RY,
+ RELABSD_RZ,
+ RELABSD_WHEEL,
+ RELABSD_MISC,
+ RELABSD_UNKNOWN
+};
+
+struct relabsd_axis
+{
+ int min;
+ int max;
+ int fuzz;
+ int flat;
+ int resolution;
+ int flags;
+};
+
+/*
+ * Gives the relabsd_axis and EV_ABS event code equivalent to an EV_REL event
+ * code.
+ * If the returned relabsd_axis is RELABSD_UNKNOWN, no value is inserted into
+ * 'abs_code'.
+ */
+enum relabsd_axis_name relabsd_axis_name_and_evdev_abs_from_evdev_rel
+(
+ const unsigned int rel_code,
+ unsigned int abs_code [const restrict static 1]
+);
+
+/*
+ * Returns the EV_REL/EV_ABS equivalent of 'e'.
+ * There is no equivalent for RELABSD_UNKNOWN, so 'e' is forbidden from
+ * taking this value.
+ */
+unsigned int relabsd_axis_name_to_evdev_rel (const enum relabsd_axis_name e);
+unsigned int relabsd_axis_name_to_evdev_abs (const enum relabsd_axis_name e);
+
+/*
+ * Returns the relabsd_axis equivalent of a EV_REL/EV_ABS code.
+ */
+enum relabsd_axis_name relabsd_axis_name_from_evdev_rel
+(
+ const unsigned int rel
+);
+
+enum relabsd_axis_name relabsd_axis_name_from_evdev_abs
+(
+ const unsigned int abs
+);
+
+/*
+ * Returns the relabsd_axis whose name is 'name', according to the configuration
+ * file syntax.
+ * RELABSD_UNKNOWN is returned for any name that didn't match any other
+ * possibility.
+ */
+enum relabsd_axis_name relabsd_axis_parse_name
+(
+ const char name [const restrict static 1]
+);
+
+/*
+ * Gives an string representation of an relabsd_axis.
+ * "??" is returned for RELABSD_UNKNOWN.
+ * Returned values should be coherent with the configuration file syntax.
+ */
+const char * relabsd_axis_name_to_string (const enum relabsd_axis_name e);
diff --git a/include/relabsd/device/physical_device.h b/include/relabsd/device/physical_device.h
new file mode 100644
index 0000000..17866f7
--- /dev/null
+++ b/include/relabsd/device/physical_device.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <libevdev/libevdev.h>
+
+struct relabsd_input
+{
+ struct libevdev * dev;
+ int fd;
+ int timed_out;
+};
+
+/*
+ * Returns -1 on (fatal) error,
+ * 0 on success.
+ *
+ * 'input' does not need to be initialized, as the function will to that for
+ * you (on success).
+ * On success, 'input' will need to be closed.
+ */
+int relabsd_input_open
+(
+ struct relabsd_input * const input,
+ const struct relabsd_config * const conf
+);
+
+void relabsd_input_close (const struct relabsd_input * const input);
+
+/*
+ * Returns -1 on (warning) error,
+ * 0 on successful read.
+ *
+ * The 'input_*' parameters do not need to be initialized, as the function will
+ * do that for you (on success).
+ * Requires 'input' to be open.
+ */
+int relabsd_input_read
+(
+ const struct relabsd_input * const input,
+ unsigned int * const input_type,
+ unsigned int * const input_code,
+ int * const input_value
+);
+
+int relabsd_input_wait_for_next_event
+(
+ const struct relabsd_input * const input,
+ const struct relabsd_config * const config
+);
+
+#endif
diff --git a/include/relabsd/device/virtual_device.h b/include/relabsd/device/virtual_device.h
new file mode 100644
index 0000000..d398c7f
--- /dev/null
+++ b/include/relabsd/device/virtual_device.h
@@ -0,0 +1,71 @@
+#pragma once
+
+#include <libevdev/libevdev.h>
+#include <libevdev/libevdev-uinput.h>
+
+/*
+ LIBEVDEV_UINPUT_OPEN_MANAGED is not defined on my machines.
+ It is not my place to define it, so I'll avoid the issue by defining my own
+ constant.
+*/
+#ifndef LIBEVDEV_UINPUT_OPEN_MANAGED
+ #pragma message "[WARNING] libevdev did not define " \
+ "LIBEVDEV_UINPUT_OPEN_MANAGED, using value '-2' instead."
+ #define RELABSD_UINPUT_OPEN_MANAGED -2
+#else
+ #define RELABSD_UINPUT_OPEN_MANAGED LIBEVDEV_UINPUT_OPEN_MANAGED
+#endif
+
+struct relabsd_device
+{
+ struct libevdev * dev;
+ struct libevdev_uinput * uidev;
+};
+
+/*
+ * - Clones the (real) input device.
+ * - Adds ABS event support to the clone.
+ * - Adds the ABS axis described in 'config' to the clone (overwriting if
+ * needed).
+ * - Disables the associated REL events from the clone.
+ *
+ * Returns -1 on (fatal) error,
+ * 0 on success.
+ *
+ * 'dev' does not need to be initialized, as the function will to that for you
+ * (on success).
+ * On success, 'dev' will need to be closed.
+ * This opens the (real) input device in read only mode to copy its properties.
+ */
+int relabsd_device_create
+(
+ struct relabsd_device * const dev,
+ const struct relabsd_config * const config
+);
+
+void relabsd_device_destroy (const struct relabsd_device * const dev);
+
+/*
+ * Write an event to 'dev'. At this time, every event written to 'dev' is
+ * followed by an EV_SYN event.
+ *
+ * Returns 0 if both the event and the EV_SYN were successful,
+ * -1 if either failed.
+ */
+int relabsd_device_write_evdev_event
+(
+ const struct relabsd_device * const dev,
+ unsigned int const type,
+ unsigned int const code,
+ 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
+);
diff --git a/include/relabsd/server.h b/include/relabsd/server.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/include/relabsd/server.h
diff --git a/include/relabsd/util/macro.h b/include/relabsd/util/macro.h
new file mode 100644
index 0000000..b9adb5e
--- /dev/null
+++ b/include/relabsd/util/macro.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#define RELABSD_ISOLATE(a) do {a} while (0)
+
+#define RELABSD__TO_STRING(x) #x
+#define RELABSD_TO_STRING(x) RELABSD__TO_STRING(x)
diff --git a/include/relabsd/util/string.h b/include/relabsd/util/string.h
new file mode 100644
index 0000000..d632720
--- /dev/null
+++ b/include/relabsd/util/string.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <string.h>
+
+#include <relabsd/util/macro.h>
+
+/* strncmp stops at '\0' and strlen does not count '\0'. */
+#define RELABSD_IS_PREFIX(a, b) (strncmp(a, b, strlen(a)) == 0)
+
+#define RELABSD_STRING_EQUALS(a, b) (strcmp(a, b) == 0)
+
+int relabsd_util_parse_int
+(
+ const char string [const restrict static 1],
+ const int min,
+ const int max,
+ int output [const restrict static 1]
+);