Debian Codegraph @

vicious/widgets/hwmontemp_linux.lua

raw symbols SCIP
1 -- widget type providing name-indexed temperatures from /sys/class/hwmon
2 -- Copyright (C) 2019, 2020 Alexander Koch <[email protected]>
3 -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 -- environment
21 local type = type
22 local tonumber = tonumber
23 local io = { open = io.open }
24
25 local helpers = require"vicious.helpers"
26 local spawn = require"vicious.spawn"
27
28 local pathcache = {}
29
30 local function read_sensor(path, callback)
31 local f = io.open(path, "r")
32 callback{ tonumber(f:read"*line") / 1000 }
33 f:close()
34 end
35
36 -- hwmontemp: provides name-indexed temps from /sys/class/hwmon
37 -- vicious.widgets.hwmontemp
38 return helpers.setasyncall{
39 async = function (format, warg, callback)
40 if type(warg) ~= "table" or type(warg[1]) ~= "string" then
41 return callback{}
42 end
43
44 local input = warg[2]
45 if type(input) == "number" then
46 input = ("temp%d_input"):format(input)
47 else
48 input = "temp1_input"
49 end
50
51 local sensor = warg[1]
52 if pathcache[sensor] then
53 read_sensor(pathcache[sensor] .. input, callback)
54 else
55 spawn.easy_async_with_shell(
56 "grep " .. sensor .. " -wl /sys/class/hwmon/*/name",
57 function (stdout, stderr, exitreason, exitcode)
58 if exitreason == "exit" and exitcode == 0 then
59 pathcache[sensor] = stdout:gsub("name%s+", "")
60 read_sensor(pathcache[sensor] .. input, callback)
61 else
62 callback{}
63 end
64 end)
65 end
66 end }
67 -- vim: ts=4:sw=4:expandtab