patches for review

5 views
Skip to first unread message

Bart Trojanowski

unread,
Jul 22, 2008, 6:54:32 PM7/22/08
to wmii...@googlegroups.com
I am following this email with the list of patches currently in the 'pu'
branch. 'pu' stands for proposed updates, and the idea is lifted from
the git development process. Patches on 'pu' will be those that
implement new feature and are being considered for merging. The 'pu'
branch will be rebased often, so make sure that you rebase any work you
don top of it to track the upstream 'pu'.

--
WebSig: http://www.jukie.net/~bart/sig/

Bart Trojanowski

unread,
Jul 22, 2008, 6:57:44 PM7/22/08
to wmii...@googlegroups.com, ba...@jukie.net, Stefan Riegler
From: Stefan Riegler <s...@bigfatflat.net>

---
plugins/battery_monitor.lua | 119 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 119 insertions(+), 0 deletions(-)
create mode 100644 plugins/battery_monitor.lua

diff --git a/plugins/battery_monitor.lua b/plugins/battery_monitor.lua
new file mode 100644
index 0000000..5e68d0a
--- /dev/null
+++ b/plugins/battery_monitor.lua
@@ -0,0 +1,119 @@
+--
+-- Copyright (c) 2007, Stefan Riegler <s...@bigfatflat.net>
+--
+-- Battery Monitor Plugin, based on the implementation for ruby-wmiirc,
+-- standard-plugin.rb by Mauricio Fernandez <m...@acm.org>
+-- http://eigenclass.org/hiki.rb?wmii+ruby
+--
+-- Licensed under the terms and conditions of the GPL
+--
+
+local wmii = require("wmii")
+local io = require("io")
+local os = require("os")
+local string = require("string")
+local tonumber = tonumber
+
+module("battery_monitor")
+api_version=0.1
+
+-- ------------------------------------------------------------
+-- Configuration Settings
+wmii.set_conf ("batmon.statefile", "/proc/acpi/battery/BAT0/state")
+wmii.set_conf ("batmon.infofile", "/proc/acpi/battery/BAT0/info")
+
+wmii.set_conf ("batmon.low", 8)
+wmii.set_conf ("batmon.low_action", 'echo "Low battery" | xmessage -center -buttons quit:0 -default quit -file -')
+
+wmii.set_conf ("batmon.critical", 2)
+wmii.set_conf ("batmon.critical_action", 'echo "Critical battery" | xmessage -center -buttons quit:0 -default quit -file -')
+
+wmii.set_conf ("batmon.warned_low", "false")
+wmii.set_conf ("batmon.warned_critical", "false")
+
+-- ------------------------------------------------------------
+-- Local Variables
+local warned_low = false
+local warned_critical = false
+
+local low = wmii.get_conf ("batmon.low")
+local critical = wmii.get_conf ("batmon.critical")
+
+local batt = nil
+local battinfo = nil
+local battpresent = nil
+
+local printout, text = nil
+
+-- ------------------------------------------------------------
+-- Init Plugin
+local widget = wmii.widget:new ("901_battery_monitor")
+
+
+-- ------------------------------------------------------------
+-- The battery status update function (wmii.timer function)
+-- parses info, state file and preps for display
+--
+local function update_batt_data (time_since_update)
+
+ local fbatt = io.open(wmii.get_conf("batmon.statefile"),"r")
+ local fbattinfo = io.open(wmii.get_conf("batmon.infofile"),"r")
+
+ if fbatt ~= nil then
+ batt = fbatt:read("*a")
+ battinfo = fbattinfo:read("*a")
+
+ battpresent = batt:match('present:%s+(%w+)')
+
+ if battpresent == "yes" then
+ batt_percent = batt:match('remaining capacity:%s+(%d+)') /
+ battinfo:match('last full capacity:%s+(%d+)') * 100
+ batt_state = batt:match('charging state:%s+(%w+)')
+
+ --
+ -- Take action in case battery is low/critical
+ --
+ if batt_state == "discharging" and batt_percent <= critical then
+ if not warned_critical then
+ wmii.log("Warning about critical battery.")
+ os.execute(wmii.get_conf("battmon.critical_action"), "&")
+ warned_critical = true
+ end
+ elseif batt_state == "discharging" and batt_percent <= low then
+ if not warned_low then
+ wmii.log("Warning about low battery.")
+ os.execute(wmii.get_conf("battmon.low_action"), "&")
+ warned_low = true
+ end
+ else
+ warned_low = false
+ warned_critical = false
+ end
+
+
+ -- If percent is 100 and state is discharging then
+ -- the battery is full and not discharging.
+ if (batt_state == "charged") or (batt_state == "discharging" and batt_percent >= 97) then
+ batt_state = "="
+ end
+ if batt_state == "charging" then
+ batt_state = "^"
+ end
+ if batt_state == "discharging" then
+ batt_state = "v"
+ end
+
+ printout = batt_state .. string.format("%.0f",batt_percent) .. batt_state
+ else
+ printout = "N/A"
+ end
+ end
+
+ widget:show(printout)
+ return 5
+end
+-- function update_batt_data
+-- ----------------------------------------------
+
+local timer = wmii.timer:new (update_batt_data, 1)
+
--
1.5.6.2.221.g3c6e79

