Skip to content

Commit

Permalink
Include system-sleep file
Browse files Browse the repository at this point in the history
  • Loading branch information
pepa65 committed Nov 30, 2023
1 parent 01a4e52 commit 8726153
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![GoDoc](https://godoc.org/github.com/pepa65/bat?status.svg)](https://godoc.org/github.com/pepa65/bat)
![Continuous Integration](https://github.com/pepa65/bat/actions/workflows/ci.yaml/badge.svg)

# bat v0.14.0
# bat v0.15.0
**Manage battery charge limit**

* Repo: github.com/pepa65/bat
Expand All @@ -11,14 +11,14 @@
* Required: Linux-5.4-rc1+ systemd-244+

```
bat v0.14.0 - Manage battery charge limit
bat v0.15.0 - Manage battery charge limit
Repo: github.com/pepa65/bat
Usage: bat <option>
Options (every option except 's[tatus]' needs root privileges):
[s[tatus]] Display charge level, limit, health & persist status.
[l[imit]] <int> Set the charge limit to <int> percent.
p[ersist] Install and enable the persist systemd unit files.
r[emove] Remove the persist systemd unit files.
p[ersist] Persist the charge limit after driver reloads.
r[emove] Do not persist the charge limit after driver reloads.
h[elp] Just display this help text.
v[ersion] Just display version information.
The battery with regex 'BAT.' in environment variable BAT_SELECT will be used.
Expand Down
6 changes: 3 additions & 3 deletions help.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Usage: bat <option>
Options (every option except 's[tatus]' needs root privileges):
[s[tatus]] Display charge level, limit, health & persist status.
[l[imit]] <int> Set the charge limit to <int> percent.
p[ersist] Install and enable the persist systemd unit files.
r[emove] Remove the persist systemd unit files.
p[ersist] Persist the charge limit after driver reloads.
r[emove] Do not persist the charge limit after driver reloads.
h[elp] Just display this help text.
v[ersion] Just display version information.
The battery with regex 'BAT.' in environment variable BAT_SELECT will be used.
If environment variable BAT_SELECT is set to regex 'BAT.' then it will be used.
38 changes: 27 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@ import (
)

const (
version = "0.14.0"
years = "2023"
prefix = "chargelimit-"
syspath = "/sys/class/power_supply/"
threshold = "charge_control_end_threshold"
services = "/etc/systemd/system/"
version = "0.15.0"
years = "2023"
prefix = "chargelimit-"
services = "/etc/systemd/system/"
sleepfilename = "/usr/lib/systemd/system-sleep/chargelimit"
syspath = "/sys/class/power_supply/"
threshold = "charge_control_end_threshold"
)

var events = [...]string{
"hibernate",
"hybrid-sleep",
"multi-user",
"sleep",
"suspend",
"suspend-then-hibernate",}
"suspend-then-hibernate"}

var (
//go:embed unit.tmpl
unitfile string
unitfile string
//go:embed system-sleep.tmpl
sleepfile string
//go:embed help.tmpl
helpmsg string
helpmsg string
//go:embed version.tmpl
versionmsg string
batpath string
Expand Down Expand Up @@ -161,6 +163,10 @@ func main() {
disabled = true
}
}
_, err = os.Stat(sleepfilename)
if errors.Is(err, os.ErrNotExist) {
disabled = true
}
enabled := "yes"
if disabled {
enabled = "no"
Expand Down Expand Up @@ -226,9 +232,19 @@ func main() {
errexit("could not enable systemd unit file '" + service + "'")
}
}
f, err := os.Create(sleepfilename)
if err != nil {
errexit("could not create system-sleep file '" + sleepfilename + "'")
}
defer f.Close()
_, err = f.WriteString(fmt.Sprintf(sleepfile, current, bat, current, bat))
if err != nil {
errexit("could not instantiate system-sleep file '" + sleepfilename + "'")
}

fmt.Printf("[%s] Persistence enabled for charge limit: %d\n", bat, current)
case "r", "remove", "-r", "--remove":
os.Remove(sleepfilename)
for _, event := range events {
service := prefix + event + ".service"
file := services + service
Expand All @@ -241,7 +257,7 @@ func main() {
continue
case strings.Contains(message, "Access denied"):
errexit("insufficient permissions, run with root privileges")
default:
default:
errexit("failure to disable unit file '" + service + "'")
}
}
Expand Down
6 changes: 6 additions & 0 deletions system-sleep.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# %d charge_control_end_threshold for %s

test "x$1" = "xpost" &&
/usr/bin/echo %d >/sys/class/power_supply/%s/charge_control_end_threshold
exit 0

0 comments on commit 8726153

Please sign in to comment.