forked from ptrcnull/bat-alert
Compare commits
8 commits
Author | SHA1 | Date | |
---|---|---|---|
5a93a1b803 | |||
4e69ce3cc5 | |||
d483eb1bcd | |||
b57cbba805 | |||
f5101f8915 | |||
4e80ad4617 | |||
|
d230d84e91 | ||
|
cc129b9387 |
6 changed files with 190 additions and 69 deletions
18
.drone.yml
18
.drone.yml
|
@ -1,18 +0,0 @@
|
||||||
---
|
|
||||||
kind: pipeline
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: build
|
|
||||||
image: "golang:alpine"
|
|
||||||
commands:
|
|
||||||
- go build
|
|
||||||
|
|
||||||
- name: release
|
|
||||||
image: plugins/gitea-release
|
|
||||||
settings:
|
|
||||||
api_key:
|
|
||||||
from_secret: gitea_api_key
|
|
||||||
base_url: https://git.ddd.rip
|
|
||||||
files: bat-alert
|
|
||||||
when:
|
|
||||||
event: tag
|
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
zig-cache/
|
||||||
|
zig-out/
|
76
build.zig
Normal file
76
build.zig
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
// Although this function looks imperative, note that its job is to
|
||||||
|
// declaratively construct a build graph that will be executed by an external
|
||||||
|
// runner.
|
||||||
|
pub fn build(b: *std.Build) void {
|
||||||
|
// Standard target options allows the person running `zig build` to choose
|
||||||
|
// what target to build for. Here we do not override the defaults, which
|
||||||
|
// means any target is allowed, and the default is native. Other options
|
||||||
|
// for restricting supported target set are available.
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
|
|
||||||
|
// Standard optimization options allow the person running `zig build` to select
|
||||||
|
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
|
||||||
|
// set a preferred release mode, allowing the user to decide how to optimize.
|
||||||
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
|
const exe = b.addExecutable(.{
|
||||||
|
.name = "bat-alert",
|
||||||
|
// In this case the main source file is merely a path, however, in more
|
||||||
|
// complicated build scripts, this could be a generated file.
|
||||||
|
.root_source_file = .{ .path = "src/main.zig" },
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
exe.linkLibC();
|
||||||
|
exe.addIncludePath(.{ .cwd_relative = "/usr/include/glib-2.0" });
|
||||||
|
exe.addIncludePath(.{ .cwd_relative = "/usr/lib/glib-2.0/include" });
|
||||||
|
exe.addIncludePath(.{ .cwd_relative = "/usr/include/gdk-pixbuf-2.0" });
|
||||||
|
exe.linkSystemLibrary("notify");
|
||||||
|
|
||||||
|
// This declares intent for the executable to be installed into the
|
||||||
|
// standard location when the user invokes the "install" step (the default
|
||||||
|
// step when running `zig build`).
|
||||||
|
b.installArtifact(exe);
|
||||||
|
|
||||||
|
// This *creates* a Run step in the build graph, to be executed when another
|
||||||
|
// step is evaluated that depends on it. The next line below will establish
|
||||||
|
// such a dependency.
|
||||||
|
const run_cmd = b.addRunArtifact(exe);
|
||||||
|
|
||||||
|
// By making the run step depend on the install step, it will be run from the
|
||||||
|
// installation directory rather than directly from within the cache directory.
|
||||||
|
// This is not necessary, however, if the application depends on other installed
|
||||||
|
// files, this ensures they will be present and in the expected location.
|
||||||
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
|
||||||
|
// This allows the user to pass arguments to the application in the build
|
||||||
|
// command itself, like this: `zig build run -- arg1 arg2 etc`
|
||||||
|
if (b.args) |args| {
|
||||||
|
run_cmd.addArgs(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This creates a build step. It will be visible in the `zig build --help` menu,
|
||||||
|
// and can be selected like this: `zig build run`
|
||||||
|
// This will evaluate the `run` step rather than the default, which is "install".
|
||||||
|
const run_step = b.step("run", "Run the app");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
|
// Creates a step for unit testing. This only builds the test executable
|
||||||
|
// but does not run it.
|
||||||
|
const unit_tests = b.addTest(.{
|
||||||
|
.root_source_file = .{ .path = "src/main.zig" },
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const run_unit_tests = b.addRunArtifact(unit_tests);
|
||||||
|
|
||||||
|
// Similar to creating the run step earlier, this exposes a `test` step to
|
||||||
|
// the `zig build --help` menu, providing a way for the user to request
|
||||||
|
// running the unit tests.
|
||||||
|
const test_step = b.step("test", "Run unit tests");
|
||||||
|
test_step.dependOn(&run_unit_tests.step);
|
||||||
|
}
|
3
go.mod
3
go.mod
|
@ -1,3 +0,0 @@
|
||||||
module git.ddd.rip/ptrcnull/bat-alert
|
|
||||||
|
|
||||||
go 1.16
|
|
48
main.go
48
main.go
|
@ -1,48 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func notify(msg string) {
|
|
||||||
cmd := exec.Command("notify-send", "-t", "5000", "bat-alert", msg)
|
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var targetFlag = flag.Int("target", 15, "when to send the alert")
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
flag.Parse()
|
|
||||||
sent := false
|
|
||||||
for {
|
|
||||||
capacity, err := os.ReadFile("/sys/class/power_supply/BAT0/capacity")
|
|
||||||
if err != nil {
|
|
||||||
notify("error: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
numCap, err := strconv.Atoi(strings.Trim(string(capacity), "\n"))
|
|
||||||
if err != nil {
|
|
||||||
notify("error: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if numCap < 15 {
|
|
||||||
if !sent {
|
|
||||||
sent = true
|
|
||||||
notify("battery running low!")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sent = false
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(time.Second * 10)
|
|
||||||
}
|
|
||||||
}
|
|
112
src/main.zig
Normal file
112
src/main.zig
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const c = @cImport({
|
||||||
|
@cInclude("libnotify/notify.h");
|
||||||
|
});
|
||||||
|
|
||||||
|
pub const std_options = struct {
|
||||||
|
pub const log_level = .info;
|
||||||
|
};
|
||||||
|
|
||||||
|
const NOTIFICATION_TITLE: [:0]const u8 = "bat-alert";
|
||||||
|
const NOTIFICATION_BODY: [:0]const u8 = "battery running low!";
|
||||||
|
|
||||||
|
const ParseMode = enum { none, target, battery };
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
if (c.notify_init("bat-alert") == c.FALSE) {
|
||||||
|
std.log.err("notify_init failed", .{});
|
||||||
|
std.process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var target: u8 = 15;
|
||||||
|
var battery: []const u8 = "BAT0";
|
||||||
|
|
||||||
|
var argsIter = std.process.args();
|
||||||
|
|
||||||
|
var parse_mode = ParseMode.none;
|
||||||
|
while (argsIter.next()) |arg| {
|
||||||
|
switch (parse_mode) {
|
||||||
|
ParseMode.none => {
|
||||||
|
if (std.mem.eql(u8, arg, "--help")) {
|
||||||
|
const stdout = std.io.getStdOut().writer();
|
||||||
|
try stdout.print(
|
||||||
|
\\Usage: bat-alert [options]
|
||||||
|
\\
|
||||||
|
\\Options:
|
||||||
|
\\ -t, --target NUM Target battery level (default: 15)
|
||||||
|
\\ --battery NAME Battery name (default: BAT0)
|
||||||
|
\\
|
||||||
|
, .{});
|
||||||
|
std.process.exit(0);
|
||||||
|
}
|
||||||
|
if (std.mem.eql(u8, arg, "--target") or std.mem.eql(u8, arg, "-t")) {
|
||||||
|
parse_mode = ParseMode.target;
|
||||||
|
}
|
||||||
|
if (std.mem.eql(u8, arg, "--battery")) {
|
||||||
|
parse_mode = ParseMode.battery;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ParseMode.battery => {
|
||||||
|
battery = arg;
|
||||||
|
parse_mode = ParseMode.none;
|
||||||
|
},
|
||||||
|
ParseMode.target => {
|
||||||
|
const parsedTarget = std.fmt.parseInt(u8, arg, 10) catch {
|
||||||
|
std.log.err("cannot parse target '{s}' as int", .{arg});
|
||||||
|
std.process.exit(1);
|
||||||
|
};
|
||||||
|
target = parsedTarget;
|
||||||
|
parse_mode = ParseMode.none;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (parse_mode != ParseMode.none) {
|
||||||
|
std.log.err("option requires an argument: --{s}", .{@tagName(parse_mode)});
|
||||||
|
std.process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var sent = false;
|
||||||
|
var batBuf: [256]u8 = undefined;
|
||||||
|
var capBuf: [8]u8 = undefined;
|
||||||
|
|
||||||
|
const batteryPath = try std.fmt.bufPrint(&batBuf, "/sys/class/power_supply/{s}/capacity", .{battery});
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const capacityFile = std.fs.openFileAbsolute(batteryPath, .{}) catch |err| {
|
||||||
|
std.log.err("open {s}: {}", .{ batteryPath, err });
|
||||||
|
std.process.exit(1);
|
||||||
|
};
|
||||||
|
_ = try capacityFile.reader().readAll(&capBuf);
|
||||||
|
capacityFile.close();
|
||||||
|
var splat = std.mem.splitScalar(u8, &capBuf, '\n');
|
||||||
|
|
||||||
|
// std.log.info("battery: {s}", .{splat.first()});
|
||||||
|
|
||||||
|
const capacity = try std.fmt.parseInt(u8, splat.first(), 10);
|
||||||
|
// std.log.info("battery (but parsed): {}", .{capacity});
|
||||||
|
|
||||||
|
if (capacity <= target) {
|
||||||
|
if (!sent) {
|
||||||
|
sent = true;
|
||||||
|
// _ = try std.process.Child.exec(.{
|
||||||
|
// .allocator = allocator,
|
||||||
|
// .argv = &[_][]const u8{ "notify-send", "-t", "5000", "-u", "critical", NOTIFICATION_TITLE, NOTIFICATION_BODY },
|
||||||
|
// });
|
||||||
|
|
||||||
|
const notif = c.notify_notification_new(NOTIFICATION_TITLE, NOTIFICATION_BODY, null);
|
||||||
|
var g_err: [*c]c.GError = null;
|
||||||
|
const ret = c.notify_notification_show(notif, &g_err);
|
||||||
|
if (ret == c.FALSE) {
|
||||||
|
std.log.err("failed to send a notification: {s}", .{g_err.*.message});
|
||||||
|
}
|
||||||
|
c.g_object_unref(c.G_OBJECT(notif));
|
||||||
|
|
||||||
|
std.log.info("[NOTIFY] battery running low!", .{});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sent = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std.time.sleep(10 * 1e9);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue