added options to control if the current rate and remaining time should be displayed
[wmiirc-lua.git] / plugins / cpu.lua
blob353eb162f1fa2b998d9ccd07bd62579975fcb322
1 --[[
2 =pod
4 =head1 NAME
6 cpu.lua - wmiirc-lua plugin for monitoring acpi stuff
8 =head1 SYNOPSIS
10 -- in your wmiirc.lua:
11 wmii.load_plugin("cpu")
14 =head1 DESCRIPTION
16 =head1 SEE ALSO
18 L<wmii(1)>, L<lua(1)>
20 =head1 AUTHOR
22 Jan-David Quesel <jdq@gmx.net>
24 =head1 LICENCE AND COPYRIGHT
26 Copyright (c) 2008, Jan-David Quesel <jdq@gmx.net>
28 This is free software. You may redistribute copies of it under the terms of
29 the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
30 is NO WARRANTY, to the extent permitted by law.
32 =cut
33 --]]
35 local wmii = require("wmii")
36 local os = require("os")
37 local posix = require("posix")
38 local io = require("io")
39 local type = type
40 local error = error
41 local pairs = pairs
42 local tostring = tostring
44 module("cpu")
45 api_version = 0.1
47 -- ------------------------------------------------------------
48 -- MODULE VARIABLES
49 local widget = nil
50 local timer = nil
52 widget = wmii.widget:new ("400_cpu")
54 local function cpu_list()
55 local dir = "/sys/devices/system/cpu/"
56 local _,cpu
57 local list = {}
58 for _,cpu in pairs(posix.glob(dir .. 'cpu[0-9]*')) do
59 local stat
60 if cpu then
61 stat = posix.stat(cpu)
62 if stat and stat.type == 'directory' then
63 list[#list+1] = cpu
64 end
65 end
66 end
67 return list
68 end
71 function read_file(path)
72 local fd = io.open(path, "r")
73 if fd == nil then
74 return nil
75 end
77 local text = fd:read("*a")
78 fd:close()
80 if type(text) == 'string' then
81 text = text:match('(%w+)')
82 end
84 return text
85 end
88 local function create_string(cpu)
89 local govfile = cpu .. '/cpufreq/scaling_governor'
90 local gov = read_file(govfile) or ""
92 local frqfile = cpu .. '/cpufreq/scaling_cur_freq'
93 local frq = read_file(frqfile) or ""
95 if type(frq) == 'string' then
96 local mhz = frq:match('(.*)000')
97 if mhz then
98 frq = mhz .. " MHz"
99 else
100 frq = frq .. "kHz"
102 else
103 frq = ""
106 return frq .. "(" .. gov .. ")"
110 function update ( new_vol )
111 local txt = ""
112 local _, cpu
113 local list = cpu_list()
114 local space = ""
115 for _,cpu in pairs(list) do
116 local str = create_string(cpu)
117 if txt == str then
118 txt = "2x " .. txt
119 else
120 txt = txt .. create_string(cpu) .. space
122 space = " "
125 widget:show(txt)
128 local function cpu_timer ( timer )
129 update(0)
130 return 10
133 timer = wmii.timer:new (cpu_timer, 1)