Skip to content

Instantly share code, notes, and snippets.

@guiambros
Last active Feb 20, 2022
Embed
What would you like to do?
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

guiambros commented May 26, 2021

@pabloab
Copy link

pabloab commented Jun 12, 2021

I removed my comment (not fast enough 😄) because I mistakenly read the "<= " as an arrow instead of less than or equal to.

@kentavv
Copy link

kentavv commented Jun 26, 2021

On Ubuntu 20.04, today seeing this:

checking for GUDEV... no
configure: error: Package requirements (gudev-1.0 >= 234) were not met:

Requested 'gudev-1.0 >= 234' but version of gudev-1.0 is 233

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables GUDEV_CFLAGS
and GUDEV_LIBS to avoid the need to call pkg-config.

@alfi-justin
Copy link

alfi-justin commented Jul 12, 2021

I'm seeing the same gudev version issue and version 234 is not currently available in the ubuntu 20.04 repo.

as a workaround, inserting git checkout UPOWER_0_99_11 after the clone should fix the versioning issue.

@guiambros
Copy link
Author

guiambros commented Jul 18, 2021

Thanks @alfi-justin and @kentavv! Unfortunately upower 0.99.12 moved things around a bit which broke the patch. And it now depends on libgudev-1.0 >=234, which works for ubuntu 21.04 or above, but ubuntu 20.10 still has v233 in its official repo.

I've updated the script and patch to reflect both cases, so now it should run in all versions, but lmk if you see any problems. Thanks!

@ramsperger
Copy link

ramsperger commented Jul 31, 2021

Sweet. Fixed my problems. When I first ran it, it had an error and blew past it, resulting in more errors.

Fixes:

  1. I added set -e so it would stop on errors.
  2. The error I had was because apt prompted for Y/n on the install. I changed it to sudo apt-get -q -y install. The help on apt notes that it is for direct user interaction and does not guarantee compatibility from version-to-version. Ubuntu states something similar and says apt-get should be used in scripts...

@guiambros
Copy link
Author

guiambros commented Aug 21, 2021

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

guiambros commented Sep 12, 2021

@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

guiambros commented Sep 13, 2021

@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

@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

guiambros commented Sep 13, 2021

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

@Tom743
Copy link

Tom743 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.

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