module XMonad.Util.SpawnNamedPipe (
spawnNamedPipe
, getNamedPipe
) where
import XMonad
import XMonad.Util.Run
import System.IO
import qualified XMonad.Util.ExtensibleState as XS
import XMonad.Prelude
import qualified Data.Map as Map
newtype NamedPipes = NamedPipes { NamedPipes -> Map [Char] Handle
pipeMap :: Map.Map String Handle }
deriving (Int -> NamedPipes -> ShowS
[NamedPipes] -> ShowS
NamedPipes -> [Char]
(Int -> NamedPipes -> ShowS)
-> (NamedPipes -> [Char])
-> ([NamedPipes] -> ShowS)
-> Show NamedPipes
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NamedPipes -> ShowS
showsPrec :: Int -> NamedPipes -> ShowS
$cshow :: NamedPipes -> [Char]
show :: NamedPipes -> [Char]
$cshowList :: [NamedPipes] -> ShowS
showList :: [NamedPipes] -> ShowS
Show)
instance ExtensionClass NamedPipes where
initialValue :: NamedPipes
initialValue = Map [Char] Handle -> NamedPipes
NamedPipes Map [Char] Handle
forall k a. Map k a
Map.empty
spawnNamedPipe :: String -> String -> X ()
spawnNamedPipe :: [Char] -> [Char] -> X ()
spawnNamedPipe [Char]
cmd [Char]
name = do
b <- (NamedPipes -> Bool) -> X Bool
forall a (m :: * -> *) b.
(ExtensionClass a, XLike m) =>
(a -> b) -> m b
XS.gets ([Char] -> Map [Char] Handle -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member [Char]
name (Map [Char] Handle -> Bool)
-> (NamedPipes -> Map [Char] Handle) -> NamedPipes -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NamedPipes -> Map [Char] Handle
pipeMap)
unless b $ do
h <- spawnPipe cmd
XS.modify (NamedPipes . Map.insert name h . pipeMap)
getNamedPipe :: String -> X (Maybe Handle)
getNamedPipe :: [Char] -> X (Maybe Handle)
getNamedPipe [Char]
name = (NamedPipes -> Maybe Handle) -> X (Maybe Handle)
forall a (m :: * -> *) b.
(ExtensionClass a, XLike m) =>
(a -> b) -> m b
XS.gets ([Char] -> Map [Char] Handle -> Maybe Handle
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup [Char]
name (Map [Char] Handle -> Maybe Handle)
-> (NamedPipes -> Map [Char] Handle) -> NamedPipes -> Maybe Handle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NamedPipes -> Map [Char] Handle
pipeMap)