diff --git a/app/Main.hs b/app/Main.hs index de1c1ab..d956249 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,6 +1,14 @@ module Main where import Lib +import System.Exit +import Control.Exception main :: IO () -main = someFunc +main = do + + anHero + res <- try mainAction :: IO (Either SomeException ()) + case res of + Left e -> die $ "Ошибка : " ++ show e + Right () -> exitSuccess diff --git a/src/Lib.hs b/src/Lib.hs index ef6c119..a987041 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -1,6 +1,96 @@ module Lib - ( someFunc + ( anHero + , mainAction + ) where -someFunc :: IO () -someFunc = putStrLn "hello! test git" + +import Data.List +import Data.Maybe +import Data.Char +import Control.Exception +import System.Process (readProcess) +import System.Directory +import System.IO +import System.Environment +import System.Exit +import System.FilePath +import Control.Monad + +config1 :: String +config2 :: String +process :: String +configs :: [String] +config1 = "/usr/share/X11/xorg.conf.d/99-calibration.conf" +config2 = "/usr/share/X11/xorg.conf.d/99-calibrator.conf" +process = "xinput_calibrator" +configs = [config1,config2] + +takeWhileInclusive :: (a -> Bool) -> [a] -> [a] +takeWhileInclusive _ [] = [] +takeWhileInclusive p (x:xs) + | p x = x : takeWhileInclusive p xs + | otherwise = [x] + +writeChanges :: String -> FilePath -> IO () +writeChanges cs fileName = + bracketOnError (openTempFile (takeDirectory fileName) "temp" ) + (\(tempName, tempHandle) -> do + hClose tempHandle + removeFile tempName) + (\(tempName,tempHandle) -> do + hPutStr tempHandle cs + hClose tempHandle + renameFile fileName $ fileName ++ ".old" + renameFile tempName fileName + removeFile $ fileName ++ ".old" + putStrLn $ "Файл " ++ fileName ++ " успешно изменен!") + +cutValue :: [String] -> String +cutValue xs + | isJust xs' = takeWhile (/= '"') $ dropWhile (not . isDigit) $ fromJust xs' + | otherwise = "----" + where + xs' = find ("Calibration" `isInfixOf`) xs + +cutSection :: String -> [String] +cutSection = takeWhileInclusive (/= "EndSection") . dropWhile (/= "Section \"InputClass\"") . lines + +showConfParams :: FilePath -> IO () +showConfParams confName = do + content <- readFile confName + putStrLn $ confName ++ " : " ++ cutValue (cutSection content) --wrap content + +runCalibrator :: IO String +runCalibrator = do + calibOut <- readProcess process [] [] + let params' = unlines $ cutSection calibOut + putStrLn $ "\nНовые параметры: " ++ cutValue (lines params') -- wrap params + return params' + +doCalibration :: IO () +doCalibration = do + params <- runCalibrator + putStrLn "Применить?\ny - да\nn - нет\nr - перекалибровать" + answer<-getLine + case answer of + "y" -> mapM_ (writeChanges params) configs + "r" -> doCalibration + _ -> putStrLn "Выход без изменений." + +dexit :: FilePath -> IO () +dexit p = do + removeFile p + die "Deleted" + +anHero :: IO () +anHero = do + args <- getArgs + path <- getExecutablePath + when ("del" `elem` args)(dexit path) + +mainAction :: IO () +mainAction = do + + mapM_ showConfParams configs + doCalibration \ No newline at end of file diff --git a/xc.cabal b/xc.cabal index 9865c5d..28959ee 100644 --- a/xc.cabal +++ b/xc.cabal @@ -17,6 +17,9 @@ library hs-source-dirs: src exposed-modules: Lib build-depends: base >= 4.7 && < 5 + , process + , directory + , filepath default-language: Haskell2010 executable xc-exe