Skip to content

Commit ef391c7

Browse files
committed
Add quit callback.
1 parent 3bcb82a commit ef391c7

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

python_mpv_jsonipc.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ class WindowsSocket(threading.Thread):
4242
Data is automatically encoded and decoded as JSON. The callback
4343
function will be called for each inbound message.
4444
"""
45-
def __init__(self, ipc_socket, callback=None):
45+
def __init__(self, ipc_socket, callback=None, quit_callback=None):
4646
"""Create the wrapper.
4747
4848
*ipc_socket* is the pipe name. (Not including \\\\.\\pipe\\)
4949
*callback(json_data)* is the function for recieving events.
5050
"""
5151
ipc_socket = "\\\\.\\pipe\\" + ipc_socket
5252
self.callback = callback
53+
self.quit_callback = quit_callback
5354

5455
access = _winapi.GENERIC_READ | _winapi.GENERIC_WRITE
5556
limit = 5 # Connection may fail at first. Try 5 times.
@@ -102,7 +103,8 @@ def run(self):
102103
self.callback(json_data)
103104
data = b''
104105
except EOFError:
105-
pass
106+
if self.quit_callback:
107+
self.quit_callback()
106108

107109
class UnixSocket(threading.Thread):
108110
"""
@@ -111,14 +113,15 @@ class UnixSocket(threading.Thread):
111113
Data is automatically encoded and decoded as JSON. The callback
112114
function will be called for each inbound message.
113115
"""
114-
def __init__(self, ipc_socket, callback=None):
116+
def __init__(self, ipc_socket, callback=None, quit_callback=None):
115117
"""Create the wrapper.
116118
117119
*ipc_socket* is the path to the socket.
118120
*callback(json_data)* is the function for recieving events.
119121
"""
120122
self.ipc_socket = ipc_socket
121123
self.callback = callback
124+
self.quit_callback = quit_callback
122125
self.socket = socket.socket(socket.AF_UNIX)
123126
self.socket.connect(self.ipc_socket)
124127

@@ -157,6 +160,8 @@ def run(self):
157160
json_data = json.loads(item)
158161
self.callback(json_data)
159162
data = b''
163+
if self.quit_callback:
164+
self.quit_callback()
160165

161166
class MPVProcess:
162167
"""
@@ -236,7 +241,7 @@ class MPVInter:
236241
"""
237242
Low-level interface to MPV. Does NOT manage an mpv process. (Internal)
238243
"""
239-
def __init__(self, ipc_socket, callback=None):
244+
def __init__(self, ipc_socket, callback=None, quit_callback=None):
240245
"""Create the wrapper.
241246
242247
*ipc_socket* is the path to the Unix/Linux socket or name of the Windows pipe.
@@ -247,10 +252,11 @@ def __init__(self, ipc_socket, callback=None):
247252
Socket = WindowsSocket
248253

249254
self.callback = callback
255+
self.quit_callback = quit_callback
250256
if self.callback is None:
251257
self.callback = lambda event, data: None
252258

253-
self.socket = Socket(ipc_socket, self.event_callback)
259+
self.socket = Socket(ipc_socket, self.event_callback, self.quit_callback)
254260
self.socket.start()
255261
self.command_id = 1
256262
self.rid_lock = threading.Lock()
@@ -352,7 +358,8 @@ class MPV:
352358
Please note that if you are using a really old MPV version, a fallback command
353359
list is used. Not all commands may actually work when this fallback is used.
354360
"""
355-
def __init__(self, start_mpv=True, ipc_socket=None, mpv_location=None, log_handler=None, loglevel=None, **kwargs):
361+
def __init__(self, start_mpv=True, ipc_socket=None, mpv_location=None,
362+
log_handler=None, loglevel=None, quit_callback=None, **kwargs):
356363
"""
357364
Create the interface to MPV and process instance.
358365
@@ -391,7 +398,7 @@ def __init__(self, start_mpv=True, ipc_socket=None, mpv_location=None, log_handl
391398
else:
392399
raise MPVError("MPV process retry limit reached.")
393400

394-
self.mpv_inter = MPVInter(ipc_socket, self._callback)
401+
self.mpv_inter = MPVInter(ipc_socket, self._callback, quit_callback)
395402
self.properties = set(x.replace("-", "_") for x in self.command("get_property", "property-list"))
396403
try:
397404
command_list = [x["name"] for x in self.command("get_property", "command-list")]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy