From 2419068ede43d58819fd4890cbf649c5eefb5433 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Tue, 25 Feb 2025 04:29:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=20awesome=20pulse=20?= =?UTF-8?q?=E9=9F=B3=E9=87=8F=E6=98=BE=E7=A4=BA=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- awesome/README.org | 5 +- awesome/multicolor-theme.lua | 2 +- awesome/pulse-audio.diff | 51 ----------------- awesome/pulse.lua | 103 +++++++++++++++++++++++++++++++++++ awesome/rc.lua | 4 +- 5 files changed, 108 insertions(+), 57 deletions(-) delete mode 100644 awesome/pulse-audio.diff create mode 100644 awesome/pulse.lua diff --git a/awesome/README.org b/awesome/README.org index 6f8a800..2d897af 100644 --- a/awesome/README.org +++ b/awesome/README.org @@ -16,9 +16,8 @@ ln -sf "${dir}/pravitekeys.lua" ~/.config/awesome/ # 链接定制主题 ln -sf "${dir}/multicolor-theme.lua" ~/.config/awesome/themes/multicolor/theme-personal.lua - # 给 pulse 组建打补丁,该补丁主要为修正蓝牙耳机显示名称 - cd ~/.config/awesome/lain/ - git apply "$dir/pulse-audio.diff" + # 链接 pulse 音量显示组件 + ln -sf "${dir}/pulse.lua" ~/.config/awesome/lain/widget/pulse.lua #+end_src * 定制 =multicolor= 主题 diff --git a/awesome/multicolor-theme.lua b/awesome/multicolor-theme.lua index 045070b..d52c8cb 100644 --- a/awesome/multicolor-theme.lua +++ b/awesome/multicolor-theme.lua @@ -203,7 +203,7 @@ local volicon = wibox.widget.imagebox(theme.widget_vol) theme.volume = lain.widget.pulse({ settings = function() vlevel = volume_now.left .. "-" .. volume_now.right .. "%|" .. volume_now.device - if volume_now.muted == "yes" then + if volume_now.muted then vlevel = vlevel .. " M" end widget:set_markup(markup.fontfg(theme.font, "#7493d2", vlevel)) diff --git a/awesome/pulse-audio.diff b/awesome/pulse-audio.diff deleted file mode 100644 index 6d2240b..0000000 --- a/awesome/pulse-audio.diff +++ /dev/null @@ -1,51 +0,0 @@ -diff --git a/widget/pulse.lua b/widget/pulse.lua -index 69f4d70..d485d97 100644 ---- a/widget/pulse.lua -+++ b/widget/pulse.lua -@@ -22,14 +22,17 @@ local function factory(args) - local settings = args.settings or function() end - - pulse.devicetype = args.devicetype or "sink" -- pulse.cmd = args.cmd or "pacmd list-" .. pulse.devicetype .. "s | sed -n -e '/*/,$!d' -e '/index/p' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p' -e '/device\\.string/p'" -+ pulse.cmd = args.cmd or "pacmd list-" .. pulse.devicetype .. "s | sed -n -e '/*/,$!d' -e '/index/p' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p' -e '/device\\.string/p' -e '/device\\.description/p'" - - function pulse.update() - helpers.async({ shell, "-c", type(pulse.cmd) == "string" and pulse.cmd or pulse.cmd() }, - function(s) -+ local description = string.match(s, "device.description = \"(.-)\"") -+ local devicestring = string.match(s, "device.string = \"(%S+)\"") -+ local device = string.len(description) < string.len(devicestring) and description or devicestring - volume_now = { - index = string.match(s, "index: (%S+)") or "N/A", -- device = string.match(s, "device.string = \"(%S+)\"") or "N/A", -+ device = device or "N/A", - muted = string.match(s, "muted: (%S+)") or "N/A" - } - -diff --git a/widget/pulsebar.lua b/widget/pulsebar.lua -index 19e73b9..b1dbc1c 100644 ---- a/widget/pulsebar.lua -+++ b/widget/pulsebar.lua -@@ -51,7 +51,7 @@ local function factory(args) - pulsebar.followtag = args.followtag or false - pulsebar.notification_preset = args.notification_preset - pulsebar.devicetype = args.devicetype or "sink" -- pulsebar.cmd = args.cmd or "pacmd list-" .. pulsebar.devicetype .. "s | sed -n -e '/*/,$!d' -e '/index/p' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p' -e '/device\\.string/p'" -+ pulsebar.cmd = args.cmd or "pacmd list-" .. pulsebar.devicetype .. "s | sed -n -e '/*/,$!d' -e '/index/p' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p' -e '/device\\.string/p' -e '/device\\.description/p'" - - if not pulsebar.notification_preset then - pulsebar.notification_preset = { -@@ -76,9 +76,12 @@ local function factory(args) - function pulsebar.update(callback) - helpers.async({ awful.util.shell, "-c", type(pulsebar.cmd) == "string" and pulsebar.cmd or pulsebar.cmd() }, - function(s) -+ local description = string.match(s, "device.description = \"(.-)\"") -+ local devicestring = string.match(s, "device.string = \"(%S+)\"") -+ local device = string.len(description) < string.len(devicestring) and description or devicestring - volume_now = { - index = string.match(s, "index: (%S+)") or "N/A", -- device = string.match(s, "device.string = \"(%S+)\"") or "N/A", -+ device = device or "N/A", - muted = string.match(s, "muted: (%S+)") or "N/A" - } - diff --git a/awesome/pulse.lua b/awesome/pulse.lua new file mode 100644 index 0000000..ca47a81 --- /dev/null +++ b/awesome/pulse.lua @@ -0,0 +1,103 @@ +local helpers = require("lain.helpers") +local dkjson = require("lain.util.dkjson") +local shell = require("awful.util").shell +local wibox = require("wibox") +local string = string +local type = type + +-- PulseAudio volume +-- lain.widget.pulse + +local function get_device_name(sink) + local names = { + sink.description, + sink.properties["api.alsa.path"], + sink.properties["device.description"], + } + + local name = names[1] + for i = 2, #names do + local current_name = names[i] + + if not name then + name = names[i] + elseif current_name and string.len(name) > string.len(current_name) then + name = current_name + end + end + + return name or "N/A" +end + +local function split_sink_and_json(text) + local fn = string.gmatch(text, "[^\r\n]+") + local default_sink = fn() + local json_text = fn() + return default_sink, json_text +end + +local function parse_sink(text) + local sink_name, json_text = split_sink_and_json(text) + + local sink + for _, s in pairs(dkjson.decode(json_text)) do + if sink then + break + end + if s.name == sink_name then + sink = s + end + end + + local names = {} + + local channel_map, vol, props = sink.channel_map, sink.volume, sink.properties + + local channel = {} + local ch = 1 + for ch_name in string.gmatch(channel_map, "([%w-]+)") do + local vol_text = vol[ch_name].value_percent + channel[ch] = string.match(vol_text, "(%d+)%%") or "N/A" + ch = ch + 1 + end + + local volume = { + index = sink.index, + device = get_device_name(sink), + muted = sink.mute, + channel = channel, + left = channel[1] or "N/A", + right = channel[2] or "N/A", + } + + return volume +end + +local function factory(args) + args = args or {} + + local pulse = { widget = args.widget or wibox.widget.textbox(), device = "N/A" } + local timeout = args.timeout or 5 + local settings = args.settings or function() end + + pulse.devicetype = args.devicetype or "sink" + pulse.cmd = args.cmd or "pactl get-default-" .. pulse.devicetype .. ";pactl -f json list " .. pulse.devicetype .. "s" + + function pulse.update() + helpers.async({ shell, "-c", type(pulse.cmd) == "string" and pulse.cmd or pulse.cmd() }, + function(s) + volume_now = parse_sink(s) + + pulse.device = volume_now.index + + widget = pulse.widget + settings() + end) + end + + helpers.newtimer("pulse", timeout, pulse.update) + + return pulse +end + +return factory diff --git a/awesome/rc.lua b/awesome/rc.lua index 691be47..64bcdd4 100644 --- a/awesome/rc.lua +++ b/awesome/rc.lua @@ -57,7 +57,7 @@ local function run_once(cmd_arr) end end -run_once({ "unclutter -root", "pulseaudio --start", "picom -b --backend xrender" }) -- entries must be separated by commas +run_once({ "unclutter -root", "gentoo-pipewire-launcher", "picom -b --backend xrender" }) -- entries must be separated by commas -- This function implements the XDG autostart specification --[[ @@ -769,7 +769,7 @@ awful.rules.rules = { properties = { tag = mytags.specify.virtualbox } }, { rule = { class = "Emacs" }, - properties = { tag = mytags.specify.emacs, switchtotag = true, fullscreen = true } }, + properties = { tag = mytags.specify.emacs, switchtotag = true, fullscreen = false } }, { rule = { class = "dingtalk" }, properties = { tag = mytags.specify.other2 } },