Released version 3-2014010502
[notion.git] / contrib / statusd / legacy / statusd_volume2.lua
blob7aabe2cc37e78a543d305c4317609c623150387f
1 -- Authors: Randall Wald <randy@rwald.com>
2 -- License: GPL, version 2
3 -- Last Changed: Unknown
4 --
5 -- statusd_volume2.lua
6 -- Volume level and state information script
7 -- Written by Randall Wald
8 -- email: randy@rwald.com
9 -- Released under the GPL
10 --
11 -- Based on a public domain script written by Benjamin Sigonneau
12 --
13 -- This script uses "amixer" to find volume information. If you don't have
14 -- "amixer," this script will fail. Sorry.
15 -- Though this is labeled "statusd_volume2.lua", rename it to
16 -- "statusd_volume.lua" to make it work.
18 -- Available monitors:
19 -- %volume_level Volume level, as a percentage from 0% to 100%
20 -- %volume_state The string "" if unmuted, "MUTE " if muted
22 -- Example use:
23 -- template="[ %date || <other stuff> || vol: %volume_level %volume state]"
24 -- (note space between monitors but lack of space after %volume_state)
25 -- This will print
26 -- [ <Date> || <other stuff> || vol: 54% ]
27 -- when unmuted but
28 -- [ <Date> || <other stuff> || vol: 54% MUTE ]
29 -- when muted.
31 local function get_volume()
32 local f=io.popen('amixer','r')
33 local s=f:read('*all')
34 f:close()
35 local _, _, master_level, master_state = string.find(s, "%[(%d*%%)%] %[(%a*)%]")
36 local sound_state = ""
37 if master_state == "off" then
38 sound_state = "MUTE "
39 end
40 return master_level.."", sound_state..""
41 end
43 local function inform_volume(name, value)
44 if statusd ~= nil then
45 statusd.inform(name, value)
46 else
47 io.stdout:write(name..": "..value.."\n")
48 end
49 end
50 local function inform_state(value)
51 if statusd ~= nil then
52 statusd.inform("volume_state", value)
53 else
54 io.stdout:write("volume_state"..value.."\n")
55 end
56 end
58 if statusd ~= nil then
59 volume_timer = statusd.create_timer()
60 end
62 local function update_volume()
63 local master_level, sound_state = get_volume()
64 inform_volume("volume_level", master_level)
65 inform_volume("volume_state", sound_state)
66 if statusd ~= nil then
67 volume_timer:set(500, update_volume)
68 end
69 end
71 update_volume()