| 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 | |
| 12 | pub fn init( |
| 13 | format: []const u8, |
| 14 | device: []const u8, |
| 15 | interval_secs: u64, |
| 16 | color: c_ulong, |
| 17 | ) CpuTemp { |
| 18 | return .{ |
| 19 | .format = format, |
| 20 | .device = device, |
| 21 | .interval_secs = interval_secs, |
| 22 | .color = color, |
| 23 | .cached_path = undefined, |
| 24 | .cached_path_len = 0, |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | fn detectPath(self: *CpuTemp, io: std.Io) void { |
| 29 | if (self.device.len > 0 and self.device[0] == '/') { |
| 30 | if (self.device.len <= self.cached_path.len) { |
| 31 | @memcpy(self.cached_path[0..self.device.len], self.device); |
| 32 | self.cached_path_len = self.device.len; |
| 33 | } |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | if (self.device.len > 0) { |
| 38 | if (self.tryPath(io, "/sys/class/thermal/{s}/temp", self.device)) return; |
| 39 | if (self.tryPath(io, "/sys/class/hwmon/{s}/temp1_input", self.device)) return; |
| 40 | } |
| 41 | |
| 42 | if (self.findHwmonCpu(io)) return; |
| 43 | |
| 44 | if (self.tryPath(io, "/sys/class/thermal/{s}/temp", "thermal_zone0")) return; |
| 45 | } |
| 46 | |
| 47 | fn tryPath(self: *CpuTemp, io: std.Io, comptime fmt: []const u8, device: []const u8) bool { |
| 48 | const path = std.fmt.bufPrint(&self.cached_path, fmt, .{device}) catch return false; |
| 49 | const file = std.Io.Dir.openFileAbsolute(io, path, .{}) catch return false; |
| 50 | file.close(io); |
| 51 | self.cached_path_len = path.len; |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | fn findHwmonCpu(self: *CpuTemp, io: std.Io) bool { |
| 56 | var dir = std.Io.Dir.openDirAbsolute(io, "/sys/class/hwmon", .{ .iterate = true }) catch return false; |
| 57 | defer dir.close(io); |
| 58 | |
| 59 | var iter = dir.iterate(); |
| 60 | while (iter.next(io) catch null) |entry| { |
| 61 | if (entry.kind != .directory and entry.kind != .sym_link) continue; |
| 62 | |
| 63 | var name_path: [128]u8 = undefined; |
| 64 | const name_path_str = std.fmt.bufPrint(&name_path, "/sys/class/hwmon/{s}/name", .{entry.name}) catch continue; |
| 65 | |
| 66 | const name_file = std.Io.Dir.openFileAbsolute(io, name_path_str, .{}) catch continue; |
| 67 | defer name_file.close(io); |
| 68 | |
| 69 | var name_buf: [32]u8 = undefined; |
| 70 | const name_len = name_file.readStreaming(io, &.{&name_buf}) catch continue; |
| 71 | const name = std.mem.trim(u8, name_buf[0..name_len], " \n\r\t"); |
| 72 | |
| 73 | if (std.mem.eql(u8, name, "coretemp") or std.mem.eql(u8, name, "k10temp")) { |
| 74 | const temp_path = std.fmt.bufPrint(&self.cached_path, "/sys/class/hwmon/{s}/temp1_input", .{entry.name}) catch continue; |
| 75 | const temp_file = std.Io.Dir.openFileAbsolute(io, temp_path, .{}) catch continue; |
| 76 | temp_file.close(io); |
| 77 | self.cached_path_len = temp_path.len; |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | pub fn content(self: *CpuTemp, io: std.Io, buffer: []u8) []const u8 { |
| 85 | if (self.cached_path_len == 0) { |
| 86 | self.detectPath(io); |
| 87 | if (self.cached_path_len == 0) return buffer[0..0]; |
| 88 | } |
| 89 | |
| 90 | const path = self.cached_path[0..self.cached_path_len]; |
| 91 | const file = std.Io.Dir.openFileAbsolute(io, path, .{}) catch return buffer[0..0]; |
| 92 | defer file.close(io); |
| 93 | |
| 94 | var temp_buf: [16]u8 = undefined; |
| 95 | const len = file.readStreaming(io, &.{&temp_buf}) catch return buffer[0..0]; |
| 96 | const temp_str = std.mem.trim(u8, temp_buf[0..len], " \n\r\t"); |
| 97 | |
| 98 | const millidegrees = std.fmt.parseInt(i32, temp_str, 10) catch return buffer[0..0]; |
| 99 | const degrees = @divTrunc(millidegrees, 1000); |
| 100 | |
| 101 | var deg_buf: [8]u8 = undefined; |
| 102 | const deg_str = std.fmt.bufPrint(°_buf, "{d}", .{degrees}) catch return buffer[0..0]; |
| 103 | |
| 104 | return format_util.substitute(self.format, deg_str, buffer); |
| 105 | } |
| 106 | |
| 107 | pub fn interval(self: *CpuTemp) u64 { |
| 108 | return self.interval_secs; |
| 109 | } |
| 110 | |
| 111 | pub fn getColor(self: *CpuTemp) c_ulong { |
| 112 | return self.color; |
| 113 | } |
| 114 | }; |