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 <anerruption@disroot.org>

* added proper keybinds and configs for brightness control

* Update src/main.zig

Co-authored-by: ShiningLea <anerruption@disroot.org>

* code improvement and changes

* updated en.ini

---------

Co-authored-by: ShiningLea <anerruption@disroot.org>
This commit is contained in:
tubi16
2024-07-04 14:17:56 +03:00
committed by GitHub
parent cbe7b37564
commit dc8d143fac
6 changed files with 52 additions and 0 deletions

View File

@@ -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,