-----------------------------------------------------------------------------
-- |
-- Module      :  Window
-- Copyright   :  (c) 2011-18, 2020-22 Jose A. Ortega Ruiz
--             :  (c) 2012 Jochen Keil
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Jose A. Ortega Ruiz <jao@gnu.org>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Window manipulation functions
--
-----------------------------------------------------------------------------

module Xmobar.X11.Window where

import qualified Control.Monad as CM

import qualified Data.Function as DF
import qualified Data.List as DL
import qualified Data.Maybe as DM

import qualified Graphics.X11.Xlib as X
import qualified Graphics.X11.Xlib.Extras as Xx

import qualified Graphics.X11.Xinerama as Xi
import qualified Foreign.C.Types as C

import qualified System.Posix.Process as PP

import qualified Xmobar.Config.Types as T
import qualified Xmobar.X11.Text as Txt

-- $window

-- | Creates a window with the attribute override_redirect set to True.
-- Windows Managers should not touch this kind of windows.
newWindow ::
  X.Display -> X.Screen -> X.Window -> X.Rectangle -> Bool -> IO X.Window
newWindow :: Display -> Screen -> Word64 -> Rectangle -> Bool -> IO Word64
newWindow Display
dpy Screen
scr Word64
rw (X.Rectangle Int32
x Int32
y Word32
w Word32
h) Bool
o = do
  let visual :: Visual
visual = Screen -> Visual
X.defaultVisualOfScreen Screen
scr
      attrmask :: Word64
attrmask = if Bool
o then Word64
X.cWOverrideRedirect else Word64
0
  (Ptr SetWindowAttributes -> IO Word64) -> IO Word64
forall a. (Ptr SetWindowAttributes -> IO a) -> IO a
X.allocaSetWindowAttributes ((Ptr SetWindowAttributes -> IO Word64) -> IO Word64)
-> (Ptr SetWindowAttributes -> IO Word64) -> IO Word64
forall a b. (a -> b) -> a -> b
$
         \Ptr SetWindowAttributes
attributes -> do
           Ptr SetWindowAttributes -> Bool -> IO ()
X.set_override_redirect Ptr SetWindowAttributes
attributes Bool
o
           Display
-> Word64
-> Int32
-> Int32
-> Word32
-> Word32
-> CInt
-> CInt
-> CInt
-> Visual
-> Word64
-> Ptr SetWindowAttributes
-> IO Word64
X.createWindow Display
dpy Word64
rw Int32
x Int32
y Word32
w Word32
h CInt
0 (Screen -> CInt
X.defaultDepthOfScreen Screen
scr)
                        CInt
X.inputOutput Visual
visual Word64
attrmask Ptr SetWindowAttributes
attributes

-- | The function to create the initial window
createWin :: X.Display -> Txt.XFont -> T.Config -> IO (X.Rectangle, X.Window)
createWin :: Display -> XFont -> Config -> IO (Rectangle, Word64)
createWin Display
d XFont
fs Config
c = do
  let dflt :: Word32
dflt = Display -> Word32
X.defaultScreen Display
d
  srs <- Display -> IO [Rectangle]
Xi.getScreenInfo Display
d
  rootw <- X.rootWindow d dflt
  (as,ds) <- Txt.textExtents fs "0"
  let ht = Int32
as Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
ds Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
4
      r = Config -> XPosition -> [Rectangle] -> Word32 -> Rectangle
setPosition Config
c (Config -> XPosition
T.position Config
c) [Rectangle]
srs (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
ht)
  win <- newWindow  d (X.defaultScreenOfDisplay d) rootw r (T.overrideRedirect c)
  setProperties c d win
  setStruts r c d win srs
  CM.when (T.lowerOnStart c) $ X.lowerWindow d win
  CM.unless (T.hideOnStart c) $ showWindow r c d win
  return (r,win)

-- | Updates the size and position of the window
repositionWin :: X.Display -> X.Window -> Txt.XFont -> T.Config -> IO X.Rectangle
repositionWin :: Display -> Word64 -> XFont -> Config -> IO Rectangle
repositionWin Display
d Word64
win XFont
fs Config
c = do
  srs <- Display -> IO [Rectangle]
Xi.getScreenInfo Display
d
  (as,ds) <- Txt.textExtents fs "0"
  let ht = Int32
as Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
ds Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
4
      r = Config -> XPosition -> [Rectangle] -> Word32 -> Rectangle
