{-# LANGUAGE MultiParamTypeClasses, PatternGuards, TypeSynonymInstances #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.MouseResize
-- Description :  A layout modifier to resize windows with the mouse.
-- Copyright   :  (c) 2007 Andrea Rossato
-- License     :  BSD-style (see xmonad/LICENSE)
--
-- Maintainer  :  andrea.rossato@unibz.it
-- Stability   :  unstable
-- Portability :  unportable
--
-- A layout modifier to resize windows with the mouse by grabbing the
-- window's lower right corner.
--
-- This module must be used together with "XMonad.Layout.WindowArranger".
-----------------------------------------------------------------------------

module XMonad.Actions.MouseResize
    ( -- * Usage:
      -- $usage
      mouseResize
    , MouseResize (..)
    ) where

import XMonad
import XMonad.Layout.Decoration

import XMonad.Layout.WindowArranger
import XMonad.Util.XUtils

-- $usage
-- Usually this module is used to create layouts, but you can also use
-- it to resize windows in any layout, together with the
-- "XMonad.Layout.WindowArranger". For usage example see
-- "XMonad.Layout.SimpleFloat" or "XMonad.Layout.DecorationMadness".
--
-- You can use this module with the following in your
-- @xmonad.hs@:
--
-- > import XMonad.Actions.MouseResize
-- > import XMonad.Layout.WindowArranger
--
-- Then edit your @layoutHook@ by modifying a given layout:
--
-- > myLayout = mouseResize $ windowArrange $ layoutHook def
--
-- and then:
--
-- > main = xmonad def { layoutHook = myLayout }
--
-- For more detailed instructions on editing the layoutHook see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial> and
-- "XMonad.Doc.Extending#Editing_the_layout_hook".

mouseResize :: l a -> ModifiedLayout MouseResize l a
mouseResize :: forall (l :: * -> *) a. l a -> ModifiedLayout MouseResize l a
mouseResize = MouseResize a -> l a -> ModifiedLayout MouseResize l a
forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout ([((a, Rectangle), Maybe a)] -> MouseResize a
forall a. [((a, Rectangle), Maybe a)] -> MouseResize a
MR [])

newtype MouseResize a = MR [((a,Rectangle),Maybe a)]
instance Show (MouseResize a) where show :: MouseResize a -> String
show        MouseResize a
_ = String
""
instance Read (MouseResize a) where readsPrec :: Int -> ReadS (MouseResize a)
readsPrec Int
_ String
s = [([((a, Rectangle), Maybe a)] -> MouseResize a
forall a. [((a, Rectangle), Maybe a)] -> MouseResize a
MR [], String
s)]

instance LayoutModifier MouseResize Window where
    redoLayout :: MouseResize Word64
-> Rectangle
-> Maybe (Stack Word64)
-> [(Word64, Rectangle)]
-> X ([(Word64, Rectangle)], Maybe (MouseResize Word64))
redoLayout MouseResize Word64
_       Rectangle
_ Maybe (Stack Word64)
Nothing  [(Word64, Rectangle)]
wrs = ([(Word64, Rectangle)], Maybe (MouseResize Word64))
-> X ([(Word64, Rectangle)], Maybe (MouseResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Word64, Rectangle)]
wrs, Maybe (MouseResize Word64)
forall a. Maybe a
Nothing)
    redoLayout (MR [((Word64, Rectangle), Maybe Word64)]
st) Rectangle
_ (Just Stack Word64
s) [(Word64, Rectangle)]
wrs
        | [] <- [((Word64, Rectangle), Maybe Word64)]
st  = X [((Word64, Rectangle), Maybe Word64)]
initState    X [((Word64, Rectangle), Maybe Word64)]
-> ([((Word64, Rectangle), Maybe Word64)]
    -> X ([(Word64, Rectangle)], Maybe (MouseResize Word64)))
