Xmonad/Mutable state in contrib modules or xmonad.hs

From HaskellWiki
< Xmonad
Revision as of 19:39, 30 January 2011 by DanielSchoepe (talk | contribs) (Data.IORef: minor formatting)

Jump to: navigation, search

This page describes how to keep track of mutable state in a module in xmonad-contrib or in the configuration.

General options


Layouts can keep track of their state placing the information in the data type that is an instance of LayoutClass. The various methods of this typeclass allow one to update this information by supplying a new value of this type as a return value.


A more general way is to store data using Data.IORef. To create an IORef, one uses newIORef; the returned value can then be passed to functions for reading from or writing to it.

Here is an example for a keybinding that keeps track of how many times it has been pressed:

import Data.IORef
updatingBinding :: IORef Integer -> X ()
updatingBinding ref = do
  val <- io $ readIORef ref
  io $ writeIORef ref (val+1)
  spawn $ "xmessage I have been pressed " ++ show val ++ " times before!"

To use this, it needs to be passed an IORef as a parameter:

import Data.IORef
import XMonad.Util.EZConfig
main = do
  ref <- newIORef 0 -- this is the initival value
  xmonad defaultConfig `additionalKeysP` [("M-S-t", updatingBinding ref)]

Only available in darcs


This module allows you to store data in xmonad's internal state eliminating the need to explicitly pass around the IORef value seen in the previous example.

For information on how to use it, refer to the module's documentation.