Add option to run command after inactivity delay (closes #747)

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2025-12-03 20:39:27 +01:00
parent 6cce221cbf
commit 3bfdc75a70
25 changed files with 64 additions and 6 deletions

View File

@@ -57,6 +57,8 @@ hide_borders: bool = false,
hide_key_hints: bool = false,
hide_keyboard_locks: bool = false,
hide_version_string: bool = false,
inactivity_cmd: ?[]const u8 = null,
inactivity_delay: u16 = 0,
initial_info_text: ?[]const u8 = null,
input_len: u8 = 34,
lang: []const u8 = "en",

View File

@@ -24,6 +24,7 @@ err_envlist: []const u8 = "failed to get envlist",
err_get_active_tty: []const u8 = "failed to get active tty",
err_hibernate: []const u8 = "failed to execute hibernate command",
err_hostname: []const u8 = "failed to get hostname",
err_inactivity: []const u8 = "failed to execute inactivity command",
err_lock_state: []const u8 = "failed to get lock state",
err_log: []const u8 = "failed to open log file",
err_mlock: []const u8 = "failed to lock password memory",

View File

@@ -98,7 +98,7 @@ pub fn main() !void {
defer _ = gpa.deinit();
// Allows stopping an animation after some time
const time_start = try interop.getTimeOfDay();
const animation_time_start = try interop.getTimeOfDay();
var animation_timed_out: bool = false;
const allocator = gpa.allocator();
@@ -284,12 +284,12 @@ pub fn main() !void {
commands_allocated = true;
if (config.start_cmd) |start_cmd| {
var sleep = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", start_cmd }, allocator);
sleep.stdout_behavior = .Ignore;
sleep.stderr_behavior = .Ignore;
var start = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", start_cmd }, allocator);
start.stdout_behavior = .Ignore;
start.stderr_behavior = .Ignore;
handle_start_cmd: {
const process_result = sleep.spawnAndWait() catch {
const process_result = start.spawnAndWait() catch {
break :handle_start_cmd;
};
start_cmd_exit_code = process_result.Exited;
@@ -594,6 +594,8 @@ pub fn main() !void {
var update = true;
var resolution_changed = false;
var auth_fails: u64 = 0;
var inactivity_time_start = try interop.getTimeOfDay();
var inactivity_cmd_ran = false;
// Switch to selected TTY
const active_tty = interop.getActiveTty(allocator) catch |err| no_tty_found: {
@@ -858,7 +860,7 @@ pub fn main() !void {
// Check how long we've been running so we can turn off the animation
const time = try interop.getTimeOfDay();
if (config.animation_timeout_sec > 0 and time.seconds - time_start.seconds > config.animation_timeout_sec) {
if (config.animation_timeout_sec > 0 and time.seconds - animation_time_start.seconds > config.animation_timeout_sec) {
animation_timed_out = true;
animation.deinit();
}
@@ -872,6 +874,28 @@ pub fn main() !void {
timeout = @intCast(1000 - @divTrunc(time.microseconds, 1000) + 1);
}
if (config.inactivity_cmd) |inactivity_cmd| {
const time = try interop.getTimeOfDay();
if (!inactivity_cmd_ran and time.seconds - inactivity_time_start.seconds > config.inactivity_delay) {
var inactivity = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", inactivity_cmd }, allocator);
inactivity.stdout_behavior = .Ignore;
inactivity.stderr_behavior = .Ignore;
handle_inactivity_cmd: {
const process_result = inactivity.spawnAndWait() catch {
break :handle_inactivity_cmd;
};
if (process_result.Exited != 0) {
try info_line.addMessage(lang.err_inactivity, config.error_bg, config.error_fg);
try log_writer.print("failed to execute inactivity command: exit code {d}\n", .{process_result.Exited});
}
}
inactivity_cmd_ran = true;
}
}
// Skip event polling if autologin is set, use simulated Enter key press instead
if (is_autologin) {
event = termbox.tb_event{
@@ -892,6 +916,9 @@ pub fn main() !void {
if (event_error < 0 or event.type != termbox.TB_EVENT_KEY) continue;
}
// Input of some kind was detected, so reset the inactivity timer
inactivity_time_start = try interop.getTimeOfDay();
switch (event.key) {
termbox.TB_KEY_ESC => {
if (config.vi_mode and insert_mode) {