diff --git a/daemon/io.c b/daemon/io.c index 12ebeff..d4b782d 100644 --- a/daemon/io.c +++ b/daemon/io.c @@ -28,7 +28,7 @@ static void process_report(libx52io_report *report, libx52io_report *prev) { // TODO: Process changes x52d_mouse_report_event(report); - memcpy(prev, report, sizeof(*prev)); + libx52io_copy_report(prev, report); } static void *x52_io_thr(void *param) @@ -43,7 +43,7 @@ static void *x52_io_thr(void *param) PINELOG_INFO(_("Starting X52 I/O thread")); // Reset the previous report, so that process_report can handle changes. - memset(&prev_report, 0, sizeof(prev_report)); + libx52io_reset_report(&prev_report); for (;;) { rc = libx52io_read_timeout(io_ctx, &report, IO_READ_TIMEOUT); diff --git a/daemon/mouse_handler.c b/daemon/mouse_handler.c index 6c5c2ff..efef3b9 100644 --- a/daemon/mouse_handler.c +++ b/daemon/mouse_handler.c @@ -196,17 +196,14 @@ static void report_sync(void) if (rc != VKM_SUCCESS) { PINELOG_ERROR(_("Error writing mouse sync event")); } else { - memcpy((void *)&old_report, (void *)&new_report, sizeof(old_report)); + libx52io_copy_report(&new_report, &old_report); } } static void reset_reports(void) { - memset((void *)&old_report, 0, sizeof(old_report)); - /* Set the default thumbstick values to the mid-point */ - old_report.axis[LIBX52IO_AXIS_THUMBX] = 8; - old_report.axis[LIBX52IO_AXIS_THUMBY] = 8; - memcpy((void *)&new_report, (void *)&old_report, sizeof(new_report)); + libx52io_reset_report(&old_report); + libx52io_copy_report(&new_report, &old_report); } static void * x52_mouse_thr(void *param) @@ -273,7 +270,7 @@ void x52d_mouse_report_event(libx52io_report *report) { bool state_changed; if (report) { - memcpy((void *)&new_report, report, sizeof(new_report)); + libx52io_copy_report(&new_report, report); if (!vkm_is_ready(mouse_context) || !mouse_thr_enabled) { return; diff --git a/include/libx52/libx52io.h b/include/libx52/libx52io.h index b977136..c7ff6bc 100644 --- a/include/libx52/libx52io.h +++ b/include/libx52/libx52io.h @@ -629,6 +629,29 @@ 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 Copy one report into another + * + * Copy \c source report to \c target report. If either parameter is NULL, it + * does nothing. + * + * @param[in] target Pointer to the target report + * @param[in] source Pointer to the source report + */ +LIBX52IO_API void libx52io_copy_report(libx52io_report *target, const libx52io_report *source); + +/** + * @brief Reset the report values to defaults. + * + * Resets all axis values to 0, except for the thumb stick axes, which are + * reset to their midpoint of 8. Also, all button states are reset to unpressed + * and mode/hat are reset to 0. + * + * @param[in] report Pointer to the report to reset + */ +LIBX52IO_API void libx52io_reset_report(libx52io_report *report); + /** @} */ #ifdef __cplusplus diff --git a/libx52io/meson.build b/libx52io/meson.build index 7928b59..c437665 100644 --- a/libx52io/meson.build +++ b/libx52io/meson.build @@ -8,6 +8,7 @@ libx52io_files = files( 'device.c', 'validation.c', 'compare.c', + 'report.c', ) lib_libx52io = library('x52io', libx52io_files, diff --git a/libx52io/report.c b/libx52io/report.c new file mode 100644 index 0000000..9974081 --- /dev/null +++ b/libx52io/report.c @@ -0,0 +1,59 @@ +/* + * Saitek X52 IO driver + * + * Copyright (C) 2012-2026 Nirenjan Krishnan (nirenjan@nirenjan.org) + * + * SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0 + */ + +#include "build-config.h" +#include +#include +#include "common.h" +#include "usb-ids.h" +#include "gettext.h" + +void libx52io_copy_report(libx52io_report *target, const libx52io_report *source) +{ + if (target == NULL || source == NULL) { + return; + } + + for (int ax = 0; ax < LIBX52IO_AXIS_MAX; ax++) { + target->axis[ax] = source->axis[ax]; + } + + for (int btn = 0; btn < LIBX52IO_BUTTON_MAX; btn++) { + target->button[btn] = source->button[btn]; + } + + target->mode = source->mode; + target->hat = source->hat; +} + +void libx52io_reset_report(libx52io_report *report) +{ + if (report == NULL) { + return; + } + + for (int ax = 0; ax < LIBX52IO_AXIS_MAX; ax++) { + switch (ax) { + case LIBX52IO_AXIS_THUMBX: + case LIBX52IO_AXIS_THUMBY: + report->axis[ax] = 8; + break; + + default: + report->axis[ax] = 0; + break; + } + } + + for (int btn = 0; btn < LIBX52IO_BUTTON_MAX; btn++) { + report->button[btn] = 0; + } + + report->mode = 0; + report->hat = 0; +}