brightness.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. --[[
  2. Awesome WM Brightness Widget
  3. Distopico Vegan <distopico [at] riseup [dot] net>
  4. Licensed under GPL3
  5. @author Tyler Compton <https://github.com/velovix/awesome.brightness-widget>
  6. --]]
  7. local wibox = require("wibox")
  8. local awful = require("awful")
  9. local naughty = require("naughty")
  10. local helpers = require("widgets/helpers")
  11. math = require("math")
  12. string = require("string")
  13. local config = awful.util.getdir("config")
  14. local widget = { mt = {}, wmt = {} }
  15. widget.wmt.__index = widget
  16. widget.__index = widget
  17. -- {{{ Create brightness widget method
  18. function widget:new(args)
  19. -- TODO: implement just sys and dims
  20. -- see: https://gitlab.com/wavexx/acpilight/-/blob/master/xbacklight
  21. -- dims: https://github.com/intervigilium/brightd
  22. local obj = setmetatable({}, self)
  23. local defaultBlPath = "/sys/class/backlight/acpi_video0"
  24. local amdGpuBlPath = "/sys/class/backlight/amdgpu_bl0"
  25. local gmuxBlPath = "/sys/class/backlight/gmux_backlight"
  26. local intelBlPath = "/sys/class/backlight/intel_backlight"
  27. obj.cmd = args.cmd or "xbacklight"
  28. obj.backlightPath = args.backlight_path or defaultBlPath
  29. if helpers:test(obj.cmd) then
  30. obj.step = args.step or 5
  31. obj.incVal = args.inc or "-inc"
  32. obj.decVal = args.dec or "-dec"
  33. obj.setVal = args.set or "-set"
  34. obj.getVal = args.get or "-get"
  35. obj.backlightPath = nil
  36. elseif helpers:exists(intelBlPath) then
  37. obj.backlightPath = intelBlPath
  38. elseif helpers:exists(gmuxBlPath) then
  39. obj.backlightPath = gmuxBlPath
  40. elseif helpers:exists(amdGpuBlPath) then
  41. obj.backlightPath = amdGpuBlPath
  42. else
  43. obj.cmd = nil
  44. obj.backlightPath = nil
  45. end
  46. if helpers:exists(obj.backlightPath) then
  47. obj.max = helpers:run("cat "..obj.backlightPath.."/max_brightness")
  48. obj.step = (obj.max / 100) * 3
  49. obj.cmd = nil
  50. end
  51. -- Create imagebox widget
  52. obj._icon = wibox.widget.imagebox()
  53. -- Change the draw method so icons can be drawn smaller
  54. --helpers:set_draw_method(obj.icon)
  55. -- icon raw path
  56. obj.iconpath = config.."/theme/icons/brightness-symbolic.svg"
  57. obj._icon:set_image(obj.iconpath)
  58. obj.icon = helpers:set_draw_method(obj._icon)
  59. -- Add a popup
  60. obj.popup = nil
  61. obj.brightness = nil
  62. helpers:listen(obj, 60)
  63. -- Listen if signal was found
  64. obj._icon:connect_signal("mouse::enter", function() obj:popupShow() end)
  65. obj._icon:connect_signal("mouse::leave", function() obj:popupHide() end)
  66. obj:update()
  67. return obj
  68. end
  69. function widget:tooltipText()
  70. return self:get().."% Brightness"
  71. end
  72. function widget:percentage(value)
  73. return math.floor((100 * value) / self.max)
  74. end
  75. function widget:update()
  76. local brightness = self:get()
  77. local iconpath = config.."/theme/icons/status/brightness"
  78. if(brightness < 5) then
  79. iconpath = iconpath .. "-none"
  80. elseif(brightness < 10) then
  81. iconpath = iconpath .. "-verylow"
  82. elseif(brightness < 25) then
  83. iconpath = iconpath .. "-low"
  84. elseif(brightness < 75) then
  85. iconpath = iconpath .. "-medium"
  86. elseif(brightness < 90) then
  87. iconpath = iconpath .. "-high"
  88. else
  89. iconpath = iconpath .. "-full"
  90. end
  91. self.iconpath = iconpath .. "-symbolic.svg"
  92. if self.brightness ~= brightness and self.brightness ~= nil then
  93. self:popupShow(1)
  94. end
  95. self._icon:set_image(self.iconpath)
  96. self.icon = helpers:set_draw_method(self._icon)
  97. self.brightness = brightness
  98. end
  99. function widget:up()
  100. if self.cmd ~= nil then
  101. helpers:run(self.cmd.." "..self.incVal.." "..self.step)
  102. elseif self.backlightPath ~= nil then
  103. local val = math.floor(helpers:run("cat "..self.backlightPath.."/brightness") + self.step)
  104. if val > math.floor(self.max) or self.brightness > 95 or self:percentage(val) >= 97 then
  105. val = self.max
  106. end
  107. local result = helpers:run("echo "..val.." > "..self.backlightPath.."/brightness")
  108. end
  109. helpers:delay(function() self:update() end, 0.1)
  110. end
  111. function widget:down()
  112. if self.cmd ~= nil then
  113. helpers:run(self.cmd.." "..self.decVal.." "..self.step)
  114. elseif self.backlightPath ~= nil then
  115. local val = math.floor(helpers:run("cat "..self.backlightPath.."/brightness") - self.step)
  116. if self:percentage(val) <= 5 then
  117. val = 0
  118. end
  119. helpers:run("echo "..val.." > "..self.backlightPath.."/brightness")
  120. end
  121. helpers:delay(function() self:update() end, 0.1)
  122. end
  123. function widget:get()
  124. if self.cmd ~= nil then
  125. local value = tonumber(helpers:run(self.cmd.." "..self.getVal)) or 0
  126. return math.floor(value)
  127. elseif self.backlightPath ~= nil then
  128. local value = tonumber(helpers:run("cat "..self.backlightPath.."/brightness"))
  129. return self:percentage(value)
  130. else
  131. return 100
  132. end
  133. end
  134. function widget:set(val)
  135. if self.cmd ~= nil then
  136. helpers:run(self.cmd.." "..self.setVal.." "..val)
  137. elseif self.backlightPath ~= nil then
  138. val = (self.max / 100) * val;
  139. helpers:run("echo "..val.." > "..self.backlightPath.."/brightness")
  140. end
  141. end
  142. function widget:popupShow(timeout)
  143. local icon = self.iconpath
  144. local tooltipText = self:tooltipText()
  145. self:popupHide()
  146. self.popup = naughty.notify({
  147. icon = icon,
  148. icon_size = 16,
  149. text = tooltipText,
  150. timeout = timeout,
  151. hover_timeout = 0.5,
  152. screen = mouse.screen,
  153. ignore_suspend = true
  154. })
  155. end
  156. function widget:popupHide()
  157. if self.popup ~= nil then
  158. naughty.destroy(self.popup)
  159. self.popup = nil
  160. end
  161. end
  162. function widget.mt:__call(...)
  163. return widget.new(...)
  164. end
  165. return widget:new({})