setPosition Config
c (Config -> XPosition
T.position Config
c) [Rectangle]
srs (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
ht)
  X.moveResizeWindow d win
    (X.rect_x r) (X.rect_y r) (X.rect_width r) (X.rect_height r)
  setStruts r c d win srs
  X.sync d False
  return r

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

setPosition ::
  T.Config -> T.XPosition -> [X.Rectangle] -> X.Dimension -> X.Rectangle
setPosition :: Config -> XPosition -> [Rectangle] -> Word32 -> Rectangle
setPosition Config
c XPosition
p [Rectangle]
rs Word32
ht =
  case XPosition
p' of
    XPosition
T.Top -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle Int32
rx Int32
ry Word32
rw Word32
h
    T.TopP Int
l Int
r -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle (Int32
rx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
l) Int32
ry (Word32
rw Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
l Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
r) Word32
h
    T.TopH Int
ch -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle Int32
rx Int32
ry Word32
rw (Int -> Word32
forall {a}. Integral a => a -> Word32
mh Int
ch)
    T.TopHM Int
ch Int
l Int
r Int
t Int
_ ->
      Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle (Int32
rx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
l) (Int32
ry Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
t) (Word32
rw Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
l Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
r) (Int -> Word32
forall {a}. Integral a => a -> Word32
mh Int
ch)
    T.TopW Align
a Int
i -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle (Align -> Int -> Int32
forall {b}. Integral b => Align -> b -> Int32
ax Align
a Int
i) Int32
ry (Int -> Word32
nw Int
i) Word32
h
    T.TopSize Align
a Int
i Int
ch -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle (Align -> Int -> Int32
forall {b}. Integral b => Align -> b -> Int32
ax Align
a Int
i) Int32
ry (Int -> Word32
nw Int
i) (Int -> Word32
forall {a}. Integral a => a -> Word32
mh Int
ch)
    XPosition
T.Bottom -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle Int32
rx Int32
ny Word32
rw Word32
h
    T.BottomH Int
ch -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle Int32
rx (Int -> Int32
forall {a}. Integral a => a -> Int32
ny' Int
ch) Word32
rw (Int -> Word32
forall {a}. Integral a => a -> Word32
mh Int
ch)
    T.BottomHM Int
ch Int
l Int
r Int
_ Int
b ->
      Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle (Int32
rx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
l) (Int32
ry Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
rh Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
b Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Int -> Word32
forall {a}. Integral a => a -> Word32
mh Int
ch)) (Word32
rw Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
l Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
r) (Int -> Word32
forall {a}. Integral a => a -> Word32
mh Int
ch)
    T.BottomW Align
a Int
i -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle (Align -> Int -> Int32
forall {b}. Integral b => Align -> b -> Int32
ax Align
a Int
i) Int32
ny (Int -> Word32
nw Int
i) Word32
h
    T.BottomP Int
l Int
r -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle (Int32
rx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
l) Int32
ny (Word32
rw Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
l Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
r) Word32
h
    T.BottomSize Align
a Int
i Int
ch  -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle (Align -> Int -> Int32
forall {b}. Integral b => Align -> b -> Int32
ax Align
a Int
i) (Int -> Int32
forall {a}. Integral a => a -> Int32
ny' Int
ch) (Int -> Word32
nw Int
i) (Int -> Word32
forall {a}. Integral a => a -> Word32
mh Int
ch)
    T.Static Int
cx Int
cy Int
cw Int
ch -> Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
X.Rectangle (Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
cx) (Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int
cy) (Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
cw) (Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int
ch)
    T.OnScreen Int
_ XPosition
p'' -> Config -> XPosition -> [Rectangle] -> Word32 -> Rectangle
setPosition Config
c XPosition
p'' [Rectangle
scr] Word32
ht
  where
    (scr :: Rectangle
scr@(X.Rectangle Int32
rx Int32
ry Word32
rw Word32
rh), XPosition
p') =
      case XPosition
p of T.OnScreen Int
i XPosition
x -> (Rectangle -> Maybe Rectangle -> Rectangle
forall a. a -> Maybe a -> a
DM.fromMaybe ([Rectangle] -> Rectangle
picker [Rectangle]
rs) (Maybe Rectangle -> Rectangle) -> Maybe Rectangle -> Rectangle
forall a b. (a -> b) -> a -> b
$ Int -> [Rectangle] -> Maybe Rectangle
forall {a} {b}. (Eq a, Num a, Enum a) => a -> [b] -> Maybe b
safeIndex Int
i [Rectangle]
rs, XPosition
x)
                XPosition
_ -> ([Rectangle] -> Rectangle
picker [Rectangle]
rs, XPosition
p)
    ny :: Int32
ny = Int32
ry Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Word32
rh Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
ht)
    center :: a -> Int32
