2021-11-07 13:35:08 -05:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Functions for handling dependencies
|
|
|
|
|
|
|
|
module XMonad.Internal.Dependency
|
2021-11-19 00:35:54 -05:00
|
|
|
( MaybeExe
|
2021-11-07 13:35:08 -05:00
|
|
|
, UnitType(..)
|
|
|
|
, Dependency(..)
|
2021-11-07 18:41:25 -05:00
|
|
|
, DependencyData(..)
|
2021-11-07 20:16:53 -05:00
|
|
|
, DBusMember(..)
|
2021-11-07 13:35:08 -05:00
|
|
|
, MaybeX
|
2021-11-11 00:11:15 -05:00
|
|
|
, Feature(..)
|
|
|
|
, evalFeature
|
2021-11-07 13:35:08 -05:00
|
|
|
, exe
|
|
|
|
, systemUnit
|
|
|
|
, userUnit
|
2021-11-07 18:41:25 -05:00
|
|
|
, pathR
|
|
|
|
, pathW
|
|
|
|
, pathRW
|
2021-11-19 00:35:54 -05:00
|
|
|
-- , checkInstalled
|
2021-11-07 13:35:08 -05:00
|
|
|
, runIfInstalled
|
|
|
|
, depInstalled
|
|
|
|
, warnMissing
|
|
|
|
, whenInstalled
|
|
|
|
, ifInstalled
|
|
|
|
, spawnIfInstalled
|
|
|
|
, spawnCmdIfInstalled
|
|
|
|
, noCheck
|
|
|
|
, fmtCmd
|
|
|
|
, spawnCmd
|
|
|
|
, doubleQuote
|
|
|
|
, singleQuote
|
|
|
|
, (#!&&)
|
|
|
|
, (#!||)
|
|
|
|
, (#!|)
|
|
|
|
, (#!>>)
|
|
|
|
, playSound
|
|
|
|
, spawnSound
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad.IO.Class
|
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
import Data.List (find)
|
|
|
|
import Data.Maybe (listToMaybe, maybeToList)
|
2021-11-07 20:16:53 -05:00
|
|
|
|
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
2021-11-08 00:27:39 -05:00
|
|
|
import qualified DBus.Introspection as I
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-07 18:41:25 -05:00
|
|
|
import System.Directory (findExecutable, readable, writable)
|
2021-11-07 13:35:08 -05:00
|
|
|
import System.Exit
|
|
|
|
import System.FilePath
|
|
|
|
|
|
|
|
import XMonad.Core (X, getXMonadDir)
|
2021-11-07 18:41:25 -05:00
|
|
|
import XMonad.Internal.IO
|
2021-11-07 13:35:08 -05:00
|
|
|
import XMonad.Internal.Process
|
|
|
|
import XMonad.Internal.Shell
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Gracefully handling missing binaries
|
|
|
|
|
|
|
|
data UnitType = SystemUnit | UserUnit deriving (Eq, Show)
|
|
|
|
|
2021-11-07 20:16:53 -05:00
|
|
|
data DBusMember = Method_ MemberName
|
|
|
|
| Signal_ MemberName
|
|
|
|
| Property_ String
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2021-11-07 18:41:25 -05:00
|
|
|
data DependencyData = Executable String
|
|
|
|
| AccessiblePath FilePath Bool Bool
|
2021-11-19 00:35:54 -05:00
|
|
|
| IOTest (IO (Maybe String))
|
2021-11-07 20:16:53 -05:00
|
|
|
| DBusEndpoint
|
2021-11-08 00:27:39 -05:00
|
|
|
{ ddDbusBus :: BusName
|
|
|
|
, ddDbusSystem :: Bool
|
|
|
|
, ddDbusObject :: ObjectPath
|
2021-11-07 20:16:53 -05:00
|
|
|
, ddDbusInterface :: InterfaceName
|
2021-11-08 00:27:39 -05:00
|
|
|
, ddDbusMember :: DBusMember
|
2021-11-07 20:16:53 -05:00
|
|
|
}
|
2021-11-07 18:41:25 -05:00
|
|
|
| Systemd UnitType String
|
2021-11-11 00:11:15 -05:00
|
|
|
|
|
|
|
data Dependency a = SubFeature (Feature a a)
|
2021-11-19 00:35:54 -05:00
|
|
|
| Dependency DependencyData
|
2021-11-11 00:11:15 -05:00
|
|
|
|
|
|
|
data Feature a b = Feature
|
|
|
|
{ ftrAction :: a
|
2021-11-11 23:52:01 -05:00
|
|
|
, ftrDefault :: Maybe a
|
2021-11-11 00:11:15 -05:00
|
|
|
, ftrSilent :: Bool
|
|
|
|
, ftrChildren :: [Dependency b]
|
2021-11-11 22:38:25 -05:00
|
|
|
} | ConstFeature a
|
2021-11-11 00:11:15 -05:00
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
-- data Chain a = Chain
|
|
|
|
-- { chainAction :: a
|
|
|
|
-- , chainChildren :: [Feature a a]
|
|
|
|
-- , chainCompose :: a -> a -> a
|
|
|
|
-- }
|
|
|
|
|
2021-11-11 00:11:15 -05:00
|
|
|
evalFeature :: Feature a b -> IO (MaybeExe a)
|
2021-11-19 00:35:54 -05:00
|
|
|
evalFeature (ConstFeature x) = return $ Right x
|
2021-11-11 00:11:15 -05:00
|
|
|
evalFeature Feature { ftrAction = a, ftrSilent = s, ftrChildren = c } = do
|
2021-11-19 00:35:54 -05:00
|
|
|
es <- mapM go c
|
|
|
|
return $ case concat es of
|
|
|
|
[] -> Right a
|
|
|
|
es' -> Left (if s then [] else es')
|
|
|
|
-- return $ case foldl groupResult ([], []) c' of
|
|
|
|
-- ([], opt) -> Installed a opt
|
|
|
|
-- (req, opt) -> if s then Ignore else Missing req opt
|
2021-11-11 00:11:15 -05:00
|
|
|
where
|
|
|
|
go (SubFeature Feature { ftrChildren = cs }) = concat <$> mapM go cs
|
2021-11-11 22:38:25 -05:00
|
|
|
go (SubFeature (ConstFeature _)) = return []
|
2021-11-19 00:35:54 -05:00
|
|
|
go (Dependency d) = do
|
|
|
|
e <- depInstalled d
|
|
|
|
return $ maybeToList e
|
|
|
|
-- groupResult (x, y) (True, z) = (z:x, y)
|
|
|
|
-- groupResult (x, y) (False, z) = (x, z:y)
|
|
|
|
|
|
|
|
-- evalChain :: Chain a -> IO (MaybeExe a)
|
|
|
|
-- evalChain Chain { chainAction = a, chainChildren = cs , chainCompose = f } =
|
|
|
|
-- flip Installed [] <$> foldM go a cs
|
|
|
|
-- where
|
|
|
|
-- go acc child = do
|
|
|
|
-- c <- evalFeature child
|
|
|
|
-- -- TODO need a way to get error messages out of this for anything
|
|
|
|
-- -- that's missing
|
|
|
|
-- return $ case c of
|
|
|
|
-- (Installed x _) -> f x acc
|
|
|
|
-- _ -> acc
|
2021-11-11 00:11:15 -05:00
|
|
|
|
|
|
|
exe :: String -> Dependency a
|
2021-11-19 00:35:54 -05:00
|
|
|
exe = Dependency . Executable
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-11 00:11:15 -05:00
|
|
|
unit :: UnitType -> String -> Dependency a
|
2021-11-19 00:35:54 -05:00
|
|
|
unit t = Dependency . Systemd t
|
2021-11-07 18:41:25 -05:00
|
|
|
|
2021-11-11 00:11:15 -05:00
|
|
|
path :: Bool -> Bool -> String -> Dependency a
|
2021-11-19 00:35:54 -05:00
|
|
|
path r w n = Dependency $ AccessiblePath n r w
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-11 00:11:15 -05:00
|
|
|
pathR :: String -> Dependency a
|
2021-11-07 18:41:25 -05:00
|
|
|
pathR = path True False
|
|
|
|
|
2021-11-11 00:11:15 -05:00
|
|
|
pathW :: String -> Dependency a
|
2021-11-07 18:41:25 -05:00
|
|
|
pathW = path False True
|
|
|
|
|
2021-11-11 00:11:15 -05:00
|
|
|
pathRW :: String -> Dependency a
|
2021-11-07 18:41:25 -05:00
|
|
|
pathRW = path True True
|
|
|
|
|
2021-11-11 00:11:15 -05:00
|
|
|
systemUnit :: String -> Dependency a
|
2021-11-07 13:35:08 -05:00
|
|
|
systemUnit = unit SystemUnit
|
|
|
|
|
2021-11-11 00:11:15 -05:00
|
|
|
userUnit :: String -> Dependency a
|
2021-11-07 13:35:08 -05:00
|
|
|
userUnit = unit UserUnit
|
|
|
|
|
2021-11-07 18:41:25 -05:00
|
|
|
-- TODO this is poorly named. This actually represents an action that has
|
|
|
|
-- one or more dependencies (where "action" is not necessarily executing an exe)
|
2021-11-19 00:35:54 -05:00
|
|
|
-- data MaybeExe a = Installed a [DependencyData]
|
|
|
|
-- | Missing [DependencyData] [DependencyData]
|
|
|
|
-- | Ignore
|
|
|
|
-- deriving (Foldable, Traversable)
|
|
|
|
-- data MaybeExe a = MaybeExe (Maybe a) [String]
|
|
|
|
type MaybeExe a = Either [String] a
|
|
|
|
-- deriving (Foldable, Traversable)
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
-- instance Functor MaybeExe where
|
|
|
|
-- fmap f (MaybeExe x m) = MaybeExe (f <$> x) m
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
type MaybeX = MaybeExe (X ())
|
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
exeInstalled :: String -> IO (Maybe String)
|
|
|
|
exeInstalled x = do
|
|
|
|
r <- findExecutable x
|
|
|
|
return $ case r of
|
|
|
|
(Just _) -> Nothing
|
|
|
|
_ -> Just $ "executable '" ++ x ++ "' not found"
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
unitInstalled :: UnitType -> String -> IO (Maybe String)
|
2021-11-07 18:41:25 -05:00
|
|
|
unitInstalled u x = do
|
2021-11-07 13:35:08 -05:00
|
|
|
(rc, _, _) <- readCreateProcessWithExitCode' (shell cmd) ""
|
|
|
|
return $ case rc of
|
2021-11-19 00:35:54 -05:00
|
|
|
ExitSuccess -> Nothing
|
|
|
|
_ -> Just $ "systemd " ++ unitType u ++ " unit '" ++ x ++ "' not found"
|
2021-11-07 13:35:08 -05:00
|
|
|
where
|
|
|
|
cmd = fmtCmd "systemctl" $ ["--user" | u == UserUnit] ++ ["status", x]
|
2021-11-19 00:35:54 -05:00
|
|
|
unitType SystemUnit = "system"
|
|
|
|
unitType UserUnit = "user"
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
-- pathAccessible :: FilePath -> Bool -> Bool -> IO (Maybe String)
|
2021-11-19 00:35:54 -05:00
|
|
|
pathAccessible :: FilePath -> Bool -> Bool -> IO (Maybe String)
|
2021-11-07 18:41:25 -05:00
|
|
|
pathAccessible p testread testwrite = do
|
|
|
|
res <- getPermissionsSafe p
|
|
|
|
let msg = permMsg res
|
|
|
|
return msg
|
|
|
|
-- return $ fmap (\m -> m ++ ": " ++ p) msg
|
|
|
|
where
|
|
|
|
testPerm False _ _ = Nothing
|
|
|
|
testPerm True f r = Just $ f r
|
2021-11-19 00:35:54 -05:00
|
|
|
permMsg NotFoundError = Just "file not found"
|
|
|
|
permMsg PermError = Just "could not get permissions"
|
|
|
|
-- permMsg NotFoundError = False
|
|
|
|
-- permMsg PermError = False
|
2021-11-07 18:41:25 -05:00
|
|
|
permMsg (PermResult r) =
|
|
|
|
case (testPerm testread readable r, testPerm testwrite writable r) of
|
2021-11-19 00:35:54 -05:00
|
|
|
(Just False, Just False) -> Just "file not readable or writable"
|
|
|
|
(Just False, _) -> Just "file not readable"
|
|
|
|
(_, Just False) -> Just "file not writable"
|
|
|
|
_ -> Nothing
|
|
|
|
-- (Just True, Just True) -> True
|
|
|
|
-- (Just True, Nothing) -> True
|
|
|
|
-- (Nothing, Just True) -> True
|
|
|
|
-- _ -> False
|
2021-11-07 18:41:25 -05:00
|
|
|
-- (Just False, Just False) -> Just "file not readable or writable"
|
|
|
|
-- (Just False, _) -> Just "file not readable"
|
|
|
|
-- (_, Just False) -> Just "file not writable"
|
|
|
|
-- _ -> Nothing
|
|
|
|
|
2021-11-08 00:27:39 -05:00
|
|
|
introspectInterface :: InterfaceName
|
|
|
|
introspectInterface = interfaceName_ "org.freedesktop.DBus.Introspectable"
|
|
|
|
|
|
|
|
introspectMethod :: MemberName
|
|
|
|
introspectMethod = memberName_ "Introspect"
|
|
|
|
|
2021-11-07 20:16:53 -05:00
|
|
|
dbusInstalled :: BusName -> Bool -> ObjectPath -> InterfaceName -> DBusMember
|
2021-11-19 00:35:54 -05:00
|
|
|
-> IO (Maybe String)
|
2021-11-07 20:16:53 -05:00
|
|
|
dbusInstalled bus usesystem objpath iface mem = do
|
|
|
|
client <- if usesystem then connectSystem else connectSession
|
2021-11-08 00:27:39 -05:00
|
|
|
reply <- call_ client (methodCall objpath introspectInterface introspectMethod)
|
|
|
|
{ methodCallDestination = Just bus }
|
2021-11-07 20:16:53 -05:00
|
|
|
let res = findMem =<< I.parseXML objpath =<< fromVariant
|
|
|
|
=<< listToMaybe (methodReturnBody reply)
|
|
|
|
disconnect client
|
2021-11-19 00:35:54 -05:00
|
|
|
return $ case res of
|
|
|
|
Just _ -> Nothing
|
|
|
|
_ -> Just "some random dbus interface not found"
|
|
|
|
-- return $ fromMaybe False res
|
2021-11-07 20:16:53 -05:00
|
|
|
where
|
|
|
|
findMem obj = fmap (matchMem mem)
|
|
|
|
$ find (\i -> I.interfaceName i == iface)
|
|
|
|
$ I.objectInterfaces obj
|
|
|
|
matchMem (Method_ n) = elem n . fmap I.methodName . I.interfaceMethods
|
|
|
|
matchMem (Signal_ n) = elem n . fmap I.signalName . I.interfaceSignals
|
|
|
|
matchMem (Property_ n) = elem n . fmap I.propertyName . I.interfaceProperties
|
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
depInstalled :: DependencyData -> IO (Maybe String)
|
2021-11-07 18:41:25 -05:00
|
|
|
depInstalled (Executable n) = exeInstalled n
|
2021-11-19 00:35:54 -05:00
|
|
|
depInstalled (IOTest t) = t
|
2021-11-07 18:41:25 -05:00
|
|
|
depInstalled (Systemd t n) = unitInstalled t n
|
|
|
|
depInstalled (AccessiblePath p r w) = pathAccessible p r w
|
2021-11-07 20:16:53 -05:00
|
|
|
depInstalled DBusEndpoint { ddDbusBus = b
|
|
|
|
, ddDbusSystem = s
|
|
|
|
, ddDbusObject = o
|
|
|
|
, ddDbusInterface = i
|
|
|
|
, ddDbusMember = m
|
|
|
|
} = dbusInstalled b s o i m
|
2021-11-07 18:41:25 -05:00
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
-- checkInstalled :: [Dependency a] -> IO ([DependencyData], [DependencyData])
|
|
|
|
-- checkInstalled = fmap go . filterMissing
|
|
|
|
-- where
|
|
|
|
-- go = join (***) (fmap depData) . partition depRequired
|
2021-11-07 18:41:25 -05:00
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
-- filterMissing :: [Dependency a] -> IO [Dependency a]
|
|
|
|
-- filterMissing = filterM (fmap not . depInstalled . depData)
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-11 00:11:15 -05:00
|
|
|
runIfInstalled :: [Dependency a] -> b -> IO (MaybeExe b)
|
|
|
|
runIfInstalled ds x = evalFeature $
|
|
|
|
Feature
|
|
|
|
{ ftrAction = x
|
2021-11-11 23:52:01 -05:00
|
|
|
, ftrDefault = Nothing
|
2021-11-11 00:11:15 -05:00
|
|
|
, ftrSilent = False
|
|
|
|
, ftrChildren = ds
|
|
|
|
}
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
spawnIfInstalled :: MonadIO m => String -> IO (MaybeExe (m ()))
|
|
|
|
spawnIfInstalled n = runIfInstalled [exe n] $ spawn n
|
|
|
|
|
|
|
|
spawnCmdIfInstalled :: MonadIO m => String -> [String] -> IO (MaybeExe (m ()))
|
|
|
|
spawnCmdIfInstalled n args = runIfInstalled [exe n] $ spawnCmd n args
|
|
|
|
|
|
|
|
whenInstalled :: Monad m => MaybeExe (m ()) -> m ()
|
|
|
|
whenInstalled = flip ifInstalled skip
|
|
|
|
|
|
|
|
ifInstalled :: MaybeExe a -> a -> a
|
2021-11-19 00:35:54 -05:00
|
|
|
ifInstalled (Right x) _ = x
|
|
|
|
ifInstalled _ alt = alt
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
noCheck :: Monad m => a () -> m (MaybeExe (a ()))
|
2021-11-19 00:35:54 -05:00
|
|
|
noCheck = return . Right
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
-- not sure what to do with these
|
|
|
|
|
|
|
|
soundDir :: FilePath
|
|
|
|
soundDir = "sound"
|
|
|
|
|
|
|
|
spawnSound :: MonadIO m => FilePath -> m () -> m () -> IO (MaybeExe (m ()))
|
|
|
|
spawnSound file pre post = runIfInstalled [exe "paplay"]
|
|
|
|
$ pre >> playSound file >> post
|
|
|
|
|
|
|
|
playSound :: MonadIO m => FilePath -> m ()
|
|
|
|
playSound file = do
|
2021-11-07 18:41:25 -05:00
|
|
|
p <- (</> soundDir </> file) <$> getXMonadDir
|
2021-11-07 13:35:08 -05:00
|
|
|
-- paplay seems to have less latency than aplay
|
2021-11-07 18:41:25 -05:00
|
|
|
spawnCmd "paplay" [p]
|
|
|
|
|
2021-11-19 00:35:54 -05:00
|
|
|
-- partitionMissing :: [MaybeExe a] -> ([DependencyData], [DependencyData])
|
|
|
|
-- partitionMissing = foldl (\(a, b) -> ((a++) *** (b++)) . go) ([], [])
|
|
|
|
-- where
|
|
|
|
-- go (Installed _ opt) = ([], opt)
|
|
|
|
-- go (Missing req opt) = (req, opt)
|
|
|
|
-- go Ignore = ([], [])
|
|
|
|
|
|
|
|
-- fmtMissing :: DependencyData -> String
|
|
|
|
-- -- TODO this error message is lame
|
|
|
|
-- fmtMissing (IOTest _) = "some random test failed"
|
|
|
|
-- fmtMissing DBusEndpoint {} = "some random dbus path is missing"
|
|
|
|
-- fmtMissing (AccessiblePath p True False) = "path '" ++ p ++ "' not readable"
|
|
|
|
-- fmtMissing (AccessiblePath p False True) = "path '" ++ p ++ "' not writable"
|
|
|
|
-- fmtMissing (AccessiblePath p True True) = "path '" ++ p ++ "' not readable/writable"
|
|
|
|
-- fmtMissing (AccessiblePath p _ _) = "path '" ++ p ++ "' not ...something"
|
|
|
|
-- fmtMissing (Executable n) = "executable '" ++ n ++ "' not found"
|
|
|
|
-- fmtMissing (Systemd st n) = "systemd " ++ unitType st ++ " unit '"
|
|
|
|
-- ++ n ++ "' not found"
|
|
|
|
-- where
|
|
|
|
-- unitType SystemUnit = "system"
|
|
|
|
-- unitType UserUnit = "user"
|
|
|
|
|
|
|
|
-- fmtMsgs :: [DependencyData] -> [DependencyData] -> [String]
|
|
|
|
-- fmtMsgs req opt = ("[WARNING] "++)
|
|
|
|
-- <$> (("[REQUIRED DEP] "++) . fmtMissing <$> req)
|
|
|
|
-- ++ (("[OPTIONAL DEP] "++) . fmtMissing <$> opt)
|
|
|
|
|
|
|
|
-- warnMsg ::
|
|
|
|
-- warnMsg xs = mapM_ putStrLn
|
|
|
|
-- $ [ "[WARNING] " ++ m | (MaybeExe _ (Just m)) <- xs ]
|
2021-11-07 18:41:25 -05:00
|
|
|
|
|
|
|
warnMissing :: [MaybeExe a] -> IO ()
|
2021-11-19 00:35:54 -05:00
|
|
|
warnMissing xs = mapM_ putStrLn $ fmap ("[WARNING] "++) $ concat $ [ m | (Left m) <- xs ]
|
2021-11-07 18:41:25 -05:00
|
|
|
|
|
|
|
-- fmtType (AccessiblePath _ _ _) = undefined
|
|
|
|
|
|
|
|
-- splitDeps :: [MaybeExe a] -> ([a], [String])
|
|
|
|
-- splitDeps xs = undefined
|
|
|
|
|
|
|
|
-- splitDeps' :: [m (MaybeExe a)] -> ([m a], [String])
|
|
|
|
-- splitDeps' xs = undefined
|