2021-11-07 13:35:08 -05:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | DBus module for DBus brightness controls
|
|
|
|
|
|
|
|
module XMonad.Internal.DBus.Brightness.Common
|
|
|
|
( BrightnessConfig(..)
|
|
|
|
, BrightnessControls(..)
|
2021-11-20 12:40:53 -05:00
|
|
|
, brightnessControls
|
|
|
|
, brightnessExporter
|
2021-11-07 13:35:08 -05:00
|
|
|
, callGetBrightness
|
|
|
|
, matchSignal
|
2021-11-20 19:35:24 -05:00
|
|
|
, signalDep
|
2021-11-07 13:35:08 -05:00
|
|
|
) where
|
|
|
|
|
2021-11-21 18:18:09 -05:00
|
|
|
-- import Control.Monad (void)
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
import Data.Int (Int32)
|
|
|
|
|
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
2021-11-20 19:35:24 -05:00
|
|
|
import qualified DBus.Introspection as I
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
import XMonad.Internal.DBus.Common
|
2021-11-07 18:41:25 -05:00
|
|
|
import XMonad.Internal.Dependency
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | External API
|
|
|
|
--
|
|
|
|
-- Define four methods to increase, decrease, maximize, or minimize the
|
|
|
|
-- brightness. These methods will all return the current brightness as a 32-bit
|
|
|
|
-- integer and emit a signal with the same brightness value. Additionally, there
|
|
|
|
-- is one method to get the current brightness.
|
|
|
|
|
|
|
|
data BrightnessConfig a b = BrightnessConfig
|
2021-11-21 00:42:40 -05:00
|
|
|
{ bcMin :: (a, a) -> IO b
|
|
|
|
, bcMax :: (a, a) -> IO b
|
|
|
|
, bcDec :: (a, a) -> IO b
|
|
|
|
, bcInc :: (a, a) -> IO b
|
|
|
|
, bcGet :: (a, a) -> IO b
|
|
|
|
, bcMinRaw :: a
|
2021-11-07 13:35:08 -05:00
|
|
|
, bcGetMax :: IO a
|
|
|
|
, bcPath :: ObjectPath
|
|
|
|
, bcInterface :: InterfaceName
|
2021-11-20 19:35:24 -05:00
|
|
|
, bcName :: String
|
2021-11-07 13:35:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
data BrightnessControls = BrightnessControls
|
2021-11-20 01:15:04 -05:00
|
|
|
{ bctlMax :: FeatureIO
|
|
|
|
, bctlMin :: FeatureIO
|
|
|
|
, bctlInc :: FeatureIO
|
|
|
|
, bctlDec :: FeatureIO
|
2021-11-07 13:35:08 -05:00
|
|
|
}
|
|
|
|
|
2021-11-21 22:47:43 -05:00
|
|
|
brightnessControls :: BrightnessConfig a b -> Maybe Client -> BrightnessControls
|
2021-11-21 17:54:00 -05:00
|
|
|
brightnessControls bc client =
|
2021-11-20 12:40:53 -05:00
|
|
|
BrightnessControls
|
2021-11-20 19:35:24 -05:00
|
|
|
{ bctlMax = cb "max brightness" memMax
|
|
|
|
, bctlMin = cb "min brightness" memMin
|
|
|
|
, bctlInc = cb "increase brightness" memInc
|
|
|
|
, bctlDec = cb "decrease brightness" memDec
|
2021-11-20 12:40:53 -05:00
|
|
|
}
|
2021-11-11 00:11:15 -05:00
|
|
|
where
|
2021-11-21 17:54:00 -05:00
|
|
|
cb = callBacklight client bc
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-21 16:58:01 -05:00
|
|
|
-- TODO not dry
|
2021-11-21 17:54:00 -05:00
|
|
|
callGetBrightness :: Num c => BrightnessConfig a b -> Client -> IO (Maybe c)
|
|
|
|
callGetBrightness BrightnessConfig { bcPath = p, bcInterface = i } client = do
|
|
|
|
reply <- callMethod client xmonadBusName p i memGet
|
2021-11-21 16:58:01 -05:00
|
|
|
return $ either (const Nothing) bodyGetBrightness reply
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-22 23:02:23 -05:00
|
|
|
signalDep :: BrightnessConfig a b -> DBusDep
|
2021-11-20 19:35:24 -05:00
|
|
|
signalDep BrightnessConfig { bcPath = p, bcInterface = i } =
|
2021-11-21 23:55:19 -05:00
|
|
|
Endpoint xmonadBusName p i $ Signal_ memCur
|
2021-11-20 19:35:24 -05:00
|
|
|
|
2021-11-23 18:28:38 -05:00
|
|
|
matchSignal :: Num c => BrightnessConfig a b -> (Maybe c -> IO ()) -> Client -> IO ()
|
|
|
|
matchSignal BrightnessConfig { bcPath = p, bcInterface = i } cb =
|
|
|
|
addMatchCallback brMatcher (cb . bodyGetBrightness)
|
2021-11-07 13:35:08 -05:00
|
|
|
where
|
|
|
|
brMatcher = matchAny
|
|
|
|
{ matchPath = Just p
|
|
|
|
, matchInterface = Just i
|
|
|
|
, matchMember = Just memCur
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Internal DBus Crap
|
|
|
|
|
2021-11-20 12:40:53 -05:00
|
|
|
brightnessExporter :: RealFrac b => [Dependency] -> BrightnessConfig a b
|
2021-11-21 22:47:43 -05:00
|
|
|
-> Maybe Client -> FeatureIO
|
2021-11-20 19:35:24 -05:00
|
|
|
brightnessExporter deps bc@BrightnessConfig { bcName = n } client = Feature
|
2021-11-22 23:28:54 -05:00
|
|
|
{ ftrDepTree = DBusTree (Single (exportBrightnessControls' bc)) client [Bus xmonadBusName] deps
|
2021-11-20 19:35:24 -05:00
|
|
|
, ftrName = n ++ " exporter"
|
|
|
|
, ftrWarning = Default
|
2021-11-11 00:11:15 -05:00
|
|
|
}
|
|
|
|
|
2021-11-07 13:35:08 -05:00
|
|
|
exportBrightnessControls' :: RealFrac b => BrightnessConfig a b -> Client -> IO ()
|
|
|
|
exportBrightnessControls' bc client = do
|
|
|
|
maxval <- bcGetMax bc -- assume the max value will never change
|
2021-11-21 00:42:40 -05:00
|
|
|
let bounds = (bcMinRaw bc, maxval)
|
|
|
|
let autoMethod' m f = autoMethod m $ emitBrightness bc client =<< f bc bounds
|
2021-11-07 13:35:08 -05:00
|
|
|
let funget = bcGet bc
|
|
|
|
export client (bcPath bc) defaultInterface
|
|
|
|
{ interfaceName = bcInterface bc
|
|
|
|
, interfaceMethods =
|
|
|
|
[ autoMethod' memMax bcMax
|
|
|
|
, autoMethod' memMin bcMin
|
|
|
|
, autoMethod' memInc bcInc
|
|
|
|
, autoMethod' memDec bcDec
|
2021-11-21 00:42:40 -05:00
|
|
|
, autoMethod memGet (round <$> funget bounds :: IO Int32)
|
2021-11-07 13:35:08 -05:00
|
|
|
]
|
2021-11-20 19:35:24 -05:00
|
|
|
, interfaceSignals = [sig]
|
2021-11-07 13:35:08 -05:00
|
|
|
}
|
2021-11-20 19:35:24 -05:00
|
|
|
where
|
|
|
|
sig = I.Signal
|
|
|
|
{ I.signalName = memCur
|
|
|
|
, I.signalArgs =
|
|
|
|
[
|
|
|
|
I.SignalArg
|
|
|
|
{ I.signalArgName = "brightness"
|
|
|
|
, I.signalArgType = TypeInt32
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
emitBrightness :: RealFrac b => BrightnessConfig a b -> Client -> b -> IO ()
|
|
|
|
emitBrightness BrightnessConfig{ bcPath = p, bcInterface = i } client cur =
|
|
|
|
emit client $ sig { signalBody = [toVariant (round cur :: Int32)] }
|
|
|
|
where
|
|
|
|
sig = signal p i memCur
|
|
|
|
|
2021-11-21 22:47:43 -05:00
|
|
|
callBacklight :: Maybe Client -> BrightnessConfig a b -> String -> MemberName -> FeatureIO
|
2021-11-21 17:54:00 -05:00
|
|
|
callBacklight client BrightnessConfig { bcPath = p, bcInterface = i, bcName = n } controlName m =
|
2021-11-21 18:18:09 -05:00
|
|
|
(featureEndpoint xmonadBusName p i m client)
|
|
|
|
{ ftrName = unwords [n, controlName] }
|
2021-11-07 13:35:08 -05:00
|
|
|
|
|
|
|
bodyGetBrightness :: Num a => [Variant] -> Maybe a
|
|
|
|
bodyGetBrightness [b] = fromIntegral <$> (fromVariant b :: Maybe Int32)
|
|
|
|
bodyGetBrightness _ = Nothing
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | DBus Members
|
|
|
|
|
|
|
|
memCur :: MemberName
|
|
|
|
memCur = memberName_ "CurrentBrightness"
|
|
|
|
|
|
|
|
memGet :: MemberName
|
|
|
|
memGet = memberName_ "GetBrightness"
|
|
|
|
|
|
|
|
memMax :: MemberName
|
|
|
|
memMax = memberName_ "MaxBrightness"
|
|
|
|
|
|
|
|
memMin :: MemberName
|
|
|
|
memMin = memberName_ "MinBrightness"
|
|
|
|
|
|
|
|
memInc :: MemberName
|
|
|
|
memInc = memberName_ "IncBrightness"
|
|
|
|
|
|
|
|
memDec :: MemberName
|
|
|
|
memDec = memberName_ "DecBrightness"
|