Backlight.zig (3208B)
1 const std = @import("std"); 2 3 const udev = @import("udev"); 4 5 const render = @import("../render.zig"); 6 const utils = @import("../utils.zig"); 7 const Backlight = @This(); 8 9 const state = &@import("root").state; 10 11 context: *udev.Udev, 12 monitor: *udev.Monitor, 13 fd: std.posix.fd_t, 14 devices: DeviceList, 15 16 const Device = struct { 17 name: []const u8, 18 value: u64, 19 max: u64, 20 }; 21 22 const DeviceList = std.ArrayList(Device); 23 24 pub fn init() !Backlight { 25 const context = try udev.Udev.new(); 26 27 const monitor = try udev.Monitor.newFromNetlink(context, "udev"); 28 try monitor.filterAddMatchSubsystemDevType("backlight", null); 29 try monitor.filterAddMatchSubsystemDevType("power_supply", null); 30 try monitor.enableReceiving(); 31 32 var devices = DeviceList.init(state.gpa); 33 try updateDevices(state.gpa, context, &devices); 34 35 return Backlight{ 36 .context = context, 37 .monitor = monitor, 38 .fd = try monitor.getFd(), 39 .devices = devices, 40 }; 41 } 42 43 pub fn deinit(self: *Backlight) void { 44 _ = self.context.unref(); 45 for (self.devices.items) |*device| { 46 state.gpa.free(device.name); 47 } 48 self.devices.deinit(); 49 } 50 51 pub fn refresh(self: *Backlight) !void { 52 _ = try self.monitor.receiveDevice(); 53 54 for (state.wayland.monitors.items) |monitor| { 55 if (monitor.bar) |bar| { 56 if (bar.configured) { 57 render.renderModules(bar) catch continue; 58 bar.modules.surface.commit(); 59 bar.background.surface.commit(); 60 } 61 } 62 } 63 } 64 65 pub fn print(self: *Backlight, writer: anytype) !void { 66 try updateDevices(state.gpa, self.context, &self.devices); 67 const device = self.devices.items[0]; 68 var percent = @as(f64, @floatFromInt(device.value)) * 100.0; 69 percent /= @as(f64, @floatFromInt(device.max)); 70 const value = @as(u8, @intFromFloat(@round(percent))); 71 72 try writer.print(" {d}%", .{value}); 73 } 74 75 fn updateDevices( 76 gpa: std.mem.Allocator, 77 context: *udev.Udev, 78 devices: *DeviceList, 79 ) !void { 80 const enumerate = try udev.Enumerate.new(context); 81 try enumerate.addMatchSubsystem("backlight"); 82 try enumerate.scanDevices(); 83 const entries = enumerate.getListEntry(); 84 85 var maybe_entry = entries; 86 while (maybe_entry) |entry| : (maybe_entry = entry.getNext()) { 87 const path = entry.getName(); 88 const device = try udev.Device.newFromSyspath(context, path); 89 try updateOrAppend(gpa, devices, device); 90 } 91 } 92 93 fn updateOrAppend( 94 gpa: std.mem.Allocator, 95 devices: *DeviceList, 96 dev: *udev.Device, 97 ) !void { 98 const value = try dev.getSysattrValue("actual_brightness"); 99 const max = try dev.getSysattrValue("max_brightness"); 100 const name = try dev.getSysname(); 101 102 const device = blk: { 103 for (devices.items) |*device| { 104 if (std.mem.eql(u8, device.name, name)) { 105 break :blk device; 106 } 107 } else { 108 const device = try devices.addOne(); 109 device.name = try gpa.dupe(u8, name); 110 break :blk device; 111 } 112 }; 113 device.value = try std.fmt.parseInt(u64, value, 10); 114 device.max = try std.fmt.parseInt(u64, max, 10); 115 }