diff options
Diffstat (limited to 'W.hs')
-rw-r--r-- | W.hs | 49 |
1 files changed, 21 insertions, 28 deletions
@@ -20,64 +20,42 @@ import System.IO import Graphics.X11.Xlib import Control.Monad.State --- -- | WState, the window manager state. -- Just the display, width, height and a window list --- data WState = WState { display :: Display , screenWidth :: !Int , screenHeight :: !Int - , windows :: Windows + , windows :: !Windows } type Windows = [Window] -- | The W monad, a StateT transformer over IO encapuslating the window -- manager state --- -newtype W a = W (StateT WState IO a) +newtype W a = W { unW :: StateT WState IO a } deriving (Functor, Monad, MonadIO) -- | Run the W monad, given a chunk of W monad code, and an initial state -- Return the result, and final state --- -runW :: W a -> WState -> IO (a, WState) -runW (W m) = runStateT m +runW :: WState -> W a -> IO (a, WState) +runW st a = runStateT (unW a) st -withIO :: (forall b. (a -> IO b) -> IO b) -> (a -> W c) -> W c -withIO f g = do - s <- W get - (y, t) <- io (f (flip runW s . g)) - W (put t) - return y - --- -- | Lift an IO action into the W monad --- io :: IO a -> W a io = liftIO --- -- | Lift an IO action into the W monad, discarding any result --- io_ :: IO a -> W () io_ f = liftIO f >> return () --- --- | A 'trace' for the W monad --- +-- | A 'trace' for the W monad. Logs a string to stderr. The result may +-- be found in your .xsession-errors file trace :: String -> W () trace msg = io $ do hPutStrLn stderr msg hFlush stderr --- --- | Run an action forever --- -forever :: (Monad m) => m a -> m b -forever a = a >> forever a - -- --------------------------------------------------------------------- -- Getting at the window manager state @@ -104,3 +82,18 @@ setWindows x = W (modify (\s -> s {windows = x})) -- | Modify the current window list modifyWindows :: (Windows -> Windows) -> W () modifyWindows f = W (modify (\s -> s {windows = f (windows s)})) + +-- --------------------------------------------------------------------- +-- Generic utilities + +-- | Run an action forever +forever :: (Monad m) => m a -> m b +forever a = a >> forever a + +-- | Add an element onto the end of a list +snoc :: [a] -> a -> [a] +snoc xs x = xs ++ [x] + +-- | Rotate a list one element +rotate [] = [] +rotate (x:xs) = xs `snoc` x |