srctree

Gregory Mullen parent 2b1d9b21 2799596e
scrolling on backlight now changes brightness

src/main.zig added: 29, removed: 8, total 21
@@ -35,7 +35,7 @@ const Body = struct {
markup: ?[]const u8 = null,
};
 
const Click = struct {
pub const Click = struct {
name: []u8,
instance: []u8,
button: u8,
@@ -65,9 +65,10 @@ fn date(_: ?Click) anyerror!Body {
var bl_handle: ?Video.Backlight = null;
var bl_buffer: [1024]u8 = undefined;
fn bl(click: ?Click) !Body {
if (bl_handle) |handle| {
if (bl_handle) |*handle| {
if (click) |clk| {
var dir: ?Mouse.Button = null;
try handle.click(clk);
if (clk.button == 4 or clk.button == 5) {
dir = if (clk.button == 4) .up else .down;
return Body{
@@ -81,8 +82,9 @@ fn bl(click: ?Click) !Body {
};
}
}
try handle.update(std.time.timestamp());
return Body{
.full_text = try std.fmt.bufPrint(&bl_buffer, "{}", .{try Video.Backlight.init()}),
.full_text = try std.fmt.bufPrint(&bl_buffer, "{}", .{handle}),
.name = "backlight",
.instance = "backlight_0",
};
 
src/video.zig added: 29, removed: 8, total 21
@@ -1,7 +1,8 @@
const std = @import("std");
const Click = @import("main.zig").Click;
 
pub const Backlight = struct {
update: i64 = 0,
next_update: i64 = 0,
current: u32 = 0,
max: u32 = 0,
 
@@ -26,11 +27,29 @@ pub const Backlight = struct {
}
 
pub fn update(self: *Backlight, t: i64) !void {
if (self.update > t) return;
self.update = t + 10;
if (self.next_update > t) return;
self.next_update = t + 10;
try self.read(false);
}
 
pub fn click(self: *Backlight, clk: Click) !void {
var goal: [0x20]u8 = undefined;
var out: []u8 = undefined;
if (clk.button == 4) {
self.current = @min(255, @max(self.current + 20, 1));
out = try std.fmt.bufPrint(&goal, "{}", .{self.current});
} else if (clk.button == 5) {
self.current = @min(255, @max(self.current - 20, 1));
out = try std.fmt.bufPrint(&goal, "{}", .{self.current});
} else return;
var file = try std.fs.openFileAbsolute(
"/sys/class/backlight/amdgpu_bl1/brightness",
.{ .mode = .read_write },
);
defer file.close();
try file.writeAll(out);
}
 
pub fn format(self: Backlight, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
const pct = self.current * 100 / self.max;
return out.print("BL {}%", .{pct});