2020-05-28 23:17:17 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2022-07-09 17:08:10 -04:00
|
|
|
-- | Device plugin
|
2020-05-28 23:17:17 -04:00
|
|
|
--
|
|
|
|
-- Display different text depending on whether or not the interface has
|
|
|
|
-- connectivity
|
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
module Xmobar.Plugins.Device
|
|
|
|
( Device(..)
|
|
|
|
, devDep
|
|
|
|
) where
|
|
|
|
|
2020-05-28 23:17:17 -04:00
|
|
|
import Control.Monad
|
|
|
|
|
2022-07-09 17:44:14 -04:00
|
|
|
import Data.Internal.DBus
|
|
|
|
import Data.Internal.Dependency
|
2020-05-28 23:17:17 -04:00
|
|
|
import Data.Word
|
|
|
|
|
|
|
|
import DBus
|
|
|
|
|
2022-07-09 01:02:37 -04:00
|
|
|
import XMonad.Internal.Command.Desktop
|
2022-07-09 17:08:10 -04:00
|
|
|
import XMonad.Internal.DBus.Common
|
2021-06-19 00:54:01 -04:00
|
|
|
import Xmobar
|
2021-11-25 00:12:00 -05:00
|
|
|
import Xmobar.Plugins.Common
|
|
|
|
|
2021-11-27 17:33:02 -05:00
|
|
|
newtype Device = Device (String, String, Colors) deriving (Read, Show)
|
2021-11-25 00:12:00 -05:00
|
|
|
|
|
|
|
nmPath :: ObjectPath
|
|
|
|
nmPath = objectPath_ "/org/freedesktop/NetworkManager"
|
2020-05-28 23:17:17 -04:00
|
|
|
|
2021-11-25 00:12:00 -05:00
|
|
|
nmInterface :: InterfaceName
|
|
|
|
nmInterface = interfaceName_ "org.freedesktop.NetworkManager"
|
2021-06-21 23:41:57 -04:00
|
|
|
|
2021-11-25 00:12:00 -05:00
|
|
|
nmDeviceInterface :: InterfaceName
|
|
|
|
nmDeviceInterface = interfaceName_ "org.freedesktop.NetworkManager.Device"
|
2020-05-28 23:17:17 -04:00
|
|
|
|
2021-11-25 00:12:00 -05:00
|
|
|
getByIP :: MemberName
|
|
|
|
getByIP = memberName_ "GetDeviceByIpIface"
|
2021-11-09 00:59:17 -05:00
|
|
|
|
2021-11-25 00:12:00 -05:00
|
|
|
devSignal :: String
|
|
|
|
devSignal = "Ip4Connectivity"
|
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
devDep :: DBusDependency_ SysClient
|
|
|
|
devDep = Endpoint networkManagerPkgs networkManagerBus nmPath nmInterface
|
|
|
|
$ Method_ getByIP
|
2021-11-09 00:59:17 -05:00
|
|
|
|
2022-07-09 17:08:10 -04:00
|
|
|
getDevice :: SysClient -> String -> IO (Maybe ObjectPath)
|
2022-07-09 17:44:14 -04:00
|
|
|
getDevice sys iface = bodyToMaybe <$> callMethod' sys mc
|
2021-11-25 00:12:00 -05:00
|
|
|
where
|
2022-07-09 17:08:10 -04:00
|
|
|
mc = (methodCallBus networkManagerBus nmPath nmInterface getByIP)
|
2021-11-25 00:12:00 -05:00
|
|
|
{ methodCallBody = [toVariant iface]
|
|
|
|
}
|
|
|
|
|
2022-07-09 17:44:14 -04:00
|
|
|
getDeviceConnected :: ObjectPath -> SysClient -> IO [Variant]
|
2022-07-09 17:08:10 -04:00
|
|
|
getDeviceConnected path = callPropertyGet networkManagerBus path nmDeviceInterface
|
2021-11-27 13:24:13 -05:00
|
|
|
$ memberName_ devSignal
|
2021-11-25 00:12:00 -05:00
|
|
|
|
|
|
|
matchStatus :: [Variant] -> SignalMatch Word32
|
2021-11-26 23:35:03 -05:00
|
|
|
matchStatus = matchPropertyChanged nmDeviceInterface devSignal
|
2020-05-28 23:17:17 -04:00
|
|
|
|
|
|
|
instance Exec Device where
|
2021-11-27 17:33:02 -05:00
|
|
|
alias (Device (iface, _, _)) = iface
|
|
|
|
start (Device (iface, text, colors)) cb = do
|
2022-07-09 17:44:14 -04:00
|
|
|
withDBusClientConnection cb $ \sys -> do
|
|
|
|
path <- getDevice sys iface
|
|
|
|
displayMaybe' cb (listener sys) path
|
2020-05-28 23:17:17 -04:00
|
|
|
where
|
2022-07-09 17:44:14 -04:00
|
|
|
listener sys path = do
|
|
|
|
rule <- matchPropertyFull sys networkManagerBus (Just path)
|
2021-11-27 13:24:13 -05:00
|
|
|
-- TODO warn the user here rather than silently drop the listener
|
|
|
|
forM_ rule $ \r ->
|
2022-07-09 17:44:14 -04:00
|
|
|
startListener r (getDeviceConnected path) matchStatus chooseColor' cb sys
|
2021-11-27 17:33:02 -05:00
|
|
|
chooseColor' = return . (\s -> colorText colors s text) . (> 1)
|