tony / oxwm

Branch: 86288f6
Switch branches/tags
master
tonybanters added comment for context on execvp, with relevant codeberg issue link associated. fixed inconsistency on movestack, and utilized .real vs .awake for clock 4d61920 2026-05-16
117 lines · 4.2 KB Raw Blame History
1 const std = @import("std");
2 const format_util = @import("format.zig");
3
4 pub const CpuTemp = struct {
5 format: []const u8,
6 device: []const u8,
7 interval_secs: u64,
8 color: c_ulong,
9 cached_path: [128]u8,
10 cached_path_len: usize,
11 detection_attempted: bool,
12
13 pub fn init(
14 format: []const u8,
15 device: []const u8,
16 interval_secs: u64,
17 color: c_ulong,
18 ) CpuTemp {
19 return .{
20 .format = format,
21 .device = device,
22 .interval_secs = interval_secs,
23 .color = color,
24 .cached_path = undefined,
25 .cached_path_len = 0,
26 .detection_attempted = false,
27 };
28 }
29
30 fn detectPath(self: *CpuTemp, io: std.Io) void {
31 if (self.device.len > 0 and self.device[0] == '/') {
32 if (self.device.len <= self.cached_path.len) {
33 @memcpy(self.cached_path[0..self.device.len], self.device);
34 self.cached_path_len = self.device.len;
35 }
36 return;
37 }
38
39 if (self.device.len > 0) {
40 if (self.tryPath(io, "/sys/class/thermal/{s}/temp", self.device)) return;
41 if (self.tryPath(io, "/sys/class/hwmon/{s}/temp1_input", self.device)) return;
42 }
43
44 if (self.findHwmonCpu(io)) return;
45
46 if (self.tryPath(io, "/sys/class/thermal/{s}/temp", "thermal_zone0")) return;
47 }
48
49 fn tryPath(self: *CpuTemp, io: std.Io, comptime fmt: []const u8, device: []const u8) bool {
50 const path = std.fmt.bufPrint(&self.cached_path, fmt, .{device}) catch return false;
51 const file = std.Io.Dir.openFileAbsolute(io, path, .{}) catch return false;
52 file.close(io);
53 self.cached_path_len = path.len;
54 return true;
55 }
56
57 fn findHwmonCpu(self: *CpuTemp, io: std.Io) bool {
58 var dir = std.Io.Dir.openDirAbsolute(io, "/sys/class/hwmon", .{ .iterate = true }) catch return false;
59 defer dir.close(io);
60
61 var iter = dir.iterate();
62 while (iter.next(io) catch null) |entry| {
63 if (entry.kind != .directory and entry.kind != .sym_link) continue;
64
65 var name_path: [128]u8 = undefined;
66 const name_path_str = std.fmt.bufPrint(&name_path, "/sys/class/hwmon/{s}/name", .{entry.name}) catch continue;
67
68 const name_file = std.Io.Dir.openFileAbsolute(io, name_path_str, .{}) catch continue;
69 defer name_file.close(io);
70
71 var name_buf: [32]u8 = undefined;
72 const name_len = name_file.readStreaming(io, &.{&name_buf}) catch continue;
73 const name = std.mem.trim(u8, name_buf[0..name_len], " \n\r\t");
74
75 if (std.mem.eql(u8, name, "coretemp") or std.mem.eql(u8, name, "k10temp")) {
76 const temp_path = std.fmt.bufPrint(&self.cached_path, "/sys/class/hwmon/{s}/temp1_input", .{entry.name}) catch continue;
77 const temp_file = std.Io.Dir.openFileAbsolute(io, temp_path, .{}) catch continue;
78 temp_file.close(io);
79 self.cached_path_len = temp_path.len;
80 return true;
81 }
82 }
83 return false;
84 }
85
86 pub fn content(self: *CpuTemp, io: std.Io, buffer: []u8) []const u8 {
87 if (!self.detection_attempted) {
88 self.detectPath(io);
89 self.detection_attempted = true;
90 }
91 if (self.cached_path_len == 0) return buffer[0..0];
92
93 const path = self.cached_path[0..self.cached_path_len];
94 const file = std.Io.Dir.openFileAbsolute(io, path, .{}) catch return buffer[0..0];
95 defer file.close(io);
96
97 var temp_buf: [16]u8 = undefined;
98 const len = file.readStreaming(io, &.{&temp_buf}) catch return buffer[0..0];
99 const temp_str = std.mem.trim(u8, temp_buf[0..len], " \n\r\t");
100
101 const millidegrees = std.fmt.parseInt(i32, temp_str, 10) catch return buffer[0..0];
102 const degrees = @divTrunc(millidegrees, 1000);
103
104 var deg_buf: [8]u8 = undefined;
105 const deg_str = std.fmt.bufPrint(&deg_buf, "{d}", .{degrees}) catch return buffer[0..0];
106
107 return format_util.substitute(self.format, deg_str, buffer);
108 }
109
110 pub fn interval(self: *CpuTemp) u64 {
111 return self.interval_secs;
112 }
113
114 pub fn getColor(self: *CpuTemp) c_ulong {
115 return self.color;
116 }
117 };