summaryrefslogtreecommitdiff
path: root/.config/mpv/scripts/mpv_socket
diff options
context:
space:
mode:
Diffstat (limited to '.config/mpv/scripts/mpv_socket')
-rw-r--r--.config/mpv/scripts/mpv_socket/LICENSE21
-rw-r--r--.config/mpv/scripts/mpv_socket/main.lua56
2 files changed, 77 insertions, 0 deletions
diff --git a/.config/mpv/scripts/mpv_socket/LICENSE b/.config/mpv/scripts/mpv_socket/LICENSE
new file mode 100644
index 0000000..db38795
--- /dev/null
+++ b/.config/mpv/scripts/mpv_socket/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2024 A Farzat
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/.config/mpv/scripts/mpv_socket/main.lua b/.config/mpv/scripts/mpv_socket/main.lua
new file mode 100644
index 0000000..cc9da6b
--- /dev/null
+++ b/.config/mpv/scripts/mpv_socket/main.lua
@@ -0,0 +1,56 @@
+-- mpvSockets, one socket per instance, removes socket on exit
+
+package.path = mp.command_native({"expand-path", "~~/script-modules/?.lua;"})..package.path
+local input = require "user-input-module"
+local utils = require 'mp.utils'
+
+local sock_dir = os.getenv("MPV_SOCKET_DIR") or
+ utils.join_path(os.getenv("XDG_RUNTIME_DIR"), "mpv/sockets")
+mp.command_native({capture_stdout = true, capture_stderr = true,
+args = {"mkdir", "-p", sock_dir}, name = "subprocess", playback_only = false})
+
+local sock_file = utils.join_path(sock_dir,
+mp.get_opt("mpv_socket") or utils.getpid())
+
+mp.set_property("options/input-ipc-server", sock_file)
+
+local function check_active_socket(file_name)
+ return mp.command_native({
+ name = "subprocess",
+ capture_stdout = true,
+ capture_stderr = true,
+ args = {"nc", "-NU", file_name},
+ playback_only = false,
+ stdin_data = '{ "command": ["get_property", "path"] }\n',
+ }).status == 0
+end
+
+local function change_sockname()
+ input.get_user_input(function(line, _)
+ if line then
+ local new_sock_file = utils.join_path(sock_dir, line)
+ if check_active_socket(new_sock_file) then
+ change_sockname()
+ else
+ mp.set_property("options/input-ipc-server", new_sock_file)
+ end
+ end
+ end, { request_text = "set socket name:" })
+end
+
+mp.add_key_binding("alt+s", "NameIPCSocket", change_sockname)
+
+local function update_socket_name()
+ local new_name = mp.get_property("options/input-ipc-server")
+ if sock_file ~= new_name then
+ os.remove(sock_file)
+ end
+ sock_file = new_name
+end
+
+mp.observe_property("options/input-ipc-server", nil, update_socket_name)
+
+local function shutdown_handler()
+ os.remove(mp.get_property("options/input-ipc-server"))
+end
+mp.register_event("shutdown", shutdown_handler)