Debian Codegraph @

vicious/contrib/pulse_all.lua

raw symbols SCIP
1 -- contrib/pulse_all.lua
2 -- Copyright (C) 2010 MrMagne <[email protected]>
3 -- Copyright (C) 2010,2017 Jörg Thalheim <[email protected]>
4 -- Copyright (C) 2017 Jonathan McCrohan <[email protected]>
5 --
6 -- This file is part of Vicious.
7 --
8 -- Vicious is free software: you can redistribute it and/or modify
9 -- it under the terms of the GNU General Public License as
10 -- published by the Free Software Foundation, either version 2 of the
11 -- License, or (at your option) any later version.
12 --
13 -- Vicious is distributed in the hope that it will be useful,
14 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 -- GNU General Public License for more details.
17 --
18 -- You should have received a copy of the GNU General Public License
19 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
20
21 -- {{{ Grab environment
22 local type = type
23 local tonumber = tonumber
24 local io = { popen = io.popen }
25 local setmetatable = setmetatable
26 local os = { execute = os.execute }
27 local table = { insert = table.insert }
28 local string = {
29 find = string.find,
30 match = string.match,
31 format = string.format,
32 gmatch = string.gmatch
33 }
34 local math = {
35 floor = math.floor,
36 ceil = math.ceil
37 }
38 -- }}}
39
40
41 -- Pulse: provides volume levels of requested pulseaudio sinks and methods to change them
42 -- vicious.contrib.pulse
43 local pulse_all = {}
44
45 -- {{{ Helper function
46 local function pacmd(args)
47 local f = io.popen("pacmd "..args)
48 if f == nil then
49 return nil
50 else
51 local line = f:read("*all")
52 f:close()
53 return line
54 end
55 end
56
57 local function escape(text)
58 local special_chars = { ["."] = "%.", ["-"] = "%-" }
59 return text:gsub("[%.%-]", special_chars)
60 end
61
62 local cached_sinks = {}
63 local function get_sink_name(sink)
64 if type(sink) == "string" then return sink end
65 -- avoid nil keys
66 local key = sink or 1
67 -- Cache requests
68 if not cached_sinks[key] then
69 local line = pacmd("list-sinks")
70 if line == nil then return nil end
71 for s in string.gmatch(line, "name: <(.-)>") do
72 table.insert(cached_sinks, s)
73 end
74 end
75
76 return cached_sinks[key]
77 end
78
79
80 -- }}}
81
82 -- {{{ Pulseaudio widget type
83 local function worker(format, sink)
84 sink = get_sink_name(sink)
85 if sink == nil then return {0, "unknown"} end
86
87 -- Get sink data
88 local data = pacmd("dump")
89 if sink == nil then return {0, "unknown"} end
90
91 -- If mute return 0 (not "Mute") so we don't break progressbars
92 if string.find(data,"set%-sink%-mute "..escape(sink).." yes") then
93 return {0, "off"}
94 end
95
96 local vol = tonumber(string.match(data, "set%-sink%-volume "..escape(sink).." (0x[%x]+)"))
97 if vol == nil then vol = 0 end
98 volpercent = vol/0x10000*100
99
100 return { volpercent % 1 >= 0.5 and math.ceil(volpercent) or math.floor(volpercent), "on"}
101 end
102 -- }}}
103
104 -- {{{ Volume control helper
105 function pulse_all.add(percent, sink)
106 sink = get_sink_name(sink)
107 if sink == nil then return end
108
109 local data = pacmd("dump")
110
111 local pattern = "set%-sink%-volume "..escape(sink).." (0x[%x]+)"
112 local initial_vol = tonumber(string.match(data, pattern))
113
114 local vol = initial_vol + percent/100*0x10000
115 if vol > 0x10000 then vol = 0x10000 end
116 if vol < 0 then vol = 0 end
117
118 vol = math.ceil(vol)
119
120 local cmd = string.format("pacmd set-sink-volume %s 0x%x >/dev/null", sink, vol)
121 return os.execute(cmd)
122 end
123
124 function pulse_all.toggle(sink)
125 sink = get_sink_name(sink)
126 if sink == nil then return end
127
128 local data = pacmd("dump")
129 local pattern = "set%-sink%-mute "..escape(sink).." (%a%a%a?)"
130 local mute = string.match(data, pattern)
131
132 -- 0 to enable a sink or 1 to mute it.
133 local state = { yes = 0, no = 1}
134 local cmd = string.format("pacmd set-sink-mute %s %d", sink, state[mute])
135 return os.execute(cmd)
136 end
137 -- }}}
138
139 return setmetatable(pulse_all, { __call = function(_, ...) return worker(...) end })