{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- --------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Operations
-- Copyright   :  (c) Spencer Janssen 2007
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  dons@cse.unsw.edu.au
-- Stability   :  unstable
-- Portability :  not portable, mtl, posix
--
-- Operations. A module for functions that don't cleanly fit anywhere else.
--
-----------------------------------------------------------------------------

module XMonad.Operations (
    -- * Manage One Window
    manage, unmanage, killWindow, kill, isClient,
    setInitialProperties, setWMState, setWindowBorderWithFallback,
    hide, reveal, tileWindow,
    setTopFocus, focus, isFixedSizeOrTransient,

    -- * Manage Windows
    windows, refresh, rescreen, modifyWindowSet, windowBracket, windowBracket_, clearEvents, getCleanedScreenInfo,
    withFocused, withUnfocused,

    -- * Keyboard and Mouse
    cleanMask, extraModifiers,
    mouseDrag, mouseMoveWindow, mouseResizeWindow,
    setButtonGrab, setFocusX, cacheNumlockMask, mkGrabs, unGrab,

    -- * Messages
    sendMessage, broadcastMessage, sendMessageWithNoRefresh,
    sendRestart, sendReplace,

    -- * Save and Restore State
    StateFile (..), writeStateToFile, readStateFile, restart,

    -- * Floating Layer
    float, floatLocation,

    -- * Window Size Hints
    D, mkAdjust, applySizeHints, applySizeHints', applySizeHintsContents,
    applyAspectHint, applyResizeIncHint, applyMaxSizeHint,

    -- * Rectangles
    containedIn, nubScreens, pointWithin, scaleRationalRect,

    -- * Other Utilities
    initColor, pointScreen, screenWorkspace,
    setLayout, updateLayout,
    ) where

import XMonad.Core
import XMonad.Layout (Full(..))
import qualified XMonad.StackSet as W

import Data.Maybe
import Data.Monoid          (Endo(..),Any(..))
import Data.List            (nub, (\\), find)
import Data.Bits            ((.|.), (.&.), complement, setBit, testBit)
import Data.Function        (on)
import Data.Ratio
import qualified Data.Map as M
import qualified Data.Set as S

import Control.Arrow (second)
import Control.Monad.Fix (fix)
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad (forM, forM_, guard, join, unless, void, when)
import qualified Control.Exception as C

import System.IO
import System.Directory
import System.Posix.Process (executeFile)
import Graphics.X11.Xlib
import Graphics.X11.Xinerama (getScreenInfo)
import Graphics.X11.Xlib.Extras

-- ---------------------------------------------------------------------
-- Window manager operations

-- | Detect whether a window has fixed size or is transient. This check
-- can be used to determine whether the window should be floating or not
--
isFixedSizeOrTransient :: Display -> Window -> X Bool
isFixedSizeOrTransient :: Display -> Word64 -> X Bool
isFixedSizeOrTransient Display
d Word64
w = do
    sh <- IO SizeHints -> X SizeHints
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO SizeHints -> X SizeHints) -> IO SizeHints -> X SizeHints
forall a b. (a -> b) -> a -> b
$ Display -> Word64 -> IO SizeHints
getWMNormalHints Display
d Word64
w
    let isFixedSize = Maybe (Word32, Word32) -> Bool
forall a. Maybe a -> Bool
isJust (SizeHints -> Maybe (Word32, Word32)
sh_min_size SizeHints
sh) Bool -> Bool -> Bool
&& SizeHints -> Maybe (Word32, Word32)
sh_min_size SizeHints
sh Maybe (Word32, Word32) -> Maybe (Word32, Word32) -> Bool
forall a. Eq a => a -> a -> Bool
== SizeHints -> Maybe (Word32, Word32)
sh_max_size SizeHints
sh
    isTransient <- isJust <$> io (getTransientForHint d w)
    return (isFixedSize || isTransient)

-- |
-- Add a new window to be managed in the current workspace.
-- Bring it into focus.
--
-- Whether the window is already managed, or not, it is mapped, has its
-- border set, and its event mask set.
--
manage :: Window -> X ()
manage :: Word64 -> X ()
manage Word64
w = X Bool -> X () -> X ()
whenX (Bool -> Bool
not (Bool -> Bool) -> X Bool -> X Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word64 -> X Bool
isClient Word64
w) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> do

    shouldFloat <- Display -> Word64 -> X Bool
isFixedSizeOrTransient Display
d Word64
w

    rr <- snd `fmap` floatLocation w
    -- ensure that float windows don't go over the edge of the screen
    let adjust (W.RationalRect Ratio Integer
x Ratio Integer
y Ratio Integer
wid Ratio Integer
h) | Ratio Integer
x Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
+ Ratio Integer
wid Ratio Integer -> Ratio Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Ratio Integer
1 Bool -> Bool -> Bool
|| Ratio Integer
y Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
+ Ratio Integer
h Ratio Integer -> Ratio Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Ratio Integer
1 Bool -> Bool -> Bool
|| Ratio Integer
x Ratio Integer -> Ratio Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Ratio Integer
0 Bool -> Bool -> Bool
|| Ratio Integer
y Ratio Integer -> Ratio Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Ratio Integer
0
                                              = Ratio Integer
-> Ratio Integer -> Ratio Integer -> Ratio Integer -> RationalRect
W.RationalRect (Ratio Integer
0.5 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
- Ratio Integer
widRatio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/Ratio Integer
2) (Ratio Integer
0.5 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
- Ratio Integer
hRatio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/Ratio Integer
2) Ratio Integer
wid Ratio Integer
h
        adjust RationalRect
r = RationalRect
r

        f StackSet i l Word64 s sd
ws | Bool
shouldFloat = Word64
-> RationalRect
-> StackSet i l Word64 s sd
-> StackSet i l Word64 s sd
forall a i l s sd.
Ord a =>
a -> RationalRect -> StackSet i l a s sd -> StackSet i l a s sd
W.float Word64
w (RationalRect -> RationalRect
adjust RationalRect
rr) (StackSet i l Word64 s sd -> StackSet i l Word64 s sd)
-> (StackSet i l Word64 s sd -> StackSet i l Word64 s sd)
-> StackSet i l Word64 s sd
-> StackSet i l Word64 s sd
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> StackSet i l Word64 s sd -> StackSet i l Word64 s sd
forall a i l s sd.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.insertUp Word64
w (StackSet i l Word64 s sd -> StackSet i l Word64 s sd)
-> (StackSet i l Word64 s sd -> StackSet i l Word64 s sd)
-> StackSet i l Word64 s sd
-> StackSet i l Word64 s sd
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> StackSet i l Word64 s sd -> StackSet i l Word64 s sd
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 i
i (StackSet i l Word64 s sd -> StackSet i l Word64 s sd)
-> StackSet i l Word64 s sd -> StackSet i l Word64 s sd
forall a b. (a -> b) -> a -> b
$ StackSet i l Word64 s sd
ws
             | Bool
otherwise   = Word64 -> StackSet i l Word64 s sd -> StackSet i l Word64 s sd
forall a i l s sd.
Eq a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.insertUp Word64
w StackSet i l Word64 s sd
ws
            where i :: i
i = Workspace i l Word64 -> i
forall i l a. Workspace i l a -> i
W.tag (Workspace i l Word64 -> i) -> Workspace i l Word64 -> i
forall a b. (a -> b) -> a -> b
$ Screen i l Word64 s sd -> Workspace i l Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (Screen i l Word64 s sd -> Workspace i l Word64)
-> Screen i l Word64 s sd -> Workspace i l Word64
forall a b. (a -> b) -> a -> b
$ StackSet i l Word64 s sd -> Screen i l Word64 s sd
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
W.current StackSet i l Word64 s sd
ws

    mh <- asks (manageHook . config)
    g <- appEndo <$> userCodeDef (Endo id) (runQuery mh w)
    windows (g . f)

-- | A window no longer exists; remove it from the window
-- list, on whatever workspace it is.
--
unmanage :: Window -> X ()
unmanage :: Word64 -> X ()
unmanage = (WindowSet -> WindowSet) -> X ()
windows ((WindowSet -> WindowSet) -> X ())
-> (Word64 -> WindowSet -> WindowSet) -> Word64 -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> WindowSet -> WindowSet
forall a i l s sd.
Ord a =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.delete

-- | Kill the specified window. If we do kill it, we'll get a
-- delete notify back from X.
--
-- There are two ways to delete a window. Either just kill it, or if it
-- supports the delete protocol, send a delete event (e.g. firefox)
--
killWindow :: Window -> X ()
killWindow :: Word64 -> X ()
killWindow Word64
w = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
    wmdelt <- X Word64
atom_WM_DELETE_WINDOW  ;  wmprot <- atom_WM_PROTOCOLS

    protocols <- io $ getWMProtocols d w
    io $ if wmdelt `elem` protocols
        then allocaXEvent $ \XEventPtr
ev -> do
                XEventPtr -> Word32 -> IO ()
setEventType XEventPtr
ev Word32
clientMessage
                XEventPtr -> Word64 -> Word64 -> CInt -> Word64 -> Word64 -> IO ()
setClientMessageEvent XEventPtr
ev Word64
w Word64
wmprot CInt
32 Word64
wmdelt Word64
currentTime
                Display -> Word64 -> Bool -> Word64 -> XEventPtr -> IO ()
sendEvent Display
d Word64
w Bool
False Word64
noEventMask XEventPtr
ev
        else void (killClient d w)

-- | Kill the currently focused client.
kill :: X ()
kill :: X ()
kill = (Word64 -> X ()) -> X ()
withFocused Word64 -> X ()
killWindow

-- ---------------------------------------------------------------------
-- Managing windows

-- | Modify the current window list with a pure function, and refresh
windows :: (WindowSet -> WindowSet) -> X ()
windows :: (WindowSet -> WindowSet) -> X ()
windows WindowSet -> WindowSet
f = do
    XState { windowset = old } <- X XState