Bart Trojanowski

unread,
Jul 22, 2008, 6:57:47 PM7/22/08
to wmii...@googlegroups.com, ba...@jukie.net
- read /sys directly, skip the cpufreq-info
- remove the battery info as we have a battery module
---
plugins/cpu.lua | 89 +++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 61 insertions(+), 28 deletions(-)

diff --git a/plugins/cpu.lua b/plugins/cpu.lua
index 79273a4..43bb003 100644
--- a/plugins/cpu.lua
+++ b/plugins/cpu.lua
@@ -34,9 +34,12 @@ is NO WARRANTY, to the extent permitted by law.



local wmii = require("wmii")

local os = require("os")

+local posix = require("posix")


local io = require("io")

local type = type
local error = error
+local pairs = pairs
+local tostring = tostring

module("cpu")
api_version = 0.1
@@ -48,43 +51,73 @@ local timer = nil

widget = wmii.widget:new ("400_cpu")

-local function _command ( cmd )
-
- if (cmd) then
- wmii.log( "about to run " .. cmd)
- local file = io.popen( cmd)
- local status = file:read("*a")
- file:close()
+local function cpu_list()
+ local dir = "/sys/devices/system/cpu/"
+ local _,cpu
+ local list = {}
+ for _,cpu in pairs(posix.glob(dir .. 'cpu[0-9]*')) do
+ local stat
+ if cpu then
+ stat = posix.stat(cpu)
+ if stat and stat.type == 'directory' then
+ list[#list+1] = cpu
+ end
+ end
+ end
+ return list
+end

- return status:match("[^\n]*")
- else
- return ""
+function read_file(path)
+ local fd = io.open(path, "r")
+ if fd == nil then
+ return nil
end
+
+ local text = fd:read("*a")
+ fd:close()
+
+ if type(text) == 'string' then
+ text = text:match('(%w+)')
+ end
+
+ return text
end

-local function create_string ( )
- wmii.log( "create string")
- local cmd = "cpufreq-info |grep 'current CPU fre'|uniq | awk '{print $5 $6}'"
- local cmd2 = "cpufreq-info | grep \'The gove\'|awk '{print $3}' | uniq"
- local cmd3 = "echo `awk '/remaining/ {print $3}' /proc/acpi/battery/BAT0/state`\*100/`awk '/last/ {print $4}' /proc/acpi/battery/BAT0/info` | bc"
- local str2 = _command(cmd2)
- str2 = str2.sub(str2, 2, str2.len(str2)-1)
- local str = _command(cmd)
- str = str.sub(str, 1, str.len(str)-1)
- return str .. "(" .. str2 .. ") BAT0: " .. _command(cmd3) .. "%"
+local function create_string(cpu)
+ local govfile = cpu .. '/cpufreq/scaling_governor'
+ local gov = read_file(govfile) or ""
+
+ local frqfile = cpu .. '/cpufreq/scaling_cur_freq'
+ local frq = read_file(frqfile) or ""
+
+ if type(frq) == 'string' then
+ local mhz = frq:match('(.*)000')
+ if mhz then
+ frq = mhz .. "MHz"
+ else
+ frq = frq .. "kHz"
+ end
+ else
+ frq = ""
+ end
+
+ return gov .. " " .. frq
end

function update ( new_vol )
- local str = create_string()
-
- widget:show(str)
+ local txt = ""
+ local _, cpu
+ local list = cpu_list()
+ for _,cpu in pairs(list) do
+ txt = txt .. create_string(cpu) .. " "
+ end
+
+ widget:show(txt)
end

-local function cpu_timer ( timer )
-
- wmii.log("cpu_timer()")
- update(0)
- return 10
+local function cpu_timer(timer)
+ update(0)
+ return 10
end

timer = wmii.timer:new (cpu_timer, 1)
--
1.5.6.2.221.g3c6e79

Bart Trojanowski

unread,
Jul 22, 2008, 6:57:45 PM7/22/08
to wmii...@googlegroups.com, ba...@jukie.net, Dave O'Neill
From: Dave O'Neill <d...@dmo.ca>

- general code cleanups
- add documentation
- colorized status widget
- configurable polling rate
- multiple battery support
---
doc/bundled-plugins | 5 +
plugins/battery.lua | 273 +++++++++++++++++++++++++++++++++++++++++++
plugins/battery_monitor.lua | 119 -------------------
3 files changed, 278 insertions(+), 119 deletions(-)
create mode 100644 plugins/battery.lua
delete mode 100644 plugins/battery_monitor.lua

diff --git a/doc/bundled-plugins b/doc/bundled-plugins
index 0d56353..4326eba 100644
--- a/doc/bundled-plugins
+++ b/doc/bundled-plugins
@@ -40,6 +40,11 @@ view_workdirs
update a table of working directories per view. See plugin comments
for more info.

+battery
+=======
+ Monitor percentage of battery capacity remaining in bar. Supports warning on
+ low and critical status, and display of multiple batteries.
+



diff --git a/plugins/battery.lua b/plugins/battery.lua
new file mode 100644
index 0000000..3538e4e
--- /dev/null
+++ b/plugins/battery.lua
@@ -0,0 +1,273 @@
+--[[
+=pod
+
+=head1 NAME
+
+battery.lua - wmiirc-lua plugin for battery percentage
+
+=head1 SYNOPSIS
+
+ -- in your wmiirc
+ wmii.load_plugin("battery")
+
+ -- To configure (after loading plugin)
+
+ -- Multiple batteries
+ wmii.set_conf("battery.names", "BAT0,BAT1");
+
+ -- Polling rate (in seconds)
+ wmii.set_conf("battery.poll_rate", 30)
+
+=head1 DESCRIPTION
+
+This plugin module provides a battery usage display.
+
+=head1 CONFIGURATION AND ENVIRONMENT
+
+There are several configurable options at the moment, most of which will not
+need to be modified from the defaults for most users.
+
+=over 4
+
+=item battery.names
+
+A comma-separated list of battery names to poll for status. This allows the
+widget to display multiple battery names.
+
+Defaults to "BAT0"
+
+=item battery.poll_rate
+
+Time in seconds to wait between checks for battery status.
+
+Defaults to 30
+
+=item battery.low
+
+Provide a "low battery" warning at this percentage of remaining capacity.
+Colour of widget will change to the defined value, and the low_action, if any,
+will be invoked.
+
+Defaults to 15
+
+=item battery.low_fgcolor
+
+Foreground colour of widget when in low battery state.
+
+Defaults to #000000
+
+=item battery.low_bgcolor
+
+Background colour of widget when in low battery state.
+
+Defaults to #FFFF66
+
+=item battery.low_action
+
+Shell command to invoke on entering low battery state.
+
+Defaults to
+
+ echo "Low battery" | xmessage -center -buttons quit:0 -default quit -file -
+
+=item battery.critical
+
+Provide a "critical battery" warning at this percentage of remaining capacity.
+Colour of widget will change to the defined value, and the critical_action, if any,
+will be invoked.
+
+Defaults to 5
+
+=item battery.critical_fgcolor
+
+Foreground colour of widget when in critical battery state.
+
+Defaults to #000000
+
+=item battery.critical_bgcolor
+
+Background colour of widget when in critical battery state.
+
+Defaults to #FF0000
+
+=item battery.critical_action
+
+Shell command to invoke on entering critical battery state.
+
+Defaults to
+
+ echo "Critical battery" | xmessage -center -buttons quit:0 -default quit -file -
+
+=back
+
+=head1 BUGS AND LIMITATIONS
+
+Please report problems to the author.
+Patches are welcome.
+
+=over 4
+
+=item *
+
+You can't have different low/critical warning thresholds or colours per
+battery. If you actually want this, please send a patch.
+
+=back
+
+=head1 SEE ALSO
+
+L<wmii(1)>, L<lua(1)>
+
+=head1 AUTHOR
+
+Dave O'Neill <d...@dmo.ca>
+
+Based on a port by Stefan Riegler <s...@bigfatflat.net> of the ruby-wmiirc
+standard-plugin.rb battery handling originally written Mauricio Fernandez.
+
+=head1 LICENCE AND COPYRIGHT
+
+Copyright (c) 2007, Stefan Riegler <s...@bigfatflat.net>
+Copyright (c) 2008, Dave O'Neill <d...@dmo.ca>
+
+This is free software. You may redistribute copies of it under the terms of
+the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
+is NO WARRANTY, to the extent permitted by law.
+
+=cut
+


+--]]
+
+local wmii = require("wmii")
+local io = require("io")
+local os = require("os")
+local string = require("string")
+

+module("battery")
+api_version=0.1
+
+--
+-- Configuration Settings
+--
+wmii.set_conf ("battery.poll_rate", 30)
+
+wmii.set_conf ("battery.names", "BAT0")
+
+wmii.set_conf ("battery.low", 15)
+wmii.set_conf ("battery.low_fgcolor", "#000000")
+wmii.set_conf ("battery.low_bgcolor", "#FFFF66")
+wmii.set_conf ("battery.low_action", 'echo "Low battery" | xmessage -center -buttons quit:0 -default quit -file -')
+
+wmii.set_conf ("battery.critical", 5)
+wmii.set_conf ("battery.critical_fgcolor", "#000000")
+wmii.set_conf ("battery.critical_bgcolor", "#FF0000")
+wmii.set_conf ("battery.critical_action", 'echo "Critical battery" | xmessage -center -buttons quit:0 -default quit -file -')
+
+-- Should not need to be modified on Linux
+wmii.set_conf ("battery.statefile", "/proc/acpi/battery/%s/state")
+wmii.set_conf ("battery.infofile", "/proc/acpi/battery/%s/info")
+
+--
+-- Local Variables
+--
+local batteries = { }
+
+-- The actual work performed here.


+-- parses info, state file and preps for display

+local function update_single_battery ( battery )
+
+ local printout = "N/A"
+ local colors = wmii.get_ctl("normcolors")
+
+ local fbatt = io.open(string.format(wmii.get_conf("battery.statefile"), battery["name"] ),"r")
+ if fbatt == nil then
+ return battery["widget"]:show(printout, colors)
+ end
+
+ local batt = fbatt:read("*a")
+
+ local battpresent = batt:match('present:%s+(%w+)')
+ if battpresent ~= "yes" then
+ return battery["widget"]:show(printout, colors)
+ end
+
+ local low = wmii.get_conf ("battery.low")
+ local critical = wmii.get_conf ("battery.critical")
+
+ local fbattinfo = io.open(string.format(wmii.get_conf("battery.infofile"), battery["name"]),"r")
+ local battinfo = fbattinfo:read("*a")
+
+ local batt_percent = batt:match('remaining capacity:%s+(%d+)')
+ / battinfo:match('last full capacity:%s+(%d+)') * 100
+ local batt_state = batt:match('charging state:%s+(%w+)')


+
+ -- Take action in case battery is low/critical

+ if batt_percent <= critical then
+ if batt_state == "discharging" and not battery["warned_crit"] then


+ wmii.log("Warning about critical battery.")
+ os.execute(wmii.get_conf("battmon.critical_action"), "&")

+ battery["warned_crit"] = true
+ end
+ colors = string.gsub(colors, "^%S+ %S+",
+ wmii.get_conf ("battery.critical_fgcolor")
+ .. " "
+ .. wmii.get_conf ("battery.critical_bgcolor"),
+ 1)
+ elseif batt_percent <= low then
+ if batt_state == "discharging" and not battery["warned_low"] then


+ wmii.log("Warning about low battery.")
+ os.execute(wmii.get_conf("battmon.low_action"), "&")

+ battery["warned_low"] = true
+ end
+ colors = string.gsub(colors, "^%S+ %S+",
+ wmii.get_conf ("battery.low_fgcolor")
+ .. " "
+ .. wmii.get_conf ("battery.low_bgcolor"),
+ 1)
+ else
+ battery["warned_low"] = true
+ battery["warned_crit"] = true
+ end
+


+
+ -- If percent is 100 and state is discharging then
+ -- the battery is full and not discharging.
+ if (batt_state == "charged") or (batt_state == "discharging" and batt_percent >= 97) then
+ batt_state = "="
+ end
+ if batt_state == "charging" then
+ batt_state = "^"
+ end
+ if batt_state == "discharging" then
+ batt_state = "v"
+ end
+
+ printout = batt_state .. string.format("%.0f",batt_percent) .. batt_state
+

+ battery["widget"]:show(printout, colors)
+end


+
+-- ------------------------------------------------------------
+-- The battery status update function (wmii.timer function)

+local function update_batt_data (time_since_update)
+

+ local batt_names = wmii.get_conf("battery.names");
+
+ for battery in batt_names:gmatch("%w+") do
+ if( not batteries[battery] ) then
+ batteries[battery] = {
+ name = battery,
+ widget = wmii.widget:new ("901_battery_" .. battery),
+ warned_low = false,
+ warned_crit = false,
+ }
+ end
+ update_single_battery( batteries[battery] )
+ end
+
+ return wmii.get_conf("battery.poll_rate")
+end
+


+
+local timer = wmii.timer:new (update_batt_data, 1)
+

diff --git a/plugins/battery_monitor.lua b/plugins/battery_monitor.lua
deleted file mode 100644
index 5e68d0a..0000000
--- a/plugins/battery_monitor.lua
+++ /dev/null
@@ -1,119 +0,0 @@
---
--- Copyright (c) 2007, Stefan Riegler <s...@bigfatflat.net>
---
--- Battery Monitor Plugin, based on the implementation for ruby-wmiirc,
--- standard-plugin.rb by Mauricio Fernandez <m...@acm.org>
--- http://eigenclass.org/hiki.rb?wmii+ruby
---
--- Licensed under the terms and conditions of the GPL
---
-
-local wmii = require("wmii")
-local io = require("io")
-local os = require("os")
-local string = require("string")
-local tonumber = tonumber
-
-module("battery_monitor")
-api_version=0.1
-
--- ------------------------------------------------------------
--- Configuration Settings
-wmii.set_conf ("batmon.statefile", "/proc/acpi/battery/BAT0/state")
-wmii.set_conf ("batmon.infofile", "/proc/acpi/battery/BAT0/info")
-
-wmii.set_conf ("batmon.low", 8)
-wmii.set_conf ("batmon.low_action", 'echo "Low battery" | xmessage -center -buttons quit:0 -default quit -file -')
-
-wmii.set_conf ("batmon.critical", 2)
-wmii.set_conf ("batmon.critical_action", 'echo "Critical battery" | xmessage -center -buttons quit:0 -default quit -file -')
-
-wmii.set_conf ("batmon.warned_low", "false")
-wmii.set_conf ("batmon.warned_critical", "false")
-
--- ------------------------------------------------------------
--- Local Variables
-local warned_low = false
-local warned_critical = false
-
-local low = wmii.get_conf ("batmon.low")
-local critical = wmii.get_conf ("batmon.critical")
-
-local batt = nil
-local battinfo = nil
-local battpresent = nil
-
-local printout, text = nil
-
--- ------------------------------------------------------------
--- Init Plugin
-local widget = wmii.widget:new ("901_battery_monitor")
-
-
--- ------------------------------------------------------------
--- The battery status update function (wmii.timer function)
--- parses info, state file and preps for display
---
-local function update_batt_data (time_since_update)
-
- local fbatt = io.open(wmii.get_conf("batmon.statefile"),"r")
- local fbattinfo = io.open(wmii.get_conf("batmon.infofile"),"r")
-
- if fbatt ~= nil then
- batt = fbatt:read("*a")
- battinfo = fbattinfo:read("*a")
-
- battpresent = batt:match('present:%s+(%w+)')
-
- if battpresent == "yes" then
- batt_percent = batt:match('remaining capacity:%s+(%d+)') /
- battinfo:match('last full capacity:%s+(%d+)') * 100
- batt_state = batt:match('charging state:%s+(%w+)')
-
- --
- -- Take action in case battery is low/critical
- --
- if batt_state == "discharging" and batt_percent <= critical then
- if not warned_critical then
- wmii.log("Warning about critical battery.")
- os.execute(wmii.get_conf("battmon.critical_action"), "&")
- warned_critical = true
- end
- elseif batt_state == "discharging" and batt_percent <= low then
- if not warned_low then
- wmii.log("Warning about low battery.")
- os.execute(wmii.get_conf("battmon.low_action"), "&")
- warned_low = true
- end
- else
- warned_low = false
- warned_critical = false
- end
-
-
- -- If percent is 100 and state is discharging then
- -- the battery is full and not discharging.
- if (batt_state == "charged") or (batt_state == "discharging" and batt_percent >= 97) then
- batt_state = "="
- end
- if batt_state == "charging" then
- batt_state = "^"
- end
- if batt_state == "discharging" then
- batt_state = "v"
- end
-
- printout = batt_state .. string.format("%.0f",batt_percent) .. batt_state
- else
- printout = "N/A"
- end
- end
-
- widget:show(printout)
- return 5
-end
--- function update_batt_data
--- ----------------------------------------------
-
-local timer = wmii.timer:new (update_batt_data, 1)
-
--
1.5.6.2.221.g3c6e79

Bart Trojanowski

unread,
Jul 22, 2008, 6:57:46 PM7/22/08
to wmii...@googlegroups.com, ba...@jukie.net, Jan-David Quesel
From: Jan-David Quesel <que...@informatik.uni-oldenburg.de>

---
plugins/cpu.lua | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 90 insertions(+), 0 deletions(-)
create mode 100644 plugins/cpu.lua

diff --git a/plugins/cpu.lua b/plugins/cpu.lua
new file mode 100644
index 0000000..79273a4
--- /dev/null
+++ b/plugins/cpu.lua
@@ -0,0 +1,90 @@


+--[[
+=pod
+
+=head1 NAME
+

+cpu.lua - wmiirc-lua plugin for monitoring acpi stuff
+
+=head1 SYNOPSIS
+
+ -- in your wmiirc.lua:
+ wmii.load_plugin("cpu")
+
+
+=head1 DESCRIPTION


+
+=head1 SEE ALSO
+
+L<wmii(1)>, L<lua(1)>
+
+=head1 AUTHOR
+

+Jan-David Quesel <j...@gmx.net>


+
+=head1 LICENCE AND COPYRIGHT
+

+Copyright (c) 2008, Jan-David Quesel <j...@gmx.net>


+
+This is free software. You may redistribute copies of it under the terms of
+the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
+is NO WARRANTY, to the extent permitted by law.
+
+=cut

+--]]
+
+local wmii = require("wmii")

+local os = require("os")
+local io = require("io")

+local type = type
+local error = error
+
+module("cpu")
+api_version = 0.1
+
+-- ------------------------------------------------------------
+-- MODULE VARIABLES
+local widget = nil
+local timer = nil
+
+widget = wmii.widget:new ("400_cpu")
+
+local function _command ( cmd )
+
+ if (cmd) then
+ wmii.log( "about to run " .. cmd)
+ local file = io.popen( cmd)
+ local status = file:read("*a")
+ file:close()
+
+ return status:match("[^\n]*")
+ else
+ return ""
+ end
+end
+
+local function create_string ( )
+ wmii.log( "create string")
+ local cmd = "cpufreq-info |grep 'current CPU fre'|uniq | awk '{print $5 $6}'"
+ local cmd2 = "cpufreq-info | grep \'The gove\'|awk '{print $3}' | uniq"
+ local cmd3 = "echo `awk '/remaining/ {print $3}' /proc/acpi/battery/BAT0/state`\*100/`awk '/last/ {print $4}' /proc/acpi/battery/BAT0/info` | bc"
+ local str2 = _command(cmd2)
+ str2 = str2.sub(str2, 2, str2.len(str2)-1)
+ local str = _command(cmd)
+ str = str.sub(str, 1, str.len(str)-1)
+ return str .. "(" .. str2 .. ") BAT0: " .. _command(cmd3) .. "%"
+end
+
+function update ( new_vol )
+ local str = create_string()
+
+ widget:show(str)
+end
+
+local function cpu_timer ( timer )
+
+ wmii.log("cpu_timer()")


+ update(0)
+ return 10

+end
+
+timer = wmii.timer:new (cpu_timer, 1)
--
1.5.6.2.221.g3c6e79

d...@dmo.ca

unread,
Jul 23, 2008, 10:32:23 PM7/23/08
to wmii...@googlegroups.com, Dave O'Neill
From: Dave O'Neill <d...@dmo.ca>

- general code formatting cleanups
- module-level documentation


- colorized status widget
- configurable polling rate

- multiple battery support including battery autodetection
- no longer leaks filehandles
---
doc/bundled-plugins | 5 +
plugins/battery.lua | 288 +++++++++++++++++++++++++++++++++++++++++++
plugins/battery_monitor.lua | 119 ------------------
3 files changed, 293 insertions(+), 119 deletions(-)


create mode 100644 plugins/battery.lua
delete mode 100644 plugins/battery_monitor.lua

diff --git a/doc/bundled-plugins b/doc/bundled-plugins
index 0d56353..4326eba 100644
--- a/doc/bundled-plugins
+++ b/doc/bundled-plugins
@@ -40,6 +40,11 @@ view_workdirs
update a table of working directories per view. See plugin comments
for more info.

+battery
+=======
+ Monitor percentage of battery capacity remaining in bar. Supports warning on
+ low and critical status, and display of multiple batteries.
+



diff --git a/plugins/battery.lua b/plugins/battery.lua
new file mode 100644

index 0000000..ce0abe1
--- /dev/null
+++ b/plugins/battery.lua
@@ -0,0 +1,288 @@


+--[[
+=pod
+
+=head1 NAME
+
+battery.lua - wmiirc-lua plugin for battery percentage
+
+=head1 SYNOPSIS
+
+ -- in your wmiirc
+ wmii.load_plugin('battery')
+
+ -- To configure (after loading plugin)
+

+ -- Polling rate (in seconds)
+ wmii.set_conf('battery.poll_rate', 30)
+
+=head1 DESCRIPTION
+

+This plugin module provides a battery usage display. Batteries are
+automatically detected via enumeration of the /proc/acpi/battery contents, and
+their percentage full displayed on the bar.


+
+=head1 CONFIGURATION AND ENVIRONMENT
+
+There are several configurable options at the moment, most of which will not
+need to be modified from the defaults for most users.
+
+=over 4
+

+local posix = require('posix')

+
+local pairs = pairs


+
+module('battery')
+api_version=0.1
+
+--
+-- Configuration Settings
+--
+wmii.set_conf ('battery.poll_rate', 30)
+

+wmii.set_conf ('battery.low', 15)


+wmii.set_conf ('battery.low_fgcolor', '#000000')
+wmii.set_conf ('battery.low_bgcolor', '#FFFF66')
+wmii.set_conf ('battery.low_action', 'echo "Low battery" | xmessage -center -buttons quit:0 -default quit -file -')
+
+wmii.set_conf ('battery.critical', 5)
+wmii.set_conf ('battery.critical_fgcolor', '#000000')
+wmii.set_conf ('battery.critical_bgcolor', '#FF0000')
+wmii.set_conf ('battery.critical_action', 'echo "Critical battery" | xmessage -center -buttons quit:0 -default quit -file -')
+
+--

+-- Local Variables
+--
+local batteries = { }
+
+-- The actual work performed here.
+-- parses info, state file and preps for display
+local function update_single_battery ( battery )
+
+ local printout = 'N/A'
+ local colors = wmii.get_ctl('normcolors')
+

+ local fbatt = io.open( battery['path'] .. '/state', 'r' )


+ if fbatt == nil then
+ return battery['widget']:show(printout, colors)
+ end
+ local batt = fbatt:read('*a')

+ fbatt:close()


+
+ local battpresent = batt:match('present:%s+(%w+)')
+ if battpresent ~= 'yes' then
+ return battery['widget']:show(printout, colors)
+ end
+
+ local low = wmii.get_conf ('battery.low')
+ local critical = wmii.get_conf ('battery.critical')
+

+ local fbattinfo = io.open( battery['path'] .. '/info', 'r' )


+ local battinfo = fbattinfo:read('*a')

+ fbattinfo:close()

+local function enumerate_batteries ( dir )
+
+ -- Make sure the dir exists
+ if not posix.stat(dir) then
+ return list
+ end
+
+ local _,batt
+ local list = {}
+ for _,path in pairs(posix.glob(dir .. 'BAT[0-9]*')) do
+ local stat
+ if path then
+ stat = posix.stat(path)


+ if stat and stat.type == 'directory' then

+ local name = path:match('(BAT%d+)')
+ list[name] = path
+ end
+ end
+ end
+
+ return list


+end
+
+-- ------------------------------------------------------------
+-- The battery status update function (wmii.timer function)
+local function update_batt_data (time_since_update)
+

+ local battlist = enumerate_batteries('/proc/acpi/battery/')
+
+ local name, path
+ for name,path in pairs(battlist) do
+ if( not batteries[name] ) then
+ batteries[name] = {
+ name = name,
+ path = path,
+ widget = wmii.widget:new ('901_battery_' .. name),


+ warned_low = false,
+ warned_crit = false,
+ }
+ end

+ update_single_battery( batteries[name] )

Reply all
Reply to author
Forward
0 new messages