diff --git a/Changes.md b/Changes.md index 22fb202..85ba41d 100644 --- a/Changes.md +++ b/Changes.md @@ -18,7 +18,7 @@ Added: Fixed: - Deprecate the use of `io.popen` in following widgets: - * wifi_linux, wifiiw_linux + * wifi_linux, wifiiw_linux, hwmontemp_linux * bat_freebsd, mem_freebsd, net_freebsd * volume, gmail, mdir, mpd - [mpd] Lua 5.3 compatibility (for real this time); also correct a typo diff --git a/widgets/hwmontemp_linux.lua b/widgets/hwmontemp_linux.lua index d995e72..9182932 100644 --- a/widgets/hwmontemp_linux.lua +++ b/widgets/hwmontemp_linux.lua @@ -4,57 +4,37 @@ ---------------------------------------------------------------- -- environment -local io = { popen = io.popen, open = io.open } -local assert = assert -local setmetatable = setmetatable +local type = type +local tonumber = tonumber +local io = { open = io.open } --- sysfs prefix for hwmon devices -local sys_hwmon = "/sys/class/hwmon/" --- cache table for hwmon device names -local paths = {} - --- transparently caching hwmon device name lookup -function name_to_path(name) - if paths[name] then return paths[name] end - - for sensor in io.popen("ls -1 " .. sys_hwmon):lines() do - local path = sys_hwmon .. sensor - local f = assert(io.open(path .. "/name", "r")) - local sname = f:read("*line") - f:close() - if sname == name then - paths[name] = path - return path - end - end - - return nil -end +local helpers = require"vicious.helpers" +local spawn = require"vicious.spawn" -- hwmontemp: provides name-indexed temps from /sys/class/hwmon -- vicious.widgets.hwmontemp -local hwmontemp_linux = {} - -function worker(format, warg) - assert(type(warg) == "table", "invalid hwmontemp argument: must be a table") - name = warg[1] - - if not warg[2] then - input = 1 - else - input = warg[2] - end - - local sensor = name_to_path(name) - if not sensor then return { "N/A" } end - - local f = assert(io.open(("%s/temp%d_input"):format(sensor, input), "r")) - local temp = f:read("*line") - f:close() - - return { temp / 1000 } -end - -return setmetatable(hwmontemp_linux, { __call = function(_, ...) return worker(...) end }) +return helpers.setasyncall{ + async = function (format, warg, callback) + if type(warg) ~= "table" or type(warg[1]) ~= "string" then + return callback{} + end + local input = warg[2] + if type(input) == "number" then + input = ("temp%d_input"):format(input) + else + input = "temp1_input" + end + spawn.easy_async_with_shell( + "grep " .. warg[1] .. " -wl /sys/class/hwmon/*/name", + function (stdout, stderr, exitreason, exitcode) + if exitreason == "exit" and exitcode == 0 then + local f = io.open(stdout:gsub("name%s+", input), "r") + callback{ tonumber(f:read"*line") / 1000 } + f:close() + else + callback{} + end + end) + end } -- vim: ts=4:sw=4:expandtab