module XMonad.Util.Replace
(
replace
) where
import XMonad
import XMonad.Prelude
replace :: IO ()
replace :: IO ()
replace = do
Display
dpy <- String -> IO Display
openDisplay String
""
let dflt :: ScreenNumber
dflt = Display -> ScreenNumber
defaultScreen Display
dpy
Window
rootw <- Display -> ScreenNumber -> IO Window
rootWindow Display
dpy ScreenNumber
dflt
Window
wmSnAtom <- Display -> String -> Bool -> IO Window
internAtom Display
dpy (String
"WM_S" String -> String -> String
forall a. [a] -> [a] -> [a]
++ ScreenNumber -> String
forall a. Show a => a -> String
show ScreenNumber
dflt) Bool
False
Window
currentWmSnOwner <- Display -> Window -> IO Window
xGetSelectionOwner Display
dpy Window
wmSnAtom
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Window
currentWmSnOwner Window -> Window -> Bool
forall a. Eq a => a -> a -> Bool
/= Window
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"Screen " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ScreenNumber -> String
forall a. Show a => a -> String
show ScreenNumber
dflt String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" on display \""
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Display -> String
displayString Display
dpy String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\" already has a window manager."
Display -> Window -> Window -> IO ()
selectInput Display
dpy Window
currentWmSnOwner Window
structureNotifyMask
Window
netWmSnOwner <- (Ptr SetWindowAttributes -> IO Window) -> IO Window
forall a. (Ptr SetWindowAttributes -> IO a) -> IO a
allocaSetWindowAttributes ((Ptr SetWindowAttributes -> IO Window) -> IO Window)
-> (Ptr SetWindowAttributes -> IO Window) -> IO Window
forall a b. (a -> b) -> a -> b
$ \Ptr SetWindowAttributes
attributes -> do
Ptr SetWindowAttributes -> Bool -> IO ()
set_override_redirect Ptr SetWindowAttributes
attributes Bool
True
Ptr SetWindowAttributes -> Window -> IO ()
set_event_mask Ptr SetWindowAttributes
attributes Window
propertyChangeMask
let screen :: Screen
screen = Display -> Screen
defaultScreenOfDisplay Display
dpy
let visual :: Visual
visual = Screen -> Visual
defaultVisualOfScreen Screen
screen
let attrmask :: Window
attrmask = Window
cWOverrideRedirect Window -> Window -> Window
forall a. Bits a => a -> a -> a
.|. Window
cWEventMask
Display
-> Window
-> Position
-> Position
-> ScreenNumber
-> ScreenNumber
-> CInt
-> CInt
-> CInt
-> Visual
-> Window
-> Ptr SetWindowAttributes
-> IO Window
createWindow Display
dpy Window
rootw (-Position
100) (-Position
100) ScreenNumber
1 ScreenNumber
1 CInt
0 CInt
copyFromParent CInt
copyFromParent Visual
visual Window
attrmask Ptr SetWindowAttributes
attributes
String -> IO ()
putStrLn String
"Replacing existing window manager..."
Display -> Window -> Window -> Window -> IO ()
xSetSelectionOwner Display
dpy Window
wmSnAtom Window
netWmSnOwner Window
currentTime
String -> IO ()
putStr String
"Waiting for other window manager to terminate... "
(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
ScreenNumber
evt <- (XEventPtr -> IO ScreenNumber) -> IO ScreenNumber
forall a. (XEventPtr -> IO a) -> IO a
allocaXEvent ((XEventPtr -> IO ScreenNumber) -> IO ScreenNumber)
-> (XEventPtr -> IO ScreenNumber) -> IO ScreenNumber
forall a b. (a -> b) -> a -> b
$ \XEventPtr
event -> do
Display -> Window -> Window -> XEventPtr -> IO ()
windowEvent Display
dpy Window
currentWmSnOwner Window
structureNotifyMask XEventPtr
event
XEventPtr -> IO ScreenNumber
get_EventType XEventPtr
event
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ScreenNumber
evt ScreenNumber -> ScreenNumber -> Bool
forall a. Eq a => a -> a -> Bool
/= ScreenNumber
destroyNotify) IO ()
again
String -> IO ()
putStrLn String
"done"
Display -> IO ()
closeDisplay Display
dpy