From dc8d143fac5b1a621f2b13f16c56f7a68d0065ff Mon Sep 17 00:00:00 2001 From: tubi16 <49732553+tubi16@users.noreply.github.com> Date: Thu, 4 Jul 2024 14:17:56 +0300 Subject: [PATCH] Add brightness control support with brightnessctl (#626) * Added key binds to control brightness I added keybinds to control brightness with brightnessctl. F5 to decrease brightness. f6 to increase brightness. * Update src/main.zig Co-authored-by: ShiningLea * added proper keybinds and configs for brightness control * Update src/main.zig Co-authored-by: ShiningLea * code improvement and changes * updated en.ini --------- Co-authored-by: ShiningLea --- res/config.ini | 5 +++++ res/lang/en.ini | 1 + src/config/Config.zig | 4 ++++ src/config/Lang.zig | 3 +++ src/interop.zig | 4 ++++ src/main.zig | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 52 insertions(+) diff --git a/res/config.ini b/res/config.ini index 68a39bb..2738c01 100644 --- a/res/config.ini +++ b/res/config.ini @@ -160,3 +160,8 @@ xauth_cmd = /usr/bin/xauth # Xorg desktop environments xsessions = /usr/share/xsessions + +# Brightness control +brightness_down_key = F5 +brightness_up_key = F6 +Brightness_change = 10 \ No newline at end of file diff --git a/res/lang/en.ini b/res/lang/en.ini index d49882a..56c4fd7 100644 --- a/res/lang/en.ini +++ b/res/lang/en.ini @@ -2,6 +2,7 @@ authenticating = authenticating... capslock = capslock err_alloc = failed memory allocation err_bounds = out-of-bounds index +err_brightness_change = failed to change brightness err_chdir = failed to open home folder err_console_dev = failed to access console err_dgn_oob = log message diff --git a/src/config/Config.zig b/src/config/Config.zig index bddda19..e7d2c83 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -51,3 +51,7 @@ xinitrc: ?[]const u8 = "~/.xinitrc", x_cmd_setup: []const u8 = build_options.data_directory ++ "/xsetup.sh", xauth_cmd: []const u8 = "/usr/bin/xauth", xsessions: []const u8 = "/usr/share/xsessions", +brightness_down_key: []const u8 = "F5", +brightness_up_key: []const u8 = "F6", +brightnessctl: []const u8 = "/usr/bin/brightnessctl", +brightness_change: []const u8 = "10", diff --git a/src/config/Lang.zig b/src/config/Lang.zig index 8c4051c..58a9703 100644 --- a/src/config/Lang.zig +++ b/src/config/Lang.zig @@ -2,6 +2,7 @@ authenticating: []const u8 = "authenticating...", capslock: []const u8 = "capslock", err_alloc: []const u8 = "failed memory allocation", err_bounds: []const u8 = "out-of-bounds index", +err_brightness_change: []const u8 = "failed to change brightness", err_chdir: []const u8 = "failed to open home folder", err_console_dev: []const u8 = "failed to access console", err_dgn_oob: []const u8 = "log message", @@ -54,3 +55,5 @@ sleep: []const u8 = "sleep", wayland: []const u8 = "wayland", xinitrc: [:0]const u8 = "xinitrc", x11: []const u8 = "x11", +brightness_down: []const u8 = "decrease brightness", +brightness_up: []const u8 = "increase brightness", diff --git a/src/interop.zig b/src/interop.zig index 14ce55a..b378f34 100644 --- a/src/interop.zig +++ b/src/interop.zig @@ -16,6 +16,10 @@ pub const xcb = @cImport({ @cInclude("xcb/xcb.h"); }); +pub const unistd = @cImport({ + @cInclude("unistd.h"); +}); + pub const c_size = u64; pub const c_uid = u32; pub const c_gid = u32; diff --git a/src/main.zig b/src/main.zig index 6171361..62e1a04 100644 --- a/src/main.zig +++ b/src/main.zig @@ -21,6 +21,7 @@ const utils = @import("tui/utils.zig"); const Ini = ini.Ini; const termbox = interop.termbox; +const unistd = interop.unistd; var session_pid: std.posix.pid_t = -1; pub fn signalHandler(i: c_int) callconv(.C) void { @@ -245,6 +246,10 @@ pub fn main() !void { const restart_key = try std.fmt.parseInt(u8, config.restart_key[1..], 10); const restart_len = try utils.strWidth(lang.restart); const sleep_key = try std.fmt.parseInt(u8, config.sleep_key[1..], 10); + const brightness_down_key = try std.fmt.parseInt(u8, config.brightness_down_key[1..], 10); + 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_len = try utils.strWidth(lang.brightness_up); var event: termbox.tb_event = undefined; var run = true; @@ -388,6 +393,20 @@ pub fn main() !void { buffer.drawLabel(lang.restart, length, 0); length += restart_len + 1; + buffer.drawLabel(config.brightness_down_key, length, 0); + length += config.brightness_down_key.len + 1; + buffer.drawLabel(" ", length - 1, 0); + + 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); + + buffer.drawLabel(lang.brightness_up, length, 0); + length += brightness_up_len + 1; + if (config.sleep_cmd != null) { buffer.drawLabel(config.sleep_key, length, 0); length += config.sleep_key.len + 1; @@ -482,6 +501,22 @@ pub fn main() !void { var sleep = std.ChildProcess.init(&[_][]const u8{ "/bin/sh", "-c", sleep_cmd }, allocator); _ = sleep.spawnAndWait() catch .{}; } + } else if (pressed_key == brightness_down_key and unistd.access(&config.brightnessctl[0], unistd.X_OK) == 0) brightness_change: { + const brightness_str = std.fmt.allocPrint(allocator, "{s}%-", .{config.brightness_change}) catch { + try info_line.setText(lang.err_brightness_change); + break :brightness_change; + }; + defer allocator.free(brightness_str); + var brightness = std.ChildProcess.init(&[_][]const u8{ config.brightnessctl, "-q", "s", brightness_str }, allocator); + _ = brightness.spawnAndWait() catch .{}; + } else if (pressed_key == brightness_up_key and unistd.access(&config.brightnessctl[0], unistd.X_OK) == 0) brightness_change: { + const brightness_str = std.fmt.allocPrint(allocator, "+{s}%", .{config.brightness_change}) catch { + try info_line.setText(lang.err_brightness_change); + break :brightness_change; + }; + defer allocator.free(brightness_str); + var brightness = std.ChildProcess.init(&[_][]const u8{ config.brightnessctl, "-q", "s", brightness_str }, allocator); + _ = brightness.spawnAndWait() catch .{}; } }, termbox.TB_KEY_CTRL_C => run = false,