From d44103add77718ae650bc0ad5e708e984192c29d Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 19 Dec 2024 21:43:03 +0530 Subject: Big refactoring --- src/Chelleport/Control.hs | 50 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 15 deletions(-) (limited to 'src/Chelleport/Control.hs') 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 - -moveMouse :: DrawContext -> CInt -> CInt -> IO () -moveMouse _ x y = do - SDL.warpMouse SDL.WarpGlobal (SDL.P $ SDL.V2 x y) - -currentMousePosition :: DrawContext -> IO (SDL.V2 CInt) -currentMousePosition _ctx = do - (SDL.P p) <- SDL.getAbsoluteMouseLocation - pure p +class (Monad m) => MonadControl m where + pressMouseButton :: MouseButtonType -> m () + moveMousePointer :: CInt -> CInt -> m () + getMousePointerPosition :: m (CInt, CInt) + +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 + + 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 -- cgit v1.3.1