From 372200a6ce83a900f4156ef329f0a97784cc6386 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 4 Nov 2026 22:09:47 -0800 Subject: [PATCH] feat: Add libx52io_reset_report function This change adds a library helper to reset the report pointer, instead of the daemon handlers having to call memset and then manually reset the thumbsticks. The helper function will make it easier to maintain the reset behavior, and change it in a single place if it becomes necessary. --- include/libx52/libx52io.h | 12 ++++++++++++ libx52io/compare.c | 31 +++++++++++++++++++++++++++++++ libx52io/meson.build | 2 +- libx52io/test_compare.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/include/libx52/libx52io.h b/include/libx52/libx52io.h index b977136..0827256 100644 --- a/include/libx52/libx52io.h +++ b/include/libx52/libx52io.h @@ -629,6 +629,18 @@ LIBX52IO_API const char *libx52io_get_serial_number_string(libx52io_context *ctx * @returns 64-bit bitmask of which fields changed. See \ref libx52io_report_bitmask */ LIBX52IO_API uint64_t libx52io_compare_reports(const libx52io_report *prev, const libx52io_report *curr); + +/** + * @brief Reset a report to defaults + * + * Reset the report contents to defaults, this is somewhat like performing + * a memset, except that the thumbstick axes are always at the midpoint. + * Does nothing if the report pointer is NULL. + * + * @param[in] report Pointer to the report to reset + */ +LIBX52IO_API void libx52io_reset_report(libx52io_report *report); + /** @} */ #ifdef __cplusplus diff --git a/libx52io/compare.c b/libx52io/compare.c index 2c5a92e..d6bc2db 100644 --- a/libx52io/compare.c +++ b/libx52io/compare.c @@ -99,3 +99,34 @@ uint64_t libx52io_compare_reports(const libx52io_report *prev, return status; } + +void libx52io_reset_report(libx52io_report *report) +{ + int i; + + if (report == NULL) { + return; + } + + for (i = 0; i < LIBX52IO_AXIS_MAX; i++) { + switch (i) { + case LIBX52IO_AXIS_THUMBX: + case LIBX52IO_AXIS_THUMBY: + // Thumbstick axes set to midpoint + report->axis[i] = 8; + break; + + default: + // All other axes set to 0 + report->axis[i] = 0; + break; + } + } + + for (i = 0; i < LIBX52IO_BUTTON_MAX; i++) { + report->button[i] = false; + } + + report->mode = 0; + report->hat = 0; +} diff --git a/libx52io/meson.build b/libx52io/meson.build index 7928b59..321bc1a 100644 --- a/libx52io/meson.build +++ b/libx52io/meson.build @@ -1,4 +1,4 @@ -libx52io_version = '1.1.0' +libx52io_version = '1.2.0' libx52io_files = files( 'core.c', diff --git a/libx52io/test_compare.c b/libx52io/test_compare.c index c06fd6a..2481c4d 100644 --- a/libx52io/test_compare.c +++ b/libx52io/test_compare.c @@ -138,6 +138,35 @@ static void test_null_both(void **state) assert_int_equal(libx52io_compare_reports(NULL, NULL), 0); } +static void test_reset_report(void **state) +{ + int i; + (void)state; + libx52io_report dummy; + libx52io_reset_report(&dummy); + + assert_int_equal(dummy.mode, 0); + assert_int_equal(dummy.hat, 0); + for (int i = 0; i < LIBX52IO_AXIS_MAX; i++) { + switch (i) { + case LIBX52IO_AXIS_THUMBX: + case LIBX52IO_AXIS_THUMBY: + assert_int_equal(dummy.axis[i], 8); + break; + + default: + assert_int_equal(dummy.axis[i], 0); + break; + } + } + + for (int i = 0; i < LIBX52IO_BUTTON_MAX; i++) { + assert_false(dummy.button[i]); + } + + libx52io_reset_report(NULL); +} + #define FUNC(x) cmocka_unit_test(test_ ## x), const struct CMUnitTest tests[] = { @@ -148,6 +177,7 @@ const struct CMUnitTest tests[] = { FUNC(null_prev) FUNC(null_curr) FUNC(null_both) + FUNC(reset_report) }; #undef FUNC