2022-12-26 14:45:49 -05:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2021-11-21 00:42:40 -05:00
|
|
|
--------------------------------------------------------------------------------
|
2022-12-30 14:58:23 -05:00
|
|
|
-- DBus module for Clevo Keyboard control
|
2021-11-21 00:42:40 -05:00
|
|
|
|
|
|
|
module XMonad.Internal.DBus.Brightness.ClevoKeyboard
|
|
|
|
( callGetBrightnessCK
|
|
|
|
, matchSignalCK
|
|
|
|
, exportClevoKeyboard
|
|
|
|
, clevoKeyboardControls
|
|
|
|
, clevoKeyboardSignalDep
|
|
|
|
, blPath
|
2022-12-30 14:58:23 -05:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
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
|
2021-11-21 00:42:40 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-12-30 14:58:23 -05:00
|
|
|
-- Low level sysfs functions
|
|
|
|
|
2021-11-21 00:42:40 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-12-30 14:58:23 -05:00
|
|
|
-- DBus interface
|
2021-11-21 00:42:40 -05:00
|
|
|
|
|
|
|
blPath :: ObjectPath
|
|
|
|
blPath = objectPath_ "/clevo_keyboard"
|
|
|
|
|
|
|
|
interface :: InterfaceName
|
|
|
|
interface = interfaceName_ "org.xmonad.Brightness"
|
|
|
|
|
|
|
|
clevoKeyboardConfig :: BrightnessConfig RawBrightness Brightness
|
2022-12-30 14:58:23 -05:00
|
|
|
clevoKeyboardConfig =
|
|
|
|
BrightnessConfig
|
|
|
|
{ bcMin = minBrightness
|
|
|
|
, bcMax = maxBrightness
|
|
|
|
, bcInc = incBrightness
|
|
|
|
, bcDec = decBrightness
|
|
|
|
, bcGet = getBrightness
|
|
|
|
, bcGetMax = return maxRawBrightness
|
|
|
|
, bcMinRaw = minRawBrightness
|
|
|
|
, bcPath = blPath
|
|
|
|
, bcInterface = interface
|
|
|
|
, bcName = "Clevo keyboard"
|
|
|
|
}
|
2021-11-21 00:42:40 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-12-30 14:58:23 -05:00
|
|
|
-- Exported haskell API
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
stateFileDep :: IODependency_
|
2022-07-09 14:59:42 -04:00
|
|
|
stateFileDep = pathRW stateFile [Package AUR "tuxedo-keyboard"]
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
brightnessFileDep :: IODependency_
|
2022-07-09 14:59:42 -04:00
|
|
|
brightnessFileDep = pathR brightnessFile [Package AUR "tuxedo-keyboard"]
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
clevoKeyboardSignalDep :: DBusDependency_ SesClient
|
2021-11-21 00:42:40 -05:00
|
|
|
clevoKeyboardSignalDep = signalDep clevoKeyboardConfig
|
|
|
|
|
2023-01-01 13:32:46 -05:00
|
|
|
exportClevoKeyboard
|
|
|
|
:: (MonadReader env m, HasLogFunc env, MonadUnliftIO m)
|
|
|
|
=> Maybe SesClient
|
|
|
|
-> Sometimes (m (), m ())
|
2022-12-30 14:58:23 -05:00
|
|
|
exportClevoKeyboard =
|
|
|
|
brightnessExporter
|
|
|
|
xpfClevoBacklight
|
|
|
|
[]
|
|
|
|
[stateFileDep, brightnessFileDep]
|
|
|
|
clevoKeyboardConfig
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2023-01-02 18:30:17 -05:00
|
|
|
clevoKeyboardControls :: MonadUnliftIO m => Maybe SesClient -> BrightnessControls m
|
2022-07-08 20:01:35 -04:00
|
|
|
clevoKeyboardControls = brightnessControls xpfClevoBacklight clevoKeyboardConfig
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2022-12-31 15:26:22 -05:00
|
|
|
callGetBrightnessCK :: MonadUnliftIO m => SesClient -> m (Maybe Brightness)
|
2022-07-09 17:44:14 -04:00
|
|
|
callGetBrightnessCK = callGetBrightness clevoKeyboardConfig
|
2021-11-21 00:42:40 -05:00
|
|
|
|
2022-12-31 15:26:22 -05:00
|
|
|
matchSignalCK
|
|
|
|
:: MonadUnliftIO m
|
|
|
|
=> (Maybe Brightness -> m ())
|
|
|
|
-> SesClient
|
|
|
|
-> m ()
|
2022-07-09 17:44:14 -04:00
|
|
|
matchSignalCK = matchSignal clevoKeyboardConfig
|