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, fromMaybe)
import XMonad.Util.XSelection (getSelection)
import XMonad.Util.EZConfig (parseKey)
import XMonad.Util.Parser (runParser)
pasteSelection :: X ()
pasteSelection :: X ()
pasteSelection = X String
forall (m :: * -> *). MonadIO m => m String
getSelection X String -> (String -> X ()) -> X ()
forall a b. X a -> (a -> X b) -> X b
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 a. Eq a => a -> [a] -> 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 -> Maybe KeySym -> KeySym
forall a. a -> Maybe a -> a
fromMaybe (Char -> KeySym
unicodeToKeysym Char
c)
(Maybe KeySym -> KeySym) -> Maybe KeySym -> KeySym
forall a b. (a -> b) -> a -> b
$ Parser KeySym -> String -> Maybe KeySym
forall a. Parser a -> String -> Maybe a
runParser Parser 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