2020-04-01 22:06:00 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Common internal DBus functions
|
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
module XMonad.Internal.DBus.Common
|
2021-11-21 16:58:01 -05:00
|
|
|
( addMatchCallback
|
2021-11-23 18:28:38 -05:00
|
|
|
, getDBusClient
|
|
|
|
, withDBusClient
|
|
|
|
, withDBusClient_
|
2021-11-24 00:21:18 -05:00
|
|
|
, matchProperty
|
2021-11-26 23:35:03 -05:00
|
|
|
, matchProperty'
|
2021-11-20 19:35:24 -05:00
|
|
|
, xmonadBusName
|
2021-11-24 00:21:18 -05:00
|
|
|
, matchPropertyChanged
|
|
|
|
, SignalMatch(..)
|
2021-11-26 23:35:03 -05:00
|
|
|
, SignalCallback
|
|
|
|
, withSignalMatch
|
2021-11-24 00:21:18 -05:00
|
|
|
, callPropertyGet
|
2020-04-01 20:17:47 -04:00
|
|
|
) where
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2021-11-23 18:28:38 -05:00
|
|
|
import Control.Exception
|
|
|
|
|
2021-11-24 00:21:18 -05:00
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
|
|
|
|
2021-11-20 19:35:24 -05:00
|
|
|
xmonadBusName :: BusName
|
|
|
|
xmonadBusName = busName_ "org.xmonad"
|
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
type SignalCallback = [Variant] -> IO ()
|
|
|
|
|
2020-04-01 22:06:00 -04:00
|
|
|
-- | Bind a callback to a signal match rule
|
2021-11-26 23:35:03 -05:00
|
|
|
addMatchCallback :: MatchRule -> SignalCallback -> Client -> IO SignalHandler
|
|
|
|
addMatchCallback rule cb client = addMatch client rule $ cb . signalBody
|
2021-11-23 18:28:38 -05:00
|
|
|
|
|
|
|
getDBusClient :: Bool -> IO (Maybe Client)
|
|
|
|
getDBusClient sys = do
|
|
|
|
res <- try $ if sys then connectSystem else connectSession
|
|
|
|
case res of
|
|
|
|
Left e -> putStrLn (clientErrorMessage e) >> return Nothing
|
|
|
|
Right c -> return $ Just c
|
|
|
|
|
|
|
|
withDBusClient :: Bool -> (Client -> a) -> IO (Maybe a)
|
|
|
|
withDBusClient sys f = do
|
|
|
|
client <- getDBusClient sys
|
|
|
|
let r = f <$> client
|
|
|
|
mapM_ disconnect client
|
|
|
|
return r
|
|
|
|
|
|
|
|
withDBusClient_ :: Bool -> (Client -> IO ()) -> IO ()
|
|
|
|
withDBusClient_ sys f = do
|
|
|
|
client <- getDBusClient sys
|
|
|
|
mapM_ f client
|
|
|
|
mapM_ disconnect client
|
2021-11-24 00:21:18 -05:00
|
|
|
|
|
|
|
propertyInterface :: InterfaceName
|
|
|
|
propertyInterface = interfaceName_ "org.freedesktop.DBus.Properties"
|
|
|
|
|
|
|
|
propertySignal :: MemberName
|
|
|
|
propertySignal = memberName_ "PropertiesChanged"
|
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
matchProperty' :: Maybe ObjectPath -> MatchRule
|
|
|
|
matchProperty' p = matchAny
|
2021-11-24 00:21:18 -05:00
|
|
|
-- NOTE: the sender for signals is usually the unique name (eg :X.Y) not the
|
|
|
|
-- requested name (eg "org.something.understandable"). If sender is included
|
|
|
|
-- here, likely nothing will match. Solution is to somehow get the unique
|
|
|
|
-- name, which I could do, but probably won't
|
2021-11-26 23:35:03 -05:00
|
|
|
{ matchPath = p
|
2021-11-24 00:21:18 -05:00
|
|
|
, matchInterface = Just propertyInterface
|
|
|
|
, matchMember = Just propertySignal
|
|
|
|
}
|
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
matchProperty :: ObjectPath -> MatchRule
|
|
|
|
matchProperty = matchProperty' . Just
|
|
|
|
|
2021-11-24 00:21:18 -05:00
|
|
|
data SignalMatch a = Match a | NoMatch | Failure deriving (Eq, Show)
|
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
withSignalMatch :: (Maybe a -> IO ()) -> SignalMatch a -> IO ()
|
|
|
|
withSignalMatch f (Match x) = f (Just x)
|
|
|
|
withSignalMatch f Failure = f Nothing
|
|
|
|
withSignalMatch _ NoMatch = return ()
|
|
|
|
|
|
|
|
matchPropertyChanged :: IsVariant a => InterfaceName -> String -> [Variant]
|
|
|
|
-> SignalMatch a
|
|
|
|
matchPropertyChanged iface property [i, body, _] =
|
2021-11-24 00:21:18 -05:00
|
|
|
let i' = (fromVariant i :: Maybe String)
|
|
|
|
b = toMap body in
|
|
|
|
case (i', b) of
|
|
|
|
(Just i'', Just b') -> if i'' == formatInterfaceName iface then
|
2021-11-26 23:35:03 -05:00
|
|
|
maybe NoMatch Match $ fromVariant =<< M.lookup property b'
|
2021-11-24 00:21:18 -05:00
|
|
|
else NoMatch
|
|
|
|
_ -> Failure
|
|
|
|
where
|
|
|
|
toMap v = fromVariant v :: Maybe (M.Map String Variant)
|
2021-11-26 23:35:03 -05:00
|
|
|
matchPropertyChanged _ _ _ = Failure
|
2021-11-24 00:21:18 -05:00
|
|
|
|
|
|
|
callPropertyGet :: BusName -> ObjectPath -> InterfaceName -> String -> Client
|
|
|
|
-> IO [Variant]
|
|
|
|
callPropertyGet bus path iface property client = either (const []) (:[])
|
|
|
|
<$> getProperty client (methodCall path iface $ memberName_ property)
|
|
|
|
{ methodCallDestination = Just bus }
|
2021-11-26 23:35:03 -05:00
|
|
|
|