module XMonad.Util.Paste (
pasteSelection,
pasteString,
pasteChar,
sendKey,
sendKeyWindow,
noModMask
)
where
import XMonad (io, theRoot, withDisplay, X ())
import Graphics.X11
import Graphics.X11.Xlib.Extras (none, setEventType, setKeyEvent)
import Control.Monad.Reader (asks)
import XMonad.Operations (withFocused)
import XMonad.Prelude (isUpper, listToMaybe)
import XMonad.Util.XSelection (getSelection)
import XMonad.Util.EZConfig (parseKey)
import Text.ParserCombinators.ReadP (readP_to_S)
pasteSelection :: X ()
pasteSelection :: X ()
pasteSelection = X String
forall (m :: * -> *). MonadIO m => m String
getSelection X String -> (String -> X ()) -> X ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> X ()
pasteString
pasteString :: String -> X ()
pasteString :: String -> X ()
pasteString = (Char -> X ()) -> String -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\Char
x -> if Char -> Bool
isUpper Char
x Bool -> Bool -> Bool
|| Char
x Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
"~!@#$%^&*()_+{}|:\"<>?" then KeyMask -> Char -> X ()
pasteChar KeyMask
shiftMask Char
x else KeyMask -> Char -> X ()
pasteChar KeyMask
noModMask Char
x)
pasteChar :: KeyMask -> Char -> X ()
pasteChar :: KeyMask -> Char -> X ()
pasteChar KeyMask
m Char
c = KeyMask -> KeySym -> X ()
sendKey KeyMask
m (KeySym -> X ()) -> KeySym -> X ()
forall a b. (a -> b) -> a -> b
$ KeySym
-> ((KeySym, String) -> KeySym) -> Maybe (KeySym, String) -> KeySym
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Char -> KeySym
unicodeToKeysym Char
c) (KeySym, String) -> KeySym
forall a b. (a, b) -> a
fst
(Maybe (KeySym, String) -> KeySym)
-> Maybe (KeySym, String) -> KeySym
forall a b. (a -> b) -> a -> b
$ [(KeySym, String)] -> Maybe (KeySym, String)
forall a. [a] -> Maybe a
listToMaybe ([(KeySym, String)] -> Maybe (KeySym, String))
-> [(KeySym, String)] -> Maybe (KeySym, String)
forall a b. (a -> b) -> a -> b
$ ReadP KeySym -> ReadS KeySym
forall a. ReadP a -> ReadS a
readP_to_S ReadP KeySym
parseKey [Char
c]
sendKey :: KeyMask -> KeySym -> X ()
sendKey :: KeyMask -> KeySym -> X ()
sendKey = ((KeySym -> X ()) -> X ()
withFocused ((KeySym -> X ()) -> X ())
-> (KeySym -> KeySym -> X ()) -> KeySym -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((KeySym -> KeySym -> X ()) -> KeySym -> X ())
-> (KeyMask -> KeySym -> KeySym -> X ())
-> KeyMask
-> KeySym
-> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyMask -> KeySym -> KeySym -> X ()
sendKeyWindow
sendKeyWindow :: KeyMask -> KeySym -> Window -> X ()
sendKeyWindow :: KeyMask -> KeySym -> KeySym -> X ()
sendKeyWindow KeyMask
mods KeySym
key KeySym
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
KeySym
rootw <- (XConf -> KeySym) -> X KeySym
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> KeySym
theRoot
KeyCode
keycode <- IO KeyCode -> X KeyCode
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO KeyCode -> X KeyCode) -> IO KeyCode -> X KeyCode
forall a b. (a -> b) -> a -> b
$ Display -> KeySym -> IO KeyCode
keysymToKeycode Display
d KeySym
key
IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ (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
ev -> do
XEventPtr -> EventType -> IO ()
setEventType XEventPtr
ev EventType
keyPress
XEventPtr
-> KeySym
-> KeySym
-> KeySym
-> KeyMask
-> KeyCode
-> Bool
-> IO ()
setKeyEvent XEventPtr
ev KeySym
w KeySym
rootw KeySym
none KeyMask
mods KeyCode
keycode Bool
True
Display -> KeySym -> Bool -> KeySym -> XEventPtr -> IO ()
sendEvent Display
d KeySym
w Bool
True KeySym
keyPressMask XEventPtr
ev
XEventPtr -> EventType -> IO ()
setEventType XEventPtr
ev EventType
keyRelease
Display -> KeySym -> Bool -> KeySym -> XEventPtr -> IO ()
sendEvent Display
d KeySym
w Bool
True KeySym
keyReleaseMask XEventPtr
ev
unicodeToKeysym :: Char -> KeySym
unicodeToKeysym :: Char -> KeySym
unicodeToKeysym Char
c
| (Int
ucp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
32) Bool -> Bool -> Bool
&& (Int
ucp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
126) = Int -> KeySym
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ucp
| (Int
ucp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
160) Bool -> Bool -> Bool
&& (Int
ucp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
255) = Int -> KeySym
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ucp
| Int
ucp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
256 = Int -> KeySym
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> KeySym) -> Int -> KeySym
forall a b. (a -> b) -> a -> b
$ Int
ucp Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0x1000000
| Bool
otherwise = KeySym
0
where ucp :: Int
ucp = Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
c