Skip to content

Commit

Permalink
Initial project commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MitMaro committed May 22, 2017
1 parent c3a8fec commit f142566
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2015, Tim Oram and contributors

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Mint Battery Notification

The default battery low indicator in Linux Mint isn't great at notifying the user of a low batter. It shows no indication while using a fullscreen application and doesn't provide an audible sound when the batter is in a critical state.

This script will show two types of notifications. The first is a visual notification when your battery has reached a low level. The second is a visual and auditory notification when the batter has reached the critical level.

## Example

![Battery Low](./battery-low.png "Battery Low")
![Battery Critical](./battery-critical.png "Battery Critical")

## Dependencies

This project is written in `Bash`, and tested with version *4.3* but other versions should work. `paplay` is used to play the notification sound, but this can be modified. `notify-send` is used to create the notification.

## Install

Download the most recent version from the releases page or checkout directly using git. Remember the location of your download, you will need it shortly.

Cron is used to check the battery level and I usually check the level every minute but this can be changed in the cron entry. To add an entry to cron using `crontab -e` and add the line below.

```
*/1 * * * * /path/to/battery-notify.sh`
````
## Configuration
Almost all parts of the script can be set from the cron entry. For example to set the battery critical level you would modify the cron entry to match:
```
*/1 * * * * CRITICAL_LEVEL=8 /path/to/battery-notify.sh`
````
### Available Configurations
| Variable | Description | Default |
|----------|-------------|---------|
| `SOUND_COMMAND` | The command that will play the notification sound | `paplay`
| `CRITICAL_LEVEL` | The battery level to show the critical alert | `5`
| `CRITICAL_ICON` | The icon to show for the critical notification | `battery-empty`
| `CRITICAL_SOUND` | The sounds to use for the critical notification | `"/usr/share/sounds/purple/alert.wav"`
| `LOW_LEVEL` | The battery level to show the low alert | `10`
| `LOW_ICON` | The icon to show for the low notification | `battery-caution`
| `LOW_SOUND` | The sounds to use for the critical notification | `""`
`CRITICAL_ICON` and `LOW_ICON` are any [icon that is supported][1] by `notify-send`. For `CRITICAL_SOUND` and `LOW_SOUND` if an empty string is provided there will be no audible notification.
## License
This project is released under the ISC license. See [LICENSE](./LICENSE).
[1]:[https://askubuntu.com/a/189262/18240]
Binary file added battery-critical.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added battery-low.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions battery-notify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

export DISPLAY="$(w -h $USER | awk '$3 ~ /:[0-9.]*/{print $3}')"
XAUTHORITY="$HOME/.Xauthority"

SOUND_COMMAND="${SOUND_COMMAND:-paplay}"
CRITICAL_LEVEL="${CRITICAL_LEVEL:-5}"
CRITICAL_ICON="${CRITICAL_ICON:-"battery-empty"}"
CRITICAL_SOUND="${CRITICAL_SOUND:-"/usr/share/sounds/purple/alert.wav"}"
LOW_LEVEL="${LOW_LEVEL:-10}"
LOW_ICON="${LOW_ICON:-"battery-caution"}"
LOW_SOUND="${LOW_SOUND:-""}"

if [[ -r "$HOME/.dbus/Xdbus" ]]; then
source "$HOME/.dbus/Xdbus"
fi

battery_level="$(acpi -b | grep -v "Charging" | grep -P -o '([0-9]+(?=%))')"

if [[ -z "$battery_level" ]]; then
exit 0
fi

if [[ "$battery_level" -le "$CRITICAL_LEVEL" ]]; then
notify-send -i "$CRITICAL_ICON" -t 15000 -u normal "Battery Critical" "Battery level is ${battery_level}%"
if [[ ! -z "$CRITICAL_SOUND" ]]; then
$SOUND_COMMAND "$CRITICAL_SOUND"
fi
exit 0
fi

if [[ "$battery_level" -le "$LOW_LEVEL" ]]; then
notify-send -i "$LOW_LEVEL" -t 15000 -u normal "Battery Low" "Battery level is ${battery_level}%"
if [[ ! -z "$LOW_SOUND" ]]; then
$SOUND_COMMAND "$LOW_SOUND"
fi
exit 0
fi

0 comments on commit f142566

Please sign in to comment.