From 60e3bca73cf38dfd97acdcf12267855c42b6010d Mon Sep 17 00:00:00 2001 From: meliurwen Date: Wed, 16 Feb 2022 09:16:46 +0100 Subject: [PATCH] Moved mpv sockets to the user runtime folder defined in `XDG_RUNTIME_DIR` with a fallback to `/tmp`, also added a lua script to properly handle env variables in the mpv config for the `input-ipc-server` property --- i3/.config/i3/scripts/webcam-toggle.sh | 2 +- mpv/.config/mpv/mpv.conf | 3 +- .../mpv/scripts/set_input-ipc-server.lua | 40 +++++++++++++++++++ polybar/.config/polybar/config | 2 +- polybar/.config/polybar/scripts/mpv.sh | 6 +-- 5 files changed, 47 insertions(+), 6 deletions(-) create mode 100755 mpv/.config/mpv/scripts/set_input-ipc-server.lua diff --git a/i3/.config/i3/scripts/webcam-toggle.sh b/i3/.config/i3/scripts/webcam-toggle.sh index 706252f..14f8b31 100755 --- a/i3/.config/i3/scripts/webcam-toggle.sh +++ b/i3/.config/i3/scripts/webcam-toggle.sh @@ -2,7 +2,7 @@ pkill -f /dev/video || \ mpv --title="mpv-floating-window" \ - --input-ipc-server="$HOME/.cache/mpvsocket-webcam" \ + --no-config \ --no-osc \ --no-input-default-bindings \ --input-conf=/dev/null \ diff --git a/mpv/.config/mpv/mpv.conf b/mpv/.config/mpv/mpv.conf index f66b418..8050a12 100644 --- a/mpv/.config/mpv/mpv.conf +++ b/mpv/.config/mpv/mpv.conf @@ -15,7 +15,8 @@ hwdec=vaapi [default] # In order to communicate to the Polybar's mpv module -input-ipc-server=~/.cache/mpvsocket +#input-ipc-server=${XDG_RUNTIME_DIR:-/tmp/$(id -u)-runtime}/mpv/main.sock +script-opts=input-ipc-server="${XDG_RUNTIME_DIR:-/tmp/$(id -u)-runtime}/mpv/main.sock" save-position-on-quit diff --git a/mpv/.config/mpv/scripts/set_input-ipc-server.lua b/mpv/.config/mpv/scripts/set_input-ipc-server.lua new file mode 100755 index 0000000..f5b2791 --- /dev/null +++ b/mpv/.config/mpv/scripts/set_input-ipc-server.lua @@ -0,0 +1,40 @@ + +--[[-- + @script input-ipc-server-plus + @author Meliurwen + @license GPLv3 + This script makes possible the use of environment variables for the + `input-ipc-server` property in the config file. + + The script triggers if and only if no `input-ipc-server` is set and the + homonymous `input-ipc-server` option in `script-opts` is set in its place. + + A typical use case is to set the path with the env variable XDG_RUNTIME_DIR + and a respective fallback path like this: + + @usage script-opts=input-ipc-server="${XDG_RUNTIME_DIR:-/tmp/$(id -u)-runtime}/mpv/main.sock" + + It's worth to note that it's possible to include subshells in order to compute + part (or the whole) path. A typical use case, like in the just showed example + is to stick to POSIX standards, using `$(id -u)` instead of `$USER`. +]] + +local function isempty(s) + return s == nil or s == "" +end + +local function os_cmd_return(command) + local handle = io.popen(command) + local result = handle:read("*a") + handle:close() + return string.format(result:match("^%s*(.+)")) +end + +local ipc_opt_par = mp.get_property("input-ipc-server") +local ipc_opt_raw = mp.get_opt("input-ipc-server") +if isempty(ipc_opt_par) and not isempty(ipc_opt_raw) then + local ipc_opt_fullpath = os_cmd_return('printf "%s" ' .. ipc_opt_raw) + local ipc_opt_dirpath = os_cmd_return("dirname " .. ipc_opt_fullpath) + os.execute("mkdir -p " .. ipc_opt_dirpath) + mp.set_property("input-ipc-server", ipc_opt_fullpath) +end diff --git a/polybar/.config/polybar/config b/polybar/.config/polybar/config index a35f481..176690f 100644 --- a/polybar/.config/polybar/config +++ b/polybar/.config/polybar/config @@ -264,7 +264,7 @@ interval = 1 format = 