The Random Thoughts of GeniusMusing

Just another random blog

22-52-HowICaughtARat

- Posted in 52Posts by

Well... maybe more of a mouse. Actually a trackball.

A Logitech MX Ergo to be specific. Details can be found here.
Logitech MX ERGO Advanced Wireless Trackball with Tilt Plate

And what do you use to catch a rat mouse?

Well... that depends on if you are running Xorg or Wayland and if you switch between both.

For Xorg I am using xinput to configure my mouse and on Wayland I am using ratbagd.

As I sometimes bounce between both and Wayland really dosn't like xinput and it seems that ratbagd has issues staying running in Xorg (and sometime in Wayland as well) and neither seem to remember my changes so I wrote a script that runs post login that sets the mouse buttons up for me to the way I like based on Xorg or Wayland. Not that many things change, just a single button, the "extra" forward button gets remapped to be the middle button so that when I clock on a link it opens the link in a new tab. Yes I can click the scroll wheel to do this but it is easier to assign a different button that I am not using and I don't accidentally scroll the page at the same time.

Setting up the mouse on Xorg was not too bad once I figured it out.

Install xinput for your distro. Run xinput list and it will return something like this.

xinput list
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ Logitech K350 id=8 [slave pointer (2)]
⎜ ↳ Logitech MX Ergo id=9 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
↳ Power Button id=7 [slave keyboard (3)]
↳ Eee PC WMI hotkeys id=10 [slave keyboard (3)]
↳ Logitech K350 id=11 [slave keyboard (3)]
↳ Logitech MX Ergo id=12 [slave keyboard (3)]

So the mouse id is 9.

Next, install xev and then run xev | grep button a little window will show up where you can click inside and get info about the button numbering.

Here is the output: state 0x10, button 1, same_screen YES
state 0x110, button 1, same_screen YES
state 0x10, button 2, same_screen YES
state 0x210, button 2, same_screen YES
state 0x10, button 3, same_screen YES
state 0x410, button 3, same_screen YES
state 0x10, button 4, same_screen YES
state 0x810, button 4, same_screen YES
state 0x10, button 5, same_screen YES
state 0x1010, button 5, same_screen YES
state 0x10, button 6, same_screen YES
state 0x10, button 6, same_screen YES
state 0x10, button 7, same_screen YES
state 0x10, button 7, same_screen YES
state 0x10, button 8, same_screen YES
state 0x10, button 8, same_screen YES
state 0x10, button 9, same_screen YES
state 0x10, button 9, same_screen YES

Here is what they map to:

button 1 = left button
button 2 = middle button/scroll click
button 3 = right button
button 4 = scroll down
button 5 = scroll up
button 6 = scroll left
button 7 = scroll right
button 8 = back button
button 9 = forward button

So what I want is for button 9 to act like button 2 for device 9, this is the command:

xinput --set-button-map 9 1 2 3 4 5 6 7 8 2 10 11 12 13 14 15 16 17 18 19 20

The format is xinput --set-button-map [device ID] [Buttons]

So if you had a 6 button mouse that was device 5 and wanted all buttons to act like button 1, the command would look like this.

xinput --set-button-map 6 1 1 1 1 1 1

To put it back to the way it was.

xinput --set-button-map 6 1 2 3 4 5 6

All done and it is retentive until reboot which is why the script at login.

As for the Wayland setup, I installed Piper, instructions are here: Installation · libratbag/piper Wiki it is in many distro repo's.

While it is not fully documented at this time you do need to add your user to the Games group for this to work. You will also want to do a reboot, not sure why but it didn't work before the reboot.

If you run ratbagctl it will return something like this:

hollering-marmot: Logitech MX Ergo

The Logitech MX Ergo is what is going to be used.

The next command to run is ratbagctl 'Logitech MX Ergo' info It returns this

hollering-marmot - Logitech MX Ergo
Model: usb:046d:406f:0
Number of Buttons: 9
Number of Leds: 1
Number of Profiles: 1
Profile 0: (active)
Name: n/a
Button: 0 is mapped to 'button 1'
Button: 1 is mapped to 'button 2'
Button: 2 is mapped to 'button 3'
Button: 3 is mapped to 'button 4'
Button: 4 is mapped to 'button 5'
Button: 5 is mapped to UNKNOWN
Button: 6 is mapped to 'wheel-left'
Button: 7 is mapped to 'wheel-right'
Button: 8 is mapped to UNKNOWN
LED: 0, depth: monochrome, mode: on, color: 000000

At this point you can run Piper and it will bring up a GUI program that will show your mouse and the buttons.

Screenshot 0

Click on the button you want to change.

Screenshot 1

Set the function you want.

Screenshot 2

Hit Apply, Apply and it has been changed.

If you run the ratbagctl 'Logitech MX Ergo' info command again it will show this.

hollering-marmot - Logitech MX Ergo
Model: usb:046d:406f:0
Number of Buttons: 9
Number of Leds: 1
Number of Profiles: 1
Profile 0: (active)
Name: n/a
Button: 0 is mapped to 'button 1'
Button: 1 is mapped to 'button 2'
Button: 2 is mapped to 'button 3'
Button: 3 is mapped to 'button 4'
Button: 4 is mapped to 'button 3'
Button: 5 is mapped to UNKNOWN
Button: 6 is mapped to 'wheel-left'
Button: 7 is mapped to 'wheel-right'
Button: 8 is mapped to UNKNOWN
LED: 0, depth: monochrome, mode: on, color: 000000

The only change is that now Button 4 is mapped to button 3, the forward button is now acting as the middle button. The script command is:

ratbagctl 'Logitech MX Ergo' button 4 action set button 3

So now we have the mouse button commands and how to find what session we are in, lets look at the script.

#!/bin/bash
# the sleep seems to be needed as without the controls are not always set 
echo "Sleeping 5 seconds"
sleep 5
# detect the session
Xdesk="$(loginctl show-session $(awk '/seat/ {print $1}' <(loginctl)) -p Type | awk -F= '{print $2}')"
Wdesk="$(loginctl show-session $(awk '/tty/ {print $1}' <(loginctl)) -p Type | awk -F= '{print $2}')"
# apply mouse settings for correct session
if [ $Xdesk == "x11" ];then
echo "x11"
xinput --set-button-map 9 1 2 3 4 5 6 7 8 2 10 11 12 13 14 15 16 17 18 19 20 
elif [ $Wdesk == 'wayland' ];then
echo "Wayland"
ratbagctl 'Logitech MX Ergo' button 4 action set button 3
fi
echo "Done!"

Here is where I got the desktop session info.

linux How to know whether Wayland or X11 is being used - Unix & Linux Stack Exchange

I made a few changes mainly due to the Xorg session not having a /tty/ option but it did have a /seat/ that I could use.

The one issue I am still having is that under Wayland when my mouse "goes to sleep to save battery" the button changes are not held like they are in the Xorg session. I will update when I have figured out the answer or if it is