Skip to content

Instantly share code, notes, and snippets.

@guiambros
Last active September 23, 2023 18:48
Show Gist options
  • Save guiambros/166039459a8579638b57f7d135505ab1 to your computer and use it in GitHub Desktop.
Save guiambros/166039459a8579638b57f7d135505ab1 to your computer and use it in GitHub Desktop.
silent-mouse.sh
#!/bin/bash
set -e
# See https://wrgms.com/disable-mouse-battery-low-spam-notification/
# for instructions on how to use it
#
# TL;DR: run with "--keyboard" if you want to patch upower to ignore both
# mice and keyboard notifications (by default it ignores only mice)
# Check distro and upower version in use, and install required libraries
#
echo
echo "---------------------------------------------------------------------------"
upower --version
echo "---------------------------------------------------------------------------"
echo
UPOWER_ORIG_VER=`upower --version`
OS=`awk -F= '/^ID=/{print $2}' /etc/os-release`
OS_VER=`awk -F= '/^VERSION_ID=/{print $2}' /etc/os-release | cut -d "\"" -f 2`
OS_VER_MAJOR=`echo ${OS_VER} | awk -F. '{print $1}'`
PATCH_LEGACY_URL="https://gist.githubusercontent.com/guiambros/f2bf07f1cc085f8f0b0a9e04c0a767b4/raw/73efac967c8fc9539802e7aa8eeba5492f8ae3b1/up-device-legacy.patch"
PATCH_CURRENT_URL="https://gist.githubusercontent.com/guiambros/f2bf07f1cc085f8f0b0a9e04c0a767b4/raw/73efac967c8fc9539802e7aa8eeba5492f8ae3b1/up-device-current-0.99.12p.patch"
PATCH_NAME="up-device.patch"
PATCH_URL=${PATCH_CURRENT_URL}
if [ "$OS" == "manjaro" ]
then
echo "-- Manjaro detected; installing required libraries"
sudo pacman -S base-devel gtk-doc gobject-introspection git
PATH_UPOWERD="/usr/lib"
PATH_UPOWER="/usr/bin"
elif [ "$OS" == "ubuntu" ]
then
echo "-- Ubuntu detected; installing required libraries"
sudo apt install -y git gtk-doc-tools gobject-introspection libgudev-1.0-dev libusb-1.0-0-dev autoconf libtool autopoint
PATH_UPOWER="/usr/bin"
if [ "${OS_VER}" == "20.10" ]
then
echo "--- Ubuntu version 20.10 (Groovy Gorilla) detected"
PATH_UPOWERD="/usr/libexec"
UPOWER_BRANCH="UPOWER_0_99_11"
PATCH_URL=${PATCH_LEGACY_URL}
elif [ ${OS_VER_MAJOR} -ge 21 ]
then
echo "--- Ubuntu version 21 or above detected"
PATH_UPOWERD="/usr/libexec"
UPOWER_BRANCH="UPOWER_0_99_11"
PATCH_URL=${PATCH_LEGACY_URL}
elif [ ${OS_VER_MAJOR} -le 20 ]
then
echo "--- Ubuntu version 20.04 or lower detected"
PATH_UPOWERD="/usr/lib/upower"
UPOWER_BRANCH="UPOWER_0_99_11"
PATCH_URL=${PATCH_LEGACY_URL}
fi
else
echo "-- Unknown system; this script was only tested on ubuntu and manjaro."
exit 1
fi
echo "---------------------------------------------------------------------------"
echo
# Download upowerd source and selects the proper branch
#
cd ~
git clone https://gitlab.freedesktop.org/upower/upower
if [ -z ${UPOWER_BRANCH} ]
then
echo "-- Using latest master branch (0.99.12 or above)"
cd upower/src
else
echo "-- Using branch ${UPOWER_BRANCH} (latest compatible with your distro)"
cd upower
git fetch --all --tags
git checkout tags/${UPOWER_BRANCH} -b ${UPOWER_BRANCH}
cd src
fi
# Download and patch upowerd
#
wget ${PATCH_URL} -O ${PATCH_NAME}
if [ "$1" == "-keyboard" ] || [ "$1" == "--keyboard" ]; then
SILENCE_KEYBOARD="+ if ((type == UP_DEVICE_KIND_MOUSE || type == UP_DEVICE_KIND_KEYBOARD) && state == UP_DEVICE_STATE_DISCHARGING) {"
sed -i "/UP_DEVICE_KIND_MOUSE/c${SILENCE_KEYBOARD}" ${PATCH_NAME}
fi
patch -F 1 < ${PATCH_NAME}
# Compile upowerd
#
cd ..
./autogen.sh
./configure
make
# Install upowerd
#
CUR_DATETIME=`date +%Y-%m-%d-%H%M%S`
pushd .
cd src/.libs
strip upowerd
sudo chown root.root upowerd
sudo mv upowerd ${PATH_UPOWERD}/upowerd-silent
cd ${PATH_UPOWERD}
sudo mv upowerd upowerd-original-${CUR_DATETIME}
sudo ln -s upowerd-silent upowerd
popd
# Install upower
#
pushd .
cd tools/.libs
strip upower
sudo chown root.root upower
sudo mv upower ${PATH_UPOWER}/upower-silent
cd ${PATH_UPOWER}
sudo mv upower upower-original-${CUR_DATETIME}
sudo ln -s upower-silent upower
popd
# Restart upowerd
#
sudo systemctl restart upower
# Compare versions before/after (they will likely be different, but it depends on what your distro packages by default)
#
echo
echo "---------------------------------------------------------------------------"
echo "upower version BEFORE the update:"
echo "${UPOWER_ORIG_VER}"
echo "-------------------------------------"
echo "upower version AFTER the update:"
upower --version
@guiambros
Copy link
Author

