From 87a86b9a599de35d09da7d954ba662091accc90b Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Thu, 9 Jan 2020 02:29:59 +0100 Subject: Adds all the new functionalities. Some TODOs and slight coding style inconsistencies remaining, though... --- include/relabsd/config/parameters.h | 10 + include/relabsd/device/axis.h | 13 +- include/relabsd/device/virtual_device.h | 18 ++ src/client.c | 11 +- .../parameters/handle_remote_client_commands.c | 302 ++++++++++++++++++++- src/config/parameters/parameters.c | 72 +++-- src/config/parameters/parameters_accessors.c | 22 ++ src/device/axis/axis.c | 19 +- src/device/virtual/virtual_device.c | 93 +++++-- src/main.c | 2 +- src/server/handle_client.c | 57 ++++ 11 files changed, 551 insertions(+), 68 deletions(-) diff --git a/include/relabsd/config/parameters.h b/include/relabsd/config/parameters.h index ae33e0b..0f59206 100644 --- a/include/relabsd/config/parameters.h +++ b/include/relabsd/config/parameters.h @@ -109,3 +109,13 @@ int relabsd_parameters_use_timeout ( const struct relabsd_parameters parameters [const restrict static 1] ); + +int relabsd_parameters_device_name_is_dirty +( + const struct relabsd_parameters parameters [const restrict static 1] +); + +void relabsd_parameters_clean_device_name +( + struct relabsd_parameters parameters [const restrict static 1] +); diff --git a/include/relabsd/device/axis.h b/include/relabsd/device/axis.h index 833a31f..b6605f5 100644 --- a/include/relabsd/device/axis.h +++ b/include/relabsd/device/axis.h @@ -79,7 +79,7 @@ int relabsd_axis_is_enabled void relabsd_axis_to_absinfo ( - struct relabsd_axis axis [const restrict static 1], + const struct relabsd_axis axis [const restrict static 1], struct input_absinfo absinfo [const restrict static 1] ); @@ -93,3 +93,14 @@ void relabsd_axis_initialize ( struct relabsd_axis axis [const restrict static 1] ); + +int relabsd_axis_attributes_are_dirty +( + const struct relabsd_axis axis [const restrict static 1] +); + +void relabsd_axis_set_attributes_are_dirty +( + const int val, + struct relabsd_axis axis [const restrict static 1] +); diff --git a/include/relabsd/device/virtual_device.h b/include/relabsd/device/virtual_device.h index f4ffeca..3642330 100644 --- a/include/relabsd/device/virtual_device.h +++ b/include/relabsd/device/virtual_device.h @@ -65,3 +65,21 @@ int relabsd_virtual_device_has_already_timed_out ( const struct relabsd_virtual_device device [const restrict static 1] ); + +int relabsd_virtual_device_update_axis_absinfo +( + const enum relabsd_axis_name axis_name, + const struct relabsd_axis axis [const restrict static 1], + const struct relabsd_virtual_device device [const restrict static 1] +); + +int relabsd_virtual_device_rename +( + const struct relabsd_parameters parameters [const restrict static 1], + const struct relabsd_virtual_device device [const restrict static 1] +); + +int relabsd_virtual_device_recreate +( + struct relabsd_virtual_device device [const restrict static 1] +); diff --git a/src/client.c b/src/client.c index 6e6b036..6a4e08d 100644 --- a/src/client.c +++ b/src/client.c @@ -107,12 +107,6 @@ static int send_commands for (i = 3; i < argc;) { - - if (fputs(argv[i], socket) == EOF) - { - // TODO: error - } - if (relabsd_parameters_argument_count_for(argv[i], &j) < 0) { RELABSD_FATAL("Unknown option '%s'.", argv[i]); @@ -121,6 +115,11 @@ static int send_commands return -1; } + if (fputs(argv[i], socket) == EOF) + { + // TODO: error + } + if (fputc('\n', socket) == EOF) { // TODO: error diff --git a/src/config/parameters/handle_remote_client_commands.c b/src/config/parameters/handle_remote_client_commands.c index 5523e37..caa82dc 100644 --- a/src/config/parameters/handle_remote_client_commands.c +++ b/src/config/parameters/handle_remote_client_commands.c @@ -58,6 +58,10 @@ static int get_next_argument return -1; } + /* + * size is "not including the terminating null byte", so it's the '\n' + * character. + */ input->buffer[(input->size - 1)] = '\0'; return 0; @@ -89,7 +93,7 @@ static void finalize_client_input static int handle_timeout_change ( struct relabsd_parameters_client_input input [const restrict static 1], - struct relabsd_parameters parameters [const static 1] + struct relabsd_parameters parameters [const restrict static 1] ) { int timeout_msec; @@ -113,11 +117,280 @@ static int handle_timeout_change return 0; } -static int handle_inputs +static int handle_name_change +( + struct relabsd_parameters_client_input input [const restrict static 1], + struct relabsd_parameters parameters [const restrict static 1] +) +{ + const char * device_name; + + if (get_next_argument(input) < 0) + { + RELABSD_S_ERROR("Could not get device name value from client."); + + return -1; + } + + device_name = + (const char *) calloc((size_t) (input->size - 1), sizeof(char)); + + if (device_name == (const char *) NULL) + { + RELABSD_S_ERROR + ( + "Could not allocate memory to store the device name requested by" + " the client." + ); + + return -1; + } + + (void) memcpy + ( + (void *) device_name, + (const void *) input->buffer, + (size_t) (input->size - 1) + ); + + if (parameters->device_name_was_modified) + { + free((void *) parameters->device_name); + } + + parameters->device_name = device_name; + parameters->device_name_was_modified = 1; + + return 0; +} + +static int handle_axis_mod ( struct relabsd_parameters_client_input input [const restrict static 1], struct relabsd_parameters parameters [const static 1] ) +{ + enum relabsd_axis_name axis_name; + int * value_to_modify; + int min_value; + int input_value; + + if (get_next_argument(input) < 0) + { + RELABSD_S_ERROR("Could not get name of axis to modify from client."); + + return -1; + } + + axis_name = relabsd_axis_parse_name(input->buffer); + + if (axis_name == RELABSD_UNKNOWN) + { + RELABSD_ERROR + ( + "Client requested modification on unknown axis \"%s\".", + input->buffer + ); + + return -1; + } + + if (get_next_argument(input) < 0) + { + RELABSD_S_ERROR("Could not get parameter of axis to modify from client."); + + return -1; + } + + if (RELABSD_STRING_EQUALS("min", input->buffer)) + { + value_to_modify = &(parameters->axes[axis_name].min); + min_value = INT_MIN; + } + else if (RELABSD_STRING_EQUALS("max", input->buffer)) + { + value_to_modify = &(parameters->axes[axis_name].max); + min_value = INT_MIN; + } + else if (RELABSD_STRING_EQUALS("fuzz", input->buffer)) + { + value_to_modify = &(parameters->axes[axis_name].fuzz); + min_value = 0; + } + else if (RELABSD_STRING_EQUALS("flat", input->buffer)) + { + value_to_modify = &(parameters->axes[axis_name].flat); + min_value = 0; + } + else if (RELABSD_STRING_EQUALS("resolution", input->buffer)) + { + value_to_modify = &(parameters->axes[axis_name].resolution); + min_value = 0; + } + else + { + RELABSD_ERROR + ( + "Client requested modification on unknown axis parameter \"%s\".", + input->buffer + ); + + return -1; + } + + if (get_next_argument(input) < 0) + { + RELABSD_S_ERROR + ( + "Could not get new value for axis modification requested by client." + ); + + return -1; + } + + if + ( + relabsd_util_parse_int + ( + (input->buffer + 1), + min_value, + INT_MAX, + &input_value + ) + < 0 + ) + { + RELABSD_ERROR + ( + "Invalid value \"%s\"for axis modification requested by client.", + (input->buffer + 1) + ); + + return -1; + } + + switch (input->buffer[0]) + { + case '=': + *value_to_modify = input_value; + break; + + case '-': + if (input_value == INT_MIN) + { + input_value = INT_MAX; + } + else + { + input_value = -input_value; + } + /* fall through */ + case '+': + if + ( + ( + (input_value > 0) + && (*value_to_modify > (INT_MAX - input_value)) + ) + || + ( + (input_value < 0) + && (*value_to_modify < (min_value - input_value)) + ) + ) + { + RELABSD_S_WARNING + ( + "Client request would make axis parameter over/underflow." + ); + + *value_to_modify = (input_value < 0) ? min_value : INT_MAX; + } + else + { + *value_to_modify += input_value; + } + break; + } + + parameters->axes[axis_name].previous_value = 0; + parameters->axes[axis_name].attributes_were_modified = 1; + + return 0; +} + +static int handle_option_toggle +( + struct relabsd_parameters_client_input input [const restrict static 1], + struct relabsd_parameters parameters [const restrict static 1] +) +{ + enum relabsd_axis_name axis_name; + + if (get_next_argument(input) < 0) + { + RELABSD_S_ERROR("Could not get name of axis to modify from client."); + + return -1; + } + + axis_name = relabsd_axis_parse_name(input->buffer); + + if (axis_name == RELABSD_UNKNOWN) + { + RELABSD_ERROR + ( + "Client requested modification on unknown axis \"%s\".", + input->buffer + ); + + return -1; + } + + if (get_next_argument(input) < 0) + { + RELABSD_S_ERROR("Could not get option of axis to modify from client."); + + return -1; + } + + if (RELABSD_STRING_EQUALS("framed", input->buffer)) + { + parameters->axes[axis_name].flags[RELABSD_FRAMED] ^= 1; + } + else if (RELABSD_STRING_EQUALS("direct", input->buffer)) + { + parameters->axes[axis_name].flags[RELABSD_DIRECT] ^= 1; + } + else if (RELABSD_STRING_EQUALS("real_fuzz", input->buffer)) + { + parameters->axes[axis_name].flags[RELABSD_REAL_FUZZ] ^= 1; + } + else if (RELABSD_STRING_EQUALS("enable", input->buffer)) + { + parameters->axes[axis_name].is_enabled ^= 1; + } + else + { + RELABSD_ERROR + ( + "Client requested toggle of unknown axis option \"%s\".", + input->buffer + ); + + return -1; + } + + parameters->axes[axis_name].previous_value = 0; + + return 0; +} + +static int handle_inputs +( + struct relabsd_parameters_client_input input [const restrict static 1], + struct relabsd_parameters parameters [const restrict static 1] +) { for (;;) { @@ -148,7 +421,7 @@ static int handle_inputs { if (handle_timeout_change(input, parameters) < 0) { - return 0; + return -1; } } else if @@ -157,8 +430,10 @@ static int handle_inputs || RELABSD_STRING_EQUALS("--name", input->buffer) ) { - /* TODO: implement */ - RELABSD_PROG_ERROR("Unimplemented command \"%s\".", input->buffer); + if (handle_name_change(input, parameters) < 0) + { + return -1; + } } else if ( @@ -166,8 +441,21 @@ static int handle_inputs || RELABSD_STRING_EQUALS("--mod-axis", input->buffer) ) { - /* TODO: implement */ - RELABSD_PROG_ERROR("Unimplemented command \"%s\".", input->buffer); + if (handle_axis_mod(input, parameters) < 0) + { + return -1; + } + } + else if + ( + RELABSD_STRING_EQUALS("-o", input->buffer) + || RELABSD_STRING_EQUALS("--toggle-option", input->buffer) + ) + { + if (handle_option_toggle(input, parameters) < 0) + { + return -1; + } } else { diff --git a/src/config/parameters/parameters.c b/src/config/parameters/parameters.c index 84b348c..04ee498 100644 --- a/src/config/parameters/parameters.c +++ b/src/config/parameters/parameters.c @@ -315,6 +315,21 @@ int relabsd_parameters_parse_options return -1; } } + else if + ( + RELABSD_STRING_EQUALS("-m", argv[i]) + || RELABSD_STRING_EQUALS("--mod-axis", argv[i]) + || RELABSD_STRING_EQUALS("-o", argv[i]) + || RELABSD_STRING_EQUALS("--toggle-argv[i]", argv[i]) + ||RELABSD_STRING_EQUALS("-q", argv[i]) + || RELABSD_STRING_EQUALS("--quit", argv[i]) + ) + { + RELABSD_FATAL("\"%s\" is not available in this mode.", argv[i]); + relabsd_parameters_print_usage(argv[0]); + + return -1; + } else { RELABSD_FATAL("Unknown