center a
i = Int32
rx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Word32 -> Word32 -> Word32
forall a. Integral a => a -> a -> a
div (a -> Word32
forall {a}. Integral a => a -> Word32
remwid a
i) Word32
2)
    right :: a -> Int32
right  a
i = Int32
rx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (a -> Word32
forall {a}. Integral a => a -> Word32
remwid a
i)
    remwid :: a -> Word32
remwid a
i = Word32
rw Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32 -> Word32
pw (a -> Word32
forall a b. (Integral a, Num b) => a -> b
fi a
i)
    ax :: Align -> b -> Int32
ax Align
T.L = Int32 -> b -> Int32
forall a b. a -> b -> a
const Int32
rx
    ax Align
T.R = b -> Int32
forall {a}. Integral a => a -> Int32
right
    ax Align
T.C = b -> Int32
forall {a}. Integral a => a -> Int32
center
    pw :: Word32 -> Word32
pw Word32
i = Word32
rw Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
min Word32
100 Word32
i Word32 -> Word32 -> Word32
forall a. Integral a => a -> a -> a
`div` Word32
100
    nw :: Int -> Word32
nw = Word32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Word32 -> Word32) -> (Int -> Word32) -> Int -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Word32
pw (Word32 -> Word32) -> (Int -> Word32) -> Int -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi
    h :: Word32
h = Word32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Word32
ht
    mh :: a -> Word32
mh a
h' = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max (a -> Word32
forall a b. (Integral a, Num b) => a -> b
fi a
h') Word32
h
    ny' :: a -> Int32
ny' a
h' = Int32
ry Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Word32
rh Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- a -> Word32
forall {a}. Integral a => a -> Word32
mh a
h')
    safeIndex :: a -> [b] -> Maybe b
safeIndex a
i = a -> [(a, b)] -> Maybe b
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup a
i ([(a, b)] -> Maybe b) -> ([b] -> [(a, b)]) -> [b] -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [b] -> [(a, b)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a
0..]
    picker :: [Rectangle] -> Rectangle
picker = if Config -> Bool
T.pickBroadest Config
c
             then (Rectangle -> Rectangle -> Ordering) -> [Rectangle] -> Rectangle
forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
DL.maximumBy (Word32 -> Word32 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Word32 -> Word32 -> Ordering)
-> (Rectangle -> Word32) -> Rectangle -> Rectangle -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`DF.on` Rectangle -> Word32
X.rect_width)
             else [Rectangle] -> Rectangle
forall a. HasCallStack => [a] -> a
head

setProperties :: T.Config -> X.Display -> X.Window -> IO ()
setProperties :: Config -> Display -> Word64 -> IO ()
setProperties Config
c Display
d Word64
w = do
  let mkatom :: String -> IO Word64
mkatom String
n = Display -> String -> Bool -> IO Word64
X.internAtom Display
d String
n Bool
False
  card <- String -> IO Word64
mkatom String
"CARDINAL"
  atom <- mkatom "ATOM"

  X.setTextProperty d w (T.wmClass c) X.wM_CLASS
  X.setTextProperty d w (T.wmName c) X.wM_NAME

  wtype <- mkatom "_NET_WM_WINDOW_TYPE"
  dock <- mkatom "_NET_WM_WINDOW_TYPE_DOCK"
  Xx.changeProperty32 d w wtype atom Xx.propModeReplace [fi dock]

  CM.when (T.allDesktops c) $ do
    desktop <- mkatom "_NET_WM_DESKTOP"
    Xx.changeProperty32 d w desktop card Xx.propModeReplace [0xffffffff]

  pid  <- mkatom "_NET_WM_PID"
  PP.getProcessID >>=
    Xx.changeProperty32 d w pid card Xx.propModeReplace . return . fi

setStruts' :: X.Display -> X.Window -> [C.CLong] -> IO ()
setStruts' :: Display -> Word64 -> [CLong] -> IO ()
setStruts' Display
d Word64
w [CLong]
svs = do
  let mkatom :: String -> IO Word64
