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 String Handle
pipeMap :: Map.Map String Handle }
deriving (Int -> NamedPipes -> ShowS
[NamedPipes] -> ShowS
NamedPipes -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NamedPipes] -> ShowS
$cshowList :: [NamedPipes] -> ShowS
show :: NamedPipes -> String
$cshow :: NamedPipes -> String
showsPrec :: Int -> NamedPipes -> ShowS
$cshowsPrec :: Int -> NamedPipes -> ShowS
Show)
instance ExtensionClass NamedPipes where
initialValue :: NamedPipes
initialValue = Map String Handle -> NamedPipes
NamedPipes forall k a. Map k a
Map.empty
spawnNamedPipe :: String -> String -> X ()
spawnNamedPipe :: String -> String -> X ()
spawnNamedPipe String
cmd String
name = do
Bool
b <- forall a (m :: * -> *) b.
(ExtensionClass a, XLike m) =>
(a -> b) -> m b
XS.gets (forall k a. Ord k => k -> Map k a -> Bool
Map.member String
name forall b c a. (b -> c) -> (a -> b) -> a -> c
. NamedPipes -> Map String Handle
pipeMap)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
b forall a b. (a -> b) -> a -> b
$ do
Handle
h <- forall (m :: * -> *). MonadIO m => String -> m Handle
spawnPipe String
cmd
forall a (m :: * -> *).
(ExtensionClass a, XLike m) =>
(a -> a) -> m ()
XS.modify (Map String Handle -> NamedPipes
NamedPipes forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert String
name Handle
h forall b c a. (b -> c) -> (a -> b) -> a -> c
. NamedPipes -> Map String Handle
pipeMap)
getNamedPipe :: String -> X (Maybe Handle)
getNamedPipe :: String -> X (Maybe Handle)
getNamedPipe String
name = forall a (m :: * -> *) b.
(ExtensionClass a, XLike m) =>
(a -> b) -> m b
XS.gets (forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup String
name forall b c a. (b -> c) -> (a -> b) -> a -> c
. NamedPipes -> Map String Handle
pipeMap)