Spacemouse using Spacenav

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
User avatar
M4x
Veteran
Posts: 1472
Joined: Sat Mar 11, 2017 9:23 am
Location: Germany

Spacemouse using Spacenav

Post by M4x »

Hello,

FreeCAD / system details
OS: Ubuntu 16.04.6 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.1.
Build type: Release
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedStates (en_US)


I've got a spacemouse compact from 3Dconnexion for the first time. I've checked the wiki site 3Dconnexion_input_devices and installed spacenavd and libspnav-dev via the terminal

Code: Select all

sudo apt-get install spacenavd libspnav-dev
followed the instructions in the wiki

Code: Select all

cp ~/.Xauthority /root/
/usr/bin/spnavd_ctl x11 stop
signalled spacenavd to stop sending X events.
and was greeted with the first error message, after using the second spacenavd_ctl command

Code: Select all

/usr/bin/spnavd_ctl x11 start
You must have an X server running before starting up spacenavd-X11 events.
I've looked into starting an XServer manually but that didn't help a lot. Afterwards, I've started FreeCAD anyway and ... it didn't work. I wasn't able to find anything in the spacenav log and started to read several threads on the forum, especially this one: Unable to connect to SpaceNavigator with FreeCAD 0.17 on Fedora 28. I've found out, that it's common practice to restart the spacenav deamon right before starting FreeCAD. Basically, I've found two advices / solutions:

Code: Select all

sudo /etc/init.d/spacenavd restart
or

Code: Select all

sudo /etc/init.d/spacenavd stop
sudo xhost +
sudo /etc/init.d/spacenavd start
Both didn't help regarding FreeCAD. I've used the second approach while dealing with the examples described below.

To take FreeCAD out of the equation, I've compiled the examples, which came with the libspnav-dev package (running sudo make within the example folders and after extracting the archive within the cube example).

simple_af_unix

Code: Select all

/usr/share/doc/libspnav-dev/examples/simple$ ./simple_af_unix

without restarting the spacenav daemon (not working):

Code: Select all

connect failed: No such file or directory
failed to connect to the space navigator daemon
with restarting the spacenav daemon (working):

Code: Select all

got motion event: t(0, 0, 0) r(0, 0, 0)

simple_x11

Code: Select all

/usr/share/doc/libspnav-dev/examples/simple$ ./simple_x11

with or without restarting the daemon (not working):

Code: Select all

failed to connect to the space navigator daemon

cube

Code: Select all

/usr/share/doc/libspnav-dev/examples/cube$ ./cube
with or without restarting the daemon (not working):

Code: Select all

failed to connect to the space navigator daemon

What I didn't expect: The spacemouse is working with all of the examples described above and with FreeCAD when I'm using this command in advance:

Code: Select all

sudo spacenavd -d
Hint: If the daemon is already running, I've to run this command first:

Code: Select all

sudo /etc/init.d/spacenavd stop
Output:

Code: Select all

Spacenav daemon 0.6
failed to open config file /etc/spnavrc: No such file or directory. using defaults.
adding device.
device name: 3Dconnexion SpaceNavigator
using device: /dev/input/event6

Why am I posting this?
I was really surprised that I've got it to work this way. I'm hoping to get an explanation here, because I've no idea what is really going on. Is there a way to avoid running spacenavd -d with sudo? Here is the output without using sudo:

Code: Select all

Spacenav daemon 0.6
failed to open config file /etc/spnavrc: No such file or directory. using defaults.
adding device.
failed to open device: Permission denied
removing device: 
adding device.
failed to open device: No such file or directory
removing device: 
failed to find any supported devices
failed to bind unix socket: /var/run/spnav.sock: Permission denied
In addition, I hope that this information is going to be useful to other users in the future. Furthermore, I'ld love to update the related wiki site (after I've understood what is going on here and why the commands from the wiki aren't working for me).

I'm looking forward to your help :P

Regards
Max
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Spacemouse using Spacenav

Post by vocx »

M4x wrote: Wed Oct 02, 2019 10:02 pm ...
I've got a spacemouse compact from 3Dconnexion for the first time.

...
In addition, I hope that this information is going to be useful to other users in the future.
I've edited the wiki a lot, and I've seen the Spacemouse information, but I've never used it, so I don't have a way of knowing how current or how outdated the information is.

