File: statusd_volume2.lua

package info (click to toggle)
notion 4.0.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,676 kB
  • sloc: ansic: 47,508; sh: 2,096; makefile: 603; perl: 270
file content (71 lines) | stat: -rw-r--r-- 2,015 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- Authors: Randall Wald <randy@rwald.com>
-- License: GPL, version 2
-- Last Changed: Unknown
--
-- statusd_volume2.lua
-- Volume level and state information script
-- Written by Randall Wald
-- email: randy@rwald.com
-- Released under the GPL
--
-- Based on a public domain script written by Benjamin Sigonneau
--
-- This script uses "amixer" to find volume information. If you don't have
-- "amixer," this script will fail. Sorry.
-- Though this is labeled "statusd_volume2.lua", rename it to
-- "statusd_volume.lua" to make it work.
--
-- Available monitors:
-- 	%volume_level	Volume level, as a percentage from 0% to 100%
-- 	%volume_state	The string "" if unmuted, "MUTE " if muted
--
-- Example use:
-- 	template="[ %date || <other stuff> || vol: %volume_level %volume state]"
-- 	(note space between monitors but lack of space after %volume_state)
-- This will print
-- 	[ <Date> || <other stuff> || vol: 54% ]
-- when unmuted but
-- 	[ <Date> || <other stuff> || vol: 54% MUTE ]
-- when muted.

local function get_volume()
   local f=io.popen('amixer','r')
   local s=f:read('*all')
   f:close()
   local _, _, master_level, master_state = string.find(s, "%[(%d*%%)%] %[(%a*)%]")
   local sound_state = ""
   if master_state == "off" then
      sound_state = "MUTE "
   end
  return master_level.."", sound_state..""
end

local function inform_volume(name, value)
   if statusd ~= nil then
      statusd.inform(name, value)
   else
      io.stdout:write(name..": "..value.."\n")
   end
end
local function inform_state(value)
   if statusd ~= nil then
      statusd.inform("volume_state", value)
   else
      io.stdout:write("volume_state"..value.."\n")
   end
end

if statusd ~= nil then
   volume_timer = statusd.create_timer()
end

local function update_volume()
   local master_level, sound_state = get_volume()
   inform_volume("volume_level", master_level)
   inform_volume("volume_state", sound_state)
   if statusd ~= nil then
      volume_timer:set(500, update_volume)
   end
end

update_volume()