| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2015-09-04 12:48:01 +0200 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2015-09-04 12:48:01 +0200 |
| commit | dc75c584513dfd746f9819891c2eb6e77bfdf1ad (patch) | |
| tree | 577d5f9ef7fbc43a5873492d386808518d845d94 | |
| parent | 385a3d17995dafde4f2b57f8d3a42d4d4782119d (diff) | |
Better debug options, better string comparisons.
| -rw-r--r-- | CMakeLists.txt | 70 | ||||
| -rw-r--r-- | src/axis.c | 21 | ||||
| -rw-r--r-- | src/config.c | 94 | ||||
| -rw-r--r-- | src/config.h | 4 | ||||
| -rw-r--r-- | src/error.h | 29 | ||||
| -rw-r--r-- | src/input.c | 8 | ||||
| -rw-r--r-- | src/main.c | 12 | ||||
| -rw-r--r-- | src/pervasive.h | 2 | ||||
| -rw-r--r-- | src/relabsd_device.c | 24 |
9 files changed, 209 insertions, 55 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 003697f..0697bcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,31 +25,61 @@ if (CMAKE_COMPILER_IS_GNUCC) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -Wextra") endif (CMAKE_COMPILER_IS_GNUCC) -# Highest debug level is defined here to be able to access it in CMake. -set(RELABSD_HIGHEST_DEBUG_LEVEL 100) -target_compile_definitions( - relabsd - PUBLIC - "-DRELABSD_HIGHEST_DEBUG_LVL=${RELABSD_HIGHEST_DEBUG_LEVEL}" +option( + RELABSD_DEBUG_PROGRAM_FLOW + "Adds debug messages to display the program's flow." + OFF ) -message( - STATUS - "[CONST] Highest debug level set to ${RELABSD_HIGHEST_DEBUG_LEVEL}." +if (RELABSD_DEBUG_PROGRAM_FLOW) + target_compile_definitions( + relabsd + PUBLIC + "-DRELABSD_DEBUG_PROGRAM_FLOW=1" + ) + message(STATUS "[OPTION] Debug messages for the program's flow.") +endif (RELABSD_DEBUG_PROGRAM_FLOW) + +option( + RELABSD_DEBUG_CONFIG + "Adds debug messages to check the program's configuration." + OFF ) +if (RELABSD_DEBUG_CONFIG) + target_compile_definitions( + relabsd + PUBLIC + "-DRELABSD_DEBUG_CONFIG=1" + ) + message(STATUS "[OPTION] Debug messages for the program's configuration.") +endif (RELABSD_DEBUG_CONFIG) -set( - RELABSD_DEBUG_LEVEL - "0" - CACHE - INTEGER - "Debug verbosity level[0-${RELABSD_HIGHEST_DEBUG_LEVEL}]." +option( + RELABSD_DEBUG_REAL_EVENTS + "Adds debug messages to check the real device's events." + OFF ) -target_compile_definitions( - relabsd - PUBLIC - "-DRELABSD_DEBUG_LVL=${RELABSD_DEBUG_LEVEL}" +if (RELABSD_DEBUG_REAL_EVENTS) + target_compile_definitions( + relabsd + PUBLIC + "-DRELABSD_DEBUG_REAL_EVENTS=1" + ) + message(STATUS "[OPTION] Debug messages for the real device's events.") +endif (RELABSD_DEBUG_REAL_EVENTS) + +option( + RELABSD_DEBUG_VIRTUAL_EVENTS + "Adds debug messages to check the virtual device's events." + OFF ) -message(STATUS "[OPTION] Debug level set to ${RELABSD_DEBUG_LEVEL}.") +if (RELABSD_DEBUG_VIRTUAL_EVENTS) + target_compile_definitions( + relabsd + PUBLIC + "-DRELABSD_DEBUG_VIRTUAL_EVENTS=1" + ) + message(STATUS "[OPTION] Debug messages for the virtual device's events.") +endif (RELABSD_DEBUG_VIRTUAL_EVENTS) option( RELABSD_ENABLE_ERROR_LOCATION @@ -5,42 +5,37 @@ #include "axis.h" #include "error.h" -/* - * Implementation note: RELABSD_IS_PREFIX, as its name implies, is checking for a - * prefix, not an equal value. This could cause issues if there were axes - * with name prefixed by another axis name. - */ enum relabsd_axis relabsd_axis_from_name (const char * const name) { - if (RELABSD_IS_PREFIX("X", name)) + if (RELABSD_STRING_EQUALS("X", name)) { return RELABSD_X; } - else if (RELABSD_IS_PREFIX("Y", name)) + else if (RELABSD_STRING_EQUALS("Y", name)) { return RELABSD_Y; } - else if (RELABSD_IS_PREFIX("Z", name)) + else if (RELABSD_STRING_EQUALS("Z", name)) { return RELABSD_Z; } - else if (RELABSD_IS_PREFIX("RX", name)) + else if (RELABSD_STRING_EQUALS("RX", name)) { return RELABSD_RX; } - else if (RELABSD_IS_PREFIX("RY", name)) + else if (RELABSD_STRING_EQUALS("RY", name)) { return RELABSD_RY; } - else if (RELABSD_IS_PREFIX("RZ", name)) + else if (RELABSD_STRING_EQUALS("RZ", name)) { return RELABSD_RZ; } - else if (RELABSD_IS_PREFIX("WL", name)) + else if (RELABSD_STRING_EQUALS("WL", name)) { return RELABSD_WHEEL; } - else if (RELABSD_IS_PREFIX("MC", name)) + else if (RELABSD_STRING_EQUALS("MC", name)) { return RELABSD_MISC; } diff --git a/src/config.c b/src/config.c index 02460d0..44d463f 100644 --- a/src/config.c +++ b/src/config.c @@ -88,14 +88,55 @@ static int parse_option 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 { @@ -280,6 +321,18 @@ static int parse_axis_configuration_line 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; @@ -319,10 +372,10 @@ static int read_config_file ) { FILE * f; - char buffer[3]; + char buffer[(RELABSD_CONF_AXIS_CODE_SIZE + 1)]; int continue_reading, prev_errno; - buffer[2] = '\0'; + buffer[RELABSD_CONF_AXIS_CODE_SIZE] = '\0'; f = fopen(filename, "r"); @@ -342,7 +395,20 @@ static int read_config_file continue_reading = 1; - while ((continue_reading == 1) && (fscanf(f, "%2s", buffer) != EOF)) + while + ( + (continue_reading == 1) + && + ( + fscanf + ( + f, + "%" RELABSD_TO_STRING(RELABSD_CONF_AXIS_CODE_SIZE) "s", + buffer + ) + != EOF + ) + ) { switch (read_config_line(conf, f, buffer)) { @@ -429,6 +495,8 @@ int relabsd_config_parse char * const * const argv ) { + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Parsing config & params..."); + if (check_usage(argc, argv) < 0) { return -1; @@ -437,14 +505,34 @@ int relabsd_config_parse if (argc == 3) { conf->device_name = NULL; + + RELABSD_S_DEBUG + ( + RELABSD_DEBUG_CONFIG, + "No virtual device name param, will use the real device's." + ); } else { conf->device_name = argv[3]; + + RELABSD_DEBUG + ( + RELABSD_DEBUG_CONFIG, + "Virtual device name param set to '%s'.", + conf->device_name + ); } 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) diff --git a/src/config.h b/src/config.h index 07fcdfb..dde989d 100644 --- a/src/config.h +++ b/src/config.h @@ -5,6 +5,9 @@ #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 @@ -15,7 +18,6 @@ enum relabsd_option RELABSD_FRAMED_OPTION }; - struct relabsd_config_axis { /* relabsd axis properties */ diff --git a/src/error.h b/src/error.h index 4aec255..863a1bb 100644 --- a/src/error.h +++ b/src/error.h @@ -6,13 +6,20 @@ #include "config.h" #include "pervasive.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_HIGHEST_DEBUG_LVL - #define RELABSD_HIGHEST_DEBUG_LVL 100 +#ifndef RELABSD_DEBUG_REAL_EVENTS + #define RELABSD_DEBUG_REAL_EVENTS 0 #endif -#ifndef RELABSD_DEBUG_LEVEL - #define RELABSD_DEBUG_LVL 0 +#ifndef RELABSD_DEBUG_VIRTUAL_EVENTS + #define RELABSD_DEBUG_VIRTUAL_EVENTS 0 #endif #define RELABSD_ENABLE_WARNINGS_OUTPUT 1 @@ -29,10 +36,16 @@ #define RELABSD_PRINT_STDERR(symbol, str, ...)\ fprintf(stderr, "[" symbol "]" RELABSD_LOCATION " " str "\n", __VA_ARGS__); -#define RELABSD_DEBUG(level, str, ...)\ +/* + * 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 (level < RELABSD_DEBUG_LVL)\ + if (flag)\ {\ RELABSD_PRINT_STDERR("D", str, __VA_ARGS__);\ }\ @@ -80,10 +93,10 @@ #define RELABSD_PRINT_S_STDERR(symbol, str)\ fprintf(stderr, "[" symbol "]" RELABSD_LOCATION " " str "\n"); -#define RELABSD_S_DEBUG(level, str)\ +#define RELABSD_S_DEBUG(flag, str)\ RELABSD_ISOLATE\ (\ - if (level < RELABSD_DEBUG_LVL)\ + if (flag)\ {\ RELABSD_PRINT_S_STDERR("D", str);\ }\ diff --git a/src/input.c b/src/input.c index a3b9e15..05484ae 100644 --- a/src/input.c +++ b/src/input.c @@ -85,6 +85,8 @@ int relabsd_input_open const struct relabsd_config * const conf ) { + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Opening input device..."); + input->fd = open(conf->input_file, O_RDONLY); if (input->fd < 0) @@ -126,6 +128,8 @@ int relabsd_input_open void relabsd_input_close (const struct relabsd_input * const input) { + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Closing input device..."); + libevdev_free(input->dev); close(input->fd); } @@ -166,8 +170,8 @@ int relabsd_input_read RELABSD_DEBUG ( - 90, - "[INPUT] Valid event received: {type = %s; code = %s; value = %d}.", + RELABSD_DEBUG_REAL_EVENTS, + "Valid event received: {type = %s; code = %s; value = %d}.", libevdev_event_type_get_name(event.type), libevdev_event_code_get_name(event.type, event.code), event.value @@ -61,6 +61,8 @@ static void convert_input unsigned int input_type, input_code; int value; + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Handling input events..."); + while (RELABSD_RUN == 1) { if (relabsd_input_read(input, &input_type, &input_code, &value) < 0) @@ -88,6 +90,8 @@ static void convert_input static int set_signal_handlers () { + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Setting signal handlers."); + if (signal(SIGINT, interrupt) == SIG_ERR) { RELABSD_S_FATAL("Unable to set the SIGINT signal handler."); @@ -104,6 +108,8 @@ int main (int argc, char ** argv) struct relabsd_input input; struct relabsd_device dev; + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "relabsd started."); + if (set_signal_handlers() < 0) { return -1; @@ -124,14 +130,14 @@ int main (int argc, char ** argv) return -4; } - RELABSD_S_DEBUG(10, "Converting inputs..."); - convert_input(&conf, &input, &dev); - RELABSD_S_DEBUG(10, "Terminating..."); + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Terminating..."); relabsd_device_destroy(&dev); relabsd_input_close(&input); + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Done."); + return 0; } diff --git a/src/pervasive.h b/src/pervasive.h index 0dd67fe..6c08305 100644 --- a/src/pervasive.h +++ b/src/pervasive.h @@ -11,4 +11,6 @@ /* 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) + #endif diff --git a/src/relabsd_device.c b/src/relabsd_device.c index e807ec6..b093203 100644 --- a/src/relabsd_device.c +++ b/src/relabsd_device.c @@ -140,6 +140,8 @@ int relabsd_device_create { int fd; + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Creating virtual device..."); + fd = open(config->input_file, O_RDONLY); if (fd < 0) @@ -208,6 +210,8 @@ int relabsd_device_create void relabsd_device_destroy (const struct relabsd_device * const dev) { + RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Destroying virtual device..."); + libevdev_uinput_destroy(dev->uidev); libevdev_free(dev->dev); } @@ -220,12 +224,22 @@ int relabsd_device_write_evdev_event int const value ) { - /* OPTIMIZE: Should we really send 'EV_SYN' after every event? */ - if + RELABSD_DEBUG ( - (libevdev_uinput_write_event(dev->uidev, type, code, value) == 0) - && (libevdev_uinput_write_event(dev->uidev, EV_SYN, SYN_REPORT, 0) == 0) - ) + RELABSD_DEBUG_VIRTUAL_EVENTS, + "Sending event: {type = %s; code = %s; value = %d}.", + libevdev_event_type_get_name(type), + libevdev_event_code_get_name(type, code), + value + ); + + /* + * We'll also send the 'EV_SYN' events when we receive them from the input + * device. + * OPTIMIZE: prevent 'EV_SYN' from being sent if we haven't sent any new + * values. (It might not be worth it though) + */ + if (libevdev_uinput_write_event(dev->uidev, type, code, value) == 0) { return 0; } |