-> X ([(Word64, Rectangle)], Maybe (MouseResize Word64))
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[((Word64, Rectangle), Maybe Word64)]
nst -> ([(Word64, Rectangle)], Maybe (MouseResize Word64))
-> X ([(Word64, Rectangle)], Maybe (MouseResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Word64, Rectangle)]
wrs, MouseResize Word64 -> Maybe (MouseResize Word64)
forall a. a -> Maybe a
Just (MouseResize Word64 -> Maybe (MouseResize Word64))
-> MouseResize Word64 -> Maybe (MouseResize Word64)
forall a b. (a -> b) -> a -> b
$ [((Word64, Rectangle), Maybe Word64)] -> MouseResize Word64
forall a. [((a, Rectangle), Maybe a)] -> MouseResize a
MR [((Word64, Rectangle), Maybe Word64)]
nst)
        | Bool
otherwise = X [((Word64, Rectangle), Maybe Word64)]
processState X [((Word64, Rectangle), Maybe Word64)]
-> ([((Word64, Rectangle), Maybe Word64)]
    -> X ([(Word64, Rectangle)], Maybe (MouseResize Word64)))
-> X ([(Word64, Rectangle)], Maybe (MouseResize Word64))
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[((Word64, Rectangle), Maybe Word64)]
nst -> ([(Word64, Rectangle)], Maybe (MouseResize Word64))
-> X ([(Word64, Rectangle)], Maybe (MouseResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Word64, Rectangle)]
wrs, MouseResize Word64 -> Maybe (MouseResize Word64)
forall a. a -> Maybe a
Just (MouseResize Word64 -> Maybe (MouseResize Word64))
-> MouseResize Word64 -> Maybe (MouseResize Word64)
forall a b. (a -> b) -> a -> b
$ [((Word64, Rectangle), Maybe Word64)] -> MouseResize Word64
forall a. [((a, Rectangle), Maybe a)] -> MouseResize a
MR [((Word64, Rectangle), Maybe Word64)]
nst)
        where
          wrs' :: [((Word64, Rectangle), Maybe Rectangle)]
wrs'         = [Rectangle]
-> [(Word64, Rectangle)]
-> [((Word64, Rectangle), Maybe Rectangle)]
forall {a}.
[Rectangle]
-> [(a, Rectangle)] -> [((a, Rectangle), Maybe Rectangle)]
wrs_to_state [] ([(Word64, Rectangle)] -> [((Word64, Rectangle), Maybe Rectangle)])
-> ([(Word64, Rectangle)] -> [(Word64, Rectangle)])
-> [(Word64, Rectangle)]
-> [((Word64, Rectangle), Maybe Rectangle)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Word64, Rectangle) -> Bool)
-> [(Word64, Rectangle)] -> [(Word64, Rectangle)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Stack Word64 -> Word64 -> Bool
forall a. Eq a => Stack a -> a -> Bool
isInStack Stack Word64
s (Word64 -> Bool)
-> ((Word64, Rectangle) -> Word64) -> (Word64, Rectangle) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word64, Rectangle) -> Word64
forall a b. (a, b) -> a
fst) ([(Word64, Rectangle)] -> [((Word64, Rectangle), Maybe Rectangle)])
-> [(Word64, Rectangle)]
-> [((Word64, Rectangle), Maybe Rectangle)]
forall a b. (a -> b) -> a -> b
$ [(Word64, Rectangle)]
wrs
          initState :: X [((Word64, Rectangle), Maybe Word64)]
initState    = (((Word64, Rectangle), Maybe Rectangle)
 -> X ((Word64, Rectangle), Maybe Word64))
