-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Hooks.SetWMName
-- Description :  Set the WM name to a given string.
-- Copyright   :  © 2007 Ivan Tarasov <Ivan.Tarasov@gmail.com>
-- License     :  BSD
--
-- Maintainer  :  Ivan.Tarasov@gmail.com
-- Stability   :  experimental
-- Portability :  unportable
--
-- Sets the WM name to a given string, so that it could be detected using
-- _NET_SUPPORTING_WM_CHECK protocol.
--
-- May be useful for making Java GUI programs work, just set WM name to "LG3D"
-- and use Java 1.6u1 (1.6.0_01-ea-b03 works for me) or later.
--
-- To your @~\/.xmonad\/xmonad.hs@ file, add the following line:
--
-- > import XMonad.Hooks.SetWMName
--
-- Then edit your @startupHook@:
--
-- > startupHook = setWMName "LG3D"
--
-- For details on the problems with running Java GUI programs in non-reparenting
-- WMs, see <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6429775> and
-- related bugs.
--
-- Setting WM name to "compiz" does not solve the problem, because of yet
-- another bug in AWT code (related to insets). For LG3D insets are explicitly
-- set to 0, while for other WMs the insets are \"guessed\" and the algorithm
-- fails miserably by guessing absolutely bogus values.
--
-- For detailed instructions on editing your hooks, see
-- "XMonad.Doc.Extending#4".
-----------------------------------------------------------------------------

module XMonad.Hooks.SetWMName (
      setWMName
    , getWMName
    )
  where

import Foreign.C.Types (CChar)
import Foreign.Marshal.Alloc (alloca)

import XMonad
import XMonad.Prelude (fromJust, join, listToMaybe, maybeToList, nub, ord)

-- | sets WM name
setWMName :: String -> X ()
setWMName :: String -> X ()
setWMName String
name = do
    Dimension
atom_NET_SUPPORTING_WM_CHECK <- X Dimension
netSupportingWMCheckAtom
    Dimension
atom_NET_WM_NAME <- String -> X Dimension
getAtom String
"_NET_WM_NAME"
    Dimension
atom_NET_SUPPORTED_ATOM <- String -> X Dimension
getAtom String
"_NET_SUPPORTED"
    Dimension
atom_UTF8_STRING <- String -> X Dimension
getAtom String
"UTF8_STRING"

    Dimension
root <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Dimension
theRoot
    Dimension
supportWindow <- X Dimension
getSupportWindow
    Display
dpy <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Display
display
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ do
        -- _NET_SUPPORTING_WM_CHECK atom of root and support windows refers to the support window
        forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\Dimension
w -> Display
-> Dimension -> Dimension -> Dimension -> CInt -> [CLong] -> IO ()
changeProperty32 Display
dpy Dimension
w Dimension
atom_NET_SUPPORTING_WM_CHECK Dimension
wINDOW CInt
propModeReplace [forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
supportWindow]) [Dimension
root, Dimension
supportWindow]
        -- set WM_NAME in supportWindow (now only accepts latin1 names to eliminate dependency on utf8 encoder)
        Display
-> Dimension -> Dimension -> Dimension -> CInt -> [CChar] -> IO ()
changeProperty8 Display
dpy Dimension
supportWindow Dimension
atom_NET_WM_NAME Dimension
atom_UTF8_STRING CInt
propModeReplace (String -> [CChar]
latin1StringToCCharList String
name)
        -- declare which _NET protocols are supported (append to the list if it exists)
        [CLong]
supportedList <- forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe a -> [a]
maybeToList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Display -> Dimension -> Dimension -> IO (Maybe [CLong])
getWindowProperty32 Display
dpy Dimension
atom_NET_SUPPORTED_ATOM Dimension
root
        Display
-> Dimension -> Dimension -> Dimension -> CInt -> [CLong] -> IO ()
changeProperty32 Display
dpy Dimension
root Dimension
atom_NET_SUPPORTED_ATOM Dimension
aTOM CInt
propModeReplace (forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
atom_NET_SUPPORTING_WM_CHECK forall a. a -> [a] -> [a]
: forall a b. (Integral a, Num b) => a -> b
fromIntegral Dimension
atom_NET_WM_NAME forall a. a -> [a] -> [a]
: [CLong]
supportedList)
  where
    latin1StringToCCharList :: String -> [CChar]
    latin1StringToCCharList :: String -> [CChar]
latin1StringToCCharList = forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord)

