Ejemplo n.º 1
0
func main() {
	X, err := xgb.NewConn()
	if err != nil {
		log.Fatal(err)
	}

	// Get the window id of the root window.
	setup := xproto.Setup(X)
	root := setup.DefaultScreen(X).Root

	// Get the atom id (i.e., intern an atom) of "_NET_ACTIVE_WINDOW".
	aname := "_NET_ACTIVE_WINDOW"
	query1, buf := xproto.InternAtom(X, nil, true, uint16(len(aname)), aname)

	activeAtom, err := query1.Reply()
	if err != nil {
		log.Fatal(err)
	}

	// Get the atom id (i.e., intern an atom) of "_NET_WM_NAME".
	aname = "_NET_WM_NAME"
	query2, buf := xproto.InternAtom(X, buf, true, uint16(len(aname)),
		aname)
	nameAtom, err := query2.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.
	query3, buf := xproto.GetProperty(X, buf, false, root, activeAtom.Atom,
		xproto.GetPropertyTypeAny, 0, (1<<32)-1)
	reply, err := query3.Reply()
	if err != nil {
		log.Fatal(err)
	}
	windowId := xproto.Window(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.
	query4, buf := xproto.GetProperty(X, buf, false, windowId, nameAtom.Atom,
		xproto.GetPropertyTypeAny, 0, (1<<32)-1)
	reply, err = query4.Reply()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Active window name: %s\n", string(reply.Value))
}
Ejemplo n.º 2
0
func main() {
	X, err := xgb.NewConn()
	if err != nil {
		log.Fatal(err)
	}

	names := seqNames(flagRequests)

	fcpu, err := os.Create(flagCpuProfName)
	if err != nil {
		log.Fatal(err)
	}
	defer fcpu.Close()
	pprof.StartCPUProfile(fcpu)
	defer pprof.StopCPUProfile()

	start := time.Now()
	cookies := make([]xproto.InternAtomCookie, flagRequests)
	for i := 0; i < flagRequests; i++ {
		// TODO: reuse buffers
		cookies[i], _ = xproto.InternAtom(X, nil,
			false, uint16(len(names[i])), names[i])
	}
	for _, cookie := range cookies {
		cookie.Reply()
	}
	fmt.Printf("Exec time: %s\n\n", time.Since(start))

	fmem, err := os.Create(flagMemProfName)
	if err != nil {
		log.Fatal(err)
	}
	defer fmem.Close()
	pprof.WriteHeapProfile(fmem)

	memStats := &runtime.MemStats{}
	runtime.ReadMemStats(memStats)

	// This isn't right. I'm not sure what's wrong.
	lastGcTime := time.Unix(int64(memStats.LastGC/1000000000),
		int64(memStats.LastGC-memStats.LastGC/1000000000))

	fmt.Printf("Alloc: %d\n", memStats.Alloc)
	fmt.Printf("TotalAlloc: %d\n", memStats.TotalAlloc)
	fmt.Printf("LastGC: %s\n", lastGcTime)
	fmt.Printf("PauseTotalNs: %d\n", memStats.PauseTotalNs)
	fmt.Printf("PauseNs: %d\n", memStats.PauseNs)
	fmt.Printf("NumGC: %d\n", memStats.NumGC)
}
Ejemplo n.º 3
0
// Atom interns an atom and panics if there is any error.
func Atom(xu *xgbutil.XUtil, name string,
	onlyIfExists bool) (xproto.Atom, error) {

	// Check the cache first
	if aid, ok := atomGet(xu, name); ok {
		return aid, nil
	}

	query, _ := xproto.InternAtom(xu.Conn(), nil, onlyIfExists,
		uint16(len(name)), name)
	reply, err := query.Reply()
	if err != nil {
		return 0, fmt.Errorf("Atom: Error interning atom '%s': %s", name, err)
	}

	// If we're here, it means we didn't have this atom cached. So cache it!
	cacheAtom(xu, name, reply.Atom)

	return reply.Atom, nil
}