summaryrefslogtreecommitdiffstats
path: root/TOOLS/umpv
diff options
context:
space:
mode:
Diffstat (limited to 'TOOLS/umpv')
-rwxr-xr-xTOOLS/umpv46
1 files changed, 18 insertions, 28 deletions
diff --git a/TOOLS/umpv b/TOOLS/umpv
index 0080b44ffb..1eece5d30b 100755
--- a/TOOLS/umpv
+++ b/TOOLS/umpv
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
"""
This script emulates "unique application" functionality on Linux. When starting
@@ -19,10 +19,6 @@ will typically just fill ~/.xsession-errors with garbage.
mpv will terminate if there are no more files to play, and running the umpv
script after that will start a new mpv instance.
-Note that you can control the mpv instance by writing to the command fifo:
-
- echo "cycle fullscreen" > ~/.umpv_fifo
-
Note: you can supply custom mpv path and options with the MPV environment
variable. The environment variable will be split on whitespace, and the
first item is used as path to mpv binary and the rest is passed as options
@@ -32,11 +28,11 @@ Note: you can supply custom mpv path and options with the MPV environment
import sys
import os
+import socket
import errno
import subprocess
-import fcntl
-import stat
import string
+import shlex
files = sys.argv[1:]
@@ -55,42 +51,36 @@ def make_abs(filename):
if not is_url(filename):
return os.path.abspath(filename)
return filename
-files = [make_abs(f) for f in files]
+files = (make_abs(f) for f in files)
-FIFO = os.path.join(os.getenv("HOME"), ".umpv_fifo")
+SOCK = os.path.join(os.getenv("XDG_RUNTIME_DIR", os.getenv("HOME")), ".umpv_socket")
-fifo_fd = -1
+sock = None
try:
- fifo_fd = os.open(FIFO, os.O_NONBLOCK | os.O_WRONLY)
-except OSError as e:
- if e.errno == errno.ENXIO:
- pass # pipe has no writer
+ sock = socket.socket(socket.AF_UNIX)
+ sock.connect(SOCK)
+except socket.error as e:
+ if e.errno == errno.ECONNREFUSED:
+ sock = None
+ pass # abandoned socket
elif e.errno == errno.ENOENT:
+ sock = None
pass # doesn't exist
else:
raise e
-if fifo_fd >= 0:
+if sock:
# Unhandled race condition: what if mpv is terminating right now?
- fcntl.fcntl(fifo_fd, fcntl.F_SETFL, 0) # set blocking mode
- fifo = os.fdopen(fifo_fd, "w")
for f in files:
# escape: \ \n "
f = f.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n")
f = "\"" + f + "\""
- fifo.write("raw loadfile " + f + " append\n")
+ sock.send(("raw loadfile " + f + " append\n").encode("utf-8"))
else:
- # Recreate pipe if it doesn't already exist.
- # Also makes sure it's safe, and no other user can create a bogus pipe
- # that breaks security.
- try:
- os.unlink(FIFO)
- except OSError as e:
- pass
- os.mkfifo(FIFO, 0o600)
+ # Let mpv recreate socket if it doesn't already exist.
- opts = (os.getenv("MPV") or "mpv").split()
- opts.extend(["--no-terminal", "--force-window", "--input-file=" + FIFO,
+ opts = shlex.split(os.getenv("MPV") or "mpv")
+ opts.extend(["--no-terminal", "--force-window", "--input-ipc-server=" + SOCK,
"--"])
opts.extend(files)