From 63016ce5c71019de315434de3e91adbf535d4986 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Tue, 31 Dec 2019 15:51:05 +0100 Subject: Still working on it... --- include/relabsd/client_types.h | 0 include/relabsd/config/config_file.h | 8 - include/relabsd/config/parameters.h | 57 ++++ include/relabsd/config/parameters_types.h | 22 ++ include/relabsd/device/axis.h | 26 +- include/relabsd/device/axis_types.h | 43 +++ include/relabsd/device/physical_device.h | 9 +- include/relabsd/device/physical_device_types.h | 10 + include/relabsd/device/virtual_device.h | 22 +- include/relabsd/device/virtual_device_types.h | 23 ++ include/relabsd/server_types.h | 0 src/client.c | 25 +- src/config/config_file.c | 149 ++------- src/config/parameters.c | 344 --------------------- src/config/parameters/parameters.c | 406 +++++++++++++++++++++++++ src/config/parameters/parameters_accessors.c | 60 ++++ src/device/axis/axis.c | 34 +++ src/device/axis/axis_filter.c | 131 ++++++++ src/server/server.c | 45 +-- 19 files changed, 847 insertions(+), 567 deletions(-) create mode 100644 include/relabsd/client_types.h create mode 100644 include/relabsd/config/parameters_types.h create mode 100644 include/relabsd/device/axis_types.h create mode 100644 include/relabsd/device/physical_device_types.h create mode 100644 include/relabsd/device/virtual_device_types.h create mode 100644 include/relabsd/server_types.h delete mode 100644 src/config/parameters.c create mode 100644 src/config/parameters/parameters.c create mode 100644 src/config/parameters/parameters_accessors.c create mode 100644 src/device/axis/axis.c create mode 100644 src/device/axis/axis_filter.c diff --git a/include/relabsd/client_types.h b/include/relabsd/client_types.h new file mode 100644 index 0000000..e69de29 diff --git a/include/relabsd/config/config_file.h b/include/relabsd/config/config_file.h index 3aa1a13..f904182 100644 --- a/include/relabsd/config/config_file.h +++ b/include/relabsd/config/config_file.h @@ -3,20 +3,12 @@ #include -#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 { diff --git a/include/relabsd/config/parameters.h b/include/relabsd/config/parameters.h index e69de29..5a12be4 100644 --- a/include/relabsd/config/parameters.h +++ b/include/relabsd/config/parameters.h @@ -0,0 +1,57 @@ +#pragma once + +#include + +/**** Utility *****************************************************************/ +int relabsd_parameters_parse_execution_mode +( + const int argc, + const char * const argv [const restrict static argc], + struct relabsd_parameters parameters [const restrict static 1] +); + +int relabsd_parameters_parse_options +( + const int argc, + const char * const argv [const restrict static argc], + struct relabsd_parameters parameters [const restrict static 1] +); + +int relabsd_parameters_argument_count_for +( + const char option [const restrict static 1], + int result [const restrict static 1] +); + +void relabsd_parameters_print_usage (const char exec [const restrict static 1]); + +/**** Accessors ***************************************************************/ +void relabsd_parameters_initialize_options +( + const struct relabsd_parameters parameters [const restrict static 1] +); + +int relabsd_parameters_get_run_as_daemon +( + const struct relabsd_parameters parameters [const restrict static 1] +); + +const char * relabsd_parameters_get_communication_node_name +( + const struct relabsd_parameters parameters [const restrict static 1] +); + +const char * relabsd_parameters_get_device_name +( + const struct relabsd_parameters parameters [const restrict static 1] +); + +const char * relabsd_parameters_get_physical_device_file_name +( + const struct relabsd_parameters parameters [const restrict static 1] +); + +enum relabsd_parameters_run_mode relabsd_parameters_get_execution_mode +( + const struct relabsd_parameters parameters [const restrict static 1] +); diff --git a/include/relabsd/config/parameters_types.h b/include/relabsd/config/parameters_types.h new file mode 100644 index 0000000..ab07bfa --- /dev/null +++ b/include/relabsd/config/parameters_types.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +enum relabsd_parameters_run_mode +{ + RELABSD_PARAMETERS_CLIENT_MODE, + RELABSD_PARAMETERS_SERVER_MODE, + RELABSD_PARAMETERS_COMPATIBILITY_TEST_MODE +}; + +struct relabsd_parameters +{ + int read_argc; + enum relabsd_parameters_run_mode mode; + int run_as_daemon; + const char * communication_node_name; + const char * device_name; + const char * physical_device_file_name; + const char * configuration_file; + struct relabsd_axis axes[RELABSD_AXIS_VALID_AXES_COUNT] +}; diff --git a/include/relabsd/device/axis.h b/include/relabsd/device/axis.h index 3829b51..fee8522 100644 --- a/include/relabsd/device/axis.h +++ b/include/relabsd/device/axis.h @@ -1,30 +1,6 @@ #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; -}; +#include /* * Gives the relabsd_axis and EV_ABS event code equivalent to an EV_REL event diff --git a/include/relabsd/device/axis_types.h b/include/relabsd/device/axis_types.h new file mode 100644 index 0000000..5ae0145 --- /dev/null +++ b/include/relabsd/device/axis_types.h @@ -0,0 +1,43 @@ +#pragma once + +/* Number of axes that can be configured. */ +#define RELABSD_AXIS_VALID_AXES_COUNT 8 +#define RELABSD_AXIS_FLAGS_COUNT 3 + +/* + * C enumerations are always int, and the standard does specify that it starts + * at zero and increases from there, unless otherwise specified in the + * declaration. + */ +enum relabsd_axis_name +{ + RELABSD_X, + RELABSD_Y, + RELABSD_Z, + RELABSD_RX, + RELABSD_RY, + RELABSD_RZ, + RELABSD_WHEEL, + RELABSD_MISC, + RELABSD_UNKNOWN +}; + +enum relabsd_axis_flag +{ + RELABSD_DIRECT, + RELABSD_REAL_FUZZ, + RELABSD_FRAMED +}; + +struct relabsd_axis +{ + int min; + int max; + int fuzz; + int flat; + int resolution; + + int is_enabled; + int previous_value; + int flags[RELABSD_AXIS_FLAGS_COUNT]; +}; diff --git a/include/relabsd/device/physical_device.h b/include/relabsd/device/physical_device.h index 6b44802..3e55823 100644 --- a/include/relabsd/device/physical_device.h +++ b/include/relabsd/device/physical_device.h @@ -1,13 +1,6 @@ #pragma once -#include - -struct relabsd_physical_device -{ - struct libevdev * libevdev; - int file; - int timed_out; -}; +#include /* * Returns -1 on (fatal) error, diff --git a/include/relabsd/device/physical_device_types.h b/include/relabsd/device/physical_device_types.h new file mode 100644 index 0000000..0c2cec7 --- /dev/null +++ b/include/relabsd/device/physical_device_types.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +struct relabsd_physical_device +{ + struct libevdev * libevdev; + int file; + int timed_out; +}; diff --git a/include/relabsd/device/virtual_device.h b/include/relabsd/device/virtual_device.h index d398c7f..646422a 100644 --- a/include/relabsd/device/virtual_device.h +++ b/include/relabsd/device/virtual_device.h @@ -1,26 +1,6 @@ #pragma once -#include -#include - -/* - 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; -}; +#include /* * - Clones the (real) input device. diff --git a/include/relabsd/device/virtual_device_types.h b/include/relabsd/device/virtual_device_types.h new file mode 100644 index 0000000..d20fafe --- /dev/null +++ b/include/relabsd/device/virtual_device_types.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +/* + 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; +}; diff --git a/include/relabsd/server_types.h b/include/relabsd/server_types.h new file mode 100644 index 0000000..e69de29 diff --git a/src/client.c b/src/client.c index d03a768..b0459af 100644 --- a/src/client.c +++ b/src/client.c @@ -20,8 +20,8 @@ /******************************************************************************/ static int open_socket ( - FILE * s [const restrict static 1], - const char socket_name [const restrict static 1] + const char socket_name [const restrict static 1], + FILE * s [const restrict static 1] ) { const int old_errno = errno; @@ -113,9 +113,16 @@ static int send_commands // TODO: error } + if (relabsd_parameters_argument_count_for(argv[i], &j) < 0) + { + RELABSD_FATAL("Unknown option '%s'.", argv[i]); + relabsd_parameters_print_usage(argv[0]); + + return -1; + } + for ( - j = relabsd_parameters_argument_count_for(argv[i]), i++; ((j > 0) && (i < argc)); j++, i-- @@ -161,14 +168,22 @@ int relabsd_client ( const int argc, const char * argv [const restrict static argc], - struct relabsd_parameters params [const restrict static 1] + struct relabsd_parameters parameters [const restrict static 1] ) { FILE * socket; RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Started client mode."); - if (open_socket(&socket, relabsd_parameters_get_node(params)) < 0) + if + ( + open_socket + ( + relabsd_parameters_get_communication_node_name(parameters), + &socket + ) + < 0 + ) { return -1; } diff --git a/src/config/config_file.c b/src/config/config_file.c index ad060a5..2e5ef0c 100644 --- a/src/config/config_file.c +++ b/src/config/config_file.c @@ -1,3 +1,4 @@ +/**** POSIX *******************************************************************/ #include #include #include @@ -6,38 +7,27 @@ #include -#include "error.h" -#include "pervasive.h" -#include "axis.h" -#include "config.h" +/**** RELABSD *****************************************************************/ +#include + +#include #ifndef RELABSD_OPTION_MAX_SIZE #define RELABSD_OPTION_MAX_SIZE 64 #endif -/* - * "errno is never set to zero by any system call or library function." - * This file makes use of this, by setting it to zero and checking if - * it was modified after calling an function (I'm guessing this is common - * practice, but I think it's worth explaining). - * Following the principle of least astonishment, if a function sets errno to - * zero, it will not return before setting it back either to its previous - * value or to a arbitrary nonzero value. - */ +/******************************************************************************/ +/**** LOCAL FUNCTIONS *********************************************************/ +/******************************************************************************/ /* * Returns -1 on (fatal) error, * 0 on EOF, * 1 on newline. */ -static int reach_next_line_or_eof (FILE * const f) +static int reach_next_line_or_eof (FILE f [const restrict static 1]) { - int prev_errno; char c; - prev_errno = errno; - - errno = 0; - c = (char) getc(f); while ((c != '\n') && c != EOF) @@ -45,21 +35,22 @@ static int reach_next_line_or_eof (FILE * const f) c = (char) getc(f); } - if (errno != 0) + if (ferror(f)) { - RELABSD_FATAL + /* + * The 'ferror' function's manual specifically states that it does not + * sets errno. There is no mention of errno in the 'getc' function's + * either, so I am assuming that errno cannot be used to indicate the + * error. + */ + RELABSD_S_FATAL ( - "[CONFIG] Error while attempting to reach EOF or next line: %s.", - strerror(errno) + "[CONFIG] Error while attempting to reach EOF or next line: %s." ); - errno = prev_errno; - return -1; } - errno = prev_errno; - if (c == EOF) { return 0; @@ -643,6 +634,7 @@ static int parse_options return 0; } + int relabsd_config_parse ( struct relabsd_config * const conf, @@ -681,106 +673,6 @@ int relabsd_config_parse return 0; } -static int direct_filter -( - struct relabsd_config_axis * const axis, - int * const value -) -{ - if (abs(*value - axis->previous_value) <= axis->fuzz) - { - if (axis->option[RELABSD_REAL_FUZZ_OPTION]) - { - axis->previous_value = *value; - } - - return -1; - } - - if (*value < axis->min) - { - *value = axis->min; - } - else if (*value > axis->max) - { - *value = axis->max; - } - else if (abs(*value) <= axis->flat) - { - *value = 0; - } - - if (*value == axis->previous_value) - { - return -1; - } - - axis->previous_value = *value; - - return 1; -} - -static int rel_to_abs_filter -( - struct relabsd_config_axis * const axis, - int * const value -) -{ - long int guard; - - guard = (((long int) axis->previous_value) + ((long int) *value)); - - if (guard < ((long int) INT_MIN)) - { - guard = ((long int) INT_MIN); - } - else if (guard > ((long int) INT_MAX)) - { - guard = ((long int) INT_MAX); - } - - *value = (int) guard; - - if (axis->option[RELABSD_FRAMED_OPTION]) - { - if (*value < axis->min) - { - *value = axis->min; - } - else if (*value > axis->max) - { - *value = axis->max; - } - - if (*value == axis->previous_value) - { - return 0; - } - - axis->previous_value = *value; - - return 1; - } - else - { - if (*value == axis->previous_value) - { - return 0; - } - - axis->previous_value = *value; - - if ((*value < axis->min) || (*value > axis->max)) - { - return 0; - } - else - { - return 1; - } - } -} - int relabsd_config_filter ( struct relabsd_config * const conf, @@ -805,6 +697,9 @@ int relabsd_config_filter } } +/******************************************************************************/ +/**** EXPORTED FUNCTIONS ******************************************************/ +/******************************************************************************/ void relabsd_config_get_absinfo ( const struct relabsd_config * const conf, diff --git a/src/config/parameters.c b/src/config/parameters.c deleted file mode 100644 index 5e79fa2..0000000 --- a/src/config/parameters.c +++ /dev/null @@ -1,344 +0,0 @@ -/**** POSIX *******************************************************************/ -#include - -/**** RELABSD *****************************************************************/ -#include -#include - -#include - -#include - -#include - -/******************************************************************************/ -/**** LOCAL FUNCTIONS *********************************************************/ -/******************************************************************************/ -static void print_usage (const char exec [const restrict static 1]) -{ - printf - ( - "USAGE: %s [