diff options
author | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-05-07 04:09:19 -0400 |
---|---|---|
committer | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-05-07 04:09:19 -0400 |
commit | dc48249e1acea54b391f53b95f16e515dead7c97 (patch) | |
tree | 3467fee8294f8800977de0df3030d82e69bb7528 /nexgb/examples/get-active-window/main.go | |
parent | 3bf376bd6648e011de9131c1d90a39c6d3890d65 (diff) | |
download | haven-dc48249e1acea54b391f53b95f16e515dead7c97.tar.gz haven-dc48249e1acea54b391f53b95f16e515dead7c97.tar.xz haven-dc48249e1acea54b391f53b95f16e515dead7c97.zip |
lots of docs and examples
Diffstat (limited to 'nexgb/examples/get-active-window/main.go')
-rw-r--r-- | nexgb/examples/get-active-window/main.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/nexgb/examples/get-active-window/main.go b/nexgb/examples/get-active-window/main.go new file mode 100644 index 0000000..e90563c --- /dev/null +++ b/nexgb/examples/get-active-window/main.go @@ -0,0 +1,57 @@ +// Example get-active-window reads the _NET_ACTIVE_WINDOW property of the root +// window and uses the result (a window id) to get the name of the window. +package main + +import ( + "fmt" + "log" + + "github.com/BurntSushi/xgb" +) + +func main() { + X, err := xgb.NewConn() + if err != nil { + log.Fatal(err) + } + + // Get the window id of the root window. + root := X.DefaultScreen().Root + + // Get the atom id (i.e., intern an atom) of "_NET_ACTIVE_WINDOW". + aname := "_NET_ACTIVE_WINDOW" + activeAtom, err := X.InternAtom(true, uint16(len(aname)), aname).Reply() + if err != nil { + log.Fatal(err) + } + + // Get the atom id (i.e., intern an atom) of "_NET_WM_NAME". + aname = "_NET_WM_NAME" + nameAtom, err := X.InternAtom(true, uint16(len(aname)), aname).Reply() + if err != nil { + log.Fatal(err) + } + + // Get the actual value of _NET_ACTIVE_WINDOW. + // Note that 'reply.Value' is just a slice of bytes, so we use an + // XGB helper function, 'Get32', to pull an unsigned 32-bit integer out + // of the byte slice. We then convert it to an X resource id so it can + // be used to get the name of the window in the next GetProperty request. + reply, err := X.GetProperty(false, root, activeAtom.Atom, + xgb.GetPropertyTypeAny, 0, (1<<32)-1).Reply() + if err != nil { + log.Fatal(err) + } + windowId := xgb.Id(xgb.Get32(reply.Value)) + fmt.Printf("Active window id: %X\n", windowId) + + // Now get the value of _NET_WM_NAME for the active window. + // Note that this time, we simply convert the resulting byte slice, + // reply.Value, to a string. + reply, err = X.GetProperty(false, windowId, nameAtom.Atom, + xgb.GetPropertyTypeAny, 0, (1<<32)-1).Reply() + if err != nil { + log.Fatal(err) + } + fmt.Printf("Active window name: %s\n", string(reply.Value)) +} |