From 85f77e501951203eca4303bda5401b605de0d4c8 Mon Sep 17 00:00:00 2001 From: Allen <63997543+aaw3@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:59:59 -0600 Subject: [PATCH] Add example hs files --- example1.hs | 33 +++++++++++++++++++++++++++++++++ example2.hs | 33 +++++++++++++++++++++++++++++++++ main.go | 2 +- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 example1.hs create mode 100644 example2.hs diff --git a/example1.hs b/example1.hs new file mode 100644 index 0000000..4cac73a --- /dev/null +++ b/example1.hs @@ -0,0 +1,33 @@ +import XMonad +import XMonad.Hooks.DynamicLog + +import qualified DBus as D +import qualified DBus.Client as D +import qualified Codec.Binary.UTF8.String as UTF8 + +main :: IO () +main = do + dbus <- D.connectSession + -- Request access to the DBus name + D.requestName dbus (D.busName_ "org.xmonad.Log") + [D.nameAllowReplacement, D.nameReplaceExisting, D.nameDoNotQueue] + + -- Example of setting up logging for the first two screens + xmonad $ def { logHook = dynamicLogWithPP (myLogHook dbus 0) <+> dynamicLogWithPP (myLogHook dbus 1) } + +-- Override the PP values as you would otherwise, adding colors etc depending +-- on the statusbar used +myLogHook :: D.Client -> Int -> PP +myLogHook dbus id = marshallPP (S id) $ def { ppOutput = dbusOutput dbus id } + +-- Emit a DBus signal on log updates +dbusOutput :: D.Client -> Int -> String -> IO () +dbusOutput dbus id str = do + let signal = (D.signal objectPath interfaceName memberName) { + D.signalBody = [D.toVariant $ UTF8.decodeString str] + } + D.emit dbus signal + where + objectPath = D.objectPath_ ("/org/xmonad/Log_" ++ show id) + interfaceName = D.interfaceName_ "org.xmonad.Log" + memberName = D.memberName_ "Update" \ No newline at end of file diff --git a/example2.hs b/example2.hs new file mode 100644 index 0000000..9bfc727 --- /dev/null +++ b/example2.hs @@ -0,0 +1,33 @@ +import XMonad +import XMonad.Hooks.DynamicLog + +import qualified DBus as D +import qualified DBus.Client as D +import qualified Codec.Binary.UTF8.String as UTF8 + +main :: IO () +main = do + dbus <- D.connectSession + -- Request access to the DBus name + D.requestName dbus (D.busName_ "org.xmonad.Log") + [D.nameAllowReplacement, D.nameReplaceExisting, D.nameDoNotQueue] + + -- Example of setting up logging for the first two screens + xmonad $ def { logHook = dynamicLogWithPP (myLogHook dbus 0 "DP_2") <+> dynamicLogWithPP (myLogHook dbus 1 "DP_0") } + +-- Override the PP values as you would otherwise, adding colors etc depending +-- on the statusbar used +myLogHook :: D.Client -> Int -> String -> PP +myLogHook dbus id log_id = marshallPP (S id) $ def { ppOutput = dbusOutput dbus log_id } + +-- Emit a DBus signal on log updates +dbusOutput :: D.Client -> String -> String -> IO () +dbusOutput dbus log_id str = do + let signal = (D.signal objectPath interfaceName memberName) { + D.signalBody = [D.toVariant $ UTF8.decodeString str] + } + D.emit dbus signal + where + objectPath = D.objectPath_ ("/org/xmonad/Log_" ++ log_id) + interfaceName = D.interfaceName_ "org.xmonad.Log" + memberName = D.memberName_ "Update" \ No newline at end of file diff --git a/main.go b/main.go index 7e42f62..aa46006 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ func main() { os.Exit(1) } - args := fmt.Sprintf("type='signal',path='/org/xmonad/Log-%s',interface='org.xmonad.Log',member='Update'", os.Args[1]) + args := fmt.Sprintf("type='signal',path='/org/xmonad/Log_%s',interface='org.xmonad.Log',member='Update'", os.Args[1]) conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, args)