2022-12-26 14:45:49 -05:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2020-03-20 20:10:15 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2022-12-30 14:58:23 -05:00
|
|
|
-- DBus module for X11 screensave/DPMS control
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
module XMonad.Internal.DBus.Screensaver
|
2020-03-20 23:47:02 -04:00
|
|
|
( exportScreensaver
|
|
|
|
, callToggle
|
|
|
|
, callQuery
|
|
|
|
, matchSignal
|
2021-11-20 19:35:24 -05:00
|
|
|
, ssSignalDep
|
2022-12-30 14:58:23 -05:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
|
|
|
import qualified DBus.Introspection as I
|
|
|
|
import Data.Internal.DBus
|
|
|
|
import Data.Internal.Dependency
|
|
|
|
import Graphics.X11.XScreenSaver
|
|
|
|
import RIO
|
|
|
|
import XMonad.Internal.DBus.Common
|
2022-12-31 20:19:09 -05:00
|
|
|
import XMonad.Internal.IO
|
2022-12-30 14:58:23 -05:00
|
|
|
import XMonad.Internal.Shell
|
2020-03-20 20:10:15 -04:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-12-30 14:58:23 -05:00
|
|
|
-- Low-level functions
|
2020-03-20 20:10:15 -04:00
|
|
|
|
|
|
|
type SSState = Bool -- true is enabled
|
|
|
|
|
2022-12-29 00:06:55 -05:00
|
|
|
ssExecutable :: FilePath
|
2021-11-09 00:59:17 -05:00
|
|
|
ssExecutable = "xset"
|
|
|
|
|
2022-12-31 20:19:09 -05:00
|
|
|
toggle :: MonadUnliftIO m => m SSState
|
2020-03-20 20:10:15 -04:00
|
|
|
toggle = do
|
|
|
|
st <- query
|
2022-12-29 00:06:55 -05:00
|
|
|
let args = if st then ["off", "-dpms"] else ["on", "+dpms"]
|
|
|
|
-- this needs to be done with shell commands, because as far as I know there
|
|
|
|
-- are no Haskell bindings for DPMSDisable/Enable (from libxext)
|
2022-12-29 12:01:40 -05:00
|
|
|
rc <- runProcess (proc ssExecutable $ "s" : args)
|
2022-12-29 00:06:55 -05:00
|
|
|
return $ if rc == ExitSuccess then not st else st
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2022-12-31 20:19:09 -05:00
|
|
|
query :: MonadUnliftIO m => m SSState
|
2020-03-20 20:10:15 -04:00
|
|
|
query = do
|
2022-12-31 20:19:09 -05:00
|
|
|
xssi <- withOpenDisplay (liftIO . xScreenSaverQueryInfo)
|
2020-03-20 20:10:15 -04:00
|
|
|
return $ case xssi of
|
2022-12-30 14:58:23 -05:00
|
|
|
Just XScreenSaverInfo {xssi_state = ScreenSaverDisabled} -> False
|
|
|
|
Just XScreenSaverInfo {xssi_state = _} -> True
|
2020-03-20 20:10:15 -04:00
|
|
|
-- TODO handle errors better (at least log them?)
|
2022-12-30 14:58:23 -05:00
|
|
|
Nothing -> False
|
2020-03-20 20:10:15 -04:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-12-30 14:58:23 -05:00
|
|
|
-- DBus Interface
|
2020-03-20 20:10:15 -04:00
|
|
|
--
|
2020-03-20 23:47:02 -04:00
|
|
|
-- Define a methods to toggle the screensaver. This methods will emit signal
|
|
|
|
-- with the new state when called. Define another method to get the current
|
|
|
|
-- state.
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2021-06-21 23:41:57 -04:00
|
|
|
ssPath :: ObjectPath
|
|
|
|
ssPath = objectPath_ "/screensaver"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
interface :: InterfaceName
|
2021-06-21 23:41:57 -04:00
|
|
|
interface = interfaceName_ "org.xmonad.Screensaver"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
memState :: MemberName
|
2021-06-21 23:41:57 -04:00
|
|
|
memState = memberName_ "State"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
memToggle :: MemberName
|
2021-06-21 23:41:57 -04:00
|
|
|
memToggle = memberName_ "Toggle"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
memQuery :: MemberName
|
2021-06-21 23:41:57 -04:00
|
|
|
memQuery = memberName_ "Query"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
sigCurrentState :: Signal
|
2021-06-21 23:41:57 -04:00
|
|
|
sigCurrentState = signal ssPath interface memState
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
ruleCurrentState :: MatchRule
|
2022-12-30 14:58:23 -05:00
|
|
|
ruleCurrentState =
|
|
|
|
matchAny
|
|
|
|
{ matchPath = Just ssPath
|
|
|
|
, matchInterface = Just interface
|
|
|
|
, matchMember = Just memState
|
|
|
|
}
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2022-12-31 20:23:27 -05:00
|
|
|
emitState :: MonadUnliftIO m => Client -> SSState -> m ()
|
|
|
|
emitState client sss =
|
|
|
|
liftIO $ emit client $ sigCurrentState {signalBody = [toVariant sss]}
|
2020-03-20 23:47:02 -04:00
|
|
|
|
|
|
|
bodyGetCurrentState :: [Variant] -> Maybe SSState
|
|
|
|
bodyGetCurrentState [b] = fromVariant b :: Maybe SSState
|
2022-12-30 14:58:23 -05:00
|
|
|
bodyGetCurrentState _ = Nothing
|
2020-03-20 23:47:02 -04:00
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2022-12-30 14:58:23 -05:00
|
|
|
-- Exported haskell API
|
2020-03-20 23:47:02 -04:00
|
|
|
|
2023-01-01 12:43:54 -05:00
|
|
|
exportScreensaver :: MonadUnliftIO m => Maybe SesClient -> Sometimes (m (), m ())
|
2022-07-09 17:08:10 -04:00
|
|
|
exportScreensaver ses =
|
2023-01-01 12:43:54 -05:00
|
|
|
sometimesDBus ses "screensaver toggle" "xset" (toAnd_ bus ssx) $ \cl -> (up cl, down cl)
|
2021-11-11 00:11:15 -05:00
|
|
|
where
|
2023-01-01 12:43:54 -05:00
|
|
|
up cl =
|
2022-12-30 14:58:23 -05:00
|
|
|
let cl' = toClient cl
|
2023-01-01 12:43:54 -05:00
|
|
|
in liftIO $ withRunInIO $ \run ->
|
2022-12-30 14:58:23 -05:00
|
|
|
export
|
|
|
|
cl'
|
|
|
|
ssPath
|
|
|
|
defaultInterface
|
|
|
|
{ interfaceName = interface
|
|
|
|
, interfaceMethods =
|
2022-12-31 20:23:27 -05:00
|
|
|
[ autoMethod memToggle $ run $ emitState cl' =<< toggle
|
2022-12-31 20:19:09 -05:00
|
|
|
, autoMethod memQuery (run query)
|
2022-12-30 14:58:23 -05:00
|
|
|
]
|
|
|
|
, interfaceSignals = [sig]
|
|
|
|
}
|
2023-01-01 12:43:54 -05:00
|
|
|
down cl = liftIO $ unexport (toClient cl) ssPath
|
2022-12-30 14:58:23 -05:00
|
|
|
sig =
|
|
|
|
I.Signal
|
|
|
|
{ I.signalName = memState
|
|
|
|
, I.signalArgs =
|
|
|
|
[ I.SignalArg
|
|
|
|
{ I.signalArgName = "enabled"
|
|
|
|
, I.signalArgType = TypeBoolean
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2022-07-09 01:02:37 -04:00
|
|
|
bus = Bus [] xmonadBusName
|
2022-07-09 14:59:42 -04:00
|
|
|
ssx = DBusIO $ sysExe [Package Official "xorg-xset"] ssExecutable
|
2021-11-11 00:11:15 -05:00
|
|
|
|
2022-12-28 12:19:44 -05:00
|
|
|
callToggle :: Maybe SesClient -> SometimesX
|
2022-12-30 14:58:23 -05:00
|
|
|
callToggle =
|
|
|
|
sometimesEndpoint
|
|
|
|
"screensaver toggle"
|
|
|
|
"dbus switch"
|
|
|
|
[]
|
|
|
|
xmonadBusName
|
|
|
|
ssPath
|
|
|
|
interface
|
|
|
|
memToggle
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2022-12-31 20:23:27 -05:00
|
|
|
callQuery :: MonadUnliftIO m => SesClient -> m (Maybe SSState)
|
2022-07-09 17:44:14 -04:00
|
|
|
callQuery ses = do
|
|
|
|
reply <- callMethod ses xmonadBusName ssPath interface memQuery
|
2021-11-21 16:58:01 -05:00
|
|
|
return $ either (const Nothing) bodyGetCurrentState reply
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2022-12-31 20:23:27 -05:00
|
|
|
matchSignal :: MonadUnliftIO m => (Maybe SSState -> m ()) -> SesClient -> m ()
|
2022-12-30 14:58:23 -05:00
|
|
|
matchSignal cb ses =
|
|
|
|
void $
|
|
|
|
addMatchCallback
|
|
|
|
ruleCurrentState
|
|
|
|
(cb . bodyGetCurrentState)
|
|
|
|
ses
|
2021-11-21 00:53:45 -05:00
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
ssSignalDep :: DBusDependency_ SesClient
|
2022-07-09 01:02:37 -04:00
|
|
|
ssSignalDep = Endpoint [] xmonadBusName ssPath interface $ Signal_ memState
|