2020-04-01 22:06:00 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Bluetooth plugin
|
|
|
|
--
|
|
|
|
-- Use the bluez interface on DBus to check status
|
2021-11-26 23:35:03 -05:00
|
|
|
--
|
|
|
|
-- 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
|
|
|
|
2021-06-21 23:41:57 -04:00
|
|
|
module Xmobar.Plugins.Bluetooth
|
|
|
|
( Bluetooth(..)
|
|
|
|
, btAlias
|
2021-11-23 18:28:38 -05:00
|
|
|
, btDep
|
2021-06-21 23:41:57 -04:00
|
|
|
) where
|
2020-03-21 01:18:38 -04:00
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
import Control.Concurrent.MVar
|
|
|
|
import Control.Monad
|
|
|
|
|
|
|
|
import Data.List
|
|
|
|
import Data.List.Split
|
2021-11-27 01:02:22 -05:00
|
|
|
import qualified Data.Map as M
|
2021-11-26 23:35:03 -05:00
|
|
|
import Data.Maybe
|
|
|
|
|
2020-03-25 18:55:52 -04:00
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
2021-11-27 01:02:22 -05:00
|
|
|
import DBus.Internal
|
2020-03-21 01:18:38 -04:00
|
|
|
|
2021-11-23 18:28:38 -05:00
|
|
|
import XMonad.Internal.Dependency
|
2021-06-19 00:54:01 -04:00
|
|
|
import Xmobar
|
2021-11-24 00:43:58 -05:00
|
|
|
import Xmobar.Plugins.Common
|
2020-03-21 01:18:38 -04:00
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
btAlias :: String
|
|
|
|
btAlias = "bluetooth"
|
|
|
|
|
2022-06-26 19:05:25 -04:00
|
|
|
btDep :: DBusDependency_
|
2021-11-27 13:24:13 -05:00
|
|
|
btDep = Endpoint btBus btOMPath omInterface $ Method_ getManagedObjects
|
2021-11-26 23:35:03 -05:00
|
|
|
|
|
|
|
data Bluetooth = Bluetooth Icons Colors deriving (Read, Show)
|
2020-03-21 01:18:38 -04:00
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
instance Exec Bluetooth where
|
|
|
|
alias (Bluetooth _ _) = btAlias
|
|
|
|
start (Bluetooth icons colors) cb =
|
|
|
|
withDBusClientConnection True cb $ startAdapter icons colors cb
|
|
|
|
|
|
|
|
startAdapter :: Icons -> Colors -> Callback -> Client -> IO ()
|
|
|
|
startAdapter is cs cb cl = do
|
|
|
|
ot <- getBtObjectTree cl
|
|
|
|
state <- newMVar emptyState
|
|
|
|
let display = 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...
|
|
|
|
void $ addAdaptorListener state display adapter cl
|
2021-11-26 23:35:03 -05:00
|
|
|
-- set up devices on the adapter (and listeners for adding/removing devices)
|
|
|
|
let devices = findDevices adapter ot
|
|
|
|
addDeviceAddedListener state display adapter cl
|
|
|
|
addDeviceRemovedListener state display adapter cl
|
|
|
|
forM_ devices $ \d -> addAndInitDevice state display d cl
|
|
|
|
-- after setting things up, show the icon based on the initialized state
|
|
|
|
display
|
2021-06-21 23:41:57 -04:00
|
|
|
|
2021-11-26 23:35:03 -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
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
type IconFormatter = (Maybe Bool -> Bool -> String)
|
|
|
|
|
|
|
|
type Icons = (String, String)
|
|
|
|
|
|
|
|
displayIcon :: Callback -> IconFormatter -> MutableBtState -> IO ()
|
|
|
|
displayIcon callback formatter =
|
|
|
|
callback . 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
|
2021-11-26 23:35:03 -05:00
|
|
|
where
|
|
|
|
icon = if connected then iconConn else iconDisc
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | 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
|
|
|
|
{ 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
|
|
|
|
emptyState = BtState
|
|
|
|
{ btDevices = M.empty
|
|
|
|
, btPowered = Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
readState :: MutableBtState -> IO (Maybe Bool, Bool)
|
|
|
|
readState state = do
|
|
|
|
p <- readPowered state
|
|
|
|
c <- readDevices state
|
|
|
|
return (p, anyDevicesConnected c)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | 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
|
|
|
|
adaptorHasDevice adaptor device = case splitPath device of
|
|
|
|
[org, bluez, hciX, _] -> splitPath adaptor == [org, bluez, hciX]
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
splitPath :: ObjectPath -> [String]
|
|
|
|
splitPath = splitOn "/" . dropWhile (=='/') . formatObjectPath
|
|
|
|
|
|
|
|
getBtObjectTree :: Client -> IO ObjectTree
|
2021-11-27 13:24:13 -05:00
|
|
|
getBtObjectTree client = callGetManagedObjects client btBus btOMPath
|
2021-11-08 00:27:39 -05:00
|
|
|
|
2021-06-21 23:41:57 -04:00
|
|
|
btBus :: BusName
|
2021-11-24 00:21:18 -05:00
|
|
|
btBus = busName_ "org.bluez"
|
2021-06-21 23:41:57 -04:00
|
|
|
|
2021-11-27 13:24:13 -05:00
|
|
|
btOMPath :: ObjectPath
|
|
|
|
btOMPath = objectPath_ "/"
|
|
|
|
|
|
|
|
addBtOMListener :: SignalCallback -> Client -> IO ()
|
|
|
|
addBtOMListener sc = void . addInterfaceAddedListener btBus btOMPath sc
|
2021-06-21 23:41:57 -04:00
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
addDeviceAddedListener :: MutableBtState -> IO () -> ObjectPath -> Client -> IO ()
|
|
|
|
addDeviceAddedListener state display adapter client =
|
2021-11-27 13:24:13 -05:00
|
|
|
addBtOMListener addDevice client
|
2021-11-26 23:35:03 -05:00
|
|
|
where
|
|
|
|
addDevice = pathCallback adapter display $ \d ->
|
|
|
|
addAndInitDevice state display d client
|
2020-03-21 14:30:27 -04:00
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
addDeviceRemovedListener :: MutableBtState -> IO () -> ObjectPath -> Client -> IO ()
|
|
|
|
addDeviceRemovedListener state display adapter client =
|
2021-11-27 13:24:13 -05:00
|
|
|
addBtOMListener remDevice client
|
2021-11-26 23:35:03 -05:00
|
|
|
where
|
|
|
|
remDevice = pathCallback adapter display $ \d -> do
|
|
|
|
old <- removeDevice state d
|
|
|
|
forM_ old $ removeMatch client . btDevSigHandler
|
|
|
|
|
|
|
|
pathCallback :: ObjectPath -> IO () -> (ObjectPath -> IO ()) -> SignalCallback
|
|
|
|
pathCallback adapter display f [device, _] = forM_ (fromVariant device) $ \d ->
|
|
|
|
when (adaptorHasDevice adapter d) $ f d >> display
|
|
|
|
pathCallback _ _ _ _ = return ()
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Adapter
|
|
|
|
|
|
|
|
initAdapter :: MutableBtState -> ObjectPath -> Client -> IO ()
|
|
|
|
initAdapter state adapter client = do
|
|
|
|
reply <- callGetPowered adapter client
|
|
|
|
putPowered state $ fromSingletonVariant reply
|
|
|
|
|
2021-11-27 13:24:13 -05:00
|
|
|
matchBTProperty :: Client -> ObjectPath -> IO (Maybe MatchRule)
|
|
|
|
matchBTProperty client p = matchPropertyFull client btBus (Just p)
|
|
|
|
|
|
|
|
addAdaptorListener :: MutableBtState -> IO () -> ObjectPath -> Client
|
|
|
|
-> IO (Maybe SignalHandler)
|
|
|
|
addAdaptorListener state display adaptor client = do
|
|
|
|
rule <- matchBTProperty client adaptor
|
|
|
|
forM rule $ \r -> addMatchCallback r (procMatch . matchPowered) client
|
2021-11-26 23:35:03 -05:00
|
|
|
where
|
|
|
|
procMatch = withSignalMatch $ \b -> putPowered state b >> display
|
|
|
|
|
|
|
|
callGetPowered :: ObjectPath -> Client -> IO [Variant]
|
|
|
|
callGetPowered adapter =
|
2021-11-27 13:24:13 -05:00
|
|
|
callPropertyGet btBus adapter adapterInterface $ memberName_ adaptorPowered
|
2021-11-23 18:28:38 -05:00
|
|
|
|
2021-11-24 00:21:18 -05:00
|
|
|
matchPowered :: [Variant] -> SignalMatch Bool
|
2021-11-26 23:35:03 -05:00
|
|
|
matchPowered = matchPropertyChanged adapterInterface adaptorPowered
|
2021-11-24 00:21:18 -05:00
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
putPowered :: MutableBtState -> Maybe Bool -> IO ()
|
|
|
|
putPowered m ds = modifyMVar_ m (\s -> return s { btPowered = ds })
|
2021-11-24 00:21:18 -05:00
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
readPowered :: MutableBtState -> IO (Maybe Bool)
|
|
|
|
readPowered = fmap btPowered . readMVar
|
|
|
|
|
|
|
|
adapterInterface :: InterfaceName
|
|
|
|
adapterInterface = interfaceName_ "org.bluez.Adapter1"
|
|
|
|
|
|
|
|
adaptorPowered :: String
|
|
|
|
adaptorPowered = "Powered"
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Devices
|
|
|
|
|
|
|
|
addAndInitDevice :: MutableBtState -> IO () -> ObjectPath -> Client -> IO ()
|
|
|
|
addAndInitDevice state display device client = do
|
|
|
|
sh <- addDeviceListener state display 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
|
2021-11-26 23:35:03 -05:00
|
|
|
|
|
|
|
initDevice :: MutableBtState -> SignalHandler -> ObjectPath -> Client -> IO ()
|
|
|
|
initDevice state sh device client = do
|
|
|
|
reply <- callGetConnected device client
|
|
|
|
void $ insertDevice state device $
|
|
|
|
BTDevice { btDevConnected = fromVariant =<< listToMaybe reply
|
|
|
|
, btDevSigHandler = sh
|
|
|
|
}
|
|
|
|
|
2021-11-27 13:24:13 -05:00
|
|
|
addDeviceListener :: MutableBtState -> IO () -> ObjectPath -> Client
|
|
|
|
-> IO (Maybe SignalHandler)
|
|
|
|
addDeviceListener state display device client = do
|
|
|
|
rule <- matchBTProperty client device
|
|
|
|
forM rule $ \r -> addMatchCallback r (procMatch . matchConnected) client
|
2021-11-26 23:35:03 -05:00
|
|
|
where
|
|
|
|
procMatch = withSignalMatch $ \c -> updateDevice state device c >> display
|
|
|
|
|
|
|
|
matchConnected :: [Variant] -> SignalMatch Bool
|
|
|
|
matchConnected = matchPropertyChanged devInterface devConnected
|
|
|
|
|
|
|
|
callGetConnected :: ObjectPath -> Client -> IO [Variant]
|
2021-11-27 13:24:13 -05:00
|
|
|
callGetConnected p = callPropertyGet btBus p devInterface $ memberName_ devConnected
|
2021-11-26 23:35:03 -05:00
|
|
|
|
|
|
|
insertDevice :: MutableBtState -> ObjectPath -> BTDevice -> IO Bool
|
|
|
|
insertDevice m device dev = modifyMVar m $ \s -> do
|
|
|
|
let new = M.insert device dev $ btDevices s
|
|
|
|
return (s { btDevices = new }, anyDevicesConnected new)
|
|
|
|
|
|
|
|
updateDevice :: MutableBtState -> ObjectPath -> Maybe Bool -> IO Bool
|
|
|
|
updateDevice m device status = modifyMVar m $ \s -> do
|
|
|
|
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
|
|
|
|
|
|
|
|
removeDevice :: MutableBtState -> ObjectPath -> IO (Maybe BTDevice)
|
|
|
|
removeDevice m device = modifyMVar m $ \s -> do
|
|
|
|
let devs = btDevices s
|
|
|
|
return (s { btDevices = M.delete device devs }, M.lookup device devs)
|
|
|
|
|
|
|
|
readDevices :: MutableBtState -> IO ConnectedDevices
|
|
|
|
readDevices = fmap btDevices . readMVar
|
|
|
|
|
|
|
|
devInterface :: InterfaceName
|
|
|
|
devInterface = interfaceName_ "org.bluez.Device1"
|
2021-11-25 00:12:00 -05:00
|
|
|
|
2021-11-26 23:35:03 -05:00
|
|
|
devConnected :: String
|
|
|
|
devConnected = "Connected"
|