| 1 | -- volume widget type for GNU/Linux |
| 2 | -- Copyright (C) 2010 Adrian C. <[email protected]> |
| 3 | -- Copyright (C) 2017 Brandon Hartshorn <[email protected]> |
| 4 | -- Copyright (C) 2017 mutlusun <[email protected]> |
| 5 | -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m> |
| 6 | -- |
| 7 | -- This file is part of Vicious. |
| 8 | -- |
| 9 | -- Vicious is free software: you can redistribute it and/or modify |
| 10 | -- it under the terms of the GNU General Public License as |
| 11 | -- published by the Free Software Foundation, either version 2 of the |
| 12 | -- License, or (at your option) any later version. |
| 13 | -- |
| 14 | -- Vicious is distributed in the hope that it will be useful, |
| 15 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | -- GNU General Public License for more details. |
| 18 | -- |
| 19 | -- You should have received a copy of the GNU General Public License |
| 20 | -- along with Vicious. If not, see <https://www.gnu.org/licenses/>. |
| 21 | |
| 22 | -- {{{ Grab environment |
| 23 | local type = type |
| 24 | local tonumber = tonumber |
| 25 | local string = { match = string.match } |
| 26 | local table = { concat = table.concat } |
| 27 | |
| 28 | local helpers = require("vicious.helpers") |
| 29 | local spawn = require("vicious.spawn") |
| 30 | -- }}} |
| 31 | |
| 32 | -- Volume: provides volume levels and state of requested ALSA mixers |
| 33 | -- vicious.widgets.volume |
| 34 | local volume_linux = {} |
| 35 | |
| 36 | -- {{{ Volume widget type |
| 37 | local STATE = { on = '🔉', off = '🔈' } |
| 38 | |
| 39 | local function parse(stdout, stderr, exitreason, exitcode) |
| 40 | -- Capture mixer control state, e.g. [ 42 % ] [ on ] |
| 41 | local volume, state = string.match(stdout, "%[([%d]+)%%%].*%[([%l]*)%]") |
| 42 | -- Handle mixers without data |
| 43 | if volume == nil then return {} end |
| 44 | |
| 45 | if state == "" and volume == "0" -- handle mixers without mute |
| 46 | or state == "off" then -- handle muted mixers |
| 47 | return { tonumber(volume), STATE.off } |
| 48 | else |
| 49 | return { tonumber(volume), STATE.on } |
| 50 | end |
| 51 | end |
| 52 | |
| 53 | function volume_linux.async(format, warg, callback) |
| 54 | if not warg then return callback{} end |
| 55 | if type(warg) ~= "table" then warg = { warg } end |
| 56 | spawn.easy_async("amixer -M get " .. table.concat(warg, " "), |
| 57 | function (...) callback(parse(...)) end) |
| 58 | end |
| 59 | -- }}} |
| 60 | |
| 61 | return helpers.setasyncall(volume_linux) |