2021-11-21 00:42:40 -05:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | DBus module for Clevo Keyboard control
|
|
|
|
|
|
|
|
module XMonad.Internal.DBus.Brightness.ClevoKeyboard
|
|
|
|
( callGetBrightnessCK
|
|
|
|
, matchSignalCK
|
|
|
|
, exportClevoKeyboard
|
|
|
|
, clevoKeyboardControls
|
|
|
|
, clevoKeyboardSignalDep
|
|
|
|
, blPath
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad (when)
|
|
|
|
|
|
|
|
import Data.Int (Int32)
|
|
|
|
|
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
|
|
|
|
|
|
|
import System.FilePath.Posix
|
|
|
|
|
|
|
|
import XMonad.Internal.DBus.Brightness.Common
|
|
|
|
import XMonad.Internal.Dependency
|
|
|
|
import XMonad.Internal.IO
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Low level sysfs functions
|
|
|
|
--
|
|
|
|
type Brightness = Float
|
|
|
|
|
|
|
|
type RawBrightness = Int32
|
|
|
|
|
|
|
|
type RawBounds = (RawBrightness, RawBrightness)
|
|
|
|
|
|
|
|
steps :: Int
|
|
|
|
steps = 16
|
|
|
|
|
|
|
|
-- assume this is hardcoded into the driver and will never change
|
|
|
|
maxRawBrightness :: RawBrightness
|
|
|
|
maxRawBrightness = 255
|
|
|
|
|
|
|
|
minRawBrightness :: RawBrightness
|
|
|
|
minRawBrightness = 0
|
|
|
|
|
|
|
|
backlightDir :: FilePath
|
|
|
|
backlightDir = "/sys/devices/platform/tuxedo_keyboard"
|
|
|
|
|
|
|
|
stateFile :: FilePath
|
|
|
|
stateFile = backlightDir </> "state"
|
|
|
|
|
|
|
|
stateChange :: Bool -> IO ()
|
|
|
|
stateChange = writeBool stateFile
|
|
|
|
|
|
|
|
stateOn :: IO ()
|
|
|
|
stateOn = stateChange True
|
|
|
|
|
|
|
|
stateOff :: IO ()
|
|
|
|
stateOff = stateChange False
|
|
|
|
|
|
|
|
brightnessFile :: FilePath
|
|
|
|
brightnessFile = backlightDir </> "brightness"
|
|
|
|
|
|
|
|
getBrightness :: RawBounds -> IO Brightness
|
|
|
|
getBrightness bounds = readPercent bounds brightnessFile
|
|
|
|
|
|
|
|
minBrightness :: RawBounds -> IO Brightness
|
|
|
|
minBrightness bounds = do
|
|
|
|
b <- writePercentMin bounds brightnessFile
|
|
|
|
stateOff
|
|
|
|
return b
|
|
|
|
|
|
|
|
maxBrightness :: RawBounds -> IO Brightness
|
|
|
|
maxBrightness bounds = stateOn >> writePercentMax bounds brightnessFile
|
|
|
|
|
|
|
|
incBrightness :: RawBounds -> IO Brightness
|
|
|
|
incBrightness bounds = stateOn >> incPercent steps brightnessFile bounds
|
|
|
|
|
|
|
|
decBrightness :: RawBounds -> IO Brightness
|
|
|
|
decBrightness bounds = do
|
|
|
|
b <- decPercent steps brightnessFile bounds
|
|
|
|
when (b == 0) stateOff
|
|
|
|
return b
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | DBus interface
|
|
|
|
|
|
|
|
blPath :: ObjectPath
|
|
|
|
blPath = objectPath_ "/clevo_keyboard"
|
|
|
|
|
|
|
|
interface :: InterfaceName
|
|
|
|
interface = interfaceName_ "org.xmonad.Brightness"
|
|
|
|
|
|
|
|
clevoKeyboardConfig :: BrightnessConfig RawBrightness Brightness
|
|
|
|
clevoKeyboardConfig = BrightnessConfig
|
|
|
|
{ bcMin = minBrightness
|
|
|
|
, bcMax = maxBrightness
|
|
|
|
, bcInc = incBrightness
|
|
|
|
, bcDec = decBrightness
|
|
|
|
, bcGet = getBrightness
|
|
|
|
, bcGetMax = return maxRawBrightness
|
|
|
|
, bcMinRaw = minRawBrightness
|
|
|
|
, bcPath = blPath
|
|
|
|
, bcInterface = interface
|
|
|
|
, bcName = "Clevo keyboard"
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Exported haskell API
|
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
stateFileDep :: IODependency_
|
2022-07-09 01:02:37 -04:00
|
|
|
stateFileDep = pathRW stateFile [Package True "tuxedo-keyboard"]
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
brightnessFileDep :: IODependency_
|
2022-07-09 01:02:37 -04:00
|
|
|
brightnessFileDep = pathR brightnessFile [Package True "tuxedo-keyboard"]
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
clevoKeyboardSignalDep :: DBusDependency_
|
2021-11-21 00:42:40 -05:00
|
|
|
clevoKeyboardSignalDep = signalDep clevoKeyboardConfig
|
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
exportClevoKeyboard :: Maybe Client -> SometimesIO
|
2022-07-09 01:02:37 -04:00
|
|
|
exportClevoKeyboard = brightnessExporter xpfClevoBacklight []
|
2022-07-08 20:01:35 -04:00
|
|
|
[stateFileDep, brightnessFileDep] clevoKeyboardConfig
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2021-11-21 22:47:43 -05:00
|
|
|
clevoKeyboardControls :: Maybe Client -> BrightnessControls
|
2022-07-08 20:01:35 -04:00
|
|
|
clevoKeyboardControls = brightnessControls xpfClevoBacklight clevoKeyboardConfig
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2021-11-21 17:54:00 -05:00
|
|
|
callGetBrightnessCK :: Client -> IO (Maybe Brightness)
|
2021-11-21 00:42:40 -05:00
|
|
|
callGetBrightnessCK = callGetBrightness clevoKeyboardConfig
|
|
|
|
|
2021-11-23 18:28:38 -05:00
|
|
|
matchSignalCK :: (Maybe Brightness -> IO ()) -> Client -> IO ()
|
2021-11-21 00:42:40 -05:00
|
|
|
matchSignalCK = matchSignal clevoKeyboardConfig
|