-- This file is part of Reno desktop. -- -- Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. -- -- Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License along with Reno desktop. If not, see . -- Pure lua module for controlling, reading and enumerating devices through sysfs -- Because in Linux we trust. local syscontrol = { power_supply = {}, backlight = {}, hwmon = {} } syscontrol.backlight.enumerate = function() local lshandler = io.popen("ls -1 /sys/class/backlight","r") if lshandler then local out = lshandler:read("*a") lshandler:close() local devices = {} out:gsub("(%S*)\n",function(a) table.insert(devices,a) end) return devices else return nil, "Unable to ls /sys/class/backlight" end end syscontrol.backlight.read_attribs = function(dev_name) local fhandler,err = io.open("/sys/class/backlight/"..dev_name.."/brightness","r") if fhandler then local bhandler = io.open("/sys/class/backlight/"..dev_name.."/max_brightness","r") local device = { name = dev_name, full_path = "/sys/class/backlight/"..dev_name, brightness = tonumber(fhandler:read("*a")), max_brightness = tonumber(bhandler:read("*a")), writable = false } fhandler:close() local wfhandler = io.open("/sys/class/backlight/"..dev_name.."/brightness","w") if wfhandler then wfhandler:close() device.writable = true end return device else return nil, err end end syscontrol.backlight.set_brightness = function(device,brightness) if device.writable then brightness = tonumber(brightness) if (brightness < 0) or (brightness > device.max_brightness) then return nil, "Brightness exceeds given limits" end local fhandler,err = io.open(device.full_path.."/brightness","w") if fhandler then fhandler:write(tostring(brightness)) fhandler:close() return true else return nil, err end else return nil, "Device is not writable" end end syscontrol.power_supply.enumerate = function() local lshandler = io.popen("ls -1 /sys/class/power_supply","r") if lshandler then local out = lshandler:read("*a") lshandler:close() local devices = {} out:gsub("(%S*)\n",function(a) table.insert(devices,a) end) return devices else return nil, "Unable to ls /sys/class/power_supply" end end syscontrol.power_supply.read_attribs = function(dev_name) local fhandler,err = io.open("/sys/class/power_supply/"..dev_name.."/type","r") if fhandler then local dtype = fhandler:read("*a"):match("%S*") fhandler:close() local full_path = "/sys/class/power_supply/"..dev_name local device = { type = dtype, full_path = full_path, name = dev_name } if dtype == "Mains" then local online_f = io.open(full_path.."/online","r") device.online = (online_f:read("*a"):match("%S*") == "1") online_f:close() elseif dtype == "Battery" then local capacity_f = io.open(full_path.."/capacity","r") local model_f = io.open(full_path.."/model_name","r") local energy_full_design_f = io.open(full_path.."/energy_full_design","r") local energy_full_f = io.open(full_path.."/energy_full","r") local charging_f = io.open(full_path.."/status","r") device.model = model_f:read("*a"):match("[^\n]*") device.energy_full_design = tonumber(energy_full_design_f:read("*a")) device.energy_full = tonumber(energy_full_f:read("*a")) device.quality = (device.energy_full/device.energy_full_design)*100 device.capacity = tonumber(capacity_f:read("*a")) device.charging = (charging_f:read("*a"):match("%S*") == "Charging") capacity_f:close() model_f:close() energy_full_design_f:close() energy_full_f:close() end return device else return nil, err end end return syscontrol