mkatom String
n = Display -> String -> Bool -> IO Word64
X.internAtom Display
d String
n Bool
False
  card <- String -> IO Word64
mkatom String
"CARDINAL"
  pstrut <- mkatom "_NET_WM_STRUT_PARTIAL"
  strut <- mkatom "_NET_WM_STRUT"
  Xx.changeProperty32 d w pstrut card Xx.propModeReplace svs
  Xx.changeProperty32 d w strut card Xx.propModeReplace (take 4 svs)

setStruts ::
  X.Rectangle -> T.Config -> X.Display -> X.Window -> [X.Rectangle] -> IO ()
setStruts :: Rectangle -> Config -> Display -> Word64 -> [Rectangle] -> IO ()
setStruts Rectangle
r Config
c Display
d Word64
w [Rectangle]
rs = do
  let svs :: [CLong]
svs = (Int -> CLong) -> [Int] -> [CLong]
forall a b. (a -> b) -> [a] -> [b]
map Int -> CLong
forall a b. (Integral a, Num b) => a -> b
fi ([Int] -> [CLong]) -> [Int] -> [CLong]
forall a b. (a -> b) -> a -> b
$ Rectangle -> XPosition -> Int -> [Int]
getStrutValues Rectangle
r (Config -> XPosition
T.position Config
c) ([Rectangle] -> Int
getRootWindowHeight [Rectangle]
rs)
  Display -> Word64 -> [CLong] -> IO ()
setStruts' Display
d Word64
w [CLong]
svs

getRootWindowHeight :: [X.Rectangle] -> Int
getRootWindowHeight :: [Rectangle] -> Int
getRootWindowHeight [Rectangle]
srs = [Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ((Rectangle -> Int) -> [Rectangle] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Rectangle -> Int
forall {a}. Num a => Rectangle -> a
getMaxScreenYCoord [Rectangle]
srs)
  where
    getMaxScreenYCoord :: Rectangle -> a
getMaxScreenYCoord Rectangle
sr = Int32 -> a
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Int32
X.rect_y Rectangle
sr) a -> a -> a
forall a. Num a => a -> a -> a
+ Word32 -> a
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
X.rect_height Rectangle
sr)

getStrutValues :: X.Rectangle -> T.XPosition -> Int -> [Int]
getStrutValues :: Rectangle -> XPosition -> Int -> [Int]
getStrutValues r :: Rectangle
r@(X.Rectangle Int32
x Int32
y Word32
w Word32
h) XPosition
p Int
rwh =
  case XPosition
p of
    T.OnScreen Int
_ XPosition
p'      -> Rectangle -> XPosition -> Int -> [Int]
getStrutValues Rectangle
r XPosition
p' Int
rwh
    XPosition
T.Top                -> [Int
0, Int
0, Int
st  , Int
0   , Int
0, Int
0, Int
0, Int
0, Int
nx, Int
nw, Int
0 , Int
0 ]
    T.TopH    Int
_          -> [Int
0, Int
0, Int
st  , Int
0   , Int
0, Int
0, Int
0, Int
0, Int
nx, Int
nw, Int
0 , Int
0 ]
    T.TopHM Int
_ Int
_ Int
_ Int
_ Int
b    -> [Int
0, Int
0, Int
stInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
b, Int
0   , Int
0, Int
0, Int
0, Int
0, Int
nx, Int
nw, Int
0 , Int
0 ]
    T.TopP    Int
_ Int
_        -> [Int
0, Int
0, Int
st  , Int
0   , Int
0, Int
0, Int
0, Int
0, Int
nx, Int
nw, Int
0 , Int
0 ]
    T.TopW    Align
_ Int
_        -> [Int
0, Int
0, Int
st  , Int
0   , Int
0, Int
0, Int
0, Int
0, Int
nx, Int
nw, Int
0 , Int
0 ]
    T.TopSize      {}    -> [Int
0, Int
0, Int
st  , Int
0   , Int
0, Int
0, Int
0, Int
0, Int
nx, Int
nw, Int
0 , Int
0 ]
    XPosition
T.Bottom             -> [Int
0, Int
0, Int
0   , Int
sb  , Int
0, Int
0, Int
0, Int
0, Int
0 , Int
0 , Int
nx, Int
nw]
    T.BottomH Int
_          -> [Int
0, Int
0, Int
0   , Int
sb  , Int
0, Int
0, Int
0, Int
0, Int
0 , Int
0 , Int
nx, Int
nw]
    T.BottomHM Int
