diff --git a/bin/xmobar.hs b/bin/xmobar.hs index 8790fce..6acbf57 100644 --- a/bin/xmobar.hs +++ b/bin/xmobar.hs @@ -1,5 +1,6 @@ import Xmobar.Plugins.Bluetooth import Xmobar.Plugins.IntelBacklight +import Xmobar.Plugins.NetworkManager import Xmobar.Plugins.Screensaver import qualified Theme as T @@ -29,6 +30,7 @@ myTemplate = formatTemplate left right , "%intelbacklight%" , "%bluetooth%" , "%screensaver%" + , "%networkmanager%" , "%locks%" , "%date%" ] @@ -126,6 +128,8 @@ config confDir = defaultConfig , Run $ Bluetooth ("\xf293", T.fgColor, T.backdropFgColor) , Run UnsafeStdinReader + + , Run $ NetworkManager ("VPN", T.fgColor, T.backdropFgColor) ] } diff --git a/lib/Xmobar/Plugins/NetworkManager.hs b/lib/Xmobar/Plugins/NetworkManager.hs new file mode 100644 index 0000000..19deb7c --- /dev/null +++ b/lib/Xmobar/Plugins/NetworkManager.hs @@ -0,0 +1,43 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE OverloadedStrings #-} + +module Xmobar.Plugins.NetworkManager where + +import Control.Concurrent +import Control.Monad + +import DBus +import DBus.Client +import DBus.Internal.Types + +import Xmobar + +newtype NetworkManager = NetworkManager (String, String, String) + deriving (Read, Show) + +rule :: MatchRule +rule = matchAny + { matchInterface = Just "org.freedesktop.NetworkManager.VPN.Connection" + , matchMember = Just "VpnStateChanged" + } + +-- TODO would polling be better for this? Using events means that we need +-- to catch all of them perfectly to stay synchronized...which *might* happen + +instance Exec NetworkManager where + alias (NetworkManager _) = "networkmanager" + start (NetworkManager (text, colorOn, colorOff)) cb = do + -- start (NetworkManager _) cb = do + client <- connectSystem + -- TODO initialize + _ <- addMatch client rule $ cb . fmtState . getVPNState . signalBody + forever (threadDelay 5000000) + where + getVPNState = \case + [Variant (ValueAtom (AtomWord32 s)), _] -> Just s + _ -> Nothing + fmtState = \case + -- state = 5 means VPN is connected + Just s -> wrapColor text $ if s == 5 then colorOn else colorOff + Nothing -> "N/A" + wrapColor s c = "" ++ s ++ "" diff --git a/my-xmonad.cabal b/my-xmonad.cabal index 8c26e21..7481919 100644 --- a/my-xmonad.cabal +++ b/my-xmonad.cabal @@ -16,6 +16,7 @@ library , DBus.Screensaver , Xmobar.Plugins.Bluetooth , Xmobar.Plugins.IntelBacklight + , Xmobar.Plugins.NetworkManager , Xmobar.Plugins.Screensaver build-depends: base , X11 >= 1.9.1