| 1 | -- contrib/ossvol_linux.lua |
| 2 | -- Copyright (C) 2010 Adrian C. <[email protected]> |
| 3 | -- Copyright (C) 2017 Jörg Thalheim <[email protected]> |
| 4 | -- |
| 5 | -- This file is part of Vicious. |
| 6 | -- |
| 7 | -- Vicious is free software: you can redistribute it and/or modify |
| 8 | -- it under the terms of the GNU General Public License as |
| 9 | -- published by the Free Software Foundation, either version 2 of the |
| 10 | -- License, or (at your option) any later version. |
| 11 | -- |
| 12 | -- Vicious is distributed in the hope that it will be useful, |
| 13 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | -- GNU General Public License for more details. |
| 16 | -- |
| 17 | -- You should have received a copy of the GNU General Public License |
| 18 | -- along with Vicious. If not, see <https://www.gnu.org/licenses/>. |
| 19 | |
| 20 | -- {{{ Grab environment |
| 21 | local tonumber = tonumber |
| 22 | local io = { popen = io.popen } |
| 23 | local setmetatable = setmetatable |
| 24 | local string = { match = string.match } |
| 25 | -- }}} |
| 26 | |
| 27 | |
| 28 | -- Ossvol: provides volume levels of requested OSS mixers |
| 29 | -- vicious.contrib.ossvol |
| 30 | local ossvol_linux = {} |
| 31 | |
| 32 | |
| 33 | -- {{{ Volume widget type |
| 34 | local function worker(format, warg) |
| 35 | if not warg then return end |
| 36 | |
| 37 | local mixer_state = { |
| 38 | ["on"] = "♫", -- "", |
| 39 | ["off"] = "♩" -- "M" |
| 40 | } |
| 41 | |
| 42 | -- Get mixer control contents |
| 43 | local f = io.popen("ossmix -c") |
| 44 | local mixer = f:read("*all") |
| 45 | f:close() |
| 46 | |
| 47 | -- Capture mixer control state |
| 48 | local volu = tonumber(string.match(mixer, warg .. "[%s]([%d%.]+)"))/0.25 |
| 49 | local mute = string.match(mixer, "vol%.mute[%s]([%a]+)") |
| 50 | -- Handle mixers without data |
| 51 | if volu == nil then |
| 52 | return {0, mixer_state["off"]} |
| 53 | end |
| 54 | |
| 55 | -- Handle mixers without mute |
| 56 | if mute == "OFF" and volu == "0" |
| 57 | -- Handle mixers that are muted |
| 58 | or mute == "ON" then |
| 59 | mute = mixer_state["off"] |
| 60 | else |
| 61 | mute = mixer_state["on"] |
| 62 | end |
| 63 | |
| 64 | return {volu, mute} |
| 65 | end |
| 66 | -- }}} |
| 67 | |
| 68 | return setmetatable(ossvol_linux, { __call = function(_, ...) return worker(...) end }) |