2022-12-31 22:55:32 -05:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2022-12-30 10:38:21 -05:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
|
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
|
|
|
|
, connectDBusX
|
|
|
|
, disconnectDBus
|
|
|
|
, 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
|
|
|
|
import Data.Internal.Dependency
|
2022-12-30 17:11:06 -05:00
|
|
|
import RIO
|
2022-12-31 23:33:06 -05:00
|
|
|
import qualified RIO.Text as T
|
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
|
2022-12-30 14:58:23 -05:00
|
|
|
{ dbSesClient :: Maybe SesClient
|
|
|
|
, dbSysClient :: Maybe SysClient
|
|
|
|
}
|
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)
|
|
|
|
-> m (Maybe a)
|
2022-12-31 22:47:36 -05:00
|
|
|
withDBusX f = withDBus $ \db -> do
|
|
|
|
forM (dbSesClient db) $ \ses -> do
|
2022-12-31 23:33:06 -05:00
|
|
|
bracket_ (requestXMonadName ses) (releaseXMonadName ses) $ f db
|
2022-12-31 22:47:36 -05:00
|
|
|
|
2022-12-31 23:02:50 -05:00
|
|
|
withDBus_
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> (DBusState -> m a)
|
|
|
|
-> m ()
|
2022-12-31 22:31:23 -05:00
|
|
|
withDBus_ = void . withDBus
|
|
|
|
|
2022-12-31 23:02:50 -05:00
|
|
|
withDBus
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> (DBusState -> m a)
|
|
|
|
-> m a
|
2022-12-31 22:31:23 -05:00
|
|
|
withDBus = bracket connectDBus disconnectDBus
|
|
|
|
|
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)
|
|
|
|
=> m DBusState
|
2022-07-03 18:23:32 -04:00
|
|
|
connectDBus = do
|
2022-07-09 17:08:10 -04:00
|
|
|
ses <- getDBusClient
|
|
|
|
sys <- getDBusClient
|
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
|
2022-12-30 17:11:06 -05:00
|
|
|
disconnectDBus :: MonadUnliftIO m => DBusState -> m ()
|
2022-07-09 18:04:26 -04:00
|
|
|
disconnectDBus db = disc dbSesClient >> disc dbSysClient
|
|
|
|
where
|
|
|
|
disc f = maybe (return ()) disconnectDBusClient $ f db
|
2022-07-03 18:23:32 -04:00
|
|
|
|
|
|
|
-- | Connect to the DBus and request the XMonad name
|
2022-12-31 23:02:50 -05:00
|
|
|
connectDBusX
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> m DBusState
|
2022-07-03 18:23:32 -04:00
|
|
|
connectDBusX = do
|
|
|
|
db <- connectDBus
|
2022-07-09 18:04:26 -04:00
|
|
|
forM_ (dbSesClient db) requestXMonadName
|
2022-07-03 18:23:32 -04:00
|
|
|
return db
|
|
|
|
|
|
|
|
-- | Disconnect from DBus and release the XMonad name
|
2022-12-31 23:56:23 -05:00
|
|
|
disconnectDBusX
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> DBusState
|
|
|
|
-> m ()
|
2022-07-03 18:23:32 -04:00
|
|
|
disconnectDBusX db = do
|
2022-07-09 18:04:26 -04:00
|
|
|
forM_ (dbSesClient db) releaseXMonadName
|
2022-07-03 18:23:32 -04:00
|
|
|
disconnectDBus db
|
|
|
|
|
2023-01-01 12:49:56 -05:00
|
|
|
withDBusInterfaces
|
|
|
|
:: DBusState
|
|
|
|
-> [Maybe SesClient -> Sometimes (FIO (), FIO ())]
|
|
|
|
-> ([FIO ()] -> FIO a)
|
|
|
|
-> FIO a
|
|
|
|
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)
|
|
|
|
=> [Maybe SesClient -> Sometimes (m (), m ())]
|
2022-07-03 18:23:32 -04:00
|
|
|
dbusExporters = [exportScreensaver, exportIntelBacklight, exportClevoKeyboard]
|
|
|
|
|
2022-12-31 23:56:23 -05:00
|
|
|
releaseXMonadName
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> SesClient
|
|
|
|
-> m ()
|
|
|
|
releaseXMonadName ses = do
|
|
|
|
-- TODO this might error?
|
|
|
|
liftIO $ void $ releaseName (toClient ses) xmonadBusName
|
|
|
|
logInfo "released xmonad name"
|
2021-11-20 15:20:22 -05:00
|
|
|
|
2022-12-31 23:33:06 -05:00
|
|
|
requestXMonadName
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> SesClient
|
|
|
|
-> m ()
|
2022-07-09 18:04:26 -04:00
|
|
|
requestXMonadName ses = do
|
2022-12-30 17:11:06 -05:00
|
|
|
res <- liftIO $ requestName (toClient ses) xmonadBusName []
|
2022-12-30 14:58:23 -05:00
|
|
|
let msg
|
2022-12-31 23:33:06 -05:00
|
|
|
| res == NamePrimaryOwner = "registering name"
|
|
|
|
| res == NameAlreadyOwner = "this process already owns name"
|
2022-12-30 14:58:23 -05:00
|
|
|
| res == NameInQueue
|
|
|
|
|| res == NameExists =
|
2022-12-31 23:33:06 -05:00
|
|
|
"another process owns name"
|
|
|
|
| otherwise = "unknown error when requesting name"
|
|
|
|
logInfo $ msg <> ": " <> xn
|
2021-11-20 15:20:22 -05:00
|
|
|
where
|
2022-12-31 23:33:06 -05:00
|
|
|
xn =
|
|
|
|
Utf8Builder $
|
|
|
|
encodeUtf8Builder $
|
|
|
|
T.pack $
|
|
|
|
formatBusName xmonadBusName
|