Battery levels of Logitech devices in GNOME
A GNOME Shell extension that shows the battery of Logitech mice and keyboards in the top bar without Solaar, and talks HID++ to the Bolt receiver the kernel does not drive.
Table of Contents
I use a Logitech mouse and keyboard, and I wanted to see their battery in the GNOME top bar. Solaar can show it, but I like to avoid dependencies and wanted the display as close to GNOME’s own as possible. None of the existing indicators did what I needed. That became the Logi Battery Indicator, an extension for GNOME Shell 50 and 51.
What it shows
Every Logitech device with a battery gets an entry in the top bar, with its level as a percentage or as a colored icon and a mark while it charges. The devices may sit on a Unifying or Bolt receiver or be paired over Bluetooth. Keyboards with a backlight on a Bolt receiver also get a tile in the quick settings, with a slider like the one GNOME shows for laptop keyboards. The display changes when a device reports a new level or connects. The extension never polls on a timer.
Batteries from UPower
GNOME already knows most of these batteries. The kernel’s hid-logitech-hidpp
driver reads them from devices on most Logitech receivers and from many over
Bluetooth, and BlueZ reads them from other Bluetooth LE devices. Both hand them
to UPower, which GNOME’s own battery indicator uses as well. The extension
listens to UPower over D-Bus and keeps the devices whose path starts with the
hidpp_battery_ name the Logitech driver gives them, or whose BlueZ device
carries Logitech’s vendor id 046D. UPower signals new devices, removed devices
and changed values, so the extension needs no timer.
The Bolt receiver
The Bolt receiver is the exception. The kernel does not drive it, so no battery
of a device behind it reaches UPower. The extension then speaks HID++,
Logitech’s protocol, to the receiver itself, through its hidraw node, which
belongs to root. A udev rule gives the logged-in user access to the receiver’s
HID++ interface alone, while the interfaces that carry key presses and mouse
movements stay root-only.
SUBSYSTEM=="hidraw", ACTION!="remove", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c548", ENV{LOGI_BATTERY_BOLT}="1"
ENV{LOGI_BATTERY_BOLT}=="1", ATTRS{bInterfaceNumber}=="02", TAG+="uaccess"
To learn which devices are paired, the extension asks the receiver the way the
kernel’s hid-logitech-dj driver does. It sets a register that makes the
receiver announce every paired device as if it had just connected. The Bolt
receiver starts with its wireless notifications switched off and stays silent
until the extension switches them on. Battery levels and the keyboard backlight
then come from HID++ features of each device.
The keyboard backlight
The brightness tile is a port of GNOME Shell’s own keyboard brightness indicator for laptops, with HID++ in place of GNOME’s power daemon. The slider snaps to the levels the keyboard offers, and the keyboard reports changes made with its own keys, so the tile and the keys stay in step. It works for keyboards on a Bolt receiver, because only there does the extension speak HID++ to the device.
A steep learning curve
Every version on extensions.gnome.org is
reviewed by hand. The
review guidelines
ask that an extension leaves nothing behind when it is disabled, and the reviews
taught me what that means in practice. Pending reads have to stop through a
Gio.Cancellable, signal connections have to go with the objects they belong
to, and nothing may block GNOME Shell’s main loop. Writes to the receiver
therefore run in a GIO thread, because hidraw waits for the USB transfer. The
code now follows the GJS guide down to its ESLint configuration.
From 1.0 to 1.3
Version 1.0 spoke HID++ to every device through hidraw. That only worked
because Fedora’s Solaar package ships a udev rule that opens these nodes, so the
promise of working without Solaar did not hold. Version 1.1 cleaned up how the
extension shuts down. Version 1.2 moved to UPower, kept HID++ only for the Bolt
receiver with a rule of its own and added GNOME 51. Version 1.2.1 added
Bluetooth LE devices through BlueZ. Version 1.3 brought the keyboard backlight, the
preferences window and stylesheets for light and dark mode, and finds devices on
the Bolt receiver by their pairing slot. The
releases list every
change.
Limits
A Bolt receiver needs the udev rule, which has to be installed once with root rights. The backlight works only on a Bolt receiver, not over Bluetooth. Devices appear only if the kernel, BlueZ or the Bolt path reports their battery.
Conclusion
The extension shows Logitech batteries in the top bar with what GNOME already has, and only speaks HID++ where the kernel leaves a gap. The source is on GitHub.