2022-12-30 10:56:09 -05:00
|
|
|
--------------------------------------------------------------------------------
|
2022-12-30 14:58:23 -05:00
|
|
|
-- High-level interface for managing XMonad's DBus
|
2022-12-30 10:56:09 -05:00
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
module XMonad.Internal.DBus.Control
|
2020-03-28 14:44:50 -04:00
|
|
|
( Client
|
2022-12-30 14:58:23 -05:00
|
|
|
, DBusState (..)
|
2023-01-01 12:43:54 -05:00
|
|
|
, withDBusInterfaces
|
2022-12-31 22:47:36 -05:00
|
|
|
, withDBusX
|
|
|
|
, withDBusX_
|
2022-12-31 22:31:23 -05:00
|
|
|
, withDBus
|
|
|
|
, withDBus_
|
2022-07-03 18:23:32 -04:00
|
|
|
, connectDBus
|
|
|
|
, disconnectDBus
|
2023-10-27 23:12:22 -04:00
|
|
|
-- , disconnectDBusX
|
2021-11-21 22:47:43 -05:00
|
|
|
, getDBusClient
|
2021-11-23 18:28:38 -05:00
|
|
|
, withDBusClient
|
|
|
|
, withDBusClient_
|
2021-11-21 22:47:43 -05:00
|
|
|
, disconnect
|
2022-06-16 18:50:24 -04:00
|
|
|
, dbusExporters
|
2022-12-30 14:58:23 -05:00
|
|
|
)
|
|
|
|
where
|
2020-03-20 00:51:36 -04:00
|
|
|
|
2022-12-30 14:58:23 -05:00
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
|
|
|
import Data.Internal.DBus
|
2023-01-01 18:33:02 -05:00
|
|
|
import Data.Internal.XIO
|
2022-12-30 17:11:06 -05:00
|
|
|
import RIO
|
2022-12-30 14:58:23 -05:00
|
|
|
import XMonad.Internal.DBus.Brightness.ClevoKeyboard
|
|
|
|
import XMonad.Internal.DBus.Brightness.IntelBacklight
|
|
|
|
import XMonad.Internal.DBus.Common
|
|
|
|
import XMonad.Internal.DBus.Screensaver
|
2020-04-01 20:17:47 -04:00
|
|
|
|
2022-07-03 18:23:32 -04:00
|
|
|
-- | Current connections to the DBus (session and system buses)
|
|
|
|
data DBusState = DBusState
|
2023-10-27 23:12:22 -04:00
|
|
|
{ dbSesClient :: Maybe NamedSesConnection
|
|
|
|
, dbSysClient :: Maybe NamedSysConnection
|
2022-12-30 14:58:23 -05:00
|
|
|
}
|
2020-03-20 00:51:36 -04:00
|
|
|
|
2022-12-31 22:55:32 -05:00
|
|
|
withDBusX_
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> (DBusState -> m a)
|
|
|
|
-> m ()
|
2022-12-31 22:47:36 -05:00
|
|
|
withDBusX_ = void . withDBusX
|
|
|
|
|
2022-12-31 22:55:32 -05:00
|
|
|
withDBusX
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> (DBusState -> m a)
|
2023-10-27 23:12:22 -04:00
|
|
|
-> m a
|
|
|
|
withDBusX = withDBus (Just xmonadSesBusName) (Just xmonadSysBusName)
|
2022-12-31 22:47:36 -05:00
|
|
|
|
2022-12-31 23:02:50 -05:00
|
|
|
withDBus_
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
2023-10-27 23:12:22 -04:00
|
|
|
=> Maybe BusName
|
|
|
|
-> Maybe BusName
|
|
|
|
-> (DBusState -> m a)
|
2022-12-31 23:02:50 -05:00
|
|
|
-> m ()
|
2023-10-27 23:12:22 -04:00
|
|
|
withDBus_ sesname sysname = void . withDBus sesname sysname
|
2022-12-31 22:31:23 -05:00
|
|
|
|
2022-12-31 23:02:50 -05:00
|
|
|
withDBus
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
2023-10-27 23:12:22 -04:00
|
|
|
=> Maybe BusName
|
|
|
|
-> Maybe BusName
|
|
|
|
-> (DBusState -> m a)
|
2022-12-31 23:02:50 -05:00
|
|
|
-> m a
|
2023-10-27 23:12:22 -04:00
|
|
|
withDBus sesname sysname = bracket (connectDBus sesname sysname) disconnectDBus
|
2022-12-31 22:31:23 -05:00
|
|
|
|
2022-07-03 18:23:32 -04:00
|
|
|
-- | Connect to the DBus
|
2022-12-31 23:02:50 -05:00
|
|
|
connectDBus
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
2023-10-27 23:12:22 -04:00
|
|
|
=> Maybe BusName
|
|
|
|
-> Maybe BusName
|
|
|
|
-> m DBusState
|
|
|
|
connectDBus sesname sysname = do
|
|
|
|
ses <- getDBusClient sesname
|
|
|
|
sys <- getDBusClient sysname
|
2022-12-30 14:58:23 -05:00
|
|
|
return DBusState {dbSesClient = ses, dbSysClient = sys}
|
2022-07-03 18:23:32 -04:00
|
|
|
|
|
|
|
-- | Disconnect from the DBus
|
2023-10-27 23:12:22 -04:00
|
|
|
disconnectDBus
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> DBusState
|
|
|
|
-> m ()
|
2022-07-09 18:04:26 -04:00
|
|
|
disconnectDBus db = disc dbSesClient >> disc dbSysClient
|
|
|
|
where
|
2023-10-27 23:12:22 -04:00
|
|
|
disc
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m, SafeClient c)
|
|
|
|
=> (DBusState -> Maybe (NamedConnection c))
|
|
|
|
-> m ()
|
2022-07-09 18:04:26 -04:00
|
|
|
disc f = maybe (return ()) disconnectDBusClient $ f db
|
2022-07-03 18:23:32 -04:00
|
|
|
|
2023-10-25 20:40:15 -04:00
|
|
|
-- -- | Connect to the DBus and request the XMonad name
|
|
|
|
-- connectDBusX
|
|
|
|
-- :: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
-- => m DBusState
|
|
|
|
-- connectDBusX = do
|
|
|
|
-- db <- connectDBus
|
|
|
|
-- requestXMonadName2 db
|
|
|
|
-- return db
|
2022-07-03 18:23:32 -04:00
|
|
|
|
2023-10-27 23:12:22 -04:00
|
|
|
-- -- | Disconnect from DBus and release the XMonad name
|
|
|
|
-- disconnectDBusX
|
|
|
|
-- :: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
-- => DBusState
|
|
|
|
-- -> m ()
|
|
|
|
-- disconnectDBusX db = do
|
|
|
|
-- forM_ (dbSesClient db) releaseBusName
|
|
|
|
-- forM_ (dbSysClient db) releaseBusName
|
|
|
|
-- disconnectDBus db
|
2022-07-03 18:23:32 -04:00
|
|
|
|
2023-10-25 20:40:15 -04:00
|
|
|
-- requestXMonadName2
|
|
|
|
-- :: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
-- => DBusState
|
|
|
|
-- -> m ()
|
|
|
|
-- requestXMonadName2 db = do
|
|
|
|
-- forM_ (dbSesClient db) requestXMonadName
|
|
|
|
-- forM_ (dbSysClient db) requestXMonadName
|
|
|
|
|
2023-01-01 12:49:56 -05:00
|
|
|
withDBusInterfaces
|
|
|
|
:: DBusState
|
2023-10-27 23:12:22 -04:00
|
|
|
-> [Maybe NamedSesConnection -> Sometimes (XIO (), XIO ())]
|
2023-01-01 15:00:40 -05:00
|
|
|
-> ([XIO ()] -> XIO a)
|
|
|
|
-> XIO a
|
2023-01-01 12:49:56 -05:00
|
|
|
withDBusInterfaces db interfaces = bracket up sequence
|
2023-01-01 12:43:54 -05:00
|
|
|
where
|
|
|
|
up = do
|
2023-01-01 12:49:56 -05:00
|
|
|
pairs <- catMaybes <$> mapM (\f -> evalSometimes $ f $ dbSesClient db) interfaces
|
2023-01-01 12:43:54 -05:00
|
|
|
mapM_ fst pairs
|
|
|
|
return $ snd <$> pairs
|
|
|
|
|
2022-07-03 18:23:32 -04:00
|
|
|
-- | All exporter features to be assigned to the DBus
|
2023-01-01 13:07:10 -05:00
|
|
|
dbusExporters
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
2023-10-27 23:12:22 -04:00
|
|
|
=> [Maybe NamedSesConnection -> Sometimes (m (), m ())]
|
2022-07-03 18:23:32 -04:00
|
|
|
dbusExporters = [exportScreensaver, exportIntelBacklight, exportClevoKeyboard]
|
|
|
|
|
2023-10-25 20:40:15 -04:00
|
|
|
-- releaseXMonadName
|
|
|
|
-- :: (SafeClient c, MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
-- => c
|
|
|
|
-- -> m ()
|
|
|
|
-- releaseXMonadName cl = do
|
|
|
|
-- -- TODO this might error?
|
|
|
|
-- liftIO $ void $ releaseName (toClient cl) xmonadBusName
|
|
|
|
-- logInfo "released xmonad name"
|
|
|
|
|
2023-10-27 23:12:22 -04:00
|
|
|
-- releaseBusName
|
|
|
|
-- :: (SafeClient c, MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
-- => BusName
|
|
|
|
-- -> c
|
|
|
|
-- -> m ()
|
|
|
|
-- releaseBusName n cl = do
|
|
|
|
-- -- TODO this might error?
|
|
|
|
-- liftIO $ void $ releaseName (toClient cl) n
|
|
|
|
-- logInfo $ "released bus name: " <> displayBusName n
|
|
|
|
|
|
|
|
-- requestBusName
|
|
|
|
-- :: (SafeClient c, MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
-- => BusName
|
|
|
|
-- -> c
|
|
|
|
-- -> m ()
|
|
|
|
-- requestBusName n cl = do
|
|
|
|
-- res <- try $ liftIO $ requestName (toClient cl) n []
|
|
|
|
-- case res of
|
|
|
|
-- Left e -> logError $ displayBytesUtf8 $ BC.pack $ clientErrorMessage e
|
|
|
|
-- Right r -> do
|
|
|
|
-- let msg
|
|
|
|
-- | r == NamePrimaryOwner = "registering name"
|
|
|
|
-- | r == NameAlreadyOwner = "this process already owns name"
|
|
|
|
-- | r == NameInQueue
|
|
|
|
-- || r == NameExists =
|
|
|
|
-- "another process owns name"
|
|
|
|
-- -- this should never happen
|
|
|
|
-- | otherwise = "unknown error when requesting name"
|
|
|
|
-- logInfo $ msg <> ": " <> displayBusName n
|
2023-10-25 20:40:15 -04:00
|
|
|
|
|
|
|
-- requestXMonadName
|
|
|
|
-- :: (SafeClient c, MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
-- => c
|
|
|
|
-- -> m ()
|
|
|
|
-- requestXMonadName cl = do
|
|
|
|
-- res <- liftIO $ requestName (toClient cl) xmonadBusName []
|
|
|
|
-- let msg
|
|
|
|
-- | res == NamePrimaryOwner = "registering name"
|
|
|
|
-- | res == NameAlreadyOwner = "this process already owns name"
|
|
|
|
-- | res == NameInQueue
|
|
|
|
-- || res == NameExists =
|
|
|
|
-- "another process owns name"
|
|
|
|
-- | otherwise = "unknown error when requesting name"
|
|
|
|
-- logInfo $ msg <> ": " <> displayBusName xmonadBusName
|