summaryrefslogtreecommitdiffstats
path: root/W.hs
diff options
context:
space:
mode:
authorDon Stewart <dons@cse.unsw.edu.au>2007-03-07 06:50:07 +0100
committerDon Stewart <dons@cse.unsw.edu.au>2007-03-07 06:50:07 +0100
commit813264ca0d49d271b919f57a3f6d830dc8034ec8 (patch)
tree3e149d3f8cd0b5a0b147c076376c4638afecb204 /W.hs
parentd26576a1d446ce4e1c71a068bd7fde5d59a278f3 (diff)
downloadmetatile-813264ca0d49d271b919f57a3f6d830dc8034ec8.tar
metatile-813264ca0d49d271b919f57a3f6d830dc8034ec8.zip
refactoring. less code
darcs-hash:20070307055007-9c5c1-2dc9d077773b25f22954c41856e670f3b5583e9f
Diffstat (limited to 'W.hs')
-rw-r--r--W.hs49
1 files changed, 21 insertions, 28 deletions
diff --git a/W.hs b/W.hs
index 5bacfea..deed0e6 100644
--- a/W.hs
+++ b/W.hs
@@ -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