{-# LANGUAGE LambdaCase #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.IndependentScreens
-- Description :  Simulate independent sets of workspaces on each screen (dwm-like).
-- Copyright   :  (c) 2009 Daniel Wagner
-- License     :  BSD3
--
-- Maintainer  :  <daniel@wagner-home.com>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Utility functions for simulating independent sets of workspaces on
-- each screen (like dwm's workspace model), using internal tags to
-- distinguish workspaces associated with each screen.
-----------------------------------------------------------------------------

module XMonad.Layout.IndependentScreens (
    -- * Usage
    -- $usage
    VirtualWorkspace, PhysicalWorkspace,
    VirtualWindowSpace, PhysicalWindowSpace,
    workspaces',
    withScreen, withScreens,
    onCurrentScreen,
    marshallPP,
    whenCurrentOn,
    countScreens,
    workspacesOn,
    workspaceOnScreen, focusWindow', focusScreen, nthWorkspace, withWspOnScreen,
    -- * Converting between virtual and physical workspaces
    -- $converting
    marshall, unmarshall, unmarshallS, unmarshallW,
    marshallWindowSpace, unmarshallWindowSpace, marshallSort,
) where

import Control.Arrow ((***))
import Graphics.X11.Xinerama
import XMonad
import XMonad.Hooks.StatusBar.PP
import XMonad.Prelude
import qualified XMonad.StackSet as W

-- $usage
-- You can use this module with the following in your @~\/.xmonad\/xmonad.hs@:
--
-- > import XMonad.Layout.IndependentScreens
--
-- You can define your workspaces by calling @withScreens@:
--
-- > myConfig = def { workspaces = withScreens 2 ["web", "email", "irc"] }
--
-- This will create \"physical\" workspaces with distinct internal names for
-- each (screen, virtual workspace) pair.
--
-- Then edit any keybindings that use the list of workspaces or refer
-- to specific workspace names.  In the default configuration, only
-- the keybindings for changing workspace do this:
--
-- > keyBindings conf = let modm = modMask conf in fromList $
-- >     {- lots of other keybindings -}
-- >     [((m .|. modm, k), windows $ f i)
-- >         | (i, k) <- zip (XMonad.workspaces conf) [xK_1 .. xK_9]
-- >         , (f, m) <- [(W.greedyView, 0), (W.shift, shiftMask)]]
--
-- This should change to
--
-- > keyBindings conf = let modm = modMask conf in fromList $
-- >     {- lots of other keybindings -}
-- >     [((m .|. modm, k), windows $ onCurrentScreen f i)
-- >         | (i, k) <- zip (workspaces' conf) [xK_1 .. xK_9]
-- >         , (f, m) <- [(W.greedyView, 0), (W.shift, shiftMask)]]
--
-- In particular, the analogue of @XMonad.workspaces@ is
-- @workspaces'@, and you can use @onCurrentScreen@ to convert functions
-- of virtual workspaces to functions of physical workspaces, which work
-- by marshalling the virtual workspace name and the currently focused
-- screen into a physical workspace name.
--
-- A complete example abusing many of the functions below is available in the
-- "XMonad.Config.Dmwit" module.

type VirtualWorkspace  = WorkspaceId
type PhysicalWorkspace = WorkspaceId

-- | A 'WindowSpace' whose tags are 'PhysicalWorkspace's.
type PhysicalWindowSpace = WindowSpace
-- | A 'WindowSpace' whose tags are 'VirtualWorkspace's.
type VirtualWindowSpace = WindowSpace

-- $converting
-- You shouldn't need to use the functions below very much. They are used
-- internally. However, in some cases, they may be useful, and so are exported
-- just in case. In general, the \"marshall\" functions convert the convenient
-- form (like \"web\") you would like to use in your configuration file to the
-- inconvenient form (like \"2_web\") that xmonad uses internally. Similarly,
-- the \"unmarshall\" functions convert in the other direction.

marshall :: ScreenId -> VirtualWorkspace -> PhysicalWorkspace
marshall :: ScreenId -> VirtualWorkspace -> VirtualWorkspace
marshall (S Int
sc) VirtualWorkspace
vws = Int -> VirtualWorkspace
forall a. Show a => a -> VirtualWorkspace
show Int
sc VirtualWorkspace -> VirtualWorkspace -> VirtualWorkspace
forall a. [a] -> [a] -> [a]
++ Char
'_'Char -> VirtualWorkspace -> VirtualWorkspace
forall a. a -> [a] -> [a]
:VirtualWorkspace
vws

unmarshall  :: PhysicalWorkspace -> (ScreenId, VirtualWorkspace)
unmarshallS :: PhysicalWorkspace -> ScreenId
unmarshallW :: PhysicalWorkspace -> VirtualWorkspace

unmarshall :: VirtualWorkspace -> (ScreenId, VirtualWorkspace)
unmarshall  = ((Int -> ScreenId
S (Int -> ScreenId)
-> (VirtualWorkspace -> Int) -> VirtualWorkspace -> ScreenId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VirtualWorkspace -> Int
forall a. Read a => VirtualWorkspace -> a
read) (VirtualWorkspace -> ScreenId)
-> (VirtualWorkspace -> VirtualWorkspace)
-> (VirtualWorkspace, VirtualWorkspace)
-> (ScreenId, VirtualWorkspace)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Int -> VirtualWorkspace -> VirtualWorkspace
forall a. Int -> [a] -> [a]
drop Int
1) ((VirtualWorkspace, VirtualWorkspace)
 -> (ScreenId, VirtualWorkspace))
-> (VirtualWorkspace -> (VirtualWorkspace, VirtualWorkspace))
-> VirtualWorkspace
-> (ScreenId, VirtualWorkspace)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool)
-> VirtualWorkspace -> (VirtualWorkspace, VirtualWorkspace)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'_')
unmarshallS :: VirtualWorkspace -> ScreenId
unmarshallS = (ScreenId, VirtualWorkspace) -> ScreenId
forall a b. (a, b) -> a
fst ((ScreenId, VirtualWorkspace) -> ScreenId)
-> (VirtualWorkspace -> (ScreenId, VirtualWorkspace))
-> VirtualWorkspace
-> ScreenId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VirtualWorkspace -> (ScreenId, VirtualWorkspace)
unmarshall
unmarshallW :: VirtualWorkspace -> VirtualWorkspace
unmarshallW = (ScreenId, VirtualWorkspace) -> VirtualWorkspace
forall a b. (a, b) -> b
snd ((ScreenId, VirtualWorkspace) -> VirtualWorkspace)
-> (VirtualWorkspace -> (ScreenId, VirtualWorkspace))
-> VirtualWorkspace
-> VirtualWorkspace
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VirtualWorkspace -> (ScreenId, VirtualWorkspace)
unmarshall

-- | Get a list of all the virtual workspace names.
workspaces' :: XConfig l -> [VirtualWorkspace]
workspaces' :: XConfig l -> [VirtualWorkspace]
workspaces' = [VirtualWorkspace] -> [VirtualWorkspace]
forall a. Eq a => [a] -> [a]
nub ([VirtualWorkspace] -> [VirtualWorkspace])
-> (XConfig l -> [VirtualWorkspace])
-> XConfig l
-> [VirtualWorkspace]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VirtualWorkspace -> VirtualWorkspace)
-> [VirtualWorkspace] -> [VirtualWorkspace]
forall a b. (a -> b) -> [a] -> [b]
map VirtualWorkspace -> VirtualWorkspace
unmarshallW ([VirtualWorkspace] -> [VirtualWorkspace])
-> (XConfig l -> [VirtualWorkspace])
-> XConfig l
-> [VirtualWorkspace]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConfig l -> [VirtualWorkspace]
forall (l :: * -> *). XConfig l -> [VirtualWorkspace]
workspaces

-- | Specify workspace names for each screen
withScreen :: ScreenId            -- ^ The screen to make workspaces for
           -> [VirtualWorkspace]  -- ^ The desired virtual workspace names
           -> [PhysicalWorkspace] -- ^ A list of all internal physical workspace names
withScreen :: ScreenId -> [VirtualWorkspace] -> [VirtualWorkspace]
withScreen ScreenId
n = (VirtualWorkspace -> VirtualWorkspace)
-> [VirtualWorkspace] -> [VirtualWorkspace]
forall a b. (a -> b) -> [a] -> [b]
map (ScreenId -> VirtualWorkspace -> VirtualWorkspace
marshall ScreenId
n)

-- | Make all workspaces across the monitors bear the same names
withScreens :: ScreenId            -- ^ The number of screens to make workspaces for
            -> [VirtualWorkspace]  -- ^ The desired virtual workspace names
            -> [PhysicalWorkspace] -- ^ A list of all internal physical workspace names
withScreens :: ScreenId -> [VirtualWorkspace] -> [VirtualWorkspace]
withScreens ScreenId
n [VirtualWorkspace]
vws = (ScreenId -> [VirtualWorkspace])
-> [ScreenId] -> [VirtualWorkspace]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (ScreenId -> [VirtualWorkspace] -> [VirtualWorkspace]
`withScreen` [VirtualWorkspace]
vws) [ScreenId
0..ScreenId
nScreenId -> ScreenId -> ScreenId
forall a. Num a => a -> a -> a
-ScreenId
1]

-- | Transform a function over physical workspaces into a function over virtual workspaces.
-- This is useful as it allows you to write code without caring about the current screen, i.e. to say "switch to workspace 3"
-- rather than saying "switch to workspace 3 on monitor 3".
onCurrentScreen :: (PhysicalWorkspace -> WindowSet -> a) -> (VirtualWorkspace -> WindowSet -> a)
onCurrentScreen :: (VirtualWorkspace -> WindowSet -> a)
-> VirtualWorkspace -> WindowSet -> a
onCurrentScreen VirtualWorkspace -> WindowSet -> a
f VirtualWorkspace
vws WindowSet
ws =
  let currentScreenId :: ScreenId
currentScreenId = Screen
  VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
-> ScreenId
forall i l a sid sd. Screen i l a sid sd -> sid
W.screen (Screen
   VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
 -> ScreenId)
-> Screen
     VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
-> ScreenId
forall a b. (a -> b) -> a -> b
$ WindowSet
-> Screen
     VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
W.current WindowSet
ws
   in VirtualWorkspace -> WindowSet -> a
f (ScreenId -> VirtualWorkspace -> VirtualWorkspace
marshall ScreenId
currentScreenId VirtualWorkspace
vws) WindowSet
ws

-- | Get the workspace currently active on a given screen
workspaceOnScreen :: ScreenId -> WindowSet -> Maybe PhysicalWorkspace
workspaceOnScreen :: ScreenId -> WindowSet -> Maybe VirtualWorkspace
workspaceOnScreen ScreenId
screenId WindowSet
ws = Workspace VirtualWorkspace (Layout Window) Window
-> VirtualWorkspace
forall i l a. Workspace i l a -> i
W.tag (Workspace VirtualWorkspace (Layout Window) Window
 -> VirtualWorkspace)
-> (Screen
      VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
    -> Workspace VirtualWorkspace (Layout Window) Window)
-> Screen
     VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
-> VirtualWorkspace
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen
  VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
-> Workspace VirtualWorkspace (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (Screen
   VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
 -> VirtualWorkspace)
-> Maybe
     (Screen
        VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail)
-> Maybe VirtualWorkspace
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ScreenId
-> WindowSet
-> Maybe
     (Screen
        VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail)
screenOnMonitor ScreenId
screenId WindowSet
ws

-- | Generate WindowSet transformation by providing a given function with the workspace active on a given screen.
-- This may for example be used to shift a window to another screen as follows:
--
-- > windows $ withWspOnScreen 1 W.shift
--
withWspOnScreen :: ScreenId                               -- ^ The screen to run on
         -> (PhysicalWorkspace -> WindowSet -> WindowSet) -- ^ The transformation that will be passed the workspace currently active on there
         -> WindowSet -> WindowSet
withWspOnScreen :: ScreenId
-> (VirtualWorkspace -> WindowSet -> WindowSet)
-> WindowSet
-> WindowSet
withWspOnScreen ScreenId
screenId VirtualWorkspace -> WindowSet -> WindowSet
operation WindowSet
ws = case ScreenId -> WindowSet -> Maybe VirtualWorkspace
workspaceOnScreen ScreenId
screenId WindowSet
ws of
    Just VirtualWorkspace
wsp -> VirtualWorkspace -> WindowSet -> WindowSet
operation VirtualWorkspace
wsp WindowSet
ws
    Maybe VirtualWorkspace
Nothing -> WindowSet
ws

-- | Get the workspace that is active on a given screen.
screenOnMonitor :: ScreenId -> WindowSet -> Maybe (W.Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail)
screenOnMonitor :: ScreenId
-> WindowSet
-> Maybe
     (Screen
        VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail)
screenOnMonitor ScreenId
screenId WindowSet
ws = (Screen
   VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
 -> Bool)
-> [Screen
      VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail]
-> Maybe
     (Screen
        VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((ScreenId
screenId ScreenId -> ScreenId -> Bool
forall a. Eq a => a -> a -> Bool
==) (ScreenId -> Bool)
-> (Screen
      VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
    -> ScreenId)
-> Screen
     VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen
  VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
-> ScreenId
forall i l a sid sd. Screen i l a sid sd -> sid
W.screen) (WindowSet
-> Screen
     VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
W.current WindowSet
ws Screen
  VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail
-> [Screen
      VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail]
-> [Screen
      VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail]
forall a. a -> [a] -> [a]
: WindowSet
-> [Screen
      VirtualWorkspace (Layout Window) Window ScreenId ScreenDetail]
forall i l a sid sd. StackSet i l a sid sd -> [Screen i l a sid sd]
W.visible WindowSet
ws)

-- | Focus a window, switching workspace on the correct Xinerama screen if neccessary.
focusWindow' :: Window -> WindowSet -> WindowSet
focusWindow' :: Window -> WindowSet -> WindowSet
focusWindow' Window
window WindowSet
ws
  | Window -> Maybe Window
forall a. a -> Maybe a
Just Window
window Maybe Window -> Maybe Window -> Bool
forall a. Eq a => a -> a -> Bool
== WindowSet -> Maybe Window
forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek WindowSet
ws = WindowSet
ws
  | Bool
otherwise = case Window -> WindowSet -> Maybe VirtualWorkspace
forall a i l s sd. Eq a => a -> StackSet i l a s sd -> Maybe i
W.findTag Window
window WindowSet
ws of
      Just VirtualWorkspace
tag -> Window -> WindowSet -> WindowSet
forall s a i l sd.
(Eq s, Eq a, Eq i) =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.focusWindow Window
window (WindowSet -> WindowSet) -> WindowSet -> WindowSet
forall a b. (a -> b) -> a -> b
$ ScreenId -> WindowSet -> WindowSet
focusScreen (VirtualWorkspace -> ScreenId
unmarshallS VirtualWorkspace
tag) WindowSet
ws
      Maybe VirtualWorkspace
Nothing -> WindowSet
ws

-- | Focus a given screen.
focusScreen :: ScreenId -> WindowSet -> WindowSet
focusScreen :: ScreenId -> WindowSet -> WindowSet
focusScreen ScreenId
screenId = ScreenId
-> (VirtualWorkspace -> WindowSet -> WindowSet)
-> WindowSet
-> WindowSet
withWspOnScreen ScreenId
screenId VirtualWorkspace -> 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

-- | Get the nth virtual workspace
nthWorkspace :: Int -> X (Maybe VirtualWorkspace)
nthWorkspace :: Int -> X (Maybe VirtualWorkspace)
nthWorkspace Int
n = ([VirtualWorkspace] -> Int -> Maybe VirtualWorkspace
forall a. [a] -> Int -> Maybe a
!? Int
n) ([VirtualWorkspace] -> Maybe VirtualWorkspace)
-> (XConfig Layout -> [VirtualWorkspace])
-> XConfig Layout
-> Maybe VirtualWorkspace
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConfig Layout -> [VirtualWorkspace]
forall (l :: * -> *). XConfig l -> [VirtualWorkspace]
workspaces' (XConfig Layout -> Maybe VirtualWorkspace)
-> X (XConfig Layout) -> X (Maybe VirtualWorkspace)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (XConf -> XConfig Layout) -> X (XConfig Layout)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> XConfig Layout
config

-- | In case you don't know statically how many screens there will be, you can call this in main before starting xmonad.  For example, part of my config reads
--
-- > main = do
-- >   nScreens <- countScreens
-- >   xmonad $ def {
-- >     ...
-- >     workspaces = withScreens nScreens (workspaces def),
-- >     ...
-- >     }
--
countScreens :: (MonadIO m, Integral i) => m i
countScreens :: m i
countScreens = ([Rectangle] -> i) -> m [Rectangle] -> m i
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Rectangle] -> i
forall i a. Num i => [a] -> i
genericLength (m [Rectangle] -> m i)
-> (IO [Rectangle] -> m [Rectangle]) -> IO [Rectangle] -> m i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO [Rectangle] -> m [Rectangle]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [Rectangle] -> m i) -> IO [Rectangle] -> m i
forall a b. (a -> b) -> a -> b
$ VirtualWorkspace -> IO Display
openDisplay VirtualWorkspace
"" IO Display -> (Display -> IO [Rectangle]) -> IO [Rectangle]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (IO [Rectangle] -> IO () -> IO [Rectangle])
-> (Display -> IO [Rectangle])
-> (Display -> IO ())
-> Display
-> IO [Rectangle]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 IO [Rectangle] -> IO () -> IO [Rectangle]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
(<*) Display -> IO [Rectangle]
getScreenInfo Display -> IO ()
closeDisplay

-- | This turns a pretty-printer into one that is aware of the independent screens. The
-- converted pretty-printer first filters out physical workspaces on other screens, then
-- converts all the physical workspaces on this screen to their virtual names.
-- Note that 'ppSort' still operates on physical (marshalled) workspace names,
-- otherwise functions from "XMonad.Util.WorkspaceCompare" wouldn't work.
-- If you need to sort on virtual names, see 'marshallSort'.
--
-- For example, if you have have two bars on the left and right screens, respectively, and @pp@ is
-- a pretty-printer, you could apply 'marshallPP' when creating a @StatusBarConfig@ from "XMonad.Hooks.StatusBar".
--
-- A sample config looks like this:
--
-- > mySBL = statusBarProp "xmobar" $ pure (marshallPP (S 0) pp)
-- > mySBR = statusBarProp "xmobar" $ pure (marshallPP (S 1) pp)
-- > main = xmonad $ withEasySB (mySBL <> mySBR) defToggleStrutsKey def
--
marshallPP :: ScreenId -> PP -> PP
marshallPP :: ScreenId -> PP -> PP
marshallPP ScreenId
s PP
pp = PP
pp { ppRename :: VirtualWorkspace
-> Workspace VirtualWorkspace (Layout Window) Window
-> VirtualWorkspace
ppRename = PP
-> VirtualWorkspace
-> Workspace VirtualWorkspace (Layout Window) Window
-> VirtualWorkspace
ppRename PP
pp (VirtualWorkspace
 -> Workspace VirtualWorkspace (Layout Window) Window
 -> VirtualWorkspace)
-> (VirtualWorkspace -> VirtualWorkspace)
-> VirtualWorkspace
-> Workspace VirtualWorkspace (Layout Window) Window
-> VirtualWorkspace
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VirtualWorkspace -> VirtualWorkspace
unmarshallW
                     , ppSort :: X ([Workspace VirtualWorkspace (Layout Window) Window]
   -> [Workspace VirtualWorkspace (Layout Window) Window])
ppSort = (([Workspace VirtualWorkspace (Layout Window) Window]
 -> [Workspace VirtualWorkspace (Layout Window) Window])
-> ([Workspace VirtualWorkspace (Layout Window) Window]
    -> [Workspace VirtualWorkspace (Layout Window) Window])
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScreenId
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
workspacesOn ScreenId
s) (([Workspace VirtualWorkspace (Layout Window) Window]
  -> [Workspace VirtualWorkspace (Layout Window) Window])
 -> [Workspace VirtualWorkspace (Layout Window) Window]
 -> [Workspace VirtualWorkspace (Layout Window) Window])
-> X ([Workspace VirtualWorkspace (Layout Window) Window]
      -> [Workspace VirtualWorkspace (Layout Window) Window])
-> X ([Workspace VirtualWorkspace (Layout Window) Window]
      -> [Workspace VirtualWorkspace (Layout Window) Window])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PP
-> X ([Workspace VirtualWorkspace (Layout Window) Window]
      -> [Workspace VirtualWorkspace (Layout Window) Window])
ppSort PP
pp }

-- | Take a pretty-printer and turn it into one that only runs when the current
-- workspace is one associated with the given screen. The way this works is a
-- bit hacky, so beware: the 'ppOutput' field of the input will not be invoked
-- if either of the following conditions is met:
--
-- 1. The 'ppSort' of the input returns an empty list (when not given one).
--
-- 2. The 'ppOrder' of the input returns the exact string @\"\\0\"@.
--
-- For example, you can use this to create a pipe which tracks the title of the
-- window currently focused on a given screen (even if the screen is not
-- current) by doing something like this:
--
-- > ppFocus s = whenCurrentOn s def
-- >     { ppOrder  = \(_:_:title:_) -> [title]
-- >     , ppOutput = appendFile ("focus" ++ show s) . (++ "\n")
-- >     }
--
-- Sequence a few of these pretty-printers to get a log hook that keeps each
-- screen's title up-to-date.
whenCurrentOn :: ScreenId -> PP -> PP
whenCurrentOn :: ScreenId -> PP -> PP
whenCurrentOn ScreenId
s PP
pp = PP
pp
    { ppSort :: X ([Workspace VirtualWorkspace (Layout Window) Window]
   -> [Workspace VirtualWorkspace (Layout Window) Window])
ppSort = do
        [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
sorter <- PP
-> X ([Workspace VirtualWorkspace (Layout Window) Window]
      -> [Workspace VirtualWorkspace (Layout Window) Window])
ppSort PP
pp
        ([Workspace VirtualWorkspace (Layout Window) Window]
 -> [Workspace VirtualWorkspace (Layout Window) Window])
-> X ([Workspace VirtualWorkspace (Layout Window) Window]
      -> [Workspace VirtualWorkspace (Layout Window) Window])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Workspace VirtualWorkspace (Layout Window) Window]
  -> [Workspace VirtualWorkspace (Layout Window) Window])
 -> X ([Workspace VirtualWorkspace (Layout Window) Window]
       -> [Workspace VirtualWorkspace (Layout Window) Window]))
-> ([Workspace VirtualWorkspace (Layout Window) Window]
    -> [Workspace VirtualWorkspace (Layout Window) Window])
-> X ([Workspace VirtualWorkspace (Layout Window) Window]
      -> [Workspace VirtualWorkspace (Layout Window) Window])
forall a b. (a -> b) -> a -> b
$ \case xs :: [Workspace VirtualWorkspace (Layout Window) Window]
xs@(Workspace VirtualWorkspace (Layout Window) Window
x:[Workspace VirtualWorkspace (Layout Window) Window]
_) | VirtualWorkspace -> ScreenId
unmarshallS (Workspace VirtualWorkspace (Layout Window) Window
-> VirtualWorkspace
forall i l a. Workspace i l a -> i
W.tag Workspace VirtualWorkspace (Layout Window) Window
x) ScreenId -> ScreenId -> Bool
forall a. Eq a => a -> a -> Bool
== ScreenId
s -> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
sorter [Workspace VirtualWorkspace (Layout Window) Window]
xs
                     [Workspace VirtualWorkspace (Layout Window) Window]
_ -> []

    , ppOrder :: [VirtualWorkspace] -> [VirtualWorkspace]
ppOrder  = \case (VirtualWorkspace
"":[VirtualWorkspace]
_) -> [VirtualWorkspace
"\0"] -- we got passed no workspaces; this is the signal from ppSort that this is a boring case
                       [VirtualWorkspace]
list   -> PP -> [VirtualWorkspace] -> [VirtualWorkspace]
ppOrder PP
pp [VirtualWorkspace]
list

    , ppOutput :: VirtualWorkspace -> IO ()
ppOutput = \case VirtualWorkspace
"\0"   -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure () -- we got passed the signal from ppOrder that this is a boring case
                       VirtualWorkspace
output -> PP -> VirtualWorkspace -> IO ()
ppOutput PP
pp VirtualWorkspace
output
    }

