2023-10-10 18:03:39 +00:00
|
|
|
const std = @import("std");
|
2023-11-13 01:57:51 +00:00
|
|
|
const c = @cImport({
|
|
|
|
@cInclude("libnotify/notify.h");
|
|
|
|
});
|
2023-10-10 18:03:39 +00:00
|
|
|
|
|
|
|
pub const std_options = struct {
|
|
|
|
pub const log_level = .info;
|
|
|
|
};
|
|
|
|
|
2023-11-13 01:57:51 +00:00
|
|
|
const NOTIFICATION_TITLE: [:0]const u8 = "bat-alert";
|
|
|
|
const NOTIFICATION_BODY: [:0]const u8 = "battery running low!";
|
|
|
|
|
|
|
|
const ParseMode = enum { none, target, battery };
|
|
|
|
|
2023-10-10 18:03:39 +00:00
|
|
|
pub fn main() !void {
|
2023-11-13 01:57:51 +00:00
|
|
|
if (c.notify_init("bat-alert") == c.FALSE) {
|
|
|
|
std.log.err("notify_init failed", .{});
|
|
|
|
std.process.exit(1);
|
|
|
|
}
|
|
|
|
|
2023-10-10 18:03:39 +00:00
|
|
|
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
|
|
|
|
2023-11-13 01:57:51 +00:00
|
|
|
var parse_mode = ParseMode.none;
|
2023-10-10 18:03:39 +00:00
|
|
|
for (args, 0..) |arg, i| {
|
2023-11-13 01:57:51 +00:00
|
|
|
_ = i;
|
|
|
|
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;
|
|
|
|
},
|
2023-11-02 20:02:14 +00:00
|
|
|
}
|
2023-10-10 18:03:39 +00:00
|
|
|
}
|
2023-11-13 01:57:51 +00:00
|
|
|
if (parse_mode != ParseMode.none) {
|
|
|
|
std.log.err("option requires an argument: --{s}", .{@tagName(parse_mode)});
|
|
|
|
std.process.exit(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-13 01:57:51 +00:00
|
|
|
const capacityFile = std.fs.openFileAbsolute(batteryPath, .{}) catch |err| {
|
|
|
|
std.log.err("open {s}: {}", .{ batteryPath, err });
|
|
|
|
std.process.exit(1);
|
|
|
|
};
|
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;
|
2023-11-13 01:57:51 +00:00
|
|
|
// _ = 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});
|
|
|
|
}
|
|
|
|
|
2023-10-10 18:03:39 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|