ENH standardize export/unexport pairs

This commit is contained in:
Nathan Dwarshuis 2023-01-01 13:26:09 -05:00
parent e0a186dd18
commit b2416153e6
2 changed files with 36 additions and 18 deletions

View File

@ -1,3 +1,5 @@
{-# LANGUAGE OverloadedStrings #-}
--------------------------------------------------------------------------------
-- Common internal DBus functions
@ -25,6 +27,7 @@ module Data.Internal.DBus
, addInterfaceRemovedListener
, fromSingletonVariant
, bodyToMaybe
, exportPair
)
where
@ -302,3 +305,24 @@ addInterfaceRemovedListener
-> m (Maybe SignalHandler)
addInterfaceRemovedListener bus =
addInterfaceChangedListener bus omInterfacesRemoved
--------------------------------------------------------------------------------
-- Interface export/unexport
exportPair
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m, SafeClient c)
=> ObjectPath
-> (Client -> m Interface)
-> c
-> (m (), m ())
exportPair path toIface cl = (up, down)
where
cl_ = toClient cl
up = do
logInfo $ "adding interface: " <> path_
i <- toIface cl_
liftIO $ export cl_ path i
down = do
logInfo $ "removing interface: " <> path_
liftIO $ unexport cl_ path
path_ = Utf8Builder $ encodeUtf8Builder $ T.pack $ formatObjectPath path

View File

@ -98,25 +98,19 @@ exportScreensaver
=> Maybe SesClient
-> Sometimes (m (), m ())
exportScreensaver ses =
sometimesDBus ses "screensaver toggle" "xset" (toAnd_ bus ssx) $ \cl -> (up cl, down cl)
sometimesDBus ses "screensaver toggle" "xset" (toAnd_ bus ssx) cmd
where
up cl =
let cl' = toClient cl
in liftIO $ withRunInIO $ \run ->
export
cl'
ssPath
cmd = exportPair ssPath $ \cl_ -> do
liftIO $ withRunInIO $ \run ->
return $
defaultInterface
{ interfaceName = interface
, interfaceMethods =
[ autoMethod memToggle $ run $ emitState cl' =<< toggle
[ autoMethod memToggle $ run $ emitState cl_ =<< toggle
, autoMethod memQuery (run query)
]
, interfaceSignals = [sig]
}
down cl = do
logInfo "removing screensaver interface"
liftIO $ unexport (toClient cl) ssPath
sig =
I.Signal
{ I.signalName = memState