    -----------------------------------------------------------------------------
    -- |
    -- Module       : XMonad.Actions.FloatKeys
    -- Copyright    : (c) Karsten Schoelzel <kuser@gmx.de>
    -- License      : BSD
    --
    -- Maintainer   : Karsten Schoelzel <kuser@gmx.de>
    -- Stability    : stable
    -- Portability  : unportable
    --
    -- Move and resize floating windows.
    -----------------------------------------------------------------------------

    module XMonad.Actions.FloatKeys (
                    -- * Usage
                    -- $usage
                    keysMoveWindow,
                    keysMoveWindowTo,
                    keysResizeWindow,
                    keysAbsResizeWindow,
                    P, G,
                    ) where

    import XMonad

    -- $usage
    -- You can use this module with the following in your @~\/.xmonad\/xmonad.hs@:
    --
    -- >    import XMonad.Actions.FloatKeys
    --
    -- Then add appropriate key bindings, for example:
    --
    -- >  , ((modm,               xK_d     ), withFocused (keysResizeWindow (-10,-10) (1,1)))
    -- >  , ((modm,               xK_s     ), withFocused (keysResizeWindow (10,10) (1,1)))
    -- >  , ((modm .|. shiftMask, xK_d     ), withFocused (keysAbsResizeWindow (-10,-10) (1024,752)))
    -- >  , ((modm .|. shiftMask, xK_s     ), withFocused (keysAbsResizeWindow (10,10) (1024,752)))
    -- >  , ((modm,               xK_a     ), withFocused (keysMoveWindowTo (512,384) (1%2,1%2)))
    --
    -- For detailed instructions on editing your key bindings, see
    -- "XMonad.Doc.Extending#Editing_key_bindings".

    -- | @keysMoveWindow (dx, dy)@ moves the window by @dx@ pixels to the
    --   right and @dy@ pixels down.
    keysMoveWindow :: D -> Window -> X ()
    keysMoveWindow :: D -> Window -> X ()
    keysMoveWindow (dx :: Dimension
    dx,dy :: Dimension
    dy) w :: Window
    w = X Bool -> X () -> X ()
    whenX (Window -> X Bool
    isClient Window
    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
    $ \d :: Display
    d -> 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 -> Window -> IO ()
    raiseWindow Display
    d Window
    w
        WindowAttributes
    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 -> Window -> IO WindowAttributes
    getWindowAttributes Display
    d Window
    w
        IO () -> X ()
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
    io (IO () -> X ()) -> IO () -> X ()
    forall a b. (a -> b) -> a -> b
    $ Display -> Window -> Position -> Position -> IO ()
    moveWindow Display
    d Window
    w (Dimension -> Position
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (CInt -> Dimension
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (WindowAttributes -> CInt
    wa_x WindowAttributes
    wa) Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    + Dimension
    dx))
                            (Dimension -> Position
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (CInt -> Dimension
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (WindowAttributes -> CInt
    wa_y WindowAttributes
    wa) Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    + Dimension
    dy))
        Window -> X ()
    float Window
    w

    -- | @keysMoveWindowTo (x, y) (gx, gy)@ moves the window relative
    --   point @(gx, gy)@ to the point @(x,y)@, where @(gx,gy)@ gives a
    --   position relative to the window border, i.e.  @gx = 0@ is the left
    --   border, @gx = 1@ is the right border, @gy = 0@ is the top border, and
    --   @gy = 1@ the bottom border.
    --
    --   For example, on a 1024x768 screen:
    --
    -- > keysMoveWindowTo (512,384) (1%2, 1%2) -- center the window on screen
    -- > keysMoveWindowTo (1024,0) (1, 0)      -- put window in the top right corner
    keysMoveWindowTo :: P -> G -> Window -> X ()
    keysMoveWindowTo :: P -> G -> Window -> X ()
    keysMoveWindowTo (x :: Position
    x,y :: Position
    y) (gx :: Rational
    gx, gy :: Rational
    gy) w :: Window
    w = X Bool -> X () -> X ()
    whenX (Window -> X Bool
    isClient Window
    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
    $ \d :: Display
    d -> 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 -> Window -> IO ()
    raiseWindow Display
    d Window
    w
        WindowAttributes
    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 -> Window -> IO WindowAttributes
    getWindowAttributes Display
    d Window
    w
        IO () -> X ()
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
    io (IO () -> X ()) -> IO () -> X ()
    forall a b. (a -> b) -> a -> b
    $ Display -> Window -> Position -> Position -> IO ()
    moveWindow Display
    d Window
    w (Position
    x Position -> Position -> Position
    forall a. Num a => a -> a -> a
    - Rational -> Position
    forall a b. (RealFrac a, Integral b) => a -> b
    round (Rational
    gx Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    * CInt -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (WindowAttributes -> CInt
    wa_width WindowAttributes
    wa)))
                            (Position
    y Position -> Position -> Position
    forall a. Num a => a -> a -> a
    - Rational -> Position
    forall a b. (RealFrac a, Integral b) => a -> b
    round (Rational
    gy Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    * CInt -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (WindowAttributes -> CInt
    wa_height WindowAttributes
    wa)))
        Window -> X ()
    float Window
    w

    type G = (Rational, Rational)
    type P = (Position, Position)

    -- | @keysResizeWindow (dx, dy) (gx, gy)@ changes the width by @dx@
    --   and the height by @dy@, leaving the window-relative point @(gx,
    --   gy)@ fixed.
    --
    --   For example:
    --
    -- > keysResizeWindow (10, 0) (0, 0)      -- make the window 10 pixels larger to the right
    -- > keysResizeWindow (10, 0) (0, 1%2)    -- does the same, unless sizeHints are applied
    -- > keysResizeWindow (10, 10) (1%2, 1%2) -- add 5 pixels on each side
    -- > keysResizeWindow (-10, -10) (0, 1)   -- shrink the window in direction of the bottom-left corner
    keysResizeWindow :: D -> G -> Window -> X ()
    keysResizeWindow :: D -> G -> Window -> X ()
    keysResizeWindow = (SizeHints -> P -> D -> D -> G -> (P, D))
    -> D -> G -> Window -> X ()
    forall a b.
    (SizeHints -> P -> D -> a -> b -> (P, D))
    -> a -> b -> Window -> X ()
    keysMoveResize SizeHints -> P -> D -> D -> G -> (P, D)
    keysResizeWindow'

    -- | @keysAbsResizeWindow (dx, dy) (ax, ay)@ changes the width by @dx@
    --   and the height by @dy@, leaving the screen absolute point @(ax,
    --   ay)@ fixed.
    --
    --   For example:
    --
    -- > keysAbsResizeWindow (10, 10) (0, 0)   -- enlarge the window; if it is not in the top-left corner it will also be moved down and to the right.
    keysAbsResizeWindow :: D -> D -> Window -> X ()
    keysAbsResizeWindow :: D -> D -> Window -> X ()
    keysAbsResizeWindow = (SizeHints -> P -> D -> D -> D -> (P, D))
    -> D -> D -> Window -> X ()
    forall a b.
    (SizeHints -> P -> D -> a -> b -> (P, D))
    -> a -> b -> Window -> X ()
    keysMoveResize SizeHints -> P -> D -> D -> D -> (P, D)
    keysAbsResizeWindow'

    keysAbsResizeWindow' :: SizeHints -> P -> D -> D -> D -> (P,D)
    keysAbsResizeWindow' :: SizeHints -> P -> D -> D -> D -> (P, D)
    keysAbsResizeWindow' sh :: SizeHints
    sh (x :: Position
    x,y :: Position
    y) (w :: Dimension
    w,h :: Dimension
    h) (dx :: Dimension
    dx,dy :: Dimension
    dy) (ax :: Dimension
    ax, ay :: Dimension
    ay) = ((Rational -> Position
    forall a b. (RealFrac a, Integral b) => a -> b
    round Rational
    nx, Rational -> Position
    forall a b. (RealFrac a, Integral b) => a -> b
    round Rational
    ny), (Dimension
    nw, Dimension
    nh))
        where
            (nw :: Dimension
    nw, nh :: Dimension
    nh) = SizeHints -> D -> D
    forall a. Integral a => SizeHints -> (a, a) -> D
    applySizeHintsContents SizeHints
    sh (Dimension
    w Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    + Dimension
    dx, Dimension
    h Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    + Dimension
    dy)
            nx :: Rational
            nx :: Rational
    nx = Dimension -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Dimension
    ax Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    * Dimension
    w Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    + Dimension
    nw Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    * (Position -> Dimension
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Position
    x Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    - Dimension
    ax)) Rational -> Rational -> Rational
    forall a. Fractional a => a -> a -> a
    / Dimension -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Dimension
    w
            ny :: Rational
            ny :: Rational
    ny = Dimension -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Dimension
    ay Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    * Dimension
    h Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    + Dimension
    nh Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    * (Position -> Dimension
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Position
    y Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    - Dimension
    ay)) Rational -> Rational -> Rational
    forall a. Fractional a => a -> a -> a
    / Dimension -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Dimension
    h

    keysResizeWindow' :: SizeHints -> P -> D -> D -> G -> (P,D)
    keysResizeWindow' :: SizeHints -> P -> D -> D -> G -> (P, D)
    keysResizeWindow' sh :: SizeHints
    sh (x :: Position
    x,y :: Position
    y) (w :: Dimension
    w,h :: Dimension
    h) (dx :: Dimension
    dx,dy :: Dimension
    dy) (gx :: Rational
    gx, gy :: Rational
    gy) = ((Position
    nx, Position
    ny), (Dimension
    nw, Dimension
    nh))
        where
            (nw :: Dimension
    nw, nh :: Dimension
    nh) = SizeHints -> D -> D
    forall a. Integral a => SizeHints -> (a, a) -> D
    applySizeHintsContents SizeHints
    sh (Dimension
    w Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    + Dimension
    dx, Dimension
    h Dimension -> Dimension -> Dimension
    forall a. Num a => a -> a -> a
    + Dimension
    dy)
            nx :: Position
    nx = Rational -> Position
    forall a b. (RealFrac a, Integral b) => a -> b
    round (Rational -> Position) -> Rational -> Position
    forall a b. (a -> b) -> a -> b
    $ Position -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Position
    x Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    + Rational
    gx Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    * Dimension -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Dimension
    w Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    - Rational
    gx Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    * Dimension -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Dimension
    nw
            ny :: Position
    ny = Rational -> Position
    forall a b. (RealFrac a, Integral b) => a -> b
    round (Rational -> Position) -> Rational -> Position
    forall a b. (a -> b) -> a -> b
    $ Position -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Position
    y Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    + Rational
    gy Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    * Dimension -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Dimension
    h Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    - Rational
    gy Rational -> Rational -> Rational
    forall a. Num a => a -> a -> a
    * Dimension -> Rational
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Dimension
    nh

    keysMoveResize :: (SizeHints -> P -> D -> a -> b -> (P,D)) -> a -> b -> Window -> X ()
    keysMoveResize :: (SizeHints -> P -> D -> a -> b -> (P, D))
    -> a -> b -> Window -> X ()
    keysMoveResize f :: SizeHints -> P -> D -> a -> b -> (P, D)
    f move :: a
    move resize :: b
    resize w :: Window
    w = X Bool -> X () -> X ()
    whenX (Window -> X Bool
    isClient Window
    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
    $ \d :: Display
    d -> 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 -> Window -> IO ()
    raiseWindow Display
    d Window
    w
        WindowAttributes
    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 -> Window -> IO WindowAttributes
    getWindowAttributes Display
    d Window
    w
        SizeHints
    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 -> Window -> IO SizeHints
    getWMNormalHints Display
    d Window
    w
        let wa_dim :: D
    wa_dim = (CInt -> Dimension
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (CInt -> Dimension) -> CInt -> Dimension
    forall a b. (a -> b) -> a -> b
    $ WindowAttributes -> CInt
    wa_width WindowAttributes
    wa, CInt -> Dimension
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (CInt -> Dimension) -> CInt -> Dimension
    forall a b. (a -> b) -> a -> b
    $ WindowAttributes -> CInt
    wa_height WindowAttributes
    wa)
            wa_pos :: P
    wa_pos = (CInt -> Position
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (CInt -> Position) -> CInt -> Position
    forall a b. (a -> b) -> a -> b
    $ WindowAttributes -> CInt
    wa_x WindowAttributes
    wa, CInt -> Position
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (CInt -> Position) -> CInt -> Position
    forall a b. (a -> b) -> a -> b
    $ WindowAttributes -> CInt
    wa_y WindowAttributes
    wa)
            (wn_pos :: P
    wn_pos, wn_dim :: D
    wn_dim) = SizeHints -> P -> D -> a -> b -> (P, D)
    f SizeHints
    sh P
    wa_pos D
    wa_dim a
    move b
    resize
        IO () -> X ()
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
    io (IO () -> X ()) -> IO () -> X ()
    forall a b. (a -> b) -> a -> b
    $ Display -> Window -> Dimension -> Dimension -> IO ()
    resizeWindow Display
    d Window
    w (Dimension -> Dimension -> IO ()) -> D -> IO ()
    forall a b c. (a -> b -> c) -> (a, b) -> c
    `uncurry` D
    wn_dim
        IO () -> X ()
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
    io (IO () -> X ()) -> IO () -> X ()
    forall a b. (a -> b) -> a -> b
    $ Display -> Window -> Position -> Position -> IO ()
    moveWindow Display
    d Window
    w (Position -> Position -> IO ()) -> P -> IO ()
    forall a b c. (a -> b -> c) -> (a, b) -> c
    `uncurry` P
    wn_pos
        Window -> X ()
    float Window
    w