ADD ethernet indicator and toggle

This commit is contained in:
Nathan Dwarshuis 2020-05-28 23:17:17 -04:00
parent 7b52b34edb
commit c65f14902b
6 changed files with 80 additions and 1 deletions

View File

@ -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", "<icon=wifi_%%.xpm/>"
] 5
, Run $ Device
("enp7s0f1", "<fn=2>\xf0e8</fn>", T.fgColor, T.backdropFgColor) 5
, Run $ Locks
[ "-N", "<fn=3>\x1f13d</fn>"
, "-n", xmobarColor T.backdropFgColor "" "<fn=3>\x1f13d</fn>"

View File

@ -444,6 +444,7 @@ mkKeys ts c =
-- M-<F1> reserved for showing the keymap
, ("M-<F2>", "restart xmonad", runCleanup ts >> runRestart)
, ("M-<F3>", "recompile xmonad", runRecompile)
, ("M-<F9>", "toggle ethernet", runToggleEthernet)
, ("M-<F10>", "toggle bluetooth", runToggleBluetooth)
, ("M-<F11>", "toggle screensaver", runToggleDPMS)
, ("M-<F12>", "switch gpu", runOptimusPrompt)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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: {}