Skip to content

mateohysa/threshd

Repository files navigation

threshd

A lightweight battery charge threshold manager for Linux laptops.

threshd icon

What is this?

Threshd allows you to cap your laptop's battery charging at a specific percentage (e.g., 80%) to extend battery lifespan. It provides a system tray icon for easy access and runs as a secure system daemon.

Features

  • Simple UI - System tray icon with quick preset buttons
  • Real-time updates - Shows current battery level and charging status
  • Secure - PolicyKit integration for safe privilege elevation
  • Lightweight - Written in C, ~100KB total binaries
  • Fast - Builds in seconds

Installation

Quick Install

git clone https://github.com/mateohysa/threshd.git
cd threshd
./install-c.sh

That's it! The tray icon will appear automatically and start on future logins.

Not sure if your laptop is supported? Run ./check-hardware.sh first to verify.

What it does

The installation script will:

  1. Check for required system dependencies
  2. Build the daemon and tray application
  3. Install system-wide with proper permissions
  4. Enable and start the background daemon
  5. Launch the tray icon

Manual Installation

If you prefer manual control:

# Install dependencies (Ubuntu/Debian)
sudo apt install build-essential pkg-config libglib2.0-dev \
    libpolkit-gobject-1-dev libgtk-3-dev \
    libayatana-appindicator3-dev zenity

# Build
make

# Install (requires sudo)
sudo make install

# Enable daemon
sudo systemctl enable --now threshdd.service

# Start tray
threshd-tray &

Usage

Right-click the battery icon in your system tray to:

  • Set threshold to 60%, 70%, 80%, or Uncapped (100%)
  • Choose Custom... for any value between 60-100%
  • View current battery percentage and threshold

The active threshold shows a checkmark.

Hardware Requirements

Your laptop must support battery charge threshold control via sysfs:

ls /sys/class/power_supply/BAT*/charge_control_end_threshold

If this file exists, you're good to go! This feature is available on:

  • Most ThinkPad models
  • Many Dell, HP, and ASUS laptops
  • Framework laptops
  • And many others

Check your manufacturer's documentation or BIOS settings if unsure.

Uninstallation

cd threshd
sudo make uninstall

How it Works

┌─────────────────────────────────────────────────┐
│  threshd-tray                                   │
│  (GTK3 system tray - runs as user)              │
└────────────────┬────────────────────────────────┘
                 │
                 │ D-Bus
                 ↓
┌─────────────────────────────────────────────────┐
│  threshdd                                       │
│  (System daemon - runs as root)                 │
└────────────────┬────────────────────────────────┘
                 │
                 │ PolicyKit authorization
                 ↓
┌─────────────────────────────────────────────────┐
│  /sys/class/power_supply/BAT0/                  │
│    charge_control_end_threshold                 │
└─────────────────────────────────────────────────┘

Building from Source

# Clone the repository
git clone https://github.com/YOUR_USERNAME/threshd.git
cd threshd

# Build
make

# The binaries will be created:
# - threshdd (daemon)
# - threshd-tray (tray application)

Troubleshooting

Tray icon not showing

# Check if running
pgrep threshd-tray

# Restart
pkill threshd-tray && threshd-tray &

Can't change threshold

# Check daemon status
sudo systemctl status threshdd.service

# View logs
sudo journalctl -u threshdd.service -f

Hardware not supported

If you get errors, your laptop may not support this feature. Check:

ls -l /sys/class/power_supply/BAT*/charge_control_*

If these files don't exist, your hardware doesn't support threshold control.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development

# Build with debug symbols
make clean
make CFLAGS="-Wall -Wextra -g -DDEBUG"

# Run daemon manually for testing
sudo ./threshdd

# Run tray manually
./threshd-tray

Project Structure

threshd/
├── src/
│   ├── threshdd.c          # System daemon
│   └── threshd-tray.c      # Tray application
├── resources/              # Icons, D-Bus configs, PolicyKit policies
├── Makefile                # Build system
├── install-c.sh            # Installation script
└── README.md

License

MIT License - see LICENSE file for details.

Acknowledgments

  • Linux kernel power supply subsystem maintainers
  • The GTK and libayatana-appindicator projects
  • Everyone who has contributed to making laptop battery management better

Note: Setting a charge threshold to 80% or lower can significantly extend your battery's lifespan by reducing stress on the cells. This is especially useful for laptops that stay plugged in most of the time.

About

Lightweight battery charge threshold manager for Linux laptops

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published