2021-06-21 23:41:57 -04:00
|
|
|
module Xmobar.Plugins.Device
|
|
|
|
( Device(..)
|
2021-11-25 00:12:00 -05:00
|
|
|
, devDep
|
2021-06-21 23:41:57 -04:00
|
|
|
) where
|
2020-05-28 23:17:17 -04:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2021-11-25 00:12:00 -05:00
|
|
|
-- | Devince plugin
|
2020-05-28 23:17:17 -04:00
|
|
|
--
|
|
|
|
-- Display different text depending on whether or not the interface has
|
|
|
|
-- connectivity
|
|
|
|
|
|
|
|
import Control.Monad
|
|
|
|
|
2021-11-25 00:12:00 -05:00
|
|
|
import Data.Maybe
|
2020-05-28 23:17:17 -04:00
|
|
|
import Data.Word
|
|
|
|
|
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
2021-11-27 01:02:22 -05:00
|
|
|
import DBus.Internal
|
2020-05-28 23:17:17 -04:00
|
|
|
|
2021-11-25 00:12:00 -05:00
|
|
|
import XMonad.Internal.Dependency
|
2021-06-19 00:54:01 -04:00
|
|
|
import Xmobar
|
2021-11-25 00:12:00 -05:00
|
|
|
import Xmobar.Plugins.Common
|
|
|
|
|
|
|
|
newtype Device = Device (String, String, String, String) deriving (Read, Show)
|
|
|
|
|
|
|
|
nmBus :: BusName
|
|
|
|
nmBus = busName_ "org.freedesktop.NetworkManager"
|
2020-05-28 23:17:17 -04:00
|
|
|
|
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"
|
|
|
|
|
|
|
|
devDep :: DBusDep
|
|
|
|
devDep = Endpoint nmBus nmPath nmInterface $ Method_ getByIP
|
2021-11-09 00:59:17 -05:00
|
|
|
|
2020-05-28 23:17:17 -04:00
|
|
|
getDevice :: Client -> String -> IO (Maybe ObjectPath)
|
2021-11-25 00:12:00 -05:00
|
|
|
getDevice client iface = either (const Nothing) (fromVariant <=< listToMaybe)
|
|
|
|
<$> callMethod' client mc
|
|
|
|
where
|
|
|
|
mc = (methodCall nmPath nmInterface getByIP)
|
|
|
|
{ methodCallBody = [toVariant iface]
|
|
|
|
, methodCallDestination = Just nmBus
|
|
|
|
}
|
|
|
|
|
|
|
|
getDeviceConnected :: ObjectPath -> Client -> IO [Variant]
|
|
|
|
getDeviceConnected path = callPropertyGet nmBus path nmDeviceInterface devSignal
|
|
|
|
|
|
|
|
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-25 00:12:00 -05:00
|
|
|
alias (Device (iface, _, _, _)) = iface
|
|
|
|
start (Device (iface, text, colorOn, colorOff)) cb = do
|
2021-11-26 23:35:03 -05:00
|
|
|
withDBusClientConnection True cb $ \c -> do
|
2021-11-25 00:12:00 -05:00
|
|
|
path <- getDevice c iface
|
|
|
|
maybe (cb na) (listener c) path
|
2020-05-28 23:17:17 -04:00
|
|
|
where
|
2021-11-25 00:12:00 -05:00
|
|
|
listener client path = startListener (matchProperty path)
|
|
|
|
(getDeviceConnected path) matchStatus chooseColor' cb client
|
2021-11-26 23:35:03 -05:00
|
|
|
chooseColor' = return . chooseColor text colorOn colorOff . (> 1)
|