battery.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. --[[
  2. Awesome WM Battery Widget
  3. Distopico Vegan <distopico [at] riseup [dot] net>
  4. Licensed under GPL3
  5. Original from: https://github.com/mrzapp/awesomerc
  6. --]]
  7. local wibox = require("wibox")
  8. local awful = require("awful")
  9. local gears = require("gears")
  10. local naughty = require("naughty")
  11. local beautiful = require("beautiful")
  12. local brightness = require("widgets/brightness")
  13. local helpers = require("widgets/helpers")
  14. local config = awful.util.getdir("config")
  15. local widget = {}
  16. local popup = nil
  17. local batterytext = "--"
  18. local iconpath = ""
  19. -- {{{ Define adapter
  20. local adapter = "BAT1"
  21. local acAdapter = "ACAD"
  22. local charge = "charge"
  23. -- {{{ Define subwidgets
  24. widget.text = wibox.widget.textbox()
  25. widget._icon = wibox.widget.imagebox()
  26. -- Change the draw method so icons can be drawn smaller
  27. -- helpers:set_draw_method(widget._icon)
  28. -- }}}
  29. -- {{{ Define interactive behaviour
  30. widget._icon:buttons(gears.table.join(
  31. awful.button({ }, 1, function ()
  32. awful.spawn("lxqt-config-powermanagement", {
  33. floating = true,
  34. tag = mouse.screen.selected_tag,
  35. placement = awful.placement.centered,
  36. })
  37. end)
  38. ))
  39. -- }}}
  40. -- {{{ Check adapter method
  41. function widget:check()
  42. local adapters = string.gmatch(helpers:run("ls /sys/class/power_supply/"), "%S+")
  43. for value in adapters do
  44. if value:match("^A") then
  45. acAdapter = value
  46. elseif value:match("^B") then
  47. adapter = value
  48. end
  49. end
  50. -- Test identifier
  51. charge = "charge"
  52. widget.hasbattery = helpers:test("cat /sys/class/power_supply/" .. adapter .. "/" .. charge .. "_now")
  53. -- Try another identifier
  54. if not widget.hasbattery then
  55. charge = "energy"
  56. widget.hasbattery = helpers:test("cat /sys/class/power_supply/" .. adapter .. "/" .. charge .. "_now")
  57. end
  58. end
  59. -- }}}
  60. -- {{{ Update method
  61. function widget:update()
  62. local sendNotify = false
  63. local cur = helpers:run("cat /sys/class/power_supply/" ..adapter .. "/" .. charge .. "_now")
  64. local cap = helpers:run("cat /sys/class/power_supply/" ..adapter .. "/" .. charge .. "_full")
  65. local sta = helpers:run("cat /sys/class/power_supply/" ..adapter .. "/status")
  66. local ac = helpers:run("cat /sys/class/power_supply/" ..acAdapter .. "/online")
  67. if cur and cap then
  68. local acStatus = ac ~= "" and math.floor(ac) or 0;
  69. local battery = math.floor(cur * 100 / cap)
  70. local colorfg = beautiful.fg_urgent
  71. local toHibernate = false
  72. if acStatus == 1 then
  73. batterytext = battery .. "% Battery | AC"
  74. else
  75. batterytext = battery .. "% Battery"
  76. end
  77. iconpath = config.."/theme/icons/status/battery"
  78. if(battery < 5) then
  79. iconpath = iconpath .. "-caution"
  80. colorfg = "#FF2B2B"
  81. toHibernate = true
  82. elseif(battery < 10) then
  83. iconpath = iconpath .. "-caution"
  84. colorfg = "#FF5757"
  85. elseif(battery < 25) then
  86. iconpath = iconpath .. "-low"
  87. elseif(battery < 75) then
  88. iconpath = iconpath .. "-good"
  89. else
  90. iconpath = iconpath .. "-full"
  91. end
  92. if sta:match("Charging") then
  93. iconpath = iconpath .. "-charging"
  94. end
  95. iconpath = iconpath .. "-symbolic.svg"
  96. widget._icon:set_image(iconpath)
  97. widget.icon = helpers:set_draw_method(widget._icon)
  98. widget.text:set_markup(batterytext)
  99. if ((battery == 18 or battery == 10 or battery < 5) and sta:match("Discharging")) then
  100. sendNotify = true
  101. batterytext = batterytext .. " Warning low level batery!"
  102. brightness:set(40)
  103. if(toHibernate) then
  104. brightness:set(30)
  105. batterytext = batterytext .. " - Prepare Hibernate"
  106. helpers:delay(function() awful.spawn("systemctl hibernate") end, 10)
  107. end
  108. end
  109. if (sendNotify) then
  110. naughty.notify({
  111. icon = iconpath,
  112. icon_size = 30,
  113. text = batterytext,
  114. timeout = 4, hover_timeout = 0.5,
  115. screen = mouse.screen,
  116. fg = colorfg,
  117. ignore_suspend = true
  118. })
  119. end
  120. else
  121. widget.text:set_markup("N/A")
  122. end
  123. end
  124. function widget:show()
  125. popup = naughty.notify({
  126. icon = iconpath,
  127. icon_size = 16,
  128. text = batterytext,
  129. timeout = 0, hover_timeout = 0.5,
  130. screen = mouse.screen,
  131. ignore_suspend = true
  132. })
  133. end
  134. function widget:hide()
  135. if popup ~= nil then
  136. naughty.destroy(popup)
  137. popup = nil
  138. end
  139. end
  140. -- }}}
  141. -- {{{ Listen if battery was found
  142. widget:check()
  143. if widget.hasbattery then
  144. helpers:listen(widget)
  145. widget._icon:connect_signal("mouse::enter", function() widget:show() end)
  146. widget._icon:connect_signal("mouse::leave", function() widget:hide() end)
  147. end
  148. -- }}}
  149. return widget