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

125 lines
3.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
2022-12-30 10:38:21 -05:00
{-# LANGUAGE ScopedTypeVariables #-}
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- High-level interface for managing XMonad's DBus
2020-04-01 20:17:47 -04:00
module XMonad.Internal.DBus.Control
( Client
2022-12-30 14:58:23 -05:00
, DBusState (..)
, withDBus
, withDBus_
, withDBusX
, withDBusX_
2022-07-03 18:23:32 -04:00
, connectDBus
, connectDBusX
, disconnectDBus
, disconnectDBusX
, getDBusClient
2021-11-23 18:28:38 -05:00
, withDBusClient
, withDBusClient_
, disconnect
, 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-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
withDBusX_
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
=> (DBusState -> m a)
-> m ()
withDBusX_ = void . withDBusX_
withDBusX
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
=> (DBusState -> m a)
-> m (Maybe a)
withDBusX f = withDBus $ \db ->
-- TODO log error if this fails
forM (dbSesClient db) $ \ses ->
bracket_ (requestXMonadName ses) (releaseXMonadName ses) (f db)
withDBus_
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
=> (DBusState -> m a)
-> m ()
withDBus_ = void . withDBus
withDBus
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
=> (DBusState -> m a)
-> m a
withDBus = bracket connectDBus disconnectDBus
2022-07-03 18:23:32 -04:00
-- | Connect to the DBus
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
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
disconnectDBusX
:: 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
-- | All exporter features to be assigned to the DBus
2022-07-09 17:08:10 -04:00
dbusExporters :: [Maybe SesClient -> SometimesIO]
2022-07-03 18:23:32 -04:00
dbusExporters = [exportScreensaver, exportIntelBacklight, exportClevoKeyboard]
releaseXMonadName
:: MonadUnliftIO m
=> SesClient
-> m ()
releaseXMonadName ses = do
liftIO $ void $ releaseName (toClient ses) xmonadBusName
2021-11-20 15:20:22 -05:00
2022-12-30 17:11:06 -05:00
requestXMonadName :: 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 []
2021-11-21 00:42:40 -05:00
-- TODO if the client is not released on shutdown the owner will be different
2022-12-30 14:58:23 -05:00
let msg
| res == NamePrimaryOwner = Nothing
| res == NameAlreadyOwner = Just $ "this process already owns " ++ xn
| res == NameInQueue
|| res == NameExists =
Just $ "another process owns " ++ xn
| otherwise = Just $ "unknown error when requesting " ++ xn
2022-12-30 17:11:06 -05:00
liftIO $ forM_ msg putStrLn
2021-11-20 15:20:22 -05:00
where
xn = "'" ++ formatBusName xmonadBusName ++ "'"