bat-alert/src/main.zig

86 lines
2.7 KiB
Zig
Raw Normal View History

2023-10-10 18:03:39 +00:00
const std = @import("std");
pub const std_options = struct {
pub const log_level = .info;
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
var target: u8 = 15;
2023-11-02 20:02:14 +00:00
var battery: []const u8 = "BAT0";
2023-10-10 18:03:39 +00:00
for (args, 0..) |arg, i| {
if (std.mem.eql(u8, arg, "--help")) {
const stdout = std.io.getStdOut().writer();
try stdout.print(
\\Usage: bat-alert [options]
\\
\\Options:
2023-11-02 20:02:52 +00:00
\\ -t, --target NUM Target battery level (default: 15)
\\ --battery NAME Battery name (default: BAT0)
2023-10-10 18:03:39 +00:00
\\
, .{});
std.process.exit(0);
}
if (std.mem.eql(u8, arg, "--target") or std.mem.eql(u8, arg, "-t")) {
if (i + 1 == args.len) {
std.log.err("--target requires an argument", .{});
std.process.exit(1);
}
const parsedTarget = std.fmt.parseInt(u8, args[i + 1], 10) catch |err| {
std.log.err("invalid target '{s}': {}", .{ args[i + 1], err });
std.process.exit(1);
};
target = parsedTarget;
}
2023-11-02 20:02:14 +00:00
if (std.mem.eql(u8, arg, "--battery")) {
if (i + 1 == args.len) {
std.log.err("--battery requires an argument", .{});
std.process.exit(1);
}
battery = args[i + 1];
}
2023-10-10 18:03:39 +00:00
}
var sent = false;
2023-11-02 20:02:14 +00:00
const batteryPath = try std.fmt.allocPrint(allocator, "/sys/class/power_supply/{s}/capacity", .{battery});
2023-10-10 18:03:39 +00:00
while (true) {
2023-11-02 20:02:14 +00:00
const capacityFile = try std.fs.openFileAbsolute(batteryPath, .{});
2023-10-10 18:03:39 +00:00
var capacityStr = std.ArrayList(u8).init(allocator);
try capacityFile.reader().streamUntilDelimiter(capacityStr.writer(), '\n', null);
capacityFile.close();
// std.log.info("battery: {s}", .{capacityStr.items});
const capacity = try std.fmt.parseInt(u8, capacityStr.items, 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", "bat-alert", "battery running low!" },
});
std.log.info("[NOTIFY] battery running low!", .{});
}
} else {
sent = false;
}
2023-11-02 20:02:14 +00:00
capacityStr.deinit();
2023-10-10 18:03:39 +00:00
std.time.sleep(10 * 1e9);
}
}