summaryrefslogtreecommitdiffhomepage
path: root/src/lib/Xmobar/Plugins/XMonadLog.hs
blob: a4f17bb3fcfd414f4e6eb05d924f559485946f94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
{-# LANGUAGE CPP #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.StdinReader
-- Copyright   :  (c) Spencer Janssen
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Spencer Janssen <spencerjanssen@gmail.com>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A plugin to display information from _XMONAD_LOG, specified at
-- http://code.haskell.org/XMonadContrib/XMonad/Hooks/DynamicLog.hs
--
-----------------------------------------------------------------------------

module Xmobar.Plugins.XMonadLog (XMonadLog(..)) where

import Control.Monad
import Graphics.X11
import Graphics.X11.Xlib.Extras
import Xmobar.Run.Commands
#ifdef UTF8
#undef UTF8
import Codec.Binary.UTF8.String as UTF8
#define UTF8
#endif
import Foreign.C (CChar)
import Xmobar.Utils (nextEvent')
import Xmobar.Actions (stripActions)

data XMonadLog = XMonadLog
               | UnsafeXMonadLog
               | XPropertyLog String
               | UnsafeXPropertyLog String
               | NamedXPropertyLog String String
               | UnsafeNamedXPropertyLog String String
    deriving (Read, Show)

instance Exec XMonadLog where
    alias XMonadLog = "XMonadLog"
    alias UnsafeXMonadLog = "UnsafeXMonadLog"
    alias (XPropertyLog atom) = atom
    alias (NamedXPropertyLog _ name) = name
    alias (UnsafeXPropertyLog atom) = atom
    alias (UnsafeNamedXPropertyLog _ name) = name

    start x cb = do
        let atom = case x of
                XMonadLog -> "_XMONAD_LOG"
                UnsafeXMonadLog -> "_XMONAD_LOG"
                XPropertyLog a -> a
                UnsafeXPropertyLog a -> a
                NamedXPropertyLog a _ -> a
                UnsafeNamedXPropertyLog a _ -> a
            sanitize = case x of
                UnsafeXMonadLog -> id
                UnsafeXPropertyLog _ -> id
                UnsafeNamedXPropertyLog _ _ -> id
                _ -> stripActions

        d <- openDisplay ""
        xlog <- internAtom d atom False

        root  <- rootWindow d (defaultScreen d)
        selectInput d root propertyChangeMask

        let update = do
                        mwp <- getWindowProperty8 d xlog root
                        maybe (return ()) (cb . sanitize . decodeCChar) mwp

        update

        allocaXEvent $ \ep -> forever $ do
            nextEvent' d ep
            e <- getEvent ep
            case e of
                PropertyEvent { ev_atom = a } | a ==  xlog -> update
                _ -> return ()

        return ()

decodeCChar :: [CChar] -> String
#ifdef UTF8
#undef UTF8
decodeCChar = UTF8.decode . map fromIntegral
#define UTF8
#else
decodeCChar = map (toEnum . fromIntegral)
#endif