2012-05-07 10:09:19 +02:00
|
|
|
// 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"
|
2012-05-10 23:01:42 +02:00
|
|
|
"github.com/BurntSushi/xgb/xproto"
|
2012-05-07 10:09:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
X, err := xgb.NewConn()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the window id of the root window.
|
2012-05-10 23:01:42 +02:00
|
|
|
setup := xproto.Setup(X)
|
|
|
|
root := setup.DefaultScreen(X).Root
|
2012-05-07 10:09:19 +02:00
|
|
|
|
|
|
|
// Get the atom id (i.e., intern an atom) of "_NET_ACTIVE_WINDOW".
|
|
|
|
aname := "_NET_ACTIVE_WINDOW"
|
2012-05-10 23:01:42 +02:00
|
|
|
activeAtom, err := xproto.InternAtom(X, true, uint16(len(aname)),
|
|
|
|
aname).Reply()
|
2012-05-07 10:09:19 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the atom id (i.e., intern an atom) of "_NET_WM_NAME".
|
|
|
|
aname = "_NET_WM_NAME"
|
2012-05-10 23:01:42 +02:00
|
|
|
nameAtom, err := xproto.InternAtom(X, true, uint16(len(aname)),
|
|
|
|
aname).Reply()
|
2012-05-07 10:09:19 +02:00
|
|
|
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.
|
2012-05-10 23:01:42 +02:00
|
|
|
reply, err := xproto.GetProperty(X, false, root, activeAtom.Atom,
|
|
|
|
xproto.GetPropertyTypeAny, 0, (1<<32)-1).Reply()
|
2012-05-07 10:09:19 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2012-05-10 23:01:42 +02:00
|
|
|
windowId := xproto.Window(xgb.Get32(reply.Value))
|
2012-05-07 10:09:19 +02:00
|
|
|
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.
|
2012-05-10 23:01:42 +02:00
|
|
|
reply, err = xproto.GetProperty(X, false, windowId, nameAtom.Atom,
|
|
|
|
xproto.GetPropertyTypeAny, 0, (1<<32)-1).Reply()
|
2012-05-07 10:09:19 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Active window name: %s\n", string(reply.Value))
|
|
|
|
}
|