summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDon Stewart <dons@cse.unsw.edu.au>2007-03-07 08:42:48 +0100
committerDon Stewart <dons@cse.unsw.edu.au>2007-03-07 08:42:48 +0100
commit04c83e1bfa8192d4fa6a157490cb1f01a1ed55cc (patch)
tree75d6c397750fdddb61a2e755a57930397f716afa
parent9648fb47e199e60f3fcf85de93e2e8bb6da4b3ab (diff)
downloadmetatile-04c83e1bfa8192d4fa6a157490cb1f01a1ed55cc.tar
metatile-04c83e1bfa8192d4fa6a157490cb1f01a1ed55cc.zip
fmt. and use a Map for keycode lookup
darcs-hash:20070307074248-9c5c1-e7bd70d5c7822120cdfded97be4da59ca18d5218
-rw-r--r--Main.hs65
-rw-r--r--W.hs4
2 files changed, 30 insertions, 39 deletions
diff --git a/Main.hs b/Main.hs
index 9e1800e..70e2574 100644
--- a/Main.hs
+++ b/Main.hs
@@ -16,6 +16,8 @@
import Data.Bits hiding (rotate)
import Data.List
+import qualified Data.Map as M
+
import System.IO
import System.Process (runCommand)
import System.Exit
@@ -30,16 +32,16 @@ import W
--
-- The keys list
--
-keys :: [(KeyMask, KeySym, W ())]
-keys =
- [ (mod1Mask .|. shiftMask, xK_Return, spawn "xterm")
- , (mod1Mask, xK_p, spawn "exe=`dmenu_path | dmenu` && exec $exe")
- , (controlMask, xK_space, spawn "gmrun")
- , (mod1Mask, xK_Tab, focus 1)
- , (mod1Mask, xK_j, focus 1)
- , (mod1Mask, xK_k, focus (-1))
- , (mod1Mask .|. shiftMask, xK_c, kill)
- , (mod1Mask .|. shiftMask, xK_q, io $ exitWith ExitSuccess)
+keys :: M.Map (KeyMask, KeySym) (W ())
+keys = M.fromList
+ [ ((mod1Mask .|. shiftMask, xK_Return), spawn "xterm")
+ , ((mod1Mask, xK_p ), spawn "exe=`dmenu_path | dmenu` && exec $exe")
+ , ((controlMask, xK_space ), spawn "gmrun")
+ , ((mod1Mask, xK_Tab ), focus 1)
+ , ((mod1Mask, xK_j ), focus 1)
+ , ((mod1Mask, xK_k ), focus (-1))
+ , (mod1Mask .|. shiftMask, xK_c ), kill)
+ , ((mod1Mask .|. shiftMask, xK_q ), io $ exitWith ExitSuccess)
]
--
@@ -69,41 +71,28 @@ main = do
e <- io $ allocaXEvent $ \ev -> nextEvent dpy ev >> getEvent ev
handle e
---
--- | grabkeys. Register key commands
---
-registerKeys :: Display -> Window -> W ()
-registerKeys dpy root =
- forM_ keys $ \(mod, sym, _) -> do
- kc <- io (keysymToKeycode dpy sym)
- io $ grabKey dpy kc mod root True grabModeAsync grabModeAsync
+ -- register keys
+ registerKeys dpy root = forM_ (M.keys keys) $ \(mod,sym) -> io $ do
+ kc <- keysymToKeycode dpy sym
+ grabKey dpy kc mod root True grabModeAsync grabModeAsync
--
-- The event handler
--
handle :: Event -> W ()
-handle (MapRequestEvent {window = w}) = manage w
-
-handle (DestroyWindowEvent {window = w}) = do
- ws <- gets windows
- when (elem w ws) (unmanage w)
-
-handle (UnmapEvent {window = w}) = do
- ws <- gets windows
- when (elem w ws) (unmanage w)
+handle (MapRequestEvent {window = w}) = manage w
+handle (DestroyWindowEvent {window = w}) = unmanage w
+handle (UnmapEvent {window = w}) = unmanage w
handle (KeyEvent {event_type = t, state = mod, keycode = code})
| t == keyPress = do
dpy <- gets display
sym <- io $ keycodeToKeysym dpy code 0
- case filter (\(mod', sym', _) -> mod == mod' && sym == sym') keys of
- [] -> return ()
- ((_, _, act):_) -> act
+ M.lookup (mod,sym) keys
handle e@(ConfigureRequestEvent {}) = do
dpy <- gets display
- io $ configureWindow dpy (window e) (value_mask e) $
- WindowChanges
+ io $ configureWindow dpy (window e) (value_mask e) $ WindowChanges
{ wcX = x e
, wcY = y e
, wcWidth = width e
@@ -151,11 +140,13 @@ manage w = do
-- | unmanage, a window no longer exists, remove it from the stack
unmanage :: Window -> W ()
unmanage w = do
- dpy <- gets display
- io $ do grabServer dpy
- sync dpy False
- ungrabServer dpy
- withWindows $ filter (/= w)
+ ws <- gets windows
+ when (w `elem` ws) $ do
+ dpy <- gets display
+ io $ do grabServer dpy
+ sync dpy False
+ ungrabServer dpy
+ withWindows $ filter (/= w)
-- | focus. focus to window at offset 'n' in list.
-- The currently focused window is always the head of the list
diff --git a/W.hs b/W.hs
index 61f13d8..b440c21 100644
--- a/W.hs
+++ b/W.hs
@@ -16,9 +16,9 @@
module W where
-import System.IO
-import Graphics.X11.Xlib
import Control.Monad.State
+import System.IO
+import Graphics.X11.Xlib (Display,Window)
-- | WState, the window manager state.
-- Just the display, width, height and a window list