Debian Codegraph @

vicious/widgets/cpufreq_linux.lua

raw symbols SCIP
1 -- CPU frequency widget type for GNU/Linux
2 -- Copyright (C) 2010 Adrian C. <[email protected]>
3 -- Copyright (C) 2017 mutlusun <[email protected]>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 -- {{{ Grab environment
21 local tonumber = tonumber
22 local helpers = require("vicious.helpers")
23 -- }}}
24
25 local GOVERNOR_STATE = {
26 ["ondemand\n"] = "↯",
27 ["powersave\n"] = "⌁",
28 ["userspace\n"] = "¤",
29 ["performance\n"] = "⚡",
30 ["conservative\n"] = "⊚"
31 }
32
33 -- {{{ CPU frequency widget type
34 return helpers.setcall(function (format, warg)
35 if not warg then return end
36
37 local _cpufreq = helpers.pathtotable(
38 ("/sys/devices/system/cpu/%s/cpufreq"):format(warg))
39 -- Default frequency and voltage values
40 local freqv = {
41 ["mhz"] = "N/A", ["ghz"] = "N/A",
42 ["v"] = "N/A", ["mv"] = "N/A",
43 }
44
45 -- Get the current frequency
46 local freq = tonumber(_cpufreq.scaling_cur_freq)
47 -- Calculate MHz and GHz
48 if freq then
49 freqv.mhz = freq / 1000
50 freqv.ghz = freqv.mhz / 1000
51
52 -- Get the current voltage
53 if _cpufreq.scaling_voltages then
54 freqv.mv = tonumber(
55 _cpufreq.scaling_voltages:match(freq .. "[%s]([%d]+)"))
56 -- Calculate voltage from mV
57 freqv.v = freqv.mv / 1000
58 end
59 end
60
61 -- Get the current governor
62 local governor = _cpufreq.scaling_governor
63 -- Represent the governor as a symbol
64 governor = GOVERNOR_STATE[governor] or governor or "N/A"
65
66 return {freqv.mhz, freqv.ghz, freqv.mv, freqv.v, governor}
67 end)
68 -- }}}