rofi-extras/app/rofi-autorandr.hs

76 lines
2.1 KiB
Haskell
Raw Normal View History

2020-08-17 18:40:43 -04:00
--------------------------------------------------------------------------------
2023-02-13 22:19:49 -05:00
-- rofi-autorandr - a rofi prompt to select autorandr profiles
2020-08-17 18:40:43 -04:00
--
-- Simple wrapper to select an autorandr profile.
module Main (main) where
2023-02-13 22:19:49 -05:00
import Control.Monad
import Data.Maybe
import qualified Data.Text.IO as TI
import RIO
import qualified RIO.Text as T
import Rofi.Command
import System.Directory
import System.Environment
import System.FilePath.Posix
import System.Process
2020-08-17 18:40:43 -04:00
main :: IO ()
main = runChecks >> getArgs >>= runPrompt
-- TOOD not DRY
runChecks :: IO ()
runChecks = checkExe "autorandr" >> checkExe "rofi"
checkExe :: String -> IO ()
checkExe cmd = do
res <- findExecutable cmd
unless (isJust res) $ do
2023-02-13 22:19:49 -05:00
TI.putStrLn $ T.append "Could not find executable: " $ T.pack cmd
2020-08-17 18:40:43 -04:00
exitWith $ ExitFailure 1
newtype ARClientConf = ARClientConf [String]
instance RofiConf ARClientConf where
defArgs (ARClientConf a) = a
runPrompt :: [String] -> IO ()
runPrompt a = do
let c = ARClientConf a
2021-06-25 00:04:50 -04:00
staticProfs <- getAutoRandrProfiles
2023-02-13 22:19:49 -05:00
runRofiIO c $
selectAction $
emptyMenu
{ groups = [mkGroup "Static" staticProfs, mkGroup "Virtual" virtProfs]
, prompt = Just "Select Profile"
}
2021-06-25 00:04:50 -04:00
where
2023-02-13 22:19:49 -05:00
mkGroup header =
titledGroup header
. toRofiActions
. fmap (\s -> (" " ++ s, selectProfile $ T.pack s))
2021-06-25 00:04:50 -04:00
virtProfs :: [String]
virtProfs = ["off", "common", "clone-largest", "horizontal", "vertical"]
2020-08-17 18:40:43 -04:00
-- TODO filter profiles based on which xrandr outputs are actually connected
getAutoRandrProfiles :: IO [String]
getAutoRandrProfiles = do
dir <- getAutoRandrDir
contents <- listDirectory dir
filterM (doesDirectoryExist . (dir </>)) contents
getAutoRandrDir :: IO String
getAutoRandrDir = do
2021-06-25 00:04:50 -04:00
c <- getXdgDirectory XdgConfig "autorandr"
e <- doesDirectoryExist c
if e then return c else appendToHome ".autorandr"
2020-08-17 18:40:43 -04:00
where
appendToHome p = (</> p) <$> getHomeDirectory
2023-02-13 22:19:49 -05:00
selectProfile :: T.Text -> RofiIO ARClientConf ()
2020-08-17 18:40:43 -04:00
selectProfile name = do
2023-02-13 22:19:49 -05:00
io $ TI.putStrLn name
io $ void $ spawnProcess "autorandr" ["--change", T.unpack name]