{-# LANGUAGE LambdaCase #-}
module XMonad.Layout.IndependentScreens (
VirtualWorkspace, PhysicalWorkspace,
VirtualWindowSpace, PhysicalWindowSpace,
workspaces',
withScreen, withScreens,
onCurrentScreen,
marshallPP,
whenCurrentOn,
countScreens,
workspacesOn, screenOnMonitor,
workspaceOnScreen, focusWindow', doFocus', focusScreen, focusWorkspace, nthWorkspace, withWspOnScreen,
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
import XMonad.Actions.OnScreen (viewOnScreen)
type VirtualWorkspace = WorkspaceId
type PhysicalWorkspace = WorkspaceId
type PhysicalWindowSpace = WindowSpace
type VirtualWindowSpace = WindowSpace
marshall :: ScreenId -> VirtualWorkspace -> PhysicalWorkspace
marshall :: ScreenId -> [Char] -> [Char]
marshall (S Int
sc) [Char]
vws = Int -> [Char]
forall a. Show a => a -> [Char]
show Int
sc [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Char
'_'Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:[Char]
vws
unmarshall :: PhysicalWorkspace -> (ScreenId, VirtualWorkspace)
unmarshallS :: PhysicalWorkspace -> ScreenId
unmarshallW :: PhysicalWorkspace -> VirtualWorkspace
unmarshall :: [Char] -> (ScreenId, [Char])
unmarshall = ((Int -> ScreenId
S (Int -> ScreenId) -> ([Char] -> Int) -> [Char] -> ScreenId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Int
forall a. Read a => [Char] -> a
read) ([Char] -> ScreenId)
-> ([Char] -> [Char]) -> ([Char], [Char]) -> (ScreenId, [Char])
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Int -> [Char] -> [Char]
forall a. Int -> [a] -> [a]
drop Int
1) (([Char], [Char]) -> (ScreenId, [Char]))
-> ([Char] -> ([Char], [Char])) -> [Char] -> (ScreenId, [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> [Char] -> ([Char], [Char])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'_')
unmarshallS :: [Char] -> ScreenId
unmarshallS = (ScreenId, [Char]) -> ScreenId
forall a b. (a, b) -> a
fst ((ScreenId, [Char]) -> ScreenId)
-> ([Char] -> (ScreenId, [Char])) -> [Char] -> ScreenId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> (ScreenId, [Char])
unmarshall
unmarshallW :: [Char] -> [Char]
unmarshallW = (ScreenId, [Char]) -> [Char]
forall a b. (a, b) -> b
snd ((ScreenId, [Char]) -> [Char])
-> ([Char] -> (ScreenId, [Char])) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> (ScreenId, [Char])
unmarshall
workspaces' :: XConfig l -> [VirtualWorkspace]
workspaces' :: forall (l :: * -> *). XConfig l -> [[Char]]
workspaces' = [[Char]] -> [[Char]]
forall a. Eq a => [a] -> [a]
nub ([[Char]] -> [[Char]])
-> (XConfig l -> [[Char]]) -> XConfig l -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> [Char]
unmarshallW ([[Char]] -> [[Char]])
-> (XConfig l -> [[Char]]) -> XConfig l -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConfig l -> [[Char]]
forall (l :: * -> *). XConfig l -> [[Char]]
workspaces
withScreen :: ScreenId
-> [VirtualWorkspace]
-> [PhysicalWorkspace]
withScreen :: ScreenId -> [[Char]] -> [[Char]]
withScreen ScreenId
n = ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map (ScreenId -> [Char] -> [Char]
marshall ScreenId
n)
withScreens :: ScreenId
-> [VirtualWorkspace]
-> [PhysicalWorkspace]
withScreens :: ScreenId -> [[Char]] -> [[Char]]
withScreens ScreenId
n [[Char]]
vws = (ScreenId -> [[Char]]) -> [ScreenId] -> [[Char]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (ScreenId -> [[Char]] -> [[Char]]
`withScreen` [[Char]]
vws) [ScreenId
0..ScreenId
nScreenId -> ScreenId -> ScreenId
forall a. Num a => a -> a -> a
-ScreenId
1]
onCurrentScreen :: (PhysicalWorkspace -> WindowSet -> a) -> (VirtualWorkspace -> WindowSet -> a)
onCurrentScreen :: forall a. ([Char] -> WindowSet -> a) -> [Char] -> WindowSet -> a
onCurrentScreen [Char] -> WindowSet -> a
f [Char]
vws WindowSet
ws =
let currentScreenId :: ScreenId
currentScreenId = Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> ScreenId
forall i l a sid sd. Screen i l a sid sd -> sid
W.screen (Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> ScreenId)
-> Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> ScreenId
forall a b. (a -> b) -> a -> b
$ WindowSet
-> Screen [Char] (Layout Word64) Word64 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 [Char] -> WindowSet -> a
f (ScreenId -> [Char] -> [Char]
marshall ScreenId
currentScreenId [Char]
vws) WindowSet
ws
workspaceOnScreen :: ScreenId -> WindowSet -> Maybe PhysicalWorkspace
workspaceOnScreen :: ScreenId -> WindowSet -> Maybe [Char]
workspaceOnScreen ScreenId
screenId WindowSet
ws = Workspace [Char] (Layout Word64) Word64 -> [Char]
forall i l a. Workspace i l a -> i
W.tag (Workspace [Char] (Layout Word64) Word64 -> [Char])
-> (Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace [Char] (Layout Word64) Word64)
-> Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace [Char] (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> [Char])
-> Maybe
(Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail)
-> Maybe [Char]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ScreenId
-> WindowSet
-> Maybe
(Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail)
screenOnMonitor ScreenId
screenId WindowSet
ws
withWspOnScreen :: ScreenId
-> (PhysicalWorkspace -> WindowSet -> WindowSet)
-> WindowSet -> WindowSet
withWspOnScreen :: ScreenId
-> ([Char] -> WindowSet -> WindowSet) -> WindowSet -> WindowSet
withWspOnScreen ScreenId
screenId [Char] -> WindowSet -> WindowSet
operation WindowSet
ws = case ScreenId -> WindowSet -> Maybe [Char]
workspaceOnScreen ScreenId
screenId WindowSet
ws of
Just [Char]
wsp -> [Char] -> WindowSet -> WindowSet
operation [Char]
wsp WindowSet
ws
Maybe [Char]
Nothing -> WindowSet
ws
screenOnMonitor :: ScreenId -> WindowSet -> Maybe WindowScreen
screenOnMonitor :: ScreenId
-> WindowSet
-> Maybe
(Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail)
screenOnMonitor ScreenId
screenId WindowSet
ws = (Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> Bool)
-> [Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail]
-> Maybe
(Screen [Char] (Layout Word64) Word64 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 [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> ScreenId)
-> Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> ScreenId
forall i l a sid sd. Screen i l a sid sd -> sid
W.screen) (WindowSet
-> Screen [Char] (Layout Word64) Word64 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 [Char] (Layout Word64) Word64 ScreenId ScreenDetail
-> [Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail]
-> [Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail]
forall a. a -> [a] -> [a]
: WindowSet
-> [Screen [Char] (Layout Word64) Word64 ScreenId ScreenDetail]
forall i l a sid sd. StackSet i l a sid sd -> [Screen i l a sid sd]
W.visible WindowSet
ws)
focusWindow' :: Window -> WindowSet -> WindowSet
focusWindow' :: Word64 -> WindowSet -> WindowSet
focusWindow' Word64
window WindowSet
ws
| Word64 -> Maybe Word64
forall a. a -> Maybe a
Just Word64
window Maybe Word64 -> Maybe Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== WindowSet -> Maybe Word64
forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek WindowSet
ws = WindowSet
ws
| Bool
otherwise = case Word64 -> WindowSet -> Maybe [Char]
forall a i l s sd. Eq a => a -> StackSet i l a s sd -> Maybe i
W.findTag Word64
window WindowSet
ws of
Just [Char]
tag -> Word64 -> 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 Word64
window (WindowSet -> WindowSet) -> WindowSet -> WindowSet
forall a b. (a -> b) -> a -> b
$ ScreenId -> WindowSet -> WindowSet
focusScreen ([Char] -> ScreenId
unmarshallS [Char]
tag) WindowSet
ws
Maybe [Char]
Nothing -> WindowSet
ws
doFocus' :: ManageHook
doFocus' :: ManageHook
doFocus' = (WindowSet -> WindowSet) -> ManageHook
forall s. (s -> s) -> Query (Endo s)
doF ((WindowSet -> WindowSet) -> ManageHook)
-> (Word64 -> WindowSet -> WindowSet) -> Word64 -> ManageHook
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> WindowSet -> WindowSet
focusWindow' (Word64 -> ManageHook) -> Query Word64 -> ManageHook
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Query Word64
forall r (m :: * -> *). MonadReader r m => m r
ask
focusScreen :: ScreenId -> WindowSet -> WindowSet
focusScreen :: ScreenId -> WindowSet -> WindowSet
focusScreen ScreenId
screenId = ScreenId
-> ([Char] -> WindowSet -> WindowSet) -> WindowSet -> WindowSet
withWspOnScreen ScreenId
screenId [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
focusWorkspace :: WorkspaceId -> WindowSet -> WindowSet
focusWorkspace :: [Char] -> WindowSet -> WindowSet
focusWorkspace [Char]
workspaceId = ScreenId -> [Char] -> WindowSet -> WindowSet
viewOnScreen ([Char] -> ScreenId
unmarshallS [Char]
workspaceId) [Char]
workspaceId
nthWorkspace :: Int -> X (Maybe VirtualWorkspace)
nthWorkspace :: Int -> X (Maybe [Char])
nthWorkspace Int
n = ([[Char]] -> Int -> Maybe [Char]
forall a. [a] -> Int -> Maybe a
!? Int
n) ([[Char]] -> Maybe [Char])
-> (XConfig Layout -> [[Char]]) -> XConfig Layout -> Maybe [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConfig Layout -> [[Char]]
forall (l :: * -> *). XConfig l -> [[Char]]
workspaces' (XConfig Layout -> Maybe [Char])
-> X (XConfig Layout) -> X (Maybe [Char])
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
countScreens :: (MonadIO m, Integral i) => m i
countScreens :: forall (m :: * -> *) i. (MonadIO m, Integral i) => m i
countScreens = ([Rectangle] -> i) -> m [Rectangle] -> m i
forall a b. (a -> b) -> m a -> m b
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 a. IO a -> m a
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
$ [Char] -> IO Display
openDisplay [Char]
"" IO Display -> (Display -> IO [Rectangle]) -> IO [Rectangle]
forall a b. IO a -> (a -> IO b) -> IO b
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 a b c.
(a -> b -> c) -> (Display -> a) -> (Display -> b) -> Display -> c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 IO [Rectangle] -> IO () -> IO [Rectangle]
forall a b. IO a -> IO b -> IO a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
(<*) Display -> IO [Rectangle]
getScreenInfo Display -> IO ()
closeDisplay
marshallPP :: ScreenId -> PP -> PP
marshallPP :: ScreenId -> PP -> PP
marshallPP ScreenId
s PP
pp = PP
pp { ppRename = ppRename pp . unmarshallW
, ppSort = (. workspacesOn s) <$> ppSort pp }
whenCurrentOn :: ScreenId -> PP -> PP
whenCurrentOn :: ScreenId -> PP -> PP
whenCurrentOn ScreenId
s PP
pp = PP
pp
{ ppSort = do
sorter <- ppSort pp
pure $ \case xs :: [Workspace [Char] (Layout Word64) Word64]
xs@(Workspace [Char] (Layout Word64) Word64
x:[Workspace [Char] (Layout Word64) Word64]
_) | [Char] -> ScreenId
unmarshallS (Workspace [Char] (Layout Word64) Word64 -> [Char]
forall i l a. Workspace i l a -> i
W.tag Workspace [Char] (Layout Word64) Word64
x) ScreenId -> ScreenId -> Bool
forall a. Eq a => a -> a -> Bool
== ScreenId
s -> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
sorter [Workspace [Char] (Layout Word64) Word64]
xs
[Workspace [Char] (Layout Word64) Word64]
_ -> []
, ppOrder = \case ([Char]
"":[[Char]]
_) -> [[Char]
"\0"]
[[Char]]
list -> PP -> [[Char]] -> [[Char]]
ppOrder PP
pp [[Char]]
list
, ppOutput = \case [Char]
"\0" -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
[Char]
output -> PP -> [Char] -> IO ()
ppOutput PP
pp [Char]
output
}
workspacesOn :: ScreenId -> [PhysicalWindowSpace] -> [PhysicalWindowSpace]
workspacesOn :: ScreenId
-> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
workspacesOn ScreenId
s = (Workspace [Char] (Layout Word64) Word64 -> Bool)
-> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Workspace [Char] (Layout Word64) Word64
ws -> [Char] -> ScreenId
unmarshallS (Workspace [Char] (Layout Word64) Word64 -> [Char]
forall i l a. Workspace i l a -> i
W.tag Workspace [Char] (Layout Word64) Word64
ws) ScreenId -> ScreenId -> Bool
forall a. Eq a => a -> a -> Bool
== ScreenId
s)
marshallSort :: ScreenId -> ([VirtualWindowSpace] -> [VirtualWindowSpace]) -> ([PhysicalWindowSpace] -> [PhysicalWindowSpace])
marshallSort :: ScreenId
-> ([Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64])
-> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
marshallSort ScreenId
s [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
vSort = [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
pScreens ([Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64])
-> ([Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64])
-> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
vSort ([Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64])
-> ([Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64])
-> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
vScreens where
vScreens :: [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
vScreens = (Workspace [Char] (Layout Word64) Word64
-> Workspace [Char] (Layout Word64) Word64)
-> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
forall a b. (a -> b) -> [a] -> [b]
map Workspace [Char] (Layout Word64) Word64
-> Workspace [Char] (Layout Word64) Word64
unmarshallWindowSpace ([Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64])
-> ([Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64])
-> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScreenId
-> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
workspacesOn ScreenId
s
pScreens :: [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
pScreens = (Workspace [Char] (Layout Word64) Word64
-> Workspace [Char] (Layout Word64) Word64)
-> [Workspace [Char] (Layout Word64) Word64]
-> [Workspace [Char] (Layout Word64) Word64]
forall a b. (a -> b) -> [a] -> [b]
map (ScreenId
-> Workspace [Char] (Layout Word64) Word64
-> Workspace [Char] (Layout Word64) Word64
marshallWindowSpace ScreenId
s)
marshallWindowSpace :: ScreenId -> WindowSpace -> WindowSpace
unmarshallWindowSpace :: WindowSpace -> WindowSpace
marshallWindowSpace :: ScreenId
-> Workspace [Char] (Layout Word64) Word64
-> Workspace [Char] (Layout Word64) Word64
marshallWindowSpace ScreenId
s Workspace [Char] (Layout Word64) Word64
ws = Workspace [Char] (Layout Word64) Word64
ws { W.tag = marshall s (W.tag ws) }
unmarshallWindowSpace :: Workspace [Char] (Layout Word64) Word64
-> Workspace [Char] (Layout Word64) Word64
unmarshallWindowSpace Workspace [Char] (Layout Word64) Word64
ws = Workspace [Char] (Layout Word64) Word64
ws { W.tag = unmarshallW (W.tag ws) }