forall s (m :: * -> *). MonadState s m => m s
get
    let oldvisible = (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> [Word64])
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> [Word64]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Maybe (Stack Word64) -> [Word64]
forall a. Maybe (Stack a) -> [a]
W.integrate' (Maybe (Stack Word64) -> [Word64])
-> (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
    -> Maybe (Stack Word64))
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> [Word64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Workspace WorkspaceId (Layout Word64) Word64
-> Maybe (Stack Word64)
forall i l a. Workspace i l a -> Maybe (Stack a)
W.stack (Workspace WorkspaceId (Layout Word64) Word64
 -> Maybe (Stack Word64))
-> (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
    -> Workspace WorkspaceId (Layout Word64) Word64)
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Maybe (Stack Word64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace) ([Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
 -> [Word64])
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> [Word64]
forall a b. (a -> b) -> a -> b
$ WindowSet
-> Screen WorkspaceId (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
old Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
forall a. a -> [a] -> [a]
: WindowSet
-> [Screen
      WorkspaceId (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
old
        newwindows = WindowSet -> [Word64]
forall a i l s sd. Eq a => StackSet i l a s sd -> [a]
W.allWindows WindowSet
ws [Word64] -> [Word64] -> [Word64]
forall a. Eq a => [a] -> [a] -> [a]
\\ WindowSet -> [Word64]
forall a i l s sd. Eq a => StackSet i l a s sd -> [a]
W.allWindows WindowSet
old
        ws = WindowSet -> WindowSet
f WindowSet
old
    XConf { display = d , normalBorder = nbc, focusedBorder = fbc } <- ask

    mapM_ setInitialProperties newwindows

    whenJust (W.peek old) $ \Word64
otherw -> do
      nbs <- (XConf -> WorkspaceId) -> X WorkspaceId
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (XConfig Layout -> WorkspaceId
forall (l :: * -> *). XConfig l -> WorkspaceId
normalBorderColor (XConfig Layout -> WorkspaceId)
-> (XConf -> XConfig Layout) -> XConf -> WorkspaceId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> XConfig Layout
config)
      setWindowBorderWithFallback d otherw nbs nbc

    modify (\XState
s -> XState
s { windowset = ws })

    -- notify non visibility
    let tags_oldvisible = (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> WorkspaceId)
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> [WorkspaceId]
forall a b. (a -> b) -> [a] -> [b]
map (Workspace WorkspaceId (Layout Word64) Word64 -> WorkspaceId
forall i l a. Workspace i l a -> i
W.tag (Workspace WorkspaceId (Layout Word64) Word64 -> WorkspaceId)
-> (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
    -> Workspace WorkspaceId (Layout Word64) Word64)
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> WorkspaceId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace) ([Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
 -> [WorkspaceId])
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> [WorkspaceId]
forall a b. (a -> b) -> a -> b
$ WindowSet
-> Screen WorkspaceId (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
old Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
forall a. a -> [a] -> [a]
: WindowSet
-> [Screen
      WorkspaceId (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
old
        gottenhidden    = (Workspace WorkspaceId (Layout Word64) Word64 -> Bool)
-> [Workspace WorkspaceId (Layout Word64) Word64]
-> [Workspace WorkspaceId (Layout Word64) Word64]
forall a. (a -> Bool) -> [a] -> [a]
filter ((WorkspaceId -> [WorkspaceId] -> Bool)
-> [WorkspaceId] -> WorkspaceId -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip WorkspaceId -> [WorkspaceId] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem [WorkspaceId]
tags_oldvisible (WorkspaceId -> Bool)
-> (Workspace WorkspaceId (Layout Word64) Word64 -> WorkspaceId)
-> Workspace WorkspaceId (Layout Word64) Word64
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Workspace WorkspaceId (Layout Word64) Word64 -> WorkspaceId
forall i l a. Workspace i l a -> i
W.tag) ([Workspace WorkspaceId (Layout Word64) Word64]
 -> [Workspace WorkspaceId (Layout Word64) Word64])
-> [Workspace WorkspaceId (Layout Word64) Word64]
-> [Workspace WorkspaceId (Layout Word64) Word64]
forall a b. (a -> b) -> a -> b
$ WindowSet -> [Workspace WorkspaceId (Layout Word64) Word64]
forall i l a sid sd. StackSet i l a sid sd -> [Workspace i l a]
W.hidden WindowSet
ws
    mapM_ (sendMessageWithNoRefresh Hide) gottenhidden

    -- for each workspace, layout the currently visible workspaces
    let allscreens     = WindowSet
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
forall i l a sid sd. StackSet i l a sid sd -> [Screen i l a sid sd]
W.screens WindowSet
ws
        summed_visible = ([Word64] -> [Word64] -> [Word64])
-> [Word64] -> [[Word64]] -> [[Word64]]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl [Word64] -> [Word64] -> [Word64]
forall a. [a] -> [a] -> [a]
(++) [] ([[Word64]] -> [[Word64]]) -> [[Word64]] -> [[Word64]]
forall a b. (a -> b) -> a -> b
$ (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> [Word64])
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> [[Word64]]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe (Stack Word64) -> [Word64]
forall a. Maybe (Stack a) -> [a]
W.integrate' (Maybe (Stack Word64) -> [Word64])
-> (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
    -> Maybe (Stack Word64))
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> [Word64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Workspace WorkspaceId (Layout Word64) Word64
-> Maybe (Stack Word64)
forall i l a. Workspace i l a -> Maybe (Stack a)
W.stack (Workspace WorkspaceId (Layout Word64) Word64
 -> Maybe (Stack Word64))
-> (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
    -> Workspace WorkspaceId (Layout Word64) Word64)
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Maybe (Stack Word64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace) [Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
allscreens
    rects <- fmap concat $ forM (zip allscreens summed_visible) $ \ (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
w, [Word64]
vis) -> do
        let wsp :: Workspace WorkspaceId (Layout Word64) Word64
wsp   = Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
w
            this :: WindowSet
this  = WorkspaceId -> 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 WorkspaceId
n WindowSet
ws
            n :: WorkspaceId
n     = Workspace WorkspaceId (Layout Word64) Word64 -> WorkspaceId
forall i l a. Workspace i l a -> i
W.tag Workspace WorkspaceId (Layout Word64) Word64
wsp
            tiled :: Maybe (Stack Word64)
tiled = (Workspace WorkspaceId (Layout Word64) Word64
-> Maybe (Stack Word64)
forall i l a. Workspace i l a -> Maybe (Stack a)
W.stack (Workspace WorkspaceId (Layout Word64) Word64
 -> Maybe (Stack Word64))
-> (WindowSet -> Workspace WorkspaceId (Layout Word64) Word64)
-> WindowSet
-> Maybe (Stack Word64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> Workspace WorkspaceId (Layout Word64) Word64)
-> (WindowSet
    -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> WindowSet
-> Workspace WorkspaceId (Layout Word64) Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> Screen WorkspaceId (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 -> Maybe (Stack Word64))
-> WindowSet -> Maybe (Stack Word64)
forall a b. (a -> b) -> a -> b
$ WindowSet
this)
                    Maybe (Stack Word64)
-> (Stack Word64 -> Maybe (Stack Word64)) -> Maybe (Stack Word64)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Word64 -> Bool) -> Stack Word64 -> Maybe (Stack Word64)
forall a. (a -> Bool) -> Stack a -> Maybe (Stack a)
W.filter (Word64 -> Map Word64 RationalRect -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`M.notMember` WindowSet -> Map Word64 RationalRect
forall i l a sid sd. StackSet i l a sid sd -> Map a RationalRect
W.floating WindowSet
ws)
                    Maybe (Stack Word64)
-> (Stack Word64 -> Maybe (Stack Word64)) -> Maybe (Stack Word64)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Word64 -> Bool) -> Stack Word64 -> Maybe (Stack Word64)
forall a. (a -> Bool) -> Stack a -> Maybe (Stack a)
W.filter (Word64 -> [Word64] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Word64]
vis)
            viewrect :: Rectangle
viewrect = ScreenDetail -> Rectangle
screenRect (ScreenDetail -> Rectangle) -> ScreenDetail -> Rectangle
forall a b. (a -> b) -> a -> b
$ Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> ScreenDetail
forall i l a sid sd. Screen i l a sid sd -> sd
W.screenDetail Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
w

        -- just the tiled windows:
        -- now tile the windows on this workspace, modified by the gap
        (rs, ml') <- Workspace WorkspaceId (Layout Word64) Word64
-> Rectangle -> X ([(Word64, Rectangle)], Maybe (Layout Word64))
forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace WorkspaceId (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout Workspace WorkspaceId (Layout Word64) Word64
wsp { W.stack = tiled } Rectangle
viewrect X ([(Word64, Rectangle)], Maybe (Layout Word64))
-> X ([(Word64, Rectangle)], Maybe (Layout Word64))
-> X ([(Word64, Rectangle)], Maybe (Layout Word64))
forall a. X a -> X a -> X a
`catchX`
                     Workspace WorkspaceId (Layout Word64) Word64
-> Rectangle -> X ([(Word64, Rectangle)], Maybe (Layout Word64))
forall (layout :: * -> *) a.
LayoutClass layout a =>
Workspace WorkspaceId (layout a) a
-> Rectangle -> X ([(a, Rectangle)], Maybe (layout a))
runLayout Workspace WorkspaceId (Layout Word64) Word64
wsp { W.stack = tiled, W.layout = Layout Full } Rectangle
viewrect
        updateLayout n ml'

        let m   = WindowSet -> Map Word64 RationalRect
forall i l a sid sd. StackSet i l a sid sd -> Map a RationalRect
W.floating WindowSet
ws
            flt = [(Word64
fw, Rectangle -> RationalRect -> Rectangle
scaleRationalRect Rectangle
viewrect RationalRect
r)
                    | Word64
fw <- (Word64 -> Bool) -> [Word64] -> [Word64]
forall a. (a -> Bool) -> [a] -> [a]
filter (Word64 -> Map Word64 RationalRect -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`M.member` Map Word64 RationalRect
m) (WindowSet -> [Word64]
forall i l a s sd. StackSet i l a s sd -> [a]
W.index WindowSet
this)
                    , Word64
fw Word64 -> [Word64] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Word64]
vis
                    , Just RationalRect
r <- [Word64 -> Map Word64 RationalRect -> Maybe RationalRect
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word64
fw Map Word64 RationalRect
m]]
            vs = [(Word64, Rectangle)]
flt [(Word64, Rectangle)]
-> [(Word64, Rectangle)] -> [(Word64, Rectangle)]
forall a. [a] -> [a] -> [a]
++ [(Word64, Rectangle)]
rs

        io $ restackWindows d (map fst vs)
        -- return the visible windows for this workspace:
        return vs

    let visible = ((Word64, Rectangle) -> Word64)
-> [(Word64, Rectangle)] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map (Word64, Rectangle) -> Word64
forall a b. (a, b) -> a
fst [(Word64, Rectangle)]
rects

    mapM_ (uncurry tileWindow) rects

    whenJust (W.peek ws) $ \Word64
w -> do
      fbs <- (XConf -> WorkspaceId) -> X WorkspaceId
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (XConfig Layout -> WorkspaceId
forall (l :: * -> *). XConfig l -> WorkspaceId
focusedBorderColor (XConfig Layout -> WorkspaceId)
-> (XConf -> XConfig Layout) -> XConf -> WorkspaceId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> XConfig Layout
config)
      setWindowBorderWithFallback d w fbs fbc

    mapM_ reveal visible
    setTopFocus

    -- hide every window that was potentially visible before, but is not
    -- given a position by a layout now.
    mapM_ hide (nub (oldvisible ++ newwindows) \\ visible)

    -- all windows that are no longer in the windowset are marked as
    -- withdrawn, it is important to do this after the above, otherwise 'hide'
    -- will overwrite withdrawnState with iconicState
    mapM_ (`setWMState` withdrawnState) (W.allWindows old \\ W.allWindows ws)

    isMouseFocused <- asks mouseFocused
    unless isMouseFocused $ clearEvents enterWindowMask
    asks (logHook . config) >>= userCodeDef ()

-- | Modify the @WindowSet@ in state with no special handling.
modifyWindowSet :: (WindowSet -> WindowSet) -> X ()
modifyWindowSet :: (WindowSet -> WindowSet) -> X ()
modifyWindowSet WindowSet -> WindowSet
f = (XState -> XState) -> X ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((XState -> XState) -> X ()) -> (XState -> XState) -> X ()
forall a b. (a -> b) -> a -> b
$ \XState
xst -> XState
xst { windowset = f (windowset xst) }

-- | Perform an @X@ action and check its return value against a predicate p.
-- If p holds, unwind changes to the @WindowSet@ and replay them using @windows@.
windowBracket :: (a -> Bool) -> X a -> X a
windowBracket :: forall a. (a -> Bool) -> X a -> X a
windowBracket a -> Bool
p X a
action = (WindowSet -> X a) -> X a
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X a) -> X a) -> (WindowSet -> X a) -> X a
forall a b. (a -> b) -> a -> b
$ \WindowSet
old -> do
  a <- X a
action
  when (p a) . withWindowSet $ \WindowSet
new -> do
    (WindowSet -> WindowSet) -> X ()
modifyWindowSet ((WindowSet -> WindowSet) -> X ())
-> (WindowSet -> WindowSet) -> X ()
forall a b. (a -> b) -> a -> b
$ WindowSet -> WindowSet -> WindowSet
forall a b. a -> b -> a
const WindowSet
old
    (WindowSet -> WindowSet) -> X ()
windows         ((WindowSet -> WindowSet) -> X ())
-> (WindowSet -> WindowSet) -> X ()
forall a b. (a -> b) -> a -> b
$ WindowSet -> WindowSet -> WindowSet
forall a b. a -> b -> a
const WindowSet
new
  return a

-- | Perform an @X@ action. If it returns @Any True@, unwind the
-- changes to the @WindowSet@ and replay them using @windows@. This is
-- a version of @windowBracket@ that discards the return value and
-- handles an @X@ action that reports its need for refresh via @Any@.
windowBracket_ :: X Any -> X ()
windowBracket_ :: X Any -> X ()
windowBracket_ = X Any -> X ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (X Any -> X ()) -> (X Any -> X Any) -> X Any -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Any -> Bool) -> X Any -> X Any
forall a. (a -> Bool) -> X a -> X a
windowBracket Any -> Bool
getAny

-- | Produce the actual rectangle from a screen and a ratio on that screen.
scaleRationalRect :: Rectangle -> W.RationalRect -> Rectangle
scaleRationalRect :: Rectangle -> RationalRect -> Rectangle
scaleRationalRect (Rectangle Int32
sx Int32
sy Word32
sw Word32
sh) (W.RationalRect Ratio Integer
rx Ratio Integer
ry Ratio Integer
rw Ratio Integer
rh)
 = Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle (Int32
sx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Ratio Integer -> Int32
forall {b} {a}. (Integral b, Real a) => a -> Ratio Integer -> b
scale Word32
sw Ratio Integer
rx) (Int32
sy Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Ratio Integer -> Int32
forall {b} {a}. (Integral b, Real a) => a -> Ratio Integer -> b
scale Word32
sh Ratio Integer
ry) (Word32 -> Ratio Integer -> Word32
forall {b} {a}. (Integral b, Real a) => a -> Ratio Integer -> b
scale Word32
sw Ratio Integer
rw) (Word32 -> Ratio Integer -> Word32
forall {b} {a}. (Integral b, Real a) => a -> Ratio Integer -> b
scale Word32
sh Ratio Integer
rh)
 where scale :: a -> Ratio Integer -> b
scale a
s Ratio Integer
r = Ratio Integer -> b
forall b. Integral b => Ratio Integer -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (a -> Ratio Integer
forall a. Real a => a -> Ratio Integer
toRational a
s Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
* Ratio Integer
r)

-- | Set a window's WM_STATE property.
setWMState :: Window -> Int -> X ()
setWMState :: Word64 -> Int -> X ()
setWMState Word64
w Int
v = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
dpy -> do
    a <- X Word64
atom_WM_STATE
    io $ changeProperty32 dpy w a a propModeReplace [fromIntegral v, fromIntegral none]

-- | Set the border color using the window's color map, if possible;
-- otherwise fall back to the color in @Pixel@.
setWindowBorderWithFallback :: Display -> Window -> String -> Pixel -> X ()
setWindowBorderWithFallback :: Display -> Word64 -> WorkspaceId -> Word64 -> X ()
setWindowBorderWithFallback Display
dpy Word64
w WorkspaceId
color Word64
basic = IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$
    (SomeException -> IO ()) -> IO () -> IO ()
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
C.handle SomeException -> IO ()
fallback (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      wa <- Display -> Word64 -> IO WindowAttributes
getWindowAttributes Display
dpy Word64
w
      pixel <- setPixelSolid . color_pixel . fst <$> allocNamedColor dpy (wa_colormap wa) color
      setWindowBorder dpy w pixel
  where
    fallback :: C.SomeException -> IO ()
    fallback :: SomeException -> IO ()
fallback SomeException
_ = Display -> Word64 -> Word64 -> IO ()
setWindowBorder Display
dpy Word64
w Word64
basic

-- | Hide a window by unmapping it and setting Iconified.
hide :: Window -> X ()
hide :: Word64 -> X ()
hide Word64
w = X Bool -> X () -> X ()
whenX ((XState -> Bool) -> X Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (Word64 -> Set Word64 -> Bool
forall a. Ord a => a -> Set a -> Bool
S.member Word64
w (Set Word64 -> Bool) -> (XState -> Set Word64) -> XState -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> Set Word64
mapped)) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
    cMask <- (XConf -> Word64) -> X Word64
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks ((XConf -> Word64) -> X Word64) -> (XConf -> Word64) -> X Word64
forall a b. (a -> b) -> a -> b
$ XConfig Layout -> Word64
forall (l :: * -> *). XConfig l -> Word64
clientMask (XConfig Layout -> Word64)
-> (XConf -> XConfig Layout) -> XConf -> Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> XConfig Layout
config
    io $ do selectInput d w (cMask .&. complement structureNotifyMask)
            unmapWindow d w
            selectInput d w cMask
    setWMState w iconicState
    -- this part is key: we increment the waitingUnmap counter to distinguish
    -- between client and xmonad initiated unmaps.
    modify (\XState
s -> XState
s { waitingUnmap = M.insertWith (+) w 1 (waitingUnmap s)
                    , mapped       = S.delete w (mapped s) })

-- | Show a window by mapping it and setting Normal.
-- This is harmless if the window was already visible.
reveal :: Window -> X ()
reveal :: Word64 -> X ()
reveal Word64
w = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
    Word64 -> Int -> X ()
setWMState Word64
w Int
normalState
    IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Word64 -> IO ()
mapWindow Display
d Word64
w
    X Bool -> X () -> X ()
whenX (Word64 -> X Bool
isClient Word64
w) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ (XState -> XState) -> X ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\XState
s -> XState
s { mapped = S.insert w (mapped s) })

-- | Set some properties when we initially gain control of a window.
setInitialProperties :: Window -> X ()
setInitialProperties :: Word64 -> X ()
setInitialProperties Word64
w = (XConf -> Word64) -> X Word64
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Word64
normalBorder X Word64 -> (Word64 -> 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
>>= \Word64
nb -> (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
    Word64 -> Int -> X ()
setWMState Word64
w Int
iconicState
    (XConf -> Word64) -> X Word64
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (XConfig Layout -> Word64
forall (l :: * -> *). XConfig l -> Word64
clientMask (XConfig Layout -> Word64)
-> (XConf -> XConfig Layout) -> XConf -> Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> XConfig Layout
config) X Word64 -> (Word64 -> 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
>>= IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> (Word64 -> IO ()) -> Word64 -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Display -> Word64 -> Word64 -> IO ()
selectInput Display
d Word64
w
    bw <- (XConf -> Word32) -> X Word32
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (XConfig Layout -> Word32
forall (l :: * -> *). XConfig l -> Word32
borderWidth (XConfig Layout -> Word32)
-> (XConf -> XConfig Layout) -> XConf -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> XConfig Layout
config)
    io $ setWindowBorderWidth d w bw
    -- we must initially set the color of new windows, to maintain invariants
    -- required by the border setting in 'windows'
    io $ setWindowBorder d w nb

-- | Render the currently visible workspaces, as determined by
-- the 'StackSet'. Also, set focus to the focused window.
--
-- This is our 'view' operation (MVC), in that it pretty prints our model
-- with X calls.
--
refresh :: X ()
refresh :: X ()
refresh = (WindowSet -> WindowSet) -> X ()
windows WindowSet -> WindowSet
forall a. a -> a
id

-- | Remove all events of a given type from the event queue.
clearEvents :: EventMask -> X ()
clearEvents :: Word64 -> X ()
clearEvents Word64
mask = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ do
    Display -> Bool -> IO ()
sync Display
d Bool
False
    (XEventPtr -> IO ()) -> IO ()
forall a. (XEventPtr -> IO a) -> IO a
allocaXEvent ((XEventPtr -> IO ()) -> IO ()) -> (XEventPtr -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \XEventPtr
p -> (IO () -> IO ()) -> IO ()
forall a. (a -> a) -> a
fix ((IO () -> IO ()) -> IO ()) -> (IO () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \IO ()
again -> do
        more <- Display -> Word64 -> XEventPtr -> IO Bool
checkMaskEvent Display
d Word64
mask XEventPtr
p
        when more again -- beautiful

-- | Move and resize @w@ such that it fits inside the given rectangle,
-- including its border.
tileWindow :: Window -> Rectangle -> X ()
tileWindow :: Word64 -> Rectangle -> X ()
tileWindow Word64
w Rectangle
r = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> Display -> Word64 -> (WindowAttributes -> X ()) -> X ()
withWindowAttributes Display
d Word64
w ((WindowAttributes -> X ()) -> X ())
-> (WindowAttributes -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowAttributes
wa -> do
    -- give all windows at least 1x1 pixels
    let bw :: Word32
bw = CInt -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Word32) -> CInt -> Word32
forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_border_width WindowAttributes
wa
        least :: Word32 -> Word32
least Word32
x | Word32
x Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32
bwWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
*Word32
2  = Word32
1
                | Bool
otherwise  = Word32
x Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
bwWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
*Word32
2
    IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Word64 -> Int32 -> Int32 -> Word32 -> Word32 -> IO ()
moveResizeWindow Display
d Word64
w (Rectangle -> Int32
rect_x Rectangle
r) (Rectangle -> Int32
rect_y Rectangle
r)
                              (Word32 -> Word32
least (Word32 -> Word32) -> Word32 -> Word32
forall a b. (a -> b) -> a -> b
$ Rectangle -> Word32
rect_width Rectangle
r) (Word32 -> Word32
least (Word32 -> Word32) -> Word32 -> Word32
forall a b. (a -> b) -> a -> b
$ Rectangle -> Word32
rect_height Rectangle
r)

-- ---------------------------------------------------------------------

-- | Returns 'True' if the first rectangle is contained within, but not equal
-- to the second.
containedIn :: Rectangle -> Rectangle -> Bool
containedIn :: Rectangle -> Rectangle -> Bool
containedIn r1 :: Rectangle
r1@(Rectangle Int32
x1 Int32
y1 Word32
w1 Word32
h1) r2 :: Rectangle
r2@(Rectangle Int32
x2 Int32
y2 Word32
w2 Word32
h2)
 = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and [ Rectangle
r1 Rectangle -> Rectangle -> Bool
forall a. Eq a => a -> a -> Bool
/= Rectangle
r2
       , Int32
x1 Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
>= Int32
x2
       , Int32
y1 Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
>= Int32
y2
       , Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
x1 Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
w1 Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
x2 Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
w2
       , Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
y1 Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
h1 Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
y2 Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
h2 ]

-- | Given a list of screens, remove all duplicated screens and screens that
-- are entirely contained within another.
nubScreens :: [Rectangle] -> [Rectangle]
nubScreens :: [Rectangle] -> [Rectangle]
nubScreens [Rectangle]
xs = [Rectangle] -> [Rectangle]
forall a. Eq a => [a] -> [a]
nub ([Rectangle] -> [Rectangle])
-> ([Rectangle] -> [Rectangle]) -> [Rectangle] -> [Rectangle]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rectangle -> Bool) -> [Rectangle] -> [Rectangle]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Rectangle
x -> Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ (Rectangle -> Bool) -> [Rectangle] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Rectangle
x Rectangle -> Rectangle -> Bool
`containedIn`) [Rectangle]
xs) ([Rectangle] -> [Rectangle]) -> [Rectangle] -> [Rectangle]
forall a b. (a -> b) -> a -> b
$ [Rectangle]
xs

-- | Clean the list of screens according to the rules documented for
-- nubScreens.
getCleanedScreenInfo :: MonadIO m => Display -> m [Rectangle]
getCleanedScreenInfo :: forall (m :: * -> *). MonadIO m => Display -> m [Rectangle]
getCleanedScreenInfo = IO [Rectangle] -> m [Rectangle]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO [Rectangle] -> m [Rectangle])
-> (Display -> IO [Rectangle]) -> Display -> m [Rectangle]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  ([Rectangle] -> [Rectangle]) -> IO [Rectangle] -> IO [Rectangle]
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Rectangle] -> [Rectangle]
nubScreens (IO [Rectangle] -> IO [Rectangle])
-> (Display -> IO [Rectangle]) -> Display -> IO [Rectangle]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Display -> IO [Rectangle]
getScreenInfo

-- | The screen configuration may have changed (due to -- xrandr),
-- update the state and refresh the screen, and reset the gap.
rescreen :: X ()
rescreen :: X ()
rescreen = (Display -> X [Rectangle]) -> X [Rectangle]
forall a. (Display -> X a) -> X a
withDisplay Display -> X [Rectangle]
forall (m :: * -> *). MonadIO m => Display -> m [Rectangle]
getCleanedScreenInfo X [Rectangle] -> ([Rectangle] -> 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
>>= \case
    [] -> WorkspaceId -> X ()
forall (m :: * -> *). MonadIO m => WorkspaceId -> m ()
trace WorkspaceId
"getCleanedScreenInfo returned []"
    Rectangle
xinesc:[Rectangle]
xinescs ->
        (WindowSet -> WindowSet) -> X ()
windows ((WindowSet -> WindowSet) -> X ())
-> (WindowSet -> WindowSet) -> X ()
forall a b. (a -> b) -> a -> b
$ \ws :: WindowSet
ws@W.StackSet{ current :: forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
W.current = Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
v, visible :: forall i l a sid sd. StackSet i l a sid sd -> [Screen i l a sid sd]
W.visible = [Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
vs, hidden :: forall i l a sid sd. StackSet i l a sid sd -> [Workspace i l a]
W.hidden = [Workspace WorkspaceId (Layout Word64) Word64]
hs } ->
            let ([Workspace WorkspaceId (Layout Word64) Word64]
xs, [Workspace WorkspaceId (Layout Word64) Word64]
ys) = Int
-> [Workspace WorkspaceId (Layout Word64) Word64]
-> ([Workspace WorkspaceId (Layout Word64) Word64],
    [Workspace WorkspaceId (Layout Word64) Word64])
forall a. Int -> [a] -> ([a], [a])
splitAt ([Rectangle] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Rectangle]
xinescs) ((Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> Workspace WorkspaceId (Layout Word64) Word64)
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> [Workspace WorkspaceId (Layout Word64) Word64]
forall a b. (a -> b) -> [a] -> [b]
map Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace [Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
vs [Workspace WorkspaceId (Layout Word64) Word64]
-> [Workspace WorkspaceId (Layout Word64) Word64]
-> [Workspace WorkspaceId (Layout Word64) Word64]
forall a. [a] -> [a] -> [a]
++ [Workspace WorkspaceId (Layout Word64) Word64]
hs)
                a :: Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
a = Workspace WorkspaceId (Layout Word64) Word64
-> ScreenId
-> ScreenDetail
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
forall i l a sid sd.
Workspace i l a -> sid -> sd -> Screen i l a sid sd
W.Screen (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
v) ScreenId
0 (Rectangle -> ScreenDetail
SD Rectangle
xinesc)
                as :: [Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
as = (Workspace WorkspaceId (Layout Word64) Word64
 -> ScreenId
 -> ScreenDetail
 -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> [Workspace WorkspaceId (Layout Word64) Word64]
-> [ScreenId]
-> [ScreenDetail]
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 Workspace WorkspaceId (Layout Word64) Word64
-> ScreenId
-> ScreenDetail
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
forall i l a sid sd.
Workspace i l a -> sid -> sd -> Screen i l a sid sd
W.Screen [Workspace WorkspaceId (Layout Word64) Word64]
xs [ScreenId
1..] ([ScreenDetail]
 -> [Screen
       WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail])
-> [ScreenDetail]
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
forall a b. (a -> b) -> a -> b
$ (Rectangle -> ScreenDetail) -> [Rectangle] -> [ScreenDetail]
forall a b. (a -> b) -> [a] -> [b]
map Rectangle -> ScreenDetail
SD [Rectangle]
xinescs
            in  WindowSet
ws { W.current = a
                   , W.visible = as
                   , W.hidden  = ys }

-- ---------------------------------------------------------------------

-- | Tell whether or not to intercept clicks on a given window
setButtonGrab :: Bool -> Window -> X ()
setButtonGrab :: Bool -> Word64 -> X ()
setButtonGrab Bool
grab Word64
w = do
    pointerMode <- (XConf -> CInt) -> X CInt
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks ((XConf -> CInt) -> X CInt) -> (XConf -> CInt) -> X CInt
forall a b. (a -> b) -> a -> b
$ \XConf
c -> if XConfig Layout -> Bool
forall (l :: * -> *). XConfig l -> Bool
clickJustFocuses (XConf -> XConfig Layout
config XConf
c)
                                    then CInt
grabModeAsync
                                    else CInt
grabModeSync
    withDisplay $ \Display
d -> IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ if Bool
grab
        then [Word32] -> (Word32 -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Word32
button1, Word32
button2, Word32
button3] ((Word32 -> IO ()) -> IO ()) -> (Word32 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Word32
b ->
            Display
-> Word32
-> CUInt
-> Word64
-> Bool
-> Word64
-> CInt
-> CInt
-> Word64
-> Word64
-> IO ()
grabButton Display
d Word32
b CUInt
anyModifier Word64
w Bool
False Word64
buttonPressMask
                       CInt
pointerMode CInt
grabModeSync Word64
none Word64
none
        else Display -> Word32 -> CUInt -> Word64 -> IO ()
ungrabButton Display
d Word32
anyButton CUInt
anyModifier Word64
w

-- ---------------------------------------------------------------------
-- Setting keyboard focus

-- | Set the focus to the window on top of the stack, or root
setTopFocus :: X ()
setTopFocus :: X ()
setTopFocus = (WindowSet -> X ()) -> X ()
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X ()) -> X ()) -> (WindowSet -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ X () -> (Word64 -> X ()) -> Maybe Word64 -> X ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Word64 -> X ()
setFocusX (Word64 -> X ()) -> X Word64 -> X ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (XConf -> Word64) -> X Word64
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Word64
theRoot) Word64 -> X ()
setFocusX (Maybe Word64 -> X ())
-> (WindowSet -> Maybe Word64) -> WindowSet -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet -> Maybe Word64
forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek

-- | Set focus explicitly to window 'w' if it is managed by us, or root.
-- This happens if X notices we've moved the mouse (and perhaps moved
-- the mouse to a new screen).
focus :: Window -> X ()
focus :: Word64 -> X ()
focus Word64
w = (XConf -> XConf) -> X () -> X ()
forall a. (XConf -> XConf) -> X a -> X a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\XConf
c -> XConf
c { mouseFocused = True }) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ (WindowSet -> X ()) -> X ()
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X ()) -> X ()) -> (WindowSet -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowSet
s -> do
    let stag :: Screen c l a sid sd -> c
stag = Workspace c l a -> c
forall i l a. Workspace i l a -> i
W.tag (Workspace c l a -> c)
-> (Screen c l a sid sd -> Workspace c l a)
-> Screen c l a sid sd
-> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen c l a sid sd -> Workspace c l a
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace
        curr :: WorkspaceId
curr = Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> WorkspaceId
forall {c} {l} {a} {sid} {sd}. Screen c l a sid sd -> c
stag (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> WorkspaceId)
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> WorkspaceId
forall a b. (a -> b) -> a -> b
$ WindowSet
-> Screen WorkspaceId (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
s
    mnew <- X (Maybe WorkspaceId)
-> ((Int32, Int32) -> X (Maybe WorkspaceId))
-> Maybe (Int32, Int32)
-> X (Maybe WorkspaceId)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe WorkspaceId -> X (Maybe WorkspaceId)
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe WorkspaceId
forall a. Maybe a
Nothing) ((Maybe
   (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
 -> Maybe WorkspaceId)
-> X (Maybe
        (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
-> X (Maybe WorkspaceId)
forall a b. (a -> b) -> X a -> X b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> WorkspaceId)
-> Maybe
     (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> Maybe WorkspaceId
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> WorkspaceId
forall {c} {l} {a} {sid} {sd}. Screen c l a sid sd -> c
stag) (X (Maybe
      (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
 -> X (Maybe WorkspaceId))
-> ((Int32, Int32)
    -> X (Maybe
            (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)))
-> (Int32, Int32)
-> X (Maybe WorkspaceId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int32
 -> Int32
 -> X (Maybe
         (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)))
-> (Int32, Int32)
-> X (Maybe
        (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int32
-> Int32
-> X (Maybe
        (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
pointScreen)
            (Maybe (Int32, Int32) -> X (Maybe WorkspaceId))
-> X (Maybe (Int32, Int32)) -> X (Maybe WorkspaceId)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (XConf -> Maybe (Int32, Int32)) -> X (Maybe (Int32, Int32))
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Maybe (Int32, Int32)
mousePosition
    root <- asks theRoot
    case () of
        ()
_ | Word64 -> WindowSet -> Bool
forall a i l s sd. Eq a => a -> StackSet i l a s sd -> Bool
W.member Word64
w WindowSet
s Bool -> Bool -> Bool
&& WindowSet -> Maybe Word64
forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek WindowSet
s Maybe Word64 -> Maybe Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64 -> Maybe Word64
forall a. a -> Maybe a
Just Word64
w -> (WindowSet -> WindowSet) -> X ()
windows (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
w)
          | Just WorkspaceId
new <- Maybe WorkspaceId
mnew, Word64
w Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
root Bool -> Bool -> Bool
&& WorkspaceId
curr WorkspaceId -> WorkspaceId -> Bool
forall a. Eq a => a -> a -> Bool
/= WorkspaceId
new
                                               -> (WindowSet -> WindowSet) -> X ()
windows (WorkspaceId -> 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 WorkspaceId
new)
          | Bool
otherwise                          -> () -> X ()
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Call X to set the keyboard focus details.
setFocusX :: Window -> X ()
setFocusX :: Word64 -> X ()
setFocusX Word64
w = (WindowSet -> X ()) -> X ()
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X ()) -> X ()) -> (WindowSet -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowSet
ws -> do
    dpy <- (XConf -> Display) -> X Display
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Display
display

    -- clear mouse button grab and border on other windows
    forM_ (W.current ws : W.visible ws) $ \Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
wk ->
        [Word64] -> (Word64 -> X ()) -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (WindowSet -> [Word64]
forall i l a s sd. StackSet i l a s sd -> [a]
W.index (WorkspaceId -> 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 (Workspace WorkspaceId (Layout Word64) Word64 -> WorkspaceId
forall i l a. Workspace i l a -> i
W.tag (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
wk)) WindowSet
ws)) ((Word64 -> X ()) -> X ()) -> (Word64 -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Word64
otherw ->
            Bool -> Word64 -> X ()
setButtonGrab Bool
True Word64
otherw

    -- If we ungrab buttons on the root window, we lose our mouse bindings.
    whenX (not <$> isRoot w) $ setButtonGrab False w

    hints <- io $ getWMHints dpy w
    protocols <- io $ getWMProtocols dpy w
    wmprot <- atom_WM_PROTOCOLS
    wmtf <- atom_WM_TAKE_FOCUS
    currevt <- asks currentEvent
    let inputHintSet = WMHints -> CLong
wmh_flags WMHints
hints CLong -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
`testBit` Int
inputHintBit

    when (inputHintSet && wmh_input hints || not inputHintSet) $
      io $ do setInputFocus dpy w revertToPointerRoot 0
    when (wmtf `elem` protocols) $
      io $ allocaXEvent $ \XEventPtr
ev -> do
        XEventPtr -> Word32 -> IO ()
setEventType XEventPtr
ev Word32
clientMessage
        XEventPtr -> Word64 -> Word64 -> CInt -> Word64 -> Word64 -> IO ()
setClientMessageEvent XEventPtr
ev Word64
w Word64
wmprot CInt
32 Word64
wmtf (Word64 -> IO ()) -> Word64 -> IO ()
forall a b. (a -> b) -> a -> b
$ Word64 -> (Event -> Word64) -> Maybe Event -> Word64
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word64
currentTime Event -> Word64
event_time Maybe Event
currevt
        Display -> Word64 -> Bool -> Word64 -> XEventPtr -> IO ()
sendEvent Display
dpy Word64
w Bool
False Word64
noEventMask XEventPtr
ev
        where event_time :: Event -> Word64
event_time Event
ev =
                if Event -> Word32
ev_event_type Event
ev Word32 -> [Word32] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Word32]
timedEvents then
                  Event -> Word64
ev_time Event
ev
                else
                  Word64
currentTime
              timedEvents :: [Word32]
timedEvents = [ Word32
keyPress, Word32
keyRelease, Word32
buttonPress, Word32
buttonRelease, Word32
enterNotify, Word32
leaveNotify, Word32
selectionRequest ]

cacheNumlockMask :: X ()
cacheNumlockMask :: X ()
cacheNumlockMask = do
    dpy <- (XConf -> Display) -> X Display
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Display
display
    ms <- io $ getModifierMapping dpy
    xs <- sequence [ do ks <- io $ keycodeToKeysym dpy kc 0
                        if ks == xK_Num_Lock
                            then return (setBit 0 (fromIntegral m))
                            else return (0 :: KeyMask)
                   | (m, kcs) <- ms, kc <- kcs, kc /= 0
                   ]
    modify (\XState
s -> XState
s { numberlockMask = foldr (.|.) 0 xs })

-- | Given a list of keybindings, turn the given 'KeySym's into actual
-- 'KeyCode's and prepare them for grabbing.
mkGrabs :: [(KeyMask, KeySym)] -> X [(KeyMask, KeyCode)]
mkGrabs :: [(CUInt, Word64)] -> X [(CUInt, Word8)]
mkGrabs [(CUInt, Word64)]
ks = (Display -> X [(CUInt, Word8)]) -> X [(CUInt, Word8)]
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X [(CUInt, Word8)]) -> X [(CUInt, Word8)])
-> (Display -> X [(CUInt, Word8)]) -> X [(CUInt, Word8)]
forall a b. (a -> b) -> a -> b
$ \Display
dpy -> do
    let (CInt
minCode, CInt
maxCode) = Display -> (CInt, CInt)
displayKeycodes Display
dpy
        allCodes :: [Word8]
allCodes = [CInt -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
minCode .. CInt -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
maxCode]
    -- build a map from keysyms to lists of keysyms (doing what
    -- XGetKeyboardMapping would do if the X11 package bound it)
    syms <- [Word8] -> (Word8 -> X Word64) -> X [Word64]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Word8]
allCodes ((Word8 -> X Word64) -> X [Word64])
-> (Word8 -> X Word64) -> X [Word64]
forall a b. (a -> b) -> a -> b
$ \Word8
code -> IO Word64 -> X Word64
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (Display -> Word8 -> CInt -> IO Word64
keycodeToKeysym Display
dpy Word8
code CInt
0)
    let -- keycodeToKeysym returns noSymbol for all unbound keycodes,
        -- and we don't want to grab those whenever someone accidentally
        -- uses def :: KeySym
        keysymMap = Word64 -> Map Word64 [Word8] -> Map Word64 [Word8]
forall k a. Ord k => k -> Map k a -> Map k a
M.delete Word64
noSymbol (Map Word64 [Word8] -> Map Word64 [Word8])
-> Map Word64 [Word8] -> Map Word64 [Word8]
forall a b. (a -> b) -> a -> b
$
            ([Word8] -> [Word8] -> [Word8])
-> [(Word64, [Word8])] -> Map Word64 [Word8]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith [Word8] -> [Word8] -> [Word8]
forall a. [a] -> [a] -> [a]
(++) ([Word64] -> [[Word8]] -> [(Word64, [Word8])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Word64]
syms [[Word8
code] | Word8
code <- [Word8]
allCodes])
        keysymToKeycodes Word64
sym = [Word8] -> Word64 -> Map Word64 [Word8] -> [Word8]
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault [] Word64
sym Map Word64 [Word8]
keysymMap
    extraMods <- extraModifiers
    pure [ (mask .|. extraMod, keycode)
         | (mask, sym) <- ks
         , keycode     <- keysymToKeycodes sym
         , extraMod    <- extraMods
         ]

-- | Release XMonad's keyboard grab, so other grabbers can do their thing.
--
-- Start a keyboard action with this if it is going to run something
-- that needs to do a keyboard, pointer, or server grab. For example,
--
-- > , ((modm .|. controlMask, xK_p), unGrab >> spawn "scrot")
--
-- (Other examples are certain screen lockers and "gksu".)
-- This avoids needing to insert a pause/sleep before running the
-- command.
--
-- XMonad retains the keyboard grab during key actions because if they
-- use a submap, they need the keyboard to be grabbed, and if they had
-- to assert their own grab then the asynchronous nature of X11 allows
-- race conditions between XMonad, other clients, and the X server that
-- would cause keys to sometimes be "leaked" to the focused window.
unGrab :: X ()
unGrab :: X ()
unGrab = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ do
    Display -> Word64 -> IO ()
ungrabKeyboard Display
d Word64
currentTime
    Display -> Word64 -> IO ()
ungrabPointer  Display
d Word64
currentTime
    Display -> Bool -> IO ()
sync Display
d Bool
False

------------------------------------------------------------------------
-- Message handling

-- | Throw a message to the current 'LayoutClass' possibly modifying how we
-- layout the windows, in which case changes are handled through a refresh.
sendMessage :: Message a => a -> X ()
sendMessage :: forall a. Message a => a -> X ()
sendMessage a
a = X Any -> X ()
windowBracket_ (X Any -> X ()) -> X Any -> X ()
forall a b. (a -> b) -> a -> b
$ do
    w <- (XState -> Workspace WorkspaceId (Layout Word64) Word64)
-> X (Workspace WorkspaceId (Layout Word64) Word64)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ((XState -> Workspace WorkspaceId (Layout Word64) Word64)
 -> X (Workspace WorkspaceId (Layout Word64) Word64))
-> (XState -> Workspace WorkspaceId (Layout Word64) Word64)
-> X (Workspace WorkspaceId (Layout Word64) Word64)
forall a b. (a -> b) -> a -> b
$ Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> Workspace WorkspaceId (Layout Word64) Word64)
-> (XState
    -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> XState
-> Workspace WorkspaceId (Layout Word64) Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> Screen WorkspaceId (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
 -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> (XState -> WindowSet)
-> XState
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset
    ml' <- handleMessage (W.layout w) (SomeMessage a) `catchX` return Nothing
    whenJust ml' $ \Layout Word64
l' ->
        (WindowSet -> WindowSet) -> X ()
modifyWindowSet ((WindowSet -> WindowSet) -> X ())
-> (WindowSet -> WindowSet) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowSet
ws -> WindowSet
ws { W.current = (W.current ws)
                                { W.workspace = (W.workspace $ W.current ws)
                                  { W.layout = l' }}}
    return (Any $ isJust ml')

-- | Send a message to all layouts, without refreshing.
broadcastMessage :: Message a => a -> X ()
broadcastMessage :: forall a. Message a => a -> X ()
broadcastMessage a
a = (WindowSet -> X ()) -> X ()
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X ()) -> X ()) -> (WindowSet -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowSet
ws -> do
    -- this is O(n²), but we can't really fix this as there's code in
    -- xmonad-contrib that touches the windowset during handleMessage
    -- (returning Nothing for changes to not get overwritten), so we
    -- unfortunately need to do this one by one and persist layout states
    -- of each workspace separately)
    let c :: Workspace WorkspaceId (Layout Word64) Word64
c = Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> Workspace WorkspaceId (Layout Word64) Word64)
-> (WindowSet
    -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> WindowSet
-> Workspace WorkspaceId (Layout Word64) Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> Screen WorkspaceId (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 -> Workspace WorkspaceId (Layout Word64) Word64)
-> WindowSet -> Workspace WorkspaceId (Layout Word64) Word64
forall a b. (a -> b) -> a -> b
$ WindowSet
ws
        v :: [Workspace WorkspaceId (Layout Word64) Word64]
v = (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> Workspace WorkspaceId (Layout Word64) Word64)
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> [Workspace WorkspaceId (Layout Word64) Word64]
forall a b. (a -> b) -> [a] -> [b]
map Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace ([Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
 -> [Workspace WorkspaceId (Layout Word64) Word64])
-> (WindowSet
    -> [Screen
          WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail])
-> WindowSet
-> [Workspace WorkspaceId (Layout Word64) Word64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> [Screen
      WorkspaceId (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 -> [Workspace WorkspaceId (Layout Word64) Word64])
-> WindowSet -> [Workspace WorkspaceId (Layout Word64) Word64]
forall a b. (a -> b) -> a -> b
$ WindowSet
ws
        h :: [Workspace WorkspaceId (Layout Word64) Word64]
h = WindowSet -> [Workspace WorkspaceId (Layout Word64) Word64]
forall i l a sid sd. StackSet i l a sid sd -> [Workspace i l a]
W.hidden WindowSet
ws
    (Workspace WorkspaceId (Layout Word64) Word64 -> X ())
-> [Workspace WorkspaceId (Layout Word64) Word64] -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (a -> Workspace WorkspaceId (Layout Word64) Word64 -> X ()
forall a.
Message a =>
a -> Workspace WorkspaceId (Layout Word64) Word64 -> X ()
sendMessageWithNoRefresh a
a) (Workspace WorkspaceId (Layout Word64) Word64
c Workspace WorkspaceId (Layout Word64) Word64
-> [Workspace WorkspaceId (Layout Word64) Word64]
-> [Workspace WorkspaceId (Layout Word64) Word64]
forall a. a -> [a] -> [a]
: [Workspace WorkspaceId (Layout Word64) Word64]
v [Workspace WorkspaceId (Layout Word64) Word64]
-> [Workspace WorkspaceId (Layout Word64) Word64]
-> [Workspace WorkspaceId (Layout Word64) Word64]
forall a. [a] -> [a] -> [a]
++ [Workspace WorkspaceId (Layout Word64) Word64]
h)

-- | Send a message to a layout, without refreshing.
sendMessageWithNoRefresh :: Message a => a -> WindowSpace -> X ()
sendMessageWithNoRefresh :: forall a.
Message a =>
a -> Workspace WorkspaceId (Layout Word64) Word64 -> X ()
sendMessageWithNoRefresh a
a Workspace WorkspaceId (Layout Word64) Word64
w =
    Layout Word64 -> SomeMessage -> X (Maybe (Layout Word64))
forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> SomeMessage -> X (Maybe (layout a))
handleMessage (Workspace WorkspaceId (Layout Word64) Word64 -> Layout Word64
forall i l a. Workspace i l a -> l
W.layout Workspace WorkspaceId (Layout Word64) Word64
w) (a -> SomeMessage
forall a. Message a => a -> SomeMessage
SomeMessage a
a) X (Maybe (Layout Word64))
-> X (Maybe (Layout Word64)) -> X (Maybe (Layout Word64))
forall a. X a -> X a -> X a
`catchX` Maybe (Layout Word64) -> X (Maybe (Layout Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Layout Word64)
forall a. Maybe a
Nothing X (Maybe (Layout Word64))
-> (Maybe (Layout Word64) -> 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
>>=
    WorkspaceId -> Maybe (Layout Word64) -> X ()
updateLayout  (Workspace WorkspaceId (Layout Word64) Word64 -> WorkspaceId
forall i l a. Workspace i l a -> i
W.tag Workspace WorkspaceId (Layout Word64) Word64
w)

-- | Update the layout field of a workspace.
updateLayout :: WorkspaceId -> Maybe (Layout Window) -> X ()
updateLayout :: WorkspaceId -> Maybe (Layout Word64) -> X ()
updateLayout WorkspaceId
i Maybe (Layout Word64)
ml = Maybe (Layout Word64) -> (Layout Word64 -> X ()) -> X ()
forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust Maybe (Layout Word64)
ml ((Layout Word64 -> X ()) -> X ())
-> (Layout Word64 -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Layout Word64
l ->
    (Workspace WorkspaceId (Layout Word64) Word64
 -> X (Workspace WorkspaceId (Layout Word64) Word64))
-> X ()
runOnWorkspaces ((Workspace WorkspaceId (Layout Word64) Word64
  -> X (Workspace WorkspaceId (Layout Word64) Word64))
 -> X ())
-> (Workspace WorkspaceId (Layout Word64) Word64
    -> X (Workspace WorkspaceId (Layout Word64) Word64))
-> X ()
forall a b. (a -> b) -> a -> b
$ \Workspace WorkspaceId (Layout Word64) Word64
ww -> Workspace WorkspaceId (Layout Word64) Word64
-> X (Workspace WorkspaceId (Layout Word64) Word64)
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Workspace WorkspaceId (Layout Word64) Word64
 -> X (Workspace WorkspaceId (Layout Word64) Word64))
-> Workspace WorkspaceId (Layout Word64) Word64
-> X (Workspace WorkspaceId (Layout Word64) Word64)
forall a b. (a -> b) -> a -> b
$ if Workspace WorkspaceId (Layout Word64) Word64 -> WorkspaceId
forall i l a. Workspace i l a -> i
W.tag Workspace WorkspaceId (Layout Word64) Word64
ww WorkspaceId -> WorkspaceId -> Bool
forall a. Eq a => a -> a -> Bool
== WorkspaceId
i then Workspace WorkspaceId (Layout Word64) Word64
ww { W.layout = l} else Workspace WorkspaceId (Layout Word64) Word64
ww

-- | Set the layout of the currently viewed workspace.
setLayout :: Layout Window -> X ()
setLayout :: Layout Word64 -> X ()
setLayout Layout Word64
l = do
    ss@W.StackSet{ W.current = c@W.Screen{ W.workspace = ws }} <- (XState -> WindowSet) -> X WindowSet
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
    handleMessage (W.layout ws) (SomeMessage ReleaseResources)
    windows $ const $ ss{ W.current = c{ W.workspace = ws{ W.layout = l } } }

-- | Signal xmonad to restart itself.
sendRestart :: IO ()
sendRestart :: IO ()
sendRestart = do
    dpy <- WorkspaceId -> IO Display
openDisplay WorkspaceId
""
    rw  <- rootWindow dpy $ defaultScreen dpy
    xmonad_restart <- internAtom dpy "XMONAD_RESTART" False
    allocaXEvent $ \XEventPtr
e -> do
        XEventPtr -> Word32 -> IO ()
setEventType XEventPtr
e Word32
clientMessage
        XEventPtr -> Word64 -> Word64 -> CInt -> [CInt] -> IO ()
setClientMessageEvent' XEventPtr
e Word64
rw Word64
xmonad_restart CInt
32 []
        Display -> Word64 -> Bool -> Word64 -> XEventPtr -> IO ()
sendEvent Display
dpy Word64
rw Bool
False Word64
structureNotifyMask XEventPtr
e
    sync dpy False

-- | Signal compliant window managers to exit.
sendReplace :: IO ()
sendReplace :: IO ()
sendReplace = do
    dpy <- WorkspaceId -> IO Display
openDisplay WorkspaceId
""
    let dflt = Display -> Word32
defaultScreen Display
dpy
    rootw <- rootWindow dpy dflt
    replace dpy dflt rootw

-- | Signal compliant window managers to exit.
replace :: Display -> ScreenNumber -> Window -> IO ()
replace :: Display -> Word32 -> Word64 -> IO ()
replace Display
dpy Word32
dflt Word64
rootw = do
    -- check for other WM
    wmSnAtom <- Display -> WorkspaceId -> Bool -> IO Word64
internAtom Display
dpy (WorkspaceId
"WM_S" WorkspaceId -> WorkspaceId -> WorkspaceId
forall a. [a] -> [a] -> [a]
++ Word32 -> WorkspaceId
forall a. Show a => a -> WorkspaceId
show Word32
dflt) Bool
False
    currentWmSnOwner <- xGetSelectionOwner dpy wmSnAtom
    when (currentWmSnOwner /= 0) $ do
        -- prepare to receive destroyNotify for old WM
        selectInput dpy currentWmSnOwner structureNotifyMask

        -- create off-screen window
        netWmSnOwner <- allocaSetWindowAttributes $ \Ptr SetWindowAttributes
attributes -> do
            Ptr SetWindowAttributes -> Bool -> IO ()
set_override_redirect Ptr SetWindowAttributes
attributes Bool
True
            Ptr SetWindowAttributes -> Word64 -> IO ()
set_event_mask Ptr SetWindowAttributes
attributes Word64
propertyChangeMask
            let screen :: Screen
screen = Display -> Screen
defaultScreenOfDisplay Display
dpy
                visual :: Visual
visual = Screen -> Visual
defaultVisualOfScreen Screen
screen
                attrmask :: Word64
attrmask = Word64
cWOverrideRedirect Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
cWEventMask
            Display
-> Word64
-> Int32
-> Int32
-> Word32
-> Word32
-> CInt
-> CInt
-> CInt
-> Visual
-> Word64
-> Ptr SetWindowAttributes
-> IO Word64
createWindow Display
dpy Word64
rootw (-Int32
100) (-Int32
100) Word32
1 Word32
1 CInt
0 CInt
copyFromParent CInt
copyFromParent Visual
visual Word64
attrmask Ptr SetWindowAttributes
attributes

        -- try to acquire wmSnAtom, this should signal the old WM to terminate
        xSetSelectionOwner dpy wmSnAtom netWmSnOwner currentTime

        -- SKIPPED: check if we acquired the selection
        -- SKIPPED: send client message indicating that we are now the WM

        -- wait for old WM to go away
        fix $ \IO ()
again -> do
            evt <- (XEventPtr -> IO Word32) -> IO Word32
forall a. (XEventPtr -> IO a) -> IO a
allocaXEvent ((XEventPtr -> IO Word32) -> IO Word32)
-> (XEventPtr -> IO Word32) -> IO Word32
forall a b. (a -> b) -> a -> b
$ \XEventPtr
event -> do
                Display -> Word64 -> Word64 -> XEventPtr -> IO ()
windowEvent Display
dpy Word64
currentWmSnOwner Word64
structureNotifyMask XEventPtr
event
                XEventPtr -> IO Word32
get_EventType XEventPtr
event

            when (evt /= destroyNotify) again

------------------------------------------------------------------------
-- Utilities

-- | Return workspace visible on screen @sc@, or 'Nothing'.
screenWorkspace :: ScreenId -> X (Maybe WorkspaceId)
screenWorkspace :: ScreenId -> X (Maybe WorkspaceId)
screenWorkspace ScreenId
sc = (WindowSet -> X (Maybe WorkspaceId)) -> X (Maybe WorkspaceId)
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X (Maybe WorkspaceId)) -> X (Maybe WorkspaceId))
-> (WindowSet -> X (Maybe WorkspaceId)) -> X (Maybe WorkspaceId)
forall a b. (a -> b) -> a -> b
$ Maybe WorkspaceId -> X (Maybe WorkspaceId)
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe WorkspaceId -> X (Maybe WorkspaceId))
-> (WindowSet -> Maybe WorkspaceId)
-> WindowSet
-> X (Maybe WorkspaceId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScreenId -> WindowSet -> Maybe WorkspaceId
forall s i l a sd. Eq s => s -> StackSet i l a s sd -> Maybe i
W.lookupWorkspace ScreenId
sc

-- | Apply an 'X' operation to the currently focused window, if there is one.
withFocused :: (Window -> X ()) -> X ()
withFocused :: (Word64 -> X ()) -> X ()
withFocused Word64 -> X ()
f = (WindowSet -> X ()) -> X ()
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X ()) -> X ()) -> (WindowSet -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowSet
w -> Maybe Word64 -> (Word64 -> X ()) -> X ()
forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust (WindowSet -> Maybe Word64
forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek WindowSet
w) Word64 -> X ()
f

-- | Apply an 'X' operation to all unfocused windows on the current workspace, if there are any.
withUnfocused :: (Window -> X ()) -> X ()
withUnfocused :: (Word64 -> X ()) -> X ()
withUnfocused Word64 -> X ()
f = (WindowSet -> X ()) -> X ()
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X ()) -> X ()) -> (WindowSet -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowSet
ws ->
    Maybe Word64 -> (Word64 -> X ()) -> X ()
forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust (WindowSet -> Maybe Word64
forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek WindowSet
ws) ((Word64 -> X ()) -> X ()) -> (Word64 -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Word64
w ->
        let unfocusedWindows :: [Word64]
unfocusedWindows = (Word64 -> Bool) -> [Word64] -> [Word64]
forall a. (a -> Bool) -> [a] -> [a]
filter (Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64
w) ([Word64] -> [Word64]) -> [Word64] -> [Word64]
forall a b. (a -> b) -> a -> b
$ WindowSet -> [Word64]
forall i l a s sd. StackSet i l a s sd -> [a]
W.index WindowSet
ws
        in (Word64 -> X ()) -> [Word64] -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Word64 -> X ()
f [Word64]
unfocusedWindows

-- | Is the window is under management by xmonad?
isClient :: Window -> X Bool
isClient :: Word64 -> X Bool
isClient Word64
w = (WindowSet -> X Bool) -> X Bool
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X Bool) -> X Bool)
-> (WindowSet -> X Bool) -> X Bool
forall a b. (a -> b) -> a -> b
$ Bool -> X Bool
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> X Bool) -> (WindowSet -> Bool) -> WindowSet -> X Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> WindowSet -> Bool
forall a i l s sd. Eq a => a -> StackSet i l a s sd -> Bool
W.member Word64
w

-- | Combinations of extra modifier masks we need to grab keys\/buttons for.
-- (numlock and capslock)
extraModifiers :: X [KeyMask]
extraModifiers :: X [CUInt]
extraModifiers = do
    nlm <- (XState -> CUInt) -> X CUInt
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> CUInt
numberlockMask
    return [0, nlm, lockMask, nlm .|. lockMask ]

-- | Strip numlock\/capslock from a mask.
cleanMask :: KeyMask -> X KeyMask
cleanMask :: CUInt -> X CUInt
cleanMask CUInt
km = do
    nlm <- (XState -> CUInt) -> X CUInt
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> CUInt
numberlockMask
    return (complement (nlm .|. lockMask) .&. km)

-- | Set the 'Pixel' alpha value to 255.
setPixelSolid :: Pixel -> Pixel
setPixelSolid :: Word64 -> Word64
setPixelSolid Word64
p = Word64
p Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
0xff000000

-- | Get the 'Pixel' value for a named color.
initColor :: Display -> String -> IO (Maybe Pixel)
initColor :: Display -> WorkspaceId -> IO (Maybe Word64)
initColor Display
dpy WorkspaceId
c = (SomeException -> IO (Maybe Word64))
-> IO (Maybe Word64) -> IO (Maybe Word64)
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
C.handle (\(C.SomeException e
_) -> Maybe Word64 -> IO (Maybe Word64)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Word64
forall a. Maybe a
Nothing) (IO (Maybe Word64) -> IO (Maybe Word64))
-> IO (Maybe Word64) -> IO (Maybe Word64)
forall a b. (a -> b) -> a -> b
$
    Word64 -> Maybe Word64
forall a. a -> Maybe a
Just (Word64 -> Maybe Word64)
-> ((Color, Color) -> Word64) -> (Color, Color) -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Word64
setPixelSolid (Word64 -> Word64)
-> ((Color, Color) -> Word64) -> (Color, Color) -> Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color -> Word64
color_pixel (Color -> Word64)
-> ((Color, Color) -> Color) -> (Color, Color) -> Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Color, Color) -> Color
forall a b. (a, b) -> a
fst ((Color, Color) -> Maybe Word64)
-> IO (Color, Color) -> IO (Maybe Word64)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Display -> Word64 -> WorkspaceId -> IO (Color, Color)
allocNamedColor Display
dpy Word64
colormap WorkspaceId
c
    where colormap :: Word64
colormap = Display -> Word32 -> Word64
defaultColormap Display
dpy (Display -> Word32
defaultScreen Display
dpy)

------------------------------------------------------------------------

-- | A type to help serialize xmonad's state to a file.
data StateFile = StateFile
  { StateFile
-> StackSet WorkspaceId WorkspaceId Word64 ScreenId ScreenDetail
sfWins :: W.StackSet  WorkspaceId String Window ScreenId ScreenDetail
  , StateFile -> [(WorkspaceId, WorkspaceId)]
sfExt  :: [(String, String)]
  } deriving (Int -> StateFile -> WorkspaceId -> WorkspaceId
[StateFile] -> WorkspaceId -> WorkspaceId
StateFile -> WorkspaceId
(Int -> StateFile -> WorkspaceId -> WorkspaceId)
-> (StateFile -> WorkspaceId)
-> ([StateFile] -> WorkspaceId -> WorkspaceId)
-> Show StateFile
forall a.
(Int -> a -> WorkspaceId -> WorkspaceId)
-> (a -> WorkspaceId)
-> ([a] -> WorkspaceId -> WorkspaceId)
-> Show a
$cshowsPrec :: Int -> StateFile -> WorkspaceId -> WorkspaceId
showsPrec :: Int -> StateFile -> WorkspaceId -> WorkspaceId
$cshow :: StateFile -> WorkspaceId
show :: StateFile -> WorkspaceId
$cshowList :: [StateFile] -> WorkspaceId -> WorkspaceId
showList :: [StateFile] -> WorkspaceId -> WorkspaceId
Show, ReadPrec [StateFile]
ReadPrec StateFile
Int -> ReadS StateFile
ReadS [StateFile]
(Int -> ReadS StateFile)
-> ReadS [StateFile]
-> ReadPrec StateFile
-> ReadPrec [StateFile]
-> Read StateFile
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS StateFile
readsPrec :: Int -> ReadS StateFile
$creadList :: ReadS [StateFile]
readList :: ReadS [StateFile]
$creadPrec :: ReadPrec StateFile
readPrec :: ReadPrec StateFile
$creadListPrec :: ReadPrec [StateFile]
readListPrec :: ReadPrec [StateFile]
Read)

-- | Write the current window state (and extensible state) to a file
-- so that xmonad can resume with that state intact.
writeStateToFile :: X ()
writeStateToFile :: X ()
writeStateToFile = do
    let maybeShow :: (a, Either WorkspaceId StateExtension) -> Maybe (a, WorkspaceId)
maybeShow (a
t, Right (PersistentExtension a
ext)) = (a, WorkspaceId) -> Maybe (a, WorkspaceId)
forall a. a -> Maybe a
Just (a
t, a -> WorkspaceId
forall a. Show a => a -> WorkspaceId
show a
ext)
        maybeShow (a
t, Left WorkspaceId
str) = (a, WorkspaceId) -> Maybe (a, WorkspaceId)
forall a. a -> Maybe a
Just (a
t, WorkspaceId
str)
        maybeShow (a, Either WorkspaceId StateExtension)
_ = Maybe (a, WorkspaceId)
forall a. Maybe a
Nothing

        wsData :: XState
-> StackSet WorkspaceId WorkspaceId Word64 ScreenId ScreenDetail
wsData   = (Layout Word64 -> WorkspaceId)
-> WindowSet
-> StackSet WorkspaceId WorkspaceId Word64 ScreenId ScreenDetail
forall l l' i a s sd.
(l -> l') -> StackSet i l a s sd -> StackSet i l' a s sd
W.mapLayout Layout Word64 -> WorkspaceId
forall a. Show a => a -> WorkspaceId
show (WindowSet
 -> StackSet WorkspaceId WorkspaceId Word64 ScreenId ScreenDetail)
-> (XState -> WindowSet)
-> XState
-> StackSet WorkspaceId WorkspaceId Word64 ScreenId ScreenDetail
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset
        extState :: XState -> [(WorkspaceId, WorkspaceId)]
extState = ((WorkspaceId, Either WorkspaceId StateExtension)
 -> Maybe (WorkspaceId, WorkspaceId))
-> [(WorkspaceId, Either WorkspaceId StateExtension)]
-> [(WorkspaceId, WorkspaceId)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (WorkspaceId, Either WorkspaceId StateExtension)
-> Maybe (WorkspaceId, WorkspaceId)
forall {a}.
(a, Either WorkspaceId StateExtension) -> Maybe (a, WorkspaceId)
maybeShow ([(WorkspaceId, Either WorkspaceId StateExtension)]
 -> [(WorkspaceId, WorkspaceId)])
-> (XState -> [(WorkspaceId, Either WorkspaceId StateExtension)])
-> XState
-> [(WorkspaceId, WorkspaceId)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map WorkspaceId (Either WorkspaceId StateExtension)
-> [(WorkspaceId, Either WorkspaceId StateExtension)]
forall k a. Map k a -> [(k, a)]
M.toList (Map WorkspaceId (Either WorkspaceId StateExtension)
 -> [(WorkspaceId, Either WorkspaceId StateExtension)])
-> (XState -> Map WorkspaceId (Either WorkspaceId StateExtension))
-> XState
-> [(WorkspaceId, Either WorkspaceId StateExtension)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> Map WorkspaceId (Either WorkspaceId StateExtension)
extensibleState

    path <- (XConf -> WorkspaceId) -> X WorkspaceId
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks ((XConf -> WorkspaceId) -> X WorkspaceId)
-> (XConf -> WorkspaceId) -> X WorkspaceId
forall a b. (a -> b) -> a -> b
$ Directories -> WorkspaceId
stateFileName (Directories -> WorkspaceId)
-> (XConf -> Directories) -> XConf -> WorkspaceId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> Directories
directories
    stateData <- gets (\XState
s -> StackSet WorkspaceId WorkspaceId Word64 ScreenId ScreenDetail
-> [(WorkspaceId, WorkspaceId)] -> StateFile
StateFile (XState
-> StackSet WorkspaceId WorkspaceId Word64 ScreenId ScreenDetail
wsData XState
s) (XState -> [(WorkspaceId, WorkspaceId)]
extState XState
s))
    catchIO (writeFile path $ show stateData)

-- | Read the state of a previous xmonad instance from a file and
-- return that state.  The state file is removed after reading it.
readStateFile :: (LayoutClass l Window, Read (l Window)) => XConfig l -> X (Maybe XState)
readStateFile :: forall (l :: * -> *).
(LayoutClass l Word64, Read (l Word64)) =>
XConfig l -> X (Maybe XState)
readStateFile XConfig l
xmc = do
    path <- (XConf -> WorkspaceId) -> X WorkspaceId
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks ((XConf -> WorkspaceId) -> X WorkspaceId)
-> (XConf -> WorkspaceId) -> X WorkspaceId
forall a b. (a -> b) -> a -> b
$ Directories -> WorkspaceId
stateFileName (Directories -> WorkspaceId)
-> (XConf -> Directories) -> XConf -> WorkspaceId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XConf -> Directories
directories

    -- I'm trying really hard here to make sure we read the entire
    -- contents of the file before it is removed from the file system.
    sf' <- userCode . io $ do
        raw <- withFile path ReadMode readStrict
        return $! maybeRead reads raw

    io (removeFile path)

    return $ do
      sf <- join sf'

      let winset = Layout Word64 -> [WorkspaceId] -> WindowSet -> WindowSet
forall i l a s sd.
Eq i =>
l -> [i] -> StackSet i l a s sd -> StackSet i l a s sd
W.ensureTags Layout Word64
layout (XConfig l -> [WorkspaceId]
forall (l :: * -> *). XConfig l -> [WorkspaceId]
workspaces XConfig l
xmc) (WindowSet -> WindowSet) -> WindowSet -> WindowSet
forall a b. (a -> b) -> a -> b
$ (WorkspaceId -> Layout Word64)
-> StackSet WorkspaceId WorkspaceId Word64 ScreenId ScreenDetail
-> WindowSet
forall l l' i a s sd.
(l -> l') -> StackSet i l a s sd -> StackSet i l' a s sd
W.mapLayout (Layout Word64 -> Maybe (Layout Word64) -> Layout Word64
forall a. a -> Maybe a -> a
fromMaybe Layout Word64
layout (Maybe (Layout Word64) -> Layout Word64)
-> (WorkspaceId -> Maybe (Layout Word64))
-> WorkspaceId
-> Layout Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WorkspaceId -> [(Layout Word64, WorkspaceId)])
-> WorkspaceId -> Maybe (Layout Word64)
forall {t} {a}. (t -> [(a, WorkspaceId)]) -> t -> Maybe a
maybeRead WorkspaceId -> [(Layout Word64, WorkspaceId)]
lreads) (StateFile
-> StackSet WorkspaceId WorkspaceId Word64 ScreenId ScreenDetail
sfWins StateFile
sf)
          extState = [(WorkspaceId, Either WorkspaceId b)]
-> Map WorkspaceId (Either WorkspaceId b)
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(WorkspaceId, Either WorkspaceId b)]
 -> Map WorkspaceId (Either WorkspaceId b))
-> ([(WorkspaceId, WorkspaceId)]
    -> [(WorkspaceId, Either WorkspaceId b)])
-> [(WorkspaceId, WorkspaceId)]
-> Map WorkspaceId (Either WorkspaceId b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((WorkspaceId, WorkspaceId) -> (WorkspaceId, Either WorkspaceId b))
-> [(WorkspaceId, WorkspaceId)]
-> [(WorkspaceId, Either WorkspaceId b)]
forall a b. (a -> b) -> [a] -> [b]
map ((WorkspaceId -> Either WorkspaceId b)
-> (WorkspaceId, WorkspaceId)
-> (WorkspaceId, Either WorkspaceId b)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second WorkspaceId -> Either WorkspaceId b
forall a b. a -> Either a b
Left) ([(WorkspaceId, WorkspaceId)]
 -> Map WorkspaceId (Either WorkspaceId b))
-> [(WorkspaceId, WorkspaceId)]
-> Map WorkspaceId (Either WorkspaceId b)
forall a b. (a -> b) -> a -> b
$ StateFile -> [(WorkspaceId, WorkspaceId)]
sfExt StateFile
sf

      return XState { windowset       = winset
                    , numberlockMask  = 0
                    , mapped          = S.empty
                    , waitingUnmap    = M.empty
                    , dragging        = Nothing
                    , extensibleState = extState
                    }
  where
    layout :: Layout Word64
layout = l Word64 -> Layout Word64
forall a (l :: * -> *).
(LayoutClass l a, Read (l a)) =>
l a -> Layout a
Layout (XConfig l -> l Word64
forall (l :: * -> *). XConfig l -> l Word64
layoutHook XConfig l
xmc)
    lreads :: WorkspaceId -> [(Layout Word64, WorkspaceId)]
lreads = Layout Word64 -> WorkspaceId -> [(Layout Word64, WorkspaceId)]
forall a. Layout a -> WorkspaceId -> [(Layout a, WorkspaceId)]
readsLayout Layout Word64
layout
    maybeRead :: (t -> [(a, WorkspaceId)]) -> t -> Maybe a
maybeRead t -> [(a, WorkspaceId)]
reads' t
s = case t -> [(a, WorkspaceId)]
reads' t
s of
                           [(a
x, WorkspaceId
"")] -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
                           [(a, WorkspaceId)]
_         -> Maybe a
forall a. Maybe a
Nothing

    readStrict :: Handle -> IO String
    readStrict :: Handle -> IO WorkspaceId
readStrict Handle
h = Handle -> IO WorkspaceId
hGetContents Handle
h IO WorkspaceId -> (WorkspaceId -> IO WorkspaceId) -> IO WorkspaceId
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \WorkspaceId
s -> WorkspaceId -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length WorkspaceId
s Int -> IO WorkspaceId -> IO WorkspaceId
forall a b. a -> b -> b
`seq` WorkspaceId -> IO WorkspaceId
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return WorkspaceId
s

-- | @restart name resume@ attempts to restart xmonad by executing the program
-- @name@. If @resume@ is 'True', restart with the current window state.
-- When executing another window manager, @resume@ should be 'False'.
restart :: String -> Bool -> X ()
restart :: WorkspaceId -> Bool -> X ()
restart WorkspaceId
prog Bool
resume = do
    LayoutMessages -> X ()
forall a. Message a => a -> X ()
broadcastMessage LayoutMessages
ReleaseResources
    IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> (Display -> IO ()) -> Display -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Display -> IO ()
flush (Display -> X ()) -> X Display -> X ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (XConf -> Display) -> X Display
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Display
display
    Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
resume X ()
writeStateToFile
    IO () -> X ()
forall (m :: * -> *). MonadIO m => IO () -> m ()
catchIO (WorkspaceId
-> Bool
-> [WorkspaceId]
-> Maybe [(WorkspaceId, WorkspaceId)]
-> IO ()
forall a.
WorkspaceId
-> Bool
-> [WorkspaceId]
-> Maybe [(WorkspaceId, WorkspaceId)]
-> IO a
executeFile WorkspaceId
prog Bool
True [] Maybe [(WorkspaceId, WorkspaceId)]
forall a. Maybe a
Nothing)

------------------------------------------------------------------------
-- Floating layer support

-- | Given a window, find the screen it is located on, and compute
-- the geometry of that window WRT that screen.
floatLocation :: Window -> X (ScreenId, W.RationalRect)
floatLocation :: Word64 -> X (ScreenId, RationalRect)
floatLocation Word64
w =
    X (ScreenId, RationalRect)
-> X (ScreenId, RationalRect) -> X (ScreenId, RationalRect)
forall a. X a -> X a -> X a
catchX X (ScreenId, RationalRect)
go (X (ScreenId, RationalRect) -> X (ScreenId, RationalRect))
-> X (ScreenId, RationalRect) -> X (ScreenId, RationalRect)
forall a b. (a -> b) -> a -> b
$ do
      -- Fallback solution if `go' fails.  Which it might, since it
      -- calls `getWindowAttributes'.
      sc <- (XState
 -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> X (Screen
        WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ((XState
  -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
 -> X (Screen
         WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
-> (XState
    -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> X (Screen
        WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
forall a b. (a -> b) -> a -> b
$ WindowSet
-> Screen WorkspaceId (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
 -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> (XState -> WindowSet)
-> XState
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XState -> WindowSet
windowset
      return (W.screen sc, W.RationalRect 0 0 1 1)

  where go :: X (ScreenId, RationalRect)
go = (Display -> X (ScreenId, RationalRect))
-> X (ScreenId, RationalRect)
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X (ScreenId, RationalRect))
 -> X (ScreenId, RationalRect))
-> (Display -> X (ScreenId, RationalRect))
-> X (ScreenId, RationalRect)
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
          ws <- (XState -> WindowSet) -> X WindowSet
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> WindowSet
windowset
          sh <- io $ getWMNormalHints d w
          wa <- io $ getWindowAttributes d w
          let bw = (CInt -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Word32)
-> (WindowAttributes -> CInt) -> WindowAttributes -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowAttributes -> CInt
wa_border_width) WindowAttributes
wa
          point_sc <- pointScreen (fi $ wa_x wa) (fi $ wa_y wa)
          managed <- isClient w

          -- ignore pointScreen for new windows unless it's the current
          -- screen, otherwise the float's relative size is computed against
          -- a different screen and the float ends up with the wrong size
          let sr_eq = Maybe Rectangle -> Maybe Rectangle -> Bool
forall a. Eq a => a -> a -> Bool
(==) (Maybe Rectangle -> Maybe Rectangle -> Bool)
-> (Maybe (Screen i l a sid ScreenDetail) -> Maybe Rectangle)
-> Maybe (Screen i l a sid ScreenDetail)
-> Maybe (Screen i l a sid ScreenDetail)
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (Screen i l a sid ScreenDetail -> Rectangle)
-> Maybe (Screen i l a sid ScreenDetail) -> Maybe Rectangle
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ScreenDetail -> Rectangle
screenRect (ScreenDetail -> Rectangle)
-> (Screen i l a sid ScreenDetail -> ScreenDetail)
-> Screen i l a sid ScreenDetail
-> Rectangle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen i l a sid ScreenDetail -> ScreenDetail
forall i l a sid sd. Screen i l a sid sd -> sd
W.screenDetail)
              sc = Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Maybe
     (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
forall a. a -> Maybe a -> a
fromMaybe (WindowSet
-> Screen WorkspaceId (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) (Maybe
   (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
 -> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> Maybe
     (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
forall a b. (a -> b) -> a -> b
$
                  if Bool
managed Bool -> Bool -> Bool
|| Maybe
  (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
point_sc Maybe
  (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> Maybe
     (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> Bool
forall {i} {l} {a} {sid}.
Maybe (Screen i l a sid ScreenDetail)
-> Maybe (Screen i l a sid ScreenDetail) -> Bool
`sr_eq` Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Maybe
     (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
forall a. a -> Maybe a
Just (WindowSet
-> Screen WorkspaceId (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) then Maybe
  (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
point_sc else Maybe
  (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
forall a. Maybe a
Nothing
              sr = ScreenDetail -> Rectangle
screenRect (ScreenDetail -> Rectangle)
-> (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
    -> ScreenDetail)
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Rectangle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> ScreenDetail
forall i l a sid sd. Screen i l a sid sd -> sd
W.screenDetail (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> Rectangle)
-> Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Rectangle
forall a b. (a -> b) -> a -> b
$ Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
sc
              x = (CInt -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_x WindowAttributes
wa) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Int32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Int32
rect_x Rectangle
sr)) Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Word32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_width Rectangle
sr)
              y = (CInt -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_y WindowAttributes
wa) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Int32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Int32
rect_y Rectangle
sr)) Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Word32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_height Rectangle
sr)
              (width, height) = applySizeHintsContents sh (wa_width wa, wa_height wa)
              rwidth  = Word32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (Word32
width Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
bwWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
*Word32
2) Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Word32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_width Rectangle
sr)
              rheight = Word32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (Word32
height Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
bwWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
*Word32
2) Integer -> Integer -> Ratio Integer
forall a. Integral a => a -> a -> Ratio a
% Word32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_height Rectangle
sr)
              -- adjust x/y of unmanaged windows if we ignored or didn't get pointScreen,
              -- it might be out of bounds otherwise
              rr = if Bool
managed Bool -> Bool -> Bool
|| Maybe
  (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
point_sc Maybe
  (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> Maybe
     (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> Bool
forall {i} {l} {a} {sid}.
Maybe (Screen i l a sid ScreenDetail)
-> Maybe (Screen i l a sid ScreenDetail) -> Bool
`sr_eq` Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Maybe
     (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
forall a. a -> Maybe a
Just Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
sc
                  then Ratio Integer
-> Ratio Integer -> Ratio Integer -> Ratio Integer -> RationalRect
W.RationalRect Ratio Integer
x Ratio Integer
y Ratio Integer
rwidth Ratio Integer
rheight
                  else Ratio Integer
-> Ratio Integer -> Ratio Integer -> Ratio Integer -> RationalRect
W.RationalRect (Ratio Integer
0.5 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
- Ratio Integer
rwidthRatio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/Ratio Integer
2) (Ratio Integer
0.5 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
- Ratio Integer
rheightRatio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/Ratio Integer
2) Ratio Integer
rwidth Ratio Integer
rheight

          return (W.screen sc, rr)

        fi :: (Integral a, Num b) => a -> b
        fi :: forall a b. (Integral a, Num b) => a -> b
fi = a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral

-- | Given a point, determine the screen (if any) that contains it.
pointScreen :: Position -> Position
            -> X (Maybe (W.Screen WorkspaceId (Layout Window) Window ScreenId ScreenDetail))
pointScreen :: Int32
-> Int32
-> X (Maybe
        (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
pointScreen Int32
x Int32
y = (WindowSet
 -> X (Maybe
         (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)))
-> X (Maybe
        (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet
  -> X (Maybe
          (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)))
 -> X (Maybe
         (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)))
-> (WindowSet
    -> X (Maybe
            (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)))
-> X (Maybe
        (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
forall a b. (a -> b) -> a -> b
$ Maybe
  (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
-> X (Maybe
        (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe
   (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
 -> X (Maybe
         (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)))
-> (WindowSet
    -> Maybe
         (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
-> WindowSet
-> X (Maybe
        (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
 -> Bool)
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
-> Maybe
     (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail
-> Bool
forall {i} {l} {a} {sid}. Screen i l a sid ScreenDetail -> Bool
p ([Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
 -> Maybe
      (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail))
-> (WindowSet
    -> [Screen
          WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail])
-> WindowSet
-> Maybe
     (Screen WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> [Screen
      WorkspaceId (Layout Word64) Word64 ScreenId ScreenDetail]
forall i l a sid sd. StackSet i l a sid sd -> [Screen i l a sid sd]
W.screens
  where p :: Screen i l a sid ScreenDetail -> Bool
p = Int32 -> Int32 -> Rectangle -> Bool
pointWithin Int32
x Int32
y (Rectangle -> Bool)
-> (Screen i l a sid ScreenDetail -> Rectangle)
-> Screen i l a sid ScreenDetail
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScreenDetail -> Rectangle
screenRect (ScreenDetail -> Rectangle)
-> (Screen i l a sid ScreenDetail -> ScreenDetail)
-> Screen i l a sid ScreenDetail
-> Rectangle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen i l a sid ScreenDetail -> ScreenDetail
forall i l a sid sd. Screen i l a sid sd -> sd
W.screenDetail

-- | @pointWithin x y r@ returns 'True' if the @(x, y)@ co-ordinate is within
-- @r@.
pointWithin :: Position -> Position -> Rectangle -> Bool
pointWithin :: Int32 -> Int32 -> Rectangle -> Bool
pointWithin Int32
x Int32
y Rectangle
r = Int32
x Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
>= Rectangle -> Int32
rect_x Rectangle
r Bool -> Bool -> Bool
&&
                    Int32
x Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<  Rectangle -> Int32
rect_x Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Rectangle -> Word32
rect_width Rectangle
r) Bool -> Bool -> Bool
&&
                    Int32
y Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
>= Rectangle -> Int32
rect_y Rectangle
r Bool -> Bool -> Bool
&&
                    Int32
y Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<  Rectangle -> Int32
rect_y Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Rectangle -> Word32
rect_height Rectangle
r)

-- | Make a tiled window floating, using its suggested rectangle
float :: Window -> X ()
float :: Word64 -> X ()
float Word64
w = do
    (sc, rr) <- Word64 -> X (ScreenId, RationalRect)
floatLocation Word64
w
    windows $ \WindowSet
ws -> Word64 -> RationalRect -> WindowSet -> WindowSet
forall a i l s sd.
Ord a =>
a -> RationalRect -> StackSet i l a s sd -> StackSet i l a s sd
W.float Word64
w RationalRect
rr (WindowSet -> WindowSet)
-> (Maybe WindowSet -> WindowSet) -> Maybe WindowSet -> WindowSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet -> Maybe WindowSet -> WindowSet
forall a. a -> Maybe a -> a
fromMaybe WindowSet
ws (Maybe WindowSet -> WindowSet) -> Maybe WindowSet -> WindowSet
forall a b. (a -> b) -> a -> b
$ do
        i  <- Word64 -> WindowSet -> Maybe WorkspaceId
forall a i l s sd. Eq a => a -> StackSet i l a s sd -> Maybe i
W.findTag Word64
w WindowSet
ws
        guard $ i `elem` map (W.tag . W.workspace) (W.screens ws)
        f  <- W.peek ws
        sw <- W.lookupWorkspace sc ws
        return (W.focusWindow f . W.shiftWin sw w $ ws)

-- ---------------------------------------------------------------------
-- Mouse handling

-- | Accumulate mouse motion events
mouseDrag :: (Position -> Position -> X ()) -> X () -> X ()
mouseDrag :: (Int32 -> Int32 -> X ()) -> X () -> X ()
mouseDrag = Maybe Glyph -> (Int32 -> Int32 -> X ()) -> X () -> X ()
mouseDragCursor Maybe Glyph
forall a. Maybe a
Nothing

-- | Like 'mouseDrag', but with the ability to specify a custom cursor
-- shape.
mouseDragCursor :: Maybe Glyph -> (Position -> Position -> X ()) -> X () -> X ()
mouseDragCursor :: Maybe Glyph -> (Int32 -> Int32 -> X ()) -> X () -> X ()
mouseDragCursor Maybe Glyph
cursorGlyph Int32 -> Int32 -> X ()
f X ()
done = do
    drag <- (XState -> Maybe (Int32 -> Int32 -> X (), X ()))
-> X (Maybe (Int32 -> Int32 -> X (), X ()))
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState -> Maybe (Int32 -> Int32 -> X (), X ())
dragging
    case drag of
        Just (Int32 -> Int32 -> X (), X ())
_ -> () -> X ()
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return () -- error case? we're already dragging
        Maybe (Int32 -> Int32 -> X (), X ())
Nothing -> do
            XConf { theRoot = root, display = d } <- X XConf
forall r (m :: * -> *). MonadReader r m => m r
ask
            io $ do cursor <- maybe (pure none) (createFontCursor d) cursorGlyph
                    grabPointer d root False (buttonReleaseMask .|. pointerMotionMask)
                      grabModeAsync grabModeAsync none cursor currentTime
            modify $ \XState
s -> XState
s { dragging = Just (motion, cleanup) }
 where
    cleanup :: X ()
cleanup = do
        (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> (Display -> IO ()) -> Display -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Display -> Word64 -> IO ()) -> Word64 -> Display -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Display -> Word64 -> IO ()
ungrabPointer Word64
currentTime
        (XState -> XState) -> X ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((XState -> XState) -> X ()) -> (XState -> XState) -> X ()
forall a b. (a -> b) -> a -> b
$ \XState
s -> XState
s { dragging = Nothing }
        X ()
done
    motion :: Int32 -> Int32 -> X ()
motion Int32
x Int32
y = do z <- Int32 -> Int32 -> X ()
f Int32
x Int32
y
                    clearEvents pointerMotionMask
                    return z

-- | Drag the window under the cursor with the mouse while it is dragged.
mouseMoveWindow :: Window -> X ()
mouseMoveWindow :: Word64 -> X ()
mouseMoveWindow Word64
w = X Bool -> X () -> X ()
whenX (Word64 -> X Bool
isClient Word64
w) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
    wa <- IO WindowAttributes -> X WindowAttributes
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO WindowAttributes -> X WindowAttributes)
-> IO WindowAttributes -> X WindowAttributes
forall a b. (a -> b) -> a -> b
$ Display -> Word64 -> IO WindowAttributes
getWindowAttributes Display
d Word64
w
    (_, _, _, ox', oy', _, _, _) <- io $ queryPointer d w
    let ox = CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
ox'
        oy = CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
oy'
    mouseDragCursor
              (Just xC_fleur)
              (\Int32
ex Int32
ey -> do
                  IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Word64 -> Int32 -> Int32 -> IO ()
moveWindow Display
d Word64
w (Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (WindowAttributes -> CInt
wa_x WindowAttributes
wa) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Int32
ex Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
ox)))
                                      (Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (WindowAttributes -> CInt
wa_y WindowAttributes
wa) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Int32
ey Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
oy)))
                  Word64 -> X ()
float Word64
w
              )
              (float w)

-- | Resize the window under the cursor with the mouse while it is dragged.
mouseResizeWindow :: Window -> X ()
mouseResizeWindow :: Word64 -> X ()
mouseResizeWindow Word64
w = X Bool -> X () -> X ()
whenX (Word64 -> X Bool
isClient Word64
w) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
    wa <- IO WindowAttributes -> X WindowAttributes
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO WindowAttributes -> X WindowAttributes)
-> IO WindowAttributes -> X WindowAttributes
forall a b. (a -> b) -> a -> b
$ Display -> Word64 -> IO WindowAttributes
getWindowAttributes Display
d Word64
w
    sh <- io $ getWMNormalHints d w
    io $ warpPointer d none w 0 0 0 0 (fromIntegral (wa_width wa)) (fromIntegral (wa_height wa))
    mouseDragCursor
              (Just xC_bottom_right_corner)
              (\Int32
ex Int32
ey -> do
                 IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Word64 -> Word32 -> Word32 -> IO ()
resizeWindow Display
d Word64
w (Word32 -> Word32 -> IO ()) -> (Word32, Word32) -> IO ()
forall a b c. (a -> b -> c) -> (a, b) -> c
`uncurry`
                    SizeHints -> (Int32, Int32) -> (Word32, Word32)
forall a. Integral a => SizeHints -> (a, a) -> (Word32, Word32)
applySizeHintsContents SizeHints
sh (Int32
ex Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (WindowAttributes -> CInt
wa_x WindowAttributes
wa),
                                               Int32
ey Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (WindowAttributes -> CInt
wa_y WindowAttributes
wa))
                 Word64 -> X ()
float Word64
w)
              (float w)

-- ---------------------------------------------------------------------
-- Support for window size hints

-- | An alias for a (width, height) pair
type D = (Dimension, Dimension)

-- | Given a window, build an adjuster function that will reduce the given
-- dimensions according to the window's border width and size hints.
mkAdjust :: Window -> X (D -> D)
mkAdjust :: Word64 -> X ((Word32, Word32) -> (Word32, Word32))
mkAdjust Word64
w = (Display -> X ((Word32, Word32) -> (Word32, Word32)))
-> X ((Word32, Word32) -> (Word32, Word32))
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ((Word32, Word32) -> (Word32, Word32)))
 -> X ((Word32, Word32) -> (Word32, Word32)))
-> (Display -> X ((Word32, Word32) -> (Word32, Word32)))
-> X ((Word32, Word32) -> (Word32, Word32))
forall a b. (a -> b) -> a -> b
$ \Display
d -> IO ((Word32, Word32) -> (Word32, Word32))
-> X ((Word32, Word32) -> (Word32, Word32))
forall a. IO a -> X a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ((Word32, Word32) -> (Word32, Word32))
 -> X ((Word32, Word32) -> (Word32, Word32)))
-> IO ((Word32, Word32) -> (Word32, Word32))
-> X ((Word32, Word32) -> (Word32, Word32))
forall a b. (a -> b) -> a -> b
$ do
    sh <- Display -> Word64 -> IO SizeHints
getWMNormalHints Display
d Word64
w
    wa <- C.try $ getWindowAttributes d w
    case wa of
         Left (SomeException
_ :: C.SomeException) -> ((Word32, Word32) -> (Word32, Word32))
-> IO ((Word32, Word32) -> (Word32, Word32))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Word32, Word32) -> (Word32, Word32)
forall a. a -> a
id
         Right WindowAttributes
wa' ->
            let bw :: Word32
bw = CInt -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Word32) -> CInt -> Word32
forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_border_width WindowAttributes
wa'
            in  ((Word32, Word32) -> (Word32, Word32))
-> IO ((Word32, Word32) -> (Word32, Word32))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (((Word32, Word32) -> (Word32, Word32))
 -> IO ((Word32, Word32) -> (Word32, Word32)))
-> ((Word32, Word32) -> (Word32, Word32))
-> IO ((Word32, Word32) -> (Word32, Word32))
forall a b. (a -> b) -> a -> b
$ Word32 -> SizeHints -> (Word32, Word32) -> (Word32, Word32)
forall a.
Integral a =>
Word32 -> SizeHints -> (a, a) -> (Word32, Word32)
applySizeHints Word32
bw SizeHints
sh

-- | Reduce the dimensions if needed to comply to the given SizeHints, taking
-- window borders into account.
applySizeHints :: Integral a => Dimension -> SizeHints -> (a, a) -> D
applySizeHints :: forall a.
Integral a =>
Word32 -> SizeHints -> (a, a) -> (Word32, Word32)
applySizeHints Word32
bw SizeHints
sh =
    (Word32 -> Word32) -> (Word32, Word32) -> (Word32, Word32)
forall {t} {b}. (t -> b) -> (t, t) -> (b, b)
tmap (Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
2 Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
bw) ((Word32, Word32) -> (Word32, Word32))
-> ((a, a) -> (Word32, Word32)) -> (a, a) -> (Word32, Word32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SizeHints -> (a, a) -> (Word32, Word32)
forall a. Integral a => SizeHints -> (a, a) -> (Word32, Word32)
applySizeHintsContents SizeHints
sh ((a, a) -> (Word32, Word32))
-> ((a, a) -> (a, a)) -> (a, a) -> (Word32, Word32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> (a, a) -> (a, a)
forall {t} {b}. (t -> b) -> (t, t) -> (b, b)
tmap (a -> a -> a
forall a. Num a => a -> a -> a
subtract (a -> a -> a) -> a -> a -> a
forall a b. (a -> b) -> a -> b
$ a
2 a -> a -> a
forall a. Num a => a -> a -> a
* Word32 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
bw)
    where
    tmap :: (t -> b) -> (t, t) -> (b, b)
tmap t -> b
f (t
x, t
y) = (t -> b
f t
x, t -> b
f t
y)

-- | Reduce the dimensions if needed to comply to the given SizeHints.
applySizeHintsContents :: Integral a => SizeHints -> (a, a) -> D
applySizeHintsContents :: forall a. Integral a => SizeHints -> (a, a) -> (Word32, Word32)
applySizeHintsContents SizeHints
sh (a
w, a
h) =
    SizeHints -> (Word32, Word32) -> (Word32, Word32)
applySizeHints' SizeHints
sh (a -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a -> Word32) -> a -> Word32
forall a b. (a -> b) -> a -> b
$ a -> a -> a
forall a. Ord a => a -> a -> a
max a
1 a
w, a -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a -> Word32) -> a -> Word32
forall a b. (a -> b) -> a -> b
$ a -> a -> a
forall a. Ord a => a -> a -> a
max a
1 a
h)

-- | Use X11 size hints to scale a pair of dimensions.
applySizeHints' :: SizeHints -> D -> D
applySizeHints' :: SizeHints -> (Word32, Word32) -> (Word32, Word32)
applySizeHints' SizeHints
sh =
      ((Word32, Word32) -> (Word32, Word32))
-> ((Word32, Word32) -> (Word32, Word32) -> (Word32, Word32))
-> Maybe (Word32, Word32)
-> (Word32, Word32)
-> (Word32, Word32)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Word32, Word32) -> (Word32, Word32)
forall a. a -> a
id (Word32, Word32) -> (Word32, Word32) -> (Word32, Word32)
applyMaxSizeHint                   (SizeHints -> Maybe (Word32, Word32)
sh_max_size   SizeHints
sh)
    ((Word32, Word32) -> (Word32, Word32))
-> ((Word32, Word32) -> (Word32, Word32))
-> (Word32, Word32)
-> (Word32, Word32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Word32, Word32) -> (Word32, Word32))
-> ((Word32, Word32) -> (Word32, Word32) -> (Word32, Word32))
-> Maybe (Word32, Word32)
-> (Word32, Word32)
-> (Word32, Word32)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Word32, Word32) -> (Word32, Word32)
forall a. a -> a
id (\(Word32
bw, Word32
bh) (Word32
w, Word32
h) -> (Word32
wWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+Word32
bw, Word32
hWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+Word32
bh)) (SizeHints -> Maybe (Word32, Word32)
sh_base_size  SizeHints
sh)
    ((Word32, Word32) -> (Word32, Word32))
-> ((Word32, Word32) -> (Word32, Word32))
-> (Word32, Word32)
-> (Word32, Word32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Word32, Word32) -> (Word32, Word32))
-> ((Word32, Word32) -> (Word32, Word32) -> (Word32, Word32))
-> Maybe (Word32, Word32)
-> (Word32, Word32)
-> (Word32, Word32)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Word32, Word32) -> (Word32, Word32)
forall a. a -> a
id (Word32, Word32) -> (Word32, Word32) -> (Word32, Word32)
applyResizeIncHint                 (SizeHints -> Maybe (Word32, Word32)
sh_resize_inc SizeHints
sh)
    ((Word32, Word32) -> (Word32, Word32))
-> ((Word32, Word32) -> (Word32, Word32))
-> (Word32, Word32)
-> (Word32, Word32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Word32, Word32) -> (Word32, Word32))
-> (((Word32, Word32), (Word32, Word32))
    -> (Word32, Word32) -> (Word32, Word32))
-> Maybe ((Word32, Word32), (Word32, Word32))
-> (Word32, Word32)
-> (Word32, Word32)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Word32, Word32) -> (Word32, Word32)
forall a. a -> a
id ((Word32, Word32), (Word32, Word32))
-> (Word32, Word32) -> (Word32, Word32)
applyAspectHint                    (SizeHints -> Maybe ((Word32, Word32), (Word32, Word32))
sh_aspect     SizeHints
sh)
    ((Word32, Word32) -> (Word32, Word32))
-> ((Word32, Word32) -> (Word32, Word32))
-> (Word32, Word32)
-> (Word32, Word32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Word32, Word32) -> (Word32, Word32))
-> ((Word32, Word32) -> (Word32, Word32) -> (Word32, Word32))
-> Maybe (Word32, Word32)
-> (Word32, Word32)
-> (Word32, Word32)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Word32, Word32) -> (Word32, Word32)
forall a. a -> a
id (\(Word32
bw,Word32
bh) (Word32
w,Word32
h)   -> (Word32
wWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
-Word32
bw, Word32
hWord32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
-Word32
bh)) (SizeHints -> Maybe (Word32, Word32)
sh_base_size  SizeHints
sh)

-- | Reduce the dimensions so their aspect ratio falls between the two given aspect ratios.
applyAspectHint :: (D, D) -> D -> D
applyAspectHint :: ((Word32, Word32), (Word32, Word32))
-> (Word32, Word32) -> (Word32, Word32)
applyAspectHint ((Word32
minx, Word32
miny), (Word32
maxx, Word32
maxy)) x :: (Word32, Word32)
x@(Word32
w,Word32
h)
    | [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Word32
minx Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
< Word32
1, Word32
miny Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
< Word32
1, Word32
maxx Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
< Word32
1, Word32
maxy Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
< Word32
1] = (Word32, Word32)
x
    | Word32
w Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
maxy Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
h Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
maxx                         = (Word32
h Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
maxx Word32 -> Word32 -> Word32
forall a. Integral a => a -> a -> a
`div` Word32
maxy, Word32
h)
    | Word32
w Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
miny Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
< Word32
h Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
minx                         = (Word32
w, Word32
w Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
miny Word32 -> Word32 -> Word32
forall a. Integral a => a -> a -> a
`div` Word32
minx)
    | Bool
otherwise                                   = (Word32, Word32)
x

-- | Reduce the dimensions so they are a multiple of the size increments.
applyResizeIncHint :: D -> D -> D
applyResizeIncHint :: (Word32, Word32) -> (Word32, Word32) -> (Word32, Word32)
applyResizeIncHint (Word32
iw,Word32
ih) x :: (Word32, Word32)
x@(Word32
w,Word32
h) =
    if Word32
iw Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
0 Bool -> Bool -> Bool
&& Word32
ih Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
0 then (Word32
w Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
w Word32 -> Word32 -> Word32
forall a. Integral a => a -> a -> a
`mod` Word32
iw, Word32
h Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
h Word32 -> Word32 -> Word32
forall a. Integral a => a -> a -> a
`mod` Word32
ih) else (Word32, Word32)
x

-- | Reduce the dimensions if they exceed the given maximum dimensions.
applyMaxSizeHint  :: D -> D -> D
applyMaxSizeHint :: (Word32, Word32) -> (Word32, Word32) -> (Word32, Word32)
applyMaxSizeHint (Word32
mw,Word32
mh) x :: (Word32, Word32)
x@(Word32
w,Word32
h) =
    if Word32
mw Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
0 Bool -> Bool -> Bool
&& Word32
mh Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
0 then (Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
min Word32
w Word32
mw,Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
min Word32
h Word32
mh) else (Word32, Word32)
x