Compare commits
3 commits
b57cbba805
...
5a93a1b803
Author | SHA1 | Date | |
---|---|---|---|
5a93a1b803 | |||
4e69ce3cc5 | |||
d483eb1bcd |
2 changed files with 84 additions and 51 deletions
|
@ -24,6 +24,12 @@ pub fn build(b: *std.Build) void {
|
||||||
.optimize = optimize,
|
.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
|
// This declares intent for the executable to be installed into the
|
||||||
// standard location when the user invokes the "install" step (the default
|
// standard location when the user invokes the "install" step (the default
|
||||||
// step when running `zig build`).
|
// step when running `zig build`).
|
||||||
|
|
93
src/main.zig
93
src/main.zig
|
@ -1,21 +1,32 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const c = @cImport({
|
||||||
|
@cInclude("libnotify/notify.h");
|
||||||
|
});
|
||||||
|
|
||||||
pub const std_options = struct {
|
pub const std_options = struct {
|
||||||
pub const log_level = .info;
|
pub const log_level = .info;
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
const NOTIFICATION_TITLE: [:0]const u8 = "bat-alert";
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
const NOTIFICATION_BODY: [:0]const u8 = "battery running low!";
|
||||||
defer _ = gpa.deinit();
|
|
||||||
const allocator = gpa.allocator();
|
|
||||||
|
|
||||||
const args = try std.process.argsAlloc(allocator);
|
const ParseMode = enum { none, target, battery };
|
||||||
defer std.process.argsFree(allocator, args);
|
|
||||||
|
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 target: u8 = 15;
|
||||||
var battery: []const u8 = "BAT0";
|
var battery: []const u8 = "BAT0";
|
||||||
|
|
||||||
for (args, 0..) |arg, i| {
|
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")) {
|
if (std.mem.eql(u8, arg, "--help")) {
|
||||||
const stdout = std.io.getStdOut().writer();
|
const stdout = std.io.getStdOut().writer();
|
||||||
try stdout.print(
|
try stdout.print(
|
||||||
|
@ -28,57 +39,73 @@ pub fn main() !void {
|
||||||
, .{});
|
, .{});
|
||||||
std.process.exit(0);
|
std.process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std.mem.eql(u8, arg, "--target") or std.mem.eql(u8, arg, "-t")) {
|
if (std.mem.eql(u8, arg, "--target") or std.mem.eql(u8, arg, "-t")) {
|
||||||
if (i + 1 == args.len) {
|
parse_mode = ParseMode.target;
|
||||||
std.log.err("--target requires an argument", .{});
|
|
||||||
std.process.exit(1);
|
|
||||||
}
|
}
|
||||||
|
if (std.mem.eql(u8, arg, "--battery")) {
|
||||||
const parsedTarget = std.fmt.parseInt(u8, args[i + 1], 10) catch |err| {
|
parse_mode = ParseMode.battery;
|
||||||
std.log.err("invalid target '{s}': {}", .{ args[i + 1], err });
|
}
|
||||||
|
},
|
||||||
|
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);
|
std.process.exit(1);
|
||||||
};
|
};
|
||||||
target = parsedTarget;
|
target = parsedTarget;
|
||||||
|
parse_mode = ParseMode.none;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (std.mem.eql(u8, arg, "--battery")) {
|
if (parse_mode != ParseMode.none) {
|
||||||
if (i + 1 == args.len) {
|
std.log.err("option requires an argument: --{s}", .{@tagName(parse_mode)});
|
||||||
std.log.err("--battery requires an argument", .{});
|
|
||||||
std.process.exit(1);
|
std.process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
battery = args[i + 1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var sent = false;
|
var sent = false;
|
||||||
const batteryPath = try std.fmt.allocPrint(allocator, "/sys/class/power_supply/{s}/capacity", .{battery});
|
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) {
|
while (true) {
|
||||||
const capacityFile = try std.fs.openFileAbsolute(batteryPath, .{});
|
const capacityFile = std.fs.openFileAbsolute(batteryPath, .{}) catch |err| {
|
||||||
var capacityStr = std.ArrayList(u8).init(allocator);
|
std.log.err("open {s}: {}", .{ batteryPath, err });
|
||||||
try capacityFile.reader().streamUntilDelimiter(capacityStr.writer(), '\n', null);
|
std.process.exit(1);
|
||||||
|
};
|
||||||
|
_ = try capacityFile.reader().readAll(&capBuf);
|
||||||
capacityFile.close();
|
capacityFile.close();
|
||||||
|
var splat = std.mem.splitScalar(u8, &capBuf, '\n');
|
||||||
|
|
||||||
// std.log.info("battery: {s}", .{capacityStr.items});
|
// std.log.info("battery: {s}", .{splat.first()});
|
||||||
|
|
||||||
const capacity = try std.fmt.parseInt(u8, capacityStr.items, 10);
|
const capacity = try std.fmt.parseInt(u8, splat.first(), 10);
|
||||||
// std.log.info("battery (but parsed): {}", .{capacity});
|
// std.log.info("battery (but parsed): {}", .{capacity});
|
||||||
|
|
||||||
if (capacity <= target) {
|
if (capacity <= target) {
|
||||||
if (!sent) {
|
if (!sent) {
|
||||||
sent = true;
|
sent = true;
|
||||||
_ = try std.process.Child.exec(.{
|
// _ = try std.process.Child.exec(.{
|
||||||
.allocator = allocator,
|
// .allocator = allocator,
|
||||||
.argv = &[_][]const u8{ "notify-send", "-t", "5000", "-u", "critical", "bat-alert", "battery running low!" },
|
// .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!", .{});
|
std.log.info("[NOTIFY] battery running low!", .{});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sent = false;
|
sent = false;
|
||||||
}
|
}
|
||||||
capacityStr.deinit();
|
|
||||||
|
|
||||||
std.time.sleep(10 * 1e9);
|
std.time.sleep(10 * 1e9);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue