3 import os, sys, getopt, io, termios
4 import socket, json, pprint
7 def __init__(self, nm):
10 return "<key %s>" % (self.nm,)
12 def __init__(self, cc):
13 super().__init__(repr(cc))
19 K_LEFT = key("K_LEFT")
20 K_RIGHT = key("K_RIGHT")
22 K_DOWN = key("K_DOWN")
23 MOD_SHIFT = key("MOD_SHIFT")
24 MOD_META = key("MOD_META")
25 MOD_CTRL = key("MOD_CTRL")
27 def __init__(self, *, path="/dev/tty"):
28 self.io = io.FileIO(os.open(path, os.O_RDWR | os.O_NOCTTY), "r+")
29 attr = termios.tcgetattr(self.io.fileno())
31 attr[3] &= ~termios.ECHO & ~termios.ICANON
32 termios.tcsetattr(self.io.fileno(), termios.TCSANOW, attr)
36 return None if b == b"" else b[0]
38 _csikeys = {'A': K_UP, 'B': K_DOWN, 'C': K_RIGHT, 'D': K_LEFT}
44 return akey("\x1b"), set()
55 par = (par * 10) + (c - 48)
65 elif chr(c) in self._csikeys:
66 key = self._csikeys[chr(c)]
72 mods.add(self.MOD_SHIFT)
74 mods.add(self.MOD_META)
76 mods.add(self.MOD_CTRL)
79 return akey(chr(c)), [self.MOD_META]
81 return akey(chr(c)), set()
84 termios.tcsetattr(self.io.fileno(), termios.TCSANOW, self.bka)
90 def __exit__(self, *exc):
95 def __init__(self, path):
97 self.sk = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
99 self.obuf = bytearray()
100 self.ibuf = bytearray()
102 def write(self, data):
103 self.obuf.extend(data)
106 while len(self.obuf) > 0:
107 ret = self.sk.send(self.obuf)
108 self.obuf[:ret] = b""
110 def recv(self, hint=1024):
111 data = self.sk.recv(hint)
114 self.ibuf.extend(data)
119 p2 = self.ibuf.find(b'\n', p)
121 ret = bytes(self.ibuf[:p2])
122 self.ibuf[:p2 + 1] = b""
127 def send(self, data):
133 resp = json.loads(self.readline().decode("utf-8"))
138 def runcmd(self, *cmd):
139 self.send(json.dumps({"command": cmd}).encode("utf-8") + b"\n")
140 resp = self.getresp()
141 if "error" not in resp:
142 sys.stderr.write("mpsync: strange response from %s: %r\n" % (self.path, resp))
143 if resp["error"] != "success":
144 sys.stderr.write("mpsync: error response from %s: %r\n" % (self.path, resp))
147 def getprop(self, pname):
148 return self.runcmd("get_property", pname)["data"]
150 def setprop(self, pname, val):
151 self.runcmd("set_property", pname, val)
154 out.write("usage: mpsync [-h] SOCKET...\n")
155 out.write("players: mpv -input-unix-socket SOCKET -pause [ARGS...] FILE\n")
157 opts, args = getopt.getopt(sys.argv[1:], "h")
166 if not os.path.exists(path):
167 sys.stderr.write("mpsync: %s: no such file or directory\n" % path)
172 targets.append(target(path))
179 cmd = json.dumps({"command": cmd}).encode("utf-8") + b"\n"
184 if "error" not in resp:
185 sys.stderr.write("mpsync: strange response from %s: %r\n" % (tgt.path, resp))
186 if resp["error"] != "success":
187 sys.stderr.write("mpsync: error response from %s: %r\n" % (tgt.path, resp))
189 def relseek(ss, offsets):
190 cur = targets[0].getprop("time-pos")
191 for tgt, off in zip(targets, offsets):
192 tgt.setprop("time-pos", cur + ss + off)
195 opos = targets[0].getprop("time-pos")
198 ret.append(tgt.getprop("time-pos") - opos)
202 runcmd("set_property", "hr-seek", "yes")
203 paused = targets[0].getprop("pause")
205 offsets = [0.0] * len(targets)
207 c, mods = tty.readkey()
215 simulcmd("set_property", "pause", paused)
216 elif c == rawtty.K_LEFT and not mods:
217 relseek(-10, offsets)
218 elif c == rawtty.K_RIGHT and not mods:
220 elif c == rawtty.K_UP and not mods:
222 elif c == rawtty.K_DOWN and not mods:
223 relseek(-60, offsets)
224 elif c == rawtty.K_LEFT and mods == {rawtty.MOD_SHIFT}:
226 elif c == rawtty.K_RIGHT and mods == {rawtty.MOD_SHIFT}:
228 elif c == rawtty.K_UP and mods == {rawtty.MOD_SHIFT}:
230 elif c == rawtty.K_DOWN and mods == {rawtty.MOD_SHIFT}:
235 mutemode = (mutemode + 1) % 3
238 tgt.setprop("mute", False)
240 for i, tgt in enumerate(targets):
241 tgt.setprop("mute", i != 0)
244 tgt.setprop("mute", True)
245 targets[0].runcmd("show_text", "Audio mode: %s" % ["All", "One", "None"][mutemode])
247 offsets = getoffsets()
248 for tgt, off in zip(targets, offsets):
249 tgt.runcmd("show_text", "Offset: %f" % off)
254 runcmd("frame_back_step")
257 with rawtty() as tty:
260 except KeyboardInterrupt: