aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2024-12-27 17:54:31 +0530
committerAkshay Nair <phenax5@gmail.com>2024-12-27 17:54:31 +0530
commit98af698a806e41904368c0114f9d8d9f377d9c09 (patch)
treeb09e835b1cf4c678fe557b1ab01905e75ee3922f
parent3d461b234d0b30cb7085251390b264089f447a97 (diff)
downloadchelleport-98af698a806e41904368c0114f9d8d9f377d9c09.tar.gz
chelleport-98af698a806e41904368c0114f9d8d9f377d9c09.zip
Minor refactor
Diffstat (limited to '')
-rw-r--r--TODO.norg12
-rw-r--r--cpp/libchelleport.cpp4
-rw-r--r--include/libchelleport.h5
-rw-r--r--include/recognizer.h2
-rw-r--r--specs/Specs/AppStateSpec.hs4
-rw-r--r--specs/Specs/KeySequenceSpec.hs4
-rw-r--r--src/Chelleport.hs10
-rw-r--r--src/Chelleport/AppState.hs5
-rw-r--r--src/Chelleport/Control.hs9
-rw-r--r--src/Chelleport/KeySequence.hs6
10 files changed, 32 insertions, 29 deletions
diff --git a/TODO.norg b/TODO.norg
index 18edf65..ad07d1a 100644
--- a/TODO.norg
+++ b/TODO.norg
@@ -1,15 +1,17 @@
* Current
- - ( ) Add hjkl for search mode
- - ( ) Middle click
+ - ( ) CLI args
+ - ( ) TextStyle for drawText
- (-) Optimize speed of ocr
+ - ( ) Sort ocr match result by position on screen (top to bottom, left to right)
+ - ( ) Middle click
* Later
- - ( ) Sort ocr match result by position on screen (top to bottom, left to right)
- ( ) Look into making mouse controls (click/mouse down/mouse up) cross-platform
- - ( ) Look into making controls cross-platform
+ - ( ) Look into making screenshot cross-platform
- ( ) Lens-ey setup for Mode access
* Maybe
- ( ) Scroll
- - ( ) Configuration
+ - ( ) Rethink keybindings
+ - ( ) Custom configuration?
- ( ) Process mode? Run in bg with root key binding to toggle
diff --git a/cpp/libchelleport.cpp b/cpp/libchelleport.cpp
index 67abb06..20c2d4f 100644
--- a/cpp/libchelleport.cpp
+++ b/cpp/libchelleport.cpp
@@ -9,8 +9,7 @@
#include "../include/libchelleport.h"
#include "../include/recognizer.h"
-extern "C" {
-OCRMatch *findWordCoordinates(const char *image_path, int *size) {
+extern "C" OCRMatch *findWordCoordinates(const char *image_path, int *size) {
std::vector<OCRMatch> matches;
MEASURE("OCR", { matches = extractTextMatches(image_path); });
@@ -22,7 +21,6 @@ OCRMatch *findWordCoordinates(const char *image_path, int *size) {
*size = matches.size();
return ptr;
}
-}
std::vector<OCRMatch> extractTextMatches(const char *imagePath) {
std::vector<OCRMatch> results;
diff --git a/include/libchelleport.h b/include/libchelleport.h
index b69466e..c022975 100644
--- a/include/libchelleport.h
+++ b/include/libchelleport.h
@@ -16,9 +16,8 @@
std::chrono::duration_cast<std::chrono::microseconds>(end - start); \
std::cout << label << ": " << duration.count() / 1000.0 << " ms" << std::endl;
-extern "C" {
-OCRMatch *findWordCoordinates(const char *image_path, /* returns */ int *size);
-}
+extern "C" OCRMatch *findWordCoordinates(const char *image_path,
+ /* returns */ int *size);
std::vector<OCRMatch> extractTextMatches(const char *imagePath);
diff --git a/include/recognizer.h b/include/recognizer.h
index c6e26da..3b13fdc 100644
--- a/include/recognizer.h
+++ b/include/recognizer.h
@@ -10,7 +10,7 @@
#define MIN_CHARACTER_COUNT 3
const tesseract::PageIteratorLevel ITER_LEVEL = tesseract::RIL_WORD;
-// NOTE: Remember to update size and alignment in ocr hs module on change
+// NOTE: Remember to update size and alignment in hs type on change
struct OCRMatch {
int startX, startY;
int endX, endY;
diff --git a/specs/Specs/AppStateSpec.hs b/specs/Specs/AppStateSpec.hs
index 2508021..de9d552 100644
--- a/specs/Specs/AppStateSpec.hs
+++ b/specs/Specs/AppStateSpec.hs
@@ -64,7 +64,7 @@ test = do
-- let currentState = defaultState
it "todo: implement" $ do
- 1 `shouldBe` 1
+ True `shouldBe` True
context "with action IncrementMouseCursor" $ do
context "when repetition is 1" $ do
@@ -137,7 +137,7 @@ test = do
context "with action HandleKeyInput" $ do
context "when mode is ModeSearch" $ do
it "todo: implement" $ do
- 1 `shouldBe` 1
+ True `shouldBe` True
context "when mode is ModeHints" $ do
context "when there are no matches" $ do
diff --git a/specs/Specs/KeySequenceSpec.hs b/specs/Specs/KeySequenceSpec.hs
index 7a92105..3811fbe 100644
--- a/specs/Specs/KeySequenceSpec.hs
+++ b/specs/Specs/KeySequenceSpec.hs
@@ -26,12 +26,12 @@ test = do
describe "#generateGrid" $ do
it "generates grid of key sequences" $ do
generateGrid 0 (4, 4) "ABCDEF"
- `shouldBe` Just [["AE", "BD", "CC", "BB"], ["AC", "FD", "EE", "EC"], ["FB", "EA", "DF", "DD"], ["CA", "DB", "CE", "BF"]]
+ `shouldBe` Right [["AE", "BD", "CC", "BB"], ["AC", "FD", "EE", "EC"], ["FB", "EA", "DF", "DD"], ["CA", "DB", "CE", "BF"]]
context "when the the keys set is too short" $ do
it "cycles back to first character" $ do
generateGrid 0 (4, 4) "AB"
- `shouldBe` Nothing
+ `shouldBe` Left "Row/Column counts too high"
describe "#findMatchPosition" $ do
it "returns the position of the matching key sequence" $ do
diff --git a/src/Chelleport.hs b/src/Chelleport.hs
index b5b76e2..0f5569e 100644
--- a/src/Chelleport.hs
+++ b/src/Chelleport.hs
@@ -3,7 +3,7 @@ module Chelleport where
import Chelleport.AppShell (setupAppShell)
import qualified Chelleport.AppState as AppState
import Chelleport.Context (initializeContext)
-import Chelleport.Control (anyAlphabetic, anyDigit, checkKey, ctrl, eventToKeycode, hjklDirection, key, pressed, released, shift)
+import Chelleport.Control (anyAlphabetic, anyDigit, checkKey, ctrl, eventToKeycode, hjkl, hjklDirection, key, pressed, released, shift)
import Chelleport.KeySequence (keycodeToInt, toKeyChar)
import Chelleport.Types
import Chelleport.Utils ((<||>))
@@ -24,9 +24,7 @@ run = do
runAppWithCtx ctx = (`runReaderT` ctx) . runAppM
eventHandler :: State -> SDL.Event -> Maybe AppAction
-eventHandler state event = do
- let hjkl = (`elem` ("HJKL" :: String)) . fromMaybe ' ' . toKeyChar . eventToKeycode
-
+eventHandler state event =
case SDL.eventPayload event of
SDL.QuitEvent -> Just ShutdownApp
SDL.KeyboardEvent ev
@@ -64,10 +62,10 @@ eventHandler state event = do
then Just $ ChainMouseClick RightClick
else Just $ TriggerMouseClick RightClick
-- 0-9: Repetition digit
- | checkKey [anyDigit, pressed] ev ->
+ | checkKey [anyDigit, not . ctrl, pressed] ev ->
Just $ UpdateRepetition (fromMaybe 0 $ keycodeToInt $ eventToKeycode ev)
-- A-Z: hint keys and search text
- | checkKey [anyAlphabetic, pressed] ev ->
+ | checkKey [anyAlphabetic, not . ctrl, pressed] ev ->
Just $ HandleKeyInput $ eventToKeycode ev
-- Shift press/release: Toggle shift mode
| checkKey [pressed, key SDL.KeycodeRShift <||> key SDL.KeycodeLShift] ev ->
diff --git a/src/Chelleport/AppState.hs b/src/Chelleport/AppState.hs
index 72a1afe..8c7bac1 100644
--- a/src/Chelleport/AppState.hs
+++ b/src/Chelleport/AppState.hs
@@ -9,13 +9,12 @@ import Chelleport.Types
import Chelleport.Utils (cIntToInt, clamp, intToCInt, isEmpty, itemAt)
import Control.Monad (replicateM_)
import Data.Char (toLower)
-import Data.List (isInfixOf)
-import Data.Maybe (fromMaybe, isJust)
+import Data.Maybe (isJust)
import qualified Text.Fuzzy as Fuzzy
initialState :: (Monad m) => m (State, Maybe AppAction)
initialState = do
- let cells = fromMaybe (pure undefined) $ generateGrid 0 (rows, columns) hintKeys
+ let cells = either error id $ generateGrid 0 (rows, columns) hintKeys
pure (defaultAppState {stateGrid = cells}, Just $ SetMode defaultHintsMode)
where
rows = 9
diff --git a/src/Chelleport/Control.hs b/src/Chelleport/Control.hs
index a7c475f..b987564 100644
--- a/src/Chelleport/Control.hs
+++ b/src/Chelleport/Control.hs
@@ -1,12 +1,13 @@
module Chelleport.Control where
-import Chelleport.KeySequence (isAlphabetic, isKeycodeDigit)
+import Chelleport.KeySequence (isAlphabetic, isKeycodeDigit, toKeyChar)
import Chelleport.Types
import Chelleport.Utils
import Control.Concurrent (threadDelay)
import Control.Monad (unless)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Reader (MonadReader (ask))
+import Data.Maybe (fromMaybe)
import qualified Debug.Trace as Debug
import Foreign.C.Types
import qualified Graphics.X11 as X11
@@ -85,6 +86,9 @@ key keycode = (keycode ==) . eventToKeycode
ctrl :: SDL.KeyboardEventData -> Bool
ctrl ev = SDL.keyModifierLeftCtrl (keyModifier ev) || SDL.keyModifierRightCtrl (keyModifier ev)
+alt :: SDL.KeyboardEventData -> Bool
+alt ev = SDL.keyModifierLeftAlt (keyModifier ev) || SDL.keyModifierRightAlt (keyModifier ev)
+
shift :: SDL.KeyboardEventData -> Bool
shift ev = SDL.keyModifierLeftShift (keyModifier ev) || SDL.keyModifierRightShift (keyModifier ev)
@@ -94,6 +98,9 @@ anyDigit = isKeycodeDigit . eventToKeycode
anyAlphabetic :: SDL.KeyboardEventData -> Bool
anyAlphabetic = isAlphabetic . eventToKeycode
+hjkl :: SDL.KeyboardEventData -> Bool
+hjkl = (`elem` ("HJKL" :: String)) . fromMaybe ' ' . toKeyChar . eventToKeycode
+
hjklDirection :: Char -> Direction
hjklDirection = \case
'H' -> DirLeft
diff --git a/src/Chelleport/KeySequence.hs b/src/Chelleport/KeySequence.hs
index 70d56e2..141e71e 100644
--- a/src/Chelleport/KeySequence.hs
+++ b/src/Chelleport/KeySequence.hs
@@ -37,10 +37,10 @@ lcg seed = (multiplier * seed + increment) `mod` modulus
getIndexRounded :: Int -> [a] -> a
getIndexRounded i ls = ls !! (i `mod` length ls)
-generateGrid :: Int -> (Int, Int) -> KeySequence -> Maybe KeyGrid
+generateGrid :: Int -> (Int, Int) -> KeySequence -> Either String KeyGrid
generateGrid seed (rows, columns) hintKeys
- | rows * columns > length hintKeys * length hintKeys = Nothing
- | otherwise = Just $ (\row -> getKeySeq row <$> [0 .. columns - 1]) <$> [0 .. rows - 1]
+ | rows * columns >= length hintKeys * length hintKeys = Left "Row/Column counts too high"
+ | otherwise = Right $ (\row -> getKeySeq row <$> [0 .. columns - 1]) <$> [0 .. rows - 1]
where
allKeySeq = take numPairs . uniq $ generatePairs
numPairs = rows * columns