xmonad-config/lib/Xmobar/Plugins/Bluetooth.hs

354 lines
11 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
2020-04-01 22:06:00 -04:00
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Bluetooth plugin
2020-04-01 22:06:00 -04:00
--
-- Use the bluez interface on DBus to check status
--
-- org.bluez dynamically updates its DBus interfaces using the standard Object
-- Manager. The adapter is located at path "/org/bluez/hci<X>" where X is
-- usually 0, and each device is "/org/bluez/hci<X>/<MAC_ADDRESS>".
--
-- This plugin will reflect if the adapter is powered and if any device is
-- connected to it. The rough outline for this procedure:
-- 1) get the adapter from the object manager
-- 2) get all devices associated with the adapter using the object interface
-- 3) determine if the adapter is powered
-- 4) determine if any devices are connected
-- 5) format the icon; powered vs not powered controls the color and connected
-- vs not connected controls the icon (connected bluetooth symbol has two
-- dots flanking it)
--
-- Step 3 can be accomplished using the "org.bluez.Adapter1" interface and
-- querying the "Powered" property. Step 4 can be done using the
-- "org.bluez.Device1" interface and the "Connected" property for each device
-- path. Since these are properties, we can asynchronously read changes to them
-- via the "PropertiesChanged" signal.
--
-- If any devices are added/removed, steps 2-4 will need to be redone and any
-- listeners will need to be updated. (TODO not sure which signals to use in
-- determining if a device is added)
--
-- TODO also not sure if I need to care about multiple adapters and/or the
-- adapter changing.
2020-04-01 22:06:00 -04:00
module Xmobar.Plugins.Bluetooth
2022-12-30 14:58:23 -05:00
( Bluetooth (..)
, btAlias
2021-11-23 18:28:38 -05:00
, btDep
2022-12-30 14:58:23 -05:00
)
where
import DBus
import DBus.Client
import Data.Internal.DBus
import Data.Internal.Dependency
2022-12-30 16:37:52 -05:00
import RIO
2022-12-31 19:47:02 -05:00
import RIO.FilePath
import RIO.List
import qualified RIO.Map as M
2022-12-30 14:58:23 -05:00
import qualified RIO.Text as T
import XMonad.Internal.DBus.Common
import Xmobar
import Xmobar.Plugins.Common
2020-03-21 01:18:38 -04:00
btAlias :: T.Text
btAlias = "bluetooth"
2022-07-09 17:08:10 -04:00
btDep :: DBusDependency_ SysClient
2022-12-30 14:58:23 -05:00
btDep =
Endpoint [Package Official "bluez"] btBus btOMPath omInterface $
Method_ getManagedObjects
data Bluetooth = Bluetooth Icons Colors deriving (Read, Show)
2020-03-21 01:18:38 -04:00
instance Exec Bluetooth where
alias (Bluetooth _ _) = T.unpack btAlias
start (Bluetooth icons colors) cb =
2022-07-09 17:08:10 -04:00
withDBusClientConnection cb $ startAdapter icons colors cb
2022-12-30 16:58:30 -05:00
startAdapter
:: MonadUnliftIO m
=> Icons
-> Colors
-> Callback
-> SysClient
-> m ()
startAdapter is cs cb cl = do
ot <- getBtObjectTree cl
state <- newMVar emptyState
2022-12-30 16:37:52 -05:00
let dpy = displayIcon cb (iconFormatter is cs) state
forM_ (findAdapter ot) $ \adapter -> do
-- set up adapter
initAdapter state adapter cl
2021-11-27 13:24:13 -05:00
-- TODO this step could fail; at least warn the user...
2022-12-30 16:37:52 -05:00
void $ addAdaptorListener state dpy adapter cl
-- set up devices on the adapter (and listeners for adding/removing devices)
let devices = findDevices adapter ot
2022-12-30 16:37:52 -05:00
addDeviceAddedListener state dpy adapter cl
addDeviceRemovedListener state dpy adapter cl
forM_ devices $ \d -> addAndInitDevice state dpy d cl
-- after setting things up, show the icon based on the initialized state
2022-12-30 16:37:52 -05:00
dpy
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Icon Display
--
-- Color corresponds to the adaptor powered state, and the icon corresponds to
-- if it is paired or not. If the adaptor state is undefined, display "N/A"
2021-11-08 00:27:39 -05:00
type IconFormatter = (Maybe Bool -> Bool -> T.Text)
type Icons = (T.Text, T.Text)
2022-12-30 16:58:30 -05:00
displayIcon :: MonadUnliftIO m => Callback -> IconFormatter -> MutableBtState -> m ()
displayIcon callback formatter =
2022-12-30 16:58:30 -05:00
liftIO . callback . T.unpack . uncurry formatter <=< readState
-- TODO maybe I want this to fail when any of the device statuses are Nothing
iconFormatter :: Icons -> Colors -> IconFormatter
2021-11-27 17:33:02 -05:00
iconFormatter (iconConn, iconDisc) cs powered connected =
maybe na (\p -> colorText cs p icon) powered
where
icon = if connected then iconConn else iconDisc
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Connection State
--
-- The signal handlers all run on separate threads, yet the icon depends on
-- the state reflected by all these signals. The best (only?) way to do this is
-- is to track the shared state of the bluetooth adaptor and its devices using
-- an MVar.
data BTDevice = BTDevice
2022-12-30 14:58:23 -05:00
{ btDevConnected :: Maybe Bool
, btDevSigHandler :: SignalHandler
}
type ConnectedDevices = M.Map ObjectPath BTDevice
data BtState = BtState
{ btDevices :: ConnectedDevices
, btPowered :: Maybe Bool
}
type MutableBtState = MVar BtState
emptyState :: BtState
2022-12-30 14:58:23 -05:00
emptyState =
BtState
{ btDevices = M.empty
, btPowered = Nothing
}
2022-12-30 16:58:30 -05:00
readState :: MonadUnliftIO m => MutableBtState -> m (Maybe Bool, Bool)
readState state = do
p <- readPowered state
c <- readDevices state
return (p, anyDevicesConnected c)
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Object manager
findAdapter :: ObjectTree -> Maybe ObjectPath
findAdapter = find (("/org/bluez/hci" `isPrefixOf`) . formatObjectPath) . M.keys
findDevices :: ObjectPath -> ObjectTree -> [ObjectPath]
findDevices adapter = filter (adaptorHasDevice adapter) . M.keys
adaptorHasDevice :: ObjectPath -> ObjectPath -> Bool
2022-12-31 19:47:02 -05:00
adaptorHasDevice adaptor device = case splitPathNoRoot device of
[org, bluez, hciX, _] -> splitPathNoRoot adaptor == [org, bluez, hciX]
2022-12-30 14:58:23 -05:00
_ -> False
2022-12-31 19:47:02 -05:00
splitPathNoRoot :: ObjectPath -> [FilePath]
splitPathNoRoot = dropWhile (== "/") . splitDirectories . formatObjectPath
2022-12-30 16:58:30 -05:00
getBtObjectTree :: MonadUnliftIO m => SysClient -> m ObjectTree
2022-07-09 17:44:14 -04:00
getBtObjectTree sys = callGetManagedObjects sys btBus btOMPath
2021-11-08 00:27:39 -05:00
2021-11-27 13:24:13 -05:00
btOMPath :: ObjectPath
btOMPath = objectPath_ "/"
2022-12-30 16:37:52 -05:00
addBtOMListener :: MonadUnliftIO m => SignalCallback m -> SysClient -> m ()
addBtOMListener sc = void . addInterfaceAddedListener btBus btOMPath sc
2022-12-30 16:58:30 -05:00
addDeviceAddedListener
:: MonadUnliftIO m
=> MutableBtState
-> m ()
-> ObjectPath
-> SysClient
-> m ()
2022-12-30 16:37:52 -05:00
addDeviceAddedListener state dpy adapter client =
2021-11-27 13:24:13 -05:00
addBtOMListener addDevice client
where
2022-12-30 16:37:52 -05:00
addDevice = pathCallback adapter dpy $ \d ->
addAndInitDevice state dpy d client
2020-03-21 14:30:27 -04:00
2022-12-30 16:58:30 -05:00
addDeviceRemovedListener
:: (MonadUnliftIO m)
=> MutableBtState
-> m ()
-> ObjectPath
-> SysClient
-> m ()
2022-12-30 16:37:52 -05:00
addDeviceRemovedListener state dpy adapter sys =
2022-07-09 17:08:10 -04:00
addBtOMListener remDevice sys
where
2022-12-30 16:37:52 -05:00
remDevice = pathCallback adapter dpy $ \d -> do
old <- removeDevice state d
2022-12-30 16:58:30 -05:00
forM_ old $ liftIO . removeMatch (toClient sys) . btDevSigHandler
2022-12-30 16:58:30 -05:00
pathCallback :: MonadUnliftIO m => ObjectPath -> m () -> (ObjectPath -> m ()) -> SignalCallback m
pathCallback adapter dpy f [device, _] = forM_ (fromVariant device) $ \d ->
2022-12-30 16:37:52 -05:00
when (adaptorHasDevice adapter d) $ f d >> dpy
pathCallback _ _ _ _ = return ()
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Adapter
2022-12-30 16:58:30 -05:00
initAdapter
:: (MonadUnliftIO m)
=> MutableBtState
-> ObjectPath
-> SysClient
-> m ()
initAdapter state adapter client = do
reply <- callGetPowered adapter client
putPowered state $ fromSingletonVariant reply
2022-12-30 16:58:30 -05:00
matchBTProperty
:: (MonadUnliftIO m)
=> SysClient
-> ObjectPath
-> m (Maybe MatchRule)
2022-07-09 17:44:14 -04:00
matchBTProperty sys p = matchPropertyFull sys btBus (Just p)
2021-11-27 13:24:13 -05:00
2022-12-30 14:58:23 -05:00
addAdaptorListener
2022-12-30 16:58:30 -05:00
:: MonadUnliftIO m
=> MutableBtState
-> m ()
2022-12-30 14:58:23 -05:00
-> ObjectPath
-> SysClient
2022-12-30 16:58:30 -05:00
-> m (Maybe SignalHandler)
2022-12-30 16:37:52 -05:00
addAdaptorListener state dpy adaptor sys = do
2022-07-09 17:08:10 -04:00
rule <- matchBTProperty sys adaptor
2022-07-09 17:44:14 -04:00
forM rule $ \r -> addMatchCallback r (procMatch . matchPowered) sys
where
2022-12-30 16:37:52 -05:00
procMatch = withSignalMatch $ \b -> putPowered state b >> dpy
2022-12-30 16:58:30 -05:00
callGetPowered :: MonadUnliftIO m => ObjectPath -> SysClient -> m [Variant]
2022-12-30 14:58:23 -05:00
callGetPowered adapter =
callPropertyGet btBus adapter adapterInterface $
memberName_ $
T.unpack adaptorPowered
2021-11-23 18:28:38 -05:00
matchPowered :: [Variant] -> SignalMatch Bool
matchPowered = matchPropertyChanged adapterInterface adaptorPowered
2022-12-30 16:58:30 -05:00
putPowered :: MonadUnliftIO m => MutableBtState -> Maybe Bool -> m ()
2022-12-30 14:58:23 -05:00
putPowered m ds = modifyMVar_ m (\s -> return s {btPowered = ds})
2022-12-30 16:58:30 -05:00
readPowered :: MonadUnliftIO m => MutableBtState -> m (Maybe Bool)
readPowered = fmap btPowered . readMVar
adapterInterface :: InterfaceName
adapterInterface = interfaceName_ "org.bluez.Adapter1"
adaptorPowered :: T.Text
adaptorPowered = "Powered"
--------------------------------------------------------------------------------
2022-12-30 14:58:23 -05:00
-- Devices
2022-12-30 16:58:30 -05:00
addAndInitDevice
:: MonadUnliftIO m
=> MutableBtState
-> m ()
-> ObjectPath
-> SysClient
-> m ()
2022-12-30 16:37:52 -05:00
addAndInitDevice state dpy device client = do
sh <- addDeviceListener state dpy device client
2021-11-27 13:24:13 -05:00
-- TODO add some intelligent error messages here
forM_ sh $ \s -> initDevice state s device client
2022-12-30 16:58:30 -05:00
initDevice
:: MonadUnliftIO m
=> MutableBtState
-> SignalHandler
-> ObjectPath
-> SysClient
-> m ()
2022-07-09 17:44:14 -04:00
initDevice state sh device sys = do
reply <- callGetConnected device sys
2022-12-30 14:58:23 -05:00
void $
insertDevice state device $
BTDevice
{ btDevConnected = fromVariant =<< listToMaybe reply
, btDevSigHandler = sh
}
addDeviceListener
2022-12-30 16:58:30 -05:00
:: MonadUnliftIO m
=> MutableBtState
-> m ()
2022-12-30 14:58:23 -05:00
-> ObjectPath
-> SysClient
2022-12-30 16:58:30 -05:00
-> m (Maybe SignalHandler)
2022-12-30 16:37:52 -05:00
addDeviceListener state dpy device sys = do
2022-07-09 17:44:14 -04:00
rule <- matchBTProperty sys device
forM rule $ \r -> addMatchCallback r (procMatch . matchConnected) sys
where
2022-12-30 16:37:52 -05:00
procMatch = withSignalMatch $ \c -> updateDevice state device c >> dpy
matchConnected :: [Variant] -> SignalMatch Bool
matchConnected = matchPropertyChanged devInterface devConnected
2022-12-30 16:58:30 -05:00
callGetConnected :: MonadUnliftIO m => ObjectPath -> SysClient -> m [Variant]
2022-12-30 14:58:23 -05:00
callGetConnected p =
callPropertyGet btBus p devInterface $
memberName_ (T.unpack devConnected)
2022-12-30 16:58:30 -05:00
insertDevice
:: MonadUnliftIO m
=> MutableBtState
-> ObjectPath
-> BTDevice
-> m Bool
insertDevice m device dev = modifyMVar m $ \s -> do
let new = M.insert device dev $ btDevices s
2022-12-30 14:58:23 -05:00
return (s {btDevices = new}, anyDevicesConnected new)
2022-12-30 16:58:30 -05:00
updateDevice
:: MonadUnliftIO m
=> MutableBtState
-> ObjectPath
-> Maybe Bool
-> m Bool
updateDevice m device status = modifyMVar m $ \s -> do
2022-12-30 14:58:23 -05:00
let new = M.update (\d -> Just d {btDevConnected = status}) device $ btDevices s
return (s {btDevices = new}, anyDevicesConnected new)
anyDevicesConnected :: ConnectedDevices -> Bool
anyDevicesConnected = or . mapMaybe btDevConnected . M.elems
2022-12-30 16:58:30 -05:00
removeDevice
:: MonadUnliftIO m
=> MutableBtState
-> ObjectPath
-> m (Maybe BTDevice)
removeDevice m device = modifyMVar m $ \s -> do
let devs = btDevices s
2022-12-30 14:58:23 -05:00
return (s {btDevices = M.delete device devs}, M.lookup device devs)
2022-12-30 16:58:30 -05:00
readDevices :: MonadUnliftIO m => MutableBtState -> m ConnectedDevices
readDevices = fmap btDevices . readMVar
devInterface :: InterfaceName
devInterface = interfaceName_ "org.bluez.Device1"
2021-11-25 00:12:00 -05:00
devConnected :: T.Text
devConnected = "Connected"