2020-03-20 20:10:15 -04:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | DBus module for X11 screensave/DPMS control
|
|
|
|
|
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
|
|
|
|
) where
|
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
import Control.Monad (void)
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-25 18:35:04 -04:00
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-25 18:35:04 -04:00
|
|
|
import Graphics.X11.Xlib.Display
|
|
|
|
import Graphics.X11.XScreenSaver
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
import XMonad.Internal.DBus.Common
|
2020-04-06 00:14:56 -04:00
|
|
|
import XMonad.Internal.Process
|
2020-03-20 20:10:15 -04:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Low-level functions
|
|
|
|
|
|
|
|
type SSState = Bool -- true is enabled
|
|
|
|
|
|
|
|
toggle :: IO SSState
|
|
|
|
toggle = do
|
|
|
|
st <- query
|
|
|
|
-- TODO figure out how not to do this with shell commands
|
2020-04-06 00:14:56 -04:00
|
|
|
void $ createProcess' $ proc "xset" $ "s" : args st
|
2020-03-20 20:10:15 -04:00
|
|
|
-- TODO this assumes the command succeeds
|
|
|
|
return $ not st
|
|
|
|
where
|
|
|
|
args s = if s then ["off", "-dpms"] else ["on", "+dpms"]
|
|
|
|
|
|
|
|
query :: IO SSState
|
|
|
|
query = do
|
|
|
|
dpy <- openDisplay ""
|
|
|
|
xssi <- xScreenSaverQueryInfo dpy
|
|
|
|
closeDisplay dpy
|
|
|
|
return $ case xssi of
|
|
|
|
Just XScreenSaverInfo { xssi_state = ScreenSaverDisabled } -> False
|
|
|
|
Just XScreenSaverInfo { xssi_state = _ } -> True
|
|
|
|
-- TODO handle errors better (at least log them?)
|
|
|
|
Nothing -> False
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | DBus Interface
|
|
|
|
--
|
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
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
path :: ObjectPath
|
|
|
|
path = "/screensaver"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
interface :: InterfaceName
|
|
|
|
interface = "org.xmonad.Screensaver"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
memState :: MemberName
|
|
|
|
memState = "State"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
memToggle :: MemberName
|
|
|
|
memToggle = "Toggle"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
memQuery :: MemberName
|
|
|
|
memQuery = "Query"
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
sigCurrentState :: Signal
|
|
|
|
sigCurrentState = signal path interface memState
|
2020-03-20 20:10:15 -04:00
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
ruleCurrentState :: MatchRule
|
|
|
|
ruleCurrentState = matchAny
|
|
|
|
{ matchPath = Just path
|
|
|
|
, matchInterface = Just interface
|
|
|
|
, matchMember = Just memState
|
2020-03-20 20:10:15 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
emitState :: Client -> SSState -> IO ()
|
|
|
|
emitState client sss = emit client $ sigCurrentState { signalBody = [toVariant sss] }
|
|
|
|
|
|
|
|
bodyGetCurrentState :: [Variant] -> Maybe SSState
|
|
|
|
bodyGetCurrentState [b] = fromVariant b :: Maybe SSState
|
|
|
|
bodyGetCurrentState _ = Nothing
|
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2020-03-20 23:47:02 -04:00
|
|
|
-- | Exported haskell API
|
|
|
|
|
2020-03-20 20:10:15 -04:00
|
|
|
exportScreensaver :: Client -> IO ()
|
|
|
|
exportScreensaver client =
|
2020-03-20 23:47:02 -04:00
|
|
|
export client path defaultInterface
|
|
|
|
{ interfaceName = interface
|
2020-03-20 20:10:15 -04:00
|
|
|
, interfaceMethods =
|
2020-03-20 23:47:02 -04:00
|
|
|
[ autoMethod memToggle $ emitState client =<< toggle
|
|
|
|
, autoMethod memQuery query
|
2020-03-20 20:10:15 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-03-20 23:47:02 -04:00
|
|
|
callToggle :: IO ()
|
|
|
|
callToggle = void $ callMethod $ methodCall path interface memToggle
|
2020-03-20 20:10:15 -04:00
|
|
|
|
|
|
|
callQuery :: IO (Maybe SSState)
|
2020-03-20 23:47:02 -04:00
|
|
|
callQuery = do
|
|
|
|
reply <- callMethod $ methodCall path interface memQuery
|
|
|
|
return $ reply >>= bodyGetCurrentState
|
2020-03-20 20:10:15 -04:00
|
|
|
|
|
|
|
matchSignal :: (Maybe SSState -> IO ()) -> IO SignalHandler
|
2020-03-20 23:47:02 -04:00
|
|
|
matchSignal cb = addMatchCallback ruleCurrentState $ cb . bodyGetCurrentState
|