diff --git a/bin/xmobar.hs b/bin/xmobar.hs
index 4317c3d..10d3410 100644
--- a/bin/xmobar.hs
+++ b/bin/xmobar.hs
@@ -14,6 +14,7 @@ module Main (main) where
import Data.List
import Xmobar.Plugins.Bluetooth
+import Xmobar.Plugins.Device
import Xmobar.Plugins.IntelBacklight
import Xmobar.Plugins.Screensaver
import Xmobar.Plugins.VPN
@@ -38,6 +39,7 @@ myTemplate = formatTemplate left right
formatTemplate l r = fmtAliases l ++ aSep ++ fmtAliases r ++ " "
left = [ "UnsafeStdinReader" ]
right = [ "wlp0s20f3wi"
+ , "enp7s0f1"
, "vpn"
, "bluetooth"
, "alsa:default:Master"
@@ -133,6 +135,9 @@ config confDir = defaultConfig
, "--quality-icon-pattern", ""
] 5
+ , Run $ Device
+ ("enp7s0f1", "\xf0e8", T.fgColor, T.backdropFgColor) 5
+
, Run $ Locks
[ "-N", "\x1f13d"
, "-n", xmobarColor T.backdropFgColor "" "\x1f13d"
diff --git a/bin/xmonad.hs b/bin/xmonad.hs
index 3deb2cf..93eb489 100644
--- a/bin/xmonad.hs
+++ b/bin/xmonad.hs
@@ -444,6 +444,7 @@ mkKeys ts c =
-- M- reserved for showing the keymap
, ("M-", "restart xmonad", runCleanup ts >> runRestart)
, ("M-", "recompile xmonad", runRecompile)
+ , ("M-", "toggle ethernet", runToggleEthernet)
, ("M-", "toggle bluetooth", runToggleBluetooth)
, ("M-", "toggle screensaver", runToggleDPMS)
, ("M-", "switch gpu", runOptimusPrompt)
diff --git a/lib/XMonad/Internal/Command/Desktop.hs b/lib/XMonad/Internal/Command/Desktop.hs
index 5cc2ebc..933fb1c 100644
--- a/lib/XMonad/Internal/Command/Desktop.hs
+++ b/lib/XMonad/Internal/Command/Desktop.hs
@@ -21,6 +21,7 @@ module XMonad.Internal.Command.Desktop
, runMinBacklight
, runMaxBacklight
, runToggleDPMS
+ , runToggleEthernet
, runRestart
, runRecompile
, runAreaCapture
@@ -122,6 +123,17 @@ runMaxBacklight = io $ void callMaxBrightness
runToggleDPMS :: X ()
runToggleDPMS = io $ void callToggle
+ethernetIface :: String
+ethernetIface = "enp7s0f1"
+
+runToggleEthernet :: X ()
+runToggleEthernet = spawn
+ $ "nmcli -g GENERAL.STATE device show " ++ ethernetIface ++ " | grep -q disconnected"
+ #!&& "a=connect"
+ #!|| "a=disconnect"
+ #!>> fmtCmd "nmcli" ["device", "$a", ethernetIface]
+ #!&& fmtNotifyCmd defNoteInfo { body = Just $ Text "ethernet \"$a\"ed" }
+
--------------------------------------------------------------------------------
-- | Configuration commands
diff --git a/lib/Xmobar/Plugins/Device.hs b/lib/Xmobar/Plugins/Device.hs
new file mode 100644
index 0000000..c1b8c60
--- /dev/null
+++ b/lib/Xmobar/Plugins/Device.hs
@@ -0,0 +1,60 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Xmobar.Plugins.Device where
+
+-- TOOD this name can be more general
+--------------------------------------------------------------------------------
+-- | Ethernet plugin
+--
+-- Display different text depending on whether or not the interface has
+-- connectivity
+
+
+import Control.Monad
+
+import Data.Word
+
+import DBus
+import DBus.Client
+
+import Xmobar
+import XMonad.Hooks.DynamicLog (xmobarColor)
+
+data Device = Device (String, String, String, String) Int
+ deriving (Read, Show)
+
+busName :: BusName
+busName = "org.freedesktop.NetworkManager"
+
+getDevice :: Client -> String -> IO (Maybe ObjectPath)
+getDevice client iface = do
+ let mc = methodCall "/org/freedesktop/NetworkManager"
+ "org.freedesktop.NetworkManager" "GetDeviceByIpIface"
+ reply <- call client $ mc { methodCallBody = [toVariant iface]
+ , methodCallDestination = Just busName
+ }
+ return $ case reply of
+ Left _ -> Nothing
+ Right b -> case methodReturnBody b of
+ [objectPath] -> fromVariant objectPath
+ _ -> Nothing
+
+getDeviceConnected :: Client -> ObjectPath -> IO (Maybe Bool)
+getDeviceConnected client objectPath = do
+ let mc = methodCall objectPath
+ "org.freedesktop.NetworkManager.Device"
+ "Ip4Connectivity"
+ either (const Nothing) (fmap ((> 1) :: Word32 -> Bool) . fromVariant)
+ <$> getProperty client mc { methodCallDestination = Just busName }
+
+instance Exec Device where
+ alias (Device (iface, _, _, _) _) = iface
+ rate (Device _ r) = r
+ run (Device (iface, text, colorOn, colorOff) _) = do
+ client <- connectSystem
+ dev <- getDevice client iface
+ state <- join <$> mapM (getDeviceConnected client) dev
+ disconnect client
+ return $ maybe "N/A" fmt state
+ where
+ fmt s = xmobarColor (if s then colorOn else colorOff) "" text
diff --git a/my-xmonad.cabal b/my-xmonad.cabal
index aeb2f45..9da1f96 100644
--- a/my-xmonad.cabal
+++ b/my-xmonad.cabal
@@ -20,6 +20,7 @@ library
, XMonad.Internal.DBus.Screensaver
, XMonad.Internal.Process
, Xmobar.Plugins.Bluetooth
+ , Xmobar.Plugins.Device
, Xmobar.Plugins.IntelBacklight
, Xmobar.Plugins.Screensaver
, Xmobar.Plugins.VPN
diff --git a/stack.yaml b/stack.yaml
index 7e9d002..0938a7b 100644
--- a/stack.yaml
+++ b/stack.yaml
@@ -43,7 +43,7 @@ extra-deps:
- iwlib-0.1.0
- fdo-notify-0.3.1
- github: ndwarshuis/xmobar
- commit: 4d750adcdecf5c1085ff583cf69392fcaf5dfaf7
+ commit: 4186bcbc50a7c78ac0aee8bc3719e0dd9a46dace
# Override default flag values for local packages and extra-deps
# flags: {}