diff options
| author | Akshay Nair <phenax5@gmail.com> | 2024-12-19 21:43:03 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2024-12-19 21:43:03 +0530 |
| commit | d44103add77718ae650bc0ad5e708e984192c29d (patch) | |
| tree | c8b7e61e127c20032ca13ff52c2309e301aaa3f3 /src/Chelleport/Control.hs | |
| parent | ac5b4be69b2ba9d2ef17d31de11d1932f9566576 (diff) | |
| download | chelleport-d44103add77718ae650bc0ad5e708e984192c29d.tar.gz chelleport-d44103add77718ae650bc0ad5e708e984192c29d.zip | |
Big refactoring
Diffstat (limited to 'src/Chelleport/Control.hs')
| -rw-r--r-- | src/Chelleport/Control.hs | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/src/Chelleport/Control.hs b/src/Chelleport/Control.hs index 0fd01cc..46c7903 100644 --- a/src/Chelleport/Control.hs +++ b/src/Chelleport/Control.hs @@ -1,27 +1,39 @@ module Chelleport.Control where import Chelleport.Types +import Chelleport.Utils (cIntToInt) import Control.Concurrent (threadDelay) +import Control.Monad.IO.Class (MonadIO (liftIO)) +import Control.Monad.Reader (MonadReader (ask)) import Foreign.C (CInt) import qualified Graphics.X11 as X11 import qualified Graphics.X11.XTest as X11 import qualified SDL -triggerMouseLeftClick :: DrawContext -> IO () -triggerMouseLeftClick (DrawContext {ctxX11Display = display}) = do - threadDelay 30_000 -- Wrap with delay to prevent async window close issues. TODO: Remove maybe? - X11.fakeButtonPress display X11.button1 - X11.sync display False - threadDelay 30_000 +class (Monad m) => MonadControl m where + pressMouseButton :: MouseButtonType -> m () + moveMousePointer :: CInt -> CInt -> m () + getMousePointerPosition :: m (CInt, CInt) -moveMouse :: DrawContext -> CInt -> CInt -> IO () -moveMouse _ x y = do - SDL.warpMouse SDL.WarpGlobal (SDL.P $ SDL.V2 x y) +instance (MonadIO m) => MonadControl (AppM m) where + pressMouseButton btn = do + (DrawContext {ctxX11Display = display}) <- ask + liftIO $ do + -- Wrap with delay to prevent async window close issues. TODO: Remove maybe? + threadDelay 30_000 + X11.fakeButtonPress display x11Button + X11.sync display False + threadDelay 30_000 + where + x11Button = case btn of + LeftClick -> X11.button1 -currentMousePosition :: DrawContext -> IO (SDL.V2 CInt) -currentMousePosition _ctx = do - (SDL.P p) <- SDL.getAbsoluteMouseLocation - pure p + moveMousePointer x y = do + SDL.warpMouse SDL.WarpGlobal (SDL.P $ SDL.V2 x y) + + getMousePointerPosition = do + (SDL.P (SDL.V2 x y)) <- SDL.getAbsoluteMouseLocation + pure (x, y) isKeyPress :: SDL.KeyboardEventData -> Bool isKeyPress = (== SDL.Pressed) . SDL.keyboardEventKeyMotion @@ -39,3 +51,11 @@ isKeyPressWith keyboardEvent keyCode = isKeyReleaseWith :: SDL.KeyboardEventData -> SDL.Keycode -> Bool isKeyReleaseWith keyboardEvent keyCode = isKeyRelease keyboardEvent && eventToKeycode keyboardEvent == keyCode + +directionalIncrement :: (CInt, CInt) -> Char -> (Int, Int) +directionalIncrement (incX, incY) = \case + 'H' -> (-cIntToInt incX, 0) + 'L' -> (cIntToInt incX, 0) + 'K' -> (0, -cIntToInt incY) + 'J' -> (0, cIntToInt incY) + _ -> undefined |
