rofi-extras/app/rofi-bt.hs

178 lines
5.5 KiB
Haskell
Raw Normal View History

2021-11-29 00:44:37 -05:00
--------------------------------------------------------------------------------
2023-02-13 22:19:49 -05:00
-- rofi-bt - a prompt to dicsonnect/connect devices
2021-11-29 00:44:37 -05:00
--
module Main (main) where
2023-02-13 22:19:49 -05:00
import DBus
import DBus.Client
import qualified Data.Map as M
import Data.Maybe
import qualified Data.Text.IO as TI
import RIO
import qualified RIO.List as L
import qualified RIO.Text as T
import Rofi.Command
import System.Environment
2021-11-29 00:55:33 -05:00
2021-11-29 00:44:37 -05:00
main :: IO ()
2021-11-29 00:55:33 -05:00
main = getArgs >>= runPrompt
2021-11-29 00:44:37 -05:00
2023-02-13 23:31:50 -05:00
data RofiBTConf = RofiBTConf [T.Text] ObjectPath
2021-11-29 00:44:37 -05:00
2023-02-14 22:28:26 -05:00
instance HasRofiConf RofiBTConf where
2021-11-29 00:55:33 -05:00
defArgs (RofiBTConf as _) = as
2021-11-29 00:44:37 -05:00
type BTAction = RofiAction RofiBTConf
2021-11-29 00:55:33 -05:00
runPrompt :: [String] -> IO ()
runPrompt args = do
2021-11-29 00:44:37 -05:00
c <- getClient
2023-02-13 22:19:49 -05:00
maybe (TI.putStrLn "could not get DBus client") run c
2021-11-29 00:44:37 -05:00
where
run client = do
paths <- M.keys <$> getObjectTree client
2023-02-13 22:19:49 -05:00
maybe (TI.putStrLn "could not get DBus adapter") (actions client paths) $
getAdapter paths
2021-11-29 00:44:37 -05:00
actions client paths adapter = do
ras <- getRofiActions client paths
2023-02-14 22:28:26 -05:00
runRofi (RofiBTConf (fmap T.pack args) adapter) $
emptyMenu
{ groups = [untitledGroup $ toRofiActions ras]
, prompt = Just "Select Device"
}
2021-11-29 00:44:37 -05:00
getRofiActions :: Client -> [ObjectPath] -> IO [BTAction]
getRofiActions client os = do
devs <- getDevices client os
catMaybes <$> mapM (deviceToRofiAction client) devs
deviceToRofiAction :: Client -> ObjectPath -> IO (Maybe BTAction)
deviceToRofiAction client dev = do
c <- getDeviceConnected client dev
n <- getDeviceName client dev
return $ case (c, n) of
2023-02-13 22:19:49 -05:00
(Just c', Just n') ->
Just
( formatDeviceEntry c' n'
, powerAdapterMaybe client >> io (mkAction c')
)
_ -> Nothing
2021-11-29 00:44:37 -05:00
where
2023-02-13 22:19:49 -05:00
mkAction True = callDeviceDisconnect client dev
2021-11-29 00:44:37 -05:00
mkAction False = callDeviceConnect client dev
2023-02-14 22:28:26 -05:00
powerAdapterMaybe :: Client -> RIO RofiBTConf ()
2021-11-29 00:44:37 -05:00
powerAdapterMaybe client = do
2021-11-29 00:55:33 -05:00
(RofiBTConf _ adapter) <- ask
2021-11-29 00:44:37 -05:00
let mc = btMethodCall adapter i m
let powerOnMaybe = flip unless $ void $ setProperty client mc value
powered <- io $ getBTProperty client adapter i m
2023-02-13 22:19:49 -05:00
io $ maybe (TI.putStrLn "could not get adapter powered status") powerOnMaybe powered
2021-11-29 00:44:37 -05:00
where
i = interfaceName_ "org.bluez.Adapter1"
m = memberName_ "Powered"
-- apparently this needs to be double-variant'd to match the signature of
-- the 'Set' method
value = toVariant $ toVariant True
2023-02-13 23:31:50 -05:00
formatDeviceEntry :: Bool -> T.Text -> T.Text
formatDeviceEntry connected name = T.unwords [prefix connected, name]
2021-11-29 00:44:37 -05:00
where
2023-02-13 22:19:49 -05:00
prefix True = "#"
2021-11-29 00:44:37 -05:00
prefix False = " "
getAdapter :: [ObjectPath] -> Maybe ObjectPath
2023-02-13 22:19:49 -05:00
getAdapter = L.find pathIsAdaptor
2021-11-29 00:44:37 -05:00
getDevices :: Client -> [ObjectPath] -> IO [ObjectPath]
getDevices client = filterM (getDevicePaired client) . filter pathIsDevice
2023-02-13 23:31:50 -05:00
type ObjectTree = M.Map ObjectPath (M.Map T.Text (M.Map T.Text Variant))
2021-11-29 00:44:37 -05:00
getObjectTree :: Client -> IO ObjectTree
getObjectTree client =
fromMaybe M.empty . eitherMaybe from <$> callBTMethod client o i m
where
o = objectPath_ "/"
i = interfaceName_ "org.freedesktop.DBus.ObjectManager"
m = memberName_ "GetManagedObjects"
from = fromVariant <=< listToMaybe . methodReturnBody
getDeviceConnected :: Client -> ObjectPath -> IO (Maybe Bool)
getDeviceConnected = getDevProperty "Connected"
2023-02-13 23:31:50 -05:00
getDeviceName :: Client -> ObjectPath -> IO (Maybe T.Text)
2021-11-29 00:44:37 -05:00
getDeviceName = getDevProperty "Name"
getDevicePaired :: Client -> ObjectPath -> IO Bool
getDevicePaired c = fmap (fromMaybe False) . getDevProperty "Paired" c
callDeviceConnect :: Client -> ObjectPath -> IO ()
callDeviceConnect = callDevMethod "Connect"
callDeviceDisconnect :: Client -> ObjectPath -> IO ()
callDeviceDisconnect = callDevMethod "Disconnect"
pathIsAdaptor :: ObjectPath -> Bool
pathIsAdaptor o = case splitPath o of
[a, b, c] -> pathIsAdaptorPrefix a b c
2023-02-13 22:19:49 -05:00
_ -> False
2021-11-29 00:44:37 -05:00
pathIsDevice :: ObjectPath -> Bool
pathIsDevice o = case splitPath o of
[a, b, c, _] -> pathIsAdaptorPrefix a b c
2023-02-13 22:19:49 -05:00
_ -> False
2021-11-29 00:44:37 -05:00
2023-02-13 23:31:50 -05:00
pathIsAdaptorPrefix :: T.Text -> T.Text -> T.Text -> Bool
pathIsAdaptorPrefix a b c = a == "org" && b == "bluez" && "hci" `T.isPrefixOf` c
2021-11-29 00:44:37 -05:00
2023-02-13 23:31:50 -05:00
splitPath :: ObjectPath -> [T.Text]
splitPath = T.split (== '/') . T.dropWhile (== '/') . T.pack . formatObjectPath
2021-11-29 00:44:37 -05:00
getClient :: IO (Maybe Client)
getClient = either warn (return . Just) =<< try connectSystem
where
2023-02-13 22:19:49 -05:00
warn e = TI.putStrLn (T.pack $ clientErrorMessage e) >> return Nothing
2021-11-29 00:44:37 -05:00
2023-02-13 23:31:50 -05:00
callDevMethod :: T.Text -> Client -> ObjectPath -> IO ()
2021-11-29 00:44:37 -05:00
callDevMethod mem client dev =
2023-02-13 23:31:50 -05:00
void $ callBTMethod client dev btDevInterface $ memberName_ $ T.unpack mem
2021-11-29 00:44:37 -05:00
2023-02-13 23:31:50 -05:00
getDevProperty :: IsVariant a => T.Text -> Client -> ObjectPath -> IO (Maybe a)
2021-11-29 00:44:37 -05:00
getDevProperty mem client dev =
2023-02-13 23:31:50 -05:00
getBTProperty client dev btDevInterface $ memberName_ $ T.unpack mem
2021-11-29 00:44:37 -05:00
2023-02-13 22:19:49 -05:00
callBTMethod
:: Client
-> ObjectPath
-> InterfaceName
-> MemberName
-> IO (Either MethodError MethodReturn)
2021-11-29 00:44:37 -05:00
callBTMethod client o i m = call client (btMethodCall o i m)
2023-02-13 22:19:49 -05:00
-- eitherMaybe (fromVariant <=< listToMaybe . methodReturnBody)
-- <$> call client (btMethodCall o i m)
getBTProperty
:: IsVariant a
=> Client
-> ObjectPath
-> InterfaceName
-> MemberName
-> IO (Maybe a)
2021-11-29 00:44:37 -05:00
getBTProperty client o i m =
eitherMaybe fromVariant <$> getProperty client (btMethodCall o i m)
btMethodCall :: ObjectPath -> InterfaceName -> MemberName -> MethodCall
2023-02-13 22:19:49 -05:00
btMethodCall o i m = (methodCall o i m) {methodCallDestination = Just btBus}
2021-11-29 00:44:37 -05:00
eitherMaybe :: (b -> Maybe c) -> Either a b -> Maybe c
eitherMaybe = either (const Nothing)
btBus :: BusName
btBus = busName_ "org.bluez"
btDevInterface :: InterfaceName
btDevInterface = interfaceName_ "org.bluez.Device1"