----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.BluetileCommands
-- Description :  Interface with the [Bluetile](https://hackage.haskell.org/package/bluetile) tiling window manager.
-- Copyright   :  (c) Jan Vornberger 2009
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  jan.vornberger@informatik.uni-oldenburg.de
-- Stability   :  unstable
-- Portability :  not portable
--
-- This is a list of selected commands that can be made available using
-- "XMonad.Hooks.ServerMode" to allow external programs to control
-- the window manager. Bluetile (<http://projects.haskell.org/bluetile/>)
-- uses this to enable its dock application to do things like changing
-- workspaces and layouts.
--
-----------------------------------------------------------------------------

module XMonad.Actions.BluetileCommands (
    -- * Usage
    -- $usage
    bluetileCommands
    ) where

import XMonad
import qualified XMonad.StackSet as W
import System.Exit

-- $usage
--
-- You can use this module with the following in your @xmonad.hs@:
--
-- >    import XMonad.Hooks.ServerMode
-- >    import XMonad.Actions.BluetileCommands
--
-- Then edit your @handleEventHook@:
--
-- > main = xmonad def { handleEventHook = serverModeEventHook' bluetileCommands }
--
-- See the documentation of "XMonad.Hooks.ServerMode" for details on
-- how to actually invoke the commands from external programs.

workspaceCommands :: Int -> X [(String, X ())]
workspaceCommands :: Int -> X [([Char], X ())]
workspaceCommands Int
sid = (XConf -> [[Char]]) -> X [[Char]]
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (XConfig Layout -> [[Char]]
forall (l :: * -> *). XConfig l -> [[Char]]
workspaces (XConfig Layout -> [[Char]])
-> (XConf -> XConfig Layout) -> XConf -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> XConfig Layout
config) X [[Char]]
-> ([[Char]] -> X [([Char], X ())]) -> X [([Char], X ())]
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[[Char]]
spaces -> [([Char], X ())] -> X [([Char], X ())]
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return
                            [( [Char]
"greedyView" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
i,
                                Int -> X ()
activateScreen Int
sid X () -> X () -> X ()
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (WindowSet -> WindowSet) -> X ()
windows ([Char] -> WindowSet -> WindowSet
forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
W.greedyView [Char]
i))
                                | [Char]
i <- [[Char]]
spaces ]

layoutCommands :: Int -> [(String, X ())]
layoutCommands :: Int -> [([Char], X ())]
layoutCommands Int
sid = [ ([Char]
"layout floating"    , Int -> X ()
activateScreen Int
sid X () -> X () -> X ()
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                                                    JumpToLayout -> X ()
forall a. Message a => a -> X ()
sendMessage ([Char] -> JumpToLayout
JumpToLayout [Char]
"Floating"))
                     , ([Char]
"layout tiled1"      , Int -> X ()
activateScreen Int
sid X () -> X () -> X ()
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                                                    JumpToLayout -> X ()
forall a. Message a => a -> X ()
sendMessage ([Char] -> JumpToLayout
JumpToLayout [Char]
"Tiled1"))
                     , ([Char]
"layout tiled2"      , Int -> X ()
activateScreen Int
sid X () -> X () -> X ()
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                                                    JumpToLayout -> X ()
forall a. Message a => a -> X ()
sendMessage ([Char] -> JumpToLayout
JumpToLayout [Char]
"Tiled2"))
                     , ([Char]
"layout fullscreen"  , Int -> X ()
activateScreen Int
sid X () -> X () -> X ()
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                                                    JumpToLayout -> X ()
forall a. Message a => a -> X ()
sendMessage ([Char] -> JumpToLayout
JumpToLayout [Char]
"Fullscreen"))
                     ]

masterAreaCommands :: Int -> [(String, X ())]
masterAreaCommands :: Int -> [([Char], X ())]
masterAreaCommands Int
sid = [ ([Char]
"increase master n", Int -> X ()
activateScreen Int
sid X () -> X () -> X ()
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                                                    IncMasterN -> X ()
forall a. Message a => a -> X ()
sendMessage (Int -> IncMasterN
IncMasterN Int
1))
                         , ([Char]
"decrease master n", Int -> X ()
activateScreen Int
sid X () -> X () -> X ()
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                                                    IncMasterN -> X ()
forall a. Message a => a -> X ()
sendMessage (Int -> IncMasterN
IncMasterN (-Int
1)))
                         ]

quitCommands :: [(String, X ())]
quitCommands :: [([Char], X ())]
quitCommands = [ ([Char]
"quit bluetile", IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO ()
forall a. IO a
exitSuccess)
               , ([Char]
"quit bluetile and start metacity", [Char] -> Bool -> X ()
restart [Char]
"metacity" Bool
False)
               ]

bluetileCommands :: X [(String, X ())]
bluetileCommands :: X [([Char], X ())]
bluetileCommands = do
    let restartCommand :: [([Char], X ())]
restartCommand = [ ([Char]
"restart bluetile", [Char] -> Bool -> X ()
restart [Char]
"bluetile" Bool
True) ]
    wscmds0 <- Int -> X [([Char], X ())]
workspaceCommands Int
0
    wscmds1 <- workspaceCommands 1
    return $ restartCommand
                ++ wscmds0 ++ layoutCommands 0 ++ masterAreaCommands 0 ++ quitCommands
                ++ wscmds1 ++ layoutCommands 1 ++ masterAreaCommands 1 ++ quitCommands

activateScreen :: Int -> X ()
activateScreen :: Int -> X ()
activateScreen Int
sid = ScreenId -> X (Maybe [Char])
screenWorkspace (Int -> ScreenId
S Int
sid) X (Maybe [Char]) -> (Maybe [Char] -> X ()) -> X ()
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [Char] -> ([Char] -> X ()) -> X ())
-> ([Char] -> X ()) -> Maybe [Char] -> X ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Maybe [Char] -> ([Char] -> X ()) -> X ()
forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust ((WindowSet -> WindowSet) -> X ()
windows ((WindowSet -> WindowSet) -> X ())
-> ([Char] -> WindowSet -> WindowSet) -> [Char] -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> WindowSet -> WindowSet
forall s i l a sd.
(Eq s, Eq i) =>
i -> StackSet i l a s sd -> StackSet i l a s sd
W.view)