-> [((Word64, Rectangle), Maybe Rectangle)]
-> X [((Word64, Rectangle), Maybe Word64)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((Word64, Rectangle), Maybe Rectangle)
-> X ((Word64, Rectangle), Maybe Word64)
createInputWindow [((Word64, Rectangle), Maybe Rectangle)]
wrs'
          processState :: X [((Word64, Rectangle), Maybe Word64)]
processState = (((Word64, Rectangle), Maybe Word64) -> X ())
-> [((Word64, Rectangle), Maybe Word64)] -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Maybe Word64 -> X ()
deleteInputWin (Maybe Word64 -> X ())
-> (((Word64, Rectangle), Maybe Word64) -> Maybe Word64)
-> ((Word64, Rectangle), Maybe Word64)
-> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Word64, Rectangle), Maybe Word64) -> Maybe Word64
forall a b. (a, b) -> b
snd) [((Word64, Rectangle), Maybe Word64)]
st X ()
-> X [((Word64, Rectangle), Maybe Word64)]
-> X [((Word64, Rectangle), Maybe Word64)]
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (((Word64, Rectangle), Maybe Rectangle)
 -> X ((Word64, Rectangle), Maybe Word64))
-> [((Word64, Rectangle), Maybe Rectangle)]
-> X [((Word64, Rectangle), Maybe Word64)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((Word64, Rectangle), Maybe Rectangle)
-> X ((Word64, Rectangle), Maybe Word64)
createInputWindow [((Word64, Rectangle), Maybe Rectangle)]
wrs'

          inputRectangle :: Rectangle -> Rectangle
inputRectangle (Rectangle Int32
x Int32
y Word32
wh Word32
ht) = Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle (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
wh Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
5) (Int32
y Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
ht Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
5) Word32
10 Word32
10

          wrs_to_state :: [Rectangle]
-> [(a, Rectangle)] -> [((a, Rectangle), Maybe Rectangle)]
wrs_to_state [Rectangle]
rs ((a
w,Rectangle
r):[(a, Rectangle)]
xs)
              | Rectangle
ir Rectangle -> [Rectangle] -> Bool
`isVisible` [Rectangle]
rs = ((a
w,Rectangle
r),Rectangle -> Maybe Rectangle
forall a. a -> Maybe a
Just Rectangle
ir) ((a, Rectangle), Maybe Rectangle)
-> [((a, Rectangle), Maybe Rectangle)]
-> [((a, Rectangle), Maybe Rectangle)]
forall a. a -> [a] -> [a]
: [Rectangle]
-> [(a, Rectangle)] -> [((a, Rectangle), Maybe Rectangle)]
wrs_to_state (Rectangle
rRectangle -> [Rectangle] -> [Rectangle]
forall a. a -> [a] -> [a]
:Rectangle
irRectangle -> [Rectangle] -> [Rectangle]
forall a. a -> [a] -> [a]
:[Rectangle]
rs) [(a, Rectangle)]
xs
              | Bool
otherwise         = ((a
w,Rectangle
r),Maybe Rectangle
forall a. Maybe a
Nothing) ((a, Rectangle), Maybe Rectangle)
-> [((a, Rectangle), Maybe Rectangle)]
-> [((a, Rectangle), Maybe Rectangle)]
forall a. a -> [a] -> [a]
: [Rectangle]
-> [(a, Rectangle)] -> [((a, Rectangle), Maybe Rectangle)]
wrs_to_state (Rectangle
rRectangle -> [Rectangle] -> [Rectangle]
forall a. a -> [a] -> [a]
:   [Rectangle]
rs) [(a, Rectangle)]
xs
              where ir :: Rectangle
ir = Rectangle -> Rectangle
inputRectangle Rectangle
r
          wrs_to_state [Rectangle]
_ [] = []

    handleMess :: MouseResize Word64 -> SomeMessage -> X (Maybe (MouseResize Word64))
handleMess (MR [((Word64, Rectangle), Maybe Word64)]
s) SomeMessage
m
        | Just Event
e <- SomeMessage -> Maybe Event
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m :: Maybe Event = [((Word64, Rectangle), Maybe Word64)] -> Event -> X ()
handleResize [((Word64, Rectangle), Maybe Word64)]
s Event
e X ()
-> X (Maybe (MouseResize Word64)) -> X (Maybe (MouseResize Word64))
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe (MouseResize Word64) -> X (Maybe (MouseResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (MouseResize Word64)
forall a. Maybe a
Nothing
        | Just LayoutMessages
Hide             <- SomeMessage -> Maybe LayoutMessages
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m = X ()
releaseResources X ()
-> X (Maybe (MouseResize Word64)) -> X (Maybe (MouseResize Word64))
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe (MouseResize Word64) -> X (Maybe (MouseResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (MouseResize Word64 -> Maybe (MouseResize Word64)
forall a. a -> Maybe a
Just (MouseResize Word64 -> Maybe (MouseResize Word64))
-> MouseResize Word64 -> Maybe (MouseResize Word64)
forall a b. (a -> b) -> a -> b
$ [((Word64, Rectangle), Maybe Word64)] -> MouseResize Word64
forall a. [((a, Rectangle), Maybe a)] -> MouseResize a
MR [])
        | Just LayoutMessages
ReleaseResources <- SomeMessage -> Maybe LayoutMessages
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m = X ()
releaseResources X ()
-> X (Maybe (MouseResize Word64)) -> X (Maybe (MouseResize Word64))
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe (MouseResize Word64) -> X (Maybe (MouseResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (MouseResize Word64 -> Maybe (MouseResize Word64)
forall a. a -> Maybe a
Just (MouseResize Word64 -> Maybe (MouseResize Word64))
-> MouseResize Word64 -> Maybe (MouseResize Word64)
forall a b. (a -> b) -> a -> b
$ [((Word64, Rectangle), Maybe Word64)] -> MouseResize Word64
forall a. [((a, Rectangle), Maybe a)] -> MouseResize a
MR [])
        where releaseResources :: X ()
releaseResources = (((Word64, Rectangle), Maybe Word64) -> X ())
-> [((Word64, Rectangle), Maybe Word64)] -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Maybe Word64 -> X ()
deleteInputWin (Maybe Word64 -> X ())
-> (((Word64, Rectangle), Maybe Word64) -> Maybe Word64)
-> ((Word64, Rectangle), Maybe Word64)
-> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Word64, Rectangle), Maybe Word64) -> Maybe Word64
forall a b. (a, b) -> b
snd) [((Word64, Rectangle), Maybe Word64)]
s
    handleMess MouseResize Word64
_ SomeMessage
_ = Maybe (MouseResize Word64) -> X (Maybe (MouseResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (MouseResize Word64)
forall a. Maybe a
Nothing

handleResize :: [((Window,Rectangle),Maybe Window)] -> Event -> X ()
handleResize :: [((Word64, Rectangle), Maybe Word64)] -> Event -> X ()
handleResize [((Word64, Rectangle), Maybe Word64)]
st ButtonEvent { ev_window :: Event -> Word64
ev_window = Word64
ew, ev_event_type :: Event -> Word32
ev_event_type = Word32
et }
    | Word32
et Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
buttonPress
    , Just (Word64
w,Rectangle Int32
wx Int32
wy Word32
_ Word32
_) <- Word64
-> [((Word64, Rectangle), Maybe Word64)]
-> Maybe (Word64, Rectangle)
forall {t} {a} {b}.
Eq t =>
t -> [((a, b), Maybe t)] -> Maybe (a, b)
getWin Word64
ew [((Word64, Rectangle), Maybe Word64)]
st = do
                                        Word64 -> X ()
focus Word64
w
                                        (Int32 -> Int32 -> X ()) -> X () -> X ()
mouseDrag (\Int32
x Int32
y -> do
                                                     let rect :: Rectangle
rect = Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
wx Int32
wy
                                                                (Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max Word32
1 (Word32 -> Word32) -> (Int32 -> Word32) -> Int32 -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Int32 -> Word32) -> Int32 -> Word32
forall a b. (a -> b) -> a -> b
$ Int32
x Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
wx)
                                                                (Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max Word32
1 (Word32 -> Word32) -> (Int32 -> Word32) -> Int32 -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Int32 -> Word32) -> Int32 -> Word32
forall a b. (a -> b) -> a -> b
$ Int32
y Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
wy)
                                                     WindowArrangerMsg -> X ()
forall a. Message a => a -> X ()
sendMessage (Rectangle -> WindowArrangerMsg
SetGeometry Rectangle
rect)) (() -> X ()
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ())

      where
        getWin :: t -> [((a, b), Maybe t)] -> Maybe (a, b)
getWin t
w (((a
win,b
r),Maybe t
tw):[((a, b), Maybe t)]
xs)
            | Just t
w' <- Maybe t
tw
            , t
w t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
w'   = (a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just (a
win,b
r)
            | Bool
otherwise = t -> [((a, b), Maybe t)] -> Maybe (a, b)
getWin t
w [((a, b), Maybe t)]
xs
        getWin t
_ []     = Maybe (a, b)
forall a. Maybe a
Nothing
handleResize [((Word64, Rectangle), Maybe Word64)]
_ Event
_ = () -> X ()
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

createInputWindow :: ((Window,Rectangle), Maybe Rectangle) -> X ((Window,Rectangle),Maybe Window)
createInputWindow :: ((Word64, Rectangle), Maybe Rectangle)
-> X ((Word64, Rectangle), Maybe Word64)
createInputWindow ((Word64
w,Rectangle
r),Maybe Rectangle
mr) =
  case Maybe Rectangle
mr of
    Just Rectangle
tr  -> (Display -> X ((Word64, Rectangle), Maybe Word64))
-> X ((Word64, Rectangle), Maybe Word64)
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ((Word64, Rectangle), Maybe Word64))
 -> X ((Word64, Rectangle), Maybe Word64))
-> (Display -> X ((Word64, Rectangle), Maybe Word64))
-> X ((Word64, Rectangle), Maybe Word64)
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
                  tw <- Display -> Rectangle -> X Word64
mkInputWindow Display
d Rectangle
tr
                  io $ selectInput d tw (exposureMask .|. buttonPressMask)

                  cursor <- io $ createFontCursor d xC_bottom_right_corner
                  io $ defineCursor d tw cursor
                  io $ freeCursor d cursor

                  showWindow tw
                  return ((w,r), Just tw)
    Maybe Rectangle
Nothing ->    ((Word64, Rectangle), Maybe Word64)
-> X ((Word64, Rectangle), Maybe Word64)
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Word64
w,Rectangle
r), Maybe Word64
forall a. Maybe a
Nothing)

