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.
pull/87/head
nirenjan 2026-11-04 22:09:47 -08:00
parent 2da88f415f
commit 372200a6ce
4 changed files with 74 additions and 1 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -1,4 +1,4 @@
libx52io_version = '1.1.0'
libx52io_version = '1.2.0'
libx52io_files = files(
'core.c',

View File

@ -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