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.

126 lines
4.8 KiB

2 years ago
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. pulse = {}
  15. }
  16. syscontrol.backlight.enumerate = function()
  17. local lshandler = io.popen("ls -1 /sys/class/backlight","r")
  18. if lshandler then
  19. local out = lshandler:read("*a")
  20. lshandler:close()
  21. local devices = {}
  22. out:gsub("(%S*)\n",function(a)
  23. table.insert(devices,a)
  24. end)
  25. return devices
  26. else
  27. return nil, "Unable to ls /sys/class/backlight"
  28. end
  29. end
  30. syscontrol.backlight.read_attribs = function(dev_name)
  31. local fhandler,err = io.open("/sys/class/backlight/"..dev_name.."/brightness","r")
  32. if fhandler then
  33. local bhandler = io.open("/sys/class/backlight/"..dev_name.."/max_brightness","r")
  34. local device = {
  35. name = dev_name,
  36. full_path = "/sys/class/backlight/"..dev_name,
  37. brightness = tonumber(fhandler:read("*a")),
  38. max_brightness = tonumber(bhandler:read("*a")),
  39. writable = false
  40. }
  41. fhandler:close()
  42. bhandler:close()
  43. local wfhandler = io.open("/sys/class/backlight/"..dev_name.."/brightness","w")
  44. if wfhandler then
  45. wfhandler:close()
  46. device.writable = true
  47. end
  48. return device
  49. else
  50. return nil, err
  51. end
  52. end
  53. syscontrol.backlight.set_brightness = function(device,brightness)
  54. if device.writable then
  55. brightness = tonumber(brightness)
  56. if (brightness < 0) or (brightness > device.max_brightness) then
  57. return nil, "Brightness exceeds given limits"
  58. end
  59. local fhandler,err = io.open(device.full_path.."/brightness","w")
  60. if fhandler then
  61. fhandler:write(tostring(brightness))
  62. fhandler:close()
  63. return true
  64. else
  65. return nil, err
  66. end
  67. else
  68. return nil, "Device is not writable"
  69. end
  70. end
  71. syscontrol.power_supply.enumerate = function()
  72. local lshandler = io.popen("ls -1 /sys/class/power_supply","r")
  73. if lshandler then
  74. local out = lshandler:read("*a")
  75. lshandler:close()
  76. local devices = {}
  77. out:gsub("(%S*)\n",function(a)
  78. table.insert(devices,a)
  79. end)
  80. return devices
  81. else
  82. return nil, "Unable to ls /sys/class/power_supply"
  83. end
  84. end
  85. syscontrol.power_supply.read_attribs = function(dev_name)
  86. local fhandler,err = io.open("/sys/class/power_supply/"..dev_name.."/type","r")
  87. if fhandler then
  88. local uevent_f = io.open("/sys/class/power_supply/"..dev_name.."/uevent","r")
  89. if not uevent_f then
  90. return nil, "Unable to open uevent file"
  91. end
  92. local uevent_data = {}
  93. local device = {
  94. type = fhandler:read("*a"):match("%S*"),
  95. full_path = "/sys/class/power_supply/"..dev_name,
  96. name = dev_name
  97. }
  98. fhandler:close()
  99. uevent_f:read("*a"):gsub("[^\n]+",function(line)
  100. local key,value = line:match("^([^=]+)=([^=]*)")
  101. if value:match("^%d+$") then
  102. value = tonumber(value)
  103. end
  104. uevent_data[key] = value
  105. end)
  106. uevent_f:close()
  107. if device.type == "Battery" then
  108. device.model = uevent_data.POWER_SUPPLY_MODEL_NAME or "N/A"
  109. device.energy_full_design = uevent_data.POWER_SUPPLY_ENERGY_FULL_DESIGN
  110. device.energy_full = uevent_data.POWER_SUPPLY_ENERGY_FULL
  111. if not device.energy_full_design then
  112. device.energy_full_design = uevent_data.POWER_SUPPLY_CHARGE_FULL_DESIGN or -1
  113. device.energy_full = uevent_data.POWER_SUPPLY_CHARGE_FULL or -1
  114. end
  115. device.quality = (device.energy_full/device.energy_full_design)*100
  116. device.capacity = uevent_data.POWER_SUPPLY_CAPACITY or 0
  117. device.charging = (uevent_data.POWER_SUPPLY_STATUS == "Charging")
  118. elseif device.type == "Mains" then
  119. device.online = (uevent_data.POWER_SUPPLY_ONLINE == 1)
  120. end
  121. return device
  122. else
  123. return nil, err
  124. end
  125. end
  126. return syscontrol