From 36fe458e23e119cd91c09f6ec66fe487eebec839 Mon Sep 17 00:00:00 2001 From: Adam Vogt Date: Tue, 23 Jun 2009 07:25:37 +0200 Subject: From A.Topicspace split functions for storing strings with root to U.StringProp Ignore-this: 543b172fbefa9feded94d792d01921c4 These functions will be used to send strings for execution by command line, in xmonad-eval darcs-hash:20090623052537-1499c-d985ff1de9431dd80a88ae87015e5e4c1b27aa46.gz --- XMonad/Util/StringProp.hs | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 XMonad/Util/StringProp.hs (limited to 'XMonad/Util') diff --git a/XMonad/Util/StringProp.hs b/XMonad/Util/StringProp.hs new file mode 100644 index 0000000..2cf26d7 --- /dev/null +++ b/XMonad/Util/StringProp.hs @@ -0,0 +1,56 @@ +----------------------------------------------------------------------------- +-- | +-- Module : XMonad.Util.StringProp +-- Copyright : (c) Nicolas Pouillard 2009 +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Nicolas Pouillard +-- Stability : unstable +-- Portability : unportable +-- +-- Internal utility functions for storing Strings with the root window. +-- +-- Used for global state like IORefs with string keys, but more latency, +-- persistent between xmonad restarts. + +module XMonad.Util.StringProp ( + StringProp, + getStringProp, setStringProp, + getStringListProp, setStringListProp, + ) where + +import XMonad +import Control.Monad(liftM) +import Control.Applicative((<$>)) +import Foreign.C.String (castCCharToChar,castCharToCChar) + +type StringProp = String + +withStringProp :: (MonadIO m) => StringProp -> Display -> (Window -> Atom -> m b) -> m b +withStringProp prop dpy f = do + rootw <- io $ rootWindow dpy $ defaultScreen dpy + a <- io $ internAtom dpy prop False + f rootw a + +-- | Set the value of a string property. +setStringProp :: (MonadIO m) => Display -> StringProp -> [Char] -> m () +setStringProp dpy prop string = + withStringProp prop dpy $ \rootw a -> + io $ changeProperty8 dpy rootw a a propModeReplace $ map castCharToCChar string + +-- | Get the name of a string property and returns it as a 'Maybe'. +getStringProp :: (MonadIO m) => Display -> StringProp -> m (Maybe [Char]) +getStringProp dpy prop = + withStringProp prop dpy $ \rootw a -> do + p <- io $ getWindowProperty8 dpy a rootw + return $ map castCCharToChar <$> p + +-- | Given a property name, returns its contents as a list. It uses the empty +-- list as default value. +getStringListProp :: (MonadIO m) => Display -> StringProp -> m [String] +getStringListProp dpy prop = maybe [] words `liftM` getStringProp dpy prop + +-- | Given a property name and a list, sets the value of this property with +-- the list given as argument. +setStringListProp :: (MonadIO m) => Display -> StringProp -> [String] -> m () +setStringListProp dpy prop str = setStringProp dpy prop (unwords str) -- cgit v1.2.3