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.

310 lines
13 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
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. -- Generic battery widget with support for multiple power sources, backlights and backlight control
  9. local awful = require("awful")
  10. local beautiful = require("beautiful")
  11. local gears = require("gears")
  12. local wibox = require("wibox")
  13. local awmtk2 = require("awmtk2")
  14. local syscontrol = require("syscontrol")
  15. local ask = require("asckey")
  16. local function get_virtual_icon(data)
  17. -- Get an icon from a cumulative total of battery percentages and current charging state
  18. local count = 0
  19. local cumulative = 0
  20. local name = "battery-"
  21. for _,v in pairs(data) do
  22. if type(v) == "number" then
  23. cumulative = cumulative + v
  24. count = count + 1
  25. end
  26. end
  27. local percentage = math.floor((cumulative/(count*100))*100)
  28. if percentage < 15 then
  29. name = name.."caution-"
  30. elseif percentage < 30 then
  31. name = name.."low-"
  32. elseif percentage < 60 then
  33. name = name.."good-"
  34. else
  35. name = name.."full-"
  36. end
  37. if data["charge"] then
  38. name = name.."charging-"
  39. end
  40. return beautiful[name.."symbolic"],percentage
  41. end
  42. return function(args)
  43. local style = awmtk2.create_style("battery",
  44. awmtk2.generic.popup,args.style)
  45. local templates = awmtk2.create_template_lib("battery",awmtk2.templates,args.templates)
  46. local t = awmtk2.build_templates(templates,style,args.vertical)
  47. -- set up popup layout
  48. local layout = wibox.widget({
  49. layout = wibox.layout.fixed.vertical,
  50. spacing = style.base.spacing
  51. })
  52. -- create popup
  53. local popup = awful.popup(t.popup(layout))
  54. local battery_widget
  55. do -- create battery widget
  56. local style = awmtk2.create_style("battery",
  57. awmtk2.generic.status_widget,args.style)
  58. local templates = awmtk2.create_template_lib("battery",awmtk2.templates,args.templates)
  59. local t = awmtk2.build_templates(templates,style,args.vertical)
  60. battery_widget = wibox.widget(t.button({
  61. t.icon({
  62. image = beautiful["battery-missing-symbolic"],
  63. resize = true,
  64. id = "virtual_id"
  65. }),
  66. (args.percentage and {
  67. markup = "0%",
  68. id = "percentage_id",
  69. widget = wibox.widget.textbox
  70. }),
  71. layout = wibox.layout.fixed.horizontal,
  72. spacing = style.base.spacing
  73. }))
  74. -- make it possible to press the button and make it toggle the popup
  75. battery_widget:connect_signal("button::press",style.button.onpress)
  76. battery_widget:connect_signal("button::release",style.button.onrelease)
  77. battery_widget:connect_signal("button::press",function(_,_,_,button)
  78. if button == 1 then
  79. popup.visible = (not popup.visible)
  80. if popup.visible then
  81. popup:move_next_to(mouse.current_widget_geometry)
  82. end
  83. end
  84. end)
  85. end
  86. -- map widgets to their names to make it easier to update separate components
  87. local widget_map = {}
  88. -- also map current charge state of every device to get the icon for the tray
  89. local percentage_map = {}
  90. -- {{{ Power supply devices
  91. local power_devices = syscontrol.power_supply.enumerate()
  92. for _,device in pairs(power_devices) do
  93. local data = syscontrol.power_supply.read_attribs(device)
  94. if data.type == "Battery" then
  95. widget_map[data.name] = wibox.widget(t.container({
  96. t.article({
  97. icon = get_virtual_icon({
  98. data.capacity,
  99. charge = data.charging
  100. }),
  101. icon_id = "battery_icon",
  102. title = "Battery ("..data.model..")",
  103. }),
  104. t.textbox({
  105. markup = ("Capacity: %d%%"):format(data.capacity),
  106. id = "capacity_id"
  107. }),
  108. t.textbox({
  109. markup = ("Quality: %.4f%%"):format(data.quality),
  110. id = "quality_id",
  111. }),
  112. layout = wibox.layout.fixed.vertical
  113. },{
  114. bg = style.container.bg_highlight,
  115. bgimage = style.container.bgimage_highlight
  116. }))
  117. layout:add(widget_map[data.name])
  118. percentage_map[data.name] = data.capacity
  119. elseif data.type == "Mains" then
  120. widget_map[data.name] = wibox.widget(t.container({
  121. t.article({
  122. icon = beautiful["ac-adapter-symbolic"],
  123. title = "Power supply",
  124. }),
  125. t.textbox({
  126. markup = "Powered: "..tostring(data.online),
  127. id = "online_id",
  128. }),
  129. layout = wibox.layout.fixed.vertical
  130. },{
  131. bg = style.container.bg_highlight,
  132. bgimage = style.container.bgimage_highlight
  133. }))
  134. layout:add(widget_map[data.name])
  135. percentage_map["charge"] = data.online
  136. end
  137. end
  138. -- "Virtual" here means a battery that displays the state of a cumulative total of all attached batteries (as if there are any devices that even have more than 1 battery)
  139. local function update_virtual_battery()
  140. local icon = battery_widget:get_children_by_id("virtual_id")[1]
  141. local percentage = battery_widget:get_children_by_id("percentage_id")[1]
  142. local capacity
  143. icon.image,capacity = get_virtual_icon(percentage_map)
  144. if percentage then
  145. percentage:set_markup(tostring(capacity).."%")
  146. end
  147. end
  148. update_virtual_battery()
  149. -- Update loop
  150. local power_update = gears.timer({
  151. timeout = args.power_polling or 2,
  152. autostart = true,
  153. callback = function()
  154. for _,v in pairs(power_devices) do
  155. local data,err = syscontrol.power_supply.read_attribs(v)
  156. if data and data.type == "Mains" then
  157. local w = widget_map[data.name]
  158. local online = w:get_children_by_id("online_id")[1]
  159. online:set_markup("Powered: "..tostring(data.online))
  160. percentage_map["charge"] = data.online
  161. elseif data and data.type == "Battery" then
  162. local w = widget_map[data.name]
  163. local icon = w:get_children_by_id("battery_icon")[1]
  164. local capacity = w:get_children_by_id("capacity_id")[1]
  165. local quality = w:get_children_by_id("quality_id")[1]
  166. icon.image = get_virtual_icon({
  167. data.capacity,
  168. charge = data.charging
  169. })
  170. capacity:set_markup(("Capacity: %d%%"):format(data.capacity))
  171. quality:set_markup(("Quality: %.4f%%"):format(data.quality))
  172. percentage_map[data.name] = data.capacity
  173. else
  174. print(err)
  175. end
  176. update_virtual_battery()
  177. end
  178. end
  179. })
  180. -- }}}
  181. -- {{{ Backlight
  182. local backlight_devices = syscontrol.backlight.enumerate()
  183. local default_backlight_device
  184. for _,v in pairs(backlight_devices) do
  185. local data = syscontrol.backlight.read_attribs(v)
  186. if data then
  187. widget_map[data.name] = wibox.widget(t.container({
  188. {
  189. t.article({
  190. icon = beautiful["backlight-symbolic"],
  191. title = "Backlight",
  192. }),
  193. (data.writable and t.center(
  194. t.checkbox({
  195. checked = false,
  196. id = "checkbox",
  197. forced_height = style.article.icon_size,
  198. forced_width = style.article.icon_size
  199. }),
  200. {
  201. width = style.checkbox.width,
  202. height = style.checkbox.height
  203. })
  204. ),
  205. layout = wibox.layout.fixed.horizontal,
  206. spacing = style.base.spacing
  207. },
  208. t.textbox({
  209. markup = "Brightness: "..tostring(data.brightness),
  210. id = "brightness_id"
  211. }),
  212. t.textbox({
  213. markup = "Max brightness: "..tostring(data.max_brightness),
  214. id = "max_brightness"
  215. }),
  216. (data.writable and t.slider({
  217. minimum = data.max_brightness*0.05,
  218. maximum = data.max_brightness,
  219. value = tonumber(data.brightness),
  220. id = "slider"
  221. })),
  222. layout = wibox.layout.fixed.vertical
  223. },{
  224. bg = style.container.bg_highlight,
  225. bgimage = style.container.bgimage_highlight
  226. }))
  227. if data.writable then
  228. local w = widget_map[data.name]
  229. local slider = w:get_children_by_id("slider")[1]
  230. slider:connect_signal("widget::redraw_needed",function(self)
  231. local value = self.value
  232. syscontrol.backlight.set_brightness(data,math.floor(value))
  233. end)
  234. slider.value = tonumber(data.brightness)
  235. local checkbox = w:get_children_by_id("checkbox")[1]
  236. checkbox:connect_signal("button::press",function()
  237. if default_backlight_device then
  238. local check2 = widget_map[default_backlight_device.name]
  239. :get_children_by_id("checkbox")[1]
  240. check2.checked = true
  241. end
  242. default_backlight_device = data
  243. end)
  244. end
  245. layout:add(widget_map[data.name])
  246. end
  247. end
  248. -- Update loop
  249. local backlight_update = gears.timer({
  250. timeout = args.backlight_polling or 2,
  251. autostart = true,
  252. callback = function()
  253. for _,v in pairs(backlight_devices) do
  254. local data,err = syscontrol.backlight.read_attribs(v)
  255. if data then
  256. local w = widget_map[data.name]
  257. local online = w:get_children_by_id("brightness_id")[1]
  258. online:set_markup("Brightness: "..tostring(data.brightness))
  259. else
  260. print(err)
  261. end
  262. end
  263. end
  264. })
  265. -- Keybindings
  266. root.keys(gears.table.join(
  267. root.keys(),
  268. ask.k(":battery.brightness_up",function()
  269. if default_backlight_device then
  270. local data = default_backlight_device
  271. local s = widget_map[data.name]:get_children_by_id("slider")[1]
  272. local value = s.value+(data.max_brightness*0.05)
  273. if value > data.max_brightness then
  274. value = data.max_brightness
  275. end
  276. syscontrol.backlight.set_brightness(data,math.floor(value))
  277. s.value = math.floor(value)
  278. end
  279. end,{description="increase brightness", group = "widgets"}),
  280. ask.k(":battery.brightness_down",function()
  281. if default_backlight_device then
  282. local data = default_backlight_device
  283. local s = widget_map[data.name]:get_children_by_id("slider")[1]
  284. local value = s.value-(data.max_brightness*0.05)
  285. if value < data.max_brightness*0.05 then
  286. value = data.max_brightness*0.05
  287. end
  288. syscontrol.backlight.set_brightness(data,math.floor(value))
  289. s.value = math.floor(value)
  290. end
  291. end,{description="decrease brightness", group = "widgets"})
  292. ))
  293. -- }}}
  294. -- We don't need this widget if we don't have anything to show
  295. local function count(t)
  296. local count = 0
  297. for k,v in pairs(t) do
  298. count = count + 1
  299. end
  300. return count
  301. end
  302. if count(widget_map) == 0 then
  303. backlight_update:stop()
  304. power_update:stop()
  305. return
  306. end
  307. return battery_widget
  308. end