Reno is the second iteration of the AWMTK-powered AwesomeWM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.7 KiB

2 years ago
2 years ago
  1. -- This file is part of Reno desktop.
  2. --
  3. -- 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.
  4. --
  5. -- 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.
  6. --
  7. -- You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>.
  8. -- Pure lua module for controlling, reading and enumerating devices through sysfs
  9. -- Because in Linux we trust.
  10. local syscontrol = {
  11. power_supply = {},
  12. backlight = {},
  13. hwmon = {}
  14. }
  15. syscontrol.backlight.enumerate = function()
  16. local lshandler = io.popen("ls -1 /sys/class/backlight","r")
  17. if lshandler then
  18. local out = lshandler:read("*a")
  19. lshandler:close()
  20. local devices = {}
  21. out:gsub("(%S*)\n",function(a)
  22. table.insert(devices,a)
  23. end)
  24. return devices
  25. else
  26. return nil, "Unable to ls /sys/class/backlight"
  27. end
  28. end
  29. syscontrol.backlight.read_attribs = function(dev_name)
  30. local fhandler,err = io.open("/sys/class/backlight/"..dev_name.."/brightness","r")
  31. if fhandler then
  32. local bhandler = io.open("/sys/class/backlight/"..dev_name.."/max_brightness","r")
  33. local device = {
  34. name = dev_name,
  35. full_path = "/sys/class/backlight/"..dev_name,
  36. brightness = tonumber(fhandler:read("*a")),
  37. max_brightness = tonumber(bhandler:read("*a")),
  38. writable = false
  39. }
  40. fhandler:close()
  41. local wfhandler = io.open("/sys/class/backlight/"..dev_name.."/brightness","w")
  42. if wfhandler then
  43. wfhandler:close()
  44. device.writable = true
  45. end
  46. return device
  47. else
  48. return nil, err
  49. end
  50. end
  51. syscontrol.backlight.set_brightness = function(device,brightness)
  52. if device.writable then
  53. brightness = tonumber(brightness)
  54. if (brightness < 0) or (brightness > device.max_brightness) then
  55. return nil, "Brightness exceeds given limits"
  56. end
  57. local fhandler,err = io.open(device.full_path.."/brightness","w")
  58. if fhandler then
  59. fhandler:write(tostring(brightness))
  60. fhandler:close()
  61. return true
  62. else
  63. return nil, err
  64. end
  65. else
  66. return nil, "Device is not writable"
  67. end
  68. end
  69. syscontrol.power_supply.enumerate = function()
  70. local lshandler = io.popen("ls -1 /sys/class/power_supply","r")
  71. if lshandler then
  72. local out = lshandler:read("*a")
  73. lshandler:close()
  74. local devices = {}
  75. out:gsub("(%S*)\n",function(a)
  76. table.insert(devices,a)
  77. end)
  78. return devices
  79. else
  80. return nil, "Unable to ls /sys/class/power_supply"
  81. end
  82. end
  83. syscontrol.power_supply.read_attribs = function(dev_name)
  84. local fhandler,err = io.open("/sys/class/power_supply/"..dev_name.."/type","r")
  85. if fhandler then
  86. local dtype = fhandler:read("*a"):match("%S*")
  87. fhandler:close()
  88. local full_path = "/sys/class/power_supply/"..dev_name
  89. local device = {
  90. type = dtype,
  91. full_path = full_path,
  92. name = dev_name
  93. }
  94. if dtype == "Mains" then
  95. local online_f = io.open(full_path.."/online","r")
  96. device.online = (online_f:read("*a"):match("%S*") == "1")
  97. online_f:close()
  98. elseif dtype == "Battery" then
  99. local capacity_f = io.open(full_path.."/capacity","r")
  100. local model_f = io.open(full_path.."/model_name","r")
  101. local energy_full_design_f = io.open(full_path.."/energy_full_design","r")
  102. local energy_full_f = io.open(full_path.."/energy_full","r")
  103. local charging_f = io.open(full_path.."/status","r")
  104. device.model = model_f:read("*a"):match("[^\n]*")
  105. device.energy_full_design = tonumber(energy_full_design_f:read("*a"))
  106. device.energy_full = tonumber(energy_full_f:read("*a"))
  107. device.quality = (device.energy_full/device.energy_full_design)*100
  108. device.capacity = tonumber(capacity_f:read("*a"))
  109. device.charging = (charging_f:read("*a"):match("%S*") == "Charging")
  110. capacity_f:close()
  111. model_f:close()
  112. energy_full_design_f:close()
  113. energy_full_f:close()
  114. end
  115. return device
  116. else
  117. return nil, err
  118. end
  119. end
  120. return syscontrol