xmonad-config/lib/XMonad/Internal/DBus/Common.hs

44 lines
1.2 KiB
Haskell
Raw Normal View History

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_
, xmonadBusName
2020-04-01 20:17:47 -04:00
) where
2021-11-23 18:28:38 -05:00
import Control.Exception
import Control.Monad
2020-04-01 20:17:47 -04:00
import DBus
import DBus.Client
xmonadBusName :: BusName
xmonadBusName = busName_ "org.xmonad"
2020-04-01 22:06:00 -04:00
-- | Bind a callback to a signal match rule
2021-11-23 18:28:38 -05:00
addMatchCallback :: MatchRule -> ([Variant] -> IO ()) -> Client -> IO ()
addMatchCallback rule cb client = void $ addMatch client rule $ cb . signalBody
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