_ Int
_ Int
_ Int
t Int
_ -> [Int
0, Int
0, Int
0   , Int
sbInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
t, Int
0, Int
0, Int
0, Int
0, Int
0 , Int
0 , Int
nx, Int
nw]
    T.BottomP Int
_ Int
_        -> [Int
0, Int
0, Int
0   , Int
sb  , Int
0, Int
0, Int
0, Int
0, Int
0 , Int
0 , Int
nx, Int
nw]
    T.BottomW Align
_ Int
_        -> [Int
0, Int
0, Int
0   , Int
sb  , Int
0, Int
0, Int
0, Int
0, Int
0 , Int
0 , Int
nx, Int
nw]
    T.BottomSize   {}    -> [Int
0, Int
0, Int
0   , Int
sb  , Int
0, Int
0, Int
0, Int
0, Int
0 , Int
0 , Int
nx, Int
nw]
    T.Static       {}    -> XPosition -> Int -> [Int]
getStaticStrutValues XPosition
p Int
rwh
  where st :: Int
st = Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fi Int32
y Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fi Word32
h
        sb :: Int
sb = Int
rwh Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fi Int32
y
        nx :: Int
nx = Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fi Int32
x
        nw :: Int
nw = Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fi (Int32
x Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
w Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
1)

-- get some reaonable strut values for static placement.
getStaticStrutValues :: T.XPosition -> Int -> [Int]
getStaticStrutValues :: XPosition -> Int -> [Int]
getStaticStrutValues (T.Static Int
cx Int
cy Int
cw Int
ch) Int
rwh
    -- if the yPos is in the top half of the screen, then assume a Top
    -- placement, otherwise, it's a Bottom placement
    | Int
cy Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< (Int
rwh Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) = [Int
0, Int
0, Int
st,  Int
0, Int
0, Int
0, Int
0, Int
0, Int
xs, Int
xe,  Int
0,  Int
0]
    | Bool
otherwise = [Int
0, Int
0,  Int
0, Int
sb, Int
0, Int
0, Int
0, Int
0,  Int
0,  Int
0, Int
xs, Int
xe]
    where st :: Int
st = Int
cy Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
ch
          sb :: Int
sb = Int
rwh Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
cy
          xs :: Int
xs = Int
cx -- a simple calculation for horizontal (x) placement
          xe :: Int
xe = Int
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
cw Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
getStaticStrutValues XPosition
_ Int
_ = [Int
0, Int
0, Int
0, Int
0, Int
0, Int
0, Int
0, Int
0, Int
0, Int
0, Int
0, Int
0]

hideWindow :: X.Display -> X.Window -> IO ()
hideWindow :: Display -> Word64 -> IO ()
hideWindow Display
d Word64
w = do
    Display -> Word64 -> [CLong] -> IO ()
setStruts' Display
d Word64
w (Int -> CLong -> [CLong]
forall a. Int -> a -> [a]
replicate Int
12 CLong
0)
    Display -> Word64 -> IO ()
Xx.unmapWindow Display
d Word64
w IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Display -> Bool -> IO ()
X.sync Display
d Bool
False

showWindow :: X.Rectangle -> T.Config -> X.Display -> X.Window -> IO ()
showWindow :: Rectangle -> Config -> Display -> Word64 -> IO ()
showWindow Rectangle
r Config
c Display
d Word64
w = do
    Display -> Word64 -> IO ()
X.mapWindow Display
d Word64
w
    Display -> IO [Rectangle]
Xi.getScreenInfo Display
d IO [Rectangle] -> ([Rectangle] -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Rectangle -> Config -> Display -> Word64 -> [Rectangle] -> IO ()
setStruts Rectangle
r Config
c Display
d Word64
w
    Display -> Bool -> IO ()
X.sync Display
d Bool
False

isMapped :: X.Display -> X.Window -> IO Bool
isMapped :: Display -> Word64 -> IO Bool
isMapped Display
d Word64
w = WindowAttributes -> Bool
ism (WindowAttributes -> Bool) -> IO WindowAttributes -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Display -> Word64 -> IO WindowAttributes
Xx.getWindowAttributes Display
d Word64
w
    where ism :: WindowAttributes -> Bool
ism Xx.WindowAttributes { wa_map_state :: WindowAttributes -> CInt
Xx.wa_map_state = CInt
wms } = CInt
wms CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
Xx.waIsUnmapped