diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2021-01-03 00:55:29 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2021-01-03 16:21:34 +0100 |
commit | 4a3d2a8ddaf4e546df360656bc54b2947bdb890b (patch) | |
tree | f571511e0b74fbd7e78f7e10966a8d6d5d400241 /accounts/gkleen@sif/xmonad/lib/XMonad/Mpv.hs | |
parent | 3b5ab82fa714a0d483a7b90d60f9f7c857646e33 (diff) | |
download | nixos-4a3d2a8ddaf4e546df360656bc54b2947bdb890b.tar nixos-4a3d2a8ddaf4e546df360656bc54b2947bdb890b.tar.gz nixos-4a3d2a8ddaf4e546df360656bc54b2947bdb890b.tar.bz2 nixos-4a3d2a8ddaf4e546df360656bc54b2947bdb890b.tar.xz nixos-4a3d2a8ddaf4e546df360656bc54b2947bdb890b.zip |
gkleen@sif: import
Diffstat (limited to 'accounts/gkleen@sif/xmonad/lib/XMonad/Mpv.hs')
-rw-r--r-- | accounts/gkleen@sif/xmonad/lib/XMonad/Mpv.hs | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/accounts/gkleen@sif/xmonad/lib/XMonad/Mpv.hs b/accounts/gkleen@sif/xmonad/lib/XMonad/Mpv.hs new file mode 100644 index 00000000..e6accdcc --- /dev/null +++ b/accounts/gkleen@sif/xmonad/lib/XMonad/Mpv.hs | |||
@@ -0,0 +1,127 @@ | |||
1 | {-# LANGUAGE DeriveGeneric, OverloadedLists, OverloadedStrings, ViewPatterns, ExistentialQuantification, MultiWayIf #-} | ||
2 | |||
3 | module XMonad.Mpv | ||
4 | ( MpvCommand(..), MpvResponse(..), MpvException(..) | ||
5 | , mpv | ||
6 | , mpvDir | ||
7 | , mpvAll, mpvOne | ||
8 | , mpvResponse | ||
9 | ) where | ||
10 | |||
11 | import Data.Aeson | ||
12 | |||
13 | import Data.Monoid | ||
14 | |||
15 | import Network.Socket hiding (recv) | ||
16 | import Network.Socket.ByteString | ||
17 | |||
18 | import qualified Data.ByteString as BS | ||
19 | import qualified Data.ByteString.Char8 as CBS | ||
20 | import qualified Data.ByteString.Lazy as LBS | ||
21 | |||
22 | import GHC.Generics (Generic) | ||
23 | import Data.Typeable (Typeable) | ||
24 | import Data.String (IsString(..)) | ||
25 | |||
26 | import Control.Exception | ||
27 | |||
28 | import System.IO.Temp (getCanonicalTemporaryDirectory) | ||
29 | |||
30 | import Control.Monad | ||
31 | import Control.Exception (bracket) | ||
32 | import Control.Monad.IO.Class (MonadIO(..)) | ||
33 | |||
34 | import System.FilePath | ||
35 | import System.Directory (getDirectoryContents) | ||
36 | |||
37 | import Data.List | ||
38 | import Data.Either | ||
39 | import Data.Maybe | ||
40 | |||
41 | import Debug.Trace | ||
42 | |||
43 | |||
44 | data MpvCommand | ||
45 | = forall a. ToJSON a => MpvSetProperty String a | ||
46 | | MpvGetProperty String | ||
47 | data MpvResponse | ||
48 | = MpvError String | ||
49 | | MpvSuccess (Maybe Value) | ||
50 | deriving (Read, Show, Generic, Eq) | ||
51 | data MpvException = MpvException String | ||
52 | | MpvNoValue | ||
53 | | MpvNoParse String | ||
54 | deriving (Generic, Typeable, Read, Show) | ||
55 | instance Exception MpvException | ||
56 | |||
57 | |||
58 | instance ToJSON MpvCommand where | ||
59 | toJSON (MpvSetProperty name val) = Array ["set_property", fromString name, toJSON val] | ||
60 | toJSON (MpvGetProperty name) = Array ["get_property", fromString name] | ||
61 | |||
62 | instance FromJSON MpvResponse where | ||
63 | parseJSON = withObject "response object" $ \obj -> do | ||
64 | mval <- obj .:? "data" | ||
65 | err <- obj .: "error" | ||
66 | |||
67 | let ret | ||
68 | | err == "success" = MpvSuccess mval | ||
69 | | otherwise = MpvError err | ||
70 | |||
71 | return ret | ||
72 | |||
73 | mpvSocket :: FilePath -> (Socket -> IO a) -> IO a | ||
74 | mpvSocket sockPath = withSocketsDo . bracket mkSock close | ||
75 | where | ||
76 | mkSock = do | ||
77 | sock <- socket AF_UNIX Stream defaultProtocol | ||
78 | connect sock $ SockAddrUnix (traceId sockPath) | ||
79 | return sock | ||
80 | |||
81 | mpvResponse :: FromJSON v => MpvResponse -> IO v | ||
82 | mpvResponse (MpvError str) = throwIO $ MpvException str | ||
83 | mpvResponse (MpvSuccess Nothing) = throwIO MpvNoValue | ||
84 | mpvResponse (MpvSuccess (Just v)) = case fromJSON v of | ||
85 | Success v' -> return v' | ||
86 | Error str -> throwIO $ MpvNoParse str | ||
87 | |||
88 | mpv :: FilePath -> MpvCommand -> IO MpvResponse | ||
89 | mpv sockPath cmd = mpvSocket sockPath $ \sock -> do | ||
90 | let message = (`BS.append` "\n") . LBS.toStrict . encode $ Object [("command", toJSON cmd)] | ||
91 | traceIO $ show message | ||
92 | sendAll sock message | ||
93 | let recvAll = do | ||
94 | prefix <- recv sock 4096 | ||
95 | if | ||
96 | | (prefix', rest) <- CBS.break (== '\n') prefix | ||
97 | , not (BS.null rest) -> return prefix' | ||
98 | | BS.null prefix -> return prefix | ||
99 | | otherwise -> BS.append prefix <$> recvAll | ||
100 | response <- recvAll | ||
101 | traceIO $ show response | ||
102 | either (ioError . userError) return . traceShowId $ eitherDecodeStrict' response | ||
103 | |||
104 | mpvDir :: Exception e => FilePath -> (FilePath -> [(FilePath, Either e MpvResponse)] -> Maybe MpvCommand) -> IO [(FilePath, Either e MpvResponse)] | ||
105 | mpvDir dir step = do | ||
106 | socks <- filter (".sock" `isSuffixOf`) <$> getDirectoryContents dir | ||
107 | go [] socks | ||
108 | where | ||
109 | go acc [] = return acc | ||
110 | go acc (sock:socks) | ||
111 | | Just cmd <- step sock acc = do | ||
112 | res <- try $ mpv (dir </> sock) cmd | ||
113 | go ((sock, res) : acc) socks | ||
114 | | otherwise = | ||
115 | go acc socks | ||
116 | |||
117 | mpvAll :: FilePath -> MpvCommand -> IO [MpvResponse] | ||
118 | mpvAll dir cmd = do | ||
119 | results <- map snd <$> (mpvDir dir (\_ _ -> Just cmd) :: IO [(FilePath, Either SomeException MpvResponse)]) | ||
120 | mapM (either throwIO return) results | ||
121 | |||
122 | mpvOne :: FilePath -> MpvCommand -> IO (Maybe MpvResponse) | ||
123 | mpvOne dir cmd = listToMaybe . snd . partitionEithers . map snd <$> (mpvDir dir step :: IO [(FilePath, Either SomeException MpvResponse)]) | ||
124 | where | ||
125 | step _ results | ||
126 | | any (isRight . snd) results = Nothing | ||
127 | | otherwise = Just cmd | ||