2020-03-21 01:18:38 -04:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2020-04-01 22:06:00 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Bluetooth plugin
|
|
|
|
--
|
|
|
|
-- Use the bluez interface on DBus to check status
|
|
|
|
|
2021-06-21 23:41:57 -04:00
|
|
|
module Xmobar.Plugins.Bluetooth
|
|
|
|
( Bluetooth(..)
|
|
|
|
, btAlias
|
|
|
|
, btBus
|
|
|
|
, btPath
|
2021-11-08 00:27:39 -05:00
|
|
|
, btPowered
|
|
|
|
, btInterface
|
2021-06-21 23:41:57 -04:00
|
|
|
) where
|
2020-03-21 01:18:38 -04:00
|
|
|
|
2020-03-25 18:55:52 -04:00
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
2020-03-21 01:18:38 -04:00
|
|
|
|
2020-04-01 20:17:47 -04:00
|
|
|
import XMonad.Hooks.DynamicLog (xmobarColor)
|
2021-06-19 00:54:01 -04:00
|
|
|
import Xmobar
|
2020-03-21 01:18:38 -04:00
|
|
|
|
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 =
|
2021-11-08 00:27:39 -05:00
|
|
|
getProperty client (methodCall btPath btInterface $ memberName_ btPowered)
|
2021-06-21 23:41:57 -04:00
|
|
|
{ methodCallDestination = Just btBus }
|
|
|
|
|
2021-11-08 00:27:39 -05:00
|
|
|
btInterface :: InterfaceName
|
|
|
|
btInterface = "org.bluez.Adapter1"
|
|
|
|
|
|
|
|
-- weird that this is a string when introspecting but a member name when calling
|
|
|
|
-- a method, not sure if it is supposed to work like that
|
|
|
|
btPowered :: String
|
|
|
|
btPowered = "Powered"
|
|
|
|
|
2021-06-21 23:41:57 -04:00
|
|
|
btBus :: BusName
|
|
|
|
btBus = "org.bluez"
|
|
|
|
|
|
|
|
-- TODO this feels like something that shouldn't be hardcoded
|
|
|
|
btPath :: ObjectPath
|
|
|
|
btPath = "/org/bluez/hci0"
|
|
|
|
|
|
|
|
btAlias :: String
|
|
|
|
btAlias = "bluetooth"
|
2020-03-21 14:30:27 -04:00
|
|
|
|
2020-03-21 01:18:38 -04:00
|
|
|
instance Exec Bluetooth where
|
2021-06-21 23:41:57 -04:00
|
|
|
alias (Bluetooth _ _) = btAlias
|
2020-03-22 01:10:02 -04:00
|
|
|
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
|
2021-06-19 00:54:01 -04:00
|
|
|
Just s -> xmobarColor (if s then colorOn else colorOff) "" text
|
2020-03-21 01:18:38 -04:00
|
|
|
Nothing -> "N/A"
|