From 862b2fc8912f31bf3cdd37f86038b02ab83eb822 Mon Sep 17 00:00:00 2001 From: Marcus Carlsson Date: Thu, 29 Dec 2016 15:59:53 +0100 Subject: [PATCH] Initial commit --- LICENSE | 21 +++++++++++++++++ README.md | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ glide.lock | 6 +++++ glide.yaml | 4 ++++ main.go | 32 ++++++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 glide.lock create mode 100644 glide.yaml create mode 100644 main.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..be45764 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Marcus Carlsson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..145f228 --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +# xmonad-log + +xmonad-log is a DBus monitoring solution that can easily be used to display +xmonad in a statusbar like [polybar](https://github.com/jaagr/polybar), +[lemonbar](https://github.com/LemonBoy/bar) and similar. + +## Installation + +xmonad-log is written in Go with one dependency: +[dbus](https://github.com/godbus/dbus). [Binary +packages](https://github.com/xintron/xmonad-log/releases) are available. + +### Building from source + +This package has been tested with Go 1.7 and above. + +To build from source: + 1. Clone this repository into `$GOPATH/src/github.com/xintron/xmonad-log`. + 2. Build it within the directory with `go build`. + +This should leave a `xmonad-log` binary in the directory. Move this to an +appropriate directory in your `$PATH`. + +## Configure xmonad + +To configure xmonad to send log events over DBus the haskell +[dbus](http://hackage.haskell.org/package/dbus) package is required. Once +installed the following can be added to your `.xmonad/xmonad.hs` configuration +to add DBus support. + +```haskell +import XMonad +import XMonad.Hooks.DynamicLog + +import qualified DBus as D +import qualified DBus.Client as D + +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] + + xmonad $ def { logHook = dynamicLogWithPP (myLogHook dbus) } + +-- Override the PP values as you would otherwise, adding colors etc depending +-- on the statusbar used +myLogHook :: D.Client -> PP +myLogHook dbus = def { ppOutput = dbusOutput dbus } + +-- Emit a DBus signal on log updates +dbusOutput :: D.Client -> String -> IO () +dbusOutput dbus 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" + interfaceName = D.interfaceName_ "org.xmonad.Log" + memberName = D.memberName_ "Update" +``` + +View [this +xmonad-config](https://github.com/xintron/configs/blob/22a33b41587c180172392f80318883921c543053/.xmonad/lib/Config.hs#L199) +for a fully working polybar example using statusbar coloring. diff --git a/glide.lock b/glide.lock new file mode 100644 index 0000000..5981b90 --- /dev/null +++ b/glide.lock @@ -0,0 +1,6 @@ +hash: 48b3e28efd90a1084dce6680bc7084d562049fddbf497c5e48d0c78e2c1e4896 +updated: 2016-12-29T15:49:27.387756858+01:00 +imports: +- name: github.com/godbus/dbus + version: 5f6efc7ef2759c81b7ba876593971bfce311eab3 +testImports: [] diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 0000000..625914e --- /dev/null +++ b/glide.yaml @@ -0,0 +1,4 @@ +package: github.com/xintron/xmonad-log +import: +- package: github.com/godbus/dbus + version: ^4.0.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..ec5b826 --- /dev/null +++ b/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "github.com/godbus/dbus" + "os" +) + +func main() { + conn, err := dbus.SessionBus() + if err != nil { + fmt.Fprintln(os.Stderr, "Failed to connect to session bus:", err) + os.Exit(1) + } + + conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, + "type='signal',path='/org/xmonad/Log',interface='org.xmonad.Log',member='Update'") + + c := make(chan *dbus.Signal, 10) + conn.Signal(c) + for s := range c { + if len(s.Body) == 0 { + continue + } + // Convert the result to a slice of strings + res, ok := s.Body[0].(string) + if !ok { + continue + } + fmt.Println(res) + } +}