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 = forall (m :: * -> *). MonadIO m => m String
getSelection forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> X ()
pasteString
pasteString :: String -> X ()
pasteString :: String -> X ()
pasteString = 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 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 forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a -> a
fromMaybe (Char -> KeySym
unicodeToKeysym Char
c)
forall a b. (a -> b) -> a -> b
$ 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
.) 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 = forall a. (Display -> X a) -> X a
withDisplay forall a b. (a -> b) -> a -> b
$ \Display
d -> do
KeySym
rootw <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> KeySym
theRoot
KeyCode
keycode <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ Display -> KeySym -> IO KeyCode
keysymToKeycode Display
d KeySym
key
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io forall a b. (a -> b) -> a -> b
$ forall a. (XEventPtr -> IO a) -> IO a
allocaXEvent 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 forall a. Ord a => a -> a -> Bool
>= Int
32) Bool -> Bool -> Bool
&& (Int
ucp forall a. Ord a => a -> a -> Bool
<= Int
126) = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ucp
| (Int
ucp forall a. Ord a => a -> a -> Bool
>= Int
160) Bool -> Bool -> Bool
&& (Int
ucp forall a. Ord a => a -> a -> Bool
<= Int
255) = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ucp
| Int
ucp forall a. Ord a => a -> a -> Bool
>= Int
256 = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Int
ucp forall a. Num a => a -> a -> a
+ Int
0x1000000
| Bool
otherwise = KeySym
0
where ucp :: Int
ucp = forall a. Enum a => a -> Int
fromEnum Char
c