Thanks @ramsperger, good catch! It was indeed missing the -y on apt install for ubuntu. Also added the set -e just in case, so it's easier for folks to troubleshoot problems in the future. Thanks!

@pabloab
Copy link

pabloab commented Aug 24, 2021

Moreover, you can use the unofficial bash strict mode: set -euo pipefail.

@iceback
Copy link

iceback commented Sep 10, 2021

Is my 2FA at github getting in the way?
Connecting to gist.githubusercontent.com (gist.githubusercontent.com)|185.199.110.133|:443... connected.
Unable to establish SSL connection.

@guiambros
Copy link
Author

@iceback: I don't think this is related to git. The only download from gist.githubusercontent.com is in line 90, so I'd guess you either have an older version of wget, or maybe you're behind a proxy but the HTTPS_PROXY is not properly set up.

You can confirm by running
wget https://gist.githubusercontent.com/guiambros/f2bf07f1cc085f8f0b0a9e04c0a767b4/raw/73efac967c8fc9539802e7aa8eeba5492f8ae3b1/up-device-current-0.99.12p.patch

and checking if you get the same error.

More details here. If confirmed, you should try a more recent version of wget (1.2x+), or adding --no-check-certificate to wget, or replacing by curl altogether.

LMK if this fixes your problem.

@iceback
Copy link

iceback commented Sep 12, 2021

add set -x to silent-mouse.sh
`+ cd src

@iceback
Copy link

iceback commented Sep 12, 2021

I don't think I've said that I'm on Ubuntu-20.04

@guiambros
Copy link
Author

@iceback: what's your wget version? Also, you can add --debug to get a dump of the handshake.

Worst case scenario you can add --no-check-certificate --auth-no-challenge to disable certificate checking.

@iceback
Copy link

iceback commented Sep 13, 2021 via email

@iceback
Copy link

iceback commented Sep 13, 2021

`+ wget --debug --no-check-certificate --auth-no-challenge https://gist.githubusercontent.com/guiambros/f2bf07f1cc085f8f0b0a9e04c0a767b4/raw/73efac967c8fc9539802e7aa8eeba5492f8ae3b1/up-device-legacy.patch -O up-device.patch
Setting --check-certificate (checkcertificate) to 0
Setting --check-certificate (checkcertificate) to 0
Setting --auth-no-challenge (authnochallenge) to 1
Setting --auth-no-challenge (authnochallenge) to 1
Setting --output-document (outputdocument) to up-device.patch
Setting --output-document (outputdocument) to up-device.patch
DEBUG output created by Wget 1.20.3 on linux-gnu.