As far as I can tell, support for such devices was included many years ago, in 2012 or so. More recently, at the beginning of this year, one user added a few more things, https://github.com/FreeCAD/FreeCAD/comm ... Dconnexion

But that's it. Honestly I don't know how popular these devices are, or who even uses them. So, you are welcome to test and improve the documentation as you see fit.

By the way, you should summarize what you did, because you mention a lot of things, but don't provide a definitely, "do these steps". You provide what worked together with what didn't, so it's confusing.

About the daemon, I think it's normal in Ubuntu to start and stop system daemons with sudo, especially when these daemons require hardware access.

For example, when my wifi card doesn't work well, I restart the network daemon like this

Code: Select all

sudo service network-manager restart
I imagine in your case it's the same

Code: Select all

sudo service spacenavd restart
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
goeland86
Posts: 43
Joined: Thu Jun 25, 2020 9:19 pm
Location: Greater Geneva area, Switzerland

Re: Spacemouse using Spacenav

Post by goeland86 »

I realize this is an old thread, but I've been fighting to get my refurbished SpaceExplorer (with the 16 buttons on it) to work right for me on Ubuntu 22.10.

I ran into the same issue as @M4x - if I depend on the service (systemd in this case) to run, I get the 6 DOF, but the button mapping defined in /etc/spnavrc (per the instructions here https://forum.freecadweb.org/viewtopic. ... 75#p547175) do not function at all. The buttons don't appear in `xev` at all.

However, if I start spacenavd with
sudo spacenavd -d -vvv -c /etc/spnavrc
Then the button mapping (including the very much wanted Ctrl key) functions flawlessly.

I'm not sure what exactly is happening, I suspect there are missing parameters in the systemd service file that could affect this, but I haven't found what they are yet. Will post back when I do find why this happens.
User avatar
goeland86
Posts: 43
Joined: Thu Jun 25, 2020 9:19 pm
Location: Greater Geneva area, Switzerland

Re: Spacemouse using Spacenav

Post by goeland86 »

Ok, finally! I figured out the issue of why it works with sudo and not with the service file.

I believe this was true all the way back to older releases as well...

When running spacenavd with sudo, it inherits the current DISPLAY and XAUTHORITY values from the user's session.
The service file, on the other hand, uses whatever is defined in /etc/profile or /root/.bashrc for those values.

The copying of ~/.Xauthority to /root/ does not fix this entirely. It helps give permissions to the Xsession... Assuming it tries to connect to the right one.

Except Ubuntu (and other distros I can remember), have the first X11 session running the login manager (gdm/kdm/whatever), on DISPLAY=:0. The correct user's DISPLAY value is then :1.

So I modified the file in /lib/systemd/system/spacenavd.service to include the following two lines in the [service] section:

Code: Select all

Environment=XAUTHORITY=/run/user/1000/gdm/Xauthority
Environment=DISPLAY=:1
And now I can get spacenavd intercepting the button presses to get the standard keyboard mappings of the buttons that I wanted (ESC, CTRL, ALT and Shift on my device).

Note that the button IDs are not all the same depending on your device. Here's for the SpaceExplorer what my button section in /etc/spnavrc looks like:

Code: Select all

kbmap6 = Escape
kbmap7 = Alt_L
kbmap8 = Shift_L
kbmap9 = Control_L
device-id = 046d:c627
I hope this helps someone else who's been looking at making their 3DConnexion device buttons work with FreeCAD on Linux.
User avatar
M4x
Veteran
Posts: 1472
Joined: Sat Mar 11, 2017 9:23 am
Location: Germany

Re: Spacemouse using Spacenav

Post by M4x »

@goeland86 sorry, I've missed your posts. Glad it's working for you. Could you add your FreeCAD version information and the version number of your spacenavd installation too?
User avatar
goeland86
Posts: 43
Joined: Thu Jun 25, 2020 9:19 pm
Location: Greater Geneva area, Switzerland

Re: Spacemouse using Spacenav

Post by goeland86 »

@M4x Oops, didn't see your post until now. I've had to move away from FreeCAD for commercial work because of issues regarding parts assembly. I was using FreeCAD 0.20.1, from the official downloads link. The spacenavd is the latest version available on Ubuntu 22.04, and my upgrade to 23.04 hasn't broken the changes I made, so it still works correctly there as well.

I don't think the specific versions of spacenavd or FreeCAD matter much, the problem was purely one of X11 sessions being mixed up as per my last post.
Post Reply