2020-04-03 10:57:24 +00:00
|
|
|
#!/usr/bin/env python3
|
2013-07-28 20:44:16 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
This script emulates "unique application" functionality on Linux. When starting
|
|
|
|
playback with this script, it will try to reuse an already running instance of
|
2014-02-05 18:01:06 +00:00
|
|
|
mpv (but only if that was started with umpv). Other mpv instances (not started
|
|
|
|
by umpv) are ignored, and the script doesn't know about them.
|
2013-07-28 20:44:16 +00:00
|
|
|
|
2014-02-05 18:01:06 +00:00
|
|
|
This only takes filenames as arguments. Custom options can't be used; the script
|
|
|
|
interprets them as filenames. If mpv is already running, the files passed to
|
|
|
|
umpv are appended to mpv's internal playlist. If a file does not exist or is
|
|
|
|
otherwise not playable, mpv will skip the playlist entry when attempting to
|
|
|
|
play it (from the GUI perspective, it's silently ignored).
|
2013-07-28 20:44:16 +00:00
|
|
|
|
|
|
|
If mpv isn't running yet, this script will start mpv and let it control the
|
2014-02-05 18:04:35 +00:00
|
|
|
current terminal. It will not write output to stdout/stderr, because this
|
|
|
|
will typically just fill ~/.xsession-errors with garbage.
|
2013-07-28 20:44:16 +00:00
|
|
|
|
|
|
|
mpv will terminate if there are no more files to play, and running the umpv
|
|
|
|
script after that will start a new mpv instance.
|
|
|
|
|
2014-09-20 11:08:32 +00:00
|
|
|
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
|
|
|
|
_if_ the script starts mpv. If mpv is not started by the script (i.e. mpv
|
|
|
|
is already running), this will be ignored.
|
2013-07-28 20:44:16 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2020-03-27 23:38:29 +00:00
|
|
|
import socket
|
2013-07-28 20:44:16 +00:00
|
|
|
import errno
|
|
|
|
import subprocess
|
2014-04-29 00:16:18 +00:00
|
|
|
import string
|
2013-07-28 20:44:16 +00:00
|
|
|
|
|
|
|
files = sys.argv[1:]
|
2014-04-29 00:16:18 +00:00
|
|
|
|
2014-09-20 12:01:03 +00:00
|
|
|
# this is the same method mpv uses to decide this
|
2014-04-29 00:16:18 +00:00
|
|
|
def is_url(filename):
|
2014-09-20 12:01:03 +00:00
|
|
|
parts = filename.split("://", 1)
|
2014-04-29 00:16:18 +00:00
|
|
|
if len(parts) < 2:
|
|
|
|
return False
|
|
|
|
# protocol prefix has no special characters => it's an URL
|
2014-09-20 12:01:03 +00:00
|
|
|
allowed_symbols = string.ascii_letters + string.digits + '_'
|
2014-04-29 00:16:18 +00:00
|
|
|
prefix = parts[0]
|
2014-09-20 12:01:03 +00:00
|
|
|
return all(map(lambda c: c in allowed_symbols, prefix))
|
2014-04-29 00:16:18 +00:00
|
|
|
|
2013-07-28 20:44:16 +00:00
|
|
|
# make them absolute; also makes them safe against interpretation as options
|
2014-04-29 00:16:18 +00:00
|
|
|
def make_abs(filename):
|
|
|
|
if not is_url(filename):
|
2014-04-29 00:21:59 +00:00
|
|
|
return os.path.abspath(filename)
|
|
|
|
return filename
|
2014-04-29 00:16:18 +00:00
|
|
|
files = [make_abs(f) for f in files]
|
2013-07-28 20:44:16 +00:00
|
|
|
|
2020-03-27 23:38:29 +00:00
|
|
|
SOCK = os.path.join(os.getenv("HOME"), ".umpv_socket")
|
2013-07-28 20:44:16 +00:00
|
|
|
|
2020-03-27 23:38:29 +00:00
|
|
|
sock = None
|
2013-07-28 20:44:16 +00:00
|
|
|
try:
|
2020-03-27 23:38:29 +00:00
|
|
|
sock = socket.socket(socket.AF_UNIX)
|
|
|
|
sock.connect(SOCK)
|
|
|
|
except socket.error as e:
|
|
|
|
if e.errno == errno.ECONNREFUSED:
|
|
|
|
sock = None
|
|
|
|
pass # abandoned socket
|
2013-07-28 20:44:16 +00:00
|
|
|
elif e.errno == errno.ENOENT:
|
2020-03-27 23:38:29 +00:00
|
|
|
sock = None
|
2013-07-28 20:44:16 +00:00
|
|
|
pass # doesn't exist
|
|
|
|
else:
|
|
|
|
raise e
|
|
|
|
|
2020-03-27 23:38:29 +00:00
|
|
|
if sock:
|
2013-07-28 20:44:16 +00:00
|
|
|
# Unhandled race condition: what if mpv is terminating right now?
|
|
|
|
for f in files:
|
|
|
|
# escape: \ \n "
|
|
|
|
f = f.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n")
|
|
|
|
f = "\"" + f + "\""
|
2020-04-03 10:57:24 +00:00
|
|
|
sock.send(("raw loadfile " + f + " append\n").encode("utf-8"))
|
2013-07-28 20:44:16 +00:00
|
|
|
else:
|
2020-03-27 23:38:29 +00:00
|
|
|
# Let mpv recreate socket if it doesn't already exist.
|
2013-07-28 20:44:16 +00:00
|
|
|
|
2014-09-20 11:08:32 +00:00
|
|
|
opts = (os.getenv("MPV") or "mpv").split()
|
2020-03-27 23:38:29 +00:00
|
|
|
opts.extend(["--no-terminal", "--force-window", "--input-ipc-server=" + SOCK,
|
2014-09-20 11:08:32 +00:00
|
|
|
"--"])
|
2014-04-28 23:23:44 +00:00
|
|
|
opts.extend(files)
|
|
|
|
|
|
|
|
subprocess.check_call(opts)
|