don't get stuck running the event loop once there are no more clients
[wmiirc-lua.git] / plugins / cpu.lua
blob43bb00333d512acb269497b73ef67e2e998820a2
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
70 function read_file(path)
71 local fd = io.open(path, "r")
72 if fd == nil then
73 return nil
74 end
76 local text = fd:read("*a")
77 fd:close()
79 if type(text) == 'string' then
80 text = text:match('(%w+)')
81 end
83 return text
84 end
86 local function create_string(cpu)
87 local govfile = cpu .. '/cpufreq/scaling_governor'
88 local gov = read_file(govfile) or ""
90 local frqfile = cpu .. '/cpufreq/scaling_cur_freq'
91 local frq = read_file(frqfile) or ""
93 if type(frq) == 'string' then
94 local mhz = frq:match('(.*)000')
95 if mhz then
96 frq = mhz .. "MHz"
97 else
98 frq = frq .. "kHz"
99 end
100 else
101 frq = ""
104 return gov .. " " .. frq
107 function update ( new_vol )
108 local txt = ""
109 local _, cpu
110 local list = cpu_list()
111 for _,cpu in pairs(list) do
112 txt = txt .. create_string(cpu) .. " "
115 widget:show(txt)
118 local function cpu_timer(timer)
119 update(0)
120 return 10
123 timer = wmii.timer:new (cpu_timer, 1)