Skip to content

Add SpaceMouse support for Linux via libspnav #20619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jellespijker
Copy link
Member

This change introduces support for 3Dconnexion SpaceMouse devices on Linux within the 3DConnexion plugin. This PR has been created with: https://jules.google.com/ and is untested

Key changes:

  • Added LinuxSpacenavClient.py, which uses ctypes to interface with the
    system-provided libspnav.so.0 library. This client handles opening a
    connection to the spacenavd daemon and polling for motion and button
    events.
  • Modified NavlibClient.py to include platform detection. On Linux, it
    now uses LinuxSpacenavClient for device input. On Windows and macOS,
    it continues to use the existing pynavlib.
  • Updated the plugin initialization in plugins/3DConnexion/__init__.py
    to gracefully handle cases where libspnav.so.0 might be missing or
    spacenavd is not accessible on Linux, disabling the plugin in such
    scenarios.
  • The core camera manipulation logic in NavlibClient.py has been adapted
    to accept transformation matrices from either pynavlib or the new
    Linux client, aiming for consistent behavior.
  • Placeholder adaptations for some pynavlib-specific methods have been
    added for the Linux path, returning None or basic Python types where
    pynav.* types were previously used.

This implementation relies on you having spacenavd (version 0.6 or newer recommended) installed and running, along with libspnav0 (or equivalent).

Testing for this feature is currently manual, involving checking device response for camera manipulation (pan, zoom, rotate) within Cura on a Linux environment with a configured SpaceMouse.

Should fix: #20578

This change introduces support for 3Dconnexion SpaceMouse devices on Linux
within the 3DConnexion plugin.

Key changes:
-   Added `LinuxSpacenavClient.py`, which uses `ctypes` to interface with the
    system-provided `libspnav.so.0` library. This client handles opening a
    connection to the `spacenavd` daemon and polling for motion and button
    events.
-   Modified `NavlibClient.py` to include platform detection. On Linux, it
    now uses `LinuxSpacenavClient` for device input. On Windows and macOS,
    it continues to use the existing `pynavlib`.
-   Updated the plugin initialization in `plugins/3DConnexion/__init__.py`
    to gracefully handle cases where `libspnav.so.0` might be missing or
    `spacenavd` is not accessible on Linux, disabling the plugin in such
    scenarios.
-   The core camera manipulation logic in `NavlibClient.py` has been adapted
    to accept transformation matrices from either `pynavlib` or the new
    Linux client, aiming for consistent behavior.
-   Placeholder adaptations for some `pynavlib`-specific methods have been
    added for the Linux path, returning `None` or basic Python types where
    `pynav.*` types were previously used.

This implementation relies on you having `spacenavd` (version 0.6 or newer recommended)
installed and running, along with `libspnav0` (or equivalent).

Testing for this feature is currently manual, involving checking device
response for camera manipulation (pan, zoom, rotate) within Cura on a
Linux environment with a configured SpaceMouse.

Output:
@jellespijker jellespijker requested a review from Copilot May 23, 2025 14:28
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces support for 3Dconnexion SpaceMouse devices on Linux by adding a new Linux client that interfaces with libspnav and adapting existing navigation logic to use it. Key changes include:

  • Adding LinuxSpacenavClient.py to handle ctypes integration with libspnav.
  • Updating NavlibClient.py with Linux-specific event polling, return type adjustments, and conditional initialization.
  • Modifying the plugin registration in init.py to disable the plugin gracefully on Linux when initialization fails.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
plugins/3DConnexion/init.py Adjusts plugin registration to check Linux client availability and disable the plugin on failure.
plugins/3DConnexion/NavlibClient.py Integrates Linux support with conditional initialization and adjusted return types for Linux paths.
plugins/3DConnexion/LinuxSpacenavClient.py Provides a new client that uses ctypes to interface with libspnav for polling motion and button events.
Comments suppressed due to low confidence (1)

plugins/3DConnexion/init.py:38

  • Catching BaseException is too broad and may hide critical exceptions; consider catching Exception instead.
except BaseException as exception:


def get_camera_matrix(self) -> "pynav.NavlibMatrix":
def get_camera_matrix(self) -> "pynav.NavlibMatrix" or Matrix: # Adjusted return type
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Using 'or' to denote a union type in type annotations is non-standard; consider using Union[pynav.NavlibMatrix, Matrix] for clarity.

Suggested change
def get_camera_matrix(self) -> "pynav.NavlibMatrix" or Matrix: # Adjusted return type
def get_camera_matrix(self) -> Union["pynav.NavlibMatrix", Matrix]: # Adjusted return type

Copilot uses AI. Check for mistakes.

self._scene_radius = 1.0 # Used in set_camera_matrix, ensure it exists


if self._platform_system == "Linux":
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The repeated platform checks for "Linux" throughout the file could be centralized to improve maintainability.

Suggested change
if self._platform_system == "Linux":
if self._is_linux():

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Test Results

23 783 tests   23 781 ✅  46s ⏱️
     1 suites       2 💤
     1 files         0 ❌

Results for commit 4698089.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SpaceMouse integration: Please add Linux support (spacenavd)
1 participant