From 6079c01a4bc814c92af3e8f8afe77e3922ebfdec Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Thu, 6 Mar 2025 19:16:47 +0100 Subject: [PATCH] Allow disabling the brightness control commands (closes #664) Signed-off-by: AnErrupTion --- res/config.ini | 4 +-- src/config/Config.zig | 4 +-- src/main.zig | 66 +++++++++++++++++++++++++------------------ 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/res/config.ini b/res/config.ini index cef6040..b568697 100644 --- a/res/config.ini +++ b/res/config.ini @@ -58,13 +58,13 @@ box_title = null # Brightness increase command brightness_down_cmd = $PREFIX_DIRECTORY/bin/brightnessctl -q s 10%- -# Brightness decrease key +# Brightness decrease key, or null to disable brightness_down_key = F5 # Brightness increase command brightness_up_cmd = $PREFIX_DIRECTORY/bin/brightnessctl -q s +10% -# Brightness increase key +# Brightness increase key, or null to disable brightness_up_key = F6 # Erase password input on failure diff --git a/src/config/Config.zig b/src/config/Config.zig index e5f57d1..5b8de0f 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -16,9 +16,9 @@ blank_box: bool = true, border_fg: u32 = 0x00FFFFFF, box_title: ?[]const u8 = null, brightness_down_cmd: [:0]const u8 = build_options.prefix_directory ++ "/bin/brightnessctl -q s 10%-", -brightness_down_key: []const u8 = "F5", +brightness_down_key: ?[]const u8 = "F5", brightness_up_cmd: [:0]const u8 = build_options.prefix_directory ++ "/bin/brightnessctl -q s +10%", -brightness_up_key: []const u8 = "F6", +brightness_up_key: ?[]const u8 = "F6", clear_password: bool = false, clock: ?[:0]const u8 = null, cmatrix_fg: u32 = 0x0000FF00, diff --git a/src/main.zig b/src/main.zig index 39f1bfd..a22daa6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -357,9 +357,9 @@ pub fn main() !void { const restart_len = try utils.strWidth(lang.restart); const sleep_key = try std.fmt.parseInt(u8, config.sleep_key[1..], 10); const sleep_len = try utils.strWidth(lang.sleep); - const brightness_down_key = try std.fmt.parseInt(u8, config.brightness_down_key[1..], 10); + const brightness_down_key = if (config.brightness_down_key) |key| try std.fmt.parseInt(u8, key[1..], 10) else null; const brightness_down_len = try utils.strWidth(lang.brightness_down); - const brightness_up_key = try std.fmt.parseInt(u8, config.brightness_up_key[1..], 10); + const brightness_up_key = if (config.brightness_down_key) |key| try std.fmt.parseInt(u8, key[1..], 10) else null; const brightness_up_len = try utils.strWidth(lang.brightness_up); var event: termbox.tb_event = undefined; @@ -503,19 +503,23 @@ pub fn main() !void { length += sleep_len + 1; } - buffer.drawLabel(config.brightness_down_key, length, 0); - length += config.brightness_down_key.len + 1; - buffer.drawLabel(" ", length - 1, 0); + if (config.brightness_down_key) |key| { + buffer.drawLabel(key, length, 0); + length += key.len + 1; + buffer.drawLabel(" ", length - 1, 0); - buffer.drawLabel(lang.brightness_down, length, 0); - length += brightness_down_len + 1; + buffer.drawLabel(lang.brightness_down, length, 0); + length += brightness_down_len + 1; + } - buffer.drawLabel(config.brightness_up_key, length, 0); - length += config.brightness_up_key.len + 1; - buffer.drawLabel(" ", length - 1, 0); + if (config.brightness_up_key) |key| { + buffer.drawLabel(key, length, 0); + length += key.len + 1; + buffer.drawLabel(" ", length - 1, 0); - buffer.drawLabel(lang.brightness_up, length, 0); - length += brightness_up_len + 1; + buffer.drawLabel(lang.brightness_up, length, 0); + length += brightness_up_len + 1; + } } if (config.box_title) |title| { @@ -627,21 +631,14 @@ pub fn main() !void { } } } - } else if (pressed_key == brightness_down_key or pressed_key == brightness_up_key) { - const cmd = if (pressed_key == brightness_down_key) config.brightness_down_cmd else config.brightness_up_cmd; - - var brightness = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", cmd }, allocator); - brightness.stdout_behavior = .Ignore; - brightness.stderr_behavior = .Ignore; - - handle_brightness_cmd: { - const process_result = brightness.spawnAndWait() catch { - break :handle_brightness_cmd; - }; - if (process_result.Exited != 0) { - try info_line.addMessage(lang.err_brightness_change, config.error_bg, config.error_fg); - } - } + } else if (brightness_down_key != null and pressed_key == brightness_down_key.?) { + adjustBrightness(allocator, config.brightness_down_cmd) catch { + try info_line.addMessage(lang.err_brightness_change, config.error_bg, config.error_fg); + }; + } else if (brightness_up_key != null and pressed_key == brightness_up_key.?) { + adjustBrightness(allocator, config.brightness_up_cmd) catch { + try info_line.addMessage(lang.err_brightness_change, config.error_bg, config.error_fg); + }; } }, termbox.TB_KEY_CTRL_C => run = false, @@ -832,6 +829,21 @@ pub fn main() !void { } } +fn adjustBrightness(allocator: std.mem.Allocator, cmd: []const u8) !void { + var brightness = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", cmd }, allocator); + brightness.stdout_behavior = .Ignore; + brightness.stderr_behavior = .Ignore; + + handle_brightness_cmd: { + const process_result = brightness.spawnAndWait() catch { + break :handle_brightness_cmd; + }; + if (process_result.Exited != 0) { + return error.BrightnessChangeFailed; + } + } +} + fn getAuthErrorMsg(err: anyerror, lang: Lang) []const u8 { return switch (err) { error.GetPasswordNameFailed => lang.err_pwnam,