-- | Filter workspaces that are on a given screen.
workspacesOn :: ScreenId -> [PhysicalWindowSpace] -> [PhysicalWindowSpace]
workspacesOn :: ScreenId
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
workspacesOn ScreenId
s = (Workspace VirtualWorkspace (Layout Window) Window -> Bool)
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Workspace VirtualWorkspace (Layout Window) Window
ws -> VirtualWorkspace -> ScreenId
unmarshallS (Workspace VirtualWorkspace (Layout Window) Window
-> VirtualWorkspace
forall i l a. Workspace i l a -> i
W.tag Workspace VirtualWorkspace (Layout Window) Window
ws) ScreenId -> ScreenId -> Bool
forall a. Eq a => a -> a -> Bool
== ScreenId
s)

-- | @vSort@ is a function that sorts 'VirtualWindowSpace's with virtual names.
-- @marshallSort s vSort@ is a function which sorts 'PhysicalWindowSpace's with virtual names,
-- but keeps only the 'WindowSpace'\'s on screen @s@.
--
-- NOTE: @vSort@ operating on virtual names comes with some caveats, see
-- <https://github.com/xmonad/xmonad-contrib/issues/420 this issue> for
-- more information. You can use 'marshallSort' like in the following example:
--
-- === __Example__
--
-- > pp' :: ScreenId -> PP -> PP
-- > pp' s pp = (marshallPP s pp) { ppSort = fmap (marshallSort s) (ppSort pp) }
-- >
-- > mySBL = statusBarProp "xmobar" $ pure (pp' (S 0) pp)
-- > mySBR = statusBarProp "xmobar" $ pure (pp' (S 1) pp)
-- > main = xmonad $ withEasySB (mySBL <> mySBR) defToggleStrutsKey def
--
-- In this way, you have a custom virtual names sort on top of 'marshallPP'.
marshallSort :: ScreenId -> ([VirtualWindowSpace] -> [VirtualWindowSpace]) -> ([PhysicalWindowSpace] -> [PhysicalWindowSpace])
marshallSort :: ScreenId
-> ([Workspace VirtualWorkspace (Layout Window) Window]
    -> [Workspace VirtualWorkspace (Layout Window) Window])
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
marshallSort ScreenId
s [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
vSort = [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
pScreens ([Workspace VirtualWorkspace (Layout Window) Window]
 -> [Workspace VirtualWorkspace (Layout Window) Window])
-> ([Workspace VirtualWorkspace (Layout Window) Window]
    -> [Workspace VirtualWorkspace (Layout Window) Window])
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
vSort ([Workspace VirtualWorkspace (Layout Window) Window]
 -> [Workspace VirtualWorkspace (Layout Window) Window])
-> ([Workspace VirtualWorkspace (Layout Window) Window]
    -> [Workspace VirtualWorkspace (Layout Window) Window])
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
vScreens where
    vScreens :: [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
vScreens    = (Workspace VirtualWorkspace (Layout Window) Window
 -> Workspace VirtualWorkspace (Layout Window) Window)
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
forall a b. (a -> b) -> [a] -> [b]
map Workspace VirtualWorkspace (Layout Window) Window
-> Workspace VirtualWorkspace (Layout Window) Window
unmarshallWindowSpace ([Workspace VirtualWorkspace (Layout Window) Window]
 -> [Workspace VirtualWorkspace (Layout Window) Window])
-> ([Workspace VirtualWorkspace (Layout Window) Window]
    -> [Workspace VirtualWorkspace (Layout Window) Window])
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScreenId
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
workspacesOn ScreenId
s
    pScreens :: [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
pScreens    = (Workspace VirtualWorkspace (Layout Window) Window
 -> Workspace VirtualWorkspace (Layout Window) Window)
-> [Workspace VirtualWorkspace (Layout Window) Window]
-> [Workspace VirtualWorkspace (Layout Window) Window]
forall a b. (a -> b) -> [a] -> [b]
map (ScreenId
-> Workspace VirtualWorkspace (Layout Window) Window
-> Workspace VirtualWorkspace (Layout Window) Window
marshallWindowSpace ScreenId
s)

-- | Convert the tag of the 'WindowSpace' from a 'VirtualWorkspace' to a 'PhysicalWorkspace'.
marshallWindowSpace   :: ScreenId -> WindowSpace -> WindowSpace
-- | Convert the tag of the 'WindowSpace' from a 'PhysicalWorkspace' to a 'VirtualWorkspace'.
unmarshallWindowSpace :: WindowSpace -> WindowSpace

marshallWindowSpace :: ScreenId
-> Workspace VirtualWorkspace (Layout Window) Window
-> Workspace VirtualWorkspace (Layout Window) Window
marshallWindowSpace ScreenId
s Workspace VirtualWorkspace (Layout Window) Window
ws = Workspace VirtualWorkspace (Layout Window) Window
ws { tag :: VirtualWorkspace
W.tag = ScreenId -> VirtualWorkspace -> VirtualWorkspace
marshall ScreenId
s  (Workspace VirtualWorkspace (Layout Window) Window
-> VirtualWorkspace
forall i l a. Workspace i l a -> i
W.tag Workspace VirtualWorkspace (Layout Window) Window
ws) }
unmarshallWindowSpace :: Workspace VirtualWorkspace (Layout Window) Window
-> Workspace VirtualWorkspace (Layout Window) Window
unmarshallWindowSpace Workspace VirtualWorkspace (Layout Window) Window
ws = Workspace VirtualWorkspace (Layout Window) Window
ws { tag :: VirtualWorkspace
W.tag = VirtualWorkspace -> VirtualWorkspace
unmarshallW (Workspace VirtualWorkspace (Layout Window) Window
-> VirtualWorkspace
forall i l a. Workspace i l a -> i
W.tag Workspace VirtualWorkspace (Layout Window) Window
ws) }