2020-03-21 01:18:38 -04:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Xmobar.Plugins.Bluetooth where
|
|
|
|
|
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
|
|
|
|
|
|
|
import Xmobar
|
|
|
|
|
2020-03-22 01:10:02 -04:00
|
|
|
data Bluetooth = Bluetooth (String, String, String) Int
|
|
|
|
deriving (Read, Show)
|
2020-03-21 01:18:38 -04:00
|
|
|
|
2020-03-22 01:10:02 -04:00
|
|
|
callGetPowered :: Client -> IO (Either MethodError Variant)
|
|
|
|
callGetPowered client =
|
|
|
|
getProperty client (methodCall "/org/bluez/hci0" "org.bluez.Adapter1" "Powered")
|
|
|
|
{ methodCallDestination = Just "org.bluez" }
|
2020-03-21 14:30:27 -04:00
|
|
|
|
2020-03-21 01:18:38 -04:00
|
|
|
instance Exec Bluetooth where
|
2020-03-22 01:10:02 -04:00
|
|
|
alias (Bluetooth _ _) = "bluetooth"
|
|
|
|
rate (Bluetooth _ r) = r
|
|
|
|
run (Bluetooth (text, colorOn, colorOff) _) = do
|
2020-03-21 01:18:38 -04:00
|
|
|
client <- connectSystem
|
2020-03-22 01:10:02 -04:00
|
|
|
reply <- callGetPowered client
|
|
|
|
disconnect client
|
|
|
|
return $ fmtState $ procReply reply
|
2020-03-21 01:18:38 -04:00
|
|
|
where
|
2020-03-22 01:10:02 -04:00
|
|
|
procReply = \case
|
|
|
|
-- TODO handle errors?
|
|
|
|
Right r -> fromVariant r
|
|
|
|
Left _ -> Nothing
|
2020-03-21 01:18:38 -04:00
|
|
|
fmtState = \case
|
|
|
|
Just s -> wrapColor text $ if s then colorOn else colorOff
|
|
|
|
Nothing -> "N/A"
|
|
|
|
wrapColor s c = "<fc=" ++ c ++ ">" ++ s ++ "</fc>"
|