Reading HSTS entries from /home/rob/.wget-hsts
URI encoding = ‘UTF-8’
--2021-09-12 21:11:04-- https://gist.githubusercontent.com/guiambros/f2bf07f1cc085f8f0b0a9e04c0a767b4/raw/73efac967c8fc9539802e7aa8eeba5492f8ae3b1/up-device-legacy.patch
Resolving gist.githubusercontent.com (gist.githubusercontent.com)... 185.199.110.133, 185.199.111.133, 185.199.108.133, ...
Caching gist.githubusercontent.com => 185.199.110.133 185.199.111.133 185.199.108.133 185.199.109.133
Connecting to gist.githubusercontent.com (gist.githubusercontent.com)|185.199.110.133|:443... connected.
Created socket 4.
Releasing 0x0000555724eccde0 (new refcount 1).
Initiating SSL handshake.
SSL handshake failed.
OpenSSL: error:1408F10B:SSL routines:ssl3_get_record:wrong version number
Closed fd 4
Unable to establish SSL connection.
`
I'm not in a hurry to upgrade to openssl 3. Not yet on my radar vis. Software Updater.

@guiambros
Copy link
Author

I'm out of ideas, but FWIW, your openssl is exactly the same as mine.

$ openssl version
OpenSSL 1.1.1f  31 Mar 2020

$ openssl s_client  -connect gist.githubusercontent.com:443
CONNECTED(00000003)
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert High Assurance EV Root CA
verify return:1
depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert SHA2 High Assurance Server CA
verify return:1
depth=0 C = US, ST = California, L = San Francisco, O = "GitHub, Inc.", CN = www.github.com
verify return:1
---
Certificate chain
 0 s:C = US, ST = California, L = San Francisco, O = "GitHub, Inc.", CN = www.github.com
   i:C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert SHA2 High Assurance Server CA
 1 s:C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert SHA2 High Assurance Server CA
   i:C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert High Assurance EV Root CA
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIHMDCCBhigAwIBAgIQAkk+B/qeN1otu8YdlEMPzzANBgkqhkiG9w0BAQsFADBw
....
9EO02QA3CcU7bE1iLWMHmKcU6ythmgsvNRU5TikxvF77JFv7n1/y8GLrprmKpB6Q
Df4PA8S9ROX9Rzgwe3KTIM6qeKU=
-----END CERTIFICATE-----
subject=C = US, ST = California, L = San Francisco, O = "GitHub, Inc.", CN = www.github.com

issuer=C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert SHA2 High Assurance Server CA

---
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: RSA-PSS
Server Temp Key: X25519, 253 bits
---
SSL handshake has read 3614 bytes and written 398 bytes
Verification: OK
---
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Server public key is 2048 bit
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
---
---
Post-Handshake New Session Ticket arrived:
SSL-Session:
    Protocol  : TLSv1.3
    Cipher    : TLS_AES_256_GCM_SHA384
    Session-ID: 3737EC8F8673F123BB4558B4E7CB8B81C1795618477AFAB6BFF174AEDF70B825
    Session-ID-ctx:
    Resumption PSK: 8204AE08BD8228A88A51C222F25D3225D60D1D09D16418174074B26B95C5182AA0D95251715B4A351140BB7CA5427DC9
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    TLS session ticket lifetime hint: 604800 (seconds)
    TLS session ticket:
    0000 - 7b 3a 42 70 0e 65 b1 40-7b d2 3f 51 21 80 71 0e   {:Bp.e.@{.?Q!.q.
    0010 - 31 e1 e7 ba b3 e7 8d b8-df 1e 75 fd 83 19 89 93   1.........u.....
    0020 - 7f 4c 77 70 94 a7 bc f7-e1 6a f3 3a 2d 10 a9 c5   .Lwp.....j.:-...
    0030 - 11 d3 b6 f0 a4 b5 86 2f-12 11 0a 4b cb 12 0d 93   ......./...K....
    0040 - 1f e7 32 4b 70 d3 b4 4e-a9 78 55 6e 96 db 5e 2b   ..2Kp..N.xUn..^+
    0050 - d5 6c 79 6e 1d 92 fc aa-91 bd fc b9 a7 74 8a ad   .lyn.........t..
    0060 - e7 a3 cd 46 06 75 5b 6c-a3 f5 cd a1 f7 b6 4c fb   ...F.u[l......L.
    0070 - c6 75 72 d1 27 ed ab 78-f0 8d 56 bc c4 95 01 6c   .ur.'..x..V....l
    0080 - c0 4c 2c 17 09 08 17 fb-a7 51 e1 8a 98 80 dc d4   .L,......Q......
    0090 - 1e b7 e9 52 93 0c bf 74-36 50 ef 6d e5 75 62 5b   ...R...t6P.m.ub[
    00a0 - cc 78 e2 85 d6 c3 31 17-72 ee 39 ab b6 b5 dd 22   .x....1.r.9...."

    Start Time: 1631509293
    Timeout   : 7200 (sec)
    Verify return code: 0 (ok)
    Extended master secret: no
    Max Early Data: 0
---
read R BLOCK

@iceback
Copy link

iceback commented Sep 13, 2021

yeah, weird. I get virtually identical report from openssl s_client -connect gist.githubusercontent.com:443 (session it/ticket etc differ of course).
If you think of anything... I'm all ears. That "mouse battery low" banned gets more annoying by the minute ;)
Thanks for your time.

@iceback
Copy link

iceback commented Sep 13, 2021

Huh. I was running silent-mouse.sh in an emacs shell buffer.
I just re-ran it from an xterm window and it completed, but with non-zero exit

Making all in rules
make[2]: Entering directory '/home/rob/upower/rules'
GEN .gitignore
make[2]: Leaving directory '/home/rob/upower/rules'
make[2]: Entering directory '/home/rob/upower'
make[2]: Leaving directory '/home/rob/upower'
make[1]: Leaving directory '/home/rob/upower'
++ date +%Y-%m-%d-%H%M%S

  • CUR_DATETIME=2021-09-13-124821
  • pushd .
    ~/upower ~/upower
  • cd src/.libs
  • strip upowerdecho installin upower
    strip: 'upowerdecho': No such file
    strip: 'installin': No such file
    strip: 'upower': No such file
    gitanmax:silentMouse$ echo $?
    1

The compilation went well, aside from some deprecation warnings wrt g_get__current_time, _GTimeVal

@iceback
Copy link

iceback commented Sep 13, 2021

But I haven't undone my edits (set -x, --debug etc) so I'll clean up and run again

@iceback
Copy link

iceback commented Sep 13, 2021

Clean download, all good. A little surprised by the final report.

upower version BEFORE the update:
UPower client version 0.99.11
UPower daemon version 0.99.11

upower version AFTER the update:
UPower client version 0.99.11
UPower daemon version 0.99.11

But I don't see the Low Power banner! Thanks so much.

@guiambros
Copy link
Author

guiambros commented Sep 13, 2021

Awesome, glad it worked well. And yes, 0.99.11 is the current version on ubuntu, so having the same version before/after is the expected result.

@stuaxo
Copy link

stuaxo commented Feb 8, 2022

@guiambros Thank you so much for silencing the insessant spam from my mouse. Coupled with a temporary bug that prevents me from redirecting audio app by app in pipewire this was torture !

I opened a bug against gnome-settings-daemon. In my case at least, upower is flagging this as though it should be ignored. So I opened an issue to say gnome settings daemon shouldn't notify in that case.

https://gitlab.gnome.org/GNOME/gnome-settings-daemon/-/issues/664

@b-Tomas
Copy link

b-Tomas commented Feb 20, 2022

I tested this script in Pop!_OS 21.10 and it works well, for those who were wondering. I changed ubuntu in line 35 to pop to make it work.

@DanielTakeshi
Copy link

@guiambros

Thanks for the script! I ran with:

wget -O - https://gist.githubusercontent.com/guiambros/166039459a8579638b57f7d135505ab1/raw/733b8dd3ac3280bb76f48ba3dc04655df6d69025/silent-mouse.sh | bash

It prints a bunch of commands, detects correctly that I'm on Ubuntu 18, but then it seems to run into a upower error. I've pasted the last few lines of the command line.

make[3]: Leaving directory '/home/seita/upower/po'
test ! -f ./upower.pot || \
  test -z "fr.gmo it.gmo sv.gmo pl.gmo" || make fr.gmo it.gmo sv.gmo pl.gmo
make[3]: Entering directory '/home/seita/upower/po'
rm -f fr.gmo && /usr/bin/msgfmt -c --statistics --verbose -o fr.gmo fr.po
fr.po:7: warning: header field 'Language' missing in header
fr.po: 26 translated messages.
rm -f it.gmo && /usr/bin/msgfmt -c --statistics --verbose -o it.gmo it.po
it.po:8: warning: header field 'Language' missing in header
it.po: 19 translated messages.
rm -f sv.gmo && /usr/bin/msgfmt -c --statistics --verbose -o sv.gmo sv.po
sv.po:7: warning: header field 'Language' missing in header
sv.po: 19 translated messages.
rm -f pl.gmo && /usr/bin/msgfmt -c --statistics --verbose -o pl.gmo pl.po
pl.po:5: warning: header field 'Language' missing in header
pl.po: 26 translated messages.
make[3]: Leaving directory '/home/seita/upower/po'
touch stamp-po
make[2]: Leaving directory '/home/seita/upower/po'
Making all in rules
make[2]: Entering directory '/home/seita/upower/rules'
  GEN      .gitignore
make[2]: Leaving directory '/home/seita/upower/rules'
make[2]: Entering directory '/home/seita/upower'
make[2]: Leaving directory '/home/seita/upower'
make[1]: Leaving directory '/home/seita/upower'
~/upower ~/upower
~/upower
~/upower ~/upower
~/upower

---------------------------------------------------------------------------
upower version BEFORE the update:
UPower client version 0.99.7
UPower daemon version 0.99.7
-------------------------------------
upower version AFTER the update:
upower: symbol lookup error: upower: undefined symbol: up_client_get_devices2
seita@starship:~ $ 

Is this error critical?

@skorasaurus
Copy link

Thanks for your work on this.

I have ubuntu 20.04 and ran the script but got stuck in some dependencies, specifically:

The following packages have unmet dependencies:
 gobject-introspection : Depends: libgirepository-1.0-1 (= 1.64.0-2) but 1.64.1-1~ubuntu20.04.1 is to be installed

I already had gobject-introspection installed, version 1.64.1-1~ubuntu20.04.1. (despite the above message) so

I downgraded the libgirepository-1.0-1 package from 1.64.1-1~ubuntu20.04.1 to v 1.64.0-2; ran the script again and it worked.

@guiambros
Copy link
Author

@skorasaurus -- thanks for sharing your experience!

This thread is quite old at this point. I'm also using 20.04, and haven't had to mess with upowerd for a while. Curious if you were still experiencing it.

It seems the frequent notifications were fixed upstream in gnome-settings-daemon. I haven't tracked the exact changes in gsd, but at least I haven't seen it nearly as frequently as before.

The folks from upower argue the issue should be fixed upstream (in this case gnome-settings-daemon).

@Ethran
Copy link

Ethran commented Sep 23, 2023

@guiambros I still experience this issue, with marathon mouse/performance plus m705 on Ubuntu 23.10 with gnome 45.0, I also encountered this issue on Ubuntu 23.04 with gnome 44.

At this point, I was seeing this (or similar, I think at the beginning it was yellow, not orange) message for three months:
image

It appears on the login screen and after logging in despite "Do Not Disturb" turned on. It stays on the screen till I click on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment