xmonad-config/lib/XMonad/Internal/DBus/Brightness/IntelBacklight.hs

139 lines
3.6 KiB
Haskell
Raw Permalink Normal View History

2023-01-03 22:18:55 -05:00
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- DBus module for Intel Backlight control
module XMonad.Internal.DBus.Brightness.IntelBacklight
( callGetBrightnessIB
, matchSignalIB
, exportIntelBacklight
, intelBacklightControls
, intelBacklightSignalDep
, blPath
2022-12-30 14:58:23 -05:00
)
where
2022-12-30 14:58:23 -05:00
import DBus
import Data.Internal.DBus
2023-01-01 18:33:02 -05:00
import Data.Internal.XIO
2022-12-31 15:26:22 -05:00
import RIO
2022-12-30 14:58:23 -05:00
import RIO.FilePath
import XMonad.Internal.DBus.Brightness.Common
import XMonad.Internal.IO
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Low level sysfs functions
type Brightness = Float
type RawBrightness = Int32
2021-11-21 00:42:40 -05:00
type RawBounds = (RawBrightness, RawBrightness)
steps :: Int
steps = 16
2021-11-21 00:42:40 -05:00
minRawBrightness :: RawBrightness
minRawBrightness = 1
backlightDir :: FilePath
backlightDir = "/sys/class/backlight/intel_backlight/"
maxFile :: FilePath
maxFile = backlightDir </> "max_brightness"
curFile :: FilePath
curFile = backlightDir </> "brightness"
2023-01-02 19:15:25 -05:00
getMaxRawBrightness :: MonadUnliftIO m => m RawBrightness
getMaxRawBrightness = readInt maxFile
2023-01-02 19:15:25 -05:00
getBrightness :: MonadUnliftIO m => RawBounds -> m Brightness
2021-11-21 00:42:40 -05:00
getBrightness bounds = readPercent bounds curFile
2023-01-02 19:15:25 -05:00
minBrightness :: MonadUnliftIO m => RawBounds -> m Brightness
2021-11-21 00:42:40 -05:00
minBrightness bounds = writePercentMin bounds curFile
2023-01-02 19:15:25 -05:00
maxBrightness :: MonadUnliftIO m => RawBounds -> m Brightness
2021-11-21 00:42:40 -05:00
maxBrightness bounds = writePercentMax bounds curFile
2023-01-02 19:15:25 -05:00
incBrightness :: MonadUnliftIO m => RawBounds -> m Brightness
incBrightness = incPercent steps curFile
2023-01-02 19:15:25 -05:00
decBrightness :: MonadUnliftIO m => RawBounds -> m Brightness
decBrightness = decPercent steps curFile
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- DBus interface
blPath :: ObjectPath
blPath = objectPath_ "/intelbacklight"
interface :: InterfaceName
interface = interfaceName_ "org.xmonad.Brightness"
2023-01-02 19:15:25 -05:00
intelBacklightConfig
:: MonadUnliftIO m
=> BrightnessConfig m RawBrightness Brightness
2022-12-30 14:58:23 -05:00
intelBacklightConfig =
BrightnessConfig
{ bcMin = minBrightness
, bcMax = maxBrightness
, bcInc = incBrightness
, bcDec = decBrightness
, bcGet = getBrightness
, bcGetMax = getMaxRawBrightness
, bcMinRaw = minRawBrightness
, bcPath = blPath
, bcInterface = interface
, bcName = "Intel backlight"
}
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Exported haskell API
curFileDep :: IODependency_
curFileDep = pathRW curFile []
maxFileDep :: IODependency_
maxFileDep = pathR maxFile []
2022-07-09 17:08:10 -04:00
intelBacklightSignalDep :: DBusDependency_ SesClient
2023-01-02 19:15:25 -05:00
intelBacklightSignalDep =
-- TODO do I need to get rid of the IO here?
signalDep (intelBacklightConfig :: BrightnessConfig IO RawBrightness Brightness)
exportIntelBacklight
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
=> Maybe SesClient
-> Sometimes (m (), m ())
2022-12-30 14:58:23 -05:00
exportIntelBacklight =
brightnessExporter
xpfIntelBacklight
[]
[curFileDep, maxFileDep]
intelBacklightConfig
2023-01-03 22:18:55 -05:00
intelBacklightControls
:: (HasClient (DBusEnv env), MonadReader env m, MonadUnliftIO m)
=> Maybe SesClient
-> BrightnessControls m
intelBacklightControls = brightnessControls xpfIntelBacklight intelBacklightConfig
2023-01-03 22:18:55 -05:00
callGetBrightnessIB
:: (SafeClient c, HasClient env, MonadReader (env c) m, MonadUnliftIO m)
=> m (Maybe Brightness)
2022-07-09 17:44:14 -04:00
callGetBrightnessIB = callGetBrightness intelBacklightConfig
2022-12-31 15:26:22 -05:00
matchSignalIB
2023-01-03 22:18:55 -05:00
:: ( SafeClient c
, HasLogFunc (env c)
, HasClient env
, MonadReader (env c) m
, MonadUnliftIO m
)
2022-12-31 15:26:22 -05:00
=> (Maybe Brightness -> m ())
-> m ()
2022-07-09 17:44:14 -04:00
matchSignalIB = matchSignal intelBacklightConfig