cpufreq.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. --[[
  2. Awesome WM CPUFreq Widget
  3. Distopico Vegan <distopico [at] riseup [dot] net>
  4. Licensed under GPL3
  5. --]]
  6. local awful = require("awful")
  7. local wibox = require("wibox")
  8. local gears = require("gears")
  9. local helpers = require("widgets/helpers")
  10. local fraxcpumenu = {}
  11. local fraxcpu = {}
  12. local governor_state = {
  13. ["performance"] = "⚡",
  14. ["powersave"] = "⌁",
  15. ["userspace"] = "¤",
  16. ["ondemand"] = "↯",
  17. ["conservative"] = "⚆",
  18. ["schedutil"] = "⏲"
  19. }
  20. -- Create fraxcpumenu, and add all available governors to it
  21. local fh= io.open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", "r")
  22. if fh ~= nil then
  23. govstr= fh:read()
  24. fh:close()
  25. local i= 1
  26. for w in string.gmatch(govstr, "%a+") do
  27. local icon = governor_state[w] or ""
  28. fraxcpumenu[i] = { icon.." "..w, "sudo cpupower frequency-set -g "..w}
  29. i= i + 1
  30. end
  31. end
  32. fraxcpumenu = awful.menu.new( { items = fraxcpumenu, theme = {width = 120} } )
  33. -- Create fraxcpu widget
  34. fraxcpu.text = wibox.widget.textbox()
  35. -- Function for updating the fraxcpu widget
  36. fraxcpuupd = 1
  37. function fraxcpu:update ()
  38. if not fraxcpuupd then return(nil) end
  39. local _cpufreq = helpers:pathtotable("/sys/devices/system/cpu/cpu0/cpufreq")
  40. -- Default frequency and voltage values
  41. local freqv = {
  42. ["mhz"] = "N/A", ["ghz"] = "N/A",
  43. ["v"] = "N/A", ["mv"] = "N/A",
  44. }
  45. -- Get the current frequency and governor
  46. local freq = tonumber(_cpufreq.scaling_cur_freq)
  47. local governor = _cpufreq.scaling_governor:gsub("\n", "")
  48. -- Represent the governor as a symbol
  49. governor = governor_state[governor] or governor or "N/A"
  50. if freq then
  51. -- Calculate MHz and GHz
  52. freqv.mhz = freq / 1000
  53. freqv.ghz = freqv.mhz / 1000
  54. -- Get the current voltage
  55. if _cpufreq.scaling_voltages then
  56. freqv.mv = tonumber(string.match(_cpufreq.scaling_voltages, freq.."[%s]([%d]+)"))
  57. -- Calculate voltage from mV
  58. freqv.v = freqv.mv / 1000
  59. end
  60. else
  61. fraxcpuupd = nil
  62. local fh = io.open("/proc/cpuinfo", "r")
  63. if fh then
  64. for l in fh:lines() do
  65. freq = string.match(l, '^%s*cpu MHz%s*:%s*([0-9]+)')
  66. if freq ~= nil then break end
  67. freq = nil
  68. end
  69. end
  70. end
  71. if type(governor) == "string" then
  72. fraxcpu.text:set_markup(string.sub(freqv.ghz, 0, 3).."GHz "..governor)
  73. end
  74. end
  75. -- Mouse button bindings for fraxcpu widget
  76. fraxcpu.text:buttons(gears.table.join(
  77. awful.button({ }, 1, function () awful.menu.toggle(fraxcpumenu) end),
  78. awful.button({ }, 2, function () fraxcpu:update() end),
  79. awful.button({ }, 3, function () fraxcpu:update() end)
  80. ))
  81. -- {{{ Listen if signal was found
  82. if fraxcpuupd then
  83. helpers:listen(fraxcpu, 5)
  84. end
  85. -- }}
  86. return fraxcpu