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

73 lines
2.2 KiB
Haskell
Raw Normal View History

module Xmobar.Plugins.Device
( Device(..)
2021-11-25 00:12:00 -05:00
, devDep
) 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-25 00:12:00 -05:00
import XMonad.Internal.DBus.Common
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-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-25 00:12:00 -05:00
devSignal :: String
devSignal = "Ip4Connectivity"
devDep :: DBusDep
devDep = Endpoint nmBus nmPath nmInterface $ Method_ getByIP
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
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
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
chooseColor' = return . chooseColor text colorOn colorOff . (> 1)