summaryrefslogtreecommitdiff
path: root/widgets/temperature.lua
blob: cbefbd2bb7dd3c6a5d273ec90603394f0d7cf7d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
local wibox = require("wibox")
local widgets_util = require("util.widgets")
local gears = require("gears")
local cpuinfo = require("lib.cpuinfo")
local naughty = require("naughty")

local temperature = {}

function temperature:set_file(file)
	self._private.file = file
	self._timer:emit_signal("timeout")
end

function linux_auto_get_file()
	-- determine hwmon name from cpu vendor
	local vendor_hwmon_name = nil
	if cpuinfo.vendor == "GenuineIntel" then
		vendor_hwmon_name = "coretemp"
	elseif cpuinfo.vendor == "AuthenticAMD" then
		vendor_hwmon_name = "k10temp"
	else
		return nil
	end

	-- find hwmon
	local hwmons = io.popen([[awk '{ sub("/name$", "", FILENAME); print FILENAME, $0 }' /sys/class/hwmon/hwmon*/name]])
	for hwmon, hwmon_name in hwmons:read("*a"):gmatch("(.-)%s(.-)\n") do
		if hwmon_name == vendor_hwmon_name then
			return hwmon .. "/temp1_input" -- TODO dont hardcode temp1_input
		end
	end
end

function linux_update(widget)
	local tempf = io.open(widget._private.file)
	if tempf then
		local temp = tempf:read() / 1000
		tempf:close()
		widget:set_text(string.format(widget._private.format, temp))
	else
		widget:set_text("err")
	end
end

-- return correct widget for os
if osname == "Linux" then
	return function(file)
		local w = wibox.widget.textbox()
		gears.table.crush(w, temperature, true)
		w._private.file = file or linux_auto_get_file()
		w._private.format = "%d°"
		widgets_util.add_timer(w, linux_update, 5)
		--return widgets.watchfn(linux.temperature, 5)
		return w
	end
end

return wibox.widget.textbox("unsupported os")