Coder Social home page Coder Social logo

Comments (11)

xM8WVqaG avatar xM8WVqaG commented on August 17, 2024 1

This is the last version I was testing. The only thing this is missing is the new libusb resolver but I think @wheaney fixed that upstream already so better to build off a newer version.

default.nix

{ lib
  , stdenv
  , fetchgit
  , cmake
  , pkg-config

  # deps
  , systemd
  , json_c
  , libevdev
  , libusb
  , hidapi
}:

stdenv.mkDerivation rec {
  pname = "xr-linux-driver";
  version = "0.6.1-beta";

  src = (fetchgit {
    url = "https://github.com/wheaney/XRLinuxDriver";
    rev = "0f74025ccee6d9017f07e39db9bcdacfaac7e134";
    sha256 = "sha256-fpIxNiMW8IelrNMuHtOPePIxeI1sxUNigZq5f/CqjyA=";
    fetchSubmodules = true;
  }).overrideAttrs (_: {
    GIT_CONFIG_COUNT = 1;
    GIT_CONFIG_KEY_0 = "url.https://gitlab.com/.insteadOf";
    GIT_CONFIG_VALUE_0 = "[email protected]:";
  });

  buildInputs = [
    systemd
    json_c
    libevdev
    libusb
    hidapi
  ];

  nativeBuildInputs = [
    pkg-config
    cmake
  ];

  dontStrip = true;

  outputs = [ "out" ];

  patches = [
    ./install.patch
    ./unlink-hidapi.patch
  ];

  meta = with lib; {
    description = "Custom user-space Linux driver for XR devices";
    homepage = "https://github.com/wheaney/XRLinuxDriver";
    license = licenses.mit;
    platforms = platforms.linux;
    maintainers = [ "wheaney" ];
  };
}

install.patch

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9d55cec..dbd8c5b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -55,4 +55,6 @@ target_include_directories(xrealAirLinuxDriver
 
 target_link_libraries(xrealAirLinuxDriver
 		PRIVATE ${LIBEVDEV_LIBRARIES} nrealAirLibrary Threads::Threads m ${VITURE_LIB_NAME} ${LIBUSB_LIBRARY}
-)
\ No newline at end of file
+)
+
+install(TARGETS xrealAirLinuxDriver DESTINATION bin)

unlink-hidapi.patch

diff --git a/modules/xrealInterfaceLibrary/.gitmodules b/modules/xrealInterfaceLibrary/.gitmodules
index 8696ec6..2b8a1a0 100644
--- a/modules/xrealInterfaceLibrary/.gitmodules
+++ b/modules/xrealInterfaceLibrary/.gitmodules
@@ -1,6 +1,3 @@
 [submodule "interface_lib/modules/Fusion"]
 	path = interface_lib/modules/Fusion
 	url = https://github.com/xioTechnologies/Fusion.git
-[submodule "interface_lib/modules/hidapi"]
-	path = interface_lib/modules/hidapi
-	url = https://github.com/libusb/hidapi.git
diff --git a/modules/xrealInterfaceLibrary/interface_lib/CMakeLists.txt b/modules/xrealInterfaceLibrary/interface_lib/CMakeLists.txt
index 1fc3449..e57fdfb 100644
--- a/modules/xrealInterfaceLibrary/interface_lib/CMakeLists.txt
+++ b/modules/xrealInterfaceLibrary/interface_lib/CMakeLists.txt
@@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD 17)
 
 find_package(json-c REQUIRED CONFIG)
 
