| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-01-03 03:50:24 +0100 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-01-03 03:50:24 +0100 |
| commit | a4841776b6e1751232d46482731836a7c17b896f (patch) | |
| tree | bd818e08f2c145dc1629d0d568feb7106ff274d8 | |
| parent | 63016ce5c71019de315434de3e91adbf535d4986 (diff) | |
Still working on it...
| -rw-r--r-- | include/relabsd/config.h | 5 | ||||
| -rw-r--r-- | include/relabsd/config/parameters.h | 2 | ||||
| -rw-r--r-- | include/relabsd/config/parameters_types.h | 3 | ||||
| -rw-r--r-- | include/relabsd/device/axis.h | 13 | ||||
| -rw-r--r-- | include/relabsd/util/string.h | 7 | ||||
| -rw-r--r-- | src/config/config_file.c | 716 | ||||
| -rw-r--r-- | src/config/parameters/parameters.c | 44 | ||||
| -rw-r--r-- | src/config/parameters/parameters_accessors.c | 6 | ||||
| -rw-r--r-- | src/config/parameters/parse_config_file.c | 348 | ||||
| -rw-r--r-- | src/device/axis/axis.c | 15 | ||||
| -rw-r--r-- | src/device/axis/axis_filter.c | 20 | ||||
| -rw-r--r-- | src/device/axis/axis_option.c | 71 | ||||
| -rw-r--r-- | src/util/string.c | 35 |
13 files changed, 538 insertions, 747 deletions
diff --git a/include/relabsd/config.h b/include/relabsd/config.h index e69de29..6558ebb 100644 --- a/include/relabsd/config.h +++ b/include/relabsd/config.h @@ -0,0 +1,5 @@ +#pragma once + +#ifndef RELABSD_CONF_AXIS_CODE_SIZE +#define RELABSD_CONF_AXIS_CODE_SIZE 2 +#endif diff --git a/include/relabsd/config/parameters.h b/include/relabsd/config/parameters.h index 5a12be4..e1c67a4 100644 --- a/include/relabsd/config/parameters.h +++ b/include/relabsd/config/parameters.h @@ -28,7 +28,7 @@ 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] + struct relabsd_parameters parameters [const restrict static 1] ); int relabsd_parameters_get_run_as_daemon diff --git a/include/relabsd/config/parameters_types.h b/include/relabsd/config/parameters_types.h index ab07bfa..e6c4009 100644 --- a/include/relabsd/config/parameters_types.h +++ b/include/relabsd/config/parameters_types.h @@ -18,5 +18,6 @@ struct relabsd_parameters const char * device_name; const char * physical_device_file_name; const char * configuration_file; - struct relabsd_axis axes[RELABSD_AXIS_VALID_AXES_COUNT] + int timeout; /* TODO: use time structure + enabled flag */ + struct relabsd_axis axes[RELABSD_AXIS_VALID_AXES_COUNT]; }; diff --git a/include/relabsd/device/axis.h b/include/relabsd/device/axis.h index fee8522..815bbb8 100644 --- a/include/relabsd/device/axis.h +++ b/include/relabsd/device/axis.h @@ -52,3 +52,16 @@ enum relabsd_axis_name relabsd_axis_parse_name * Returned values should be coherent with the configuration file syntax. */ const char * relabsd_axis_name_to_string (const enum relabsd_axis_name e); + +/* + * Returns -1 if the option was discarded (an error has been reported), + * 0 if the option was successfully parsed. + */ +int relabsd_axis_enable_option_from_name +( + const char option_name [const restrict static 1], + const char axis_name [const restrict static 1], + struct relabsd_axis axis [const restrict static 1] +); + +void relabsd_axis_enable (struct relabsd_axis axis [const restrict static 1]); diff --git a/include/relabsd/util/string.h b/include/relabsd/util/string.h index d632720..a0ffde3 100644 --- a/include/relabsd/util/string.h +++ b/include/relabsd/util/string.h @@ -16,3 +16,10 @@ int relabsd_util_parse_int const int max, int output [const restrict static 1] ); + +/* + * Returns -1 on error, + * 0 on EOF, + * 1 on newline. + */ +int relabsd_util_reach_next_line_or_eof (FILE f [const restrict static 1]); diff --git a/src/config/config_file.c b/src/config/config_file.c deleted file mode 100644 index 2e5ef0c..0000000 --- a/src/config/config_file.c +++ /dev/null @@ -1,716 +0,0 @@ -/**** POSIX *******************************************************************/ -#include <errno.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> -#include <limits.h> - -#include <sys/time.h> - -/**** RELABSD *****************************************************************/ -#include <relabsd/debug.h> - -#include <relabsd/config/config_file.h> - -#ifndef RELABSD_OPTION_MAX_SIZE - #define RELABSD_OPTION_MAX_SIZE 64 -#endif - -/******************************************************************************/ -/**** LOCAL FUNCTIONS *********************************************************/ -/******************************************************************************/ -/* - * Returns -1 on (fatal) error, - * 0 on EOF, - * 1 on newline. - */ -static int reach_next_line_or_eof (FILE f [const restrict static 1]) -{ - char c; - - c = (char) getc(f); - - while ((c != '\n') && c != EOF) - { - c = (char) getc(f); - } - - if (ferror(f)) - { - /* - * 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." - ); - - return -1; - } - - if (c == EOF) - { - return 0; - } - - return 1; -} - -/* - * Returns -1 if the option was discarded (an error has been reported), - * 0 if the option was successfully parsed. - * - * ('length' - 1) is the number of relevant characters in 'name'. - * 'name' must support 'length' characters. - * name[length] will be set to \0, so it does not need to be when calling - * this function. - */ -static int parse_option -( - struct relabsd_config * const conf, - enum relabsd_axis const axis, - char * const name, - int const length -) -{ - name[length] = '\0'; - - if (strcmp(name, "direct") == 0) - { - conf->axis[axis].option[RELABSD_DIRECT_OPTION] = 1; - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Axis '%s' enabled option 'direct'.", - relabsd_axis_to_name(axis) - ); - - if (conf->axis[axis].option[RELABSD_FRAMED_OPTION]) - { - RELABSD_WARNING - ( - "[CONFIG] Axis '%s': using option 'direct' discards option" - "'framed'.", - relabsd_axis_to_name(axis) - ); - } - } - else if (strcmp(name, "real_fuzz") == 0) - { - conf->axis[axis].option[RELABSD_REAL_FUZZ_OPTION] = 1; - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Axis '%s' enabled option 'real_fuzz'.", - relabsd_axis_to_name(axis) - ); - } - else if (strcmp(name, "framed") == 0) - { - conf->axis[axis].option[RELABSD_FRAMED_OPTION] = 1; - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Axis '%s' enabled option 'framed'.", - relabsd_axis_to_name(axis) - ); - - if (conf->axis[axis].option[RELABSD_DIRECT_OPTION]) - { - RELABSD_WARNING - ( - "[CONFIG] Axis '%s': using option 'direct' discards option" - "'framed'.", - relabsd_axis_to_name(axis) - ); - } - } - else - { - RELABSD_ERROR - ( - "[CONFIG] Unknown option '%s' for axis '%s'.", - name, - relabsd_axis_to_name(axis) - ); - - return -1; - } - - return 0; -} - -/* - * Returns -1 on error, - * 0 on EOF, - * 1 on newline. - */ -static int read_axis_options -( - struct relabsd_config * const conf, - FILE * const f, - enum relabsd_axis const axis -) -{ - char option[(RELABSD_OPTION_MAX_SIZE + 1)]; - int i, prev_errno; - char c; - - option[RELABSD_OPTION_MAX_SIZE] = '\0'; - - prev_errno = errno; - - errno = 0; - - memset(conf->axis[axis].option, 0, RELABSD_OPTIONS_COUNT * sizeof(int)); - - i = 0; - - while (i <= RELABSD_OPTION_MAX_SIZE) - { - c = (char) getc(f); - - if ((errno != 0) && (c == EOF)) - { - RELABSD_FATAL - ( - "[CONFIG] Reading error while parsing option name (axis '%s'): %s.", - relabsd_axis_to_name(axis), - strerror(errno) - ); - - errno = prev_errno; - - return -1; - } - - switch (c) - { - case ' ': - case '\t': - break; - - case ',': - /* We parsed a new option and there is a least another. */ - parse_option(conf, axis, option, i); - - i = 0; - - break; - - case '\n': - parse_option(conf, axis, option, i); - errno = prev_errno; - - return 1; - - case EOF: - parse_option(conf, axis, option, i); - errno = prev_errno; - - return 0; - - default: - option[i] = c; - i++; - - break; - } - } - - RELABSD_FATAL - ( - "[CONFIG] Option name '%s[...]' (axis '%s') is too long (%d chars max).", - option, - relabsd_axis_to_name(axis), - RELABSD_OPTION_MAX_SIZE - ); - - return -1; -} - -static int parse_timeout_option -( - struct relabsd_config * const conf, - const char * const param -) -{ - int timeout_msec; - const int prev_errno = errno; - - conf->enable_timeout = 1; - - errno = 0; - - timeout_msec = atoi(param); - - if (timeout_msec <= 0) - { - RELABSD_FATAL - ( - "Illegal value for timeout \"%d\": accepted range is [1, %d].", - timeout_msec, - INT_MAX - ); - - return -1; - } - - 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) - * ((suseconds_t) 1000) - ); - - return 0; -} - -/* - * Returns -1 on (fatal) error, - * 0 on succes. - */ -static int parse_axis_configuration_line -( - struct relabsd_config * const conf, - FILE * const f, - const char * const buffer -) -{ - int valc, prev_errno; - enum relabsd_axis axis; - - axis = relabsd_axis_from_name(buffer); - - if (axis == RELABSD_UNKNOWN) - { - RELABSD_FATAL - ( - "[CONFIG] Unknown axis '%s'.", - buffer - ); - - return -1; - } - - prev_errno = errno; - errno = 0; - - valc = - fscanf - ( - f, - "%d %d %d %d %d", - &(conf->axis[axis].min), - &(conf->axis[axis].max), - &(conf->axis[axis].fuzz), - &(conf->axis[axis].flat), - &(conf->axis[axis].resolution) - ); - - if (valc == EOF) - { - if (errno == 0) - { - RELABSD_FATAL - ( - "[CONFIG] Unexpected end of file while reading axis '%s'.", - buffer - ); - } - else - { - RELABSD_FATAL - ( - "[CONFIG] An error occured while reading axis '%s': %s.", - buffer, - strerror(errno) - ); - } - - errno = prev_errno; - - return -1; - } - else if (valc < 5) - { - RELABSD_FATAL - ( - "[CONFIG] Invalid parameter count for axis '%s'.", - buffer - ); - - errno = prev_errno; - - return -1; - } - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Axis '%s': {min = %d; max = %d; fuzz = %d; flat = %d; resolution = %d}", - buffer, - conf->axis[axis].min, - conf->axis[axis].max, - conf->axis[axis].fuzz, - conf->axis[axis].flat, - conf->axis[axis].resolution - ); - - errno = prev_errno; - - conf->axis[axis].enabled = 1; - conf->axis[axis].previous_value = 0; - - return read_axis_options(conf, f, axis); -} - -/* - * Returns -1 on (fatal) error, - * 0 on EOF, - * 1 on newline. - */ -static int read_config_line -( - struct relabsd_config * const conf, - FILE * const f, - const char * const prefix -) -{ - if (!RELABSD_IS_PREFIX("#", prefix)) - { - return parse_axis_configuration_line(conf, f, prefix); - } - - return reach_next_line_or_eof(f); -} - -/* - * Returns -1 on (fatal) error, - * 0 on success. - */ -static int read_config_file -( - struct relabsd_config * const conf, - char * const filename -) -{ - FILE * f; - char buffer[(RELABSD_CONF_AXIS_CODE_SIZE + 1)]; - int continue_reading, prev_errno; - - buffer[RELABSD_CONF_AXIS_CODE_SIZE] = '\0'; - - f = fopen(filename, "r"); - - if (f == (FILE *) NULL) - { - RELABSD_FATAL - ( - "[CONFIG] Could not open file: %s.", - strerror(errno) - ); - - return -1; - } - - - prev_errno = errno; - errno = 0; - - continue_reading = 1; - - while - ( - (continue_reading == 1) - && - ( - fscanf - ( - f, - "%" RELABSD_TO_STRING(RELABSD_CONF_AXIS_CODE_SIZE) "s", - buffer - ) - != EOF - ) - ) - { - switch (read_config_line(conf, f, buffer)) - { - case 1: - /* Everything is going well. */ - break; - - case 0: - /* EOF reached. */ - continue_reading = 0; - break; - - case -1: - /* A fatal error occured. */ - errno = prev_errno; - - fclose(f); - return -1; - } - } - - if (errno != 0) - { - /* An error happened in the while loop condition. */ - RELABSD_FATAL - ( - "[CONFIG] Error while reading file: %s, last read '%s'.", - strerror(errno), - buffer - ); - - errno = prev_errno; - - fclose(f); - - return -1; - } - - errno = prev_errno; - - fclose(f); - - return 0; -} - -static void print_usage -( - const char * const exec -) -{ - RELABSD_FATAL - ( - "USAGE: %s input_device config_file [<OPTION>+]\n" - "<OPTION>:\n" - "\t [-n | --name] <relabsd_device_name>: Names the virtual device.\n" - "\t [-t | --timeout] <timeout_in_ms>: Resets all enabled axes to zero" - " <timeout_in_ms> milliseconds after the last event. Range for" - " <timeout_in_ms>: [1, %d].\n" - "Alternatively, the previous usage is still supported:\n" - "\t%s input_device config_file [<relabsd_device_name>]\n" - "However, with that usage, <relabsd_device_name> cannot start with '-'.", - exec, - INT_MAX, - exec - ); -} -/* - * Returns -1 on (fatal) error, - * 0 on valid usage. - */ -static int check_usage -( - int const argc, - char * const * const argv -) -{ - if (argc < 3) - { - print_usage(argv[0]); - - return -1; - } - - return 0; -} - -static void init_axes_config (struct relabsd_config * const conf) -{ - int i; - - for (i = RELABSD_VALID_AXES_COUNT; i --> 0;) - { - conf->axis[i].enabled = 0; - } -} - -static int parse_options -( - struct relabsd_config * const conf, - int const argc, - char * const * const argv -) -{ - int i; - - if ((argc == 4) && !RELABSD_IS_PREFIX("-", argv[3])) - { - /* Old usage */ - - RELABSD_S_DEBUG(RELABSD_DEBUG_CONFIG, "Old usage detected."); - - conf->device_name = argv[3]; - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Virtual device name param set to '%s'.", - conf->device_name - ); - - return 0; - } - - conf->device_name = NULL; - conf->enable_timeout = 0; - - for (i = 3; i < argc; ++i) - { - if - ( - RELABSD_STRING_EQUALS("-n", argv[i]) - || RELABSD_STRING_EQUALS("--name", argv[i]) - ) - { - i += 1; - - if (i >= argc) - { - RELABSD_S_FATAL - ( - "Missing value for parameter \"--name\"." - ); - - print_usage(argv[0]); - - return -1; - } - - conf->device_name = argv[i]; - } - else if - ( - RELABSD_STRING_EQUALS("-t", argv[i]) - || RELABSD_STRING_EQUALS("--timeout", argv[i]) - ) - { - i += 1; - - if (i >= argc) - { - RELABSD_S_FATAL - ( - "Missing value for parameter \"--timeout\"." - ); - - print_usage(argv[0]); - - return -1; - } - - if (parse_timeout_option(conf, argv[i]) < 0) - { - return -1; - } - } - else - { - RELABSD_FATAL - ( - "Unknown parameter \"%s\".", - argv[i] - ); - - print_usage(argv[0]); - - return -1; - } - } - - return 0; -} - -int relabsd_config_parse -( - struct relabsd_config * const conf, - int const argc, - char * const * const argv -) -{ - RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Parsing config & params..."); - - if (check_usage(argc, argv) < 0) - { - return -1; - } - - if (parse_options(conf, argc, argv) < 0) - { - return -1; - } - - conf->input_file = argv[1]; - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Using configuration file '%s'.", - conf->input_file - ); - - init_axes_config(conf); - - if (read_config_file(conf, argv[2]) < 0) - { - return -1; - } - - return 0; -} - -int relabsd_config_filter -( - struct relabsd_config * const conf, - enum relabsd_axis const axis, - int * const value -) -{ - if ((axis == RELABSD_UNKNOWN) || !conf->axis[axis].enabled) - { - return 0; - } - - /* TODO: handle conf->axis[axis].resolution */ - - if (conf->axis[axis].option[RELABSD_DIRECT_OPTION]) - { - return direct_filter((conf->axis + axis), value); - } - else - { - return rel_to_abs_filter((conf->axis + axis), value); - } -} - -/******************************************************************************/ -/**** EXPORTED FUNCTIONS ******************************************************/ -/******************************************************************************/ -void relabsd_config_get_absinfo -( - const struct relabsd_config * const conf, - enum relabsd_axis const axis, - struct input_absinfo * const absinfo -) -{ - absinfo->value = (__s32) 0; - absinfo->minimum = (__s32) conf->axis[axis].min; - absinfo->maximum = (__s32) conf->axis[axis].max; - absinfo->fuzz = (__s32) conf->axis[axis].fuzz; - absinfo->flat = (__s32) conf->axis[axis].flat; - absinfo->resolution = (__s32) conf->axis[axis].resolution; -} diff --git a/src/config/parameters/parameters.c b/src/config/parameters/parameters.c index c8db4fa..ac6a790 100644 --- a/src/config/parameters/parameters.c +++ b/src/config/parameters/parameters.c @@ -17,11 +17,11 @@ static int parse_axis ( const int argc, - const char * argv [const restrict static argc], - struct relabsd_axis axes [const restrict static RELABSD_AXIS_AXES_COUNT] + const char * const argv [const restrict static argc], + struct relabsd_axis axes [const static RELABSD_AXIS_VALID_AXES_COUNT] ) { - enum relabsd_axis_name axis_name; + enum relabsd_axis_name axis_index; struct relabsd_axis *axis; if (argc < 7) @@ -31,7 +31,7 @@ static int parse_axis return -1; } - axis_index = relabsd_axis_from_name(argv[0]); + axis_index = relabsd_axis_parse_name(argv[0]); if (axis_index == RELABSD_UNKNOWN) { @@ -90,7 +90,7 @@ static int parse_axis int relabsd_parameters_parse_execution_mode ( const int argc, - const char * argv [const restrict static argc], + const char * const argv [const restrict static argc], struct relabsd_parameters parameters [const restrict static 1] ) { @@ -110,7 +110,7 @@ int relabsd_parameters_parse_execution_mode ) { parameters->mode = RELABSD_PARAMETERS_COMPATIBILITY_TEST_MODE; - parameters->physical_device_name = argv[2]; + parameters->physical_device_file_name = argv[2]; parameters->read_argc = 2; } else if @@ -121,7 +121,7 @@ int relabsd_parameters_parse_execution_mode { parameters->mode = RELABSD_PARAMETERS_CLIENT_MODE; parameters->communication_node_name = argv[2]; - parameters->physical_device_name = (const char *) NULL; + parameters->physical_device_file_name = (const char *) NULL; parameters->read_argc = 2; } else if @@ -132,7 +132,7 @@ int relabsd_parameters_parse_execution_mode { parameters->mode = RELABSD_PARAMETERS_SERVER_MODE; parameters->communication_node_name = argv[2]; - parameters->physical_device_name = argv[3]; + parameters->physical_device_file_name = argv[3]; parameters->read_argc = 3; } else if @@ -143,7 +143,7 @@ int relabsd_parameters_parse_execution_mode { parameters->mode = RELABSD_PARAMETERS_SERVER_MODE; parameters->communication_node_name = (char *) NULL; - parameters->physical_device_name = argv[2]; + parameters->physical_device_file_name = argv[2]; parameters->read_argc = 2; } else @@ -172,11 +172,11 @@ int relabsd_parameters_parse_options relabsd_parameters_initialize_options(parameters); /* - * i = (params->read_argc + 1) because reading 2 params is actually reaching - * the [2] element of the array, since the [0] element is the executable - * name. + * i = (parameters->read_argc + 1) because reading 2 params is actually + * reaching the [2] element of the array, since the [0] element is the + * executable name. */ - for (i = (params->read_argc + 1); i < argc; ++i) + for (i = (parameters->read_argc + 1); i < argc; ++i) { if ( @@ -184,9 +184,9 @@ int relabsd_parameters_parse_options || RELABSD_STRING_EQUALS("--daemon", argv[i]) ) { - params->run_as_daemon = 1; + parameters->run_as_daemon = 1; - if (params->node == ((char *) NULL)) + if (parameters->communication_node_name == ((char *) NULL)) { RELABSD_S_WARNING ( @@ -208,7 +208,7 @@ int relabsd_parameters_parse_options } ++i; - params->device_name = argv[i]; + parameters->device_name = argv[i]; } else if ( @@ -228,7 +228,13 @@ int relabsd_parameters_parse_options if ( - relabsd_util_parse_int(argv[i], 0, INT_MAX, &(params->timeout)) + relabsd_util_parse_int + ( + argv[i], + 0, + INT_MAX, + &(parameters->timeout) + ) < 0 ) { @@ -261,7 +267,7 @@ int relabsd_parameters_parse_options ++i; - if (parse_axis((argc - i), (argv + i), params->axes) < 0) + if (parse_axis((argc - i), (argv + i), parameters->axes) < 0) { relabsd_parameters_print_usage(argv[0]); @@ -283,7 +289,7 @@ int relabsd_parameters_parse_options } ++i; - params->configuration_file = argv[i]; + parameters->configuration_file = argv[i]; } else { diff --git a/src/config/parameters/parameters_accessors.c b/src/config/parameters/parameters_accessors.c index e53cfe0..e1fd805 100644 --- a/src/config/parameters/parameters_accessors.c +++ b/src/config/parameters/parameters_accessors.c @@ -1,3 +1,7 @@ +/**** POSIXS ^*****************************************************************/ +#include <stdlib.h> + +/**** RELABSD *****************************************************************/ #include <relabsd/config/parameters.h> /******************************************************************************/ @@ -9,7 +13,7 @@ /******************************************************************************/ void relabsd_parameters_initialize_options ( - const struct relabsd_parameters parameters [const restrict static 1] + struct relabsd_parameters parameters [const restrict static 1] ) { parameters->run_as_daemon = 0; diff --git a/src/config/parameters/parse_config_file.c b/src/config/parameters/parse_config_file.c new file mode 100644 index 0000000..a6920b4 --- /dev/null +++ b/src/config/parameters/parse_config_file.c @@ -0,0 +1,348 @@ +/**** POSIX *******************************************************************/ +#include <errno.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <limits.h> + +#include <sys/time.h> + +/**** RELABSD *****************************************************************/ +#include <relabsd/config.h> +#include <relabsd/debug.h> + +#include <relabsd/config/parameters.h> + +#include <relabsd/device/axis.h> + +#include <relabsd/util/string.h> + + +/* TODO: move this to relabsd/config.h */ +#ifndef RELABSD_OPTION_MAX_SIZE + #define RELABSD_OPTION_MAX_SIZE 64 +#endif + +/******************************************************************************/ +/**** LOCAL FUNCTIONS *********************************************************/ +/******************************************************************************/ + +/* + * Returns -1 on error, + * 0 on EOF, + * 1 on newline. + */ +static int read_axis_options +( + FILE file [const restrict static 1], + const char axis_name [const restrict static 1], + struct relabsd_axis axis [const restrict static 1] +) +{ + char option[(RELABSD_OPTION_MAX_SIZE + 1)]; + int i; + char c; + + option[RELABSD_OPTION_MAX_SIZE] = '\0'; + + i = 0; + + while (i <= RELABSD_OPTION_MAX_SIZE) + { + c = (char) getc(file); + + if (c == EOF) + { + if (ferror(file)) + { + RELABSD_FATAL + ( + "Reading error while parsing an option in the configuration file" + " (axis '%s').", + axis_name + ); + } + else + { + RELABSD_FATAL + ( + "End of file reached while parsing an option in the" + " configuration file (axis '%s').", + axis_name + ); + } + + return -1; + } + + switch (c) + { + case ' ': + case '\t': + break; + + case ',': + i = 0; + option[i] = '\n'; + /* We parsed a new option and there is a least another. */ + (void) + relabsd_axis_enable_option_from_name(option, axis_name, axis); + + break; + + case '\n': + option[i] = '\n'; + (void) + relabsd_axis_enable_option_from_name(option, axis_name, axis); + + return 1; + + case EOF: + option[i] = '\n'; + (void) + relabsd_axis_enable_option_from_name(option, axis_name, axis); + + return 0; + + default: + option[i] = c; + i++; + + break; + } + } + + RELABSD_FATAL + ( + "[CONFIG] Option name '%s[...]' (axis '%s') is too long (%d chars max).", + option, + axis_name, + RELABSD_OPTION_MAX_SIZE + ); + + return -1; +} + +/* +static int parse_timeout_option +( + struct relabsd_config * const conf, + const char * const param +) +{ + int timeout_msec; + const int prev_errno = errno; + + conf->enable_timeout = 1; + + errno = 0; + + timeout_msec = atoi(param); + + if (timeout_msec <= 0) + { + RELABSD_FATAL + ( + "Illegal value for timeout \"%d\": accepted range is [1, %d].", + timeout_msec, + INT_MAX + ); + + return -1; + } + + 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) + * ((suseconds_t) 1000) + ); + + return 0; +} +*/ + +/* + * Returns -1 on (fatal) error, + * 0 on succes. + */ +static int parse_axis_configuration_line +( + FILE file [const restrict static 1], + const char axis_name [const restrict static 1], + struct relabsd_parameters parameters [const static 1] +) +{ + int read_count; + enum relabsd_axis_name axis_index; + struct relabsd_axis * axis; + + axis_index = relabsd_axis_parse_name(axis_name); + + if (axis_index == RELABSD_UNKNOWN) + { + RELABSD_FATAL("Unknown axis '%s' in the configuration file.", axis_name); + + return -1; + + } + + axis = (parameters->axes + axis_index); + + errno = 0; + + read_count = + fscanf + ( + file, + "%d%d%d%d%d", + &(axis->min), + &(axis->max), + &(axis->fuzz), + &(axis->flat), + &(axis->resolution) + ); + + if (read_count == EOF) + { + if (errno == 0) + { + RELABSD_FATAL + ( + "Unexpected end of file while reading the '%s' axis' parameters in" + " the configuration file.", + axis_name + ); + } + else + { + RELABSD_FATAL + ( + "An error occured while reading the '%s' axis' parameters in the" + " configuration file: %s.", + axis_name, + strerror(errno) + ); + } + + return -1; + } + else if (read_count < 5) + { + RELABSD_FATAL + ( + "Invalid parameter count for the '%s' axis in the configuration file.", + axis_name + ); + + return -1; + } + + relabsd_axis_enable(axis); + + return read_axis_options(file, axis_name, axis); +} + +/* + * Returns -1 on (fatal) error, + * 0 on EOF, + * 1 on newline. + */ +static int read_config_line +( + FILE file [const restrict static 1], + const char prefix [const restrict static 1], + struct relabsd_parameters parameters [const restrict static 1] +) +{ + if (RELABSD_IS_PREFIX("#", prefix)) + { + return relabsd_util_reach_next_line_or_eof(file); + } + + return parse_axis_configuration_line(file, prefix, parameters); +} + +/******************************************************************************/ +/**** EXPORTED FUNCTIONS ******************************************************/ +/******************************************************************************/ +int relabsd_parameters_parse_config_file +( + const char filename [const restrict static 1], + struct relabsd_parameters parameters [const restrict static 1] +) +{ + FILE * file; + char buffer[(RELABSD_CONF_AXIS_CODE_SIZE + 1)]; + int continue_reading; + + buffer[RELABSD_CONF_AXIS_CODE_SIZE] = '\0'; + + errno = 0; + file = fopen(filename, "r"); + + if (file == (FILE *) NULL) + { + RELABSD_FATAL("Could not open file %s: %s.", filename, strerror(errno)); + + return -1; + } + + errno = 0; + continue_reading = 1; + + while + ( + (continue_reading == 1) + && + ( + fscanf + ( + file, + "%" RELABSD_TO_STRING(RELABSD_CONF_AXIS_CODE_SIZE) "s", + buffer + ) + != EOF + ) + ) + { + switch (read_config_line(file, buffer, parameters)) + { + case 1: + /* Everything is going well. */ + break; + + case 0: + /* EOF reached. */ + continue_reading = 0; + break; + + case -1: + /* A fatal error occured. */ + fclose(file); + return -1; + } + } + + if (errno != 0) + { + /* An error happened in the while loop condition. */ + RELABSD_FATAL + ( + "[CONFIG] Error while reading file: %s, last read '%s'.", + strerror(errno), + buffer + ); + + fclose(file); + + return -1; + } + + fclose(file); + + return 0; +} diff --git a/src/device/axis/axis.c b/src/device/axis/axis.c index 75afad8..2659c84 100644 --- a/src/device/axis/axis.c +++ b/src/device/axis/axis.c @@ -1,6 +1,9 @@ /**** POSIX *******************************************************************/ #include <string.h> +/**** LIBEVDEV ****************************************************************/ +#include <libevdev/libevdev.h> + /**** RELABSD *****************************************************************/ #include <relabsd/device/axis.h> @@ -21,7 +24,7 @@ void relabsd_axis_initialize void relabsd_axis_to_absinfo ( - struct relabsd_axis axis [const restrict static 1] + struct relabsd_axis axis [const restrict static 1], struct input_absinfo absinfo [const restrict static 1] ) { @@ -32,3 +35,13 @@ void relabsd_axis_to_absinfo absinfo->flat = (__s32) axis->flat; absinfo->resolution = (__s32) axis->resolution; } + +void relabsd_axis_enable +( + struct relabsd_axis axis [const restrict static 1] +) +{ + axis->is_enabled = 1; +} + + diff --git a/src/device/axis/axis_filter.c b/src/device/axis/axis_filter.c index 295a7f6..eb14edd 100644 --- a/src/device/axis/axis_filter.c +++ b/src/device/axis/axis_filter.c @@ -1,3 +1,7 @@ +/**** POSIX *******************************************************************/ +#include <stdlib.h> +#include <limits.h> + /**** RELABSD *****************************************************************/ #include <relabsd/device/axis.h> @@ -6,13 +10,13 @@ /******************************************************************************/ static int direct_filter ( - struct relabsd_config_axis * const axis, - int * const value + struct relabsd_axis axis [const restrict static 1], + int value [const restrict static 1] ) { if (abs(*value - axis->previous_value) <= axis->fuzz) { - if (axis->option[RELABSD_REAL_FUZZ_OPTION]) + if (axis->flags[RELABSD_REAL_FUZZ]) { axis->previous_value = *value; } @@ -45,8 +49,8 @@ static int direct_filter static int rel_to_abs_filter ( - struct relabsd_config_axis * const axis, - int * const value + struct relabsd_axis axis [const restrict static 1], + int value [const restrict static 1] ) { long int guard; @@ -64,7 +68,7 @@ static int rel_to_abs_filter *value = (int) guard; - if (axis->option[RELABSD_FRAMED_OPTION]) + if (axis->flags[RELABSD_FRAMED]) { if (*value < axis->min) { @@ -115,12 +119,12 @@ int relabsd_axis_filter_new_value { if (!(axis->is_enabled)) { - return; + return 0; } /* TODO: handle conf->axis[axis].resolution */ - if (axis->flag[RELABSD_DIRECT_OPTION]) + if (axis->flags[RELABSD_DIRECT]) { return direct_filter(axis, value); } diff --git a/src/device/axis/axis_option.c b/src/device/axis/axis_option.c new file mode 100644 index 0000000..fd1cb9c --- /dev/null +++ b/src/device/axis/axis_option.c @@ -0,0 +1,71 @@ +/**** POSIX *******************************************************************/ +#include <string.h> + +/**** RELABSD *****************************************************************/ +#include <relabsd/debug.h> + +#include <relabsd/device/axis.h> + +/******************************************************************************/ +/**** LOCAL FUNCTIONS *********************************************************/ +/******************************************************************************/ + +/******************************************************************************/ +/**** EXPORTED FUNCTIONS ******************************************************/ +/******************************************************************************/ +/* + * Returns -1 if the option was discarded (an error has been reported), + * 0 if the option was successfully parsed. + */ +int relabsd_axis_enable_option_from_name +( + const char option_name [const restrict static 1], + const char axis_name [const restrict static 1], + struct relabsd_axis axis [const restrict static 1] +) +{ + + if (strcmp(option_name, "direct") == 0) + { + axis->flags[RELABSD_DIRECT] = 1; + + if (axis->flags[RELABSD_FRAMED]) + { + RELABSD_WARNING + ( + "Option 'direct' on axis '%s' supersedes its 'framed' option.", + axis_name + ); + } + } + else if (strcmp(option_name, "real_fuzz") == 0) + { + axis->flags[RELABSD_REAL_FUZZ] = 1; + } + else if (strcmp(option_name, "framed") == 0) + { + axis->flags[RELABSD_FRAMED] = 1; + + if (axis->flags[RELABSD_DIRECT]) + { + RELABSD_WARNING + ( + "Option 'direct' on axis '%s' supersedes its 'framed' option.", + axis_name + ); + } + } + else + { + RELABSD_ERROR + ( + "Unknown option '%s' for axis '%s'.", + option_name, + axis_name + ); + + return -1; + } + + return 0; +} diff --git a/src/util/string.c b/src/util/string.c index edb5eb6..7e754bd 100644 --- a/src/util/string.c +++ b/src/util/string.c @@ -34,3 +34,38 @@ int relabsd_util_parse_int *output = ((int) buffer); return 0; } + +/* + * Returns -1 on error, + * 0 on EOF, + * 1 on newline. + */ +int relabsd_util_reach_next_line_or_eof (FILE f [const restrict static 1]) +{ + char c; + + c = (char) getc(f); + + while ((c != '\n') && c != EOF) + { + c = (char) getc(f); + } + + if (ferror(f)) + { + /* + * 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. + */ + return -1; + } + + if (c == EOF) + { + return 0; + } + + return 1; +} |


