-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpvd.hy
40 lines (36 loc) · 1.43 KB
/
mpvd.hy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(import argparse mpv mpd threading signal
[mpd.server [Server]]
[protocol [commands control status]])
(require [hy.contrib.walk [let]])
;; The socketserver needs to be closed from a different thread. This
;; thread blocks until a signal is received and closes both the mpv
;; connection and the socket server.
(defclass CleanupThread [threading.Thread]
(defn --init-- [self socket-server mpv]
(setv self.server socket-server)
(setv self.mpv mpv)
(setv self.lock (threading.Semaphore 0))
(signal.signal signal.SIGINT
(fn [signal frame] (self.lock.release)))
(.--init-- threading.Thread self))
(defn run [self]
(self.lock.acquire)
(self.server.shutdown)
(self.mpv.shutdown)))
(defn start-server [addr port path]
(let [mpv-conn (mpv.Connection path)
handler (fn [cmd] (commands.call mpv-conn cmd))]
(with [server (Server (, addr port) handler)]
(.start (CleanupThread server mpv-conn))
(server.serve-forever))))
(defmain [&rest args]
(let [parser (argparse.ArgumentParser)]
(parser.add-argument "PATH" :type string
:help "path to mpv IPC server")
(parser.add-argument "-p" :type int :metavar "PORT"
:default 6600 :help "TCP port used by the MPD server")
(parser.add-argument "-a" :type string :metavar "ADDR"
:default "localhost" :help "Address the MPD server binds to")
(let [args (parser.parse-args)]
(start-server args.a args.p args.PATH)))
0)