-add_subdirectory(modules/hidapi)
+find_package(hidapi REQUIRED)
 add_subdirectory(modules/Fusion/Fusion)
 
 add_library(
@@ -25,7 +25,6 @@ target_include_directories(nrealAirLibrary
 
 target_include_directories(nrealAirLibrary
 		SYSTEM BEFORE PRIVATE 
-		${CMAKE_CURRENT_SOURCE_DIR}/modules/hidapi
 		${CMAKE_CURRENT_SOURCE_DIR}/modules/Fusion
 )

Build it with:

nix-build -E 'let pkgs = import <nixpkgs> { }; in pkgs.callPackage ./default.nix {}'

The binary will be available in ./results.

To create the systemd unit and enable the kernel module you'll want something like:

systemd.services.xr-linux-driver = {
  enable = true;
  description = "xr-linux-driver";
  after = [ "network.target" ];

  serviceConfig = {
    Type = "simple";
    Environment = "HOME=/tmp/xr-linux-driver";
    ExecStart = "/nix/store/j3rf49f3b06cni2ynyrz94alqmhp6z3d-xr-linux-driver-0.6.1-beta/bin/xrealAirLinuxDriver";
    Restart = "on-failure";
    PrivateNetwork = "true";
    # PrivateTmp = "true";
  };

  wantedBy = [ "multi-user.target" ];
};

boot.kernelModules = [ "uinput" ];

You can optionally change the build type to get more noise in logs:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9d55cec..305d468 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,14 +1,15 @@
 cmake_minimum_required(VERSION 3.16)
 project(xrealAirLinuxDriver C)
 
-set(CMAKE_BUILD_TYPE Release)
+#set(CMAKE_BUILD_TYPE Release)
+set(CMAKE_BUILD_TYPE Debug)
 set(CMAKE_C_STANDARD 17)
 set(THREADS_PREFER_PTHREAD_FLAG ON)
 find_package(Threads REQUIRED)
 
 list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
 find_package(LIBEVDEV REQUIRED)
 find_package(LIBUSB REQUIRED)
 
 set(VITURE_LIB_NAME viture_sdk)

I am unable to get the original binary from releases to work on my SteamDeck since I updated the firmware of my glasses so I'm unable to confirm how well this actually works. My glasses are fine otherwise, and I can read data from them without issue, just this software never does anything after connecting to the glasses.

from xrlinuxdriver.

canguy247 avatar canguy247 commented on August 17, 2024 1

I am using Hyprland on Wayland

from xrlinuxdriver.

wheaney avatar wheaney commented on August 17, 2024

Wow, this is interesting. I'm not familiar with Nix so this isn't something I would tackle until I've achieved some of my longer term goals and had time to dig into it.

A couple things I see:

  1. The USB device you're seeing is the glasses itself. My driver doesn't create a new USB device, so you'll never see it created there. It does create a new uinput device through libevdev when in joystick mode, but for mouse mode I believe it'll just modify the uinput fp for the existing mouse device.
  2. Your log is indicating that it never sees the device connect (due to never leaving the Waiting for glasses state)

So your libinput observations are just a red herring, the more important piece is that it seems like the hidapi library that gets used by my driver under the hood isn't picking up the USB device at all, even though clearly the USB device is registered by your OS. I would recommend double-checking that the daemon isn't crashing by checking systemctl status xreal-air-driver (or I guess xr-linux-driver?). Otherwise, you'd need to debug the hidapi integration or even just create a simple test program that queries hidapi directly for easier debugging.

from xrlinuxdriver.

xM8WVqaG avatar xM8WVqaG commented on August 17, 2024

Thanks for the rapid response! I appreciate this usecase isn't something you currently support so I'm grateful for the suggestions.

I had a little more time tonight to play with this but didn't solve it yet.

I would recommend double-checking that the daemon isn't crashing

I can confirm it's happily idle and not crashing. It seems to be oblivious to the world!

Based on your suggestion to poke around hidapi:

  • I patched out the submodule'd hidapi to use the "system" one. It linked fine, but no dice. Turns out they were the same version.
  • I wrote some new udev rules just in case it was a perms thing still, but that didn't seem to have an impact.
  • I tried building the upstream nrealAirLinuxDriver project by itself using the same build script and noticed it was failing looking for udev and I had only provided evdev. I imported systemd for that and confirmed that did get linked to xrealAirLinuxDriver so something did want that.

After that, next time I started the service the logs were:

Starting up XR driver
Error calling shmat
Starting up XR driver
Error calling shmat
Starting up XR driver

But given this error is from ipc.c it seems to be another red herring.

I'll have another think about next steps for debugging hidapi locally. Thanks again!

from xrlinuxdriver.

xM8WVqaG avatar xM8WVqaG commented on August 17, 2024

@wheaney I had a bit of time this morning to have another play with this. It still doesn't create a device for joystick or influence cursor movement. but I can confirm it's reading data from the glasses.

I changed the CMake build type from Release to Debug to run it via gdb and that started dumping loads more bits in the logs:

Starting up XR driver
Found device with product_id: 0x424
G: -0.47 -0.21 -0.22
A: -0.99 -0.05 -0.02
M: -1.00 -1.00 -1.00
ERROR: Not matching signature
Found device with product_id: 0x424
MCU: 07.1.02.313_20231027
DP: 112E
DSP: 07.1.00.001_1.0.1.7_1.1
Brightness: 7
Disp-Mode: 1
Error calling shmat
Starting up XR driver
...

At this point, if it's running under root via systemd it segfaults:

Jan 04 00:59:41 hostname systemd[1]: Started xr-linux-driver.
Jan 04 00:59:43 hostname kernel: xrealAirLinuxDr[79308]: segfault at c0 ip 00007f89b154d4d2 sp 00007f89aa7fb7f0 error 4 in libc.so.6[7f89b1514000+15a000] likely on CPU 5 (core 1, socket 0)
Jan 04 00:59:43 hostname kernel: Code: 41 55 41 89 cd 41 54 49 89 d4 55 48 89 f5 53 48 89 fb 48 81 ec f8 00 00 00 64 48 8b 04 25 28 00 00 00 48 89 84 24 e8 00 00 00 <8b> 87 c0 00 00 00 85 c0 0f 85 a0 00 00 00 c7 87 c0 00 00 00 ff ff
Jan 04 00:59:43 hostname systemd[1]: Started Process Core Dump (PID 79310/UID 0).
Jan 04 00:59:43 hostname systemd-coredump[79311]: [🡕] Process 79301 (xrealAirLinuxDr) of user 0 dumped core.
                                              
                                              Module libcap.so.2 without build-id.
                                              Module libatomic.so.1 without build-id.
                                              Module libudev.so.1 without build-id.
                                              Module libjson-c.so.5 without build-id.
                                              Module libhidapi-hidraw.so.0 without build-id.
                                              Module libusb-1.0.so.0 without build-id.
                                              Module libevdev.so.2 without build-id.
                                              Module xrealAirLinuxDriver without build-id.
                                              Stack trace of thread 79308:
                                              #0  0x00007f89b154d4d2 __vfprintf_internal (libc.so.6 + 0x5f4d2)
                                              #1  0x00007f89b16098a3 __fprintf_chk (libc.so.6 + 0x11b8a3)
                                              #2  0x00000000004086a7 write_state (xrealAirLinuxDriver + 0x86a7)
                                              #3  0x00000000004055bf manage_state_thread_func (xrealAirLinuxDriver + 0x55bf)
                                              #4  0x00007f89b1579084 start_thread (libc.so.6 + 0x8b084)
                                              #5  0x00007f89b15fb60c __clone3 (libc.so.6 + 0x10d60c)
                                              
                                              Stack trace of thread 79304:
                                              #0  0x00007f89b15edd8f __poll (libc.so.6 + 0xffd8f)
                                              #1  0x00007f89b16ff238 usbi_wait_for_events (libusb-1.0.so.0 + 0xe238)
                                              #2  0x00007f89b16fc7b6 handle_events (libusb-1.0.so.0 + 0xb7b6)
                                              #3  0x00007f89b16fdd00 libusb_handle_events_timeout_completed (libusb-1.0.so.0 + 0xcd00)
                                              #4  0x00007f89b16fdd5a libusb_handle_events (libusb-1.0.so.0 + 0xcd5a)
                                              #5  0x000000000041225a read_thread (xrealAirLinuxDriver + 0x1225a)
                                              #6  0x00007f89b1579084 start_thread (libc.so.6 + 0x8b084)
                                              #7  0x00007f89b15fb60c __clone3 (libc.so.6 + 0x10d60c)

                                              Stack trace of thread 79306:
                                              #0  0x00007f89b15f7856 __mmap (libc.so.6 + 0x109856)
                                              #1  0x00007f89b1579b66 pthread_create@GLIBC_2.2.5 (libc.so.6 + 0x8bb66)
                                              #2  0x0000000000409aff xreal_block_on_device (xrealAirLinuxDriver + 0x9aff)
                                              #3  0x0000000000405a81 block_on_device_thread_func (xrealAirLinuxDriver + 0x5a81)
                                              #4  0x00007f89b1579084 start_thread (libc.so.6 + 0x8b084)
                                              #5  0x00007f89b15fb60c __clone3 (libc.so.6 + 0x10d60c)

                                              Stack trace of thread 79301:
                                              #0  0x00007f89b1575c96 __futex_abstimed_wait_common (libc.so.6 + 0x87c96)
                                              #1  0x00007f89b157ab23 __pthread_clockjoin_ex (libc.so.6 + 0x8cb23)
                                              #2  0x0000000000404cfc main (xrealAirLinuxDriver + 0x4cfc)
                                              #3  0x00007f89b1515fce __libc_start_call_main (libc.so.6 + 0x27fce)
                                              #4  0x00007f89b1516089 __libc_start_main@@GLIBC_2.34 (libc.so.6 + 0x28089)
                                              #5  0x0000000000404d95 _start (xrealAirLinuxDriver + 0x4d95)

                                              Stack trace of thread 79309:
                                              #0  0x00007f89b15fb5fd __clone3 (libc.so.6 + 0x10d5fd)
                                              #1  0x00007f89a0000b80 n/a (n/a + 0x0)
                                              ELF object binary architecture: AMD x86-64
Jan 04 00:59:43 hostname systemd[1]: xr-linux-driver.service: Main process exited, code=dumped, status=11/SEGV
Jan 04 00:59:43 hostname systemd[1]: xr-linux-driver.service: Failed with result 'core-dump'.
Jan 04 00:59:43 hostname systemd[1]: [email protected]: Deactivated successfully.
Jan 04 00:59:43 hostname systemd[1]: xr-linux-driver.service: Scheduled restart job, restart counter is at 5.
Jan 04 00:59:43 hostname systemd[1]: xr-linux-driver.service: Start request repeated too quickly.
Jan 04 00:59:43 hostname systemd[1]: xr-linux-driver.service: Failed with result 'core-dump'.
Jan 04 00:59:43 hostname systemd[1]: Failed to start xr-linux-driver.

The segfault seems to be from write_state in state.c via manage_state_thread_func in driver.c.

I found if I first rm /dev/shm/xr_driver_state /tmp/shader_runtime_* ${FAKE_HOME}/.xreal* before execution it will connect without crashing:

If I start the driver before connecting hardware I get:

Starting up XR driver
ERROR: No handle
Waiting for glasses
ERROR: No handle
ERROR: No handle
ERROR: No handle
ERROR: No handle

If I start the driver after connecting hardware it will error and restart a few times with Error calling shmat but then will happily spew out gyro/accel/magne data in the logs:

Starting up XR driver
Found device with product_id: 0x424
G: -0.37 -0.28 -0.08
A: -0.98 0.03 0.16
M: -1.00 -1.00 -1.00
ERROR: Not matching signature
Found device with product_id: 0x424
MCU: 07.1.02.313_20231027
DP: 112E
DSP: 07.1.00.001_1.0.1.7_1.1
Brightness: 7
Disp-Mode: 1
Error calling shmat
Starting up XR driver
Found device with product_id: 0x424
G: -0.48 -0.29 -0.18
A: -0.98 0.04 0.16
M: -1.00 -1.00 -1.00
ERROR: Not matching signature
Found device with product_id: 0x424
MCU: 07.1.02.313_20231027
DP: 112E
DSP: 07.1.00.001_1.0.1.7_1.1
Brightness: 7
Disp-Mode: 1
G: -0.39 -0.29 -0.12
A: -0.98 0.03 0.16
M: -0.96 -0.96 -0.89
G: -0.45 -0.17 -0.24
A: -0.98 0.03 0.16
M: -0.96 -0.96 -0.89
G: -0.32 -0.22 -0.25
A: -0.98 0.03 0.16
M: -0.96 -0.96 -0.89

As far as I know, there isn't any human input and at this point it should Just Work? if you do have a moment to think about where this might be going wrong or what I might look at next that would be appreciated!

from xrlinuxdriver.

canguy247 avatar canguy247 commented on August 17, 2024

I had a poke around today to get it running on NixOS and since it is set up for an immutable OS it should run on Nix. I would rather avoid doing a "proper" install the Nix way until this project is stable (and I learn more about that since I am a Nix newb)

The first issue I had was that the setup script for the shader uses chown and assumes the normal distro setup where a user is in a group named after their username. In Nix the group is called "users".

Once I fixed this the driver setup script failed when it checked for systemd using the command: "ps -p 1 -o comm=" which should work but does not with the following output:

ps: invalid option -- 'p'
BusyBox v1.36.1 () multi-call binary.

Usage: ps [-o COL1,COL2=HEADER] [-T]

Show list of processes

	-o COL1,COL2=HEADER	Select columns for display
	-T			Show threads

I will have another look tomorrow as I have some other things to do right now.

I had a few more minutes and tried to manually copy files and run as root but got:
bash: ./xrealAirLinuxDriver: cannot execute: required file not found

Edit: this method will not work as the binary is looking for libraries (glibc) in specific spots, and Nix is not a standard directory tree. Will have to do this properly I guess, lol

from xrlinuxdriver.

canguy247 avatar canguy247 commented on August 17, 2024

First, thanks @xM8WVqaG for the above info and @wheaney for the great work here!. This has been an amazing learning process!

I compiled the driver (haven't touched the shader stuff yet) and it works!

The only issue I had is just like xM8WVqaG I have to restart the service after plugging in the glasses or it does not work and the logs continue to show:

ERROR: No handle

I will have a look at the shader next (maybe not tonight) and see how that goes

Here is a section of the log where things started up:

ERROR: No handle
ERROR: No handle
Starting up XR driver
Found device with product_id: 0x424
Found device with product_id: 0x424
MSG: 27672 = 6c18 (18)
01 00 00 00 00 00 00
MCU: 07.1.02.321_20240105
DP: 112E
DSP: 07.1.00.001_1.0.1.7_1.1
Brightness: 7
Disp-Mode: 1
Device connected, redirecting input to mouse...
G: -3.27 -5.42 -1.20
A: 0.16 -0.13 -0.96
M: -1.00 -1.00 -0.98
G: -3.12 -5.36 -1.39
A: 0.16 -0.13 -0.96
M: -1.00 -1.00 -0.98

from xrlinuxdriver.

xM8WVqaG avatar xM8WVqaG commented on August 17, 2024

Interesting, can you confirm which window manager or desktop environment you're using? Was it X11 or Wayland?

I've never actually managed to get Device connected, redirecting input to mouse... to come up. I think there is some missing error handling around this part because it just hangs. You can see from my logs the device is discovered I do get the GAM output. The only error is ERROR: Not matching signature which comes from nrealAirLinuxDriver/interface_lib/src/device3.c but otherwise, no exceptions, just nothing. I haven't had time to try and add more handling to debug this.

from xrlinuxdriver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.