deleteInputWin :: Maybe Window -> X ()
deleteInputWin :: Maybe Word64 -> X ()
deleteInputWin = X () -> (Word64 -> X ()) -> Maybe Word64 -> X ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> X ()
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ()) Word64 -> X ()
deleteWindow

mkInputWindow :: Display -> Rectangle -> X Window
mkInputWindow :: Display -> Rectangle -> X Word64
mkInputWindow Display
d (Rectangle Int32
x Int32
y Word32
w Word32
h) = do
  rw <- (XConf -> Word64) -> X Word64
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Word64
theRoot
  let screen   = Display -> Screen
defaultScreenOfDisplay Display
d
      visual   = Screen -> Visual
defaultVisualOfScreen Screen
screen
      attrmask = Word64
cWOverrideRedirect
  io $ allocaSetWindowAttributes $
         \Ptr SetWindowAttributes
attributes -> do
           Ptr SetWindowAttributes -> Bool -> IO ()
set_override_redirect Ptr SetWindowAttributes
attributes Bool
True
           Display
-> Word64
-> Int32
-> Int32
-> Word32
-> Word32
-> CInt
-> CInt
-> CInt
-> Visual
-> Word64
-> Ptr SetWindowAttributes
-> IO Word64
createWindow Display
d Word64
rw Int32
x Int32
y Word32
w Word32
h CInt
0 CInt
0 CInt
inputOnly Visual
visual Word64
attrmask Ptr SetWindowAttributes
attributes