netSupportingWMCheckAtom :: X Atom
netSupportingWMCheckAtom :: X Dimension
netSupportingWMCheckAtom = String -> X Dimension
getAtom String
"_NET_SUPPORTING_WM_CHECK"

getSupportWindow :: X Window
getSupportWindow :: X Dimension
getSupportWindow = forall a. (Display -> X a) -> X a
withDisplay forall a b. (a -> b) -> a -> b
$ \Display
dpy -> do
    Dimension
atom_NET_SUPPORTING_WM_CHECK <- X Dimension
netSupportingWMCheckAtom
    Dimension
root <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Dimension
theRoot
    Maybe CLong
supportWindow <- (forall a. [a] -> Maybe a
listToMaybe forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Display -> Dimension -> Dimension -> IO (Maybe [CLong])
getWindowProperty32 Display
dpy Dimension
atom_NET_SUPPORTING_WM_CHECK Dimension
root)
    Maybe Dimension -> X Dimension
validateWindow (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Integral a, Num b) => a -> b
fromIntegral Maybe CLong
supportWindow)
  where
    validateWindow :: Maybe Window -> X Window
    validateWindow :: Maybe Dimension -> X Dimension
validateWindow Maybe Dimension
w = do
        Bool
valid <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False) Dimension -> X Bool
isValidWindow Maybe Dimension
w
        if Bool
valid then
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Maybe a -> a
fromJust Maybe Dimension
w
          else
            X Dimension
createSupportWindow

    -- is there a better way to check the validity of the window?
    isValidWindow :: Window -> X Bool
    isValidWindow :: Dimension -> X Bool
isValidWindow Dimension
w = forall a. (Display -> X a) -> X a
withDisplay forall a b. (a -> b) -> a -> b
$ \Display
dpy -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr WindowAttributes
p -> do
        CInt
status <- Display -> Dimension -> Ptr WindowAttributes -> IO CInt
xGetWindowAttributes Display
dpy Dimension
w Ptr WindowAttributes
p
        forall (m :: * -> *) a. Monad m => a -> m a
return (CInt
status forall a. Eq a => a -> a -> Bool
/= CInt
0)

    -- this code was translated from C (see OpenBox WM, screen.c)
    createSupportWindow :: X Window
    createSupportWindow :: X Dimension
createSupportWindow = forall a. (Display -> X a) -> X a
withDisplay forall a b. (a -> b) -> a -> b
$ \Display
dpy -> do
        Dimension
root <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Dimension
theRoot
        let visual :: Visual
visual = Display -> Dimension -> Visual
defaultVisual Display
dpy (Display -> Dimension
defaultScreen Display
dpy)  -- should be CopyFromParent (=0), but the constructor is hidden in X11.XLib
        Dimension
window <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ forall a. (Ptr SetWindowAttributes -> IO a) -> IO a
allocaSetWindowAttributes forall a b. (a -> b) -> a -> b
$ \Ptr SetWindowAttributes
winAttrs -> do
            Ptr SetWindowAttributes -> Bool -> IO ()
set_override_redirect Ptr SetWindowAttributes
winAttrs Bool
True         -- WM cannot decorate/move/close this window
            Ptr SetWindowAttributes -> Dimension -> IO ()
set_event_mask Ptr SetWindowAttributes
winAttrs Dimension
propertyChangeMask  -- not sure if this is needed
            let bogusX :: Position
bogusX = -Position
100
                bogusY :: Position
bogusY = -Position
100
              in
                Display
-> Dimension
-> Position
-> Position
-> Dimension
-> Dimension
-> CInt
-> CInt
-> CInt
-> Visual
-> Dimension
-> Ptr SetWindowAttributes
-> IO Dimension
createWindow Display
dpy Dimension
root Position
bogusX Position
bogusY Dimension
1 Dimension
1 CInt
0 CInt
0 CInt
inputOutput Visual
visual (Dimension
cWEventMask forall a. Bits a => a -> a -> a
.|. Dimension
cWOverrideRedirect) Ptr SetWindowAttributes
winAttrs
        forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ Display -> Dimension -> IO ()
mapWindow Display
dpy Dimension
window   -- not sure if this is needed
        forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ Display -> Dimension -> IO ()
lowerWindow Display
dpy Dimension
window -- not sure if this is needed
        forall (m :: * -> *) a. Monad m => a -> m a
return Dimension
window

-- | Get WM name.
getWMName :: X String
getWMName :: X String
getWMName = X Dimension
getSupportWindow forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Query a -> Dimension -> X a
runQuery Query String
title