summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrea Rossato <andrea.rossato@ing.unitn.it>2009-09-25 13:56:55 +0200
committerAndrea Rossato <andrea.rossato@ing.unitn.it>2009-09-25 13:56:55 +0200
commit51b7cdce37a7e0469d02d3aade3a1966907271ef (patch)
tree26c5bda5c4917c6538fbe79feb3c72c4d7560ed3
parent617c0535db5a67726c4e03cb037a5768a59b4387 (diff)
downloadxmobar-51b7cdce37a7e0469d02d3aade3a1966907271ef.tar.gz
xmobar-51b7cdce37a7e0469d02d3aade3a1966907271ef.tar.bz2
clean up a bit the previous patch
Ignore-this: 464ed4ec3e01439d3b927778a916d700c8b9673d darcs-hash:20090925115655-d6583-85df3240b6893297074beff203987ecd9224b14a.gz
-rw-r--r--XUtil.hsc28
1 files changed, 10 insertions, 18 deletions
diff --git a/XUtil.hsc b/XUtil.hsc
index 678ffcb..4d9e707 100644
--- a/XUtil.hsc
+++ b/XUtil.hsc
@@ -40,6 +40,7 @@ import qualified Graphics.X11.Xlib as Xlib (textExtents, textWidth)
import Graphics.X11.Xlib.Extras
import System.Posix.Types (Fd(..))
import System.IO
+import System.IO.Unsafe (unsafePerformIO)
#if defined XFT || defined UTF8
import Foreign.C
import qualified System.IO.UTF8 as UTF8 (readFile,hGetLine)
@@ -189,41 +190,32 @@ initColor dpy c = (initColor' dpy c) `catch`
(const . return $ DynPixel False (blackPixel dpy $ defaultScreen dpy))
type ColorCache = [(String, Color)]
-colorCache :: IO (IORef ColorCache)
-colorCache = newIORef []
+{-# NOINLINE colorCache #-}
+colorCache :: IORef ColorCache
+colorCache = unsafePerformIO $ newIORef []
getCachedColor :: String -> IO (Maybe Color)
-getCachedColor color_name = do
- ref <- colorCache
- result <- lookup color_name `fmap` readIORef ref
- return result
+getCachedColor color_name = lookup color_name `fmap` readIORef colorCache
putCachedColor :: String -> Color -> IO ()
-putCachedColor color_name color_id = do
- ref <- colorCache
- modifyIORef ref $ \cache -> (color_name,color_id) : cache
+putCachedColor name c_id = modifyIORef colorCache $ \c -> (name, c_id) : c
initColor' :: Display -> String -> IO DynPixel
initColor' dpy c = do
+ let colormap = defaultColormap dpy (defaultScreen dpy)
cached_color <- getCachedColor c
- c' <- do
- case cached_color of
+ c' <- case cached_color of
Just col -> return col
_ -> do (c'', _) <- allocNamedColor dpy colormap c
putCachedColor c c''
return c''
return $ DynPixel True (color_pixel c')
- where colormap = defaultColormap dpy (defaultScreen dpy)
withColors :: MonadIO m => Display -> [String] -> ([Pixel] -> m a) -> m a
withColors d cs f = do
+ let cmap = defaultColormap d (defaultScreen d)
ps <- mapM (io . initColor d) cs
- r <- f $ map pixel ps
- -- there is color leak in 'putCachedColor'. might be freed at xmobar shutdown
- -- io $ freeColors d cmap (map pixel $ filter allocated ps) 0
- return r
- where
- cmap = defaultColormap d (defaultScreen d)
+ f $ map pixel ps
-- | Creates a window with the attribute override_redirect set to True.
-- Windows Managers should not touch this kind of windows.