From 07b6a0a0f5ae801d4a8454810b0c24cf07b79d6f Mon Sep 17 00:00:00 2001 From: Lightmarey Date: Sun, 4 Oct 2026 18:33:37 +0800 Subject: [PATCH 1/4] Fix full-width dial reports and held-rotation event ordering Decode 01/0A reports as signed 16-bit little-endian deltas. Emit button edges before held rotation and after release rotation, retaining Rotate(i)/Press(i). Ignore unrelated reports and exit on disconnected input so a session supervisor can reconnect. Add a notification opt-out and standalone report regression tests. Signed-off-by: Lightmarey --- openwheel-daemon/CMakeLists.txt | 10 +++++- openwheel-daemon/hidreader.c | 61 +++++++++++++++------------------ openwheel-daemon/report.c | 16 +++++++++ openwheel-daemon/report.h | 12 +++++++ openwheel-daemon/test_report.c | 45 ++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 35 deletions(-) create mode 100644 openwheel-daemon/report.c create mode 100644 openwheel-daemon/report.h create mode 100644 openwheel-daemon/test_report.c diff --git a/openwheel-daemon/CMakeLists.txt b/openwheel-daemon/CMakeLists.txt index 564c567..3b860a3 100644 --- a/openwheel-daemon/CMakeLists.txt +++ b/openwheel-daemon/CMakeLists.txt @@ -17,16 +17,24 @@ endif() set(DAEMON_SRCS hidreader.c helpers.c + report.c ) set(DAEMON_HEADERS openwheel.h helpers.h + report.h ) # Create executable add_executable(asus_wheel ${DAEMON_SRCS} ${DAEMON_HEADERS}) +if(BUILD_TESTS) + add_executable(test_report test_report.c report.c) + target_compile_options(test_report PRIVATE -Wall -Wextra) + add_test(NAME hid_report COMMAND test_report) +endif() + # Include directories target_include_directories(asus_wheel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} @@ -55,4 +63,4 @@ install(TARGETS asus_wheel install(FILES ${DAEMON_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/openwheel COMPONENT daemon-dev -) \ No newline at end of file +) diff --git a/openwheel-daemon/hidreader.c b/openwheel-daemon/hidreader.c index a818384..2114cbf 100644 --- a/openwheel-daemon/hidreader.c +++ b/openwheel-daemon/hidreader.c @@ -4,13 +4,22 @@ #include #include #include +#include +#include #include "openwheel.h" #include "helpers.h" +#include "report.h" -int main() { +int main(int argc, char **argv) { + int notify = 1; + if (argc == 2 && strcmp(argv[1], "--no-notify") == 0) notify = 0; + else if (argc != 1) { + fprintf(stderr, "Usage: %s [--no-notify]\n", argv[0]); + return 1; + } openlog("openwheel", LOG_PID, LOG_DAEMON); char device_path[BUFFER_SIZE]; @@ -37,57 +46,41 @@ int main() { dbus_error_free(&err); } if (connection == NULL) { + close(fd); return 1; } // Request the name 'org.asus.dial' - int ret = dbus_bus_request_name(connection, "org.asus.dial", DBUS_NAME_FLAG_REPLACE_EXISTING, &err); + int ret = dbus_bus_request_name(connection, "org.asus.dial", DBUS_NAME_FLAG_DO_NOT_QUEUE, &err); if (dbus_error_is_set(&err)) { syslog(LOG_ERR, "Failed to request D-Bus name 'org.asus.dial' (%s)", err.message); dbus_error_free(&err); } if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { syslog(LOG_ERR, "Failed to request D-Bus name 'org.asus.dial'"); + dbus_connection_unref(connection); + close(fd); return 1; } - WheelPacket pkt; + uint8_t packet[64]; + WheelEvent events[2]; int was_down = 0; - - send_notification("OpenWheel", "OpenWheel openwheel-daemon started"); + if (notify) send_notification("OpenWheel", "OpenWheel openwheel-daemon started"); while (1) { - int bytesRead = read(fd, &pkt, sizeof(pkt)); - if (bytesRead < 0) { - syslog(LOG_ERR, "Failed to read from HID device"); + ssize_t bytesRead = read(fd, packet, sizeof(packet)); + if (bytesRead < 0 && errno == EINTR) continue; + if (bytesRead <= 0) { + syslog(LOG_ERR, "HID device disconnected or read failed"); break; } - if (bytesRead == 4) { - if (pkt.rotation_hb == ROTATE_PLUS) { - send_dbus_signal(connection, DIAL_ROTATE_SIGNAL, 1); // Send rotation event - syslog(LOG_DEBUG, "sent rotate plus"); - } - - if (pkt.rotation_hb == ROTATE_MINUS) { - send_dbus_signal(connection, DIAL_ROTATE_SIGNAL, -1); - syslog(LOG_DEBUG,"sent rotate minus"); - } - - if (pkt.button == BUTTON_DOWN) { - send_dbus_signal(connection, DIAL_PRESS_SIGNAL, 1); - was_down = 1; - syslog(LOG_DEBUG,"sent button down"); - } - - if (pkt.button == BUTTON_UP && was_down) { - send_dbus_signal(connection, DIAL_PRESS_SIGNAL, 0); - was_down = 0; - syslog(LOG_DEBUG,"sent button up"); - } - } else { - syslog(LOG_ERR, "Malformed packet received"); + size_t count = wheel_process_report(packet, (size_t)bytesRead, &was_down, events); + for (size_t i = 0; i < count; i++) { + send_dbus_signal(connection, events[i].type == WHEEL_ROTATE ? + DIAL_ROTATE_SIGNAL : DIAL_PRESS_SIGNAL, events[i].value); } } - close(fd); - return 0; + dbus_connection_unref(connection); + return 1; /* Let a session supervisor reconnect after device removal. */ } diff --git a/openwheel-daemon/report.c b/openwheel-daemon/report.c new file mode 100644 index 0000000..2d2ab2d --- /dev/null +++ b/openwheel-daemon/report.c @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: MIT */ +#include "report.h" +size_t wheel_process_report(const uint8_t *bytes, size_t length, + int *button_down, WheelEvent events[2]) { + if (length != 4 || (bytes[0] != 0x01 && bytes[0] != 0x0a)) return 0; + if (bytes[0] == 0x0a && bytes[1] > 1) return 0; + int pressed = bytes[1] & 1; + unsigned encoded = (unsigned)bytes[2] | ((unsigned)bytes[3] << 8); + int delta = encoded >= 0x8000 ? (int)encoded - 0x10000 : (int)encoded; + size_t count = 0; + if (pressed && !*button_down) events[count++] = (WheelEvent){WHEEL_PRESS, 1}; + if (delta) events[count++] = (WheelEvent){WHEEL_ROTATE, delta}; + if (!pressed && *button_down) events[count++] = (WheelEvent){WHEEL_PRESS, 0}; + *button_down = pressed; + return count; +} diff --git a/openwheel-daemon/report.h b/openwheel-daemon/report.h new file mode 100644 index 0000000..839218a --- /dev/null +++ b/openwheel-daemon/report.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: MIT */ +#ifndef OPENWHEEL_REPORT_H +#define OPENWHEEL_REPORT_H +#include +#include +typedef enum { WHEEL_ROTATE, WHEEL_PRESS } WheelEventType; +typedef struct { WheelEventType type; int value; } WheelEvent; +/* Press-down precedes rotation, and release follows rotation, so consumers can + * distinguish a held rotation without adding another D-Bus signal. */ +size_t wheel_process_report(const uint8_t *bytes, size_t length, + int *button_down, WheelEvent events[2]); +#endif diff --git a/openwheel-daemon/test_report.c b/openwheel-daemon/test_report.c new file mode 100644 index 0000000..4a080aa --- /dev/null +++ b/openwheel-daemon/test_report.c @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: MIT */ +#include +#include +#include "report.h" +#define CHECK(condition) do { if (!(condition)) { \ + fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #condition); exit(1); \ +} } while (0) +int main(void) { + WheelEvent events[2]; + int down = 0; + const int deltas[] = {-32768, -3600, -257, -256, -2, -1, 0, 1, 2, 256, 257, 3600, 32767}; + const uint8_t reports[] = {0x01, 0x0a}; + for (size_t r = 0; r < sizeof(reports); r++) { + for (size_t i = 0; i < sizeof(deltas) / sizeof(deltas[0]); i++) { + unsigned encoded = (unsigned)(deltas[i] & 0xffff); + uint8_t packet[] = {reports[r], 0, encoded & 0xff, encoded >> 8}; + size_t count = wheel_process_report(packet, sizeof(packet), &down, events); + CHECK(count == (size_t)(deltas[i] != 0)); + if (count) CHECK(events[0].type == WHEEL_ROTATE && events[0].value == deltas[i]); + } + } + uint8_t packet[] = {0x0a, 1, 1, 0}; + CHECK(wheel_process_report(packet, 4, &down, events) == 2); + CHECK(events[0].type == WHEEL_PRESS && events[0].value == 1); + CHECK(events[1].type == WHEEL_ROTATE && events[1].value == 1); + CHECK(down == 1); + CHECK(wheel_process_report(packet, 4, &down, events) == 1); + CHECK(events[0].type == WHEEL_ROTATE); + packet[1] = 0; packet[2] = 0xff; packet[3] = 0xff; + CHECK(wheel_process_report(packet, 4, &down, events) == 2); + CHECK(events[0].type == WHEEL_ROTATE && events[0].value == -1); + CHECK(events[1].type == WHEEL_PRESS && events[1].value == 0); + CHECK(down == 0); + packet[2] = 0; packet[3] = 0; + CHECK(wheel_process_report(packet, 4, &down, events) == 0); + packet[1] = 3; + CHECK(wheel_process_report(packet, 4, &down, events) == 0 && down == 0); + packet[0] = 1; packet[1] = 0x81; + CHECK(wheel_process_report(packet, 4, &down, events) == 1 && down == 1); + packet[0] = 0x02; packet[1] = 0; + CHECK(wheel_process_report(packet, 4, &down, events) == 0 && down == 1); + CHECK(wheel_process_report(packet, 3, &down, events) == 0 && down == 1); + puts("HID report decoding and held-rotation ordering: passed"); + return 0; +} From b7ca2213d8b5c0a508a212bc49a415c921766a46 Mon Sep 17 00:00:00 2001 From: Lightmarey Date: Wed, 18 Nov 2026 15:55:24 +0800 Subject: [PATCH 2/4] Build and test the requested components in CI Install D-Bus and Qt development packages on the runner. Fail configuration for missing required dependencies and verify that requested executables were built and installed. Keep daemon-only checks independent of Qt and run the rotation tests without a display. Signed-off-by: Lightmarey --- .github/workflows/cmake-single-platform.yml | 74 ++++++++----- .gitignore | 5 +- BUILD.md | 29 ++--- CMakeLists.txt | 7 +- openwheel-daemon/CMakeLists.txt | 59 ++-------- openwheel-gadget/CMakeLists.txt | 115 ++++++-------------- tests/CMakeLists.txt | 78 ++++--------- tests/check_artifact.cmake | 3 + tests/test_rotation.cpp | 2 +- 9 files changed, 125 insertions(+), 247 deletions(-) create mode 100644 tests/check_artifact.cmake diff --git a/.github/workflows/cmake-single-platform.yml b/.github/workflows/cmake-single-platform.yml index 0ac33d9..a09f924 100644 --- a/.github/workflows/cmake-single-platform.yml +++ b/.github/workflows/cmake-single-platform.yml @@ -1,39 +1,53 @@ -# This starter workflow is for a CMake project running on a single platform. There is a different starter workflow if you need cross-platform coverage. -# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-multi-platform.yml -name: CMake on a single platform +name: Build and test on: push: - branches: [ "main" ] + branches: [main] pull_request: - branches: [ "main" ] + branches: [main] -env: - # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) - BUILD_TYPE: Release +permissions: + contents: read jobs: build: - # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. - # You can convert this to a matrix build if you need cross-platform coverage. - # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix - runs-on: ubuntu-latest - + name: ${{ matrix.component }} + runs-on: ubuntu-24.04 + strategy: + fail-fast: false + matrix: + include: + - component: daemon + gadget: 'OFF' + packages: libdbus-1-dev libsystemd-dev + - component: qt + gadget: 'ON' + packages: libdbus-1-dev libsystemd-dev qt6-base-dev qt6-declarative-dev libx11-dev libxtst-dev steps: - - uses: actions/checkout@v4 - - - name: Configure CMake - # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. - # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type - run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} - - - name: Build - # Build your program with the given configuration - run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} - - - name: Test - working-directory: ${{github.workspace}}/build - # Execute tests defined by the CMake configuration. - # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest -C ${{env.BUILD_TYPE}} - + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Install build dependencies + run: | + sudo apt-get update + sudo apt-get install --no-install-recommends -y build-essential cmake ninja-build pkg-config ${{ matrix.packages }} + - name: Configure + run: >- + cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release + -DBUILD_DAEMON=ON -DBUILD_GADGET=${{ matrix.gadget }} -DBUILD_TESTS=ON + - name: Build + run: cmake --build build --parallel 2 + - name: Test + run: ctest --test-dir build --output-on-failure --no-tests=error + - name: Verify installed executables + run: | + prefix="$RUNNER_TEMP/openwheel" + cmake --install build --prefix "$prefix" + test -x "$prefix/bin/asus_wheel" + if [ '${{ matrix.gadget }}' = 'ON' ]; then + test -x "$prefix/bin/openwheel-gadget" + ldd "$prefix/bin/openwheel-gadget" > "$RUNNER_TEMP/gadget-libraries.txt" + if grep -F 'not found' "$RUNNER_TEMP/gadget-libraries.txt"; then + exit 1 + fi + fi diff --git a/.gitignore b/.gitignore index 21d3fb4..791da87 100644 --- a/.gitignore +++ b/.gitignore @@ -8,14 +8,13 @@ CMakeCache.txt CMakeFiles/ cmake_install.cmake Makefile -*.cmake # Compiled binaries *.o *.so *.a -openwheel-gadget -asus_wheel +/bin/openwheel-gadget +/bin/asus_wheel # Qt/QML compiled files *.qmlc diff --git a/BUILD.md b/BUILD.md index b2d5435..f6af9cb 100644 --- a/BUILD.md +++ b/BUILD.md @@ -17,11 +17,11 @@ OpenWheel uses CMake as its build system with support for: - C11 compiler (for openwheel-daemon) - C++20 compiler (for openwheel-gadget) - pkg-config -- DBus development libraries +- D-Bus development libraries (for openwheel-daemon) ### For Gadget Component -- Qt5 (5.15+) or Qt6 -- Qt modules: Core, Gui, Widgets, Qml, Quick, DBus +- Qt6 with Core, Gui, Widgets, Qml, Quick, QuickControls2 and DBus +- Qt6 Test module when `BUILD_TESTS=ON` ### Optional - KDE Frameworks 6 (for enhanced features) @@ -107,11 +107,12 @@ ctest -j$(nproc) ### Available Tests -- `check_build_outputs` - Verifies build completed successfully -- `daemon_executable_exists` - Checks daemon binary exists (if built) -- `gadget_executable_exists` - Checks gadget binary exists (if built) -- `version_test` - Validates project version -- `config_summary` - Displays build configuration +- `hid_report` tests HID decoding and button/rotation ordering (daemon builds). +- `test_rotation` tests the Qt rotation handler (gadget builds). +- `daemon_executable_exists` and `gadget_executable_exists` check the requested build artifacts. + +The daemon-only build and tests require no Qt dependencies. +Configure with `cmake -S . -B build-daemon -DBUILD_GADGET=OFF`. ## Installation @@ -221,18 +222,6 @@ sudo pacman -S qt6-base qt6-declarative sudo pacman -S libx11 libxtst ``` -### Qt Version Issues - -If both Qt5 and Qt6 are installed and you want to use a specific version: - -```bash -# Force Qt6 -cmake -DQT_VERSION_MAJOR=6 .. - -# Force Qt5 -cmake -DQT_VERSION_MAJOR=5 .. -``` - ### Build Warnings To enable additional compiler warnings: diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f3d857..4bb44ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,9 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # Include standard CMake modules include(GNUInstallDirs) -include(CTest) +if(BUILD_TESTS) + enable_testing() +endif() # Add subdirectories if(BUILD_DAEMON) @@ -34,7 +36,6 @@ endif() # Enable testing if(BUILD_TESTS) - enable_testing() add_subdirectory(tests) message(STATUS "Testing enabled") endif() @@ -47,4 +48,4 @@ message(STATUS " Build daemon: ${BUILD_DAEMON}") message(STATUS " Build gadget: ${BUILD_GADGET}") message(STATUS " Build tests: ${BUILD_TESTS}") message(STATUS " Install prefix: ${CMAKE_INSTALL_PREFIX}") -message(STATUS "") \ No newline at end of file +message(STATUS "") diff --git a/openwheel-daemon/CMakeLists.txt b/openwheel-daemon/CMakeLists.txt index 3b860a3..921e583 100644 --- a/openwheel-daemon/CMakeLists.txt +++ b/openwheel-daemon/CMakeLists.txt @@ -1,17 +1,5 @@ -cmake_minimum_required(VERSION 3.16) -project(openwheel-daemon C) - -set(CMAKE_C_STANDARD 11) -set(CMAKE_C_STANDARD_REQUIRED ON) - -# Find required packages find_package(PkgConfig REQUIRED) -pkg_check_modules(DBUS dbus-1) - -if(NOT DBUS_FOUND) - message(WARNING "DBus not found - daemon will not be built") - return() -endif() +pkg_check_modules(DBUS REQUIRED IMPORTED_TARGET dbus-1) # Source files set(DAEMON_SRCS @@ -26,41 +14,14 @@ set(DAEMON_HEADERS report.h ) -# Create executable add_executable(asus_wheel ${DAEMON_SRCS} ${DAEMON_HEADERS}) - -if(BUILD_TESTS) - add_executable(test_report test_report.c report.c) - target_compile_options(test_report PRIVATE -Wall -Wextra) - add_test(NAME hid_report COMMAND test_report) +target_compile_features(asus_wheel PRIVATE c_std_11) +target_compile_options(asus_wheel PRIVATE -Wall -Wextra) +target_link_libraries(asus_wheel PRIVATE PkgConfig::DBUS) + +if(ENABLE_INSTALL) + install(TARGETS asus_wheel + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT daemon) + install(FILES ${DAEMON_HEADERS} + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/openwheel COMPONENT daemon-dev) endif() - -# Include directories -target_include_directories(asus_wheel PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR} - ${DBUS_INCLUDE_DIRS} -) - -# Link libraries -target_link_libraries(asus_wheel PRIVATE - ${DBUS_LIBRARIES} -) - -# Compile options -target_compile_options(asus_wheel PRIVATE - ${DBUS_CFLAGS_OTHER} - -Wall - -Wextra -) - -# Installation -install(TARGETS asus_wheel - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - COMPONENT daemon -) - -# Install headers (for development) -install(FILES ${DAEMON_HEADERS} - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/openwheel - COMPONENT daemon-dev -) diff --git a/openwheel-gadget/CMakeLists.txt b/openwheel-gadget/CMakeLists.txt index ab059e6..09d0bee 100644 --- a/openwheel-gadget/CMakeLists.txt +++ b/openwheel-gadget/CMakeLists.txt @@ -8,60 +8,19 @@ set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) -# Try to find Qt6 first -find_package(Qt6 QUIET COMPONENTS Core Gui Widgets Qml Quick QuickControls2 DBus) - -if(NOT Qt6_FOUND) - message(STATUS "Qt6 not found, trying Qt5...") - find_package(Qt5 5.15 QUIET COMPONENTS Core Gui Widgets Qml Quick QuickControls2 DBus) - if(NOT Qt5_FOUND) - message(WARNING "Neither Qt6 nor Qt5 found - gadget will not be built") - return() - endif() - set(QT_VERSION_MAJOR 5) -else() - set(QT_VERSION_MAJOR 6) - message(STATUS "Using Qt6") -endif() +find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Qml Quick QuickControls2 DBus) # Try to find KDE Frameworks (optional for basic build) -if(QT_VERSION_MAJOR EQUAL 6) - find_package(ECM QUIET NO_MODULE) - if(ECM_FOUND) - set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH}) - include(KDEInstallDirs) - include(KDECMakeSettings) - include(KDECompilerSettings NO_POLICY_SCOPE) - - find_package(KF6 QUIET COMPONENTS - Config - CoreAddons - I18n - WindowSystem - DBusAddons - ) - - if(KF6_FOUND) - set(HAVE_KF6 TRUE) - message(STATUS "KDE Frameworks 6 found") - endif() +find_package(ECM QUIET NO_MODULE) +if(ECM_FOUND) + list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH}) + find_package(KF6 QUIET COMPONENTS Config CoreAddons I18n WindowSystem DBusAddons) + if(KF6_FOUND) + message(STATUS "KDE Frameworks 6 found") endif() endif() -# Check for X11 -find_package(X11) -if(X11_FOUND) - find_package(X11 COMPONENTS XTest) - if(X11_XTest_FOUND) - set(HAVE_X11 TRUE) - add_definitions(-DHAVE_X11) - message(STATUS "X11 with XTest support enabled") - else() - message(WARNING "XTest not found - keyboard/mouse simulation will not be available") - endif() -else() - message(WARNING "X11 not found - keyboard/mouse simulation will not be available") -endif() +find_package(X11 QUIET COMPONENTS Xtst) # Source files set(openwheel_gadget_SRCS @@ -84,33 +43,19 @@ target_include_directories(openwheel-gadget PRIVATE ) # Link Qt libraries -if(QT_VERSION_MAJOR EQUAL 6) - target_link_libraries(openwheel-gadget - Qt6::Core - Qt6::Gui - Qt6::Widgets - Qt6::Qml - Qt6::Quick - Qt6::QuickControls2 - Qt6::DBus - Qt6::QuickControls2 - ) -else() - target_link_libraries(openwheel-gadget - Qt5::Core - Qt5::Gui - Qt5::Widgets - Qt5::Qml - Qt5::Quick - Qt5::QuickControls2 - Qt5::DBus - Qt5::QuickControls2 - ) -endif() +target_link_libraries(openwheel-gadget PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Widgets + Qt6::Qml + Qt6::Quick + Qt6::QuickControls2 + Qt6::DBus +) # Link KDE Frameworks if available -if(HAVE_KF6) - target_link_libraries(openwheel-gadget +if(KF6_FOUND) + target_link_libraries(openwheel-gadget PRIVATE KF6::ConfigCore KF6::CoreAddons KF6::I18n @@ -124,11 +69,15 @@ else() endif() # Link X11 libraries if available -if(HAVE_X11) - target_link_libraries(openwheel-gadget - ${X11_LIBRARIES} - ${X11_XTest_LIB} - ) +if(X11_FOUND) + target_compile_definitions(openwheel-gadget PRIVATE HAVE_X11) + target_link_libraries(openwheel-gadget PRIVATE X11::X11 X11::Xtst) +else() + message(STATUS "X11/XTest not found: building without X11 input simulation") +endif() + +if(NOT ENABLE_INSTALL) + return() endif() # Installation @@ -169,7 +118,7 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/ message(STATUS "") message(STATUS "Gadget Configuration Summary:") -message(STATUS " Qt version: ${QT_VERSION_MAJOR}") -message(STATUS " KDE Frameworks: ${HAVE_KF6}") -message(STATUS " X11 support: ${HAVE_X11}") -message(STATUS "") \ No newline at end of file +message(STATUS " Qt version: ${Qt6_VERSION}") +message(STATUS " KDE Frameworks: ${KF6_FOUND}") +message(STATUS " X11 support: ${X11_FOUND}") +message(STATUS "") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4374962..32aee6a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,65 +1,27 @@ -# Tests for OpenWheel - -set(CMAKE_AUTOMOC ON) -find_package(Qt6 REQUIRED COMPONENTS Core Test Gui) - -# Define source files needed for testing -set(TEST_COMMON_SRCS - ${CMAKE_SOURCE_DIR}/openwheel-gadget/src/core/rotationhandler.cpp - ${CMAKE_SOURCE_DIR}/openwheel-gadget/src/profiles/profile.cpp -) - -# Rotation test -add_executable(test_rotation test_rotation.cpp ${TEST_COMMON_SRCS}) -target_include_directories(test_rotation PRIVATE - ${CMAKE_SOURCE_DIR}/openwheel-gadget/src -) -target_link_libraries(test_rotation PRIVATE Qt6::Core Qt6::Test Qt6::Gui) -add_test(NAME test_rotation COMMAND test_rotation) - -# Basic sanity test - check if executables exist -add_test( - NAME check_build_outputs - COMMAND ${CMAKE_COMMAND} -E echo "Checking build outputs exist" -) - -# Test daemon executable exists if(BUILD_DAEMON) - add_test( - NAME daemon_executable_exists - COMMAND ${CMAKE_COMMAND} -E echo "Daemon executable check" - ) - set_tests_properties(daemon_executable_exists PROPERTIES - PASS_REGULAR_EXPRESSION "Daemon executable check" + add_executable(test_report + ../openwheel-daemon/test_report.c + ../openwheel-daemon/report.c ) + target_compile_features(test_report PRIVATE c_std_11) + target_compile_options(test_report PRIVATE -Wall -Wextra) + add_test(NAME hid_report COMMAND test_report) + add_test(NAME daemon_executable_exists + COMMAND ${CMAKE_COMMAND} -DTARGET_FILE=$ + -P ${CMAKE_CURRENT_SOURCE_DIR}/check_artifact.cmake) endif() -# Test gadget executable exists if(BUILD_GADGET) - add_test( - NAME gadget_executable_exists - COMMAND ${CMAKE_COMMAND} -E echo "Gadget executable check" - ) - set_tests_properties(gadget_executable_exists PROPERTIES - PASS_REGULAR_EXPRESSION "Gadget executable check" + find_package(Qt6 REQUIRED COMPONENTS Core Test) + add_executable(test_rotation test_rotation.cpp + ../openwheel-gadget/src/core/rotationhandler.cpp + ../openwheel-gadget/src/profiles/profile.cpp ) + set_target_properties(test_rotation PROPERTIES AUTOMOC ON) + target_include_directories(test_rotation PRIVATE ../openwheel-gadget/src) + target_link_libraries(test_rotation PRIVATE Qt6::Core Qt6::Test) + add_test(NAME test_rotation COMMAND test_rotation) + add_test(NAME gadget_executable_exists + COMMAND ${CMAKE_COMMAND} -DTARGET_FILE=$ + -P ${CMAKE_CURRENT_SOURCE_DIR}/check_artifact.cmake) endif() - -# Add version test -add_test( - NAME version_test - COMMAND ${CMAKE_COMMAND} -E echo "OpenWheel version ${PROJECT_VERSION}" -) -set_tests_properties(version_test PROPERTIES - PASS_REGULAR_EXPRESSION "${PROJECT_VERSION}" -) - -# Configuration test -add_test( - NAME config_summary - COMMAND ${CMAKE_COMMAND} -E echo "Build configuration: ${CMAKE_BUILD_TYPE}" -) - -message(STATUS "Test configuration:") -message(STATUS " CTest enabled: ON") -message(STATUS " Test build type: ${CMAKE_BUILD_TYPE}") diff --git a/tests/check_artifact.cmake b/tests/check_artifact.cmake new file mode 100644 index 0000000..65d424f --- /dev/null +++ b/tests/check_artifact.cmake @@ -0,0 +1,3 @@ +if(NOT EXISTS "${TARGET_FILE}" OR IS_DIRECTORY "${TARGET_FILE}") + message(FATAL_ERROR "Expected a built executable at ${TARGET_FILE}") +endif() diff --git a/tests/test_rotation.cpp b/tests/test_rotation.cpp index 52d8015..b9faedd 100644 --- a/tests/test_rotation.cpp +++ b/tests/test_rotation.cpp @@ -111,5 +111,5 @@ void TestRotation::testBaseThreshold() QVERIFY(spy.count() >= 1); // Now accumulated 35 > 30 } -QTEST_MAIN(TestRotation) +QTEST_GUILESS_MAIN(TestRotation) #include "test_rotation.moc" From 03e50c3ff219d2da8cbd2b13fd60f41642571544 Mon Sep 17 00:00:00 2001 From: Lightmarey Date: Wed, 18 Nov 2026 15:55:24 +0800 Subject: [PATCH 3/4] Harden dial report handling and preserve gadget tick behavior Keep the full delta on D-Bus while feeding the legacy gadget one tick per accepted report. Check both packet formats in Release tests and report device open, disconnect and read failures separately. Signed-off-by: Lightmarey --- openwheel-daemon/helpers.c | 15 +-- openwheel-daemon/hidreader.c | 45 ++++---- openwheel-daemon/report.c | 39 +++++-- openwheel-daemon/report.h | 33 +++++- openwheel-daemon/test_report.c | 106 ++++++++++++------- openwheel-gadget/src/core/dialcontroller.cpp | 8 +- 6 files changed, 169 insertions(+), 77 deletions(-) diff --git a/openwheel-daemon/helpers.c b/openwheel-daemon/helpers.c index fc65392..1ef897f 100644 --- a/openwheel-daemon/helpers.c +++ b/openwheel-daemon/helpers.c @@ -166,7 +166,8 @@ int find_hidraw_device(char *device_path, size_t path_size) { // Iterate through the hidrawX directories while ((entry = readdir(dp)) != NULL) { if (entry->d_type == DT_LNK) { - char sysfs_device_path[BUFFER_SIZE]; + char sysfs_device_path[sizeof(SYSFS_HIDRAW_PATH) + + sizeof(entry->d_name) + sizeof("/device/product")]; char file_content[BUFFER_SIZE]; // Try USB path first: device/product @@ -175,9 +176,9 @@ int find_hidraw_device(char *device_path, size_t path_size) { if (read_sysfs_file(sysfs_device_path, file_content, sizeof(file_content)) == 0) { if (strstr(file_content, DEVICE_NAME)) { - snprintf(device_path, path_size, "/dev/%s", entry->d_name); + int length = snprintf(device_path, path_size, "/dev/%s", entry->d_name); closedir(dp); - return 0; + return length >= 0 && (size_t)length < path_size ? 0 : -1; } } @@ -190,9 +191,9 @@ int find_hidraw_device(char *device_path, size_t path_size) { while (fgets(file_content, sizeof(file_content), fp) != NULL) { if (strstr(file_content, DEVICE_NAME)) { fclose(fp); - snprintf(device_path, path_size, "/dev/%s", entry->d_name); + int length = snprintf(device_path, path_size, "/dev/%s", entry->d_name); closedir(dp); - return 0; + return length >= 0 && (size_t)length < path_size ? 0 : -1; } } fclose(fp); @@ -233,6 +234,7 @@ void send_notification(const char *summary, const char *body) { if (msg == NULL) { fprintf(stderr, "Message Null\n"); + dbus_connection_unref(conn); return; } @@ -268,4 +270,5 @@ void send_notification(const char *summary, const char *body) { dbus_connection_flush(conn); dbus_message_unref(msg); -} \ No newline at end of file + dbus_connection_unref(conn); +} diff --git a/openwheel-daemon/hidreader.c b/openwheel-daemon/hidreader.c index 2114cbf..3dd3214 100644 --- a/openwheel-daemon/hidreader.c +++ b/openwheel-daemon/hidreader.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -10,13 +9,9 @@ #include "openwheel.h" #include "helpers.h" #include "report.h" - - - int main(int argc, char **argv) { - int notify = 1; - if (argc == 2 && strcmp(argv[1], "--no-notify") == 0) notify = 0; - else if (argc != 1) { + const bool notify = argc == 1; + if (!notify && (argc != 2 || strcmp(argv[1], "--no-notify") != 0)) { fprintf(stderr, "Usage: %s [--no-notify]\n", argv[0]); return 1; } @@ -29,9 +24,9 @@ int main(int argc, char **argv) { } syslog(LOG_INFO, "Found ASUS Dial at %s", device_path); - int fd = open(device_path, O_RDONLY); + int fd = open(device_path, O_RDONLY | O_CLOEXEC); if (fd < 0) { - syslog(LOG_ERR, "Failed to open HID device %s", device_path); + syslog(LOG_ERR, "Failed to open HID device %s: %s", device_path, strerror(errno)); return 1; } @@ -64,20 +59,30 @@ int main(int argc, char **argv) { } uint8_t packet[64]; - WheelEvent events[2]; - int was_down = 0; - if (notify) send_notification("OpenWheel", "OpenWheel openwheel-daemon started"); - while (1) { - ssize_t bytesRead = read(fd, packet, sizeof(packet)); - if (bytesRead < 0 && errno == EINTR) continue; - if (bytesRead <= 0) { - syslog(LOG_ERR, "HID device disconnected or read failed"); + WheelEvent events[WHEEL_MAX_EVENTS]; + bool button_down = false; + if (notify) { + send_notification("OpenWheel", "OpenWheel openwheel-daemon started"); + } + for (;;) { + ssize_t bytes_read = read(fd, packet, sizeof(packet)); + if (bytes_read < 0) { + if (errno == EINTR) { + continue; + } + syslog(LOG_ERR, "HID read failed for %s: %s", device_path, strerror(errno)); break; } - size_t count = wheel_process_report(packet, (size_t)bytesRead, &was_down, events); + if (bytes_read == 0) { + syslog(LOG_ERR, "HID device disconnected: %s", device_path); + break; + } + + size_t count = wheel_process_report(packet, (size_t)bytes_read, &button_down, events); for (size_t i = 0; i < count; i++) { - send_dbus_signal(connection, events[i].type == WHEEL_ROTATE ? - DIAL_ROTATE_SIGNAL : DIAL_PRESS_SIGNAL, events[i].value); + const char *name = events[i].type == WHEEL_ROTATE + ? DIAL_ROTATE_SIGNAL : DIAL_PRESS_SIGNAL; + send_dbus_signal(connection, name, events[i].value); } } close(fd); diff --git a/openwheel-daemon/report.c b/openwheel-daemon/report.c index 2d2ab2d..8129592 100644 --- a/openwheel-daemon/report.c +++ b/openwheel-daemon/report.c @@ -1,16 +1,37 @@ /* SPDX-License-Identifier: MIT */ #include "report.h" + +enum { + REPORT_SIZE = 4, + REPORT_ID_LEGACY = 0x01, + REPORT_ID_ASUS2020 = 0x0a, + BUTTON_BIT = 0x01, +}; + size_t wheel_process_report(const uint8_t *bytes, size_t length, - int *button_down, WheelEvent events[2]) { - if (length != 4 || (bytes[0] != 0x01 && bytes[0] != 0x0a)) return 0; - if (bytes[0] == 0x0a && bytes[1] > 1) return 0; - int pressed = bytes[1] & 1; - unsigned encoded = (unsigned)bytes[2] | ((unsigned)bytes[3] << 8); - int delta = encoded >= 0x8000 ? (int)encoded - 0x10000 : (int)encoded; + bool *button_down, WheelEvent events[WHEEL_MAX_EVENTS]) +{ + if (length != REPORT_SIZE || + (bytes[0] != REPORT_ID_LEGACY && bytes[0] != REPORT_ID_ASUS2020)) { + return 0; + } + if (bytes[0] == REPORT_ID_ASUS2020 && bytes[1] > BUTTON_BIT) { + return 0; + } + + const bool pressed = (bytes[1] & BUTTON_BIT) != 0; + const int encoded = bytes[2] | (bytes[3] << 8); + const int delta = encoded >= 0x8000 ? encoded - 0x10000 : encoded; size_t count = 0; - if (pressed && !*button_down) events[count++] = (WheelEvent){WHEEL_PRESS, 1}; - if (delta) events[count++] = (WheelEvent){WHEEL_ROTATE, delta}; - if (!pressed && *button_down) events[count++] = (WheelEvent){WHEEL_PRESS, 0}; + if (pressed && !*button_down) { + events[count++] = (WheelEvent){WHEEL_PRESS, 1}; + } + if (delta != 0) { + events[count++] = (WheelEvent){WHEEL_ROTATE, delta}; + } + if (!pressed && *button_down) { + events[count++] = (WheelEvent){WHEEL_PRESS, 0}; + } *button_down = pressed; return count; } diff --git a/openwheel-daemon/report.h b/openwheel-daemon/report.h index 839218a..20309e2 100644 --- a/openwheel-daemon/report.h +++ b/openwheel-daemon/report.h @@ -1,12 +1,35 @@ /* SPDX-License-Identifier: MIT */ #ifndef OPENWHEEL_REPORT_H #define OPENWHEEL_REPORT_H + +#include #include #include -typedef enum { WHEEL_ROTATE, WHEEL_PRESS } WheelEventType; -typedef struct { WheelEventType type; int value; } WheelEvent; -/* Press-down precedes rotation, and release follows rotation, so consumers can - * distinguish a held rotation without adding another D-Bus signal. */ + +#ifdef __cplusplus +extern "C" { +#endif + +enum { WHEEL_MAX_EVENTS = 2 }; + +typedef enum { + WHEEL_ROTATE, + WHEEL_PRESS, +} WheelEventType; + +typedef struct { + WheelEventType type; + int value; +} WheelEvent; + +/* Initialize button_down to false when opening a device. A report produces at + * most one button edge and one rotation. A press precedes rotation and a + * release follows it so consumers can recognize a held rotation. */ size_t wheel_process_report(const uint8_t *bytes, size_t length, - int *button_down, WheelEvent events[2]); + bool *button_down, WheelEvent events[WHEEL_MAX_EVENTS]); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/openwheel-daemon/test_report.c b/openwheel-daemon/test_report.c index 4a080aa..68be007 100644 --- a/openwheel-daemon/test_report.c +++ b/openwheel-daemon/test_report.c @@ -1,45 +1,79 @@ /* SPDX-License-Identifier: MIT */ +#include "report.h" + #include #include -#include "report.h" -#define CHECK(condition) do { if (!(condition)) { \ - fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #condition); exit(1); \ -} } while (0) -int main(void) { - WheelEvent events[2]; - int down = 0; + +#define CHECK(condition) do { \ + if (!(condition)) { \ + fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #condition); \ + exit(EXIT_FAILURE); \ + } \ +} while (0) + +static void test_signed_delta(uint8_t report_id) +{ const int deltas[] = {-32768, -3600, -257, -256, -2, -1, 0, 1, 2, 256, 257, 3600, 32767}; - const uint8_t reports[] = {0x01, 0x0a}; - for (size_t r = 0; r < sizeof(reports); r++) { - for (size_t i = 0; i < sizeof(deltas) / sizeof(deltas[0]); i++) { - unsigned encoded = (unsigned)(deltas[i] & 0xffff); - uint8_t packet[] = {reports[r], 0, encoded & 0xff, encoded >> 8}; - size_t count = wheel_process_report(packet, sizeof(packet), &down, events); - CHECK(count == (size_t)(deltas[i] != 0)); - if (count) CHECK(events[0].type == WHEEL_ROTATE && events[0].value == deltas[i]); + WheelEvent events[WHEEL_MAX_EVENTS]; + bool button_down = false; + + for (size_t i = 0; i < sizeof(deltas) / sizeof(deltas[0]); i++) { + const uint16_t encoded = (uint16_t)deltas[i]; + const uint8_t report[] = {report_id, 0, encoded & 0xff, encoded >> 8}; + size_t count = wheel_process_report(report, sizeof(report), &button_down, events); + CHECK(count == (size_t)(deltas[i] != 0)); + if (count != 0) { + CHECK(events[0].type == WHEEL_ROTATE && events[0].value == deltas[i]); } + CHECK(!button_down); } - uint8_t packet[] = {0x0a, 1, 1, 0}; - CHECK(wheel_process_report(packet, 4, &down, events) == 2); +} + +static void test_button_edges(uint8_t report_id, uint8_t button_flags) +{ + const uint8_t press[] = {report_id, button_flags | 1, 0x01, 0x01}; + const uint8_t release[] = {report_id, button_flags, 0xff, 0xfe}; + const uint8_t idle[] = {report_id, button_flags, 0, 0}; + WheelEvent events[WHEEL_MAX_EVENTS]; + bool button_down = false; + + CHECK(wheel_process_report(press, sizeof(press), &button_down, events) == 2); CHECK(events[0].type == WHEEL_PRESS && events[0].value == 1); - CHECK(events[1].type == WHEEL_ROTATE && events[1].value == 1); - CHECK(down == 1); - CHECK(wheel_process_report(packet, 4, &down, events) == 1); - CHECK(events[0].type == WHEEL_ROTATE); - packet[1] = 0; packet[2] = 0xff; packet[3] = 0xff; - CHECK(wheel_process_report(packet, 4, &down, events) == 2); - CHECK(events[0].type == WHEEL_ROTATE && events[0].value == -1); + CHECK(events[1].type == WHEEL_ROTATE && events[1].value == 257); + CHECK(button_down); + CHECK(wheel_process_report(press, sizeof(press), &button_down, events) == 1); + CHECK(events[0].type == WHEEL_ROTATE && events[0].value == 257); + CHECK(wheel_process_report(release, sizeof(release), &button_down, events) == 2); + CHECK(events[0].type == WHEEL_ROTATE && events[0].value == -257); CHECK(events[1].type == WHEEL_PRESS && events[1].value == 0); - CHECK(down == 0); - packet[2] = 0; packet[3] = 0; - CHECK(wheel_process_report(packet, 4, &down, events) == 0); - packet[1] = 3; - CHECK(wheel_process_report(packet, 4, &down, events) == 0 && down == 0); - packet[0] = 1; packet[1] = 0x81; - CHECK(wheel_process_report(packet, 4, &down, events) == 1 && down == 1); - packet[0] = 0x02; packet[1] = 0; - CHECK(wheel_process_report(packet, 4, &down, events) == 0 && down == 1); - CHECK(wheel_process_report(packet, 3, &down, events) == 0 && down == 1); - puts("HID report decoding and held-rotation ordering: passed"); - return 0; + CHECK(!button_down); + CHECK(wheel_process_report(idle, sizeof(idle), &button_down, events) == 0); +} + +static void test_ignored_reports(void) +{ + const uint8_t other_id[] = {0x02, 0, 1, 0}; + const uint8_t other_asus2020[] = {0x0a, 3, 1, 0}; + const uint8_t wrong_length[] = {0x0a, 0, 1, 0, 0}; + WheelEvent events[WHEEL_MAX_EVENTS]; + bool button_down = true; + + CHECK(wheel_process_report(other_id, sizeof(other_id), &button_down, events) == 0); + CHECK(button_down); + CHECK(wheel_process_report(other_asus2020, sizeof(other_asus2020), &button_down, events) == 0); + CHECK(button_down); + CHECK(wheel_process_report(wrong_length, sizeof(wrong_length), &button_down, events) == 0); + CHECK(button_down); + CHECK(wheel_process_report(wrong_length, 3, &button_down, events) == 0); + CHECK(button_down); +} + +int main(void) +{ + test_signed_delta(0x01); + test_signed_delta(0x0a); + test_button_edges(0x01, 0x80); + test_button_edges(0x0a, 0); + test_ignored_reports(); + return EXIT_SUCCESS; } diff --git a/openwheel-gadget/src/core/dialcontroller.cpp b/openwheel-gadget/src/core/dialcontroller.cpp index 706f4c8..64327b0 100644 --- a/openwheel-gadget/src/core/dialcontroller.cpp +++ b/openwheel-gadget/src/core/dialcontroller.cpp @@ -39,7 +39,13 @@ void DialController::setupConnections() { // D-Bus interface connections connect(m_dbusInterface.get(), &DBusInterface::rotationChanged, - this, &DialController::onRotationChanged); + this, [this](int delta) { + // The gadget's thresholds and synchronous actions are tuned + // for one tick per report; the bus carries the full value. + if (delta != 0) { + onRotationChanged(delta > 0 ? 1 : -1); + } + }); connect(m_dbusInterface.get(), &DBusInterface::buttonPressed, this, &DialController::onButtonPressed); connect(m_dbusInterface.get(), &DBusInterface::buttonReleased, From 76d40b461d2ec8732d489792acf0b1a9358bb09e Mon Sep 17 00:00:00 2001 From: Lightmarey Date: Wed, 18 Nov 2026 16:07:25 +0800 Subject: [PATCH 4/4] Make the acceleration test independent of runner timing Signed-off-by: Lightmarey --- tests/test_rotation.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/test_rotation.cpp b/tests/test_rotation.cpp index b9faedd..0a18e56 100644 --- a/tests/test_rotation.cpp +++ b/tests/test_rotation.cpp @@ -10,7 +10,7 @@ class TestRotation : public QObject private Q_SLOTS: void testInitialState(); - void testAccelerationCurve(); + void testAccelerationRespectsMinimumThreshold(); void testReset(); void testZeroDeltaIgnored(); void testActionTriggered(); @@ -25,21 +25,22 @@ void TestRotation::testInitialState() QCOMPARE(handler.isAdjusting(), 0); } -void TestRotation::testAccelerationCurve() +void TestRotation::testAccelerationRespectsMinimumThreshold() { - // Test via the public interface: slow rotation should not accelerate RotationHandler handler; handler.setAccelerationEnabled(1); handler.setBaseThreshold(15); - // A single small rotation shouldn't trigger (below threshold) + // Three degrees remain below the threshold at every acceleration factor. + // Elapsed time between unit-test calls varies on loaded runners. QSignalSpy spy(&handler, &RotationHandler::actionTriggered); - handler.onRotation(10); + for (int i = 0; i < 3; ++i) { + handler.onRotation(1); + } QCOMPARE(spy.count(), 0); - // Enough rotation should trigger - handler.onRotation(10); - QVERIFY(spy.count() >= 1); + handler.onRotation(15); + QCOMPARE(spy.count(), 1); } void TestRotation::testReset()