update deps

This commit is contained in:
a 2023-06-11 09:21:08 -05:00
parent 9d961ec0ba
commit ba042cc537
159 changed files with 89054 additions and 6 deletions

7
go.mod
View File

@ -2,13 +2,12 @@ module tuxpa.in/t/wm
go 1.19 go 1.19
replace github.com/jezek/xgb v1.1.0 => ./xgb replace github.com/jezek/xgb v1.1.0 => ./vend/xgb
replace github.com/jezek/xgbutil v0.0.0-20230603163917-04188eb39cf0 => ./xgbutil replace github.com/jezek/xgbutil v0.0.0-20230603163917-04188eb39cf0 => ./vend/xgbutil
require github.com/jezek/xgb v1.1.0
require ( require (
github.com/jezek/xgb v1.1.0
github.com/jezek/xgbutil v0.0.0-20230603163917-04188eb39cf0 github.com/jezek/xgbutil v0.0.0-20230603163917-04188eb39cf0
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
) )

2
vend/xgb/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
xgbgen/xgbgen
.*.swp

18
vend/xgb/AUTHORS Normal file
View File

@ -0,0 +1,18 @@
Andrew Gallant is the maintainer of this fork. What follows is the original
list of authors for the x-go-binding.
# This is the official list of XGB authors for copyright purposes.
# This file is distinct from the CONTRIBUTORS files.
# See the latter for an explanation.
# Names should be added to this file as
# Name or Organization <email address>
# The email address is not required for organizations.
# Please keep the list sorted.
Anthony Martin <ality@pbrane.org>
Firmansyah Adiputra <frm.adiputra@gmail.com>
Google Inc.
Scott Lawrence <bytbox@gmail.com>
Tor Andersson <tor.andersson@gmail.com>

39
vend/xgb/CONTRIBUTORS Normal file
View File

@ -0,0 +1,39 @@
Andrew Gallant is the maintainer of this fork. What follows is the original
list of contributors for the x-go-binding.
# This is the official list of people who can contribute
# (and typically have contributed) code to the XGB repository.
# The AUTHORS file lists the copyright holders; this file
# lists people. For example, Google employees are listed here
# but not in AUTHORS, because Google holds the copyright.
#
# The submission process automatically checks to make sure
# that people submitting code are listed in this file (by email address).
#
# Names should be added to this file only after verifying that
# the individual or the individual's organization has agreed to
# the appropriate Contributor License Agreement, found here:
#
# http://code.google.com/legal/individual-cla-v1.0.html
# http://code.google.com/legal/corporate-cla-v1.0.html
#
# The agreement for individuals can be filled out on the web.
#
# When adding J Random Contributor's name to this file,
# either J's name or J's organization's name should be
# added to the AUTHORS file, depending on whether the
# individual or corporate CLA was used.
# Names should be added to this file like so:
# Name <email address>
# Please keep the list sorted.
Anthony Martin <ality@pbrane.org>
Firmansyah Adiputra <frm.adiputra@gmail.com>
Ian Lance Taylor <iant@golang.org>
Nigel Tao <nigeltao@golang.org>
Robert Griesemer <gri@golang.org>
Russ Cox <rsc@golang.org>
Scott Lawrence <bytbox@gmail.com>
Tor Andersson <tor.andersson@gmail.com>

42
vend/xgb/LICENSE Normal file
View File

@ -0,0 +1,42 @@
// Copyright (c) 2009 The XGB Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Subject to the terms and conditions of this License, Google hereby
// grants to You a perpetual, worldwide, non-exclusive, no-charge,
// royalty-free, irrevocable (except as stated in this section) patent
// license to make, have made, use, offer to sell, sell, import, and
// otherwise transfer this implementation of XGB, where such license
// applies only to those patent claims licensable by Google that are
// necessarily infringed by use of this implementation of XGB. If You
// institute patent litigation against any entity (including a
// cross-claim or counterclaim in a lawsuit) alleging that this
// implementation of XGB or a Contribution incorporated within this
// implementation of XGB constitutes direct or contributory patent
// infringement, then any patent licenses granted to You under this
// License for this implementation of XGB shall terminate as of the date
// such litigation is filed.

80
vend/xgb/Makefile Normal file
View File

@ -0,0 +1,80 @@
# This Makefile is used by the developer. It is not needed in any way to build
# a checkout of the XGB repository.
# It will be useful, however, if you are hacking at the code generator.
# i.e., after making a change to the code generator, run 'make' in the
# xgb directory. This will build xgbgen and regenerate each sub-package.
# 'make test' will then run any appropriate tests (just tests xproto right now).
# 'make bench' will test a couple of benchmarks.
# 'make build-all' will then try to build each extension. This isn't strictly
# necessary, but it's a good idea to make sure each sub-package is a valid
# Go package.
# My path to the X protocol XML descriptions.
ifndef XPROTO
XPROTO=/usr/share/xcb
endif
# All of the XML files in my /usr/share/xcb directory EXCEPT XKB. -_-
# This is intended to build xgbgen and generate Go code for each supported
# extension.
all: build-xgbgen \
bigreq.xml composite.xml damage.xml dpms.xml dri2.xml \
ge.xml glx.xml randr.xml record.xml render.xml res.xml \
screensaver.xml shape.xml shm.xml xc_misc.xml \
xevie.xml xf86dri.xml xf86vidmode.xml xfixes.xml xinerama.xml \
xprint.xml xproto.xml xselinux.xml xtest.xml \
xvmc.xml xv.xml
build-xgbgen:
(cd xgbgen && go build)
# Builds each individual sub-package to make sure its valid Go code.
build-all: bigreq.b composite.b damage.b dpms.b dri2.b ge.b glx.b randr.b \
record.b render.b res.b screensaver.b shape.b shm.b xcmisc.b \
xevie.b xf86dri.b xf86vidmode.b xfixes.b xinerama.b \
xprint.b xproto.b xselinux.b xtest.b xv.b xvmc.b
%.b:
(cd $* ; go build)
# Installs each individual sub-package.
install: bigreq.i composite.i damage.i dpms.i dri2.i ge.i glx.i randr.i \
record.i render.i res.i screensaver.i shape.i shm.i xcmisc.i \
xevie.i xf86dri.i xf86vidmode.i xfixes.i xinerama.i \
xprint.i xproto.i xselinux.i xtest.i xv.i xvmc.i
go install
%.i:
(cd $* ; go install)
# xc_misc is special because it has an underscore.
# There's probably a way to do this better, but Makefiles aren't my strong suit.
xc_misc.xml: build-xgbgen
mkdir -p xcmisc
xgbgen/xgbgen --proto-path $(XPROTO) $(XPROTO)/xc_misc.xml > xcmisc/xcmisc.go
%.xml: build-xgbgen
mkdir -p $*
xgbgen/xgbgen --proto-path $(XPROTO) $(XPROTO)/$*.xml > $*/$*.go
# Just test the xproto core protocol for now.
test:
(cd xproto ; go test)
# Force all xproto benchmarks to run and no tests.
bench:
(cd xproto ; go test -run 'nomatch' -bench '.*' -cpu 1,2,3,6)
# gofmt all non-auto-generated code.
# (auto-generated code is already gofmt'd.)
# Also do a column check (80 cols) after a gofmt.
# But don't check columns on auto-generated code, since I don't care if they
# break 80 cols.
gofmt:
gofmt -w *.go xgbgen/*.go examples/*.go examples/*/*.go xproto/xproto_test.go
colcheck *.go xgbgen/*.go examples/*.go examples/*/*.go xproto/xproto_test.go
push:
git push origin master
git push github master

62
vend/xgb/README Normal file
View File

@ -0,0 +1,62 @@
XGB is the X Go Binding, which is a low-level API to communicate with the
core X protocol and many of the X extensions. It is closely modeled after
XCB and xpyb.
It is thread safe and gets immediate improvement from parallelism when
GOMAXPROCS > 1. (See the benchmarks in xproto/xproto_test.go for evidence.)
Please see doc.go for more info.
Note that unless you know you need XGB, you can probably make your life
easier by using a slightly higher level library: xgbutil.
This is a fork of github.com/BurntSushi/xgb
Quick Usage
===========
go get github.com/jezek/xgb
go run go/path/src/github.com/jezek/xgb/examples/create-window/main.go
jezek's Fork
============
I've forked the XGB repository from BurntSushi's github to apply some
patches which caused panics and memory leaks upon close and tests were added,
to test multiple server close scenarios.
BurntSushi's Fork
=================
I've forked the XGB repository from Google Code due to inactivty upstream.
Godoc documentation can be found here:
https://godoc.org/github.com/BurntSushi/xgb
Much of the code has been rewritten in an effort to support thread safety
and multiple extensions. Namely, go_client.py has been thrown away in favor
of an xgbgen package.
The biggest parts that *haven't* been rewritten by me are the connection and
authentication handshakes. They're inherently messy, and there's really no
reason to re-work them. The rest of XGB has been completely rewritten.
I like to release my code under the WTFPL, but since I'm starting with someone
else's work, I'm leaving the original license/contributor/author information
in tact.
I suppose I can legitimately release xgbgen under the WTFPL. To be fair, it is
at least as complex as XGB itself. *sigh*
What follows is the original README:
XGB README
==========
XGB is the X protocol Go language Binding.
It is the Go equivalent of XCB, the X protocol C-language Binding
(http://xcb.freedesktop.org/).
Unless otherwise noted, the XGB source files are distributed
under the BSD-style license found in the LICENSE file.
Contributions should follow the same procedure as for the Go project:
http://golang.org/doc/contribute.html

29
vend/xgb/STYLE Normal file
View File

@ -0,0 +1,29 @@
I like to keep all my code to 80 columns or less. I have plenty of screen real
estate, but enjoy 80 columns so that I can have multiple code windows open side
to side and not be plagued by the ugly auto-wrapping of a text editor.
If you don't oblige me, I will fix any patch you submit to abide 80 columns.
Note that this style restriction does not preclude gofmt, but introduces a few
peculiarities. The first is that gofmt will occasionally add spacing (typically
to comments) that ends up going over 80 columns. Either shorten the comment or
put it on its own line.
The second and more common hiccup is when a function definition extends beyond
80 columns. If one adds line breaks to keep it below 80 columns, gofmt will
indent all subsequent lines in a function definition to the same indentation
level of the function body. This results in a less-than-ideal separation
between function definition and function body. To remedy this, simply add a
line break like so:
func RestackWindowExtra(xu *xgbutil.XUtil, win xproto.Window, stackMode int,
sibling xproto.Window, source int) error {
return ClientEvent(xu, win, "_NET_RESTACK_WINDOW", source, int(sibling),
stackMode)
}
Something similar should also be applied to long 'if' or 'for' conditionals,
although it would probably be preferrable to break up the conditional to
smaller chunks with a few helper variables.

110
vend/xgb/auth.go Normal file
View File

@ -0,0 +1,110 @@
package xgb
/*
auth.go contains functions to facilitate the parsing of .Xauthority files.
It is largely unmodified from the original XGB package that I forked.
*/
import (
"encoding/binary"
"errors"
"io"
"os"
)
// readAuthority reads the X authority file for the DISPLAY.
// If hostname == "" or hostname == "localhost",
// then use the system's hostname (as returned by os.Hostname) instead.
func readAuthority(hostname, display string) (
name string, data []byte, err error) {
// b is a scratch buffer to use and should be at least 256 bytes long
// (i.e. it should be able to hold a hostname).
b := make([]byte, 256)
// As per /usr/include/X11/Xauth.h.
const familyLocal = 256
const familyWild = 65535
if len(hostname) == 0 || hostname == "localhost" {
hostname, err = os.Hostname()
if err != nil {
return "", nil, err
}
}
fname := os.Getenv("XAUTHORITY")
if len(fname) == 0 {
home := os.Getenv("HOME")
if len(home) == 0 {
err = errors.New("Xauthority not found: $XAUTHORITY, $HOME not set")
return "", nil, err
}
fname = home + "/.Xauthority"
}
r, err := os.Open(fname)
if err != nil {
return "", nil, err
}
defer r.Close()
for {
var family uint16
if err := binary.Read(r, binary.BigEndian, &family); err != nil {
return "", nil, err
}
addr, err := getString(r, b)
if err != nil {
return "", nil, err
}
disp, err := getString(r, b)
if err != nil {
return "", nil, err
}
name0, err := getString(r, b)
if err != nil {
return "", nil, err
}
data0, err := getBytes(r, b)
if err != nil {
return "", nil, err
}
addrmatch := (family == familyWild) ||
(family == familyLocal && addr == hostname)
dispmatch := (disp == "") || (disp == display)
if addrmatch && dispmatch {
return name0, data0, nil
}
}
panic("unreachable")
}
func getBytes(r io.Reader, b []byte) ([]byte, error) {
var n uint16
if err := binary.Read(r, binary.BigEndian, &n); err != nil {
return nil, err
} else if n > uint16(len(b)) {
return nil, errors.New("bytes too long for buffer")
}
if _, err := io.ReadFull(r, b[0:n]); err != nil {
return nil, err
}
return b[0:n], nil
}
func getString(r io.Reader, b []byte) (string, error) {
b, err := getBytes(r, b)
if err != nil {
return "", err
}
return string(b), nil
}

152
vend/xgb/bigreq/bigreq.go Normal file
View File

@ -0,0 +1,152 @@
// Package bigreq is the X client API for the BIG-REQUESTS extension.
package bigreq
// This file is automatically generated from bigreq.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the BIG-REQUESTS extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 12, "BIG-REQUESTS").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named BIG-REQUESTS could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["BIG-REQUESTS"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["BIG-REQUESTS"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["BIG-REQUESTS"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["BIG-REQUESTS"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["BIG-REQUESTS"] = make(map[int]xgb.NewErrorFun)
}
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// EnableCookie is a cookie used only for Enable requests.
type EnableCookie struct {
*xgb.Cookie
}
// Enable sends a checked request.
// If an error occurs, it will be returned with the reply by calling EnableCookie.Reply()
func Enable(c *xgb.Conn) EnableCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["BIG-REQUESTS"]; !ok {
panic("Cannot issue request 'Enable' using the uninitialized extension 'BIG-REQUESTS'. bigreq.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(enableRequest(c), cookie)
return EnableCookie{cookie}
}
// EnableUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func EnableUnchecked(c *xgb.Conn) EnableCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["BIG-REQUESTS"]; !ok {
panic("Cannot issue request 'Enable' using the uninitialized extension 'BIG-REQUESTS'. bigreq.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(enableRequest(c), cookie)
return EnableCookie{cookie}
}
// EnableReply represents the data returned from a Enable request.
type EnableReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
MaximumRequestLength uint32
}
// Reply blocks and returns the reply data for a Enable request.
func (cook EnableCookie) Reply() (*EnableReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return enableReply(buf), nil
}
// enableReply reads a byte slice into a EnableReply value.
func enableReply(buf []byte) *EnableReply {
v := new(EnableReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.MaximumRequestLength = xgb.Get32(buf[b:])
b += 4
return v
}
// Write request to wire for Enable
// enableRequest writes a Enable request to a byte slice.
func enableRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["BIG-REQUESTS"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}

View File

@ -0,0 +1,721 @@
// Package composite is the X client API for the Composite extension.
package composite
// This file is automatically generated from composite.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xfixes"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the Composite extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 9, "Composite").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named Composite could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["Composite"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["Composite"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["Composite"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["Composite"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["Composite"] = make(map[int]xgb.NewErrorFun)
}
const (
RedirectAutomatic = 0
RedirectManual = 1
)
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// CreateRegionFromBorderClipCookie is a cookie used only for CreateRegionFromBorderClip requests.
type CreateRegionFromBorderClipCookie struct {
*xgb.Cookie
}
// CreateRegionFromBorderClip sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func CreateRegionFromBorderClip(c *xgb.Conn, Region xfixes.Region, Window xproto.Window) CreateRegionFromBorderClipCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'CreateRegionFromBorderClip' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(createRegionFromBorderClipRequest(c, Region, Window), cookie)
return CreateRegionFromBorderClipCookie{cookie}
}
// CreateRegionFromBorderClipChecked sends a checked request.
// If an error occurs, it can be retrieved using CreateRegionFromBorderClipCookie.Check()
func CreateRegionFromBorderClipChecked(c *xgb.Conn, Region xfixes.Region, Window xproto.Window) CreateRegionFromBorderClipCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'CreateRegionFromBorderClip' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(createRegionFromBorderClipRequest(c, Region, Window), cookie)
return CreateRegionFromBorderClipCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook CreateRegionFromBorderClipCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for CreateRegionFromBorderClip
// createRegionFromBorderClipRequest writes a CreateRegionFromBorderClip request to a byte slice.
func createRegionFromBorderClipRequest(c *xgb.Conn, Region xfixes.Region, Window xproto.Window) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Composite"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 5 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Region))
b += 4
xgb.Put32(buf[b:], uint32(Window))
b += 4
return buf
}
// GetOverlayWindowCookie is a cookie used only for GetOverlayWindow requests.
type GetOverlayWindowCookie struct {
*xgb.Cookie
}
// GetOverlayWindow sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetOverlayWindowCookie.Reply()
func GetOverlayWindow(c *xgb.Conn, Window xproto.Window) GetOverlayWindowCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'GetOverlayWindow' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getOverlayWindowRequest(c, Window), cookie)
return GetOverlayWindowCookie{cookie}
}
// GetOverlayWindowUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetOverlayWindowUnchecked(c *xgb.Conn, Window xproto.Window) GetOverlayWindowCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'GetOverlayWindow' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getOverlayWindowRequest(c, Window), cookie)
return GetOverlayWindowCookie{cookie}
}
// GetOverlayWindowReply represents the data returned from a GetOverlayWindow request.
type GetOverlayWindowReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
OverlayWin xproto.Window
// padding: 20 bytes
}
// Reply blocks and returns the reply data for a GetOverlayWindow request.
func (cook GetOverlayWindowCookie) Reply() (*GetOverlayWindowReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getOverlayWindowReply(buf), nil
}
// getOverlayWindowReply reads a byte slice into a GetOverlayWindowReply value.
func getOverlayWindowReply(buf []byte) *GetOverlayWindowReply {
v := new(GetOverlayWindowReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.OverlayWin = xproto.Window(xgb.Get32(buf[b:]))
b += 4
b += 20 // padding
return v
}
// Write request to wire for GetOverlayWindow
// getOverlayWindowRequest writes a GetOverlayWindow request to a byte slice.
func getOverlayWindowRequest(c *xgb.Conn, Window xproto.Window) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Composite"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 7 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
return buf
}
// NameWindowPixmapCookie is a cookie used only for NameWindowPixmap requests.
type NameWindowPixmapCookie struct {
*xgb.Cookie
}
// NameWindowPixmap sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func NameWindowPixmap(c *xgb.Conn, Window xproto.Window, Pixmap xproto.Pixmap) NameWindowPixmapCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'NameWindowPixmap' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(nameWindowPixmapRequest(c, Window, Pixmap), cookie)
return NameWindowPixmapCookie{cookie}
}
// NameWindowPixmapChecked sends a checked request.
// If an error occurs, it can be retrieved using NameWindowPixmapCookie.Check()
func NameWindowPixmapChecked(c *xgb.Conn, Window xproto.Window, Pixmap xproto.Pixmap) NameWindowPixmapCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'NameWindowPixmap' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(nameWindowPixmapRequest(c, Window, Pixmap), cookie)
return NameWindowPixmapCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook NameWindowPixmapCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for NameWindowPixmap
// nameWindowPixmapRequest writes a NameWindowPixmap request to a byte slice.
func nameWindowPixmapRequest(c *xgb.Conn, Window xproto.Window, Pixmap xproto.Pixmap) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Composite"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 6 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
xgb.Put32(buf[b:], uint32(Pixmap))
b += 4
return buf
}
// QueryVersionCookie is a cookie used only for QueryVersion requests.
type QueryVersionCookie struct {
*xgb.Cookie
}
// QueryVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling QueryVersionCookie.Reply()
func QueryVersion(c *xgb.Conn, ClientMajorVersion uint32, ClientMinorVersion uint32) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func QueryVersionUnchecked(c *xgb.Conn, ClientMajorVersion uint32, ClientMinorVersion uint32) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionReply represents the data returned from a QueryVersion request.
type QueryVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
MajorVersion uint32
MinorVersion uint32
// padding: 16 bytes
}
// Reply blocks and returns the reply data for a QueryVersion request.
func (cook QueryVersionCookie) Reply() (*QueryVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return queryVersionReply(buf), nil
}
// queryVersionReply reads a byte slice into a QueryVersionReply value.
func queryVersionReply(buf []byte) *QueryVersionReply {
v := new(QueryVersionReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.MajorVersion = xgb.Get32(buf[b:])
b += 4
v.MinorVersion = xgb.Get32(buf[b:])
b += 4
b += 16 // padding
return v
}
// Write request to wire for QueryVersion
// queryVersionRequest writes a QueryVersion request to a byte slice.
func queryVersionRequest(c *xgb.Conn, ClientMajorVersion uint32, ClientMinorVersion uint32) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Composite"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], ClientMajorVersion)
b += 4
xgb.Put32(buf[b:], ClientMinorVersion)
b += 4
return buf
}
// RedirectSubwindowsCookie is a cookie used only for RedirectSubwindows requests.
type RedirectSubwindowsCookie struct {
*xgb.Cookie
}
// RedirectSubwindows sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func RedirectSubwindows(c *xgb.Conn, Window xproto.Window, Update byte) RedirectSubwindowsCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'RedirectSubwindows' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(redirectSubwindowsRequest(c, Window, Update), cookie)
return RedirectSubwindowsCookie{cookie}
}
// RedirectSubwindowsChecked sends a checked request.
// If an error occurs, it can be retrieved using RedirectSubwindowsCookie.Check()
func RedirectSubwindowsChecked(c *xgb.Conn, Window xproto.Window, Update byte) RedirectSubwindowsCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'RedirectSubwindows' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(redirectSubwindowsRequest(c, Window, Update), cookie)
return RedirectSubwindowsCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook RedirectSubwindowsCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for RedirectSubwindows
// redirectSubwindowsRequest writes a RedirectSubwindows request to a byte slice.
func redirectSubwindowsRequest(c *xgb.Conn, Window xproto.Window, Update byte) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Composite"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 2 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
buf[b] = Update
b += 1
b += 3 // padding
return buf
}
// RedirectWindowCookie is a cookie used only for RedirectWindow requests.
type RedirectWindowCookie struct {
*xgb.Cookie
}
// RedirectWindow sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func RedirectWindow(c *xgb.Conn, Window xproto.Window, Update byte) RedirectWindowCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'RedirectWindow' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(redirectWindowRequest(c, Window, Update), cookie)
return RedirectWindowCookie{cookie}
}
// RedirectWindowChecked sends a checked request.
// If an error occurs, it can be retrieved using RedirectWindowCookie.Check()
func RedirectWindowChecked(c *xgb.Conn, Window xproto.Window, Update byte) RedirectWindowCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'RedirectWindow' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(redirectWindowRequest(c, Window, Update), cookie)
return RedirectWindowCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook RedirectWindowCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for RedirectWindow
// redirectWindowRequest writes a RedirectWindow request to a byte slice.
func redirectWindowRequest(c *xgb.Conn, Window xproto.Window, Update byte) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Composite"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 1 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
buf[b] = Update
b += 1
b += 3 // padding
return buf
}
// ReleaseOverlayWindowCookie is a cookie used only for ReleaseOverlayWindow requests.
type ReleaseOverlayWindowCookie struct {
*xgb.Cookie
}
// ReleaseOverlayWindow sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func ReleaseOverlayWindow(c *xgb.Conn, Window xproto.Window) ReleaseOverlayWindowCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'ReleaseOverlayWindow' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(releaseOverlayWindowRequest(c, Window), cookie)
return ReleaseOverlayWindowCookie{cookie}
}
// ReleaseOverlayWindowChecked sends a checked request.
// If an error occurs, it can be retrieved using ReleaseOverlayWindowCookie.Check()
func ReleaseOverlayWindowChecked(c *xgb.Conn, Window xproto.Window) ReleaseOverlayWindowCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'ReleaseOverlayWindow' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(releaseOverlayWindowRequest(c, Window), cookie)
return ReleaseOverlayWindowCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook ReleaseOverlayWindowCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for ReleaseOverlayWindow
// releaseOverlayWindowRequest writes a ReleaseOverlayWindow request to a byte slice.
func releaseOverlayWindowRequest(c *xgb.Conn, Window xproto.Window) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Composite"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 8 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
return buf
}
// UnredirectSubwindowsCookie is a cookie used only for UnredirectSubwindows requests.
type UnredirectSubwindowsCookie struct {
*xgb.Cookie
}
// UnredirectSubwindows sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func UnredirectSubwindows(c *xgb.Conn, Window xproto.Window, Update byte) UnredirectSubwindowsCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'UnredirectSubwindows' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(unredirectSubwindowsRequest(c, Window, Update), cookie)
return UnredirectSubwindowsCookie{cookie}
}
// UnredirectSubwindowsChecked sends a checked request.
// If an error occurs, it can be retrieved using UnredirectSubwindowsCookie.Check()
func UnredirectSubwindowsChecked(c *xgb.Conn, Window xproto.Window, Update byte) UnredirectSubwindowsCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'UnredirectSubwindows' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(unredirectSubwindowsRequest(c, Window, Update), cookie)
return UnredirectSubwindowsCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook UnredirectSubwindowsCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for UnredirectSubwindows
// unredirectSubwindowsRequest writes a UnredirectSubwindows request to a byte slice.
func unredirectSubwindowsRequest(c *xgb.Conn, Window xproto.Window, Update byte) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Composite"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 4 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
buf[b] = Update
b += 1
b += 3 // padding
return buf
}
// UnredirectWindowCookie is a cookie used only for UnredirectWindow requests.
type UnredirectWindowCookie struct {
*xgb.Cookie
}
// UnredirectWindow sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func UnredirectWindow(c *xgb.Conn, Window xproto.Window, Update byte) UnredirectWindowCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'UnredirectWindow' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(unredirectWindowRequest(c, Window, Update), cookie)
return UnredirectWindowCookie{cookie}
}
// UnredirectWindowChecked sends a checked request.
// If an error occurs, it can be retrieved using UnredirectWindowCookie.Check()
func UnredirectWindowChecked(c *xgb.Conn, Window xproto.Window, Update byte) UnredirectWindowCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Composite"]; !ok {
panic("Cannot issue request 'UnredirectWindow' using the uninitialized extension 'Composite'. composite.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(unredirectWindowRequest(c, Window, Update), cookie)
return UnredirectWindowCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook UnredirectWindowCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for UnredirectWindow
// unredirectWindowRequest writes a UnredirectWindow request to a byte slice.
func unredirectWindowRequest(c *xgb.Conn, Window xproto.Window, Update byte) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Composite"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 3 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
buf[b] = Update
b += 1
b += 3 // padding
return buf
}

186
vend/xgb/conn.go Normal file
View File

@ -0,0 +1,186 @@
package xgb
/*
conn.go contains a couple of functions that do some real dirty work related
to the initial connection handshake with X.
This code is largely unmodified from the original XGB package that I forked.
*/
import (
"errors"
"fmt"
"io"
"net"
"os"
"strconv"
"strings"
)
// connect connects to the X server given in the 'display' string,
// and does all the necessary setup handshaking.
// If 'display' is empty it will be taken from os.Getenv("DISPLAY").
// Note that you should read and understand the "Connection Setup" of the
// X Protocol Reference Manual before changing this function:
// http://goo.gl/4zGQg
func (c *Conn) connect(display string) error {
err := c.dial(display)
if err != nil {
return err
}
return c.postConnect()
}
// connect init from to the net.Conn,
func (c *Conn) connectNet(netConn net.Conn) error {
c.conn = netConn
return c.postConnect()
}
// do the postConnect action after Conn get it's underly net.Conn
func (c *Conn) postConnect() error {
// Get authentication data
authName, authData, err := readAuthority(c.host, c.display)
noauth := false
if err != nil {
Logger.Printf("Could not get authority info: %v", err)
Logger.Println("Trying connection without authority info...")
authName = ""
authData = []byte{}
noauth = true
}
// Assume that the authentication protocol is "MIT-MAGIC-COOKIE-1".
if !noauth && (authName != "MIT-MAGIC-COOKIE-1" || len(authData) != 16) {
return errors.New("unsupported auth protocol " + authName)
}
buf := make([]byte, 12+Pad(len(authName))+Pad(len(authData)))
buf[0] = 0x6c
buf[1] = 0
Put16(buf[2:], 11)
Put16(buf[4:], 0)
Put16(buf[6:], uint16(len(authName)))
Put16(buf[8:], uint16(len(authData)))
Put16(buf[10:], 0)
copy(buf[12:], []byte(authName))
copy(buf[12+Pad(len(authName)):], authData)
if _, err = c.conn.Write(buf); err != nil {
return err
}
head := make([]byte, 8)
if _, err = io.ReadFull(c.conn, head[0:8]); err != nil {
return err
}
code := head[0]
reasonLen := head[1]
major := Get16(head[2:])
minor := Get16(head[4:])
dataLen := Get16(head[6:])
if major != 11 || minor != 0 {
return fmt.Errorf("x protocol version mismatch: %d.%d", major, minor)
}
buf = make([]byte, int(dataLen)*4+8, int(dataLen)*4+8)
copy(buf, head)
if _, err = io.ReadFull(c.conn, buf[8:]); err != nil {
return err
}
if code == 0 {
reason := buf[8 : 8+reasonLen]
return fmt.Errorf("x protocol authentication refused: %s",
string(reason))
}
// Unfortunately, it isn't really feasible to read the setup bytes here,
// since the code to do so is in a different package.
// Users must call 'xproto.Setup(X)' to get the setup info.
c.SetupBytes = buf
// But also read stuff that we *need* to get started.
c.setupResourceIdBase = Get32(buf[12:])
c.setupResourceIdMask = Get32(buf[16:])
return nil
}
// dial initializes the actual net connection with X.
func (c *Conn) dial(display string) error {
if len(display) == 0 {
display = os.Getenv("DISPLAY")
}
display0 := display
if len(display) == 0 {
return errors.New("empty display string")
}
colonIdx := strings.LastIndex(display, ":")
if colonIdx < 0 {
return errors.New("bad display string: " + display0)
}
var protocol, socket string
if display[0] == '/' {
socket = display[0:colonIdx]
} else {
slashIdx := strings.LastIndex(display, "/")
if slashIdx >= 0 {
protocol = display[0:slashIdx]
c.host = display[slashIdx+1 : colonIdx]
} else {
c.host = display[0:colonIdx]
}
}
display = display[colonIdx+1 : len(display)]
if len(display) == 0 {
return errors.New("bad display string: " + display0)
}
var scr string
dotIdx := strings.LastIndex(display, ".")
if dotIdx < 0 {
c.display = display[0:]
} else {
c.display = display[0:dotIdx]
scr = display[dotIdx+1:]
}
var err error
c.DisplayNumber, err = strconv.Atoi(c.display)
if err != nil || c.DisplayNumber < 0 {
return errors.New("bad display string: " + display0)
}
if len(scr) != 0 {
c.DefaultScreen, err = strconv.Atoi(scr)
if err != nil {
return errors.New("bad display string: " + display0)
}
}
// Connect to server
if len(socket) != 0 {
c.conn, err = net.Dial("unix", socket+":"+c.display)
} else if len(c.host) != 0 && c.host != "unix" {
if protocol == "" {
protocol = "tcp"
}
c.conn, err = net.Dial(protocol,
c.host+":"+strconv.Itoa(6000+c.DisplayNumber))
} else {
c.host = ""
c.conn, err = net.Dial("unix", "/tmp/.X11-unix/X"+c.display)
}
if err != nil {
return errors.New("cannot connect to " + display0 + ": " + err.Error())
}
return nil
}

178
vend/xgb/cookie.go Normal file
View File

@ -0,0 +1,178 @@
package xgb
import (
"errors"
"io"
)
// Cookie is the internal representation of a cookie, where one is generated
// for *every* request sent by XGB.
// 'cookie' is most frequently used by embedding it into a more specific
// kind of cookie, i.e., 'GetInputFocusCookie'.
type Cookie struct {
conn *Conn
Sequence uint16
replyChan chan []byte
errorChan chan error
pingChan chan bool
}
// NewCookie creates a new cookie with the correct channels initialized
// depending upon the values of 'checked' and 'reply'. Together, there are
// four different kinds of cookies. (See more detailed comments in the
// function for more info on those.)
// Note that a sequence number is not set until just before the request
// corresponding to this cookie is sent over the wire.
//
// Unless you're building requests from bytes by hand, this method should
// not be used.
func (c *Conn) NewCookie(checked, reply bool) *Cookie {
cookie := &Cookie{
conn: c,
Sequence: 0, // we add the sequence id just before sending a request
replyChan: nil,
errorChan: nil,
pingChan: nil,
}
// There are four different kinds of cookies:
// Checked requests with replies get a reply channel and an error channel.
// Unchecked requests with replies get a reply channel and a ping channel.
// Checked requests w/o replies get a ping channel and an error channel.
// Unchecked requests w/o replies get no channels.
// The reply channel is used to send reply data.
// The error channel is used to send error data.
// The ping channel is used when one of the 'reply' or 'error' channels
// is missing but the other is present. The ping channel is way to force
// the blocking to stop and basically say "the error has been received
// in the main event loop" (when the ping channel is coupled with a reply
// channel) or "the request you made that has no reply was successful"
// (when the ping channel is coupled with an error channel).
if checked {
cookie.errorChan = make(chan error, 1)
if !reply {
cookie.pingChan = make(chan bool, 1)
}
}
if reply {
cookie.replyChan = make(chan []byte, 1)
if !checked {
cookie.pingChan = make(chan bool, 1)
}
}
return cookie
}
// Reply detects whether this is a checked or unchecked cookie, and calls
// 'replyChecked' or 'replyUnchecked' appropriately.
//
// Unless you're building requests from bytes by hand, this method should
// not be used.
func (c Cookie) Reply() ([]byte, error) {
// checked
if c.errorChan != nil {
return c.replyChecked()
}
return c.replyUnchecked()
}
// replyChecked waits for a response on either the replyChan or errorChan
// channels. If the former arrives, the bytes are returned with a nil error.
// If the latter arrives, no bytes are returned (nil) and the error received
// is returned.
// Returns (nil, io.EOF) when the connection is closed.
//
// Unless you're building requests from bytes by hand, this method should
// not be used.
func (c Cookie) replyChecked() ([]byte, error) {
if c.replyChan == nil {
return nil, errors.New("Cannot call 'replyChecked' on a cookie that " +
"is not expecting a *reply* or an error.")
}
if c.errorChan == nil {
return nil, errors.New("Cannot call 'replyChecked' on a cookie that " +
"is not expecting a reply or an *error*.")
}
select {
case reply := <-c.replyChan:
return reply, nil
case err := <-c.errorChan:
return nil, err
case <-c.conn.doneRead:
// c.conn.readResponses is no more, there will be no replys or errors
return nil, io.EOF
}
}
// replyUnchecked waits for a response on either the replyChan or pingChan
// channels. If the former arrives, the bytes are returned with a nil error.
// If the latter arrives, no bytes are returned (nil) and a nil error
// is returned. (In the latter case, the corresponding error can be retrieved
// from (Wait|Poll)ForEvent asynchronously.)
// Returns (nil, io.EOF) when the connection is closed.
// In all honesty, you *probably* don't want to use this method.
//
// Unless you're building requests from bytes by hand, this method should
// not be used.
func (c Cookie) replyUnchecked() ([]byte, error) {
if c.replyChan == nil {
return nil, errors.New("Cannot call 'replyUnchecked' on a cookie " +
"that is not expecting a *reply*.")
}
select {
case reply := <-c.replyChan:
return reply, nil
case <-c.pingChan:
return nil, nil
case <-c.conn.doneRead:
// c.conn.readResponses is no more, there will be no replys or pings
return nil, io.EOF
}
}
// Check is used for checked requests that have no replies. It is a mechanism
// by which to report "success" or "error" in a synchronous fashion. (Therefore,
// unchecked requests without replies cannot use this method.)
// If the request causes an error, it is sent to this cookie's errorChan.
// If the request was successful, there is no response from the server.
// Thus, pingChan is sent a value when the *next* reply is read.
// If no more replies are being processed, we force a round trip request with
// GetInputFocus.
// Returns io.EOF error when the connection is closed.
//
// Unless you're building requests from bytes by hand, this method should
// not be used.
func (c Cookie) Check() error {
if c.replyChan != nil {
return errors.New("Cannot call 'Check' on a cookie that is " +
"expecting a *reply*. Use 'Reply' instead.")
}
if c.errorChan == nil {
return errors.New("Cannot call 'Check' on a cookie that is " +
"not expecting a possible *error*.")
}
// First do a quick non-blocking check to see if we've been pinged.
select {
case err := <-c.errorChan:
return err
case <-c.pingChan:
return nil
default:
}
// Now force a round trip and try again, but block this time.
c.conn.Sync()
select {
case err := <-c.errorChan:
return err
case <-c.pingChan:
return nil
case <-c.conn.doneRead:
// c.conn.readResponses is no more, there will be no errors or pings
return io.EOF
}
}

592
vend/xgb/damage/damage.go Normal file
View File

@ -0,0 +1,592 @@
// Package damage is the X client API for the DAMAGE extension.
package damage
// This file is automatically generated from damage.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xfixes"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the DAMAGE extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 6, "DAMAGE").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named DAMAGE could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["DAMAGE"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["DAMAGE"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["DAMAGE"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["DAMAGE"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["DAMAGE"] = make(map[int]xgb.NewErrorFun)
}
// BadBadDamage is the error number for a BadBadDamage.
const BadBadDamage = 0
type BadDamageError struct {
Sequence uint16
NiceName string
}
// BadDamageErrorNew constructs a BadDamageError value that implements xgb.Error from a byte slice.
func BadDamageErrorNew(buf []byte) xgb.Error {
v := BadDamageError{}
v.NiceName = "BadDamage"
b := 1 // skip error determinant
b += 1 // don't read error number
v.Sequence = xgb.Get16(buf[b:])
b += 2
return v
}
// SequenceId returns the sequence id attached to the BadBadDamage error.
// This is mostly used internally.
func (err BadDamageError) SequenceId() uint16 {
return err.Sequence
}
// BadId returns the 'BadValue' number if one exists for the BadBadDamage error. If no bad value exists, 0 is returned.
func (err BadDamageError) BadId() uint32 {
return 0
}
// Error returns a rudimentary string representation of the BadBadDamage error.
func (err BadDamageError) Error() string {
fieldVals := make([]string, 0, 0)
fieldVals = append(fieldVals, "NiceName: "+err.NiceName)
fieldVals = append(fieldVals, xgb.Sprintf("Sequence: %d", err.Sequence))
return "BadBadDamage {" + xgb.StringsJoin(fieldVals, ", ") + "}"
}
func init() {
xgb.NewExtErrorFuncs["DAMAGE"][0] = BadDamageErrorNew
}
type Damage uint32
func NewDamageId(c *xgb.Conn) (Damage, error) {
id, err := c.NewId()
if err != nil {
return 0, err
}
return Damage(id), nil
}
// Notify is the event number for a NotifyEvent.
const Notify = 0
type NotifyEvent struct {
Sequence uint16
Level byte
Drawable xproto.Drawable
Damage Damage
Timestamp xproto.Timestamp
Area xproto.Rectangle
Geometry xproto.Rectangle
}
// NotifyEventNew constructs a NotifyEvent value that implements xgb.Event from a byte slice.
func NotifyEventNew(buf []byte) xgb.Event {
v := NotifyEvent{}
b := 1 // don't read event number
v.Level = buf[b]
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Drawable = xproto.Drawable(xgb.Get32(buf[b:]))
b += 4
v.Damage = Damage(xgb.Get32(buf[b:]))
b += 4
v.Timestamp = xproto.Timestamp(xgb.Get32(buf[b:]))
b += 4
v.Area = xproto.Rectangle{}
b += xproto.RectangleRead(buf[b:], &v.Area)
v.Geometry = xproto.Rectangle{}
b += xproto.RectangleRead(buf[b:], &v.Geometry)
return v
}
// Bytes writes a NotifyEvent value to a byte slice.
func (v NotifyEvent) Bytes() []byte {
buf := make([]byte, 32)
b := 0
// write event number
buf[b] = 0
b += 1
buf[b] = v.Level
b += 1
b += 2 // skip sequence number
xgb.Put32(buf[b:], uint32(v.Drawable))
b += 4
xgb.Put32(buf[b:], uint32(v.Damage))
b += 4
xgb.Put32(buf[b:], uint32(v.Timestamp))
b += 4
{
structBytes := v.Area.Bytes()
copy(buf[b:], structBytes)
b += len(structBytes)
}
{
structBytes := v.Geometry.Bytes()
copy(buf[b:], structBytes)
b += len(structBytes)
}
return buf
}
// SequenceId returns the sequence id attached to the Notify event.
// Events without a sequence number (KeymapNotify) return 0.
// This is mostly used internally.
func (v NotifyEvent) SequenceId() uint16 {
return v.Sequence
}
// String is a rudimentary string representation of NotifyEvent.
func (v NotifyEvent) String() string {
fieldVals := make([]string, 0, 6)
fieldVals = append(fieldVals, xgb.Sprintf("Sequence: %d", v.Sequence))
fieldVals = append(fieldVals, xgb.Sprintf("Level: %d", v.Level))
fieldVals = append(fieldVals, xgb.Sprintf("Drawable: %d", v.Drawable))
fieldVals = append(fieldVals, xgb.Sprintf("Damage: %d", v.Damage))
fieldVals = append(fieldVals, xgb.Sprintf("Timestamp: %d", v.Timestamp))
return "Notify {" + xgb.StringsJoin(fieldVals, ", ") + "}"
}
func init() {
xgb.NewExtEventFuncs["DAMAGE"][0] = NotifyEventNew
}
const (
ReportLevelRawRectangles = 0
ReportLevelDeltaRectangles = 1
ReportLevelBoundingBox = 2
ReportLevelNonEmpty = 3
)
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// AddCookie is a cookie used only for Add requests.
type AddCookie struct {
*xgb.Cookie
}
// Add sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func Add(c *xgb.Conn, Drawable xproto.Drawable, Region xfixes.Region) AddCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'Add' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(addRequest(c, Drawable, Region), cookie)
return AddCookie{cookie}
}
// AddChecked sends a checked request.
// If an error occurs, it can be retrieved using AddCookie.Check()
func AddChecked(c *xgb.Conn, Drawable xproto.Drawable, Region xfixes.Region) AddCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'Add' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(addRequest(c, Drawable, Region), cookie)
return AddCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook AddCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for Add
// addRequest writes a Add request to a byte slice.
func addRequest(c *xgb.Conn, Drawable xproto.Drawable, Region xfixes.Region) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DAMAGE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 4 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Drawable))
b += 4
xgb.Put32(buf[b:], uint32(Region))
b += 4
return buf
}
// CreateCookie is a cookie used only for Create requests.
type CreateCookie struct {
*xgb.Cookie
}
// Create sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func Create(c *xgb.Conn, Damage Damage, Drawable xproto.Drawable, Level byte) CreateCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'Create' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(createRequest(c, Damage, Drawable, Level), cookie)
return CreateCookie{cookie}
}
// CreateChecked sends a checked request.
// If an error occurs, it can be retrieved using CreateCookie.Check()
func CreateChecked(c *xgb.Conn, Damage Damage, Drawable xproto.Drawable, Level byte) CreateCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'Create' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(createRequest(c, Damage, Drawable, Level), cookie)
return CreateCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook CreateCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for Create
// createRequest writes a Create request to a byte slice.
func createRequest(c *xgb.Conn, Damage Damage, Drawable xproto.Drawable, Level byte) []byte {
size := 16
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DAMAGE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 1 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Damage))
b += 4
xgb.Put32(buf[b:], uint32(Drawable))
b += 4
buf[b] = Level
b += 1
b += 3 // padding
return buf
}
// DestroyCookie is a cookie used only for Destroy requests.
type DestroyCookie struct {
*xgb.Cookie
}
// Destroy sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func Destroy(c *xgb.Conn, Damage Damage) DestroyCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'Destroy' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(destroyRequest(c, Damage), cookie)
return DestroyCookie{cookie}
}
// DestroyChecked sends a checked request.
// If an error occurs, it can be retrieved using DestroyCookie.Check()
func DestroyChecked(c *xgb.Conn, Damage Damage) DestroyCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'Destroy' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(destroyRequest(c, Damage), cookie)
return DestroyCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook DestroyCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for Destroy
// destroyRequest writes a Destroy request to a byte slice.
func destroyRequest(c *xgb.Conn, Damage Damage) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DAMAGE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 2 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Damage))
b += 4
return buf
}
// QueryVersionCookie is a cookie used only for QueryVersion requests.
type QueryVersionCookie struct {
*xgb.Cookie
}
// QueryVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling QueryVersionCookie.Reply()
func QueryVersion(c *xgb.Conn, ClientMajorVersion uint32, ClientMinorVersion uint32) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func QueryVersionUnchecked(c *xgb.Conn, ClientMajorVersion uint32, ClientMinorVersion uint32) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionReply represents the data returned from a QueryVersion request.
type QueryVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
MajorVersion uint32
MinorVersion uint32
// padding: 16 bytes
}
// Reply blocks and returns the reply data for a QueryVersion request.
func (cook QueryVersionCookie) Reply() (*QueryVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return queryVersionReply(buf), nil
}
// queryVersionReply reads a byte slice into a QueryVersionReply value.
func queryVersionReply(buf []byte) *QueryVersionReply {
v := new(QueryVersionReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.MajorVersion = xgb.Get32(buf[b:])
b += 4
v.MinorVersion = xgb.Get32(buf[b:])
b += 4
b += 16 // padding
return v
}
// Write request to wire for QueryVersion
// queryVersionRequest writes a QueryVersion request to a byte slice.
func queryVersionRequest(c *xgb.Conn, ClientMajorVersion uint32, ClientMinorVersion uint32) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DAMAGE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], ClientMajorVersion)
b += 4
xgb.Put32(buf[b:], ClientMinorVersion)
b += 4
return buf
}
// SubtractCookie is a cookie used only for Subtract requests.
type SubtractCookie struct {
*xgb.Cookie
}
// Subtract sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func Subtract(c *xgb.Conn, Damage Damage, Repair xfixes.Region, Parts xfixes.Region) SubtractCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'Subtract' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(subtractRequest(c, Damage, Repair, Parts), cookie)
return SubtractCookie{cookie}
}
// SubtractChecked sends a checked request.
// If an error occurs, it can be retrieved using SubtractCookie.Check()
func SubtractChecked(c *xgb.Conn, Damage Damage, Repair xfixes.Region, Parts xfixes.Region) SubtractCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DAMAGE"]; !ok {
panic("Cannot issue request 'Subtract' using the uninitialized extension 'DAMAGE'. damage.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(subtractRequest(c, Damage, Repair, Parts), cookie)
return SubtractCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook SubtractCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for Subtract
// subtractRequest writes a Subtract request to a byte slice.
func subtractRequest(c *xgb.Conn, Damage Damage, Repair xfixes.Region, Parts xfixes.Region) []byte {
size := 16
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DAMAGE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 3 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Damage))
b += 4
xgb.Put32(buf[b:], uint32(Repair))
b += 4
xgb.Put32(buf[b:], uint32(Parts))
b += 4
return buf
}

146
vend/xgb/doc.go Normal file
View File

@ -0,0 +1,146 @@
/*
Package XGB provides the X Go Binding, which is a low-level API to communicate
with the core X protocol and many of the X extensions.
It is *very* closely modeled on XCB, so that experience with XCB (or xpyb) is
easily translatable to XGB. That is, it uses the same cookie/reply model
and is thread safe. There are otherwise no major differences (in the API).
Most uses of XGB typically fall under the realm of window manager and GUI kit
development, but other applications (like pagers, panels, tilers, etc.) may
also require XGB. Moreover, it is a near certainty that if you need to work
with X, xgbutil will be of great use to you as well:
https://github.com/jezek/xgbutil
Example
This is an extremely terse example that demonstrates how to connect to X,
create a window, listen to StructureNotify events and Key{Press,Release}
events, map the window, and print out all events received. An example with
accompanying documentation can be found in examples/create-window.
package main
import (
"fmt"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
func main() {
X, err := xgb.NewConn()
if err != nil {
fmt.Println(err)
return
}
wid, _ := xproto.NewWindowId(X)
screen := xproto.Setup(X).DefaultScreen(X)
xproto.CreateWindow(X, screen.RootDepth, wid, screen.Root,
0, 0, 500, 500, 0,
xproto.WindowClassInputOutput, screen.RootVisual,
xproto.CwBackPixel | xproto.CwEventMask,
[]uint32{ // values must be in the order defined by the protocol
0xffffffff,
xproto.EventMaskStructureNotify |
xproto.EventMaskKeyPress |
xproto.EventMaskKeyRelease})
xproto.MapWindow(X, wid)
for {
ev, xerr := X.WaitForEvent()
if ev == nil && xerr == nil {
fmt.Println("Both event and error are nil. Exiting...")
return
}
if ev != nil {
fmt.Printf("Event: %s\n", ev)
}
if xerr != nil {
fmt.Printf("Error: %s\n", xerr)
}
}
}
Xinerama Example
This is another small example that shows how to query Xinerama for geometry
information of each active head. Accompanying documentation for this example
can be found in examples/xinerama.
package main
import (
"fmt"
"log"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xinerama"
)
func main() {
X, err := xgb.NewConn()
if err != nil {
log.Fatal(err)
}
// Initialize the Xinerama extension.
// The appropriate 'Init' function must be run for *every*
// extension before any of its requests can be used.
err = xinerama.Init(X)
if err != nil {
log.Fatal(err)
}
reply, err := xinerama.QueryScreens(X).Reply()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Number of heads: %d\n", reply.Number)
for i, screen := range reply.ScreenInfo {
fmt.Printf("%d :: X: %d, Y: %d, Width: %d, Height: %d\n",
i, screen.XOrg, screen.YOrg, screen.Width, screen.Height)
}
}
Parallelism
XGB can benefit greatly from parallelism due to its concurrent design. For
evidence of this claim, please see the benchmarks in xproto/xproto_test.go.
Tests
xproto/xproto_test.go contains a number of contrived tests that stress
particular corners of XGB that I presume could be problem areas. Namely:
requests with no replies, requests with replies, checked errors, unchecked
errors, sequence number wrapping, cookie buffer flushing (i.e., forcing a round
trip every N requests made that don't have a reply), getting/setting properties
and creating a window and listening to StructureNotify events.
Code Generator
Both XCB and xpyb use the same Python module (xcbgen) for a code generator. XGB
(before this fork) used the same code generator as well, but in my attempt to
add support for more extensions, I found the code generator extremely difficult
to work with. Therefore, I re-wrote the code generator in Go. It can be found
in its own sub-package, xgbgen, of xgb. My design of xgbgen includes a rough
consideration that it could be used for other languages.
What works
I am reasonably confident that the core X protocol is in full working form. I've
also tested the Xinerama and RandR extensions sparingly. Many of the other
existing extensions have Go source generated (and are compilable) and are
included in this package, but I am currently unsure of their status. They
*should* work.
What does not work
XKB is the only extension that intentionally does not work, although I suspect
that GLX also does not work (however, there is Go source code for GLX that
compiles, unlike XKB). I don't currently have any intention of getting XKB
working, due to its complexity and my current mental incapacity to test it.
*/
package xgb

715
vend/xgb/dpms/dpms.go Normal file
View File

@ -0,0 +1,715 @@
// Package dpms is the X client API for the DPMS extension.
package dpms
// This file is automatically generated from dpms.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the DPMS extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 4, "DPMS").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named DPMS could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["DPMS"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["DPMS"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["DPMS"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["DPMS"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["DPMS"] = make(map[int]xgb.NewErrorFun)
}
const (
DPMSModeOn = 0
DPMSModeStandby = 1
DPMSModeSuspend = 2
DPMSModeOff = 3
)
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// CapableCookie is a cookie used only for Capable requests.
type CapableCookie struct {
*xgb.Cookie
}
// Capable sends a checked request.
// If an error occurs, it will be returned with the reply by calling CapableCookie.Reply()
func Capable(c *xgb.Conn) CapableCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'Capable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(capableRequest(c), cookie)
return CapableCookie{cookie}
}
// CapableUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func CapableUnchecked(c *xgb.Conn) CapableCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'Capable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(capableRequest(c), cookie)
return CapableCookie{cookie}
}
// CapableReply represents the data returned from a Capable request.
type CapableReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
Capable bool
// padding: 23 bytes
}
// Reply blocks and returns the reply data for a Capable request.
func (cook CapableCookie) Reply() (*CapableReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return capableReply(buf), nil
}
// capableReply reads a byte slice into a CapableReply value.
func capableReply(buf []byte) *CapableReply {
v := new(CapableReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
if buf[b] == 1 {
v.Capable = true
} else {
v.Capable = false
}
b += 1
b += 23 // padding
return v
}
// Write request to wire for Capable
// capableRequest writes a Capable request to a byte slice.
func capableRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DPMS"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 1 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}
// DisableCookie is a cookie used only for Disable requests.
type DisableCookie struct {
*xgb.Cookie
}
// Disable sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func Disable(c *xgb.Conn) DisableCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'Disable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(disableRequest(c), cookie)
return DisableCookie{cookie}
}
// DisableChecked sends a checked request.
// If an error occurs, it can be retrieved using DisableCookie.Check()
func DisableChecked(c *xgb.Conn) DisableCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'Disable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(disableRequest(c), cookie)
return DisableCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook DisableCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for Disable
// disableRequest writes a Disable request to a byte slice.
func disableRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DPMS"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 5 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}
// EnableCookie is a cookie used only for Enable requests.
type EnableCookie struct {
*xgb.Cookie
}
// Enable sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func Enable(c *xgb.Conn) EnableCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'Enable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(enableRequest(c), cookie)
return EnableCookie{cookie}
}
// EnableChecked sends a checked request.
// If an error occurs, it can be retrieved using EnableCookie.Check()
func EnableChecked(c *xgb.Conn) EnableCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'Enable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(enableRequest(c), cookie)
return EnableCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook EnableCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for Enable
// enableRequest writes a Enable request to a byte slice.
func enableRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DPMS"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 4 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}
// ForceLevelCookie is a cookie used only for ForceLevel requests.
type ForceLevelCookie struct {
*xgb.Cookie
}
// ForceLevel sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func ForceLevel(c *xgb.Conn, PowerLevel uint16) ForceLevelCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'ForceLevel' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(forceLevelRequest(c, PowerLevel), cookie)
return ForceLevelCookie{cookie}
}
// ForceLevelChecked sends a checked request.
// If an error occurs, it can be retrieved using ForceLevelCookie.Check()
func ForceLevelChecked(c *xgb.Conn, PowerLevel uint16) ForceLevelCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'ForceLevel' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(forceLevelRequest(c, PowerLevel), cookie)
return ForceLevelCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook ForceLevelCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for ForceLevel
// forceLevelRequest writes a ForceLevel request to a byte slice.
func forceLevelRequest(c *xgb.Conn, PowerLevel uint16) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DPMS"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 6 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put16(buf[b:], PowerLevel)
b += 2
return buf
}
// GetTimeoutsCookie is a cookie used only for GetTimeouts requests.
type GetTimeoutsCookie struct {
*xgb.Cookie
}
// GetTimeouts sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetTimeoutsCookie.Reply()
func GetTimeouts(c *xgb.Conn) GetTimeoutsCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'GetTimeouts' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getTimeoutsRequest(c), cookie)
return GetTimeoutsCookie{cookie}
}
// GetTimeoutsUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetTimeoutsUnchecked(c *xgb.Conn) GetTimeoutsCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'GetTimeouts' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getTimeoutsRequest(c), cookie)
return GetTimeoutsCookie{cookie}
}
// GetTimeoutsReply represents the data returned from a GetTimeouts request.
type GetTimeoutsReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
StandbyTimeout uint16
SuspendTimeout uint16
OffTimeout uint16
// padding: 18 bytes
}
// Reply blocks and returns the reply data for a GetTimeouts request.
func (cook GetTimeoutsCookie) Reply() (*GetTimeoutsReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getTimeoutsReply(buf), nil
}
// getTimeoutsReply reads a byte slice into a GetTimeoutsReply value.
func getTimeoutsReply(buf []byte) *GetTimeoutsReply {
v := new(GetTimeoutsReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.StandbyTimeout = xgb.Get16(buf[b:])
b += 2
v.SuspendTimeout = xgb.Get16(buf[b:])
b += 2
v.OffTimeout = xgb.Get16(buf[b:])
b += 2
b += 18 // padding
return v
}
// Write request to wire for GetTimeouts
// getTimeoutsRequest writes a GetTimeouts request to a byte slice.
func getTimeoutsRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DPMS"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 2 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}
// GetVersionCookie is a cookie used only for GetVersion requests.
type GetVersionCookie struct {
*xgb.Cookie
}
// GetVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetVersionCookie.Reply()
func GetVersion(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) GetVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'GetVersion' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return GetVersionCookie{cookie}
}
// GetVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetVersionUnchecked(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) GetVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'GetVersion' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return GetVersionCookie{cookie}
}
// GetVersionReply represents the data returned from a GetVersion request.
type GetVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
ServerMajorVersion uint16
ServerMinorVersion uint16
}
// Reply blocks and returns the reply data for a GetVersion request.
func (cook GetVersionCookie) Reply() (*GetVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getVersionReply(buf), nil
}
// getVersionReply reads a byte slice into a GetVersionReply value.
func getVersionReply(buf []byte) *GetVersionReply {
v := new(GetVersionReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.ServerMajorVersion = xgb.Get16(buf[b:])
b += 2
v.ServerMinorVersion = xgb.Get16(buf[b:])
b += 2
return v
}
// Write request to wire for GetVersion
// getVersionRequest writes a GetVersion request to a byte slice.
func getVersionRequest(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DPMS"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put16(buf[b:], ClientMajorVersion)
b += 2
xgb.Put16(buf[b:], ClientMinorVersion)
b += 2
return buf
}
// InfoCookie is a cookie used only for Info requests.
type InfoCookie struct {
*xgb.Cookie
}
// Info sends a checked request.
// If an error occurs, it will be returned with the reply by calling InfoCookie.Reply()
func Info(c *xgb.Conn) InfoCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'Info' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(infoRequest(c), cookie)
return InfoCookie{cookie}
}
// InfoUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func InfoUnchecked(c *xgb.Conn) InfoCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'Info' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(infoRequest(c), cookie)
return InfoCookie{cookie}
}
// InfoReply represents the data returned from a Info request.
type InfoReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
PowerLevel uint16
State bool
// padding: 21 bytes
}
// Reply blocks and returns the reply data for a Info request.
func (cook InfoCookie) Reply() (*InfoReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return infoReply(buf), nil
}
// infoReply reads a byte slice into a InfoReply value.
func infoReply(buf []byte) *InfoReply {
v := new(InfoReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.PowerLevel = xgb.Get16(buf[b:])
b += 2
if buf[b] == 1 {
v.State = true
} else {
v.State = false
}
b += 1
b += 21 // padding
return v
}
// Write request to wire for Info
// infoRequest writes a Info request to a byte slice.
func infoRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DPMS"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 7 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}
// SetTimeoutsCookie is a cookie used only for SetTimeouts requests.
type SetTimeoutsCookie struct {
*xgb.Cookie
}
// SetTimeouts sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func SetTimeouts(c *xgb.Conn, StandbyTimeout uint16, SuspendTimeout uint16, OffTimeout uint16) SetTimeoutsCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'SetTimeouts' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(setTimeoutsRequest(c, StandbyTimeout, SuspendTimeout, OffTimeout), cookie)
return SetTimeoutsCookie{cookie}
}
// SetTimeoutsChecked sends a checked request.
// If an error occurs, it can be retrieved using SetTimeoutsCookie.Check()
func SetTimeoutsChecked(c *xgb.Conn, StandbyTimeout uint16, SuspendTimeout uint16, OffTimeout uint16) SetTimeoutsCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["DPMS"]; !ok {
panic("Cannot issue request 'SetTimeouts' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(setTimeoutsRequest(c, StandbyTimeout, SuspendTimeout, OffTimeout), cookie)
return SetTimeoutsCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook SetTimeoutsCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for SetTimeouts
// setTimeoutsRequest writes a SetTimeouts request to a byte slice.
func setTimeoutsRequest(c *xgb.Conn, StandbyTimeout uint16, SuspendTimeout uint16, OffTimeout uint16) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["DPMS"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 3 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put16(buf[b:], StandbyTimeout)
b += 2
xgb.Put16(buf[b:], SuspendTimeout)
b += 2
xgb.Put16(buf[b:], OffTimeout)
b += 2
return buf
}

1821
vend/xgb/dri2/dri2.go Normal file

File diff suppressed because it is too large Load Diff

3
vend/xgb/examples/atoms/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
atoms
*.info
*.prof

View File

@ -0,0 +1,12 @@
atoms:
go build
test: atoms
./atoms --requests 500000 --cpu 1 \
--cpuprof cpu1.prof --memprof mem1.prof > atoms1.info 2>&1
./atoms --requests 500000 --cpu 2 \
--cpuprof cpu2.prof --memprof mem2.prof > atoms2.info 2>&1
./atoms --requests 500000 --cpu 3 \
--cpuprof cpu3.prof --memprof mem3.prof > atoms3.info 2>&1
./atoms --requests 500000 --cpu 6 \
--cpuprof cpu6.prof --memprof mem6.prof > atoms6.info 2>&1

View File

@ -0,0 +1,91 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"time"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
var (
flagRequests int
flagGOMAXPROCS int
flagCpuProfName string
flagMemProfName string
)
func init() {
flag.IntVar(&flagRequests, "requests", 100000, "Number of atoms to intern.")
flag.IntVar(&flagGOMAXPROCS, "cpu", 1, "Value of GOMAXPROCS.")
flag.StringVar(&flagCpuProfName, "cpuprof", "cpu.prof",
"Name of CPU profile file.")
flag.StringVar(&flagMemProfName, "memprof", "mem.prof",
"Name of memory profile file.")
flag.Parse()
runtime.GOMAXPROCS(flagGOMAXPROCS)
}
func seqNames(n int) []string {
names := make([]string, n)
for i := range names {
names[i] = fmt.Sprintf("NAME%d", i)
}
return names
}
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++ {
cookies[i] = xproto.InternAtom(X,
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)
}

View File

@ -0,0 +1,148 @@
// Example create-window shows how to create a window, map it, resize it,
// and listen to structure and key events (i.e., when the window is resized
// by the window manager, or when key presses/releases are made when the
// window has focus). The events are printed to stdout.
package main
import (
"fmt"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
func main() {
X, err := xgb.NewConn()
if err != nil {
fmt.Println(err)
return
}
defer X.Close()
// xproto.Setup retrieves the Setup information from the setup bytes
// gathered during connection.
setup := xproto.Setup(X)
// This is the default screen with all its associated info.
screen := setup.DefaultScreen(X)
// Any time a new resource (i.e., a window, pixmap, graphics context, etc.)
// is created, we need to generate a resource identifier.
// If the resource is a window, then use xproto.NewWindowId. If it's for
// a pixmap, then use xproto.NewPixmapId. And so on...
wid, _ := xproto.NewWindowId(X)
// CreateWindow takes a boatload of parameters.
xproto.CreateWindow(X, screen.RootDepth, wid, screen.Root,
0, 0, 500, 500, 0,
xproto.WindowClassInputOutput, screen.RootVisual, 0, []uint32{})
// This call to ChangeWindowAttributes could be factored out and
// included with the above CreateWindow call, but it is left here for
// instructive purposes. It tells X to send us events when the 'structure'
// of the window is changed (i.e., when it is resized, mapped, unmapped,
// etc.) and when a key press or a key release has been made when the
// window has focus.
// We also set the 'BackPixel' to white so that the window isn't butt ugly.
xproto.ChangeWindowAttributes(X, wid,
xproto.CwBackPixel|xproto.CwEventMask,
[]uint32{ // values must be in the order defined by the protocol
0xffffffff,
xproto.EventMaskStructureNotify |
xproto.EventMaskKeyPress |
xproto.EventMaskKeyRelease,
})
// MapWindow makes the window we've created appear on the screen.
// We demonstrated the use of a 'checked' request here.
// A checked request is a fancy way of saying, "do error handling
// synchronously." Namely, if there is a problem with the MapWindow request,
// we'll get the error *here*. If we were to do a normal unchecked
// request (like the above CreateWindow and ChangeWindowAttributes
// requests), then we would only see the error arrive in the main event
// loop.
//
// Typically, checked requests are useful when you need to make sure they
// succeed. Since they are synchronous, they incur a round trip cost before
// the program can continue, but this is only going to be noticeable if
// you're issuing tons of requests in succession.
//
// Note that requests without replies are by default unchecked while
// requests *with* replies are checked by default.
err = xproto.MapWindowChecked(X, wid).Check()
if err != nil {
fmt.Printf("Checked Error for mapping window %d: %s\n", wid, err)
} else {
fmt.Printf("Map window %d successful!\n", wid)
}
// This is an example of an invalid MapWindow request and what an error
// looks like.
err = xproto.MapWindowChecked(X, 0).Check()
if err != nil {
fmt.Printf("Checked Error for mapping window 0x1: %s\n", err)
} else { // neva
fmt.Printf("Map window 0x1 successful!\n")
}
// Start the main event loop.
for {
// WaitForEvent either returns an event or an error and never both.
// If both are nil, then something went wrong and the loop should be
// halted.
//
// An error can only be seen here as a response to an unchecked
// request.
ev, xerr := X.WaitForEvent()
if ev == nil && xerr == nil {
fmt.Println("Both event and error are nil. Exiting...")
return
}
if ev != nil {
fmt.Printf("Event: %s\n", ev)
}
if xerr != nil {
fmt.Printf("Error: %s\n", xerr)
}
// This is how accepting events work:
// The application checks what event we got and reacts to it
// accordingly. All events are defined in the xproto subpackage.
// To receive events, we have to first register it using
// either xproto.CreateWindow or xproto.ChangeWindowAttributes.
switch ev.(type) {
case xproto.KeyPressEvent:
// See https://pkg.go.dev/github.com/jezek/xgb/xproto#KeyPressEvent
// for documentation about a key press event.
kpe := ev.(xproto.KeyPressEvent)
fmt.Printf("Key pressed: %d\n", kpe.Detail)
// The Detail value depends on the keyboard layout,
// for QWERTY, q is #24.
if kpe.Detail == 24 {
return // exit on q
}
case xproto.DestroyNotifyEvent:
// Depending on the user's desktop environment (especially
// window manager), killing a window might close the
// client's X connection (e. g. the default Ubuntu
// desktop environment).
//
// If that's the case for your environment, closing this example's window
// will also close the underlying Go program (because closing the X
// connection gives a nil event and EOF error).
//
// Consider how a single application might have multiple windows
// (e.g. an open popup or dialog, ...)
//
// With other DEs, the X connection will still stay open even after the
// X window is closed. For these DEs (e.g. i3) we have to check whether
// the WM sent us a DestroyNotifyEvent and close our program.
//
// For more information about closing windows while maintaining
// the X connection see
// https://github.com/jezek/xgbutil/blob/master/_examples/graceful-window-close/main.go
return
}
}
}

21
vend/xgb/examples/doc.go Normal file
View File

@ -0,0 +1,21 @@
/*
Package examples contains a few different use cases of XGB, like creating
a window, reading properties, and querying for information about multiple
heads using the Xinerama or RandR extensions.
If you're looking to get started quickly, I recommend checking out the
create-window example first. It is the most documented and probably covers
some of the more common bare bones cases of creating windows and responding
to events.
If you're looking to query information about your window manager,
get-active-window is a start. However, to do anything extensive requires
a lot of boiler plate. To that end, I'd recommend use of a higher level
library (eg. xgbutil: https://github.com/jezek/xgbutil).
There are also examples of using the Xinerama and RandR extensions, if you're
interested in querying information about your active heads. In RandR's case,
you can also reconfigure your heads, but the example doesn't cover that.
*/
package documentation

View File

@ -0,0 +1,61 @@
// 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/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
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"
activeAtom, err := xproto.InternAtom(X, 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 := xproto.InternAtom(X, 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 := xproto.GetProperty(X, false, root, activeAtom.Atom,
xproto.GetPropertyTypeAny, 0, (1<<32)-1).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.
reply, err = xproto.GetProperty(X, false, windowId, nameAtom.Atom,
xproto.GetPropertyTypeAny, 0, (1<<32)-1).Reply()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Active window name: %s\n", string(reply.Value))
}

View File

@ -0,0 +1,92 @@
// Example randr uses the randr protocol to get information about the active
// heads. It also listens for events that are sent when the head configuration
// changes. Since it listens to events, you'll have to manually kill this
// process when you're done (i.e., ctrl+c.)
//
// While this program is running, if you use 'xrandr' to reconfigure your
// heads, you should see event information dumped to standard out.
//
// For more information, please see the RandR protocol spec:
// http://www.x.org/releases/X11R7.6/doc/randrproto/randrproto.txt
package main
import (
"fmt"
"log"
"github.com/jezek/xgb"
"github.com/jezek/xgb/randr"
"github.com/jezek/xgb/xproto"
)
func main() {
X, _ := xgb.NewConn()
// Every extension must be initialized before it can be used.
err := randr.Init(X)
if err != nil {
log.Fatal(err)
}
// Get the root window on the default screen.
root := xproto.Setup(X).DefaultScreen(X).Root
// Gets the current screen resources. Screen resources contains a list
// of names, crtcs, outputs and modes, among other things.
resources, err := randr.GetScreenResources(X, root).Reply()
if err != nil {
log.Fatal(err)
}
// Iterate through all of the outputs and show some of their info.
for _, output := range resources.Outputs {
info, err := randr.GetOutputInfo(X, output, 0).Reply()
if err != nil {
log.Fatal(err)
}
if info.Connection == randr.ConnectionConnected {
bestMode := info.Modes[0]
for _, mode := range resources.Modes {
if mode.Id == uint32(bestMode) {
fmt.Printf("Width: %d, Height: %d\n",
mode.Width, mode.Height)
}
}
}
}
fmt.Println("")
// Iterate through all of the crtcs and show some of their info.
for _, crtc := range resources.Crtcs {
info, err := randr.GetCrtcInfo(X, crtc, 0).Reply()
if err != nil {
log.Fatal(err)
}
fmt.Printf("X: %d, Y: %d, Width: %d, Height: %d\n",
info.X, info.Y, info.Width, info.Height)
}
// Tell RandR to send us events. (I think these are all of them, as of 1.3.)
err = randr.SelectInputChecked(X, root,
randr.NotifyMaskScreenChange|
randr.NotifyMaskCrtcChange|
randr.NotifyMaskOutputChange|
randr.NotifyMaskOutputProperty).Check()
if err != nil {
log.Fatal(err)
}
// Listen to events and just dump them to standard out.
// A more involved approach will have to read the 'U' field of
// RandrNotifyEvent, which is a union (really a struct) of type
// RanrNotifyDataUnion.
for {
ev, err := X.WaitForEvent()
if err != nil {
log.Fatal(err)
}
fmt.Println(ev)
}
}

View File

@ -0,0 +1,306 @@
// The shapes example shows how to draw basic shapes into a window.
// It can be considered the Go equivalent of
// https://x.org/releases/X11R7.5/doc/libxcb/tutorial/#drawingprim
// Four points, a single polyline, two line segments,
// two rectangles and two arcs are drawn.
// In addition to this, we will also write some text
// and fill a rectangle.
package main
import (
"fmt"
"unicode/utf16"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
func main() {
X, err := xgb.NewConn()
if err != nil {
fmt.Println("error connecting to X:", err)
return
}
defer X.Close()
setup := xproto.Setup(X)
screen := setup.DefaultScreen(X)
wid, err := xproto.NewWindowId(X)
if err != nil {
fmt.Println("error creating window id:", err)
return
}
draw := xproto.Drawable(wid) // for now, we simply draw into the window
// Create the window
xproto.CreateWindow(X, screen.RootDepth, wid, screen.Root,
0, 0, 180, 200, 8, // X, Y, width, height, *border width*
xproto.WindowClassInputOutput, screen.RootVisual,
xproto.CwBackPixel|xproto.CwEventMask,
[]uint32{screen.WhitePixel, xproto.EventMaskStructureNotify | xproto.EventMaskExposure})
// Map the window on the screen
xproto.MapWindow(X, wid)
// Up to here everything is the same as in the `create-window` example.
// We opened a connection, created and mapped the window.
// But this time we'll be drawing some basic shapes.
// Note how this time the border width is set to 8 instead of 0.
//
// First of all we need to create a context to draw with.
// The graphics context combines all properties (e.g. color, line width, font, fill style, ...)
// that should be used to draw something. All available properties
//
// These properties can be set by or'ing their keys (xproto.Gc*)
// and adding the value to the end of the values array.
// The order in which the values have to be given corresponds to the order that they defined
// mentioned in `xproto`.
//
// Here we create a new graphics context
// which only has the foreground (color) value set to black:
foreground, err := xproto.NewGcontextId(X)
if err != nil {
fmt.Println("error creating foreground context:", err)
return
}
mask := uint32(xproto.GcForeground)
values := []uint32{screen.BlackPixel}
xproto.CreateGC(X, foreground, draw, mask, values)
// It is possible to set the foreground value to something different.
// In production, this should use xorg color maps instead for compatibility
// but for demonstration setting the color directly also works.
// For more information on color maps, see the xcb documentation:
// https://x.org/releases/X11R7.5/doc/libxcb/tutorial/#usecolor
red, err := xproto.NewGcontextId(X)
if err != nil {
fmt.Println("error creating red context:", err)
return
}
mask = uint32(xproto.GcForeground)
values = []uint32{0xff0000}
xproto.CreateGC(X, red, draw, mask, values)
// We'll create another graphics context that draws thick lines:
thick, err := xproto.NewGcontextId(X)
if err != nil {
fmt.Println("error creating thick context:", err)
return
}
mask = uint32(xproto.GcLineWidth)
values = []uint32{10}
xproto.CreateGC(X, thick, draw, mask, values)
// It is even possible to set multiple properties at once.
// Only remember to put the values in the same order as they're
// defined in `xproto`:
// Foreground is defined first, so we also set it's value first.
// LineWidth comes second.
blue, err := xproto.NewGcontextId(X)
if err != nil {
fmt.Println("error creating blue context:", err)
return
}
mask = uint32(xproto.GcForeground | xproto.GcLineWidth)
values = []uint32{0x0000ff, 4}
xproto.CreateGC(X, blue, draw, mask, values)
// Properties of an already created gc can also be changed
// if the original values aren't needed anymore.
// In this case, we will change the line width
// and cap (line corner) style of our foreground context,
// to smooth out the polyline:
mask = uint32(xproto.GcLineWidth | xproto.GcCapStyle)
values = []uint32{3, xproto.CapStyleRound}
xproto.ChangeGC(X, foreground, mask, values)
// Writing text needs a bit more setup -- we first have
// to open the required font.
font, err := xproto.NewFontId(X)
if err != nil {
fmt.Println("error creating font id:", err)
return
}
// The font identifier that has to be passed to X for opening the font
// sets all font properties:
// publisher-family-weight-slant-width-adstyl-pxlsz-ptSz-resx-resy-spc-avgWidth-registry-encoding
// For all available fonts, install and run xfontsel.
//
// To load any available font, set all fields to an asterisk.
// To specify a font, set one or multiple fields.
// This can also be seen in xfontsel -- initially every field is set to *,
// however, the more fields are set, the fewer fonts match.
//
// Using a specific font (e.g. Gnu Unifont) can be as easy as
// "-gnu-unifont-*-*-*-*-16-*-*-*-*-*-*-*"
//
// To load any font that is encoded for usage
// with Unicode characters, one would use
// fontname := "-*-*-*-*-*-*-14-*-*-*-*-*-iso10646-1"
//
// For now, we'll simply stick with the fixed font which is available
// to every X session:
fontname := "-*-fixed-*-*-*-*-14-*-*-*-*-*-*-*"
err = xproto.OpenFontChecked(X, font, uint16(len(fontname)), fontname).Check()
if err != nil {
fmt.Println("failed opening the font:", err)
return
}
// And create a context from it. We simply pass the font's ID to the GcFont property.
textCtx, err := xproto.NewGcontextId(X)
if err != nil {
fmt.Println("error creating text context:", err)
return
}
mask = uint32(xproto.GcForeground | xproto.GcBackground | xproto.GcFont)
values = []uint32{screen.BlackPixel, screen.WhitePixel, uint32(font)}
xproto.CreateGC(X, textCtx, draw, mask, values)
text := convertStringToChar2b("Hellö World!") // Unicode capable!
// Close the font handle:
xproto.CloseFont(X, font)
// After all, writing text is way more comfortable using Xft - it supports TrueType,
// and overall better configuration.
points := []xproto.Point{
{X: 10, Y: 10},
{X: 20, Y: 10},
{X: 30, Y: 10},
{X: 40, Y: 10},
}
// A polyline is essentially a line with multiple points.
// The first point is placed absolutely inside the window,
// while every other point is placed relative to the one before it.
polyline := []xproto.Point{
{X: 50, Y: 10},
{X: 5, Y: 20}, // move 5 to the right, 20 down
{X: 25, Y: -20}, // move 25 to the right, 20 up - notice how this point is level again with the first point
{X: 10, Y: 10}, // move 10 to the right, 10 down
}
segments := []xproto.Segment{
{X1: 100, Y1: 10, X2: 140, Y2: 30},
{X1: 110, Y1: 25, X2: 130, Y2: 60},
{X1: 0, Y1: 160, X2: 90, Y2: 100},
}
// Rectangles have a start coordinate (upper left) and width and height.
rectangles := []xproto.Rectangle{
{X: 10, Y: 50, Width: 40, Height: 20},
{X: 80, Y: 50, Width: 10, Height: 40},
}
// This rectangle we will use to demonstrate filling a shape.
rectangles2 := []xproto.Rectangle{
{X: 150, Y: 50, Width: 20, Height: 60},
}
// Arcs are defined by a top left position (notice where the third line goes to)
// their width and height, a starting and end angle.
// Angles are defined in units of 1/64 of a single degree,
// so we have to multiply the degrees by 64 (or left shift them by 6).
arcs := []xproto.Arc{
{X: 10, Y: 100, Width: 60, Height: 40, Angle1: 0 << 6, Angle2: 90 << 6},
{X: 90, Y: 100, Width: 55, Height: 40, Angle1: 20 << 6, Angle2: 270 << 6},
}
for {
evt, err := X.WaitForEvent()
if err != nil {
fmt.Println("error reading event:", err)
return
} else if evt == nil {
return
}
switch evt.(type) {
case xproto.ExposeEvent:
// Draw the four points we specified earlier.
// Notice how we use the `foreground` context to draw them in black.
// Also notice how even though we changed the line width to 3,
// these still only appear as a single pixel.
// To draw points that are bigger than a single pixel,
// one has to either fill rectangles, circles or polygons.
xproto.PolyPoint(X, xproto.CoordModeOrigin, draw, foreground, points)
// Draw the polyline. This time we specified `xproto.CoordModePrevious`,
// which means that every point is placed relatively to the previous.
// If we were to use `xproto.CoordModeOrigin` instead,
// we could specify each point absolutely on the screen.
// It is also possible to use `xproto.CoordModePrevious` for drawing *points*
// which means that each point would be specified relative to the previous one,
// just as we did with the polyline.
xproto.PolyLine(X, xproto.CoordModePrevious, draw, foreground, polyline)
// Draw two lines in red.
xproto.PolySegment(X, draw, red, segments)
// Draw two thick rectangles.
// The line width only specifies the width of the outline.
// Notice how the second rectangle gets completely filled
// due to the line width.
xproto.PolyRectangle(X, draw, thick, rectangles)
// Draw the circular arcs in blue.
xproto.PolyArc(X, draw, blue, arcs)
// There's also a fill variant for all drawing commands:
xproto.PolyFillRectangle(X, draw, red, rectangles2)
// Draw the text. Xorg currently knows two ways of specifying text:
// a) the (extended) ASCII encoding using ImageText8(..., []byte)
// b) UTF16 encoding using ImageText16(..., []Char2b) -- Char2b is
// a structure consisting of two bytes.
// At the bottom of this example, there are two utility functions that help
// convert a go string into an array of Char2b's.
xproto.ImageText16(X, byte(len(text)), draw, textCtx, 10, 160, text)
case xproto.DestroyNotifyEvent:
return
}
}
}
// Char2b is defined as
// Byte1 byte
// Byte2 byte
// and is used as a utf16 character.
// This function takes a string and converts each rune into a char2b.
func convertStringToChar2b(s string) []xproto.Char2b {
var chars []xproto.Char2b
var p []uint16
for _, r := range []rune(s) {
p = utf16.Encode([]rune{r})
if len(p) == 1 {
chars = append(chars, convertUint16ToChar2b(p[0]))
} else {
// If the utf16 representation is larger than 2 bytes
// we can not use it and insert a blank instead:
chars = append(chars, xproto.Char2b{Byte1: 0, Byte2: 32})
}
}
return chars
}
// convertUint16ToChar2b converts a uint16 (which is basically two bytes)
// into a Char2b by using the higher 8 bits of u as Byte1
// and the lower 8 bits of u as Byte2.
func convertUint16ToChar2b(u uint16) xproto.Char2b {
return xproto.Char2b{
Byte1: byte((u & 0xff00) >> 8),
Byte2: byte((u & 0x00ff)),
}
}

View File

@ -0,0 +1,40 @@
// Example xinerama shows how to query the geometry of all active heads.
package main
import (
"fmt"
"log"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xinerama"
)
func main() {
X, err := xgb.NewConn()
if err != nil {
log.Fatal(err)
}
// Initialize the Xinerama extension.
// The appropriate 'Init' function must be run for *every*
// extension before any of its requests can be used.
err = xinerama.Init(X)
if err != nil {
log.Fatal(err)
}
// Issue a request to get the screen information.
reply, err := xinerama.QueryScreens(X).Reply()
if err != nil {
log.Fatal(err)
}
// reply.Number is the number of active heads, while reply.ScreenInfo
// is a slice of XineramaScreenInfo containing the rectangle geometry
// of each head.
fmt.Printf("Number of heads: %d\n", reply.Number)
for i, screen := range reply.ScreenInfo {
fmt.Printf("%d :: X: %d, Y: %d, Width: %d, Height: %d\n",
i, screen.XOrg, screen.YOrg, screen.Width, screen.Height)
}
}

165
vend/xgb/ge/ge.go Normal file
View File

@ -0,0 +1,165 @@
// Package ge is the X client API for the Generic Event Extension extension.
package ge
// This file is automatically generated from ge.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the Generic Event Extension extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 23, "Generic Event Extension").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named Generic Event Extension could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["Generic Event Extension"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["Generic Event Extension"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["Generic Event Extension"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["Generic Event Extension"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["Generic Event Extension"] = make(map[int]xgb.NewErrorFun)
}
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// QueryVersionCookie is a cookie used only for QueryVersion requests.
type QueryVersionCookie struct {
*xgb.Cookie
}
// QueryVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling QueryVersionCookie.Reply()
func QueryVersion(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Generic Event Extension"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'Generic Event Extension'. ge.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func QueryVersionUnchecked(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["Generic Event Extension"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'Generic Event Extension'. ge.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionReply represents the data returned from a QueryVersion request.
type QueryVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
MajorVersion uint16
MinorVersion uint16
// padding: 20 bytes
}
// Reply blocks and returns the reply data for a QueryVersion request.
func (cook QueryVersionCookie) Reply() (*QueryVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return queryVersionReply(buf), nil
}
// queryVersionReply reads a byte slice into a QueryVersionReply value.
func queryVersionReply(buf []byte) *QueryVersionReply {
v := new(QueryVersionReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.MajorVersion = xgb.Get16(buf[b:])
b += 2
v.MinorVersion = xgb.Get16(buf[b:])
b += 2
b += 20 // padding
return v
}
// Write request to wire for QueryVersion
// queryVersionRequest writes a QueryVersion request to a byte slice.
func queryVersionRequest(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["Generic Event Extension"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put16(buf[b:], ClientMajorVersion)
b += 2
xgb.Put16(buf[b:], ClientMinorVersion)
b += 2
return buf
}

10930
vend/xgb/glx/glx.go Normal file

File diff suppressed because it is too large Load Diff

3
vend/xgb/go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/jezek/xgb
go 1.11

105
vend/xgb/help.go Normal file
View File

@ -0,0 +1,105 @@
package xgb
/*
help.go is meant to contain a rough hodge podge of functions that are mainly
used in the auto generated code. Indeed, several functions here are simple
wrappers so that the sub-packages don't need to be smart about which stdlib
packages to import.
Also, the 'Get..' and 'Put..' functions are used through the core xgb package
too. (xgbutil uses them too.)
*/
import (
"fmt"
"strings"
)
// StringsJoin is an alias to strings.Join. It allows us to avoid having to
// import 'strings' in each of the generated Go files.
func StringsJoin(ss []string, sep string) string {
return strings.Join(ss, sep)
}
// Sprintf is so we don't need to import 'fmt' in the generated Go files.
func Sprintf(format string, v ...interface{}) string {
return fmt.Sprintf(format, v...)
}
// Errorf is just a wrapper for fmt.Errorf. Exists for the same reason
// that 'stringsJoin' and 'sprintf' exists.
func Errorf(format string, v ...interface{}) error {
return fmt.Errorf(format, v...)
}
// Pad a length to align on 4 bytes.
func Pad(n int) int {
return (n + 3) & ^3
}
// PopCount counts the number of bits set in a value list mask.
func PopCount(mask0 int) int {
mask := uint32(mask0)
n := 0
for i := uint32(0); i < 32; i++ {
if mask&(1<<i) != 0 {
n++
}
}
return n
}
// Put16 takes a 16 bit integer and copies it into a byte slice.
func Put16(buf []byte, v uint16) {
buf[0] = byte(v)
buf[1] = byte(v >> 8)
}
// Put32 takes a 32 bit integer and copies it into a byte slice.
func Put32(buf []byte, v uint32) {
buf[0] = byte(v)
buf[1] = byte(v >> 8)
buf[2] = byte(v >> 16)
buf[3] = byte(v >> 24)
}
// Put64 takes a 64 bit integer and copies it into a byte slice.
func Put64(buf []byte, v uint64) {
buf[0] = byte(v)
buf[1] = byte(v >> 8)
buf[2] = byte(v >> 16)
buf[3] = byte(v >> 24)
buf[4] = byte(v >> 32)
buf[5] = byte(v >> 40)
buf[6] = byte(v >> 48)
buf[7] = byte(v >> 56)
}
// Get16 constructs a 16 bit integer from the beginning of a byte slice.
func Get16(buf []byte) uint16 {
v := uint16(buf[0])
v |= uint16(buf[1]) << 8
return v
}
// Get32 constructs a 32 bit integer from the beginning of a byte slice.
func Get32(buf []byte) uint32 {
v := uint32(buf[0])
v |= uint32(buf[1]) << 8
v |= uint32(buf[2]) << 16
v |= uint32(buf[3]) << 24
return v
}
// Get64 constructs a 64 bit integer from the beginning of a byte slice.
func Get64(buf []byte) uint64 {
v := uint64(buf[0])
v |= uint64(buf[1]) << 8
v |= uint64(buf[2]) << 16
v |= uint64(buf[3]) << 24
v |= uint64(buf[4]) << 32
v |= uint64(buf[5]) << 40
v |= uint64(buf[6]) << 48
v |= uint64(buf[7]) << 56
return v
}

6607
vend/xgb/randr/randr.go Normal file

File diff suppressed because it is too large Load Diff

1204
vend/xgb/record/record.go Normal file

File diff suppressed because it is too large Load Diff

4029
vend/xgb/render/render.go Normal file

File diff suppressed because it is too large Load Diff

1110
vend/xgb/res/res.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,690 @@
// Package screensaver is the X client API for the MIT-SCREEN-SAVER extension.
package screensaver
// This file is automatically generated from screensaver.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the MIT-SCREEN-SAVER extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 16, "MIT-SCREEN-SAVER").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named MIT-SCREEN-SAVER could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["MIT-SCREEN-SAVER"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["MIT-SCREEN-SAVER"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["MIT-SCREEN-SAVER"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["MIT-SCREEN-SAVER"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["MIT-SCREEN-SAVER"] = make(map[int]xgb.NewErrorFun)
}
const (
EventNotifyMask = 1
EventCycleMask = 2
)
const (
KindBlanked = 0
KindInternal = 1
KindExternal = 2
)
// Notify is the event number for a NotifyEvent.
const Notify = 0
type NotifyEvent struct {
Sequence uint16
State byte
Time xproto.Timestamp
Root xproto.Window
Window xproto.Window
Kind byte
Forced bool
// padding: 14 bytes
}
// NotifyEventNew constructs a NotifyEvent value that implements xgb.Event from a byte slice.
func NotifyEventNew(buf []byte) xgb.Event {
v := NotifyEvent{}
b := 1 // don't read event number
v.State = buf[b]
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Time = xproto.Timestamp(xgb.Get32(buf[b:]))
b += 4
v.Root = xproto.Window(xgb.Get32(buf[b:]))
b += 4
v.Window = xproto.Window(xgb.Get32(buf[b:]))
b += 4
v.Kind = buf[b]
b += 1
if buf[b] == 1 {
v.Forced = true
} else {
v.Forced = false
}
b += 1
b += 14 // padding
return v
}
// Bytes writes a NotifyEvent value to a byte slice.
func (v NotifyEvent) Bytes() []byte {
buf := make([]byte, 32)
b := 0
// write event number
buf[b] = 0
b += 1
buf[b] = v.State
b += 1
b += 2 // skip sequence number
xgb.Put32(buf[b:], uint32(v.Time))
b += 4
xgb.Put32(buf[b:], uint32(v.Root))
b += 4
xgb.Put32(buf[b:], uint32(v.Window))
b += 4
buf[b] = v.Kind
b += 1
if v.Forced {
buf[b] = 1
} else {
buf[b] = 0
}
b += 1
b += 14 // padding
return buf
}
// SequenceId returns the sequence id attached to the Notify event.
// Events without a sequence number (KeymapNotify) return 0.
// This is mostly used internally.
func (v NotifyEvent) SequenceId() uint16 {
return v.Sequence
}
// String is a rudimentary string representation of NotifyEvent.
func (v NotifyEvent) String() string {
fieldVals := make([]string, 0, 7)
fieldVals = append(fieldVals, xgb.Sprintf("Sequence: %d", v.Sequence))
fieldVals = append(fieldVals, xgb.Sprintf("State: %d", v.State))
fieldVals = append(fieldVals, xgb.Sprintf("Time: %d", v.Time))
fieldVals = append(fieldVals, xgb.Sprintf("Root: %d", v.Root))
fieldVals = append(fieldVals, xgb.Sprintf("Window: %d", v.Window))
fieldVals = append(fieldVals, xgb.Sprintf("Kind: %d", v.Kind))
fieldVals = append(fieldVals, xgb.Sprintf("Forced: %t", v.Forced))
return "Notify {" + xgb.StringsJoin(fieldVals, ", ") + "}"
}
func init() {
xgb.NewExtEventFuncs["MIT-SCREEN-SAVER"][0] = NotifyEventNew
}
const (
StateOff = 0
StateOn = 1
StateCycle = 2
StateDisabled = 3
)
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// QueryInfoCookie is a cookie used only for QueryInfo requests.
type QueryInfoCookie struct {
*xgb.Cookie
}
// QueryInfo sends a checked request.
// If an error occurs, it will be returned with the reply by calling QueryInfoCookie.Reply()
func QueryInfo(c *xgb.Conn, Drawable xproto.Drawable) QueryInfoCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'QueryInfo' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(queryInfoRequest(c, Drawable), cookie)
return QueryInfoCookie{cookie}
}
// QueryInfoUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func QueryInfoUnchecked(c *xgb.Conn, Drawable xproto.Drawable) QueryInfoCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'QueryInfo' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(queryInfoRequest(c, Drawable), cookie)
return QueryInfoCookie{cookie}
}
// QueryInfoReply represents the data returned from a QueryInfo request.
type QueryInfoReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
State byte
SaverWindow xproto.Window
MsUntilServer uint32
MsSinceUserInput uint32
EventMask uint32
Kind byte
// padding: 7 bytes
}
// Reply blocks and returns the reply data for a QueryInfo request.
func (cook QueryInfoCookie) Reply() (*QueryInfoReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return queryInfoReply(buf), nil
}
// queryInfoReply reads a byte slice into a QueryInfoReply value.
func queryInfoReply(buf []byte) *QueryInfoReply {
v := new(QueryInfoReply)
b := 1 // skip reply determinant
v.State = buf[b]
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.SaverWindow = xproto.Window(xgb.Get32(buf[b:]))
b += 4
v.MsUntilServer = xgb.Get32(buf[b:])
b += 4
v.MsSinceUserInput = xgb.Get32(buf[b:])
b += 4
v.EventMask = xgb.Get32(buf[b:])
b += 4
v.Kind = buf[b]
b += 1
b += 7 // padding
return v
}
// Write request to wire for QueryInfo
// queryInfoRequest writes a QueryInfo request to a byte slice.
func queryInfoRequest(c *xgb.Conn, Drawable xproto.Drawable) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SCREEN-SAVER"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 1 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Drawable))
b += 4
return buf
}
// QueryVersionCookie is a cookie used only for QueryVersion requests.
type QueryVersionCookie struct {
*xgb.Cookie
}
// QueryVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling QueryVersionCookie.Reply()
func QueryVersion(c *xgb.Conn, ClientMajorVersion byte, ClientMinorVersion byte) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func QueryVersionUnchecked(c *xgb.Conn, ClientMajorVersion byte, ClientMinorVersion byte) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionReply represents the data returned from a QueryVersion request.
type QueryVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
ServerMajorVersion uint16
ServerMinorVersion uint16
// padding: 20 bytes
}
// Reply blocks and returns the reply data for a QueryVersion request.
func (cook QueryVersionCookie) Reply() (*QueryVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return queryVersionReply(buf), nil
}
// queryVersionReply reads a byte slice into a QueryVersionReply value.
func queryVersionReply(buf []byte) *QueryVersionReply {
v := new(QueryVersionReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.ServerMajorVersion = xgb.Get16(buf[b:])
b += 2
v.ServerMinorVersion = xgb.Get16(buf[b:])
b += 2
b += 20 // padding
return v
}
// Write request to wire for QueryVersion
// queryVersionRequest writes a QueryVersion request to a byte slice.
func queryVersionRequest(c *xgb.Conn, ClientMajorVersion byte, ClientMinorVersion byte) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SCREEN-SAVER"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
buf[b] = ClientMajorVersion
b += 1
buf[b] = ClientMinorVersion
b += 1
b += 2 // padding
return buf
}
// SelectInputCookie is a cookie used only for SelectInput requests.
type SelectInputCookie struct {
*xgb.Cookie
}
// SelectInput sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func SelectInput(c *xgb.Conn, Drawable xproto.Drawable, EventMask uint32) SelectInputCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'SelectInput' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(selectInputRequest(c, Drawable, EventMask), cookie)
return SelectInputCookie{cookie}
}
// SelectInputChecked sends a checked request.
// If an error occurs, it can be retrieved using SelectInputCookie.Check()
func SelectInputChecked(c *xgb.Conn, Drawable xproto.Drawable, EventMask uint32) SelectInputCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'SelectInput' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(selectInputRequest(c, Drawable, EventMask), cookie)
return SelectInputCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook SelectInputCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for SelectInput
// selectInputRequest writes a SelectInput request to a byte slice.
func selectInputRequest(c *xgb.Conn, Drawable xproto.Drawable, EventMask uint32) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SCREEN-SAVER"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 2 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Drawable))
b += 4
xgb.Put32(buf[b:], EventMask)
b += 4
return buf
}
// SetAttributesCookie is a cookie used only for SetAttributes requests.
type SetAttributesCookie struct {
*xgb.Cookie
}
// SetAttributes sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func SetAttributes(c *xgb.Conn, Drawable xproto.Drawable, X int16, Y int16, Width uint16, Height uint16, BorderWidth uint16, Class byte, Depth byte, Visual xproto.Visualid, ValueMask uint32, ValueList []uint32) SetAttributesCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'SetAttributes' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(setAttributesRequest(c, Drawable, X, Y, Width, Height, BorderWidth, Class, Depth, Visual, ValueMask, ValueList), cookie)
return SetAttributesCookie{cookie}
}
// SetAttributesChecked sends a checked request.
// If an error occurs, it can be retrieved using SetAttributesCookie.Check()
func SetAttributesChecked(c *xgb.Conn, Drawable xproto.Drawable, X int16, Y int16, Width uint16, Height uint16, BorderWidth uint16, Class byte, Depth byte, Visual xproto.Visualid, ValueMask uint32, ValueList []uint32) SetAttributesCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'SetAttributes' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(setAttributesRequest(c, Drawable, X, Y, Width, Height, BorderWidth, Class, Depth, Visual, ValueMask, ValueList), cookie)
return SetAttributesCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook SetAttributesCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for SetAttributes
// setAttributesRequest writes a SetAttributes request to a byte slice.
func setAttributesRequest(c *xgb.Conn, Drawable xproto.Drawable, X int16, Y int16, Width uint16, Height uint16, BorderWidth uint16, Class byte, Depth byte, Visual xproto.Visualid, ValueMask uint32, ValueList []uint32) []byte {
size := xgb.Pad((28 + xgb.Pad((4 * xgb.PopCount(int(ValueMask))))))
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SCREEN-SAVER"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 3 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Drawable))
b += 4
xgb.Put16(buf[b:], uint16(X))
b += 2
xgb.Put16(buf[b:], uint16(Y))
b += 2
xgb.Put16(buf[b:], Width)
b += 2
xgb.Put16(buf[b:], Height)
b += 2
xgb.Put16(buf[b:], BorderWidth)
b += 2
buf[b] = Class
b += 1
buf[b] = Depth
b += 1
xgb.Put32(buf[b:], uint32(Visual))
b += 4
xgb.Put32(buf[b:], ValueMask)
b += 4
for i := 0; i < xgb.PopCount(int(ValueMask)); i++ {
xgb.Put32(buf[b:], ValueList[i])
b += 4
}
b = xgb.Pad(b)
return buf
}
// SuspendCookie is a cookie used only for Suspend requests.
type SuspendCookie struct {
*xgb.Cookie
}
// Suspend sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func Suspend(c *xgb.Conn, Suspend uint32) SuspendCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'Suspend' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(suspendRequest(c, Suspend), cookie)
return SuspendCookie{cookie}
}
// SuspendChecked sends a checked request.
// If an error occurs, it can be retrieved using SuspendCookie.Check()
func SuspendChecked(c *xgb.Conn, Suspend uint32) SuspendCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'Suspend' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(suspendRequest(c, Suspend), cookie)
return SuspendCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook SuspendCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for Suspend
// suspendRequest writes a Suspend request to a byte slice.
func suspendRequest(c *xgb.Conn, Suspend uint32) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SCREEN-SAVER"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 5 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], Suspend)
b += 4
return buf
}
// UnsetAttributesCookie is a cookie used only for UnsetAttributes requests.
type UnsetAttributesCookie struct {
*xgb.Cookie
}
// UnsetAttributes sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func UnsetAttributes(c *xgb.Conn, Drawable xproto.Drawable) UnsetAttributesCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'UnsetAttributes' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(unsetAttributesRequest(c, Drawable), cookie)
return UnsetAttributesCookie{cookie}
}
// UnsetAttributesChecked sends a checked request.
// If an error occurs, it can be retrieved using UnsetAttributesCookie.Check()
func UnsetAttributesChecked(c *xgb.Conn, Drawable xproto.Drawable) UnsetAttributesCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SCREEN-SAVER"]; !ok {
panic("Cannot issue request 'UnsetAttributes' using the uninitialized extension 'MIT-SCREEN-SAVER'. screensaver.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(unsetAttributesRequest(c, Drawable), cookie)
return UnsetAttributesCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook UnsetAttributesCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for UnsetAttributes
// unsetAttributesRequest writes a UnsetAttributes request to a byte slice.
func unsetAttributesRequest(c *xgb.Conn, Drawable xproto.Drawable) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SCREEN-SAVER"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 4 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Drawable))
b += 4
return buf
}

1025
vend/xgb/shape/shape.go Normal file

File diff suppressed because it is too large Load Diff

945
vend/xgb/shm/shm.go Normal file
View File

@ -0,0 +1,945 @@
// Package shm is the X client API for the MIT-SHM extension.
package shm
// This file is automatically generated from shm.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the MIT-SHM extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 7, "MIT-SHM").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named MIT-SHM could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["MIT-SHM"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["MIT-SHM"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["MIT-SHM"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["MIT-SHM"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["MIT-SHM"] = make(map[int]xgb.NewErrorFun)
}
// BadBadSeg is the error number for a BadBadSeg.
const BadBadSeg = 0
type BadSegError xproto.ValueError
// BadSegErrorNew constructs a BadSegError value that implements xgb.Error from a byte slice.
func BadSegErrorNew(buf []byte) xgb.Error {
v := BadSegError(xproto.ValueErrorNew(buf).(xproto.ValueError))
v.NiceName = "BadSeg"
return v
}
// SequenceId returns the sequence id attached to the BadBadSeg error.
// This is mostly used internally.
func (err BadSegError) SequenceId() uint16 {
return err.Sequence
}
// BadId returns the 'BadValue' number if one exists for the BadBadSeg error. If no bad value exists, 0 is returned.
func (err BadSegError) BadId() uint32 {
return 0
}
// Error returns a rudimentary string representation of the BadBadSeg error.
func (err BadSegError) Error() string {
fieldVals := make([]string, 0, 4)
fieldVals = append(fieldVals, "NiceName: "+err.NiceName)
fieldVals = append(fieldVals, xgb.Sprintf("Sequence: %d", err.Sequence))
fieldVals = append(fieldVals, xgb.Sprintf("BadValue: %d", err.BadValue))
fieldVals = append(fieldVals, xgb.Sprintf("MinorOpcode: %d", err.MinorOpcode))
fieldVals = append(fieldVals, xgb.Sprintf("MajorOpcode: %d", err.MajorOpcode))
return "BadBadSeg {" + xgb.StringsJoin(fieldVals, ", ") + "}"
}
func init() {
xgb.NewExtErrorFuncs["MIT-SHM"][0] = BadSegErrorNew
}
// Completion is the event number for a CompletionEvent.
const Completion = 0
type CompletionEvent struct {
Sequence uint16
// padding: 1 bytes
Drawable xproto.Drawable
MinorEvent uint16
MajorEvent byte
// padding: 1 bytes
Shmseg Seg
Offset uint32
}
// CompletionEventNew constructs a CompletionEvent value that implements xgb.Event from a byte slice.
func CompletionEventNew(buf []byte) xgb.Event {
v := CompletionEvent{}
b := 1 // don't read event number
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Drawable = xproto.Drawable(xgb.Get32(buf[b:]))
b += 4
v.MinorEvent = xgb.Get16(buf[b:])
b += 2
v.MajorEvent = buf[b]
b += 1
b += 1 // padding
v.Shmseg = Seg(xgb.Get32(buf[b:]))
b += 4
v.Offset = xgb.Get32(buf[b:])
b += 4
return v
}
// Bytes writes a CompletionEvent value to a byte slice.
func (v CompletionEvent) Bytes() []byte {
buf := make([]byte, 32)
b := 0
// write event number
buf[b] = 0
b += 1
b += 1 // padding
b += 2 // skip sequence number
xgb.Put32(buf[b:], uint32(v.Drawable))
b += 4
xgb.Put16(buf[b:], v.MinorEvent)
b += 2
buf[b] = v.MajorEvent
b += 1
b += 1 // padding
xgb.Put32(buf[b:], uint32(v.Shmseg))
b += 4
xgb.Put32(buf[b:], v.Offset)
b += 4
return buf
}
// SequenceId returns the sequence id attached to the Completion event.
// Events without a sequence number (KeymapNotify) return 0.
// This is mostly used internally.
func (v CompletionEvent) SequenceId() uint16 {
return v.Sequence
}
// String is a rudimentary string representation of CompletionEvent.
func (v CompletionEvent) String() string {
fieldVals := make([]string, 0, 7)
fieldVals = append(fieldVals, xgb.Sprintf("Sequence: %d", v.Sequence))
fieldVals = append(fieldVals, xgb.Sprintf("Drawable: %d", v.Drawable))
fieldVals = append(fieldVals, xgb.Sprintf("MinorEvent: %d", v.MinorEvent))
fieldVals = append(fieldVals, xgb.Sprintf("MajorEvent: %d", v.MajorEvent))
fieldVals = append(fieldVals, xgb.Sprintf("Shmseg: %d", v.Shmseg))
fieldVals = append(fieldVals, xgb.Sprintf("Offset: %d", v.Offset))
return "Completion {" + xgb.StringsJoin(fieldVals, ", ") + "}"
}
func init() {
xgb.NewExtEventFuncs["MIT-SHM"][0] = CompletionEventNew
}
type Seg uint32
func NewSegId(c *xgb.Conn) (Seg, error) {
id, err := c.NewId()
if err != nil {
return 0, err
}
return Seg(id), nil
}
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// AttachCookie is a cookie used only for Attach requests.
type AttachCookie struct {
*xgb.Cookie
}
// Attach sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func Attach(c *xgb.Conn, Shmseg Seg, Shmid uint32, ReadOnly bool) AttachCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'Attach' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(attachRequest(c, Shmseg, Shmid, ReadOnly), cookie)
return AttachCookie{cookie}
}
// AttachChecked sends a checked request.
// If an error occurs, it can be retrieved using AttachCookie.Check()
func AttachChecked(c *xgb.Conn, Shmseg Seg, Shmid uint32, ReadOnly bool) AttachCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'Attach' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(attachRequest(c, Shmseg, Shmid, ReadOnly), cookie)
return AttachCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook AttachCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for Attach
// attachRequest writes a Attach request to a byte slice.
func attachRequest(c *xgb.Conn, Shmseg Seg, Shmid uint32, ReadOnly bool) []byte {
size := 16
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SHM"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 1 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Shmseg))
b += 4
xgb.Put32(buf[b:], Shmid)
b += 4
if ReadOnly {
buf[b] = 1
} else {
buf[b] = 0
}
b += 1
b += 3 // padding
return buf
}
// AttachFdCookie is a cookie used only for AttachFd requests.
type AttachFdCookie struct {
*xgb.Cookie
}
// AttachFd sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func AttachFd(c *xgb.Conn, Shmseg Seg, ReadOnly bool) AttachFdCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'AttachFd' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(attachFdRequest(c, Shmseg, ReadOnly), cookie)
return AttachFdCookie{cookie}
}
// AttachFdChecked sends a checked request.
// If an error occurs, it can be retrieved using AttachFdCookie.Check()
func AttachFdChecked(c *xgb.Conn, Shmseg Seg, ReadOnly bool) AttachFdCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'AttachFd' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(attachFdRequest(c, Shmseg, ReadOnly), cookie)
return AttachFdCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook AttachFdCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for AttachFd
// attachFdRequest writes a AttachFd request to a byte slice.
func attachFdRequest(c *xgb.Conn, Shmseg Seg, ReadOnly bool) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SHM"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 6 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Shmseg))
b += 4
if ReadOnly {
buf[b] = 1
} else {
buf[b] = 0
}
b += 1
b += 3 // padding
return buf
}
// CreatePixmapCookie is a cookie used only for CreatePixmap requests.
type CreatePixmapCookie struct {
*xgb.Cookie
}
// CreatePixmap sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func CreatePixmap(c *xgb.Conn, Pid xproto.Pixmap, Drawable xproto.Drawable, Width uint16, Height uint16, Depth byte, Shmseg Seg, Offset uint32) CreatePixmapCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'CreatePixmap' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(createPixmapRequest(c, Pid, Drawable, Width, Height, Depth, Shmseg, Offset), cookie)
return CreatePixmapCookie{cookie}
}
// CreatePixmapChecked sends a checked request.
// If an error occurs, it can be retrieved using CreatePixmapCookie.Check()
func CreatePixmapChecked(c *xgb.Conn, Pid xproto.Pixmap, Drawable xproto.Drawable, Width uint16, Height uint16, Depth byte, Shmseg Seg, Offset uint32) CreatePixmapCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'CreatePixmap' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(createPixmapRequest(c, Pid, Drawable, Width, Height, Depth, Shmseg, Offset), cookie)
return CreatePixmapCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook CreatePixmapCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for CreatePixmap
// createPixmapRequest writes a CreatePixmap request to a byte slice.
func createPixmapRequest(c *xgb.Conn, Pid xproto.Pixmap, Drawable xproto.Drawable, Width uint16, Height uint16, Depth byte, Shmseg Seg, Offset uint32) []byte {
size := 28
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SHM"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 5 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Pid))
b += 4
xgb.Put32(buf[b:], uint32(Drawable))
b += 4
xgb.Put16(buf[b:], Width)
b += 2
xgb.Put16(buf[b:], Height)
b += 2
buf[b] = Depth
b += 1
b += 3 // padding
xgb.Put32(buf[b:], uint32(Shmseg))
b += 4
xgb.Put32(buf[b:], Offset)
b += 4
return buf
}
// CreateSegmentCookie is a cookie used only for CreateSegment requests.
type CreateSegmentCookie struct {
*xgb.Cookie
}
// CreateSegment sends a checked request.
// If an error occurs, it will be returned with the reply by calling CreateSegmentCookie.Reply()
func CreateSegment(c *xgb.Conn, Shmseg Seg, Size uint32, ReadOnly bool) CreateSegmentCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'CreateSegment' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(createSegmentRequest(c, Shmseg, Size, ReadOnly), cookie)
return CreateSegmentCookie{cookie}
}
// CreateSegmentUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func CreateSegmentUnchecked(c *xgb.Conn, Shmseg Seg, Size uint32, ReadOnly bool) CreateSegmentCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'CreateSegment' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(createSegmentRequest(c, Shmseg, Size, ReadOnly), cookie)
return CreateSegmentCookie{cookie}
}
// CreateSegmentReply represents the data returned from a CreateSegment request.
type CreateSegmentReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
Nfd byte
// padding: 24 bytes
}
// Reply blocks and returns the reply data for a CreateSegment request.
func (cook CreateSegmentCookie) Reply() (*CreateSegmentReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return createSegmentReply(buf), nil
}
// createSegmentReply reads a byte slice into a CreateSegmentReply value.
func createSegmentReply(buf []byte) *CreateSegmentReply {
v := new(CreateSegmentReply)
b := 1 // skip reply determinant
v.Nfd = buf[b]
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
b += 24 // padding
return v
}
// Write request to wire for CreateSegment
// createSegmentRequest writes a CreateSegment request to a byte slice.
func createSegmentRequest(c *xgb.Conn, Shmseg Seg, Size uint32, ReadOnly bool) []byte {
size := 16
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SHM"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 7 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Shmseg))
b += 4
xgb.Put32(buf[b:], Size)
b += 4
if ReadOnly {
buf[b] = 1
} else {
buf[b] = 0
}
b += 1
b += 3 // padding
return buf
}
// DetachCookie is a cookie used only for Detach requests.
type DetachCookie struct {
*xgb.Cookie
}
// Detach sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func Detach(c *xgb.Conn, Shmseg Seg) DetachCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'Detach' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(detachRequest(c, Shmseg), cookie)
return DetachCookie{cookie}
}
// DetachChecked sends a checked request.
// If an error occurs, it can be retrieved using DetachCookie.Check()
func DetachChecked(c *xgb.Conn, Shmseg Seg) DetachCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'Detach' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(detachRequest(c, Shmseg), cookie)
return DetachCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook DetachCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for Detach
// detachRequest writes a Detach request to a byte slice.
func detachRequest(c *xgb.Conn, Shmseg Seg) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SHM"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 2 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Shmseg))
b += 4
return buf
}
// GetImageCookie is a cookie used only for GetImage requests.
type GetImageCookie struct {
*xgb.Cookie
}
// GetImage sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetImageCookie.Reply()
func GetImage(c *xgb.Conn, Drawable xproto.Drawable, X int16, Y int16, Width uint16, Height uint16, PlaneMask uint32, Format byte, Shmseg Seg, Offset uint32) GetImageCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'GetImage' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getImageRequest(c, Drawable, X, Y, Width, Height, PlaneMask, Format, Shmseg, Offset), cookie)
return GetImageCookie{cookie}
}
// GetImageUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetImageUnchecked(c *xgb.Conn, Drawable xproto.Drawable, X int16, Y int16, Width uint16, Height uint16, PlaneMask uint32, Format byte, Shmseg Seg, Offset uint32) GetImageCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'GetImage' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getImageRequest(c, Drawable, X, Y, Width, Height, PlaneMask, Format, Shmseg, Offset), cookie)
return GetImageCookie{cookie}
}
// GetImageReply represents the data returned from a GetImage request.
type GetImageReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
Depth byte
Visual xproto.Visualid
Size uint32
}
// Reply blocks and returns the reply data for a GetImage request.
func (cook GetImageCookie) Reply() (*GetImageReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getImageReply(buf), nil
}
// getImageReply reads a byte slice into a GetImageReply value.
func getImageReply(buf []byte) *GetImageReply {
v := new(GetImageReply)
b := 1 // skip reply determinant
v.Depth = buf[b]
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.Visual = xproto.Visualid(xgb.Get32(buf[b:]))
b += 4
v.Size = xgb.Get32(buf[b:])
b += 4
return v
}
// Write request to wire for GetImage
// getImageRequest writes a GetImage request to a byte slice.
func getImageRequest(c *xgb.Conn, Drawable xproto.Drawable, X int16, Y int16, Width uint16, Height uint16, PlaneMask uint32, Format byte, Shmseg Seg, Offset uint32) []byte {
size := 32
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SHM"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 4 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Drawable))
b += 4
xgb.Put16(buf[b:], uint16(X))
b += 2
xgb.Put16(buf[b:], uint16(Y))
b += 2
xgb.Put16(buf[b:], Width)
b += 2
xgb.Put16(buf[b:], Height)
b += 2
xgb.Put32(buf[b:], PlaneMask)
b += 4
buf[b] = Format
b += 1
b += 3 // padding
xgb.Put32(buf[b:], uint32(Shmseg))
b += 4
xgb.Put32(buf[b:], Offset)
b += 4
return buf
}
// PutImageCookie is a cookie used only for PutImage requests.
type PutImageCookie struct {
*xgb.Cookie
}
// PutImage sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func PutImage(c *xgb.Conn, Drawable xproto.Drawable, Gc xproto.Gcontext, TotalWidth uint16, TotalHeight uint16, SrcX uint16, SrcY uint16, SrcWidth uint16, SrcHeight uint16, DstX int16, DstY int16, Depth byte, Format byte, SendEvent byte, Shmseg Seg, Offset uint32) PutImageCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'PutImage' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(putImageRequest(c, Drawable, Gc, TotalWidth, TotalHeight, SrcX, SrcY, SrcWidth, SrcHeight, DstX, DstY, Depth, Format, SendEvent, Shmseg, Offset), cookie)
return PutImageCookie{cookie}
}
// PutImageChecked sends a checked request.
// If an error occurs, it can be retrieved using PutImageCookie.Check()
func PutImageChecked(c *xgb.Conn, Drawable xproto.Drawable, Gc xproto.Gcontext, TotalWidth uint16, TotalHeight uint16, SrcX uint16, SrcY uint16, SrcWidth uint16, SrcHeight uint16, DstX int16, DstY int16, Depth byte, Format byte, SendEvent byte, Shmseg Seg, Offset uint32) PutImageCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'PutImage' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(putImageRequest(c, Drawable, Gc, TotalWidth, TotalHeight, SrcX, SrcY, SrcWidth, SrcHeight, DstX, DstY, Depth, Format, SendEvent, Shmseg, Offset), cookie)
return PutImageCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook PutImageCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for PutImage
// putImageRequest writes a PutImage request to a byte slice.
func putImageRequest(c *xgb.Conn, Drawable xproto.Drawable, Gc xproto.Gcontext, TotalWidth uint16, TotalHeight uint16, SrcX uint16, SrcY uint16, SrcWidth uint16, SrcHeight uint16, DstX int16, DstY int16, Depth byte, Format byte, SendEvent byte, Shmseg Seg, Offset uint32) []byte {
size := 40
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SHM"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 3 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Drawable))
b += 4
xgb.Put32(buf[b:], uint32(Gc))
b += 4
xgb.Put16(buf[b:], TotalWidth)
b += 2
xgb.Put16(buf[b:], TotalHeight)
b += 2
xgb.Put16(buf[b:], SrcX)
b += 2
xgb.Put16(buf[b:], SrcY)
b += 2
xgb.Put16(buf[b:], SrcWidth)
b += 2
xgb.Put16(buf[b:], SrcHeight)
b += 2
xgb.Put16(buf[b:], uint16(DstX))
b += 2
xgb.Put16(buf[b:], uint16(DstY))
b += 2
buf[b] = Depth
b += 1
buf[b] = Format
b += 1
buf[b] = SendEvent
b += 1
b += 1 // padding
xgb.Put32(buf[b:], uint32(Shmseg))
b += 4
xgb.Put32(buf[b:], Offset)
b += 4
return buf
}
// QueryVersionCookie is a cookie used only for QueryVersion requests.
type QueryVersionCookie struct {
*xgb.Cookie
}
// QueryVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling QueryVersionCookie.Reply()
func QueryVersion(c *xgb.Conn) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(queryVersionRequest(c), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func QueryVersionUnchecked(c *xgb.Conn) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["MIT-SHM"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'MIT-SHM'. shm.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(queryVersionRequest(c), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionReply represents the data returned from a QueryVersion request.
type QueryVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
SharedPixmaps bool
MajorVersion uint16
MinorVersion uint16
Uid uint16
Gid uint16
PixmapFormat byte
// padding: 15 bytes
}
// Reply blocks and returns the reply data for a QueryVersion request.
func (cook QueryVersionCookie) Reply() (*QueryVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return queryVersionReply(buf), nil
}
// queryVersionReply reads a byte slice into a QueryVersionReply value.
func queryVersionReply(buf []byte) *QueryVersionReply {
v := new(QueryVersionReply)
b := 1 // skip reply determinant
if buf[b] == 1 {
v.SharedPixmaps = true
} else {
v.SharedPixmaps = false
}
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.MajorVersion = xgb.Get16(buf[b:])
b += 2
v.MinorVersion = xgb.Get16(buf[b:])
b += 2
v.Uid = xgb.Get16(buf[b:])
b += 2
v.Gid = xgb.Get16(buf[b:])
b += 2
v.PixmapFormat = buf[b]
b += 1
b += 15 // padding
return v
}
// Write request to wire for QueryVersion
// queryVersionRequest writes a QueryVersion request to a byte slice.
func queryVersionRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["MIT-SHM"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}

29
vend/xgb/sync.go Normal file
View File

@ -0,0 +1,29 @@
package xgb
// Sync sends a round trip request and waits for the response.
// This forces all pending cookies to be dealt with.
// You actually shouldn't need to use this like you might with Xlib. Namely,
// buffers are automatically flushed using Go's channels and round trip requests
// are forced where appropriate automatically.
func (c *Conn) Sync() {
cookie := c.NewCookie(true, true)
c.NewRequest(c.getInputFocusRequest(), cookie)
cookie.Reply() // wait for the buffer to clear
}
// getInputFocusRequest writes the raw bytes to a buffer.
// It is duplicated from xproto/xproto.go.
func (c *Conn) getInputFocusRequest() []byte {
size := 4
b := 0
buf := make([]byte, size)
buf[b] = 43 // request opcode
b += 1
b += 1 // padding
Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}

426
vend/xgb/testingTools.go Normal file
View File

@ -0,0 +1,426 @@
package xgb
import (
"bytes"
"errors"
"io"
"net"
"regexp"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
// Leaks monitor
type goroutine struct {
id int
name string
stack []byte
}
type leaks struct {
name string
goroutines map[int]goroutine
report []*leaks
}
func leaksMonitor(name string, monitors ...*leaks) *leaks {
return &leaks{
name,
leaks{}.collectGoroutines(),
monitors,
}
}
// ispired by https://golang.org/src/runtime/debug/stack.go?s=587:606#L21
// stack returns a formatted stack trace of all goroutines.
// It calls runtime.Stack with a large enough buffer to capture the entire trace.
func (_ leaks) stack() []byte {
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
return buf[:n]
}
buf = make([]byte, 2*len(buf))
}
}
func (l leaks) collectGoroutines() map[int]goroutine {
res := make(map[int]goroutine)
stacks := bytes.Split(l.stack(), []byte{'\n', '\n'})
regexpId := regexp.MustCompile(`^\s*goroutine\s*(\d+)`)
for _, st := range stacks {
lines := bytes.Split(st, []byte{'\n'})
if len(lines) < 2 {
panic("routine stach has less tnan two lines: " + string(st))
}
idMatches := regexpId.FindSubmatch(lines[0])
if len(idMatches) < 2 {
panic("no id found in goroutine stack's first line: " + string(lines[0]))
}
id, err := strconv.Atoi(string(idMatches[1]))
if err != nil {
panic("converting goroutine id to number error: " + err.Error())
}
if _, ok := res[id]; ok {
panic("2 goroutines with same id: " + strconv.Itoa(id))
}
name := strings.TrimSpace(string(lines[1]))
//filter out our stack routine
if strings.Contains(name, "xgb.leaks.stack") {
continue
}
res[id] = goroutine{id, name, st}
}
return res
}
func (l leaks) leakingGoroutines() []goroutine {
goroutines := l.collectGoroutines()
res := []goroutine{}
for id, gr := range goroutines {
if _, ok := l.goroutines[id]; ok {
continue
}
res = append(res, gr)
}
return res
}
func (l leaks) checkTesting(t *testing.T) {
if len(l.leakingGoroutines()) == 0 {
return
}
leakTimeout := 10 * time.Millisecond
time.Sleep(leakTimeout)
//t.Logf("possible goroutine leakage, waiting %v", leakTimeout)
grs := l.leakingGoroutines()
for _, gr := range grs {
t.Errorf("%s: %s is leaking", l.name, gr.name)
//t.Errorf("%s: %s is leaking\n%v", l.name, gr.name, string(gr.stack))
}
for _, rl := range l.report {
rl.ignoreLeak(grs...)
}
}
func (l *leaks) ignoreLeak(grs ...goroutine) {
for _, gr := range grs {
l.goroutines[gr.id] = gr
}
}
// dummy net.Conn
type dAddr struct {
s string
}
func (_ dAddr) Network() string { return "dummy" }
func (a dAddr) String() string { return a.s }
var (
dNCErrNotImplemented = errors.New("command not implemented")
dNCErrClosed = errors.New("server closed")
dNCErrWrite = errors.New("server write failed")
dNCErrRead = errors.New("server read failed")
dNCErrResponse = errors.New("server response error")
)
type dNCIoResult struct {
n int
err error
}
type dNCIo struct {
b []byte
result chan dNCIoResult
}
type dNCCWriteLock struct{}
type dNCCWriteUnlock struct{}
type dNCCWriteError struct{}
type dNCCWriteSuccess struct{}
type dNCCReadLock struct{}
type dNCCReadUnlock struct{}
type dNCCReadError struct{}
type dNCCReadSuccess struct{}
// dummy net.Conn interface. Needs to be constructed via newDummyNetConn([...]) function.
type dNC struct {
reply func([]byte) []byte
addr dAddr
in, out chan dNCIo
control chan interface{}
done chan struct{}
}
// Results running dummy server, satisfying net.Conn interface for test purposes.
// 'name' parameter will be returned via (*dNC).Local/RemoteAddr().String()
// 'reply' parameter function will be runned only on successful (*dNC).Write(b) with 'b' as parameter to 'reply'. The result will be stored in internal buffer and can be retrieved later via (*dNC).Read([...]) method.
// It is users responsibility to stop and clean up resources with (*dNC).Close, if not needed anymore.
// By default, the (*dNC).Write([...]) and (*dNC).Read([...]) methods are unlocked and will not result in error.
//TODO make (*dNC).SetDeadline, (*dNC).SetReadDeadline, (*dNC).SetWriteDeadline work proprely.
func newDummyNetConn(name string, reply func([]byte) []byte) *dNC {
s := &dNC{
reply,
dAddr{name},
make(chan dNCIo), make(chan dNCIo),
make(chan interface{}),
make(chan struct{}),
}
in, out := s.in, chan dNCIo(nil)
buf := &bytes.Buffer{}
errorRead, errorWrite := false, false
lockRead := false
go func() {
defer close(s.done)
for {
select {
case dxsio := <-in:
if errorWrite {
dxsio.result <- dNCIoResult{0, dNCErrWrite}
break
}
response := s.reply(dxsio.b)
buf.Write(response)
dxsio.result <- dNCIoResult{len(dxsio.b), nil}
if !lockRead && buf.Len() > 0 && out == nil {
out = s.out
}
case dxsio := <-out:
if errorRead {
dxsio.result <- dNCIoResult{0, dNCErrRead}
break
}
n, err := buf.Read(dxsio.b)
dxsio.result <- dNCIoResult{n, err}
if buf.Len() == 0 {
out = nil
}
case ci := <-s.control:
if ci == nil {
return
}
switch ci.(type) {
case dNCCWriteLock:
in = nil
case dNCCWriteUnlock:
in = s.in
case dNCCWriteError:
errorWrite = true
case dNCCWriteSuccess:
errorWrite = false
case dNCCReadLock:
out = nil
lockRead = true
case dNCCReadUnlock:
lockRead = false
if buf.Len() > 0 && out == nil {
out = s.out
}
case dNCCReadError:
errorRead = true
case dNCCReadSuccess:
errorRead = false
default:
}
}
}
}()
return s
}
// Shuts down dummy net.Conn server. Every blocking or future method calls will do nothing and result in error.
// Result will be dNCErrClosed if server was allready closed.
// Server can not be unclosed.
func (s *dNC) Close() error {
select {
case s.control <- nil:
<-s.done
return nil
case <-s.done:
}
return dNCErrClosed
}
// Performs a write action to server.
// If not locked by (*dNC).WriteLock, it results in error or success. If locked, this method will block until unlocked, or closed.
//
// This method can be set to result in error or success, via (*dNC).WriteError() or (*dNC).WriteSuccess() methods.
//
// If setted to result in error, the 'reply' function will NOT be called and internal buffer will NOT increasethe.
// Result will be (0, dNCErrWrite).
//
// If setted to result in success, the 'reply' function will be called and its result will be writen to internal buffer.
// If there is something in the internal buffer, the (*dNC).Read([...]) will be unblocked (if not previously locked with (*dNC).ReadLock).
// Result will be (len(b), nil)
//
// If server was closed previously, result will be (0, dNCErrClosed).
func (s *dNC) Write(b []byte) (int, error) {
resChan := make(chan dNCIoResult)
select {
case s.in <- dNCIo{b, resChan}:
res := <-resChan
return res.n, res.err
case <-s.done:
}
return 0, dNCErrClosed
}
// Performs a read action from server.
// If locked by (*dNC).ReadLock(), this method will block until unlocked with (*dNC).ReadUnlock(), or server closes.
//
// If not locked, this method can be setted to result imidiatly in error, will block if internal buffer is empty or will perform an read operation from internal buffer.
//
// If setted to result in error via (*dNC).ReadError(), the result will be (0, dNCErrWrite).
//
// If not locked and not setted to result in error via (*dNC).ReadSuccess(), this method will block until internall buffer is not empty, than it returns the result of the buffer read operation via (*bytes.Buffer).Read([...]).
// If the internal buffer is empty after this method, all follwing (*dNC).Read([...]), requests will block until internall buffer is filled after successful write requests.
//
// If server was closed previously, result will be (0, io.EOF).
func (s *dNC) Read(b []byte) (int, error) {
resChan := make(chan dNCIoResult)
select {
case s.out <- dNCIo{b, resChan}:
res := <-resChan
return res.n, res.err
case <-s.done:
}
return 0, io.EOF
}
func (s *dNC) LocalAddr() net.Addr { return s.addr }
func (s *dNC) RemoteAddr() net.Addr { return s.addr }
func (s *dNC) SetDeadline(t time.Time) error { return dNCErrNotImplemented }
func (s *dNC) SetReadDeadline(t time.Time) error { return dNCErrNotImplemented }
func (s *dNC) SetWriteDeadline(t time.Time) error { return dNCErrNotImplemented }
func (s *dNC) Control(i interface{}) error {
select {
case s.control <- i:
return nil
case <-s.done:
}
return dNCErrClosed
}
// Locks writing. All write requests will be blocked until write is unlocked with (*dNC).WriteUnlock, or server closes.
func (s *dNC) WriteLock() error {
return s.Control(dNCCWriteLock{})
}
// Unlocks writing. All blocked write requests until now will be accepted.
func (s *dNC) WriteUnlock() error {
return s.Control(dNCCWriteUnlock{})
}
// Unlocks writing and makes (*dNC).Write to result (0, dNCErrWrite).
func (s *dNC) WriteError() error {
if err := s.WriteUnlock(); err != nil {
return err
}
return s.Control(dNCCWriteError{})
}
// Unlocks writing and makes (*dNC).Write([...]) not result in error. See (*dNC).Write for details.
func (s *dNC) WriteSuccess() error {
if err := s.WriteUnlock(); err != nil {
return err
}
return s.Control(dNCCWriteSuccess{})
}
// Locks reading. All read requests will be blocked until read is unlocked with (*dNC).ReadUnlock, or server closes.
// (*dNC).Read([...]) wil block even after successful write.
func (s *dNC) ReadLock() error {
return s.Control(dNCCReadLock{})
}
// Unlocks reading. If the internall buffer is not empty, next read will not block.
func (s *dNC) ReadUnlock() error {
return s.Control(dNCCReadUnlock{})
}
// Unlocks read and makes every blocked and following (*dNC).Read([...]) imidiatly result in error. See (*dNC).Read for details.
func (s *dNC) ReadError() error {
if err := s.ReadUnlock(); err != nil {
return err
}
return s.Control(dNCCReadError{})
}
// Unlocks read and makes every blocked and following (*dNC).Read([...]) requests be handled, if according to internal buffer. See (*dNC).Read for details.
func (s *dNC) ReadSuccess() error {
if err := s.ReadUnlock(); err != nil {
return err
}
return s.Control(dNCCReadSuccess{})
}
// dummy X server replier for dummy net.Conn
type dXSEvent struct{}
func (_ dXSEvent) Bytes() []byte { return nil }
func (_ dXSEvent) String() string { return "dummy X server event" }
type dXSError struct {
seqId uint16
}
func (e dXSError) SequenceId() uint16 { return e.seqId }
func (_ dXSError) BadId() uint32 { return 0 }
func (_ dXSError) Error() string { return "dummy X server error reply" }
func newDummyXServerReplier() func([]byte) []byte {
// register xgb error & event replies
NewErrorFuncs[255] = func(buf []byte) Error {
return dXSError{Get16(buf[2:])}
}
NewEventFuncs[128&127] = func(buf []byte) Event {
return dXSEvent{}
}
// sequence number generator
seqId := uint16(1)
incrementSequenceId := func() {
// this has to be the same algorithm as in (*Conn).generateSeqIds
if seqId == uint16((1<<16)-1) {
seqId = 0
} else {
seqId++
}
}
return func(request []byte) []byte {
res := make([]byte, 32)
switch string(request) {
case "event":
res[0] = 128
return res
case "error":
res[0] = 0 // error
res[1] = 255 // error function
default:
res[0] = 1 // reply
}
Put16(res[2:], seqId) // sequence number
incrementSequenceId()
if string(request) == "noreply" {
return nil
}
return res
}
}

View File

@ -0,0 +1,350 @@
package xgb
import (
"bytes"
"errors"
"fmt"
"io"
"reflect"
"sync"
"testing"
"time"
)
func TestLeaks(t *testing.T) {
lm := leaksMonitor("lm")
if lgrs := lm.leakingGoroutines(); len(lgrs) != 0 {
t.Errorf("leakingGoroutines returned %d leaking goroutines, want 0", len(lgrs))
}
done := make(chan struct{})
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
<-done
wg.Done()
}()
if lgrs := lm.leakingGoroutines(); len(lgrs) != 1 {
t.Errorf("leakingGoroutines returned %d leaking goroutines, want 1", len(lgrs))
}
wg.Add(1)
go func() {
<-done
wg.Done()
}()
if lgrs := lm.leakingGoroutines(); len(lgrs) != 2 {
t.Errorf("leakingGoroutines returned %d leaking goroutines, want 2", len(lgrs))
}
close(done)
wg.Wait()
if lgrs := lm.leakingGoroutines(); len(lgrs) != 0 {
t.Errorf("leakingGoroutines returned %d leaking goroutines, want 0", len(lgrs))
}
lm.checkTesting(t)
//TODO multiple leak monitors with report ignore tests
}
func TestDummyNetConn(t *testing.T) {
ioStatesPairGenerator := func(writeStates, readStates []string) []func() (*dNC, error) {
writeSetters := map[string]func(*dNC) error{
"lock": (*dNC).WriteLock,
"error": (*dNC).WriteError,
"success": (*dNC).WriteSuccess,
}
readSetters := map[string]func(*dNC) error{
"lock": (*dNC).ReadLock,
"error": (*dNC).ReadError,
"success": (*dNC).ReadSuccess,
}
res := []func() (*dNC, error){}
for _, writeState := range writeStates {
writeState, writeSetter := writeState, writeSetters[writeState]
if writeSetter == nil {
panic("unknown write state: " + writeState)
continue
}
for _, readState := range readStates {
readState, readSetter := readState, readSetters[readState]
if readSetter == nil {
panic("unknown read state: " + readState)
continue
}
res = append(res, func() (*dNC, error) {
// loopback server
s := newDummyNetConn("w:"+writeState+";r:"+readState, func(b []byte) []byte { return b })
if err := readSetter(s); err != nil {
s.Close()
return nil, errors.New("set read " + readState + " error: " + err.Error())
}
if err := writeSetter(s); err != nil {
s.Close()
return nil, errors.New("set write " + writeState + " error: " + err.Error())
}
return s, nil
})
}
}
return res
}
timeout := 10 * time.Millisecond
wantResponse := func(action func(*dNC) error, want, block error) func(*dNC) error {
return func(s *dNC) error {
actionResult := make(chan error)
timedOut := make(chan struct{})
go func() {
err := action(s)
select {
case <-timedOut:
if err != block {
t.Errorf("after unblocking, action result=%v, want %v", err, block)
}
case actionResult <- err:
}
}()
select {
case err := <-actionResult:
if err != want {
return errors.New(fmt.Sprintf("action result=%v, want %v", err, want))
}
case <-time.After(timeout):
close(timedOut)
return errors.New(fmt.Sprintf("action did not respond for %v, result want %v", timeout, want))
}
return nil
}
}
wantBlock := func(action func(*dNC) error, unblock error) func(*dNC) error {
return func(s *dNC) error {
actionResult := make(chan error)
timedOut := make(chan struct{})
go func() {
err := action(s)
select {
case <-timedOut:
if err != unblock {
t.Errorf("after unblocking, action result=%v, want %v", err, unblock)
}
case actionResult <- err:
}
}()
select {
case err := <-actionResult:
return errors.New(fmt.Sprintf("action result=%v, want to be blocked", err))
case <-time.After(timeout):
close(timedOut)
}
return nil
}
}
write := func(b string) func(*dNC) error {
return func(s *dNC) error {
n, err := s.Write([]byte(b))
if err == nil && n != len(b) {
return errors.New("Write returned nil error, but not everything was written")
}
return err
}
}
read := func(b string) func(*dNC) error {
return func(s *dNC) error {
r := make([]byte, len(b))
n, err := s.Read(r)
if err == nil {
if n != len(b) {
return errors.New("Read returned nil error, but not everything was read")
}
if !reflect.DeepEqual(r, []byte(b)) {
return errors.New("Read=\"" + string(r) + "\", want \"" + string(b) + "\"")
}
}
return err
}
}
testCases := []struct {
description string
servers []func() (*dNC, error)
actions []func(*dNC) error // actions per server
}{
{"close,close",
ioStatesPairGenerator(
[]string{"lock", "error", "success"},
[]string{"lock", "error", "success"},
),
[]func(*dNC) error{
wantResponse((*dNC).Close, nil, dNCErrClosed),
wantResponse((*dNC).Close, dNCErrClosed, dNCErrClosed),
},
},
{"write,close,write",
ioStatesPairGenerator(
[]string{"lock"},
[]string{"lock", "error", "success"},
),
[]func(*dNC) error{
wantBlock(write(""), dNCErrClosed),
wantResponse((*dNC).Close, nil, dNCErrClosed),
wantResponse(write(""), dNCErrClosed, dNCErrClosed),
},
},
{"write,close,write",
ioStatesPairGenerator(
[]string{"error"},
[]string{"lock", "error", "success"},
),
[]func(*dNC) error{
wantResponse(write(""), dNCErrWrite, dNCErrClosed),
wantResponse((*dNC).Close, nil, dNCErrClosed),
wantResponse(write(""), dNCErrClosed, dNCErrClosed),
},
},
{"write,close,write",
ioStatesPairGenerator(
[]string{"success"},
[]string{"lock", "error", "success"},
),
[]func(*dNC) error{
wantResponse(write(""), nil, dNCErrClosed),
wantResponse((*dNC).Close, nil, dNCErrClosed),
wantResponse(write(""), dNCErrClosed, dNCErrClosed),
},
},
{"read,close,read",
ioStatesPairGenerator(
[]string{"lock", "error", "success"},
[]string{"lock", "error", "success"},
),
[]func(*dNC) error{
wantBlock(read(""), io.EOF),
wantResponse((*dNC).Close, nil, dNCErrClosed),
wantResponse(read(""), io.EOF, io.EOF),
},
},
{"write,read",
ioStatesPairGenerator(
[]string{"lock"},
[]string{"lock", "error", "success"},
),
[]func(*dNC) error{
wantBlock(write("1"), dNCErrClosed),
wantBlock(read("1"), io.EOF),
},
},
{"write,read",
ioStatesPairGenerator(
[]string{"error"},
[]string{"lock", "error", "success"},
),
[]func(*dNC) error{
wantResponse(write("1"), dNCErrWrite, dNCErrClosed),
wantBlock(read("1"), io.EOF),
},
},
{"write,read",
ioStatesPairGenerator(
[]string{"success"},
[]string{"lock"},
),
[]func(*dNC) error{
wantResponse(write("1"), nil, dNCErrClosed),
wantBlock(read("1"), io.EOF),
},
},
{"write,read",
ioStatesPairGenerator(
[]string{"success"},
[]string{"error"},
),
[]func(*dNC) error{
wantResponse(write("1"), nil, dNCErrClosed),
wantResponse(read("1"), dNCErrRead, io.EOF),
},
},
{"write,read",
ioStatesPairGenerator(
[]string{"success"},
[]string{"success"},
),
[]func(*dNC) error{
wantResponse(write("1"), nil, dNCErrClosed),
wantResponse(read("1"), nil, io.EOF),
},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
defer leaksMonitor(tc.description).checkTesting(t)
for _, server := range tc.servers {
s, err := server()
if err != nil {
t.Error(err)
continue
}
if s == nil {
t.Error("nil server in testcase")
continue
}
t.Run(s.LocalAddr().String(), func(t *testing.T) {
defer leaksMonitor(s.LocalAddr().String()).checkTesting(t)
for _, action := range tc.actions {
if err := action(s); err != nil {
t.Error(err)
break
}
}
s.Close()
})
}
})
}
}
func TestDummyXServerReplier(t *testing.T) {
testCases := [][][2][]byte{
{
[2][]byte{[]byte("reply"), []byte{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("eply"), []byte{1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("ply"), []byte{1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("event"), []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("ly"), []byte{1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("y"), []byte{1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte(""), []byte{1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("event"), []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("reply"), []byte{1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("error"), []byte{0, 255, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("ply"), []byte{1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("event"), []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("ly"), []byte{1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("noreply"), nil},
[2][]byte{[]byte("error"), []byte{0, 255, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[2][]byte{[]byte("noreply"), nil},
[2][]byte{[]byte(""), []byte{1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
},
}
for tci, tc := range testCases {
replier := newDummyXServerReplier()
for ai, ioPair := range tc {
in, want := ioPair[0], ioPair[1]
if out := replier(in); !bytes.Equal(out, want) {
t.Errorf("testCase %d, action %d, replier(%s) = %v, want %v", tci, ai, string(in), out, want)
break
}
}
}
}

361
vend/xgb/xcmisc/xcmisc.go Normal file
View File

@ -0,0 +1,361 @@
// Package xcmisc is the X client API for the XC-MISC extension.
package xcmisc
// This file is automatically generated from xc_misc.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the XC-MISC extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 7, "XC-MISC").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named XC-MISC could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["XC-MISC"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["XC-MISC"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["XC-MISC"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["XC-MISC"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["XC-MISC"] = make(map[int]xgb.NewErrorFun)
}
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// GetVersionCookie is a cookie used only for GetVersion requests.
type GetVersionCookie struct {
*xgb.Cookie
}
// GetVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetVersionCookie.Reply()
func GetVersion(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) GetVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XC-MISC"]; !ok {
panic("Cannot issue request 'GetVersion' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return GetVersionCookie{cookie}
}
// GetVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetVersionUnchecked(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) GetVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XC-MISC"]; !ok {
panic("Cannot issue request 'GetVersion' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return GetVersionCookie{cookie}
}
// GetVersionReply represents the data returned from a GetVersion request.
type GetVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
ServerMajorVersion uint16
ServerMinorVersion uint16
}
// Reply blocks and returns the reply data for a GetVersion request.
func (cook GetVersionCookie) Reply() (*GetVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getVersionReply(buf), nil
}
// getVersionReply reads a byte slice into a GetVersionReply value.
func getVersionReply(buf []byte) *GetVersionReply {
v := new(GetVersionReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.ServerMajorVersion = xgb.Get16(buf[b:])
b += 2
v.ServerMinorVersion = xgb.Get16(buf[b:])
b += 2
return v
}
// Write request to wire for GetVersion
// getVersionRequest writes a GetVersion request to a byte slice.
func getVersionRequest(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XC-MISC"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put16(buf[b:], ClientMajorVersion)
b += 2
xgb.Put16(buf[b:], ClientMinorVersion)
b += 2
return buf
}
// GetXIDListCookie is a cookie used only for GetXIDList requests.
type GetXIDListCookie struct {
*xgb.Cookie
}
// GetXIDList sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetXIDListCookie.Reply()
func GetXIDList(c *xgb.Conn, Count uint32) GetXIDListCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XC-MISC"]; !ok {
panic("Cannot issue request 'GetXIDList' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getXIDListRequest(c, Count), cookie)
return GetXIDListCookie{cookie}
}
// GetXIDListUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetXIDListUnchecked(c *xgb.Conn, Count uint32) GetXIDListCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XC-MISC"]; !ok {
panic("Cannot issue request 'GetXIDList' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getXIDListRequest(c, Count), cookie)
return GetXIDListCookie{cookie}
}
// GetXIDListReply represents the data returned from a GetXIDList request.
type GetXIDListReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
IdsLen uint32
// padding: 20 bytes
Ids []uint32 // size: xgb.Pad((int(IdsLen) * 4))
}
// Reply blocks and returns the reply data for a GetXIDList request.
func (cook GetXIDListCookie) Reply() (*GetXIDListReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getXIDListReply(buf), nil
}
// getXIDListReply reads a byte slice into a GetXIDListReply value.
func getXIDListReply(buf []byte) *GetXIDListReply {
v := new(GetXIDListReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.IdsLen = xgb.Get32(buf[b:])
b += 4
b += 20 // padding
v.Ids = make([]uint32, v.IdsLen)
for i := 0; i < int(v.IdsLen); i++ {
v.Ids[i] = xgb.Get32(buf[b:])
b += 4
}
return v
}
// Write request to wire for GetXIDList
// getXIDListRequest writes a GetXIDList request to a byte slice.
func getXIDListRequest(c *xgb.Conn, Count uint32) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XC-MISC"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 2 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], Count)
b += 4
return buf
}
// GetXIDRangeCookie is a cookie used only for GetXIDRange requests.
type GetXIDRangeCookie struct {
*xgb.Cookie
}
// GetXIDRange sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetXIDRangeCookie.Reply()
func GetXIDRange(c *xgb.Conn) GetXIDRangeCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XC-MISC"]; !ok {
panic("Cannot issue request 'GetXIDRange' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getXIDRangeRequest(c), cookie)
return GetXIDRangeCookie{cookie}
}
// GetXIDRangeUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetXIDRangeUnchecked(c *xgb.Conn) GetXIDRangeCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XC-MISC"]; !ok {
panic("Cannot issue request 'GetXIDRange' using the uninitialized extension 'XC-MISC'. xcmisc.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getXIDRangeRequest(c), cookie)
return GetXIDRangeCookie{cookie}
}
// GetXIDRangeReply represents the data returned from a GetXIDRange request.
type GetXIDRangeReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
StartId uint32
Count uint32
}
// Reply blocks and returns the reply data for a GetXIDRange request.
func (cook GetXIDRangeCookie) Reply() (*GetXIDRangeReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getXIDRangeReply(buf), nil
}
// getXIDRangeReply reads a byte slice into a GetXIDRangeReply value.
func getXIDRangeReply(buf []byte) *GetXIDRangeReply {
v := new(GetXIDRangeReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.StartId = xgb.Get32(buf[b:])
b += 4
v.Count = xgb.Get32(buf[b:])
b += 4
return v
}
// Write request to wire for GetXIDRange
// getXIDRangeRequest writes a GetXIDRange request to a byte slice.
func getXIDRangeRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XC-MISC"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 1 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}

595
vend/xgb/xevie/xevie.go Normal file
View File

@ -0,0 +1,595 @@
// Package xevie is the X client API for the XEVIE extension.
package xevie
// This file is automatically generated from xevie.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the XEVIE extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 5, "XEVIE").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named XEVIE could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["XEVIE"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["XEVIE"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["XEVIE"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["XEVIE"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["XEVIE"] = make(map[int]xgb.NewErrorFun)
}
const (
DatatypeUnmodified = 0
DatatypeModified = 1
)
type Event struct {
// padding: 32 bytes
}
// EventRead reads a byte slice into a Event value.
func EventRead(buf []byte, v *Event) int {
b := 0
b += 32 // padding
return b
}
// EventReadList reads a byte slice into a list of Event values.
func EventReadList(buf []byte, dest []Event) int {
b := 0
for i := 0; i < len(dest); i++ {
dest[i] = Event{}
b += EventRead(buf[b:], &dest[i])
}
return xgb.Pad(b)
}
// Bytes writes a Event value to a byte slice.
func (v Event) Bytes() []byte {
buf := make([]byte, 32)
b := 0
b += 32 // padding
return buf[:b]
}
// EventListBytes writes a list of Event values to a byte slice.
func EventListBytes(buf []byte, list []Event) int {
b := 0
var structBytes []byte
for _, item := range list {
structBytes = item.Bytes()
copy(buf[b:], structBytes)
b += len(structBytes)
}
return xgb.Pad(b)
}
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// EndCookie is a cookie used only for End requests.
type EndCookie struct {
*xgb.Cookie
}
// End sends a checked request.
// If an error occurs, it will be returned with the reply by calling EndCookie.Reply()
func End(c *xgb.Conn, Cmap uint32) EndCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'End' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(endRequest(c, Cmap), cookie)
return EndCookie{cookie}
}
// EndUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func EndUnchecked(c *xgb.Conn, Cmap uint32) EndCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'End' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(endRequest(c, Cmap), cookie)
return EndCookie{cookie}
}
// EndReply represents the data returned from a End request.
type EndReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
// padding: 24 bytes
}
// Reply blocks and returns the reply data for a End request.
func (cook EndCookie) Reply() (*EndReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return endReply(buf), nil
}
// endReply reads a byte slice into a EndReply value.
func endReply(buf []byte) *EndReply {
v := new(EndReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
b += 24 // padding
return v
}
// Write request to wire for End
// endRequest writes a End request to a byte slice.
func endRequest(c *xgb.Conn, Cmap uint32) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XEVIE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 2 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], Cmap)
b += 4
return buf
}
// QueryVersionCookie is a cookie used only for QueryVersion requests.
type QueryVersionCookie struct {
*xgb.Cookie
}
// QueryVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling QueryVersionCookie.Reply()
func QueryVersion(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func QueryVersionUnchecked(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(queryVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionReply represents the data returned from a QueryVersion request.
type QueryVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
ServerMajorVersion uint16
ServerMinorVersion uint16
// padding: 20 bytes
}
// Reply blocks and returns the reply data for a QueryVersion request.
func (cook QueryVersionCookie) Reply() (*QueryVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return queryVersionReply(buf), nil
}
// queryVersionReply reads a byte slice into a QueryVersionReply value.
func queryVersionReply(buf []byte) *QueryVersionReply {
v := new(QueryVersionReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.ServerMajorVersion = xgb.Get16(buf[b:])
b += 2
v.ServerMinorVersion = xgb.Get16(buf[b:])
b += 2
b += 20 // padding
return v
}
// Write request to wire for QueryVersion
// queryVersionRequest writes a QueryVersion request to a byte slice.
func queryVersionRequest(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XEVIE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put16(buf[b:], ClientMajorVersion)
b += 2
xgb.Put16(buf[b:], ClientMinorVersion)
b += 2
return buf
}
// SelectInputCookie is a cookie used only for SelectInput requests.
type SelectInputCookie struct {
*xgb.Cookie
}
// SelectInput sends a checked request.
// If an error occurs, it will be returned with the reply by calling SelectInputCookie.Reply()
func SelectInput(c *xgb.Conn, EventMask uint32) SelectInputCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'SelectInput' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(selectInputRequest(c, EventMask), cookie)
return SelectInputCookie{cookie}
}
// SelectInputUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func SelectInputUnchecked(c *xgb.Conn, EventMask uint32) SelectInputCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'SelectInput' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(selectInputRequest(c, EventMask), cookie)
return SelectInputCookie{cookie}
}
// SelectInputReply represents the data returned from a SelectInput request.
type SelectInputReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
// padding: 24 bytes
}
// Reply blocks and returns the reply data for a SelectInput request.
func (cook SelectInputCookie) Reply() (*SelectInputReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return selectInputReply(buf), nil
}
// selectInputReply reads a byte slice into a SelectInputReply value.
func selectInputReply(buf []byte) *SelectInputReply {
v := new(SelectInputReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
b += 24 // padding
return v
}
// Write request to wire for SelectInput
// selectInputRequest writes a SelectInput request to a byte slice.
func selectInputRequest(c *xgb.Conn, EventMask uint32) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XEVIE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 4 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], EventMask)
b += 4
return buf
}
// SendCookie is a cookie used only for Send requests.
type SendCookie struct {
*xgb.Cookie
}
// Send sends a checked request.
// If an error occurs, it will be returned with the reply by calling SendCookie.Reply()
func Send(c *xgb.Conn, Event Event, DataType uint32) SendCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'Send' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(sendRequest(c, Event, DataType), cookie)
return SendCookie{cookie}
}
// SendUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func SendUnchecked(c *xgb.Conn, Event Event, DataType uint32) SendCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'Send' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(sendRequest(c, Event, DataType), cookie)
return SendCookie{cookie}
}
// SendReply represents the data returned from a Send request.
type SendReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
// padding: 24 bytes
}
// Reply blocks and returns the reply data for a Send request.
func (cook SendCookie) Reply() (*SendReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return sendReply(buf), nil
}
// sendReply reads a byte slice into a SendReply value.
func sendReply(buf []byte) *SendReply {
v := new(SendReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
b += 24 // padding
return v
}
// Write request to wire for Send
// sendRequest writes a Send request to a byte slice.
func sendRequest(c *xgb.Conn, Event Event, DataType uint32) []byte {
size := 104
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XEVIE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 3 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
{
structBytes := Event.Bytes()
copy(buf[b:], structBytes)
b += len(structBytes)
}
xgb.Put32(buf[b:], DataType)
b += 4
b += 64 // padding
return buf
}
// StartCookie is a cookie used only for Start requests.
type StartCookie struct {
*xgb.Cookie
}
// Start sends a checked request.
// If an error occurs, it will be returned with the reply by calling StartCookie.Reply()
func Start(c *xgb.Conn, Screen uint32) StartCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'Start' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(startRequest(c, Screen), cookie)
return StartCookie{cookie}
}
// StartUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func StartUnchecked(c *xgb.Conn, Screen uint32) StartCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XEVIE"]; !ok {
panic("Cannot issue request 'Start' using the uninitialized extension 'XEVIE'. xevie.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(startRequest(c, Screen), cookie)
return StartCookie{cookie}
}
// StartReply represents the data returned from a Start request.
type StartReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
// padding: 24 bytes
}
// Reply blocks and returns the reply data for a Start request.
func (cook StartCookie) Reply() (*StartReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return startReply(buf), nil
}
// startReply reads a byte slice into a StartReply value.
func startReply(buf []byte) *StartReply {
v := new(StartReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
b += 24 // padding
return v
}
// Write request to wire for Start
// startRequest writes a Start request to a byte slice.
func startRequest(c *xgb.Conn, Screen uint32) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XEVIE"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 1 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], Screen)
b += 4
return buf
}

1301
vend/xgb/xf86dri/xf86dri.go Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

2995
vend/xgb/xfixes/xfixes.go Normal file

File diff suppressed because it is too large Load Diff

608
vend/xgb/xgb.go Normal file
View File

@ -0,0 +1,608 @@
package xgb
import (
"errors"
"io"
"log"
"net"
"os"
"sync"
)
var (
// Where to log error-messages. Defaults to stderr.
// To disable logging, just set this to log.New(ioutil.Discard, "", 0)
Logger = log.New(os.Stderr, "XGB: ", log.Lshortfile)
)
const (
// cookieBuffer represents the queue size of cookies existing at any
// point in time. The size of the buffer is really only important when
// there are many requests without replies made in sequence. Once the
// buffer fills, a round trip request is made to clear the buffer.
cookieBuffer = 1000
// xidBuffer represents the queue size of the xid channel.
// I don't think this value matters much, since xid generation is not
// that expensive.
xidBuffer = 5
// seqBuffer represents the queue size of the sequence number channel.
// I don't think this value matters much, since sequence number generation
// is not that expensive.
seqBuffer = 5
// reqBuffer represents the queue size of the number of requests that
// can be made until new ones block. This value seems OK.
reqBuffer = 100
// eventBuffer represents the queue size of the number of events or errors
// that can be loaded off the wire and not grabbed with WaitForEvent
// until reading an event blocks. This value should be big enough to handle
// bursts of events.
eventBuffer = 5000
)
// A Conn represents a connection to an X server.
type Conn struct {
host string
conn net.Conn
display string
DisplayNumber int
DefaultScreen int
SetupBytes []byte
setupResourceIdBase uint32
setupResourceIdMask uint32
eventChan chan eventOrError
cookieChan chan *Cookie
xidChan chan xid
seqChan chan uint16
reqChan chan *request
doneSend chan struct{}
doneRead chan struct{}
// ExtLock is a lock used whenever new extensions are initialized.
// It should not be used. It is exported for use in the extension
// sub-packages.
ExtLock sync.RWMutex
// Extensions is a map from extension name to major opcode. It should
// not be used. It is exported for use in the extension sub-packages.
Extensions map[string]byte
}
// NewConn creates a new connection instance. It initializes locks, data
// structures, and performs the initial handshake. (The code for the handshake
// has been relegated to conn.go.)
// It is up to user to close connection with Close() method to finish all unfinished requests and clean up spawned goroutines.
// If the connection unexpectedly closes itself and WaitForEvent() returns "nil, nil", everything is cleaned by that moment, but nothing bad happens if you call Close() after.
func NewConn() (*Conn, error) {
return NewConnDisplay("")
}
// NewConnDisplay is just like NewConn (see closing instructions), but allows a specific DISPLAY
// string to be used.
// If 'display' is empty it will be taken from os.Getenv("DISPLAY").
//
// Examples:
// NewConn(":1") -> net.Dial("unix", "", "/tmp/.X11-unix/X1")
// NewConn("/tmp/launch-12/:0") -> net.Dial("unix", "", "/tmp/launch-12/:0")
// NewConn("hostname:2.1") -> net.Dial("tcp", "", "hostname:6002")
// NewConn("tcp/hostname:1.0") -> net.Dial("tcp", "", "hostname:6001")
func NewConnDisplay(display string) (*Conn, error) {
c := &Conn{}
// First connect. This reads authority, checks DISPLAY environment
// variable, and loads the initial Setup info.
err := c.connect(display)
if err != nil {
return nil, err
}
return postNewConn(c)
}
// NewConnNet is just like NewConn (see closing instructions), but allows a specific net.Conn
// to be used.
func NewConnNet(netConn net.Conn) (*Conn, error) {
c := &Conn{}
// First connect. This reads authority, checks DISPLAY environment
// variable, and loads the initial Setup info.
err := c.connectNet(netConn)
if err != nil {
return nil, err
}
return postNewConn(c)
}
func postNewConn(c *Conn) (*Conn, error) {
c.Extensions = make(map[string]byte)
c.cookieChan = make(chan *Cookie, cookieBuffer)
c.xidChan = make(chan xid, xidBuffer)
c.seqChan = make(chan uint16, seqBuffer)
c.reqChan = make(chan *request, reqBuffer)
c.eventChan = make(chan eventOrError, eventBuffer)
c.doneSend = make(chan struct{})
c.doneRead = make(chan struct{})
go c.generateXIds()
go c.generateSeqIds()
go c.sendRequests()
go c.readResponses()
return c, nil
}
// Close gracefully closes the connection to the X server.
// When everything is cleaned up, the WaitForEvent method will return (nil, nil)
func (c *Conn) Close() {
select {
case c.reqChan <- nil:
case <-c.doneSend:
}
}
// Event is an interface that can contain any of the events returned by the
// server. Use a type assertion switch to extract the Event structs.
type Event interface {
Bytes() []byte
String() string
}
// NewEventFun is the type of function use to construct events from raw bytes.
// It should not be used. It is exported for use in the extension sub-packages.
type NewEventFun func(buf []byte) Event
// NewEventFuncs is a map from event numbers to functions that create
// the corresponding event. It should not be used. It is exported for use
// in the extension sub-packages.
var NewEventFuncs = make(map[int]NewEventFun)
// NewExtEventFuncs is a temporary map that stores event constructor functions
// for each extension. When an extension is initialized, each event for that
// extension is added to the 'NewEventFuncs' map. It should not be used. It is
// exported for use in the extension sub-packages.
var NewExtEventFuncs = make(map[string]map[int]NewEventFun)
// Error is an interface that can contain any of the errors returned by
// the server. Use a type assertion switch to extract the Error structs.
type Error interface {
SequenceId() uint16
BadId() uint32
Error() string
}
// NewErrorFun is the type of function use to construct errors from raw bytes.
// It should not be used. It is exported for use in the extension sub-packages.
type NewErrorFun func(buf []byte) Error
// NewErrorFuncs is a map from error numbers to functions that create
// the corresponding error. It should not be used. It is exported for use in
// the extension sub-packages.
var NewErrorFuncs = make(map[int]NewErrorFun)
// NewExtErrorFuncs is a temporary map that stores error constructor functions
// for each extension. When an extension is initialized, each error for that
// extension is added to the 'NewErrorFuncs' map. It should not be used. It is
// exported for use in the extension sub-packages.
var NewExtErrorFuncs = make(map[string]map[int]NewErrorFun)
// eventOrError corresponds to values that can be either an event or an
// error.
type eventOrError interface{}
// NewId generates a new unused ID for use with requests like CreateWindow.
// If no new ids can be generated, the id returned is 0 and error is non-nil.
// This shouldn't be used directly, and is exported for use in the extension
// sub-packages.
// If you need identifiers, use the appropriate constructor.
// e.g., For a window id, use xproto.NewWindowId. For
// a new pixmap id, use xproto.NewPixmapId. And so on.
// Returns (0, io.EOF) when the connection is closed.
func (c *Conn) NewId() (uint32, error) {
xid, ok := <-c.xidChan
if !ok {
return 0, io.EOF
}
if xid.err != nil {
return 0, xid.err
}
return xid.id, nil
}
// xid encapsulates a resource identifier being sent over the Conn.xidChan
// channel. If no new resource id can be generated, id is set to 0 and a
// non-nil error is set in xid.err.
type xid struct {
id uint32
err error
}
// generateXids sends new Ids down the channel for NewId to use.
// generateXids should be run in its own goroutine.
// This needs to be updated to use the XC Misc extension once we run out of
// new ids.
// Thanks to libxcb/src/xcb_xid.c. This code is greatly inspired by it.
func (c *Conn) generateXIds() {
defer close(c.xidChan)
// This requires some explanation. From the horse's mouth:
// "The resource-id-mask contains a single contiguous set of bits (at least
// 18). The client allocates resource IDs for types WINDOW, PIXMAP,
// CURSOR, FONT, GCONTEXT, and COLORMAP by choosing a value with only some
// subset of these bits set and ORing it with resource-id-base. Only values
// constructed in this way can be used to name newly created resources over
// this connection."
// So for example (using 8 bit integers), the mask might look like:
// 00111000
// So that valid values would be 00101000, 00110000, 00001000, and so on.
// Thus, the idea is to increment it by the place of the last least
// significant '1'. In this case, that value would be 00001000. To get
// that value, we can AND the original mask with its two's complement:
// 00111000 & 11001000 = 00001000.
// And we use that value to increment the last resource id to get a new one.
// (And then, of course, we OR it with resource-id-base.)
inc := c.setupResourceIdMask & -c.setupResourceIdMask
max := c.setupResourceIdMask
last := uint32(0)
for {
id := xid{}
if last > 0 && last >= max-inc+1 {
// TODO: Use the XC Misc extension to look for released ids.
id = xid{
id: 0,
err: errors.New("There are no more available resource identifiers."),
}
} else {
last += inc
id = xid{
id: last | c.setupResourceIdBase,
err: nil,
}
}
select {
case c.xidChan <- id:
case <-c.doneSend:
// c.sendRequests is down and since this id is used by requests, we don't need this goroutine running anymore.
return
}
}
}
// newSeqId fetches the next sequence id from the Conn.seqChan channel.
func (c *Conn) newSequenceId() uint16 {
return <-c.seqChan
}
// generateSeqIds returns new sequence ids. It is meant to be run in its
// own goroutine.
// A sequence id is generated for *every* request. It's the identifier used
// to match up replies with requests.
// Since sequence ids can only be 16 bit integers we start over at zero when it
// comes time to wrap.
// N.B. As long as the cookie buffer is less than 2^16, there are no limitations
// on the number (or kind) of requests made in sequence.
func (c *Conn) generateSeqIds() {
defer close(c.seqChan)
seqid := uint16(1)
for {
select {
case c.seqChan <- seqid:
if seqid == uint16((1<<16)-1) {
seqid = 0
} else {
seqid++
}
case <-c.doneSend:
// c.sendRequests is down and since only that function uses sequence ids (via newSequenceId method), we don't need this goroutine running anymore.
return
}
}
}
// request encapsulates a buffer of raw bytes (containing the request data)
// and a cookie, which when combined represents a single request.
// The cookie is used to match up the reply/error.
type request struct {
buf []byte
cookie *Cookie
// seq is closed when the request (cookie) has been sequenced by the Conn.
seq chan struct{}
}
// NewRequest takes the bytes and a cookie of a particular request, constructs
// a request type, and sends it over the Conn.reqChan channel.
// Note that the sequence number is added to the cookie after it is sent
// over the request channel, but before it is sent to X.
//
// Note that you may safely use NewRequest to send arbitrary byte requests
// to X. The resulting cookie can be used just like any normal cookie and
// abides by the same rules, except that for replies, you'll get back the
// raw byte data. This may be useful for performance critical sections where
// every allocation counts, since all X requests in XGB allocate a new byte
// slice. In contrast, NewRequest allocates one small request struct and
// nothing else. (Except when the cookie buffer is full and has to be flushed.)
//
// If you're using NewRequest manually, you'll need to use NewCookie to create
// a new cookie.
//
// In all likelihood, you should be able to copy and paste with some minor
// edits the generated code for the request you want to issue.
func (c *Conn) NewRequest(buf []byte, cookie *Cookie) {
seq := make(chan struct{})
select {
case c.reqChan <- &request{buf: buf, cookie: cookie, seq: seq}:
// request is in buffer
// wait until request is processed or connection is closed
select {
case <-seq:
// request was successfully sent to X server
case <-c.doneSend:
// c.sendRequests is down, your request was not handled
}
case <-c.doneSend:
// c.sendRequests is down, nobody is listening to your requests
}
}
// sendRequests is run as a single goroutine that takes requests and writes
// the bytes to the wire and adds the cookie to the cookie queue.
// It is meant to be run as its own goroutine.
func (c *Conn) sendRequests() {
defer close(c.cookieChan)
defer c.conn.Close()
defer close(c.doneSend)
for {
select {
case req := <-c.reqChan:
if req == nil {
// a request by c.Close() to gracefully exit
// Flush the response reading goroutine.
if err := c.noop(); err != nil {
c.conn.Close()
<-c.doneRead
}
return
}
// ho there! if the cookie channel is nearly full, force a round
// trip to clear out the cookie buffer.
// Note that we circumvent the request channel, because we're *in*
// the request channel.
if len(c.cookieChan) == cookieBuffer-1 {
if err := c.noop(); err != nil {
// Shut everything down.
c.conn.Close()
<-c.doneRead
return
}
}
req.cookie.Sequence = c.newSequenceId()
c.cookieChan <- req.cookie
if err := c.writeBuffer(req.buf); err != nil {
c.conn.Close()
<-c.doneRead
return
}
close(req.seq)
case <-c.doneRead:
return
}
}
}
// noop circumvents the usual request sending goroutines and forces a round
// trip request manually.
func (c *Conn) noop() error {
cookie := c.NewCookie(true, true)
cookie.Sequence = c.newSequenceId()
c.cookieChan <- cookie
if err := c.writeBuffer(c.getInputFocusRequest()); err != nil {
return err
}
cookie.Reply() // wait for the buffer to clear
return nil
}
// writeBuffer is a convenience function for writing a byte slice to the wire.
func (c *Conn) writeBuffer(buf []byte) error {
if _, err := c.conn.Write(buf); err != nil {
Logger.Printf("A write error is unrecoverable: %s", err)
return err
}
return nil
}
// readResponses is a goroutine that reads events, errors and
// replies off the wire.
// When an event is read, it is always added to the event channel.
// When an error is read, if it corresponds to an existing checked cookie,
// it is sent to that cookie's error channel. Otherwise it is added to the
// event channel.
// When a reply is read, it is added to the corresponding cookie's reply
// channel. (It is an error if no such cookie exists in this case.)
// Finally, cookies that came "before" this reply are always cleaned up.
func (c *Conn) readResponses() {
defer close(c.eventChan)
defer c.conn.Close()
defer close(c.doneRead)
var (
err Error
seq uint16
replyBytes []byte
)
for {
buf := make([]byte, 32)
err, seq = nil, 0
if _, err := io.ReadFull(c.conn, buf); err != nil {
select {
case <-c.doneSend:
// gracefully closing
return
default:
}
Logger.Printf("A read error is unrecoverable: %s", err)
c.eventChan <- err
return
}
switch buf[0] {
case 0: // This is an error
// Use the constructor function for this error (that is auto
// generated) by looking it up by the error number.
newErrFun, ok := NewErrorFuncs[int(buf[1])]
if !ok {
Logger.Printf("BUG: Could not find error constructor function "+
"for error with number %d.", buf[1])
continue
}
err = newErrFun(buf)
seq = err.SequenceId()
// This error is either sent to the event channel or a specific
// cookie's error channel below.
case 1: // This is a reply
seq = Get16(buf[2:])
// check to see if this reply has more bytes to be read
size := Get32(buf[4:])
if size > 0 {
byteCount := 32 + size*4
biggerBuf := make([]byte, byteCount)
copy(biggerBuf[:32], buf)
if _, err := io.ReadFull(c.conn, biggerBuf[32:]); err != nil {
Logger.Printf("A read error is unrecoverable: %s", err)
c.eventChan <- err
return
}
replyBytes = biggerBuf
} else {
replyBytes = buf
}
// This reply is sent to its corresponding cookie below.
default: // This is an event
// Use the constructor function for this event (like for errors,
// and is also auto generated) by looking it up by the event number.
// Note that we AND the event number with 127 so that we ignore
// the most significant bit (which is set when it was sent from
// a SendEvent request).
evNum := int(buf[0] & 127)
newEventFun, ok := NewEventFuncs[evNum]
if !ok {
Logger.Printf("BUG: Could not find event construct function "+
"for event with number %d.", evNum)
continue
}
c.eventChan <- newEventFun(buf)
continue
}
// At this point, we have a sequence number and we're either
// processing an error or a reply, which are both responses to
// requests. So all we have to do is find the cookie corresponding
// to this error/reply, and send the appropriate data to it.
// In doing so, we make sure that any cookies that came before it
// are marked as successful if they are void and checked.
// If there's a cookie that requires a reply that is before this
// reply, then something is wrong.
for cookie := range c.cookieChan {
// This is the cookie we're looking for. Process and break.
if cookie.Sequence == seq {
if err != nil { // this is an error to a request
// synchronous processing
if cookie.errorChan != nil {
cookie.errorChan <- err
} else { // asynchronous processing
c.eventChan <- err
// if this is an unchecked reply, ping the cookie too
if cookie.pingChan != nil {
cookie.pingChan <- true
}
}
} else { // this is a reply
if cookie.replyChan == nil {
Logger.Printf("Reply with sequence id %d does not "+
"have a cookie with a valid reply channel.", seq)
continue
} else {
cookie.replyChan <- replyBytes
}
}
break
}
switch {
// Checked requests with replies
case cookie.replyChan != nil && cookie.errorChan != nil:
Logger.Printf("Found cookie with sequence id %d that is "+
"expecting a reply but will never get it. Currently "+
"on sequence number %d", cookie.Sequence, seq)
// Unchecked requests with replies
case cookie.replyChan != nil && cookie.pingChan != nil:
Logger.Printf("Found cookie with sequence id %d that is "+
"expecting a reply (and not an error) but will never "+
"get it. Currently on sequence number %d",
cookie.Sequence, seq)
// Checked requests without replies
case cookie.pingChan != nil && cookie.errorChan != nil:
cookie.pingChan <- true
// Unchecked requests without replies don't have any channels,
// so we can't do anything with them except let them pass by.
}
}
}
}
// processEventOrError takes an eventOrError, type switches on it,
// and returns it in Go idiomatic style.
func processEventOrError(everr eventOrError) (Event, Error) {
switch ee := everr.(type) {
case Event:
return ee, nil
case Error:
return nil, ee
case error:
// c.conn read error
case nil:
// c.eventChan is closed
default:
Logger.Printf("Invalid event/error type: %T", everr)
}
return nil, nil
}
// WaitForEvent returns the next event from the server.
// It will block until an event is available.
// WaitForEvent returns either an Event or an Error. (Returning both
// is a bug.) Note than an Error here is an X error and not an XGB error. That
// is, X errors are sometimes completely expected (and you may want to ignore
// them in some cases).
//
// If both the event and error are nil, then the connection has been closed.
func (c *Conn) WaitForEvent() (Event, Error) {
return processEventOrError(<-c.eventChan)
}
// PollForEvent returns the next event from the server if one is available in
// the internal queue without blocking. Note that unlike WaitForEvent, both
// Event and Error could be nil. Indeed, they are both nil when the event queue
// is empty.
func (c *Conn) PollForEvent() (Event, Error) {
select {
case everr := <-c.eventChan:
return processEventOrError(everr)
default:
return nil, nil
}
}

225
vend/xgb/xgb_test.go Normal file
View File

@ -0,0 +1,225 @@
package xgb
import (
"errors"
"fmt"
"testing"
"time"
)
func TestConnOnNonBlockingDummyXServer(t *testing.T) {
timeout := 10 * time.Millisecond
checkedReply := func(wantError bool) func(*Conn) error {
request := "reply"
if wantError {
request = "error"
}
return func(c *Conn) error {
cookie := c.NewCookie(true, true)
c.NewRequest([]byte(request), cookie)
_, err := cookie.Reply()
if wantError && err == nil {
return errors.New(fmt.Sprintf("checked request \"%v\" with reply resulted in nil error, want some error", request))
}
if !wantError && err != nil {
return errors.New(fmt.Sprintf("checked request \"%v\" with reply resulted in error %v, want nil error", request, err))
}
return nil
}
}
checkedNoreply := func(wantError bool) func(*Conn) error {
request := "noreply"
if wantError {
request = "error"
}
return func(c *Conn) error {
cookie := c.NewCookie(true, false)
c.NewRequest([]byte(request), cookie)
err := cookie.Check()
if wantError && err == nil {
return errors.New(fmt.Sprintf("checked request \"%v\" with no reply resulted in nil error, want some error", request))
}
if !wantError && err != nil {
return errors.New(fmt.Sprintf("checked request \"%v\" with no reply resulted in error %v, want nil error", request, err))
}
return nil
}
}
uncheckedReply := func(wantError bool) func(*Conn) error {
request := "reply"
if wantError {
request = "error"
}
return func(c *Conn) error {
cookie := c.NewCookie(false, true)
c.NewRequest([]byte(request), cookie)
_, err := cookie.Reply()
if err != nil {
return errors.New(fmt.Sprintf("unchecked request \"%v\" with reply resulted in %v, want nil", request, err))
}
return nil
}
}
uncheckedNoreply := func(wantError bool) func(*Conn) error {
request := "noreply"
if wantError {
request = "error"
}
return func(c *Conn) error {
cookie := c.NewCookie(false, false)
c.NewRequest([]byte(request), cookie)
return nil
}
}
event := func() func(*Conn) error {
return func(c *Conn) error {
_, err := c.conn.Write([]byte("event"))
if err != nil {
return errors.New(fmt.Sprintf("asked dummy server to send event, but resulted in error: %v\n", err))
}
return err
}
}
waitEvent := func(wantError bool) func(*Conn) error {
return func(c *Conn) error {
_, err := c.WaitForEvent()
if wantError && err == nil {
return errors.New(fmt.Sprintf("wait for event resulted in nil error, want some error"))
}
if !wantError && err != nil {
return errors.New(fmt.Sprintf("wait for event resulted in error %v, want nil error", err))
}
return nil
}
}
checkClosed := func(c *Conn) error {
select {
case eoe, ok := <-c.eventChan:
if ok {
return fmt.Errorf("(*Conn).eventChan should be closed, but is not and returns %v", eoe)
}
case <-time.After(timeout):
return fmt.Errorf("(*Conn).eventChan should be closed, but is not and was blocking for %v", timeout)
}
return nil
}
testCases := []struct {
description string
actions []func(*Conn) error
}{
{"close",
[]func(*Conn) error{},
},
{"double close",
[]func(*Conn) error{
func(c *Conn) error {
c.Close()
return nil
},
},
},
{"checked requests with reply",
[]func(*Conn) error{
checkedReply(false),
checkedReply(true),
checkedReply(false),
checkedReply(true),
},
},
{"checked requests no reply",
[]func(*Conn) error{
checkedNoreply(false),
checkedNoreply(true),
checkedNoreply(false),
checkedNoreply(true),
},
},
{"unchecked requests with reply",
[]func(*Conn) error{
uncheckedReply(false),
uncheckedReply(true),
waitEvent(true),
uncheckedReply(false),
event(),
waitEvent(false),
},
},
{"unchecked requests no reply",
[]func(*Conn) error{
uncheckedNoreply(false),
uncheckedNoreply(true),
waitEvent(true),
uncheckedNoreply(false),
event(),
waitEvent(false),
},
},
{"close with pending requests",
[]func(*Conn) error{
func(c *Conn) error {
c.conn.(*dNC).ReadLock()
defer c.conn.(*dNC).ReadUnlock()
c.NewRequest([]byte("reply"), c.NewCookie(false, true))
c.Close()
return nil
},
checkClosed,
},
},
{"unexpected conn close",
[]func(*Conn) error{
func(c *Conn) error {
c.conn.Close()
if ev, err := c.WaitForEvent(); ev != nil || err != nil {
return fmt.Errorf("WaitForEvent() = (%v, %v), want (nil, nil)", ev, err)
}
return nil
},
checkClosed,
},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
sclm := leaksMonitor("after server close, before testcase exit")
defer sclm.checkTesting(t)
s := newDummyNetConn("dummyX", newDummyXServerReplier())
defer s.Close()
c, err := postNewConn(&Conn{conn: s})
if err != nil {
t.Errorf("connect to dummy server error: %v", err)
return
}
defer leaksMonitor("after actions end", sclm).checkTesting(t)
for _, action := range tc.actions {
if err := action(c); err != nil {
t.Error(err)
break
}
}
recovered := false
func() {
defer func() {
if err := recover(); err != nil {
t.Errorf("(*Conn).Close() panic recover: %v", err)
recovered = true
}
}()
c.Close()
}()
if !recovered {
if err := checkClosed(c); err != nil {
t.Error(err)
}
}
})
}
}

13
vend/xgb/xgbgen/COPYING Normal file
View File

@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.

168
vend/xgb/xgbgen/context.go Normal file
View File

@ -0,0 +1,168 @@
package main
import (
"bytes"
"encoding/xml"
"fmt"
"log"
"sort"
)
// Context represents the protocol we're converting to Go, and a writer
// buffer to write the Go source to.
type Context struct {
protocol *Protocol
out *bytes.Buffer
}
func newContext() *Context {
return &Context{
out: bytes.NewBuffer([]byte{}),
}
}
// Putln calls put and adds a new line to the end of 'format'.
func (c *Context) Putln(format string, v ...interface{}) {
c.Put(format+"\n", v...)
}
// Put is a short alias to write to 'out'.
func (c *Context) Put(format string, v ...interface{}) {
_, err := c.out.WriteString(fmt.Sprintf(format, v...))
if err != nil {
log.Fatalf("There was an error writing to context buffer: %s", err)
}
}
// Morph is the big daddy of them all. It takes in an XML byte slice,
// parse it, transforms the XML types into more usable types,
// and writes Go code to the 'out' buffer.
func (c *Context) Morph(xmlBytes []byte) {
parsedXml := &XML{}
err := xml.Unmarshal(xmlBytes, parsedXml)
if err != nil {
log.Fatal(err)
}
// Parse all imports
parsedXml.Imports.Eval()
// Translate XML types to nice types
c.protocol = parsedXml.Translate(nil)
// For backwards compatibility we patch the type of the send_event field of
// PutImage to be byte
if c.protocol.Name == "shm" {
for _, req := range c.protocol.Requests {
if req.xmlName != "PutImage" {
continue
}
for _, ifield := range req.Fields {
field, ok := ifield.(*SingleField)
if !ok || field.xmlName != "send_event" {
continue
}
field.Type = &Base{ srcName: "byte", xmlName: "CARD8", size: newFixedSize(1, true) }
}
}
}
// Start with Go header.
c.Putln("// Package %s is the X client API for the %s extension.",
c.protocol.PkgName(), c.protocol.ExtXName)
c.Putln("package %s", c.protocol.PkgName())
c.Putln("")
c.Putln("// This file is automatically generated from %s.xml. "+
"Edit at your peril!", c.protocol.Name)
c.Putln("")
// Write imports. We always need to import at least xgb.
// We also need to import xproto if it's an extension.
c.Putln("import (")
c.Putln("\"github.com/jezek/xgb\"")
c.Putln("")
if c.protocol.isExt() {
c.Putln("\"github.com/jezek/xgb/xproto\"")
}
sort.Sort(Protocols(c.protocol.Imports))
for _, imp := range c.protocol.Imports {
// We always import xproto, so skip it if it's explicitly imported
if imp.Name == "xproto" {
continue
}
c.Putln("\"github.com/jezek/xgb/%s\"", imp.Name)
}
c.Putln(")")
c.Putln("")
// If this is an extension, create a function to initialize the extension
// before it can be used.
if c.protocol.isExt() {
xname := c.protocol.ExtXName
c.Putln("// Init must be called before using the %s extension.",
xname)
c.Putln("func Init(c *xgb.Conn) error {")
c.Putln("reply, err := xproto.QueryExtension(c, %d, \"%s\").Reply()",
len(xname), xname)
c.Putln("switch {")
c.Putln("case err != nil:")
c.Putln("return err")
c.Putln("case !reply.Present:")
c.Putln("return xgb.Errorf(\"No extension named %s could be found on "+
"on the server.\")", xname)
c.Putln("}")
c.Putln("")
c.Putln("c.ExtLock.Lock()")
c.Putln("c.Extensions[\"%s\"] = reply.MajorOpcode", xname)
c.Putln("c.ExtLock.Unlock()")
c.Putln("for evNum, fun := range xgb.NewExtEventFuncs[\"%s\"] {",
xname)
c.Putln("xgb.NewEventFuncs[int(reply.FirstEvent) + evNum] = fun")
c.Putln("}")
c.Putln("for errNum, fun := range xgb.NewExtErrorFuncs[\"%s\"] {",
xname)
c.Putln("xgb.NewErrorFuncs[int(reply.FirstError) + errNum] = fun")
c.Putln("}")
c.Putln("return nil")
c.Putln("}")
c.Putln("")
// Make sure newExtEventFuncs["EXT_NAME"] map is initialized.
// Same deal for newExtErrorFuncs["EXT_NAME"]
c.Putln("func init() {")
c.Putln("xgb.NewExtEventFuncs[\"%s\"] = make(map[int]xgb.NewEventFun)",
xname)
c.Putln("xgb.NewExtErrorFuncs[\"%s\"] = make(map[int]xgb.NewErrorFun)",
xname)
c.Putln("}")
c.Putln("")
} else {
// In the xproto package, we must provide a Setup function that uses
// SetupBytes in xgb.Conn to return a SetupInfo structure.
c.Putln("// Setup parses the setup bytes retrieved when")
c.Putln("// connecting into a SetupInfo struct.")
c.Putln("func Setup(c *xgb.Conn) *SetupInfo {")
c.Putln("setup := new(SetupInfo)")
c.Putln("SetupInfoRead(c.SetupBytes, setup)")
c.Putln("return setup")
c.Putln("}")
c.Putln("")
c.Putln("// DefaultScreen gets the default screen info from SetupInfo.")
c.Putln("func (s *SetupInfo) DefaultScreen(c *xgb.Conn) *ScreenInfo {")
c.Putln("return &s.Roots[c.DefaultScreen]")
c.Putln("}")
c.Putln("")
}
// Now write Go source code
sort.Sort(Types(c.protocol.Types))
sort.Sort(Requests(c.protocol.Requests))
for _, typ := range c.protocol.Types {
typ.Define(c)
}
for _, req := range c.protocol.Requests {
req.Define(c)
}
}

74
vend/xgb/xgbgen/doc.go Normal file
View File

@ -0,0 +1,74 @@
/*
xgbgen constructs Go source files from xproto XML description files. xgbgen
accomplishes the same task as the Python code generator for XCB and xpyb.
Usage:
xgbgen [flags] some-protocol.xml
The flags are:
--proto-path path
The path to a directory containing xproto XML description files.
This is only necessary when 'some-protocol.xml' imports other
protocol files.
--gofmt=true
When false, the outputted Go code will not be gofmt'd. And it won't
be very pretty at all. This is typically useful if there are syntax
errors that need to be debugged in code generation. gofmt will hiccup;
this will allow you to see the raw code.
How it works
xgbgen works by parsing the input XML file using Go's encoding/xml package.
The majority of this work is done in xml.go and xml_fields.go, where the
appropriate types are declared.
Due to the nature of the XML in the protocol description files, the types
required to parse the XML are not well suited to reasoning about code
generation. Because of this, all data parsed in the XML types is translated
into more reasonable types. This translation is done in translation.go,
and is mainly grunt work. (The only interesting tidbits are the translation
of XML names to Go source names, and connecting fields with their
appropriate types.)
The organization of these types is greatly
inspired by the description of the XML found here:
http://cgit.freedesktop.org/xcb/proto/tree/doc/xml-xcb.txt
These types come with a lot of supporting methods to make their use in
code generation easier. They can be found in expression.go, field.go,
protocol.go, request_reply.go and type.go. Of particular interest are
expression evaluation and size calculation (in bytes).
These types also come with supporting methods that convert their
representation into Go source code. I've quartered such methods in
go.go, go_error.go, go_event.go, go_list.go, go_request_reply.go,
go_single_field.go, go_struct.go and go_union.go. The idea is to keep
as much of the Go specific code generation in one area as possible. Namely,
while not *all* Go related code is found in the 'go*.go' files, *most*
of it is. (If there's any interest in using xgbgen for other languages,
I'd be happy to try and make xgbgen a little more friendly in this regard.
I did, however, design xgbgen with this in mind, so it shouldn't involve
anything as serious as a re-design.)
Why
I wrote xgbgen because I found the existing code generator that was written in
Python to be unwieldy. In particular, static and strong typing greatly helped
me reason better about the code generation task.
What does not work
The core X protocol should be completely working. As far as I know, most
extensions should work too, although I've only tested (and not much) the
Xinerama and RandR extensions.
XKB does not work. I don't have any real plans of working on this unless there
is demand and I have some test cases to work with. (i.e., even if I could get
something generated for XKB, I don't have the inclination to understand it
enough to verify that it works.) XKB poses several extremely difficult
problems that XCB also has trouble with. More info on that can be found at
http://cgit.freedesktop.org/xcb/libxcb/tree/doc/xkb_issues and
http://cgit.freedesktop.org/xcb/libxcb/tree/doc/xkb_internals.
*/
package main

View File

@ -0,0 +1,436 @@
package main
import (
"fmt"
"log"
)
// Expression represents all the different forms of expressions possible in
// side an XML protocol description file. It's also received a few custom
// addendums to make applying special functions (like padding) easier.
type Expression interface {
// Concrete determines whether this particular expression can be computed
// to some constant value inside xgbgen. (The alternative is that the
// expression can only be computed with values at run time of the
// generated code.)
Concrete() bool
// Eval evaluates a concrete expression. It is an error to call Eval
// on any expression that is not concrete (or contains any sub-expression
// that is not concrete).
Eval() int
// Reduce attempts to evaluate any concrete sub-expressions.
// i.e., (1 + 2 * (5 + 1 + someSizeOfStruct) reduces to
// (3 * (6 + someSizeOfStruct)).
// 'prefix' is used preprended to any field reference name.
Reduce(prefix string) string
// String is an alias for Reduce("")
String() string
// Initialize makes sure all names in this expression and any subexpressions
// have been translated to Go source names.
Initialize(p *Protocol)
// Makes all field references relative to path
Specialize(path string) Expression
}
// Function is a custom expression not found in the XML. It's simply used
// to apply a function named in 'Name' to the Expr expression.
type Function struct {
Name string
Expr Expression
}
func (e *Function) Concrete() bool {
return false
}
func (e *Function) Eval() int {
log.Fatalf("Cannot evaluate a 'Function'. It is not concrete.")
panic("unreachable")
}
func (e *Function) Reduce(prefix string) string {
return fmt.Sprintf("%s(%s)", e.Name, e.Expr.Reduce(prefix))
}
func (e *Function) String() string {
return e.Reduce("")
}
func (e *Function) Initialize(p *Protocol) {
e.Expr.Initialize(p)
}
func (e *Function) Specialize(path string) Expression {
r := *e
r.Expr = r.Expr.Specialize(path)
return &r
}
// BinaryOp is an expression that performs some operation (defined in the XML
// file) with Expr1 and Expr2 as operands.
type BinaryOp struct {
Op string
Expr1 Expression
Expr2 Expression
}
// newBinaryOp constructs a new binary expression when both expr1 and expr2
// are not nil. If one or both are nil, then the non-nil expression is
// returned unchanged or nil is returned.
func newBinaryOp(op string, expr1, expr2 Expression) Expression {
switch {
case expr1 != nil && expr2 != nil:
return &BinaryOp{
Op: op,
Expr1: expr1,
Expr2: expr2,
}
case expr1 != nil && expr2 == nil:
return expr1
case expr1 == nil && expr2 != nil:
return expr2
case expr1 == nil && expr2 == nil:
return nil
}
panic("unreachable")
}
func (e *BinaryOp) Concrete() bool {
return e.Expr1.Concrete() && e.Expr2.Concrete()
}
func (e *BinaryOp) Eval() int {
switch e.Op {
case "+":
return e.Expr1.Eval() + e.Expr2.Eval()
case "-":
return e.Expr1.Eval() - e.Expr2.Eval()
case "*":
return e.Expr1.Eval() * e.Expr2.Eval()
case "/":
return e.Expr1.Eval() / e.Expr2.Eval()
case "&amp;":
return e.Expr1.Eval() & e.Expr2.Eval()
case "&lt;&lt;":
return int(uint(e.Expr1.Eval()) << uint(e.Expr2.Eval()))
}
log.Fatalf("Invalid binary operator '%s' for expression.", e.Op)
panic("unreachable")
}
func (e *BinaryOp) Reduce(prefix string) string {
if e.Concrete() {
return fmt.Sprintf("%d", e.Eval())
}
// An incredibly dirty hack to make sure any time we perform an operation
// on a field, we're dealing with ints...
expr1, expr2 := e.Expr1, e.Expr2
switch expr1.(type) {
case *FieldRef:
expr1 = &Function{
Name: "int",
Expr: expr1,
}
}
switch expr2.(type) {
case *FieldRef:
expr2 = &Function{
Name: "int",
Expr: expr2,
}
}
return fmt.Sprintf("(%s %s %s)",
expr1.Reduce(prefix), e.Op, expr2.Reduce(prefix))
}
func (e *BinaryOp) String() string {
return e.Reduce("")
}
func (e *BinaryOp) Initialize(p *Protocol) {
e.Expr1.Initialize(p)
e.Expr2.Initialize(p)
}
func (e *BinaryOp) Specialize(path string) Expression {
r := *e
r.Expr1 = r.Expr1.Specialize(path)
r.Expr2 = r.Expr2.Specialize(path)
return &r
}
// UnaryOp is the same as BinaryOp, except it's a unary operator with only
// one sub-expression.
type UnaryOp struct {
Op string
Expr Expression
}
func (e *UnaryOp) Concrete() bool {
return e.Expr.Concrete()
}
func (e *UnaryOp) Eval() int {
switch e.Op {
case "~":
return ^e.Expr.Eval()
}
log.Fatalf("Invalid unary operator '%s' for expression.", e.Op)
panic("unreachable")
}
func (e *UnaryOp) Reduce(prefix string) string {
if e.Concrete() {
return fmt.Sprintf("%d", e.Eval())
}
return fmt.Sprintf("(%s (%s))", e.Op, e.Expr.Reduce(prefix))
}
func (e *UnaryOp) String() string {
return e.Reduce("")
}
func (e *UnaryOp) Initialize(p *Protocol) {
e.Expr.Initialize(p)
}
func (e *UnaryOp) Specialize(path string) Expression {
r := *e
r.Expr = r.Expr.Specialize(path)
return &r
}
// Padding represents the application of the 'pad' function to some
// sub-expression.
type Padding struct {
Expr Expression
}
func (e *Padding) Concrete() bool {
return e.Expr.Concrete()
}
func (e *Padding) Eval() int {
return pad(e.Expr.Eval())
}
func (e *Padding) Reduce(prefix string) string {
if e.Concrete() {
return fmt.Sprintf("%d", e.Eval())
}
return fmt.Sprintf("xgb.Pad(%s)", e.Expr.Reduce(prefix))
}
func (e *Padding) String() string {
return e.Reduce("")
}
func (e *Padding) Initialize(p *Protocol) {
e.Expr.Initialize(p)
}
func (e *Padding) Specialize(path string) Expression {
r := *e
r.Expr = r.Expr.Specialize(path)
return &r
}
// PopCount represents the application of the 'PopCount' function to
// some sub-expression.
type PopCount struct {
Expr Expression
}
func (e *PopCount) Concrete() bool {
return e.Expr.Concrete()
}
func (e *PopCount) Eval() int {
return int(popCount(uint(e.Expr.Eval())))
}
func (e *PopCount) Reduce(prefix string) string {
if e.Concrete() {
return fmt.Sprintf("%d", e.Eval())
}
return fmt.Sprintf("xgb.PopCount(%s)", e.Expr.Reduce(prefix))
}
func (e *PopCount) String() string {
return e.Reduce("")
}
func (e *PopCount) Initialize(p *Protocol) {
e.Expr.Initialize(p)
}
func (e *PopCount) Specialize(path string) Expression {
r := *e
r.Expr = r.Expr.Specialize(path)
return &r
}
// Value represents some constant integer.
type Value struct {
v int
}
func (e *Value) Concrete() bool {
return true
}
func (e *Value) Eval() int {
return e.v
}
func (e *Value) Reduce(prefix string) string {
return fmt.Sprintf("%d", e.v)
}
func (e *Value) String() string {
return e.Reduce("")
}
func (e *Value) Initialize(p *Protocol) {}
func (e *Value) Specialize(path string) Expression {
return e
}
// Bit represents some bit whose value is computed by '1 << bit'.
type Bit struct {
b int
}
func (e *Bit) Concrete() bool {
return true
}
func (e *Bit) Eval() int {
return int(1 << uint(e.b))
}
func (e *Bit) Reduce(prefix string) string {
return fmt.Sprintf("%d", e.Eval())
}
func (e *Bit) String() string {
return e.Reduce("")
}
func (e *Bit) Initialize(p *Protocol) {}
func (e *Bit) Specialize(path string) Expression {
return e
}
// FieldRef represents a reference to some variable in the generated code
// with name Name.
type FieldRef struct {
Name string
}
func (e *FieldRef) Concrete() bool {
return false
}
func (e *FieldRef) Eval() int {
log.Fatalf("Cannot evaluate a 'FieldRef'. It is not concrete.")
panic("unreachable")
}
func (e *FieldRef) Reduce(prefix string) string {
val := e.Name
if len(prefix) > 0 {
val = fmt.Sprintf("%s%s", prefix, val)
}
return val
}
func (e *FieldRef) String() string {
return e.Reduce("")
}
func (e *FieldRef) Initialize(p *Protocol) {
e.Name = SrcName(p, e.Name)
}
func (e *FieldRef) Specialize(path string) Expression {
return &FieldRef{Name: path + "." + e.Name}
}
// EnumRef represents a reference to some enumeration field.
// EnumKind is the "group" an EnumItem is the name of the specific enumeration
// value inside that group.
type EnumRef struct {
EnumKind Type
EnumItem string
}
func (e *EnumRef) Concrete() bool {
return false
}
func (e *EnumRef) Eval() int {
log.Fatalf("Cannot evaluate an 'EnumRef'. It is not concrete.")
panic("unreachable")
}
func (e *EnumRef) Reduce(prefix string) string {
return fmt.Sprintf("%s%s", e.EnumKind, e.EnumItem)
}
func (e *EnumRef) String() string {
return e.Reduce("")
}
func (e *EnumRef) Initialize(p *Protocol) {
e.EnumKind = e.EnumKind.(*Translation).RealType(p)
e.EnumItem = SrcName(p, e.EnumItem)
}
func (e *EnumRef) Specialize(path string) Expression {
return e
}
// SumOf represents a summation of the variable in the generated code named by
// Name. It is not currently used. (It's XKB voodoo.)
type SumOf struct {
Name string
}
func (e *SumOf) Concrete() bool {
return false
}
func (e *SumOf) Eval() int {
log.Fatalf("Cannot evaluate a 'SumOf'. It is not concrete.")
panic("unreachable")
}
func (e *SumOf) Reduce(prefix string) string {
if len(prefix) > 0 {
return fmt.Sprintf("sum(%s%s)", prefix, e.Name)
}
return fmt.Sprintf("sum(%s)", e.Name)
}
func (e *SumOf) String() string {
return e.Reduce("")
}
func (e *SumOf) Initialize(p *Protocol) {
e.Name = SrcName(p, e.Name)
}
func (e *SumOf) Specialize(path string) Expression {
return e
}

397
vend/xgb/xgbgen/field.go Normal file
View File

@ -0,0 +1,397 @@
package main
import (
"fmt"
"log"
"strings"
)
// Field corresponds to any field described in an XML protocol description
// file. This includes struct fields, union fields, request fields,
// reply fields and so on.
// To make code generation easier, fields that have types are also stored.
// Note that not all fields support all methods defined in this interface.
// For instance, a padding field does not have a source name.
type Field interface {
// Initialize sets up the source name of this field.
Initialize(p *Protocol)
// SrcName is the Go source name of this field.
SrcName() string
// XmlName is the name of this field from the XML file.
XmlName() string
// SrcType is the Go source type name of this field.
SrcType() string
// Size returns an expression that computes the size (in bytes)
// of this field.
Size() Size
// Define writes the Go code to declare this field (in a struct definition).
Define(c *Context)
// Read writes the Go code to convert a byte slice to a Go value
// of this field.
// 'prefix' is the prefix of the name of the Go value.
Read(c *Context, prefix string)
// Write writes the Go code to convert a Go value to a byte slice of
// this field.
// 'prefix' is the prefix of the name of the Go value.
Write(c *Context, prefix string)
}
func (pad *PadField) Initialize(p *Protocol) {}
// PadField represents any type of padding. It is omitted from
// definitions, but is used in Read/Write to increment the buffer index.
// It is also used in size calculation.
type PadField struct {
Bytes uint
Align uint16
}
func (p *PadField) SrcName() string {
panic("illegal to take source name of a pad field")
}
func (p *PadField) XmlName() string {
panic("illegal to take XML name of a pad field")
}
func (f *PadField) SrcType() string {
panic("it is illegal to call SrcType on a PadField field")
}
func (p *PadField) Size() Size {
if p.Align > 0 {
return newFixedSize(uint(p.Align), false)
} else {
return newFixedSize(p.Bytes, true)
}
}
type RequiredStartAlign struct {
}
func (f *RequiredStartAlign) Initialize(p *Protocol) { }
func (f *RequiredStartAlign) SrcName() string {
panic("illegal to take source name of a required_start_align field")
}
func (f *RequiredStartAlign) XmlName() string {
panic("illegal to take XML name of a required_start_align field")
}
func (f *RequiredStartAlign) SrcType() string {
panic("it is illegal to call SrcType on a required_start_align field")
}
func (f *RequiredStartAlign) Size() Size {
return newFixedSize(0, true)
}
func (f *RequiredStartAlign) Define(c *Context) { }
func (f *RequiredStartAlign) Read(c *Context, prefix string) { }
func (f *RequiredStartAlign) Write(c *Context, prefix string) { }
// SingleField represents most of the fields in an XML protocol description.
// It corresponds to any single value.
type SingleField struct {
srcName string
xmlName string
Type Type
}
func (f *SingleField) Initialize(p *Protocol) {
f.srcName = SrcName(p, f.XmlName())
f.Type = f.Type.(*Translation).RealType(p)
}
func (f *SingleField) SrcName() string {
if f.srcName == "Bytes" {
return "Bytes_"
}
return f.srcName
}
func (f *SingleField) XmlName() string {
return f.xmlName
}
func (f *SingleField) SrcType() string {
return f.Type.SrcName()
}
func (f *SingleField) Size() Size {
return f.Type.Size()
}
// ListField represents a list of values.
type ListField struct {
srcName string
xmlName string
Type Type
LengthExpr Expression
}
func (f *ListField) SrcName() string {
return f.srcName
}
func (f *ListField) XmlName() string {
return f.xmlName
}
func (f *ListField) SrcType() string {
if strings.ToLower(f.Type.XmlName()) == "char" {
return fmt.Sprintf("string")
}
return fmt.Sprintf("[]%s", f.Type.SrcName())
}
// Length computes the *number* of values in a list.
// If this ListField does not have any length expression, we throw our hands
// up and simply compute the 'len' of the field name of this list.
func (f *ListField) Length() Size {
if f.LengthExpr == nil {
return newExpressionSize(&Function{
Name: "len",
Expr: &FieldRef{
Name: f.SrcName(),
},
}, true)
}
return newExpressionSize(f.LengthExpr, true)
}
// Size computes the *size* of a list (in bytes).
// It it typically a simple matter of multiplying the length of the list by
// the size of the type of the list.
// But if it's a list of struct where the struct has a list field, we use a
// special function written in go_struct.go to compute the size (since the
// size in this case can only be computed recursively).
func (f *ListField) Size() Size {
elsz := f.Type.Size()
simpleLen := &Padding{
Expr: newBinaryOp("*", f.Length().Expression, elsz.Expression),
}
switch field := f.Type.(type) {
case *Struct:
if field.HasList() {
sizeFun := &Function{
Name: fmt.Sprintf("%sListSize", f.Type.SrcName()),
Expr: &FieldRef{Name: f.SrcName()},
}
return newExpressionSize(sizeFun, elsz.exact)
} else {
return newExpressionSize(simpleLen, elsz.exact)
}
case *Union:
return newExpressionSize(simpleLen, elsz.exact)
case *Base:
return newExpressionSize(simpleLen, elsz.exact)
case *Resource:
return newExpressionSize(simpleLen, elsz.exact)
case *TypeDef:
return newExpressionSize(simpleLen, elsz.exact)
default:
log.Panicf("Cannot compute list size with type '%T'.", f.Type)
}
panic("unreachable")
}
func (f *ListField) Initialize(p *Protocol) {
f.srcName = SrcName(p, f.XmlName())
f.Type = f.Type.(*Translation).RealType(p)
if f.LengthExpr != nil {
f.LengthExpr.Initialize(p)
}
}
// LocalField is exactly the same as a regular SingleField, except it isn't
// sent over the wire. (i.e., it's probably used to compute an ExprField).
type LocalField struct {
*SingleField
}
// ExprField is a field that is not parameterized, but is computed from values
// of other fields.
type ExprField struct {
srcName string
xmlName string
Type Type
Expr Expression
}
func (f *ExprField) SrcName() string {
return f.srcName
}
func (f *ExprField) XmlName() string {
return f.xmlName
}
func (f *ExprField) SrcType() string {
return f.Type.SrcName()
}
func (f *ExprField) Size() Size {
return f.Type.Size()
}
func (f *ExprField) Initialize(p *Protocol) {
f.srcName = SrcName(p, f.XmlName())
f.Type = f.Type.(*Translation).RealType(p)
f.Expr.Initialize(p)
}
// ValueField represents two fields in one: a mask and a list of 4-byte
// integers. The mask specifies which kinds of values are in the list.
// (i.e., See ConfigureWindow, CreateWindow, ChangeWindowAttributes, etc.)
type ValueField struct {
Parent interface{}
MaskType Type
MaskName string
ListName string
}
func (f *ValueField) SrcName() string {
panic("it is illegal to call SrcName on a ValueField field")
}
func (f *ValueField) XmlName() string {
panic("it is illegal to call XmlName on a ValueField field")
}
func (f *ValueField) SrcType() string {
return f.MaskType.SrcName()
}
// Size computes the size in bytes of the combination of the mask and list
// in this value field.
// The expression to compute this looks complicated, but it's really just
// the number of bits set in the mask multiplied 4 (and padded of course).
func (f *ValueField) Size() Size {
maskSize := f.MaskType.Size()
listSize := newExpressionSize(&Function{
Name: "xgb.Pad",
Expr: &BinaryOp{
Op: "*",
Expr1: &Value{v: 4},
Expr2: &PopCount{
Expr: &Function{
Name: "int",
Expr: &FieldRef{
Name: f.MaskName,
},
},
},
},
}, true)
return maskSize.Add(listSize)
}
func (f *ValueField) ListLength() Size {
return newExpressionSize(&PopCount{
Expr: &Function{
Name: "int",
Expr: &FieldRef{
Name: f.MaskName,
},
},
}, true)
}
func (f *ValueField) Initialize(p *Protocol) {
f.MaskType = f.MaskType.(*Translation).RealType(p)
f.MaskName = SrcName(p, f.MaskName)
f.ListName = SrcName(p, f.ListName)
}
// SwitchField represents a 'switch' element in the XML protocol description
// file.
// Currently we translate this to a slice of uint32 and let the user sort
// through it.
type SwitchField struct {
xmlName string
Name string
MaskName string
Expr Expression
Bitcases []*Bitcase
}
func (f *SwitchField) SrcName() string {
return f.Name
}
func (f *SwitchField) XmlName() string {
return f.xmlName
}
func (f *SwitchField) SrcType() string {
return "[]uint32"
}
func (f *SwitchField) Size() Size {
// TODO: size expression used here is not correct unless every element of
// the switch is 32 bit long. This assumption holds for xproto but may not
// hold for other protocols (xkb?)
listSize := newExpressionSize(&Function{
Name: "xgb.Pad",
Expr: &BinaryOp{
Op: "*",
Expr1: &Value{v: 4},
Expr2: &PopCount{
Expr: &Function{
Name: "int",
Expr: &FieldRef{
Name: f.MaskName,
},
},
},
},
}, true)
return listSize
}
func (f *SwitchField) ListLength() Size {
return newExpressionSize(&PopCount{
Expr: &Function{
Name: "int",
Expr: &FieldRef{
Name: f.MaskName,
},
},
}, true)
}
func (f *SwitchField) Initialize(p *Protocol) {
f.xmlName = f.Name
f.Name = SrcName(p, f.Name)
f.Expr.Initialize(p)
fieldref, ok := f.Expr.(*FieldRef)
if !ok {
panic("switch field's expression not a fieldref")
}
f.MaskName = SrcName(p, fieldref.Name)
for _, bitcase := range f.Bitcases {
bitcase.Expr.Initialize(p)
for _, field := range bitcase.Fields {
field.Initialize(p)
}
}
}
// Bitcase represents a single bitcase inside a switch expression.
// It is not currently used. (i.e., it's XKB voodoo.)
type Bitcase struct {
Fields []Field
Expr Expression
}

220
vend/xgb/xgbgen/go.go Normal file
View File

@ -0,0 +1,220 @@
package main
import (
"fmt"
)
// BaseTypeMap is a map from X base types to Go types.
// X base types should correspond to the smallest set of X types
// that can be used to rewrite ALL X types in terms of Go types.
// That is, if you remove any of the following types, at least one
// XML protocol description will produce an invalid Go program.
// The types on the left *never* show themselves in the source.
var BaseTypeMap = map[string]string{
"CARD8": "byte",
"CARD16": "uint16",
"CARD32": "uint32",
"INT8": "int8",
"INT16": "int16",
"INT32": "int32",
"BYTE": "byte",
"BOOL": "bool",
"float": "float64",
"double": "float64",
"char": "byte",
"void": "byte",
}
// BaseTypeSizes should have precisely the same keys as in BaseTypeMap,
// and the values should correspond to the size of the type in bytes.
var BaseTypeSizes = map[string]uint{
"CARD8": 1,
"CARD16": 2,
"CARD32": 4,
"INT8": 1,
"INT16": 2,
"INT32": 4,
"BYTE": 1,
"BOOL": 1,
"float": 4,
"double": 8,
"char": 1,
"void": 1,
// Id is a special type used to determine the size of all Xid types.
// "Id" is not actually written in the source.
"Id": 4,
}
// TypeMap is a map from types in the XML to type names that is used
// in the functions that follow. Basically, every occurrence of the key
// type is replaced with the value type.
var TypeMap = map[string]string{
"VISUALTYPE": "VisualInfo",
"DEPTH": "DepthInfo",
"SCREEN": "ScreenInfo",
"Setup": "SetupInfo",
}
// NameMap is the same as TypeMap, but for names.
var NameMap = map[string]string{}
// Reading, writing and defining...
// Base types
func (b *Base) Define(c *Context) {
c.Putln("// Skipping definition for base type '%s'",
SrcName(c.protocol, b.XmlName()))
c.Putln("")
}
// Enum types
func (enum *Enum) Define(c *Context) {
c.Putln("const (")
for _, item := range enum.Items {
c.Putln("%s%s = %d", enum.SrcName(), item.srcName, item.Expr.Eval())
}
c.Putln(")")
c.Putln("")
}
// Resource types
func (res *Resource) Define(c *Context) {
c.Putln("type %s uint32", res.SrcName())
c.Putln("")
c.Putln("func New%sId(c *xgb.Conn) (%s, error) {",
res.SrcName(), res.SrcName())
c.Putln("id, err := c.NewId()")
c.Putln("if err != nil {")
c.Putln("return 0, err")
c.Putln("}")
c.Putln("return %s(id), nil", res.SrcName())
c.Putln("}")
c.Putln("")
}
// TypeDef types
func (td *TypeDef) Define(c *Context) {
c.Putln("type %s %s", td.srcName, td.Old.SrcName())
c.Putln("")
}
// Field definitions, reads and writes.
// Pad fields
func (f *PadField) Define(c *Context) {
if f.Align > 0 {
c.Putln("// alignment gap to multiple of %d", f.Align)
} else {
c.Putln("// padding: %d bytes", f.Bytes)
}
}
func (f *PadField) Read(c *Context, prefix string) {
if f.Align > 0 {
c.Putln("b = (b + %d) & ^%d // alignment gap", f.Align-1, f.Align-1)
} else {
c.Putln("b += %s // padding", f.Size())
}
}
func (f *PadField) Write(c *Context, prefix string) {
if f.Align > 0 {
c.Putln("b = (b + %d) & ^%d // alignment gap", f.Align-1, f.Align-1)
} else {
c.Putln("b += %s // padding", f.Size())
}
}
// Local fields
func (f *LocalField) Define(c *Context) {
c.Putln("// local field: %s %s", f.SrcName(), f.Type.SrcName())
panic("unreachable")
}
func (f *LocalField) Read(c *Context, prefix string) {
c.Putln("// reading local field: %s (%s) :: %s",
f.SrcName(), f.Size(), f.Type.SrcName())
panic("unreachable")
}
func (f *LocalField) Write(c *Context, prefix string) {
c.Putln("// skip writing local field: %s (%s) :: %s",
f.SrcName(), f.Size(), f.Type.SrcName())
}
// Expr fields
func (f *ExprField) Define(c *Context) {
c.Putln("// expression field: %s %s (%s)",
f.SrcName(), f.Type.SrcName(), f.Expr)
panic("unreachable")
}
func (f *ExprField) Read(c *Context, prefix string) {
c.Putln("// reading expression field: %s (%s) (%s) :: %s",
f.SrcName(), f.Size(), f.Expr, f.Type.SrcName())
panic("unreachable")
}
func (f *ExprField) Write(c *Context, prefix string) {
// Special case for bools, grrr.
if f.Type.SrcName() == "bool" {
c.Putln("buf[b] = byte(%s)", f.Expr.Reduce(prefix))
c.Putln("b += 1")
} else {
WriteSimpleSingleField(c, f.Expr.Reduce(prefix), f.Type)
}
}
// Value field
func (f *ValueField) Define(c *Context) {
c.Putln("%s %s", f.MaskName, f.SrcType())
c.Putln("%s []uint32", f.ListName)
}
func (f *ValueField) Read(c *Context, prefix string) {
ReadSimpleSingleField(c,
fmt.Sprintf("%s%s", prefix, f.MaskName), f.MaskType)
c.Putln("")
c.Putln("%s%s = make([]uint32, %s)",
prefix, f.ListName, f.ListLength().Reduce(prefix))
c.Putln("for i := 0; i < %s; i++ {", f.ListLength().Reduce(prefix))
c.Putln("%s%s[i] = xgb.Get32(buf[b:])", prefix, f.ListName)
c.Putln("b += 4")
c.Putln("}")
c.Putln("b = xgb.Pad(b)")
}
func (f *ValueField) Write(c *Context, prefix string) {
WriteSimpleSingleField(c,
fmt.Sprintf("%s%s", prefix, f.MaskName), f.MaskType)
c.Putln("for i := 0; i < %s; i++ {", f.ListLength().Reduce(prefix))
c.Putln("xgb.Put32(buf[b:], %s%s[i])", prefix, f.ListName)
c.Putln("b += 4")
c.Putln("}")
c.Putln("b = xgb.Pad(b)")
}
// Switch field
func (f *SwitchField) Define(c *Context) {
c.Putln("%s []uint32", f.Name)
}
func (f *SwitchField) Read(c *Context, prefix string) {
c.Putln("")
c.Putln("%s%s = make([]uint32, %s)",
prefix, f.Name, f.ListLength().Reduce(prefix))
c.Putln("for i := 0; i < %s; i++ {", f.ListLength().Reduce(prefix))
c.Putln("%s%s[i] = xgb.Get32(buf[b:])", prefix, f.Name)
c.Putln("b += 4")
c.Putln("}")
c.Putln("b = xgb.Pad(b)")
}
func (f *SwitchField) Write(c *Context, prefix string) {
c.Putln("for i := 0; i < %s; i++ {", f.ListLength().Reduce(prefix))
c.Putln("xgb.Put32(buf[b:], %s%s[i])", prefix, f.Name)
c.Putln("b += 4")
c.Putln("}")
c.Putln("b = xgb.Pad(b)")
}

179
vend/xgb/xgbgen/go_error.go Normal file
View File

@ -0,0 +1,179 @@
package main
import (
"fmt"
)
// Error types
func (e *Error) Define(c *Context) {
c.Putln("// %s is the error number for a %s.", e.ErrConst(), e.ErrConst())
c.Putln("const %s = %d", e.ErrConst(), e.Number)
c.Putln("")
c.Putln("type %s struct {", e.ErrType())
c.Putln("Sequence uint16")
c.Putln("NiceName string")
for _, field := range e.Fields {
field.Define(c)
}
c.Putln("}")
c.Putln("")
// Read defines a function that transforms a byte slice into this
// error struct.
e.Read(c)
// Makes sure this error type implements the xgb.Error interface.
e.ImplementsError(c)
// Let's the XGB event loop read this error.
c.Putln("func init() {")
if c.protocol.isExt() {
c.Putln("xgb.NewExtErrorFuncs[\"%s\"][%d] = %sNew",
c.protocol.ExtXName, e.Number, e.ErrType())
} else {
c.Putln("xgb.NewErrorFuncs[%d] = %sNew", e.Number, e.ErrType())
}
c.Putln("}")
c.Putln("")
}
func (e *Error) Read(c *Context) {
c.Putln("// %sNew constructs a %s value that implements xgb.Error from "+
"a byte slice.", e.ErrType(), e.ErrType())
c.Putln("func %sNew(buf []byte) xgb.Error {", e.ErrType())
c.Putln("v := %s{}", e.ErrType())
c.Putln("v.NiceName = \"%s\"", e.SrcName())
c.Putln("")
c.Putln("b := 1 // skip error determinant")
c.Putln("b += 1 // don't read error number")
c.Putln("")
c.Putln("v.Sequence = xgb.Get16(buf[b:])")
c.Putln("b += 2")
c.Putln("")
for _, field := range e.Fields {
field.Read(c, "v.")
c.Putln("")
}
c.Putln("return v")
c.Putln("}")
c.Putln("")
}
// ImplementsError writes functions to implement the XGB Error interface.
func (e *Error) ImplementsError(c *Context) {
c.Putln("// SequenceId returns the sequence id attached to the %s error.",
e.ErrConst())
c.Putln("// This is mostly used internally.")
c.Putln("func (err %s) SequenceId() uint16 {", e.ErrType())
c.Putln("return err.Sequence")
c.Putln("}")
c.Putln("")
c.Putln("// BadId returns the 'BadValue' number if one exists for the "+
"%s error. If no bad value exists, 0 is returned.", e.ErrConst())
c.Putln("func (err %s) BadId() uint32 {", e.ErrType())
if !c.protocol.isExt() {
c.Putln("return err.BadValue")
} else {
c.Putln("return 0")
}
c.Putln("}")
c.Putln("// Error returns a rudimentary string representation of the %s "+
"error.", e.ErrConst())
c.Putln("")
c.Putln("func (err %s) Error() string {", e.ErrType())
ErrorFieldString(c, e.Fields, e.ErrConst())
c.Putln("}")
c.Putln("")
}
// ErrorCopy types
func (e *ErrorCopy) Define(c *Context) {
c.Putln("// %s is the error number for a %s.", e.ErrConst(), e.ErrConst())
c.Putln("const %s = %d", e.ErrConst(), e.Number)
c.Putln("")
c.Putln("type %s %s", e.ErrType(), e.Old.(*Error).ErrType())
c.Putln("")
// Read defines a function that transforms a byte slice into this
// error struct.
e.Read(c)
// Makes sure this error type implements the xgb.Error interface.
e.ImplementsError(c)
// Let's the XGB know how to read this error.
c.Putln("func init() {")
if c.protocol.isExt() {
c.Putln("xgb.NewExtErrorFuncs[\"%s\"][%d] = %sNew",
c.protocol.ExtXName, e.Number, e.ErrType())
} else {
c.Putln("xgb.NewErrorFuncs[%d] = %sNew", e.Number, e.ErrType())
}
c.Putln("}")
c.Putln("")
}
func (e *ErrorCopy) Read(c *Context) {
c.Putln("// %sNew constructs a %s value that implements xgb.Error from "+
"a byte slice.", e.ErrType(), e.ErrType())
c.Putln("func %sNew(buf []byte) xgb.Error {", e.ErrType())
c.Putln("v := %s(%sNew(buf).(%s))",
e.ErrType(), e.Old.(*Error).ErrType(), e.Old.(*Error).ErrType())
c.Putln("v.NiceName = \"%s\"", e.SrcName())
c.Putln("return v")
c.Putln("}")
c.Putln("")
}
// ImplementsError writes functions to implement the XGB Error interface.
func (e *ErrorCopy) ImplementsError(c *Context) {
c.Putln("// SequenceId returns the sequence id attached to the %s error.",
e.ErrConst())
c.Putln("// This is mostly used internally.")
c.Putln("func (err %s) SequenceId() uint16 {", e.ErrType())
c.Putln("return err.Sequence")
c.Putln("}")
c.Putln("")
c.Putln("// BadId returns the 'BadValue' number if one exists for the "+
"%s error. If no bad value exists, 0 is returned.", e.ErrConst())
c.Putln("func (err %s) BadId() uint32 {", e.ErrType())
if !c.protocol.isExt() {
c.Putln("return err.BadValue")
} else {
c.Putln("return 0")
}
c.Putln("}")
c.Putln("")
c.Putln("// Error returns a rudimentary string representation of the %s "+
"error.", e.ErrConst())
c.Putln("func (err %s) Error() string {", e.ErrType())
ErrorFieldString(c, e.Old.(*Error).Fields, e.ErrConst())
c.Putln("}")
c.Putln("")
}
// ErrorFieldString works for both Error and ErrorCopy. It assembles all of the
// fields in an error and formats them into a single string.
func ErrorFieldString(c *Context, fields []Field, errName string) {
c.Putln("fieldVals := make([]string, 0, %d)", len(fields))
c.Putln("fieldVals = append(fieldVals, \"NiceName: \" + err.NiceName)")
c.Putln("fieldVals = append(fieldVals, "+
"xgb.Sprintf(\"Sequence: %s\", err.Sequence))", "%d")
for _, field := range fields {
switch field.(type) {
case *PadField:
continue
default:
if field.SrcType() == "string" {
c.Putln("fieldVals = append(fieldVals, \"%s: \" + err.%s)",
field.SrcName(), field.SrcName())
} else {
format := fmt.Sprintf("xgb.Sprintf(\"%s: %s\", err.%s)",
field.SrcName(), "%d", field.SrcName())
c.Putln("fieldVals = append(fieldVals, %s)", format)
}
}
}
c.Putln("return \"%s {\" + xgb.StringsJoin(fieldVals, \", \") + \"}\"",
errName)
}

211
vend/xgb/xgbgen/go_event.go Normal file
View File

@ -0,0 +1,211 @@
package main
import (
"fmt"
)
// Event types
func (e *Event) Define(c *Context) {
c.Putln("// %s is the event number for a %s.", e.SrcName(), e.EvType())
c.Putln("const %s = %d", e.SrcName(), e.Number)
c.Putln("")
c.Putln("type %s struct {", e.EvType())
if !e.NoSequence {
c.Putln("Sequence uint16")
}
for _, field := range e.Fields {
field.Define(c)
}
c.Putln("}")
c.Putln("")
// Read defines a function that transforms a byte slice into this
// event struct.
e.Read(c)
// Write defines a function that transforms this event struct into
// a byte slice.
e.Write(c)
// Makes sure that this event type is an Event interface.
c.Putln("// SequenceId returns the sequence id attached to the %s event.",
e.SrcName())
c.Putln("// Events without a sequence number (KeymapNotify) return 0.")
c.Putln("// This is mostly used internally.")
c.Putln("func (v %s) SequenceId() uint16 {", e.EvType())
if e.NoSequence {
c.Putln("return uint16(0)")
} else {
c.Putln("return v.Sequence")
}
c.Putln("}")
c.Putln("")
c.Putln("// String is a rudimentary string representation of %s.",
e.EvType())
c.Putln("func (v %s) String() string {", e.EvType())
EventFieldString(c, e.Fields, e.SrcName())
c.Putln("}")
c.Putln("")
// Let's the XGB event loop read this event.
c.Putln("func init() {")
if c.protocol.isExt() {
c.Putln("xgb.NewExtEventFuncs[\"%s\"][%d] = %sNew",
c.protocol.ExtXName, e.Number, e.EvType())
} else {
c.Putln("xgb.NewEventFuncs[%d] = %sNew", e.Number, e.EvType())
}
c.Putln("}")
c.Putln("")
}
func (e *Event) Read(c *Context) {
c.Putln("// %sNew constructs a %s value that implements xgb.Event from "+
"a byte slice.", e.EvType(), e.EvType())
c.Putln("func %sNew(buf []byte) xgb.Event {", e.EvType())
c.Putln("v := %s{}", e.EvType())
c.Putln("b := 1 // don't read event number")
c.Putln("")
for i, field := range e.Fields {
if i == 1 && !e.NoSequence {
c.Putln("v.Sequence = xgb.Get16(buf[b:])")
c.Putln("b += 2")
c.Putln("")
}
field.Read(c, "v.")
c.Putln("")
}
c.Putln("return v")
c.Putln("}")
c.Putln("")
}
func (e *Event) Write(c *Context) {
c.Putln("// Bytes writes a %s value to a byte slice.", e.EvType())
c.Putln("func (v %s) Bytes() []byte {", e.EvType())
c.Putln("buf := make([]byte, %s)", e.Size())
c.Putln("b := 0")
c.Putln("")
c.Putln("// write event number")
c.Putln("buf[b] = %d", e.Number)
c.Putln("b += 1")
c.Putln("")
for i, field := range e.Fields {
if i == 1 && !e.NoSequence {
c.Putln("b += 2 // skip sequence number")
c.Putln("")
}
field.Write(c, "v.")
c.Putln("")
}
c.Putln("return buf")
c.Putln("}")
c.Putln("")
}
// EventCopy types
func (e *EventCopy) Define(c *Context) {
c.Putln("// %s is the event number for a %s.", e.SrcName(), e.EvType())
c.Putln("const %s = %d", e.SrcName(), e.Number)
c.Putln("")
c.Putln("type %s %s", e.EvType(), e.Old.(*Event).EvType())
c.Putln("")
// Read defines a function that transforms a byte slice into this
// event struct.
e.Read(c)
// Write defines a function that transoforms this event struct into
// a byte slice.
e.Write(c)
// Makes sure that this event type is an Event interface.
c.Putln("// SequenceId returns the sequence id attached to the %s event.",
e.SrcName())
c.Putln("// Events without a sequence number (KeymapNotify) return 0.")
c.Putln("// This is mostly used internally.")
c.Putln("func (v %s) SequenceId() uint16 {", e.EvType())
if e.Old.(*Event).NoSequence {
c.Putln("return uint16(0)")
} else {
c.Putln("return v.Sequence")
}
c.Putln("}")
c.Putln("")
c.Putln("func (v %s) String() string {", e.EvType())
EventFieldString(c, e.Old.(*Event).Fields, e.SrcName())
c.Putln("}")
c.Putln("")
// Let's the XGB event loop read this event.
c.Putln("func init() {")
if c.protocol.isExt() {
c.Putln("xgb.NewExtEventFuncs[\"%s\"][%d] = %sNew",
c.protocol.ExtXName, e.Number, e.EvType())
} else {
c.Putln("xgb.NewEventFuncs[%d] = %sNew", e.Number, e.EvType())
}
c.Putln("}")
c.Putln("")
}
func (e *EventCopy) Read(c *Context) {
c.Putln("// %sNew constructs a %s value that implements xgb.Event from "+
"a byte slice.", e.EvType(), e.EvType())
c.Putln("func %sNew(buf []byte) xgb.Event {", e.EvType())
c.Putln("return %s(%sNew(buf).(%s))",
e.EvType(), e.Old.(*Event).EvType(), e.Old.(*Event).EvType())
c.Putln("}")
c.Putln("")
}
func (e *EventCopy) Write(c *Context) {
c.Putln("// Bytes writes a %s value to a byte slice.", e.EvType())
c.Putln("func (v %s) Bytes() []byte {", e.EvType())
c.Putln("buf := %s(v).Bytes()", e.Old.(*Event).EvType())
c.Putln("buf[0] = %d", e.Number)
c.Putln("return buf")
c.Putln("}")
c.Putln("")
}
// EventFieldString works for both Event and EventCopy. It assembles all of the
// fields in an event and formats them into a single string.
func EventFieldString(c *Context, fields []Field, evName string) {
c.Putln("fieldVals := make([]string, 0, %d)", len(fields))
if evName != "KeymapNotify" {
c.Putln("fieldVals = append(fieldVals, "+
"xgb.Sprintf(\"Sequence: %s\", v.Sequence))", "%d")
}
for _, field := range fields {
switch f := field.(type) {
case *PadField:
continue
case *SingleField:
switch f.Type.(type) {
case *Base:
case *Resource:
case *TypeDef:
default:
continue
}
switch field.SrcType() {
case "string":
format := fmt.Sprintf("xgb.Sprintf(\"%s: %s\", v.%s)",
field.SrcName(), "%s", field.SrcName())
c.Putln("fieldVals = append(fieldVals, %s)", format)
case "bool":
format := fmt.Sprintf("xgb.Sprintf(\"%s: %s\", v.%s)",
field.SrcName(), "%t", field.SrcName())
c.Putln("fieldVals = append(fieldVals, %s)", format)
default:
format := fmt.Sprintf("xgb.Sprintf(\"%s: %s\", v.%s)",
field.SrcName(), "%d", field.SrcName())
c.Putln("fieldVals = append(fieldVals, %s)", format)
}
}
}
c.Putln("return \"%s {\" + xgb.StringsJoin(fieldVals, \", \") + \"}\"",
evName)
}

107
vend/xgb/xgbgen/go_list.go Normal file
View File

@ -0,0 +1,107 @@
package main
import (
"fmt"
"log"
"strings"
)
// List fields
func (f *ListField) Define(c *Context) {
c.Putln("%s %s // size: %s",
f.SrcName(), f.SrcType(), f.Size())
}
func (f *ListField) Read(c *Context, prefix string) {
switch t := f.Type.(type) {
case *Resource:
length := f.LengthExpr.Reduce(prefix)
c.Putln("%s%s = make([]%s, %s)",
prefix, f.SrcName(), t.SrcName(), length)
c.Putln("for i := 0; i < int(%s); i++ {", length)
ReadSimpleSingleField(c, fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
c.Putln("}")
case *Base:
length := f.LengthExpr.Reduce(prefix)
if strings.ToLower(t.XmlName()) == "char" {
c.Putln("{")
c.Putln("byteString := make([]%s, %s)", t.SrcName(), length)
c.Putln("copy(byteString[:%s], buf[b:])", length)
c.Putln("%s%s = string(byteString)", prefix, f.SrcName())
// This is apparently a special case. The "Str" type itself
// doesn't specify any padding. I suppose it's up to the
// request/reply spec that uses it to get the padding right?
c.Putln("b += int(%s)", length)
c.Putln("}")
} else if t.SrcName() == "byte" {
c.Putln("%s%s = make([]%s, %s)",
prefix, f.SrcName(), t.SrcName(), length)
c.Putln("copy(%s%s[:%s], buf[b:])", prefix, f.SrcName(), length)
c.Putln("b += int(%s)", length)
} else {
c.Putln("%s%s = make([]%s, %s)",
prefix, f.SrcName(), t.SrcName(), length)
c.Putln("for i := 0; i < int(%s); i++ {", length)
ReadSimpleSingleField(c,
fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
c.Putln("}")
}
case *TypeDef:
length := f.LengthExpr.Reduce(prefix)
c.Putln("%s%s = make([]%s, %s)",
prefix, f.SrcName(), t.SrcName(), length)
c.Putln("for i := 0; i < int(%s); i++ {", length)
ReadSimpleSingleField(c, fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
c.Putln("}")
case *Union:
c.Putln("%s%s = make([]%s, %s)",
prefix, f.SrcName(), t.SrcName(), f.LengthExpr.Reduce(prefix))
c.Putln("b += %sReadList(buf[b:], %s%s)",
t.SrcName(), prefix, f.SrcName())
case *Struct:
c.Putln("%s%s = make([]%s, %s)",
prefix, f.SrcName(), t.SrcName(), f.LengthExpr.Reduce(prefix))
c.Putln("b += %sReadList(buf[b:], %s%s)",
t.SrcName(), prefix, f.SrcName())
default:
log.Panicf("Cannot read list field '%s' with %T type.",
f.XmlName(), f.Type)
}
}
func (f *ListField) Write(c *Context, prefix string) {
switch t := f.Type.(type) {
case *Resource:
length := f.Length().Reduce(prefix)
c.Putln("for i := 0; i < int(%s); i++ {", length)
WriteSimpleSingleField(c,
fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
c.Putln("}")
case *Base:
length := f.Length().Reduce(prefix)
if t.SrcName() == "byte" {
c.Putln("copy(buf[b:], %s%s[:%s])", prefix, f.SrcName(), length)
c.Putln("b += int(%s)", length)
} else {
c.Putln("for i := 0; i < int(%s); i++ {", length)
WriteSimpleSingleField(c,
fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
c.Putln("}")
}
case *TypeDef:
length := f.Length().Reduce(prefix)
c.Putln("for i := 0; i < int(%s); i++ {", length)
WriteSimpleSingleField(c,
fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
c.Putln("}")
case *Union:
c.Putln("b += %sListBytes(buf[b:], %s%s)",
t.SrcName(), prefix, f.SrcName())
case *Struct:
c.Putln("b += %sListBytes(buf[b:], %s%s)",
t.SrcName(), prefix, f.SrcName())
default:
log.Panicf("Cannot write list field '%s' with %T type.",
f.XmlName(), f.Type)
}
}

View File

@ -0,0 +1,242 @@
package main
import (
"fmt"
"strings"
)
func (r *Request) Define(c *Context) {
c.Putln("// %s is a cookie used only for %s requests.",
r.CookieName(), r.SrcName())
c.Putln("type %s struct {", r.CookieName())
c.Putln("*xgb.Cookie")
c.Putln("}")
c.Putln("")
if r.Reply != nil {
c.Putln("// %s sends a checked request.", r.SrcName())
c.Putln("// If an error occurs, it will be returned with the reply "+
"by calling %s.Reply()", r.CookieName())
c.Putln("func %s(c *xgb.Conn, %s) %s {",
r.SrcName(), r.ParamNameTypes(), r.CookieName())
r.CheckExt(c)
c.Putln("cookie := c.NewCookie(true, true)")
c.Putln("c.NewRequest(%s(c, %s), cookie)", r.ReqName(), r.ParamNames())
c.Putln("return %s{cookie}", r.CookieName())
c.Putln("}")
c.Putln("")
c.Putln("// %sUnchecked sends an unchecked request.", r.SrcName())
c.Putln("// If an error occurs, it can only be retrieved using " +
"xgb.WaitForEvent or xgb.PollForEvent.")
c.Putln("func %sUnchecked(c *xgb.Conn, %s) %s {",
r.SrcName(), r.ParamNameTypes(), r.CookieName())
r.CheckExt(c)
c.Putln("cookie := c.NewCookie(false, true)")
c.Putln("c.NewRequest(%s(c, %s), cookie)", r.ReqName(), r.ParamNames())
c.Putln("return %s{cookie}", r.CookieName())
c.Putln("}")
c.Putln("")
r.ReadReply(c)
} else {
c.Putln("// %s sends an unchecked request.", r.SrcName())
c.Putln("// If an error occurs, it can only be retrieved using " +
"xgb.WaitForEvent or xgb.PollForEvent.")
c.Putln("func %s(c *xgb.Conn, %s) %s {",
r.SrcName(), r.ParamNameTypes(), r.CookieName())
r.CheckExt(c)
c.Putln("cookie := c.NewCookie(false, false)")
c.Putln("c.NewRequest(%s(c, %s), cookie)", r.ReqName(), r.ParamNames())
c.Putln("return %s{cookie}", r.CookieName())
c.Putln("}")
c.Putln("")
c.Putln("// %sChecked sends a checked request.", r.SrcName())
c.Putln("// If an error occurs, it can be retrieved using "+
"%s.Check()", r.CookieName())
c.Putln("func %sChecked(c *xgb.Conn, %s) %s {",
r.SrcName(), r.ParamNameTypes(), r.CookieName())
r.CheckExt(c)
c.Putln("cookie := c.NewCookie(true, false)")
c.Putln("c.NewRequest(%s(c, %s), cookie)", r.ReqName(), r.ParamNames())
c.Putln("return %s{cookie}", r.CookieName())
c.Putln("}")
c.Putln("")
c.Putln("// Check returns an error if one occurred for checked " +
"requests that are not expecting a reply.")
c.Putln("// This cannot be called for requests expecting a reply, " +
"nor for unchecked requests.")
c.Putln("func (cook %s) Check() error {", r.CookieName())
c.Putln("return cook.Cookie.Check()")
c.Putln("}")
c.Putln("")
}
r.WriteRequest(c)
}
func (r *Request) CheckExt(c *Context) {
if !c.protocol.isExt() {
return
}
c.Putln("c.ExtLock.RLock()")
c.Putln("defer c.ExtLock.RUnlock()")
c.Putln("if _, ok := c.Extensions[\"%s\"]; !ok {", c.protocol.ExtXName)
c.Putln("panic(\"Cannot issue request '%s' using the uninitialized "+
"extension '%s'. %s.Init(connObj) must be called first.\")",
r.SrcName(), c.protocol.ExtXName, c.protocol.PkgName())
c.Putln("}")
}
func (r *Request) ReadReply(c *Context) {
c.Putln("// %s represents the data returned from a %s request.",
r.ReplyTypeName(), r.SrcName())
c.Putln("type %s struct {", r.ReplyTypeName())
c.Putln("Sequence uint16 // sequence number of the request for this reply")
c.Putln("Length uint32 // number of bytes in this reply")
for _, field := range r.Reply.Fields {
field.Define(c)
}
c.Putln("}")
c.Putln("")
c.Putln("// Reply blocks and returns the reply data for a %s request.",
r.SrcName())
c.Putln("func (cook %s) Reply() (*%s, error) {",
r.CookieName(), r.ReplyTypeName())
c.Putln("buf, err := cook.Cookie.Reply()")
c.Putln("if err != nil {")
c.Putln("return nil, err")
c.Putln("}")
c.Putln("if buf == nil {")
c.Putln("return nil, nil")
c.Putln("}")
c.Putln("return %s(buf), nil", r.ReplyName())
c.Putln("}")
c.Putln("")
c.Putln("// %s reads a byte slice into a %s value.",
r.ReplyName(), r.ReplyTypeName())
c.Putln("func %s(buf []byte) *%s {",
r.ReplyName(), r.ReplyTypeName())
c.Putln("v := new(%s)", r.ReplyTypeName())
c.Putln("b := 1 // skip reply determinant")
c.Putln("")
for i, field := range r.Reply.Fields {
field.Read(c, "v.")
c.Putln("")
if i == 0 {
c.Putln("v.Sequence = xgb.Get16(buf[b:])")
c.Putln("b += 2")
c.Putln("")
c.Putln("v.Length = xgb.Get32(buf[b:]) // 4-byte units")
c.Putln("b += 4")
c.Putln("")
}
}
c.Putln("return v")
c.Putln("}")
c.Putln("")
}
func (r *Request) WriteRequest(c *Context) {
sz := r.Size(c)
writeSize1 := func() {
if sz.exact {
c.Putln("xgb.Put16(buf[b:], uint16(size / 4)) " +
"// write request size in 4-byte units")
} else {
c.Putln("blen := b")
}
c.Putln("b += 2")
c.Putln("")
}
writeSize2 := func() {
if sz.exact {
c.Putln("return buf")
return
}
c.Putln("b = xgb.Pad(b)")
c.Putln("xgb.Put16(buf[blen:], uint16(b / 4)) " +
"// write request size in 4-byte units")
c.Putln("return buf[:b]")
}
c.Putln("// Write request to wire for %s", r.SrcName())
c.Putln("// %s writes a %s request to a byte slice.",
r.ReqName(), r.SrcName())
c.Putln("func %s(c *xgb.Conn, %s) []byte {",
r.ReqName(), r.ParamNameTypes())
c.Putln("size := %s", sz)
c.Putln("b := 0")
c.Putln("buf := make([]byte, size)")
c.Putln("")
if c.protocol.isExt() {
c.Putln("c.ExtLock.RLock()")
c.Putln("buf[b] = c.Extensions[\"%s\"]", c.protocol.ExtXName)
c.Putln("c.ExtLock.RUnlock()")
c.Putln("b += 1")
c.Putln("")
}
c.Putln("buf[b] = %d // request opcode", r.Opcode)
c.Putln("b += 1")
c.Putln("")
if len(r.Fields) == 0 {
if !c.protocol.isExt() {
c.Putln("b += 1 // padding")
}
writeSize1()
} else if c.protocol.isExt() {
writeSize1()
}
for i, field := range r.Fields {
field.Write(c, "")
c.Putln("")
if i == 0 && !c.protocol.isExt() {
writeSize1()
}
}
writeSize2()
c.Putln("}")
c.Putln("")
}
func (r *Request) ParamNames() string {
names := make([]string, 0, len(r.Fields))
for _, field := range r.Fields {
switch f := field.(type) {
case *ValueField:
names = append(names, f.MaskName)
names = append(names, f.ListName)
case *PadField:
continue
case *ExprField:
continue
default:
names = append(names, fmt.Sprintf("%s", field.SrcName()))
}
}
return strings.Join(names, ", ")
}
func (r *Request) ParamNameTypes() string {
nameTypes := make([]string, 0, len(r.Fields))
for _, field := range r.Fields {
switch f := field.(type) {
case *ValueField:
nameTypes = append(nameTypes,
fmt.Sprintf("%s %s", f.MaskName, f.MaskType.SrcName()))
nameTypes = append(nameTypes,
fmt.Sprintf("%s []uint32", f.ListName))
case *PadField:
continue
case *ExprField:
continue
case *RequiredStartAlign:
continue
default:
nameTypes = append(nameTypes,
fmt.Sprintf("%s %s", field.SrcName(), field.SrcType()))
}
}
return strings.Join(nameTypes, ", ")
}

View File

@ -0,0 +1,166 @@
package main
import (
"fmt"
"log"
)
func (f *SingleField) Define(c *Context) {
c.Putln("%s %s", f.SrcName(), f.Type.SrcName())
}
func ReadSimpleSingleField(c *Context, name string, typ Type) {
switch t := typ.(type) {
case *Resource:
c.Putln("%s = %s(xgb.Get32(buf[b:]))", name, t.SrcName())
case *TypeDef:
switch t.Size().Eval() {
case 1:
c.Putln("%s = %s(buf[b])", name, t.SrcName())
case 2:
c.Putln("%s = %s(xgb.Get16(buf[b:]))", name, t.SrcName())
case 4:
c.Putln("%s = %s(xgb.Get32(buf[b:]))", name, t.SrcName())
case 8:
c.Putln("%s = %s(xgb.Get64(buf[b:]))", name, t.SrcName())
}
case *Base:
// If this is a bool, stop short and do something special.
if t.SrcName() == "bool" {
c.Putln("if buf[b] == 1 {")
c.Putln("%s = true", name)
c.Putln("} else {")
c.Putln("%s = false", name)
c.Putln("}")
break
}
var val string
switch t.Size().Eval() {
case 1:
val = fmt.Sprintf("buf[b]")
case 2:
val = fmt.Sprintf("xgb.Get16(buf[b:])")
case 4:
val = fmt.Sprintf("xgb.Get32(buf[b:])")
case 8:
val = fmt.Sprintf("xgb.Get64(buf[b:])")
}
// We need to convert base types if they aren't uintXX or byte
ty := t.SrcName()
if ty != "byte" && ty != "uint16" && ty != "uint32" && ty != "uint64" {
val = fmt.Sprintf("%s(%s)", ty, val)
}
c.Putln("%s = %s", name, val)
default:
log.Panicf("Cannot read field '%s' as a simple field with %T type.",
name, typ)
}
c.Putln("b += %s", typ.Size())
}
func (f *SingleField) Read(c *Context, prefix string) {
switch t := f.Type.(type) {
case *Resource:
ReadSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
case *TypeDef:
ReadSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
case *Base:
ReadSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
case *Struct:
c.Putln("%s%s = %s{}", prefix, f.SrcName(), t.SrcName())
c.Putln("b += %sRead(buf[b:], &%s%s)", t.SrcName(), prefix, f.SrcName())
case *Union:
c.Putln("%s%s = %s{}", prefix, f.SrcName(), t.SrcName())
c.Putln("b += %sRead(buf[b:], &%s%s)", t.SrcName(), prefix, f.SrcName())
default:
log.Panicf("Cannot read field '%s' with %T type.", f.XmlName(), f.Type)
}
}
func WriteSimpleSingleField(c *Context, name string, typ Type) {
switch t := typ.(type) {
case *Resource:
c.Putln("xgb.Put32(buf[b:], uint32(%s))", name)
case *TypeDef:
switch t.Size().Eval() {
case 1:
c.Putln("buf[b] = byte(%s)", name)
case 2:
c.Putln("xgb.Put16(buf[b:], uint16(%s))", name)
case 4:
c.Putln("xgb.Put32(buf[b:], uint32(%s))", name)
case 8:
c.Putln("xgb.Put64(buf[b:], uint64(%s))", name)
}
case *Base:
// If this is a bool, stop short and do something special.
if t.SrcName() == "bool" {
c.Putln("if %s {", name)
c.Putln("buf[b] = 1")
c.Putln("} else {")
c.Putln("buf[b] = 0")
c.Putln("}")
break
}
switch t.Size().Eval() {
case 1:
if t.SrcName() != "byte" {
c.Putln("buf[b] = byte(%s)", name)
} else {
c.Putln("buf[b] = %s", name)
}
case 2:
if t.SrcName() != "uint16" {
c.Putln("xgb.Put16(buf[b:], uint16(%s))", name)
} else {
c.Putln("xgb.Put16(buf[b:], %s)", name)
}
case 4:
if t.SrcName() != "uint32" {
c.Putln("xgb.Put32(buf[b:], uint32(%s))", name)
} else {
c.Putln("xgb.Put32(buf[b:], %s)", name)
}
case 8:
if t.SrcName() != "uint64" {
c.Putln("xgb.Put64(buf[b:], uint64(%s))", name)
} else {
c.Putln("xgb.Put64(buf[b:], %s)", name)
}
}
default:
log.Fatalf("Cannot read field '%s' as a simple field with %T type.",
name, typ)
}
c.Putln("b += %s", typ.Size())
}
func (f *SingleField) Write(c *Context, prefix string) {
switch t := f.Type.(type) {
case *Resource:
WriteSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
case *TypeDef:
WriteSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
case *Base:
WriteSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
case *Union:
c.Putln("{")
c.Putln("unionBytes := %s%s.Bytes()", prefix, f.SrcName())
c.Putln("copy(buf[b:], unionBytes)")
c.Putln("b += len(unionBytes)")
c.Putln("}")
case *Struct:
c.Putln("{")
c.Putln("structBytes := %s%s.Bytes()", prefix, f.SrcName())
c.Putln("copy(buf[b:], structBytes)")
c.Putln("b += len(structBytes)")
c.Putln("}")
default:
log.Fatalf("Cannot read field '%s' with %T type.", f.XmlName(), f.Type)
}
}

View File

@ -0,0 +1,118 @@
package main
func (s *Struct) Define(c *Context) {
c.Putln("type %s struct {", s.SrcName())
for _, field := range s.Fields {
field.Define(c)
}
c.Putln("}")
c.Putln("")
// Write function that reads bytes and produces this struct.
s.Read(c)
// Write function that reads bytes and produces a list of this struct.
s.ReadList(c)
// Write function that writes bytes given this struct.
s.Write(c)
// Write function that writes a list of this struct.
s.WriteList(c)
// Write function that computes the size of a list of these structs,
// IF there is a list field in this struct.
if s.HasList() {
s.WriteListSize(c)
}
}
// Read for a struct creates a function 'ReadStructName' that takes a source
// byte slice (i.e., the buffer) and a destination struct, and returns
// the number of bytes read off the buffer.
// 'ReadStructName' should only be used to read raw reply data from the wire.
func (s *Struct) Read(c *Context) {
c.Putln("// %sRead reads a byte slice into a %s value.",
s.SrcName(), s.SrcName())
c.Putln("func %sRead(buf []byte, v *%s) int {", s.SrcName(), s.SrcName())
c.Putln("b := 0")
c.Putln("")
for _, field := range s.Fields {
field.Read(c, "v.")
c.Putln("")
}
c.Putln("return b")
c.Putln("}")
c.Putln("")
}
// ReadList for a struct creates a function 'ReadStructNameList' that takes
// a source (i.e., the buffer) byte slice, and a destination slice and returns
// the number of bytes read from the byte slice.
func (s *Struct) ReadList(c *Context) {
c.Putln("// %sReadList reads a byte slice into a list of %s values.",
s.SrcName(), s.SrcName())
c.Putln("func %sReadList(buf []byte, dest []%s) int {",
s.SrcName(), s.SrcName())
c.Putln("b := 0")
c.Putln("for i := 0; i < len(dest); i++ {")
c.Putln("dest[i] = %s{}", s.SrcName())
c.Putln("b += %sRead(buf[b:], &dest[i])", s.SrcName())
c.Putln("}")
c.Putln("return xgb.Pad(b)")
c.Putln("}")
c.Putln("")
}
func (s *Struct) Write(c *Context) {
c.Putln("// Bytes writes a %s value to a byte slice.", s.SrcName())
c.Putln("func (v %s) Bytes() []byte {", s.SrcName())
c.Putln("buf := make([]byte, %s)", s.Size().Reduce("v."))
c.Putln("b := 0")
c.Putln("")
for _, field := range s.Fields {
field.Write(c, "v.")
c.Putln("")
}
c.Putln("return buf[:b]")
c.Putln("}")
c.Putln("")
}
func (s *Struct) WriteList(c *Context) {
c.Putln("// %sListBytes writes a list of %s values to a byte slice.",
s.SrcName(), s.SrcName())
c.Putln("func %sListBytes(buf []byte, list []%s) int {",
s.SrcName(), s.SrcName())
c.Putln("b := 0")
c.Putln("var structBytes []byte")
c.Putln("for _, item := range list {")
c.Putln("structBytes = item.Bytes()")
c.Putln("copy(buf[b:], structBytes)")
c.Putln("b += len(structBytes)")
c.Putln("}")
c.Putln("return xgb.Pad(b)")
c.Putln("}")
c.Putln("")
}
func (s *Struct) WriteListSize(c *Context) {
c.Putln("// %sListSize computes the size (bytes) of a list of %s values.",
s.SrcName(), s.SrcName())
c.Putln("func %sListSize(list []%s) int {", s.SrcName(), s.SrcName())
c.Putln("size := 0")
if s.Size().Expression.Concrete() {
c.Putln("for _ = range list {")
} else {
c.Putln("for _, item := range list {")
}
c.Putln("size += %s", s.Size().Reduce("item."))
c.Putln("}")
c.Putln("return size")
c.Putln("}")
c.Putln("")
}

147
vend/xgb/xgbgen/go_union.go Normal file
View File

@ -0,0 +1,147 @@
package main
// Union types
func (u *Union) Define(c *Context) {
c.Putln("// %s is a represention of the %s union type.",
u.SrcName(), u.SrcName())
c.Putln("// Note that to *create* a Union, you should *never* create")
c.Putln("// this struct directly (unless you know what you're doing).")
c.Putln("// Instead use one of the following constructors for '%s':",
u.SrcName())
for _, field := range u.Fields {
c.Putln("// %s%sNew(%s %s) %s", u.SrcName(), field.SrcName(),
field.SrcName(), field.SrcType(), u.SrcName())
}
c.Putln("type %s struct {", u.SrcName())
for _, field := range u.Fields {
field.Define(c)
}
c.Putln("}")
c.Putln("")
// Write functions for each field that create instances of this
// union using the corresponding field.
u.New(c)
// Write function that reads bytes and produces this union.
u.Read(c)
// Write function that reads bytes and produces a list of this union.
u.ReadList(c)
// Write function that writes bytes given this union.
u.Write(c)
// Write function that writes a list of this union.
u.WriteList(c)
}
func (u *Union) New(c *Context) {
for _, field := range u.Fields {
c.Putln("// %s%sNew constructs a new %s union type with the %s field.",
u.SrcName(), field.SrcName(), u.SrcName(), field.SrcName())
c.Putln("func %s%sNew(%s %s) %s {",
u.SrcName(), field.SrcName(), field.SrcName(),
field.SrcType(), u.SrcName())
c.Putln("var b int")
c.Putln("buf := make([]byte, %s)", u.Size())
c.Putln("")
field.Write(c, "")
c.Putln("")
c.Putln("// Create the Union type")
c.Putln("v := %s{}", u.SrcName())
c.Putln("")
c.Putln("// Now copy buf into all fields")
c.Putln("")
for _, field2 := range u.Fields {
c.Putln("b = 0 // always read the same bytes")
field2.Read(c, "v.")
c.Putln("")
}
c.Putln("return v")
c.Putln("}")
c.Putln("")
}
}
func (u *Union) Read(c *Context) {
c.Putln("// %sRead reads a byte slice into a %s value.",
u.SrcName(), u.SrcName())
c.Putln("func %sRead(buf []byte, v *%s) int {", u.SrcName(), u.SrcName())
c.Putln("var b int")
c.Putln("")
for _, field := range u.Fields {
c.Putln("b = 0 // re-read the same bytes")
field.Read(c, "v.")
c.Putln("")
}
c.Putln("return %s", u.Size())
c.Putln("}")
c.Putln("")
}
func (u *Union) ReadList(c *Context) {
c.Putln("// %sReadList reads a byte slice into a list of %s values.",
u.SrcName(), u.SrcName())
c.Putln("func %sReadList(buf []byte, dest []%s) int {",
u.SrcName(), u.SrcName())
c.Putln("b := 0")
c.Putln("for i := 0; i < len(dest); i++ {")
c.Putln("dest[i] = %s{}", u.SrcName())
c.Putln("b += %sRead(buf[b:], &dest[i])", u.SrcName())
c.Putln("}")
c.Putln("return xgb.Pad(b)")
c.Putln("}")
c.Putln("")
}
// This is a bit tricky since writing from a Union implies that only
// the data inside ONE of the elements is actually written.
// However, we only currently support unions where every field has the
// *same* *fixed* size. Thus, we make sure to always read bytes into
// every field which allows us to simply pick the first field and write it.
func (u *Union) Write(c *Context) {
c.Putln("// Bytes writes a %s value to a byte slice.", u.SrcName())
c.Putln("// Each field in a union must contain the same data.")
c.Putln("// So simply pick the first field and write that to the wire.")
c.Putln("func (v %s) Bytes() []byte {", u.SrcName())
c.Putln("buf := make([]byte, %s)", u.Size().Reduce("v."))
c.Putln("b := 0")
c.Putln("")
u.Fields[0].Write(c, "v.")
c.Putln("return buf")
c.Putln("}")
c.Putln("")
}
func (u *Union) WriteList(c *Context) {
c.Putln("// %sListBytes writes a list of %s values to a byte slice.",
u.SrcName(), u.SrcName())
c.Putln("func %sListBytes(buf []byte, list []%s) int {",
u.SrcName(), u.SrcName())
c.Putln("b := 0")
c.Putln("var unionBytes []byte")
c.Putln("for _, item := range list {")
c.Putln("unionBytes = item.Bytes()")
c.Putln("copy(buf[b:], unionBytes)")
c.Putln("b += xgb.Pad(len(unionBytes))")
c.Putln("}")
c.Putln("return b")
c.Putln("}")
c.Putln("")
}
func (u *Union) WriteListSize(c *Context) {
c.Putln("// Union list size %s", u.SrcName())
c.Putln("// %sListSize computes the size (bytes) of a list of %s values.",
u.SrcName())
c.Putln("func %sListSize(list []%s) int {", u.SrcName(), u.SrcName())
c.Putln("size := 0")
c.Putln("for _, item := range list {")
c.Putln("size += %s", u.Size().Reduce("item."))
c.Putln("}")
c.Putln("return size")
c.Putln("}")
c.Putln("")
}

64
vend/xgb/xgbgen/main.go Normal file
View File

@ -0,0 +1,64 @@
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
var (
protoPath = flag.String("proto-path",
"/usr/share/xcb", "path to directory of X protocol XML files")
gofmt = flag.Bool("gofmt", true,
"When disabled, gofmt will not be run before outputting Go code")
)
func usage() {
basename := os.Args[0]
if lastSlash := strings.LastIndex(basename, "/"); lastSlash > -1 {
basename = basename[lastSlash+1:]
}
log.Printf("Usage: %s [flags] xml-file", basename)
flag.PrintDefaults()
os.Exit(1)
}
func init() {
log.SetFlags(0)
}
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() != 1 {
log.Printf("A single XML protocol file can be processed at once.")
flag.Usage()
}
// Read the single XML file into []byte
xmlBytes, err := ioutil.ReadFile(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
// Initialize the buffer, parse it, and filter it through gofmt.
c := newContext()
c.Morph(xmlBytes)
if !*gofmt {
c.out.WriteTo(os.Stdout)
} else {
cmdGofmt := exec.Command("gofmt")
cmdGofmt.Stdin = c.out
cmdGofmt.Stdout = os.Stdout
cmdGofmt.Stderr = os.Stderr
err = cmdGofmt.Run()
if err != nil {
log.Fatal(err)
}
}
}

49
vend/xgb/xgbgen/misc.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"regexp"
"strings"
)
// AllCaps is a regex to test if a string identifier is made of
// all upper case letters.
var allCaps = regexp.MustCompile("^[A-Z0-9]+$")
// popCount counts number of bits 'set' in mask.
func popCount(mask uint) uint {
m := uint32(mask)
n := uint(0)
for i := uint32(0); i < 32; i++ {
if m&(1<<i) != 0 {
n++
}
}
return n
}
// pad makes sure 'n' aligns on 4 bytes.
func pad(n int) int {
return (n + 3) & ^3
}
// splitAndTitle takes a string, splits it by underscores, capitalizes the
// first letter of each chunk, and smushes'em back together.
func splitAndTitle(s string) string {
// If the string is all caps, lower it and capitalize first letter.
if allCaps.MatchString(s) {
return strings.Title(strings.ToLower(s))
}
// If the string has no underscores, capitalize it and leave it be.
if i := strings.Index(s, "_"); i == -1 {
return strings.Title(s)
}
// Now split the name at underscores, capitalize the first
// letter of each chunk, and smush'em back together.
chunks := strings.Split(s, "_")
for i, chunk := range chunks {
chunks[i] = strings.Title(strings.ToLower(chunk))
}
return strings.Join(chunks, "")
}

View File

@ -0,0 +1,70 @@
package main
import (
"log"
"strings"
)
// Protocol is a type that encapsulates all information about one
// particular XML file. It also contains links to other protocol types
// if this protocol imports other other extensions. The import relationship
// is recursive.
type Protocol struct {
Parent *Protocol
Name string
ExtXName string
ExtName string
MajorVersion string
MinorVersion string
Imports []*Protocol
Types []Type
Requests []*Request
}
type Protocols []*Protocol
func (ps Protocols) Len() int { return len(ps) }
func (ps Protocols) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] }
func (ps Protocols) Less(i, j int) bool { return ps[i].ExtName < ps[j].ExtName }
// Initialize traverses all structures, looks for 'Translation' type,
// and looks up the real type in the namespace. It also sets the source
// name for all relevant fields/structures.
// This is necessary because we don't traverse the XML in order initially.
func (p *Protocol) Initialize() {
for _, typ := range p.Types {
typ.Initialize(p)
}
for _, req := range p.Requests {
req.Initialize(p)
}
}
// PkgName returns the name of this package.
// i.e., 'xproto' for the core X protocol, 'randr' for the RandR extension, etc.
func (p *Protocol) PkgName() string {
return strings.Replace(p.Name, "_", "", -1)
}
// ProtocolGet searches the current context for the protocol with the given
// name. (i.e., the current protocol and its imports.)
// It is an error if one is not found.
func (p *Protocol) ProtocolFind(name string) *Protocol {
if p.Name == name {
return p // that was easy
}
for _, imp := range p.Imports {
if imp.Name == name {
return imp
}
}
log.Panicf("Could not find protocol with name '%s'.", name)
panic("unreachable")
}
// isExt returns true if this protocol is an extension.
// i.e., it's name isn't "xproto".
func (p *Protocol) isExt() bool {
return strings.ToLower(p.Name) != "xproto"
}

View File

@ -0,0 +1,152 @@
package main
import (
"fmt"
"log"
"unicode"
)
// Request represents all XML 'request' nodes.
// If the request doesn't have a reply, Reply is nil.
type Request struct {
srcName string // The Go name of this request.
xmlName string // The XML name of this request.
Opcode int
Combine bool // Not currently used.
Fields []Field // All fields in the request.
Reply *Reply // A reply, if one exists for this request.
}
type Requests []*Request
func (rs Requests) Len() int { return len(rs) }
func (rs Requests) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
func (rs Requests) Less(i, j int) bool { return rs[i].xmlName < rs[j].xmlName }
// Initialize creates the proper Go source name for this request.
// It also initializes the reply if one exists, and all fields in this request.
func (r *Request) Initialize(p *Protocol) {
r.srcName = SrcName(p, r.xmlName)
if p.isExt() {
r.srcName = r.srcName
}
if r.Reply != nil {
r.Reply.Initialize(p)
}
for _, field := range r.Fields {
field.Initialize(p)
}
}
func (r *Request) SrcName() string {
return r.srcName
}
func (r *Request) XmlName() string {
return r.xmlName
}
// ReplyName gets the Go source name of the function that generates a
// reply type from a slice of bytes.
// The generated function is not currently exported.
func (r *Request) ReplyName() string {
if r.Reply == nil {
log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.",
r.SrcName())
}
name := r.SrcName()
lower := string(unicode.ToLower(rune(name[0]))) + name[1:]
return fmt.Sprintf("%sReply", lower)
}
// ReplyTypeName gets the Go source name of the type holding all reply data
// for this request.
func (r *Request) ReplyTypeName() string {
if r.Reply == nil {
log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.",
r.SrcName())
}
return fmt.Sprintf("%sReply", r.SrcName())
}
// ReqName gets the Go source name of the function that generates a byte
// slice from a list of parameters.
// The generated function is not currently exported.
func (r *Request) ReqName() string {
name := r.SrcName()
lower := string(unicode.ToLower(rune(name[0]))) + name[1:]
return fmt.Sprintf("%sRequest", lower)
}
// CookieName gets the Go source name of the type that holds cookies for
// this request.
func (r *Request) CookieName() string {
return fmt.Sprintf("%sCookie", r.SrcName())
}
// Size for Request needs a context.
// Namely, if this is an extension, we need to account for *four* bytes
// of a header (extension opcode, request opcode, and the sequence number).
// If it's a core protocol request, then we only account for *three*
// bytes of the header (remove the extension opcode).
func (r *Request) Size(c *Context) Size {
size := newFixedSize(0, true)
// If this is a core protocol request, we squeeze in an extra byte of
// data (from the fields below) between the opcode and the size of the
// request. In an extension request, this byte is always occupied
// by the opcode of the request (while the first byte is always occupied
// by the opcode of the extension).
if !c.protocol.isExt() {
size = size.Add(newFixedSize(3, true))
} else {
size = size.Add(newFixedSize(4, true))
}
for _, field := range r.Fields {
switch field := field.(type) {
case *LocalField: // local fields don't go over the wire
continue
case *SingleField:
fsz := field.Size()
if _, isstruct := field.Type.(*Struct); isstruct {
fsz.Expression = fsz.Expression.Specialize(field.SrcName())
}
size = size.Add(fsz)
default:
size = size.Add(field.Size())
}
}
return newExpressionSize(&Padding{
Expr: size.Expression,
}, size.exact)
}
// Reply encapsulates the fields associated with a 'reply' element.
type Reply struct {
Fields []Field
}
// Size gets the number of bytes in this request's reply.
// A reply always has at least 7 bytes:
// 1 byte: A reply discriminant (first byte set to 1)
// 2 bytes: A sequence number
// 4 bytes: Number of additional bytes in 4-byte units past initial 32 bytes.
func (r *Reply) Size() Size {
size := newFixedSize(0, true)
// Account for reply discriminant, sequence number and reply length
size = size.Add(newFixedSize(7, true))
for _, field := range r.Fields {
size = size.Add(field.Size())
}
return size
}
func (r *Reply) Initialize(p *Protocol) {
for _, field := range r.Fields {
field.Initialize(p)
}
}

31
vend/xgb/xgbgen/size.go Normal file
View File

@ -0,0 +1,31 @@
package main
// Size corresponds to an expression that represents the number of bytes
// in some *thing*. Generally, sizes are used to allocate buffers and to
// inform X how big requests are.
// Size is basically a thin layer over an Expression that yields easy methods
// for adding and multiplying sizes.
type Size struct {
Expression
exact bool
}
// newFixedSize creates a new Size with some fixed and known value.
func newFixedSize(fixed uint, exact bool) Size {
return Size{&Value{v: int(fixed)}, exact}
}
// newExpressionSize creates a new Size with some expression.
func newExpressionSize(variable Expression, exact bool) Size {
return Size{variable, exact}
}
// Add adds s1 and s2 and returns a new Size.
func (s1 Size) Add(s2 Size) Size {
return Size{newBinaryOp("+", s1, s2), s1.exact && s2.exact}
}
// Multiply mupltiplies s1 and s2 and returns a new Size.
func (s1 Size) Multiply(s2 Size) Size {
return Size{newBinaryOp("*", s1, s2), s1.exact && s2.exact}
}

View File

@ -0,0 +1,427 @@
package main
/*
translation.go provides a 'Translate' method on every XML type that converts
the XML type into our "better" representation.
i.e., the representation of Fields and Expressions is just too general.
We end up losing a lot of the advantages of static typing if we keep
the types that encoding/xml forces us into.
Please see 'representation.go' for the type definitions that we're
translating to.
*/
import (
"log"
"strconv"
"strings"
)
func (xml *XML) Translate(parent *Protocol) *Protocol {
protocol := &Protocol{
Parent: parent,
Name: xml.Header,
ExtXName: xml.ExtensionXName,
ExtName: xml.ExtensionName,
MajorVersion: xml.MajorVersion,
MinorVersion: xml.MinorVersion,
Imports: make([]*Protocol, 0),
Types: make([]Type, 0),
Requests: make([]*Request, len(xml.Requests)),
}
for _, imp := range xml.Imports {
if imp.xml != nil {
protocol.Imports = append(protocol.Imports,
imp.xml.Translate(protocol))
}
}
for xmlName, srcName := range BaseTypeMap {
newBaseType := &Base{
srcName: srcName,
xmlName: xmlName,
size: newFixedSize(BaseTypeSizes[xmlName], true),
}
protocol.Types = append(protocol.Types, newBaseType)
}
for _, enum := range xml.Enums {
protocol.Types = append(protocol.Types, enum.Translate())
}
for _, xid := range xml.Xids {
protocol.Types = append(protocol.Types, xid.Translate())
}
for _, xidunion := range xml.XidUnions {
protocol.Types = append(protocol.Types, xidunion.Translate())
}
for _, typedef := range xml.TypeDefs {
protocol.Types = append(protocol.Types, typedef.Translate())
}
for _, s := range xml.Structs {
protocol.Types = append(protocol.Types, s.Translate())
}
for _, union := range xml.Unions {
protocol.Types = append(protocol.Types, union.Translate())
}
for _, ev := range xml.Events {
protocol.Types = append(protocol.Types, ev.Translate())
}
for _, evcopy := range xml.EventCopies {
protocol.Types = append(protocol.Types, evcopy.Translate())
}
for _, err := range xml.Errors {
protocol.Types = append(protocol.Types, err.Translate())
}
for _, errcopy := range xml.ErrorCopies {
protocol.Types = append(protocol.Types, errcopy.Translate())
}
for i, request := range xml.Requests {
protocol.Requests[i] = request.Translate()
}
// Now load all of the type and source name information.
protocol.Initialize()
// Make sure all enums have concrete values.
for _, typ := range protocol.Types {
enum, ok := typ.(*Enum)
if !ok {
continue
}
nextValue := 0
for _, item := range enum.Items {
if item.Expr == nil {
item.Expr = &Value{v: nextValue}
nextValue++
} else {
nextValue = item.Expr.Eval() + 1
}
}
}
return protocol
}
func (x *XMLEnum) Translate() *Enum {
enum := &Enum{
xmlName: x.Name,
Items: make([]*EnumItem, len(x.Items)),
}
for i, item := range x.Items {
enum.Items[i] = &EnumItem{
xmlName: item.Name,
Expr: item.Expr.Translate(),
}
}
return enum
}
func (x *XMLXid) Translate() *Resource {
return &Resource{
xmlName: x.Name,
}
}
func (x *XMLTypeDef) Translate() *TypeDef {
return &TypeDef{
xmlName: x.New,
Old: newTranslation(x.Old),
}
}
func (x *XMLEvent) Translate() *Event {
ev := &Event{
xmlName: x.Name,
Number: x.Number,
NoSequence: x.NoSequence,
Fields: make([]Field, 0, len(x.Fields)),
}
for _, field := range x.Fields {
if field.XMLName.Local == "doc" {
continue
}
ev.Fields = append(ev.Fields, field.Translate(ev))
}
return ev
}
func (x *XMLEventCopy) Translate() *EventCopy {
return &EventCopy{
xmlName: x.Name,
Number: x.Number,
Old: newTranslation(x.Ref),
}
}
func (x *XMLError) Translate() *Error {
err := &Error{
xmlName: x.Name,
Number: x.Number,
Fields: make([]Field, len(x.Fields)),
}
for i, field := range x.Fields {
err.Fields[i] = field.Translate(err)
}
return err
}
func (x *XMLErrorCopy) Translate() *ErrorCopy {
return &ErrorCopy{
xmlName: x.Name,
Number: x.Number,
Old: newTranslation(x.Ref),
}
}
func (x *XMLStruct) Translate() *Struct {
s := &Struct{
xmlName: x.Name,
Fields: make([]Field, len(x.Fields)),
}
for i, field := range x.Fields {
s.Fields[i] = field.Translate(s)
}
return s
}
func (x *XMLUnion) Translate() *Union {
u := &Union{
xmlName: x.Name,
Fields: make([]Field, len(x.Fields)),
}
for i, field := range x.Fields {
u.Fields[i] = field.Translate(u)
}
return u
}
func (x *XMLRequest) Translate() *Request {
r := &Request{
xmlName: x.Name,
Opcode: x.Opcode,
Combine: x.Combine,
Fields: make([]Field, 0, len(x.Fields)),
Reply: x.Reply.Translate(),
}
for _, field := range x.Fields {
if field.XMLName.Local == "doc" || field.XMLName.Local == "fd" {
continue
}
r.Fields = append(r.Fields, field.Translate(r))
}
// Address bug (or legacy code) in QueryTextExtents.
// The XML protocol description references 'string_len' in the
// computation of the 'odd_length' field. However, 'string_len' is not
// defined. Therefore, let's forcefully add it as a 'local field'.
// (i.e., a parameter in the caller but does not get sent over the wire.)
if x.Name == "QueryTextExtents" {
stringLenLocal := &LocalField{&SingleField{
xmlName: "string_len",
Type: newTranslation("CARD16"),
}}
r.Fields = append(r.Fields, stringLenLocal)
}
return r
}
func (x *XMLReply) Translate() *Reply {
if x == nil {
return nil
}
r := &Reply{
Fields: make([]Field, 0, len(x.Fields)),
}
for _, field := range x.Fields {
if field.XMLName.Local == "doc" || field.XMLName.Local == "fd" {
continue
}
r.Fields = append(r.Fields, field.Translate(r))
}
return r
}
func (x *XMLExpression) Translate() Expression {
if x == nil {
return nil
}
switch x.XMLName.Local {
case "op":
if len(x.Exprs) != 2 {
log.Panicf("'op' found %d expressions; expected 2.", len(x.Exprs))
}
return &BinaryOp{
Op: x.Op,
Expr1: x.Exprs[0].Translate(),
Expr2: x.Exprs[1].Translate(),
}
case "unop":
if len(x.Exprs) != 1 {
log.Panicf("'unop' found %d expressions; expected 1.", len(x.Exprs))
}
return &UnaryOp{
Op: x.Op,
Expr: x.Exprs[0].Translate(),
}
case "popcount":
if len(x.Exprs) != 1 {
log.Panicf("'popcount' found %d expressions; expected 1.",
len(x.Exprs))
}
return &PopCount{
Expr: x.Exprs[0].Translate(),
}
case "value":
val, err := strconv.Atoi(strings.TrimSpace(x.Data))
if err != nil {
log.Panicf("Could not convert '%s' in 'value' expression to int.",
x.Data)
}
return &Value{
v: val,
}
case "bit":
bit, err := strconv.Atoi(strings.TrimSpace(x.Data))
if err != nil {
log.Panicf("Could not convert '%s' in 'bit' expression to int.",
x.Data)
}
if bit < 0 || bit > 31 {
log.Panicf("A 'bit' literal must be in the range [0, 31], but "+
" is %d", bit)
}
return &Bit{
b: bit,
}
case "fieldref":
return &FieldRef{
Name: x.Data,
}
case "enumref":
return &EnumRef{
EnumKind: newTranslation(x.Ref),
EnumItem: x.Data,
}
case "sumof":
return &SumOf{
Name: x.Ref,
}
}
log.Panicf("Unrecognized tag '%s' in expression context. Expected one of "+
"op, fieldref, value, bit, enumref, unop, sumof or popcount.",
x.XMLName.Local)
panic("unreachable")
}
func (x *XMLField) Translate(parent interface{}) Field {
switch x.XMLName.Local {
case "pad":
return &PadField{
Bytes: x.Bytes,
Align: x.Align,
}
case "field":
s := &SingleField{
xmlName: x.Name,
Type: newTranslation(x.Type),
}
return s
case "list":
return &ListField{
xmlName: x.Name,
Type: newTranslation(x.Type),
LengthExpr: x.Expr.Translate(),
}
case "localfield":
return &LocalField{&SingleField{
xmlName: x.Name,
Type: newTranslation(x.Type),
}}
case "exprfield":
return &ExprField{
xmlName: x.Name,
Type: newTranslation(x.Type),
Expr: x.Expr.Translate(),
}
case "valueparam":
return &ValueField{
Parent: parent,
MaskType: newTranslation(x.ValueMaskType),
MaskName: x.ValueMaskName,
ListName: x.ValueListName,
}
case "switch":
swtch := &SwitchField{
Name: x.Name,
Expr: x.Expr.Translate(),
Bitcases: make([]*Bitcase, len(x.Bitcases)),
}
for i, bitcase := range x.Bitcases {
swtch.Bitcases[i] = bitcase.Translate()
}
return swtch
case "required_start_align":
return &RequiredStartAlign{}
}
log.Panicf("Unrecognized field element: %s", x.XMLName.Local)
panic("unreachable")
}
func (x *XMLBitcase) Translate() *Bitcase {
b := &Bitcase{
Expr: x.Expr().Translate(),
Fields: make([]Field, len(x.Fields)),
}
for i, field := range x.Fields {
b.Fields[i] = field.Translate(b)
}
return b
}
// SrcName is used to translate any identifier into a Go name.
// Mostly used for fields, but used in a couple other places too (enum items).
func SrcName(p *Protocol, name string) string {
// If it's in the name map, use that translation.
if newn, ok := NameMap[name]; ok {
return newn
}
return splitAndTitle(name)
}
func TypeSrcName(p *Protocol, typ Type) string {
t := typ.XmlName()
// If this is a base type, then write the raw Go type.
if baseType, ok := typ.(*Base); ok {
return baseType.SrcName()
}
// If it's in the type map, use that translation.
if newt, ok := TypeMap[t]; ok {
return newt
}
// If there's a namespace to this type, just use it and be done.
if colon := strings.Index(t, ":"); colon > -1 {
namespace := t[:colon]
rest := t[colon+1:]
return p.ProtocolFind(namespace).PkgName() + "." + splitAndTitle(rest)
}
// Since there's no namespace, we're left with the raw type name.
// If the type is part of the source we're generating (i.e., there is
// no parent protocol), then just return that type name.
// Otherwise, we must qualify it with a package name.
if p.Parent == nil {
return splitAndTitle(t)
}
return p.PkgName() + "." + splitAndTitle(t)
}

390
vend/xgb/xgbgen/type.go Normal file
View File

@ -0,0 +1,390 @@
package main
import (
"fmt"
"strings"
)
type Type interface {
Initialize(p *Protocol)
SrcName() string
XmlName() string
Size() Size
Define(c *Context)
}
type Types []Type
func (ts Types) Len() int { return len(ts) }
func (ts Types) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] }
func (ts Types) Less(i, j int) bool {
x1, x2 := ts[i].XmlName(), ts[j].XmlName()
s1, s2 := ts[i].SrcName(), ts[j].SrcName()
return (s1 == s2 && x1 < x2) || s1 < s2
}
// Translation is used *only* when transitioning from XML types to
// our better representation. They are placeholders for the real types (below)
// that will replace them.
type Translation struct {
xmlName string
}
func newTranslation(name string) *Translation {
return &Translation{xmlName: name}
}
// RealType takes 'XmlName' and finds its real concrete type in our Protocol.
// It is an error if we can't find such a type.
func (t *Translation) RealType(p *Protocol) Type {
// Check to see if there is a namespace. If so, strip it and use it to
// make sure we only look for a type in that protocol.
namespace, typeName := "", t.XmlName()
if ni := strings.Index(t.XmlName(), ":"); ni > -1 {
namespace, typeName = strings.ToLower(typeName[:ni]), typeName[ni+1:]
}
if len(namespace) == 0 || namespace == strings.ToLower(p.Name) {
for _, typ := range p.Types {
if typeName == typ.XmlName() {
return typ
}
}
}
for _, imp := range p.Imports {
if len(namespace) == 0 || namespace == strings.ToLower(imp.Name) {
for _, typ := range imp.Types {
if typeName == typ.XmlName() {
return typ
}
}
}
}
panic("Could not find real type for translation type: " + t.XmlName())
}
func (t *Translation) SrcName() string {
panic("it is illegal to call SrcName on a translation type")
}
func (t *Translation) XmlName() string {
return t.xmlName
}
func (t *Translation) Size() Size {
panic("it is illegal to call Size on a translation type")
}
func (t *Translation) Define(c *Context) {
panic("it is illegal to call Define on a translation type")
}
func (t *Translation) Initialize(p *Protocol) {
panic("it is illegal to call Initialize on a translation type")
}
type Base struct {
srcName string
xmlName string
size Size
}
func (b *Base) SrcName() string {
return b.srcName
}
func (b *Base) XmlName() string {
return b.xmlName
}
func (b *Base) Size() Size {
return b.size
}
func (b *Base) Initialize(p *Protocol) {
b.srcName = TypeSrcName(p, b)
}
type Enum struct {
srcName string
xmlName string
Items []*EnumItem
}
type EnumItem struct {
srcName string
xmlName string
Expr Expression
}
func (enum *Enum) SrcName() string {
return enum.srcName
}
func (enum *Enum) XmlName() string {
return enum.xmlName
}
func (enum *Enum) Size() Size {
panic("Cannot take size of enum")
}
func (enum *Enum) Initialize(p *Protocol) {
enum.srcName = TypeSrcName(p, enum)
for _, item := range enum.Items {
item.srcName = SrcName(p, item.xmlName)
if item.Expr != nil {
item.Expr.Initialize(p)
}
}
}
type Resource struct {
srcName string
xmlName string
}
func (r *Resource) SrcName() string {
return r.srcName
}
func (r *Resource) XmlName() string {
return r.xmlName
}
func (r *Resource) Size() Size {
return newFixedSize(BaseTypeSizes["Id"], true)
}
func (r *Resource) Initialize(p *Protocol) {
r.srcName = TypeSrcName(p, r)
}
type TypeDef struct {
srcName string
xmlName string
Old Type
}
func (t *TypeDef) SrcName() string {
return t.srcName
}
func (t *TypeDef) XmlName() string {
return t.xmlName
}
func (t *TypeDef) Size() Size {
return t.Old.Size()
}
func (t *TypeDef) Initialize(p *Protocol) {
t.Old = t.Old.(*Translation).RealType(p)
t.srcName = TypeSrcName(p, t)
}
type Event struct {
srcName string
xmlName string
Number int
NoSequence bool
Fields []Field
}
func (e *Event) SrcName() string {
return e.srcName
}
func (e *Event) XmlName() string {
return e.xmlName
}
func (e *Event) Size() Size {
return newExpressionSize(&Value{v: 32}, true)
}
func (e *Event) Initialize(p *Protocol) {
e.srcName = TypeSrcName(p, e)
for _, field := range e.Fields {
field.Initialize(p)
}
}
func (e *Event) EvType() string {
return fmt.Sprintf("%sEvent", e.srcName)
}
type EventCopy struct {
srcName string
xmlName string
Old Type
Number int
}
func (e *EventCopy) SrcName() string {
return e.srcName
}
func (e *EventCopy) XmlName() string {
return e.xmlName
}
func (e *EventCopy) Size() Size {
return newExpressionSize(&Value{v: 32}, true)
}
func (e *EventCopy) Initialize(p *Protocol) {
e.srcName = TypeSrcName(p, e)
e.Old = e.Old.(*Translation).RealType(p)
if _, ok := e.Old.(*Event); !ok {
panic("an EventCopy's old type *must* be *Event")
}
}
func (e *EventCopy) EvType() string {
return fmt.Sprintf("%sEvent", e.srcName)
}
type Error struct {
srcName string
xmlName string
Number int
Fields []Field
}
func (e *Error) SrcName() string {
return e.srcName
}
func (e *Error) XmlName() string {
return e.xmlName
}
func (e *Error) Size() Size {
return newExpressionSize(&Value{v: 32}, true)
}
func (e *Error) Initialize(p *Protocol) {
e.srcName = TypeSrcName(p, e)
for _, field := range e.Fields {
field.Initialize(p)
}
}
func (e *Error) ErrConst() string {
return fmt.Sprintf("Bad%s", e.srcName)
}
func (e *Error) ErrType() string {
return fmt.Sprintf("%sError", e.srcName)
}
type ErrorCopy struct {
srcName string
xmlName string
Old Type
Number int
}
func (e *ErrorCopy) SrcName() string {
return e.srcName
}
func (e *ErrorCopy) XmlName() string {
return e.xmlName
}
func (e *ErrorCopy) Size() Size {
return newExpressionSize(&Value{v: 32}, true)
}
func (e *ErrorCopy) Initialize(p *Protocol) {
e.srcName = TypeSrcName(p, e)
e.Old = e.Old.(*Translation).RealType(p)
if _, ok := e.Old.(*Error); !ok {
panic("an ErrorCopy's old type *must* be *Event")
}
}
func (e *ErrorCopy) ErrConst() string {
return fmt.Sprintf("Bad%s", e.srcName)
}
func (e *ErrorCopy) ErrType() string {
return fmt.Sprintf("%sError", e.srcName)
}
type Struct struct {
srcName string
xmlName string
Fields []Field
}
func (s *Struct) SrcName() string {
return s.srcName
}
func (s *Struct) XmlName() string {
return s.xmlName
}
func (s *Struct) Size() Size {
size := newFixedSize(0, true)
for _, field := range s.Fields {
size = size.Add(field.Size())
}
return size
}
func (s *Struct) Initialize(p *Protocol) {
s.srcName = TypeSrcName(p, s)
for _, field := range s.Fields {
field.Initialize(p)
}
}
// HasList returns whether there is a field in this struct that is a list.
// When true, a more involved calculation is necessary to compute this struct's
// size.
func (s *Struct) HasList() bool {
for _, field := range s.Fields {
if _, ok := field.(*ListField); ok {
return true
}
}
return false
}
type Union struct {
srcName string
xmlName string
Fields []Field
}
func (u *Union) SrcName() string {
return u.srcName
}
func (u *Union) XmlName() string {
return u.xmlName
}
// Size for Union is broken. At least, it's broken for XKB.
// It *looks* like the protocol inherently relies on some amount of
// memory unsafety, since some members of unions in XKB are *variable* in
// length! The only thing I can come up with, maybe, is when a union has
// variable size, simply return the raw bytes. Then it's up to the user to
// pass those raw bytes into the appropriate New* constructor. GROSS!
// As of now, just pluck out the first field and return that size. This
// should work for union elements in randr.xml and xproto.xml.
func (u *Union) Size() Size {
return u.Fields[0].Size()
}
func (u *Union) Initialize(p *Protocol) {
u.srcName = fmt.Sprintf("%sUnion", TypeSrcName(p, u))
for _, field := range u.Fields {
field.Initialize(p)
}
}

138
vend/xgb/xgbgen/xml.go Normal file
View File

@ -0,0 +1,138 @@
package main
import (
"encoding/xml"
"io/ioutil"
"log"
)
type XML struct {
// Root 'xcb' element properties.
XMLName xml.Name `xml:"xcb"`
Header string `xml:"header,attr"`
ExtensionXName string `xml:"extension-xname,attr"`
ExtensionName string `xml:"extension-name,attr"`
MajorVersion string `xml:"major-version,attr"`
MinorVersion string `xml:"minor-version,attr"`
// Types for all top-level elements.
// First are the simple ones.
Imports XMLImports `xml:"import"`
Enums []*XMLEnum `xml:"enum"`
Xids []*XMLXid `xml:"xidtype"`
XidUnions []*XMLXid `xml:"xidunion"`
TypeDefs []*XMLTypeDef `xml:"typedef"`
EventCopies []*XMLEventCopy `xml:"eventcopy"`
ErrorCopies []*XMLErrorCopy `xml:"errorcopy"`
// Here are the complex ones, i.e., anything with "structure contents"
Structs []*XMLStruct `xml:"struct"`
Unions []*XMLUnion `xml:"union"`
Requests []*XMLRequest `xml:"request"`
Events []*XMLEvent `xml:"event"`
Errors []*XMLError `xml:"error"`
}
type XMLImports []*XMLImport
func (imports XMLImports) Eval() {
for _, imp := range imports {
xmlBytes, err := ioutil.ReadFile(*protoPath + "/" + imp.Name + ".xml")
if err != nil {
log.Fatalf("Could not read X protocol description for import "+
"'%s' because: %s", imp.Name, err)
}
imp.xml = &XML{}
err = xml.Unmarshal(xmlBytes, imp.xml)
if err != nil {
log.Fatal("Could not parse X protocol description for import "+
"'%s' because: %s", imp.Name, err)
}
// recursive imports...
imp.xml.Imports.Eval()
}
}
type XMLImport struct {
Name string `xml:",chardata"`
xml *XML `xml:"-"`
}
type XMLEnum struct {
Name string `xml:"name,attr"`
Items []*XMLEnumItem `xml:"item"`
}
type XMLEnumItem struct {
Name string `xml:"name,attr"`
Expr *XMLExpression `xml:",any"`
}
type XMLXid struct {
XMLName xml.Name
Name string `xml:"name,attr"`
}
type XMLTypeDef struct {
Old string `xml:"oldname,attr"`
New string `xml:"newname,attr"`
}
type XMLEventCopy struct {
Name string `xml:"name,attr"`
Number int `xml:"number,attr"`
Ref string `xml:"ref,attr"`
}
type XMLErrorCopy struct {
Name string `xml:"name,attr"`
Number int `xml:"number,attr"`
Ref string `xml:"ref,attr"`
}
type XMLStruct struct {
Name string `xml:"name,attr"`
Fields []*XMLField `xml:",any"`
}
type XMLUnion struct {
Name string `xml:"name,attr"`
Fields []*XMLField `xml:",any"`
}
type XMLRequest struct {
Name string `xml:"name,attr"`
Opcode int `xml:"opcode,attr"`
Combine bool `xml:"combine-adjacent,attr"`
Fields []*XMLField `xml:",any"`
Reply *XMLReply `xml:"reply"`
}
type XMLReply struct {
Fields []*XMLField `xml:",any"`
}
type XMLEvent struct {
Name string `xml:"name,attr"`
Number int `xml:"number,attr"`
NoSequence bool `xml:"no-sequence-number,attr"`
Fields []*XMLField `xml:",any"`
}
type XMLError struct {
Name string `xml:"name,attr"`
Number int `xml:"number,attr"`
Fields []*XMLField `xml:",any"`
}
type XMLExpression struct {
XMLName xml.Name
Exprs []*XMLExpression `xml:",any"`
Data string `xml:",chardata"`
Op string `xml:"op,attr"`
Ref string `xml:"ref,attr"`
}

View File

@ -0,0 +1,86 @@
package main
import (
"encoding/xml"
"log"
)
type XMLField struct {
XMLName xml.Name
// For 'pad' element
Bytes uint `xml:"bytes,attr"`
Align uint16 `xml:"align,attr"`
// For 'field', 'list', 'localfield', 'exprfield' and 'switch' elements.
Name string `xml:"name,attr"`
// For 'field', 'list', 'localfield', and 'exprfield' elements.
Type string `xml:"type,attr"`
// For 'list', 'exprfield' and 'switch' elements.
Expr *XMLExpression `xml:",any"`
// For 'valueparm' element.
ValueMaskType string `xml:"value-mask-type,attr"`
ValueMaskName string `xml:"value-mask-name,attr"`
ValueListName string `xml:"value-list-name,attr"`
// For 'switch' element.
Bitcases []*XMLBitcase `xml:"bitcase"`
// I don't know which elements these are for. The documentation is vague.
// They also seem to be completely optional.
OptEnum string `xml:"enum,attr"`
OptMask string `xml:"mask,attr"`
OptAltEnum string `xml:"altenum,attr"`
}
// Bitcase represents a single expression followed by any number of fields.
// Namely, if the switch's expression (all bitcases are inside a switch),
// and'd with the bitcase's expression is equal to the bitcase expression,
// then the fields should be included in its parent structure.
// Note that since a bitcase is unique in that expressions and fields are
// siblings, we must exhaustively search for one of them. Essentially,
// it's the closest thing to a Union I can get to in Go without interfaces.
// Would an '<expression>' tag have been too much to ask? :-(
type XMLBitcase struct {
Fields []*XMLField `xml:",any"`
// All the different expressions.
// When it comes time to choose one, use the 'Expr' method.
ExprOp *XMLExpression `xml:"op"`
ExprUnOp *XMLExpression `xml:"unop"`
ExprField *XMLExpression `xml:"fieldref"`
ExprValue *XMLExpression `xml:"value"`
ExprBit *XMLExpression `xml:"bit"`
ExprEnum *XMLExpression `xml:"enumref"`
ExprSum *XMLExpression `xml:"sumof"`
ExprPop *XMLExpression `xml:"popcount"`
}
// Expr chooses the only non-nil Expr* field from Bitcase.
// Panic if there is more than one non-nil expression.
func (b *XMLBitcase) Expr() *XMLExpression {
choices := []*XMLExpression{
b.ExprOp, b.ExprUnOp, b.ExprField, b.ExprValue,
b.ExprBit, b.ExprEnum, b.ExprSum, b.ExprPop,
}
var choice *XMLExpression = nil
numNonNil := 0
for _, c := range choices {
if c != nil {
numNonNil++
choice = c
}
}
if choice == nil {
log.Panicf("No top level expression found in a bitcase.")
}
if numNonNil > 1 {
log.Panicf("More than one top-level expression was found in a bitcase.")
}
return choice
}

View File

@ -0,0 +1,718 @@
// Package xinerama is the X client API for the XINERAMA extension.
package xinerama
// This file is automatically generated from xinerama.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the XINERAMA extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 8, "XINERAMA").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named XINERAMA could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["XINERAMA"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["XINERAMA"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["XINERAMA"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["XINERAMA"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["XINERAMA"] = make(map[int]xgb.NewErrorFun)
}
type ScreenInfo struct {
XOrg int16
YOrg int16
Width uint16
Height uint16
}
// ScreenInfoRead reads a byte slice into a ScreenInfo value.
func ScreenInfoRead(buf []byte, v *ScreenInfo) int {
b := 0
v.XOrg = int16(xgb.Get16(buf[b:]))
b += 2
v.YOrg = int16(xgb.Get16(buf[b:]))
b += 2
v.Width = xgb.Get16(buf[b:])
b += 2
v.Height = xgb.Get16(buf[b:])
b += 2
return b
}
// ScreenInfoReadList reads a byte slice into a list of ScreenInfo values.
func ScreenInfoReadList(buf []byte, dest []ScreenInfo) int {
b := 0
for i := 0; i < len(dest); i++ {
dest[i] = ScreenInfo{}
b += ScreenInfoRead(buf[b:], &dest[i])
}
return xgb.Pad(b)
}
// Bytes writes a ScreenInfo value to a byte slice.
func (v ScreenInfo) Bytes() []byte {
buf := make([]byte, 8)
b := 0
xgb.Put16(buf[b:], uint16(v.XOrg))
b += 2
xgb.Put16(buf[b:], uint16(v.YOrg))
b += 2
xgb.Put16(buf[b:], v.Width)
b += 2
xgb.Put16(buf[b:], v.Height)
b += 2
return buf[:b]
}
// ScreenInfoListBytes writes a list of ScreenInfo values to a byte slice.
func ScreenInfoListBytes(buf []byte, list []ScreenInfo) int {
b := 0
var structBytes []byte
for _, item := range list {
structBytes = item.Bytes()
copy(buf[b:], structBytes)
b += len(structBytes)
}
return xgb.Pad(b)
}
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// GetScreenCountCookie is a cookie used only for GetScreenCount requests.
type GetScreenCountCookie struct {
*xgb.Cookie
}
// GetScreenCount sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetScreenCountCookie.Reply()
func GetScreenCount(c *xgb.Conn, Window xproto.Window) GetScreenCountCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'GetScreenCount' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getScreenCountRequest(c, Window), cookie)
return GetScreenCountCookie{cookie}
}
// GetScreenCountUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetScreenCountUnchecked(c *xgb.Conn, Window xproto.Window) GetScreenCountCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'GetScreenCount' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getScreenCountRequest(c, Window), cookie)
return GetScreenCountCookie{cookie}
}
// GetScreenCountReply represents the data returned from a GetScreenCount request.
type GetScreenCountReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
ScreenCount byte
Window xproto.Window
}
// Reply blocks and returns the reply data for a GetScreenCount request.
func (cook GetScreenCountCookie) Reply() (*GetScreenCountReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getScreenCountReply(buf), nil
}
// getScreenCountReply reads a byte slice into a GetScreenCountReply value.
func getScreenCountReply(buf []byte) *GetScreenCountReply {
v := new(GetScreenCountReply)
b := 1 // skip reply determinant
v.ScreenCount = buf[b]
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.Window = xproto.Window(xgb.Get32(buf[b:]))
b += 4
return v
}
// Write request to wire for GetScreenCount
// getScreenCountRequest writes a GetScreenCount request to a byte slice.
func getScreenCountRequest(c *xgb.Conn, Window xproto.Window) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XINERAMA"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 2 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
return buf
}
// GetScreenSizeCookie is a cookie used only for GetScreenSize requests.
type GetScreenSizeCookie struct {
*xgb.Cookie
}
// GetScreenSize sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetScreenSizeCookie.Reply()
func GetScreenSize(c *xgb.Conn, Window xproto.Window, Screen uint32) GetScreenSizeCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'GetScreenSize' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getScreenSizeRequest(c, Window, Screen), cookie)
return GetScreenSizeCookie{cookie}
}
// GetScreenSizeUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetScreenSizeUnchecked(c *xgb.Conn, Window xproto.Window, Screen uint32) GetScreenSizeCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'GetScreenSize' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getScreenSizeRequest(c, Window, Screen), cookie)
return GetScreenSizeCookie{cookie}
}
// GetScreenSizeReply represents the data returned from a GetScreenSize request.
type GetScreenSizeReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
Width uint32
Height uint32
Window xproto.Window
Screen uint32
}
// Reply blocks and returns the reply data for a GetScreenSize request.
func (cook GetScreenSizeCookie) Reply() (*GetScreenSizeReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getScreenSizeReply(buf), nil
}
// getScreenSizeReply reads a byte slice into a GetScreenSizeReply value.
func getScreenSizeReply(buf []byte) *GetScreenSizeReply {
v := new(GetScreenSizeReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.Width = xgb.Get32(buf[b:])
b += 4
v.Height = xgb.Get32(buf[b:])
b += 4
v.Window = xproto.Window(xgb.Get32(buf[b:]))
b += 4
v.Screen = xgb.Get32(buf[b:])
b += 4
return v
}
// Write request to wire for GetScreenSize
// getScreenSizeRequest writes a GetScreenSize request to a byte slice.
func getScreenSizeRequest(c *xgb.Conn, Window xproto.Window, Screen uint32) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XINERAMA"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 3 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
xgb.Put32(buf[b:], Screen)
b += 4
return buf
}
// GetStateCookie is a cookie used only for GetState requests.
type GetStateCookie struct {
*xgb.Cookie
}
// GetState sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetStateCookie.Reply()
func GetState(c *xgb.Conn, Window xproto.Window) GetStateCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'GetState' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getStateRequest(c, Window), cookie)
return GetStateCookie{cookie}
}
// GetStateUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetStateUnchecked(c *xgb.Conn, Window xproto.Window) GetStateCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'GetState' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getStateRequest(c, Window), cookie)
return GetStateCookie{cookie}
}
// GetStateReply represents the data returned from a GetState request.
type GetStateReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
State byte
Window xproto.Window
}
// Reply blocks and returns the reply data for a GetState request.
func (cook GetStateCookie) Reply() (*GetStateReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getStateReply(buf), nil
}
// getStateReply reads a byte slice into a GetStateReply value.
func getStateReply(buf []byte) *GetStateReply {
v := new(GetStateReply)
b := 1 // skip reply determinant
v.State = buf[b]
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.Window = xproto.Window(xgb.Get32(buf[b:]))
b += 4
return v
}
// Write request to wire for GetState
// getStateRequest writes a GetState request to a byte slice.
func getStateRequest(c *xgb.Conn, Window xproto.Window) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XINERAMA"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 1 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
return buf
}
// IsActiveCookie is a cookie used only for IsActive requests.
type IsActiveCookie struct {
*xgb.Cookie
}
// IsActive sends a checked request.
// If an error occurs, it will be returned with the reply by calling IsActiveCookie.Reply()
func IsActive(c *xgb.Conn) IsActiveCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'IsActive' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(isActiveRequest(c), cookie)
return IsActiveCookie{cookie}
}
// IsActiveUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func IsActiveUnchecked(c *xgb.Conn) IsActiveCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'IsActive' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(isActiveRequest(c), cookie)
return IsActiveCookie{cookie}
}
// IsActiveReply represents the data returned from a IsActive request.
type IsActiveReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
State uint32
}
// Reply blocks and returns the reply data for a IsActive request.
func (cook IsActiveCookie) Reply() (*IsActiveReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return isActiveReply(buf), nil
}
// isActiveReply reads a byte slice into a IsActiveReply value.
func isActiveReply(buf []byte) *IsActiveReply {
v := new(IsActiveReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.State = xgb.Get32(buf[b:])
b += 4
return v
}
// Write request to wire for IsActive
// isActiveRequest writes a IsActive request to a byte slice.
func isActiveRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XINERAMA"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 4 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}
// QueryScreensCookie is a cookie used only for QueryScreens requests.
type QueryScreensCookie struct {
*xgb.Cookie
}
// QueryScreens sends a checked request.
// If an error occurs, it will be returned with the reply by calling QueryScreensCookie.Reply()
func QueryScreens(c *xgb.Conn) QueryScreensCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'QueryScreens' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(queryScreensRequest(c), cookie)
return QueryScreensCookie{cookie}
}
// QueryScreensUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func QueryScreensUnchecked(c *xgb.Conn) QueryScreensCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'QueryScreens' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(queryScreensRequest(c), cookie)
return QueryScreensCookie{cookie}
}
// QueryScreensReply represents the data returned from a QueryScreens request.
type QueryScreensReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
Number uint32
// padding: 20 bytes
ScreenInfo []ScreenInfo // size: xgb.Pad((int(Number) * 8))
}
// Reply blocks and returns the reply data for a QueryScreens request.
func (cook QueryScreensCookie) Reply() (*QueryScreensReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return queryScreensReply(buf), nil
}
// queryScreensReply reads a byte slice into a QueryScreensReply value.
func queryScreensReply(buf []byte) *QueryScreensReply {
v := new(QueryScreensReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.Number = xgb.Get32(buf[b:])
b += 4
b += 20 // padding
v.ScreenInfo = make([]ScreenInfo, v.Number)
b += ScreenInfoReadList(buf[b:], v.ScreenInfo)
return v
}
// Write request to wire for QueryScreens
// queryScreensRequest writes a QueryScreens request to a byte slice.
func queryScreensRequest(c *xgb.Conn) []byte {
size := 4
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XINERAMA"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 5 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
return buf
}
// QueryVersionCookie is a cookie used only for QueryVersion requests.
type QueryVersionCookie struct {
*xgb.Cookie
}
// QueryVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling QueryVersionCookie.Reply()
func QueryVersion(c *xgb.Conn, Major byte, Minor byte) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(queryVersionRequest(c, Major, Minor), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func QueryVersionUnchecked(c *xgb.Conn, Major byte, Minor byte) QueryVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XINERAMA"]; !ok {
panic("Cannot issue request 'QueryVersion' using the uninitialized extension 'XINERAMA'. xinerama.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(queryVersionRequest(c, Major, Minor), cookie)
return QueryVersionCookie{cookie}
}
// QueryVersionReply represents the data returned from a QueryVersion request.
type QueryVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
// padding: 1 bytes
Major uint16
Minor uint16
}
// Reply blocks and returns the reply data for a QueryVersion request.
func (cook QueryVersionCookie) Reply() (*QueryVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return queryVersionReply(buf), nil
}
// queryVersionReply reads a byte slice into a QueryVersionReply value.
func queryVersionReply(buf []byte) *QueryVersionReply {
v := new(QueryVersionReply)
b := 1 // skip reply determinant
b += 1 // padding
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.Major = xgb.Get16(buf[b:])
b += 2
v.Minor = xgb.Get16(buf[b:])
b += 2
return v
}
// Write request to wire for QueryVersion
// queryVersionRequest writes a QueryVersion request to a byte slice.
func queryVersionRequest(c *xgb.Conn, Major byte, Minor byte) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XINERAMA"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
buf[b] = Major
b += 1
buf[b] = Minor
b += 1
return buf
}

2554
vend/xgb/xprint/xprint.go Normal file

File diff suppressed because it is too large Load Diff

14934
vend/xgb/xproto/xproto.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,384 @@
package xproto
/*
Tests for XGB.
These tests only test the core X protocol at the moment. It isn't even
close to complete coverage (and probably never will be), but it does test
a number of different corners: requests with no replies, requests without
replies, checked (i.e., synchronous) errors, unchecked (i.e., asynchronous)
errors, and sequence number wrapping.
There are also a couple of benchmarks that show the difference between
correctly issuing lots of requests and gathering replies and
incorrectly doing the same. (This particular difference is one of the
claimed advantages of the XCB, and therefore XGB, family.)
In sum, these tests are more focused on testing the core xgb package itself,
rather than whether xproto has properly implemented the core X client
protocol.
*/
import (
"fmt"
"log"
"math/rand"
"testing"
"time"
"github.com/jezek/xgb"
)
// The X connection used throughout testing.
var X *xgb.Conn
// init initializes the X connection, seeds the RNG and starts waiting
// for events.
func init() {
var err error
X, err = xgb.NewConn()
if err != nil {
log.Fatal(err)
}
rand.Seed(time.Now().UnixNano())
go grabEvents()
}
/******************************************************************************/
// Tests
/******************************************************************************/
// TestSynchronousError purposefully causes a BadWindow error in a
// MapWindow request, and checks it synchronously.
func TestSynchronousError(t *testing.T) {
err := MapWindowChecked(X, 0).Check() // resource 0 is always invalid
if err == nil {
t.Fatalf("MapWindow: A MapWindow request that should return an " +
"error has returned a nil error.")
}
verifyMapWindowError(t, err)
}
// TestAsynchronousError does the same thing as TestSynchronousError, but
// grabs the error asynchronously instead.
func TestAsynchronousError(t *testing.T) {
MapWindow(X, 0) // resource id 0 is always invalid
evOrErr := waitForEvent(t, 5)
if evOrErr.ev != nil {
t.Fatalf("After issuing an erroneous MapWindow request, we have "+
"received an event rather than an error: %s", evOrErr.ev)
}
verifyMapWindowError(t, evOrErr.err)
}
// TestCookieBuffer issues (2^16) + n requets *without* replies to guarantee
// that the sequence number wraps and that the cookie buffer will have to
// flush itself (since there are no replies coming in to flush it).
// And just like TestSequenceWrap, we issue another request with a reply
// at the end to make sure XGB is still working properly.
func TestCookieBuffer(t *testing.T) {
n := (1 << 16) + 10
for i := 0; i < n; i++ {
NoOperation(X)
}
TestProperty(t)
}
// TestSequenceWrap issues (2^16) + n requests w/ replies to guarantee that the
// sequence number (which is a 16 bit integer) will wrap. It then issues one
// final request to ensure things still work properly.
func TestSequenceWrap(t *testing.T) {
n := (1 << 16) + 10
for i := 0; i < n; i++ {
_, err := InternAtom(X, false, 5, "RANDO").Reply()
if err != nil {
t.Fatalf("InternAtom: %s", err)
}
}
TestProperty(t)
}
// TestProperty tests whether a random value can be set and read.
func TestProperty(t *testing.T) {
propName := randString(20) // whatevs
writeVal := randString(20)
readVal, err := changeAndGetProp(propName, writeVal)
if err != nil {
t.Error(err)
}
if readVal != writeVal {
t.Errorf("The value written, '%s', is not the same as the "+
"value read '%s'.", writeVal, readVal)
}
}
// TestWindowEvents creates a window, maps it, listens for configure notify
// events, issues a configure request, and checks for the appropriate
// configure notify event.
// This probably violates the notion of "test one thing and test it well,"
// but testing X stuff is unique since it involves so much state.
// Each request is checked to make sure there are no errors returned. If there
// is an error, the test is failed.
// You may see a window appear quickly and then disappear. Do not be alarmed :P
// It's possible that this test will yield a false negative because we cannot
// control our environment. That is, the window manager could override the
// placement set. However, we set override redirect on the window, so the
// window manager *shouldn't* touch our window if it is well-behaved.
func TestWindowEvents(t *testing.T) {
// The geometry to set the window.
gx, gy, gw, gh := 200, 400, 1000, 300
wid, err := NewWindowId(X)
if err != nil {
t.Fatalf("NewId: %s", err)
}
screen := Setup(X).DefaultScreen(X) // alias
err = CreateWindowChecked(X, screen.RootDepth, wid, screen.Root,
0, 0, 500, 500, 0,
WindowClassInputOutput, screen.RootVisual,
CwBackPixel|CwOverrideRedirect, []uint32{0xffffffff, 1}).Check()
if err != nil {
t.Fatalf("CreateWindow: %s", err)
}
err = MapWindowChecked(X, wid).Check()
if err != nil {
t.Fatalf("MapWindow: %s", err)
}
// We don't listen in the CreateWindow request so that we don't get
// a MapNotify event.
err = ChangeWindowAttributesChecked(X, wid,
CwEventMask, []uint32{EventMaskStructureNotify}).Check()
if err != nil {
t.Fatalf("ChangeWindowAttributes: %s", err)
}
err = ConfigureWindowChecked(X, wid,
ConfigWindowX|ConfigWindowY|
ConfigWindowWidth|ConfigWindowHeight,
[]uint32{uint32(gx), uint32(gy), uint32(gw), uint32(gh)}).Check()
if err != nil {
t.Fatalf("ConfigureWindow: %s", err)
}
evOrErr := waitForEvent(t, 5)
switch event := evOrErr.ev.(type) {
case ConfigureNotifyEvent:
if event.X != int16(gx) {
t.Fatalf("x was set to %d but ConfigureNotify reports %d",
gx, event.X)
}
if event.Y != int16(gy) {
t.Fatalf("y was set to %d but ConfigureNotify reports %d",
gy, event.Y)
}
if event.Width != uint16(gw) {
t.Fatalf("width was set to %d but ConfigureNotify reports %d",
gw, event.Width)
}
if event.Height != uint16(gh) {
t.Fatalf("height was set to %d but ConfigureNotify reports %d",
gh, event.Height)
}
default:
t.Fatalf("Expected a ConfigureNotifyEvent but got %T instead.", event)
}
// Okay, clean up!
err = ChangeWindowAttributesChecked(X, wid,
CwEventMask, []uint32{0}).Check()
if err != nil {
t.Fatalf("ChangeWindowAttributes: %s", err)
}
err = DestroyWindowChecked(X, wid).Check()
if err != nil {
t.Fatalf("DestroyWindow: %s", err)
}
}
// Calls GetFontPath function, Issue #12
func TestGetFontPath(t *testing.T) {
fontPathReply, err := GetFontPath(X).Reply()
if err != nil {
t.Fatalf("GetFontPath: %v", err)
}
_ = fontPathReply
}
func TestListFonts(t *testing.T) {
listFontsReply, err := ListFonts(X, 10, 1, "*").Reply()
if err != nil {
t.Fatalf("ListFonts: %v", err)
}
_ = listFontsReply
}
/******************************************************************************/
// Benchmarks
/******************************************************************************/
// BenchmarkInternAtomsGood shows how many requests with replies
// *should* be sent and gathered from the server. Namely, send as many
// requests as you can at once, then go back and gather up all the replies.
// More importantly, this approach can exploit parallelism when
// GOMAXPROCS > 1.
// Run with `go test -run 'nomatch' -bench '.*' -cpu 1,2,6` if you have
// multiple cores to see the improvement that parallelism brings.
func BenchmarkInternAtomsGood(b *testing.B) {
b.StopTimer()
names := seqNames(b.N)
b.StartTimer()
cookies := make([]InternAtomCookie, b.N)
for i := 0; i < b.N; i++ {
cookies[i] = InternAtom(X, false, uint16(len(names[i])), names[i])
}
for _, cookie := range cookies {
cookie.Reply()
}
}
// BenchmarkInternAtomsBad shows how *not* to issue a lot of requests with
// replies. Namely, each subsequent request isn't issued *until* the last
// reply is made. This implies a round trip to the X server for every
// iteration.
func BenchmarkInternAtomsPoor(b *testing.B) {
b.StopTimer()
names := seqNames(b.N)
b.StartTimer()
for i := 0; i < b.N; i++ {
InternAtom(X, false, uint16(len(names[i])), names[i]).Reply()
}
}
/******************************************************************************/
// Helper functions
/******************************************************************************/
// changeAndGetProp sets property 'prop' with value 'val'.
// It then gets the value of that property and returns it.
// (It's used to check that the 'val' going in is the same 'val' going out.)
// It tests both requests with and without replies (GetProperty and
// ChangeProperty respectively.)
func changeAndGetProp(prop, val string) (string, error) {
setup := Setup(X)
root := setup.DefaultScreen(X).Root
propAtom, err := InternAtom(X, false, uint16(len(prop)), prop).Reply()
if err != nil {
return "", fmt.Errorf("InternAtom: %s", err)
}
typName := "UTF8_STRING"
typAtom, err := InternAtom(X, false, uint16(len(typName)), typName).Reply()
if err != nil {
return "", fmt.Errorf("InternAtom: %s", err)
}
err = ChangePropertyChecked(X, PropModeReplace, root, propAtom.Atom,
typAtom.Atom, 8, uint32(len(val)), []byte(val)).Check()
if err != nil {
return "", fmt.Errorf("ChangeProperty: %s", err)
}
reply, err := GetProperty(X, false, root, propAtom.Atom,
GetPropertyTypeAny, 0, (1<<32)-1).Reply()
if err != nil {
return "", fmt.Errorf("GetProperty: %s", err)
}
if reply.Format != 8 {
return "", fmt.Errorf("Property reply format is %d but it should be 8.",
reply.Format)
}
return string(reply.Value), nil
}
// verifyMapWindowError takes an error that is returned with an invalid
// MapWindow request with a window Id of 0 and makes sure the error is the
// right type and contains the correct values.
func verifyMapWindowError(t *testing.T, err error) {
switch e := err.(type) {
case WindowError:
if e.BadValue != 0 {
t.Fatalf("WindowError should report a bad value of 0 but "+
"it reports %d instead.", e.BadValue)
}
if e.MajorOpcode != 8 {
t.Fatalf("WindowError should report a major opcode of 8 "+
"(which is a MapWindow request), but it reports %d instead.",
e.MajorOpcode)
}
default:
t.Fatalf("Expected a WindowError but got %T instead.", e)
}
}
// randString generates a random string of length n.
func randString(n int) string {
byts := make([]byte, n)
for i := 0; i < n; i++ {
rando := rand.Intn(53)
switch {
case rando <= 25:
byts[i] = byte(65 + rando)
case rando <= 51:
byts[i] = byte(97 + rando - 26)
default:
byts[i] = ' '
}
}
return string(byts)
}
// seqNames creates a slice of NAME0, NAME1, ..., NAMEN.
func seqNames(n int) []string {
names := make([]string, n)
for i := range names {
names[i] = fmt.Sprintf("NAME%d", i)
}
return names
}
// evErr represents a value that is either an event or an error.
type evErr struct {
ev xgb.Event
err xgb.Error
}
// channel used to pass evErrs.
var evOrErrChan = make(chan evErr, 0)
// grabEvents is a goroutine that reads events off the wire.
// We used this instead of WaitForEvent directly in our tests so that
// we can timeout and fail a test.
func grabEvents() {
for {
ev, err := X.WaitForEvent()
evOrErrChan <- evErr{ev, err}
}
}
// waitForEvent asks the evOrErrChan channel for an event.
// If it doesn't get an event in 'n' seconds, the current test is failed.
func waitForEvent(t *testing.T, n int) evErr {
var evOrErr evErr
select {
case evOrErr = <-evOrErrChan:
case <-time.After(time.Second * 5):
t.Fatalf("After waiting 5 seconds for an event or an error, " +
"we have timed out.")
}
return evOrErr
}

File diff suppressed because it is too large Load Diff

416
vend/xgb/xtest/xtest.go Normal file
View File

@ -0,0 +1,416 @@
// Package xtest is the X client API for the XTEST extension.
package xtest
// This file is automatically generated from xtest.xml. Edit at your peril!
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
// Init must be called before using the XTEST extension.
func Init(c *xgb.Conn) error {
reply, err := xproto.QueryExtension(c, 5, "XTEST").Reply()
switch {
case err != nil:
return err
case !reply.Present:
return xgb.Errorf("No extension named XTEST could be found on on the server.")
}
c.ExtLock.Lock()
c.Extensions["XTEST"] = reply.MajorOpcode
c.ExtLock.Unlock()
for evNum, fun := range xgb.NewExtEventFuncs["XTEST"] {
xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
}
for errNum, fun := range xgb.NewExtErrorFuncs["XTEST"] {
xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
}
return nil
}
func init() {
xgb.NewExtEventFuncs["XTEST"] = make(map[int]xgb.NewEventFun)
xgb.NewExtErrorFuncs["XTEST"] = make(map[int]xgb.NewErrorFun)
}
const (
CursorNone = 0
CursorCurrent = 1
)
// Skipping definition for base type 'Bool'
// Skipping definition for base type 'Byte'
// Skipping definition for base type 'Card8'
// Skipping definition for base type 'Char'
// Skipping definition for base type 'Void'
// Skipping definition for base type 'Double'
// Skipping definition for base type 'Float'
// Skipping definition for base type 'Int16'
// Skipping definition for base type 'Int32'
// Skipping definition for base type 'Int8'
// Skipping definition for base type 'Card16'
// Skipping definition for base type 'Card32'
// CompareCursorCookie is a cookie used only for CompareCursor requests.
type CompareCursorCookie struct {
*xgb.Cookie
}
// CompareCursor sends a checked request.
// If an error occurs, it will be returned with the reply by calling CompareCursorCookie.Reply()
func CompareCursor(c *xgb.Conn, Window xproto.Window, Cursor xproto.Cursor) CompareCursorCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XTEST"]; !ok {
panic("Cannot issue request 'CompareCursor' using the uninitialized extension 'XTEST'. xtest.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(compareCursorRequest(c, Window, Cursor), cookie)
return CompareCursorCookie{cookie}
}
// CompareCursorUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func CompareCursorUnchecked(c *xgb.Conn, Window xproto.Window, Cursor xproto.Cursor) CompareCursorCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XTEST"]; !ok {
panic("Cannot issue request 'CompareCursor' using the uninitialized extension 'XTEST'. xtest.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(compareCursorRequest(c, Window, Cursor), cookie)
return CompareCursorCookie{cookie}
}
// CompareCursorReply represents the data returned from a CompareCursor request.
type CompareCursorReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
Same bool
}
// Reply blocks and returns the reply data for a CompareCursor request.
func (cook CompareCursorCookie) Reply() (*CompareCursorReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return compareCursorReply(buf), nil
}
// compareCursorReply reads a byte slice into a CompareCursorReply value.
func compareCursorReply(buf []byte) *CompareCursorReply {
v := new(CompareCursorReply)
b := 1 // skip reply determinant
if buf[b] == 1 {
v.Same = true
} else {
v.Same = false
}
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
return v
}
// Write request to wire for CompareCursor
// compareCursorRequest writes a CompareCursor request to a byte slice.
func compareCursorRequest(c *xgb.Conn, Window xproto.Window, Cursor xproto.Cursor) []byte {
size := 12
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XTEST"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 1 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
xgb.Put32(buf[b:], uint32(Window))
b += 4
xgb.Put32(buf[b:], uint32(Cursor))
b += 4
return buf
}
// FakeInputCookie is a cookie used only for FakeInput requests.
type FakeInputCookie struct {
*xgb.Cookie
}
// FakeInput sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func FakeInput(c *xgb.Conn, Type byte, Detail byte, Time uint32, Root xproto.Window, RootX int16, RootY int16, Deviceid byte) FakeInputCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XTEST"]; !ok {
panic("Cannot issue request 'FakeInput' using the uninitialized extension 'XTEST'. xtest.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(fakeInputRequest(c, Type, Detail, Time, Root, RootX, RootY, Deviceid), cookie)
return FakeInputCookie{cookie}
}
// FakeInputChecked sends a checked request.
// If an error occurs, it can be retrieved using FakeInputCookie.Check()
func FakeInputChecked(c *xgb.Conn, Type byte, Detail byte, Time uint32, Root xproto.Window, RootX int16, RootY int16, Deviceid byte) FakeInputCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XTEST"]; !ok {
panic("Cannot issue request 'FakeInput' using the uninitialized extension 'XTEST'. xtest.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(fakeInputRequest(c, Type, Detail, Time, Root, RootX, RootY, Deviceid), cookie)
return FakeInputCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook FakeInputCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for FakeInput
// fakeInputRequest writes a FakeInput request to a byte slice.
func fakeInputRequest(c *xgb.Conn, Type byte, Detail byte, Time uint32, Root xproto.Window, RootX int16, RootY int16, Deviceid byte) []byte {
size := 36
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XTEST"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 2 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
buf[b] = Type
b += 1
buf[b] = Detail
b += 1
b += 2 // padding
xgb.Put32(buf[b:], Time)
b += 4
xgb.Put32(buf[b:], uint32(Root))
b += 4
b += 8 // padding
xgb.Put16(buf[b:], uint16(RootX))
b += 2
xgb.Put16(buf[b:], uint16(RootY))
b += 2
b += 7 // padding
buf[b] = Deviceid
b += 1
return buf
}
// GetVersionCookie is a cookie used only for GetVersion requests.
type GetVersionCookie struct {
*xgb.Cookie
}
// GetVersion sends a checked request.
// If an error occurs, it will be returned with the reply by calling GetVersionCookie.Reply()
func GetVersion(c *xgb.Conn, MajorVersion byte, MinorVersion uint16) GetVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XTEST"]; !ok {
panic("Cannot issue request 'GetVersion' using the uninitialized extension 'XTEST'. xtest.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, true)
c.NewRequest(getVersionRequest(c, MajorVersion, MinorVersion), cookie)
return GetVersionCookie{cookie}
}
// GetVersionUnchecked sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GetVersionUnchecked(c *xgb.Conn, MajorVersion byte, MinorVersion uint16) GetVersionCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XTEST"]; !ok {
panic("Cannot issue request 'GetVersion' using the uninitialized extension 'XTEST'. xtest.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, true)
c.NewRequest(getVersionRequest(c, MajorVersion, MinorVersion), cookie)
return GetVersionCookie{cookie}
}
// GetVersionReply represents the data returned from a GetVersion request.
type GetVersionReply struct {
Sequence uint16 // sequence number of the request for this reply
Length uint32 // number of bytes in this reply
MajorVersion byte
MinorVersion uint16
}
// Reply blocks and returns the reply data for a GetVersion request.
func (cook GetVersionCookie) Reply() (*GetVersionReply, error) {
buf, err := cook.Cookie.Reply()
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return getVersionReply(buf), nil
}
// getVersionReply reads a byte slice into a GetVersionReply value.
func getVersionReply(buf []byte) *GetVersionReply {
v := new(GetVersionReply)
b := 1 // skip reply determinant
v.MajorVersion = buf[b]
b += 1
v.Sequence = xgb.Get16(buf[b:])
b += 2
v.Length = xgb.Get32(buf[b:]) // 4-byte units
b += 4
v.MinorVersion = xgb.Get16(buf[b:])
b += 2
return v
}
// Write request to wire for GetVersion
// getVersionRequest writes a GetVersion request to a byte slice.
func getVersionRequest(c *xgb.Conn, MajorVersion byte, MinorVersion uint16) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XTEST"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 0 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
buf[b] = MajorVersion
b += 1
b += 1 // padding
xgb.Put16(buf[b:], MinorVersion)
b += 2
return buf
}
// GrabControlCookie is a cookie used only for GrabControl requests.
type GrabControlCookie struct {
*xgb.Cookie
}
// GrabControl sends an unchecked request.
// If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
func GrabControl(c *xgb.Conn, Impervious bool) GrabControlCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XTEST"]; !ok {
panic("Cannot issue request 'GrabControl' using the uninitialized extension 'XTEST'. xtest.Init(connObj) must be called first.")
}
cookie := c.NewCookie(false, false)
c.NewRequest(grabControlRequest(c, Impervious), cookie)
return GrabControlCookie{cookie}
}
// GrabControlChecked sends a checked request.
// If an error occurs, it can be retrieved using GrabControlCookie.Check()
func GrabControlChecked(c *xgb.Conn, Impervious bool) GrabControlCookie {
c.ExtLock.RLock()
defer c.ExtLock.RUnlock()
if _, ok := c.Extensions["XTEST"]; !ok {
panic("Cannot issue request 'GrabControl' using the uninitialized extension 'XTEST'. xtest.Init(connObj) must be called first.")
}
cookie := c.NewCookie(true, false)
c.NewRequest(grabControlRequest(c, Impervious), cookie)
return GrabControlCookie{cookie}
}
// Check returns an error if one occurred for checked requests that are not expecting a reply.
// This cannot be called for requests expecting a reply, nor for unchecked requests.
func (cook GrabControlCookie) Check() error {
return cook.Cookie.Check()
}
// Write request to wire for GrabControl
// grabControlRequest writes a GrabControl request to a byte slice.
func grabControlRequest(c *xgb.Conn, Impervious bool) []byte {
size := 8
b := 0
buf := make([]byte, size)
c.ExtLock.RLock()
buf[b] = c.Extensions["XTEST"]
c.ExtLock.RUnlock()
b += 1
buf[b] = 3 // request opcode
b += 1
xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
b += 2
if Impervious {
buf[b] = 1
} else {
buf[b] = 0
}
b += 1
b += 3 // padding
return buf
}

3063
vend/xgb/xv/xv.go Normal file

File diff suppressed because it is too large Load Diff

1041
vend/xgb/xvmc/xvmc.go Normal file

File diff suppressed because it is too large Load Diff

6
vend/xgbutil/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
*.swp
*.png
tst_first
tst_graphics
TAGS

674
vend/xgbutil/LICENSE Normal file
View File

@ -0,0 +1,674 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

36
vend/xgbutil/Makefile Normal file
View File

@ -0,0 +1,36 @@
all: callback.go types_auto.go gofmt
install:
go install -p 6 . ./ewmh ./gopher ./icccm ./keybind ./motif ./mousebind \
./xcursor ./xevent ./xgraphics ./xinerama ./xprop ./xrect ./xwindow
push:
git push origin master
git push github master
build-ex:
find ./_examples/ -type d -wholename './_examples/[a-z]*' -print0 \
| xargs -0 go build -p 6
gofmt:
gofmt -w *.go */*.go _examples/*/*.go
colcheck *.go */*.go _examples/*/*.go
callback.go:
scripts/write-events callbacks > xevent/callback.go
types_auto.go:
scripts/write-events evtypes > xevent/types_auto.go
tags:
find ./ \( -name '*.go' -and -not -wholename './tests/*' -and -not -wholename './_examples/*' \) -print0 | xargs -0 gotags > TAGS
loc:
find ./ -name '*.go' -and -not -wholename './tests*' -and -not -name '*keysymdef.go' -and -not -name '*gopher.go' -print | sort | xargs wc -l
ex-%:
go run _examples/$*/main.go
gopherimg:
go-bindata -f GopherPng -p gopher -i gopher/gophercolor-small.png -o gopher/gopher.go

61
vend/xgbutil/README Normal file
View File

@ -0,0 +1,61 @@
xgbutil is a utility library designed to work with the X Go Binding. This
project's main goal is to make various X related tasks easier. For example,
binding keys, using the EWMH or ICCCM specs with the window manager,
moving/resizing windows, assigning function callbacks to particular events,
drawing images to a window, etc.
xgbutil attempts to be thread safe, but it has not been completely tested in
this regard. In general, the X event loop implemented in the xevent package is
sequential. The idea is to be sequential by default, and let the user spawn
concurrent code at their discretion. (i.e., the complexity of making the main
event loop generally concurrent is vast.)
You may sleep safely at night by assuming that XGB is thread safe, though.
To start using xgbutil, you should have at least a passing familiarity with X.
Your first stop should be the examples directory.
Installation
============
go get github.com/jezek/xgbutil
Dependencies
============
XGB is the main dependency. Use of the xgraphics packages requires graphics-go
and freetype-go.
XGB project URL: https://github.com/jezek/xgb
graphics-go project URL: https://github.com/BurntSushi/graphics-go
freetype-go project URL: https://github.com/BurntSushi/freetype-go
Quick Example
=============
go get github.com/jezek/xgbutil/_examples/window-name-sizes
"$GOPATH"/bin/window-name-sizes
The output will be a list of names of all top-level windows and their geometry
including window manager decorations. (Assuming your window manager supports
some basic EWMH properties.)
Documentation
=============
https://godoc.org/github.com/jezek/xgbutil
Examples
========
There are several examples in the examples directory covering common use cases.
They are heavily documented and should run out of the box.
Python
======
An older project of mine (BurntSushi), xpybutil, served as inspiration for xgbutil. If you
want to use Python, xpybutil should help quite a bit. Please note though, that
at this point, xgbutil provides a lot more functionality and is much better
documented.
xpybutil project URL: https://github.com/BurntSushi/xpybutil
jezek's Fork
============
Why I've forked the xgbutil repository from BurntSushi's github is discussed in [issue #2](https://github.com/jezek/xgb/issues/2).
I've also changed the LICENSE to GNU GPL v3 to meet the requirements for pkg.go.dev

29
vend/xgbutil/STYLE Normal file
View File

@ -0,0 +1,29 @@
I like to keep all my code to 80 columns or less. I have plenty of screen real
estate, but enjoy 80 columns so that I can have multiple code windows open side
to side and not be plagued by the ugly auto-wrapping of a text editor.
If you don't oblige me, I will fix any patch you submit to abide 80 columns.
Note that this style restriction does not preclude gofmt, but introduces a few
peculiarities. The first is that gofmt will occasionally add spacing (typically
to comments) that ends up going over 80 columns. Either shorten the comment or
put it on its own line.
The second and more common hiccup is when a function definition extends beyond
80 columns. If one adds line breaks to keep it below 80 columns, gofmt will
indent all subsequent lines in a function definition to the same indentation
level of the function body. This results in a less-than-ideal separation
between function definition and function body. To remedy this, simply add a
line break like so:
func RestackWindowExtra(xu *xgbutil.XUtil, win xproto.Window, stackMode int,
sibling xproto.Window, source int) error {
return ClientEvent(xu, win, "_NET_RESTACK_WINDOW", source, int(sibling),
stackMode)
}
Something similar should also be applied to long 'if' or 'for' conditionals,
although it would probably be preferrable to break up the conditional to
smaller chunks with a few helper variables.

View File

@ -0,0 +1,63 @@
// Example change-cursor shows how to use the cursor package to change the
// X cursor in a particular window. To see the new cursor, move your cursor
// into the window created by this program.
// Note that this only shows how to use one of the pre-defined cursors built
// into X using the "cursor" font. Creating your own cursor with your own
// image is a bit more complex, and probably not an instructive example.
//
// While this example shows how to set a cursor in an entire window, the cursor
// value returned from xcursor.CreateCursor[Extra] can be used in pointer
// grab requests too. (So that the cursor changes during the grab.)
package main
import (
"log"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/xcursor"
"github.com/jezek/xgbutil/xwindow"
)
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Create the cursor. You can find a list of available cursors in
// xcursor/cursordef.go.
// We'll make an umbrella here, with an orange foreground and a blue
// background. (The background it typically the outline of the cursor.)
// Note that each component of the RGB color is a 16 bit color. I think
// using the most significant byte to specify each component is good
// enough.
cursor, err := xcursor.CreateCursorExtra(X, xcursor.Umbrella,
0xff00, 0x5500, 0x0000,
0x3300, 0x6600, 0xff00)
if err != nil {
log.Fatal(err)
}
// Create a new window. In the create window request, we'll set the
// background color and set the cursor we created above.
// This results in changing the cursor only when it moves into this window.
win, err := xwindow.Generate(X)
if err != nil {
log.Fatal(err)
}
win.Create(X.RootWin(), 0, 0, 500, 500,
xproto.CwBackPixel|xproto.CwCursor,
0xffffffff, uint32(cursor))
win.Map()
// We can free the cursor now that we've set it.
// If you plan on using this cursor again, then it shouldn't be freed.
// (i.e., if you try to free this before setting it as the cursor in a
// window, you'll get a BadCursor error when trying to use it.)
xproto.FreeCursor(X.Conn(), cursor)
// Block. No need to process any events.
select {}
}

View File

@ -0,0 +1,176 @@
/*
Example compress-events shows how to manipulate the xevent package's event
queue to compress events that arrive more often than you'd like to process
them. This example in particular shows how to compress MotionNotify events,
but the same approach could be used to compress ConfigureNotify events.
Note that we show the difference between compressed and uncompressed
MotionNotify events by displaying two windows that listen for MotionNotify
events. The green window compresses them while the red window does not.
Hovering over each window will print the x and y positions in each
MotionNotify event received. You should notice that the red window
lags behind the pointer (particularly if you moved the pointer quickly in
and out of the window) while the green window always keeps up, regardless
of the speed of the pointer.
In each case, we simulate work by sleeping for some amount of time. (The
whole point of compressing events is that there is too much work to be done
for each event.)
Note that when compressing events, you should always make sure that the
event you're compressing *ought* to be compressed. For example, with
MotionNotify events, if the Event field changes, then it applies to a
different window and probably shouldn't be compressed with MotionNotify
events for other windows.
Finally, compressing events implicitly assumes that the event handler doing
the compression is the *only* event handler for a particular (event, window)
tuple. If there is more than one event handler for a single (event, window)
tuple and one of them does compression, the other will be left out in the
cold. (Since the main event loop is subverted and won't process the
compressed events in the usual way.)
N.B. This functionality isn't included in xgbutil because event compression
isn't something that is always desirable, and the conditions under which
compression happens can vary. In particular, compressing ConfigureRequest
events from the perspective of the window manager can be faulty, since
changes to other properties (like WM_NORMAL_HINTS) can change the semantics
of a ConfigureRequest event. (i.e., your compression would need to
specifically look for events that could change future ConfigureRequest
events.)
*/
package main
import (
"fmt"
"log"
"time"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xwindow"
)
// workTime is the amount of time to sleep to simulate "work" in response to
// MotionNotify events. Increasing this will exacerbate the difference
// between the green and red windows. But if you increase it too much,
// the red window starts to *really* lag, and you'll probably have to kill
// the program.
var workTime = 50 * time.Millisecond
// newWindow creates a new window that listens to MotionNotify events with
// the given backgroundcolor.
func newWindow(X *xgbutil.XUtil, color uint32) *xwindow.Window {
win, err := xwindow.Generate(X)
if err != nil {
log.Fatal(err)
}
err = win.CreateChecked(X.RootWin(), 0, 0, 400, 400,
xproto.CwBackPixel|xproto.CwEventMask,
color, xproto.EventMaskPointerMotion)
if err != nil {
log.Fatal(err)
}
win.Map()
return win
}
// compressMotionNotify takes a MotionNotify event, and inspects the event
// queue for any future MotionNotify events that can be received without
// blocking. The most recent MotionNotify event is then returned.
// Note that we need to make sure that the Event, Child, Detail, State, Root
// and SameScreen fields are the same to ensure the same window/action is
// generating events. That is, we are only compressing the RootX, RootY,
// EventX and EventY fields.
// This function is not thread safe, since Peek returns a *copy* of the
// event queue---which could be out of date by the time we dequeue events.
func compressMotionNotify(X *xgbutil.XUtil,
ev xevent.MotionNotifyEvent) xevent.MotionNotifyEvent {
// We force a round trip request so that we make sure to read all
// available events.
X.Sync()
xevent.Read(X, false)
// The most recent MotionNotify event that we'll end up returning.
laste := ev
// Look through each event in the queue. If it's an event and it matches
// all the fields in 'ev' that are detailed above, then set it to 'laste'.
// In which case, we'll also dequeue the event, otherwise it will be
// processed twice!
// N.B. If our only goal was to find the most recent relevant MotionNotify
// event, we could traverse the event queue backwards and simply use
// the first MotionNotify we see. However, this could potentially leave
// other MotionNotify events in the queue, which we *don't* want to be
// processed. So we stride along and just pick off MotionNotify events
// until we don't see any more.
for i, ee := range xevent.Peek(X) {
if ee.Err != nil { // This is an error, skip it.
continue
}
// Use type assertion to make sure this is a MotionNotify event.
if mn, ok := ee.Event.(xproto.MotionNotifyEvent); ok {
// Now make sure all appropriate fields are equivalent.
if ev.Event == mn.Event && ev.Child == mn.Child &&
ev.Detail == mn.Detail && ev.State == mn.State &&
ev.Root == mn.Root && ev.SameScreen == mn.SameScreen {
// Set the most recent/valid motion notify event.
laste = xevent.MotionNotifyEvent{&mn}
// We cheat and use the stack semantics of defer to dequeue
// most recent motion notify events first, so that the indices
// don't become invalid. (If we dequeued oldest first, we'd
// have to account for all future events shifting to the left
// by one.)
defer func(i int) { xevent.DequeueAt(X, i) }(i)
}
}
}
// This isn't strictly necessary, but is correct. We should update
// xgbutil's sense of time with the most recent event processed.
// This is typically done in the main event loop, but since we are
// subverting the main event loop, we should take care of it.
X.TimeSet(laste.Time)
return laste
}
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Create window for receiving compressed MotionNotify events.
cwin := newWindow(X, 0x00ff00)
// Attach event handler for MotionNotify that compresses events.
xevent.MotionNotifyFun(
func(X *xgbutil.XUtil, ev xevent.MotionNotifyEvent) {
ev = compressMotionNotify(X, ev)
fmt.Printf("COMPRESSED: (EventX %d, EventY %d)\n",
ev.EventX, ev.EventY)
time.Sleep(workTime)
}).Connect(X, cwin.Id)
// Create window for receiving uncompressed MotionNotify events.
uwin := newWindow(X, 0xff0000)
// Attach event handler for MotionNotify that does not compress events.
xevent.MotionNotifyFun(
func(X *xgbutil.XUtil, ev xevent.MotionNotifyEvent) {
fmt.Printf("UNCOMPRESSED: (EventX %d, EventY %d)\n",
ev.EventX, ev.EventY)
time.Sleep(workTime)
}).Connect(X, uwin.Id)
xevent.Main(X)
}

View File

@ -0,0 +1,20 @@
/*
Package examples contains several complete examples depicting some common use
cases of xgbutil.
The examples included are designed to either demonstrate how a particular
facility of xgbutil works, or how to accomplish some common goal when using X.
For example, example simple-keybinding demonstrates how to use xgbutil to
respond to particular key presses while example graceful-window-close shows
how to close windows without killing your connection to X.
My goal is that each example demonstrates one thing, although the nature of X
typically requires some amount of boilerplate so that some examples are a bit
more bloated than I would like.
If you have any suggestions for other examples to include, I'd be happy to hear
them. You may post them at the main project page:
https://github.com/jezek/xgbutil.
*/
package documentation

View File

@ -0,0 +1,91 @@
// Example draw-text shows how to draw text to an xgraphics.Image type.
package main
import (
"image"
"log"
"os"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xgraphics"
)
var (
// The geometry of the canvas to draw text on.
canvasWidth, canvasHeight = 600, 100
// The background color of the canvas.
bg = xgraphics.BGRA{B: 0xff, G: 0x66, R: 0x33, A: 0xff}
// The path to the font used to draw text.
fontPath = "/usr/share/fonts/TTF/FreeMonoBold.ttf"
// The color of the text.
fg = xgraphics.BGRA{B: 0xff, G: 0xff, R: 0xff, A: 0xff}
// The size of the text.
size = 20.0
// The text to draw.
msg = "This is some text drawn by xgraphics!"
)
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Load some font. You may need to change the path depending upon your
// system configuration.
fontReader, err := os.Open(fontPath)
if err != nil {
log.Fatal(err)
}
// Now parse the font.
font, err := xgraphics.ParseFont(fontReader)
if err != nil {
log.Fatal(err)
}
// Create some canvas.
ximg := xgraphics.New(X, image.Rect(0, 0, canvasWidth, canvasHeight))
ximg.For(func(x, y int) xgraphics.BGRA {
return bg
})
// Now write the text.
_, _, err = ximg.Text(10, 10, fg, size, font, msg)
if err != nil {
log.Fatal(err)
}
// Compute extents of first line of text.
_, firsth := xgraphics.Extents(font, size, msg)
// Now show the image in its own window.
win := ximg.XShowExtra("Drawing text using xgraphics", true)
// Now draw some more text below the above and demonstrate how to update
// only the region we've updated.
_, _, err = ximg.Text(10, 10+firsth, fg, size, font, "Some more text.")
if err != nil {
log.Fatal(err)
}
// Now compute extents of the second line of text, so we know which region
// to update.
secw, sech := xgraphics.Extents(font, size, "Some more text.")
// Now repaint on the region that we drew text on. Then update the screen.
bounds := image.Rect(10, 10+firsth, 10+secw, 10+firsth+sech)
ximg.SubImage(bounds).(*xgraphics.Image).XDraw()
ximg.XPaint(win.Id)
// All we really need to do is block, which could be achieved using
// 'select{}'. Invoking the main event loop however, will emit error
// message if anything went seriously wrong above.
xevent.Main(X)
}

View File

@ -0,0 +1,136 @@
// Example fullscreen shows how to make a window showing the Go Gopher go
// fullscreen and back using keybindings and EWMH.
package main
import (
"bytes"
"image"
_ "image/png"
"log"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/ewmh"
"github.com/jezek/xgbutil/gopher"
"github.com/jezek/xgbutil/icccm"
"github.com/jezek/xgbutil/keybind"
"github.com/jezek/xgbutil/mousebind"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xgraphics"
"github.com/jezek/xgbutil/xwindow"
)
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
keybind.Initialize(X) // call once before using keybind package
// Read an example gopher image into a regular png image.
img, _, err := image.Decode(bytes.NewBuffer(gopher.GopherPng()))
if err != nil {
log.Fatal(err)
}
// Now convert it into an X image.
ximg := xgraphics.NewConvert(X, img)
// Now show it in a new window.
// We set the window title and tell the program to quit gracefully when
// the window is closed.
// There is also a convenience method, XShow, that requires no parameters.
win := showImage(ximg, "The Go Gopher!", true)
// Listen for key press events.
win.Listen(xproto.EventMaskKeyPress)
err = keybind.KeyPressFun(
func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) {
println("fullscreen!")
err := ewmh.WmStateReq(X, win.Id, ewmh.StateToggle,
"_NET_WM_STATE_FULLSCREEN")
if err != nil {
log.Fatal(err)
}
}).Connect(X, win.Id, "f", false)
if err != nil {
log.Fatal(err)
}
err = keybind.KeyPressFun(
func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) {
println("quit fullscreen!")
err := ewmh.WmStateReq(X, win.Id, ewmh.StateToggle,
"_NET_WM_STATE_FULLSCREEN")
if err != nil {
log.Fatal(err)
}
}).Connect(X, win.Id, "Escape", false)
if err != nil {
log.Fatal(err)
}
// If we don't block, the program will end and the window will disappear.
// We could use a 'select{}' here, but xevent.Main will emit errors if
// something went wrong, so use that instead.
xevent.Main(X)
}
// This is a slightly modified version of xgraphics.XShowExtra that does
// not set any resize constraints on the window (so that it can go
// fullscreen).
func showImage(im *xgraphics.Image, name string, quit bool) *xwindow.Window {
if len(name) == 0 {
name = "xgbutil Image Window"
}
w, h := im.Rect.Dx(), im.Rect.Dy()
win, err := xwindow.Generate(im.X)
if err != nil {
xgbutil.Logger.Printf("Could not generate new window id: %s", err)
return nil
}
// Create a very simple window with dimensions equal to the image.
win.Create(im.X.RootWin(), 0, 0, w, h, 0)
// Make this window close gracefully.
win.WMGracefulClose(func(w *xwindow.Window) {
xevent.Detach(w.X, w.Id)
keybind.Detach(w.X, w.Id)
mousebind.Detach(w.X, w.Id)
w.Destroy()
if quit {
xevent.Quit(w.X)
}
})
// Set WM_STATE so it is interpreted as a top-level window.
err = icccm.WmStateSet(im.X, win.Id, &icccm.WmState{
State: icccm.StateNormal,
})
if err != nil { // not a fatal error
xgbutil.Logger.Printf("Could not set WM_STATE: %s", err)
}
// Set _NET_WM_NAME so it looks nice.
err = ewmh.WmNameSet(im.X, win.Id, name)
if err != nil { // not a fatal error
xgbutil.Logger.Printf("Could not set _NET_WM_NAME: %s", err)
}
// Paint our image before mapping.
im.XSurfaceSet(win.Id)
im.XDraw()
im.XPaint(win.Id)
// Now we can map, since we've set all our properties.
// (The initial map is when the window manager starts managing.)
win.Map()
return win
}

View File

@ -0,0 +1,120 @@
// Example graceful-window-close shows how to create windows that can be closed
// without killing your X connection (and thereby destroying any other windows
// you may have open). This is actually achieved through an ICCCM standard.
// We add the WM_DELETE_WINDOW to the WM_PROTOCOLS property on our window.
// This indicates to well-behaving window managers that a certain kind of
// client message should be sent to our client when the window should be closed.
// If we *don't* add the WM_DELETE_WINDOW to the WM_PROTOCOLS property, the
// window manager will typically call KillClient---which will destroy the
// window and your X connection.
//
// If you click inside one of the windows created, a new window will be
// automatically created.
//
// The program will stop when all windows have been closed.
//
// For more information on the convention used please see
// http://tronche.com/gui/x/icccm/sec-4.html#s-4.1.2.7 and
// http://tronche.com/gui/x/icccm/sec-4.html#s-4.2.8
package main
import (
"log"
"math/rand"
"os"
"time"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/mousebind"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xwindow"
)
// When counter reaches 0, exit.
var counter int32
// Just iniaitlize the RNG seed for generating random background colors.
func init() {
rand.Seed(time.Now().UnixNano())
}
// newWindow creates a new window with a random background color. It sets the
// WM_PROTOCOLS property to contain the WM_DELETE_WINDOW atom. It also sets
// up a ClientMessage event handler so that we know when to destroy the window.
// We also set up a mouse binding so that clicking inside a window will
// create another one.
func newWindow(X *xgbutil.XUtil) {
counter++
win, err := xwindow.Generate(X)
if err != nil {
log.Fatal(err)
}
// Get a random background color, create the window (ask to receive button
// release events while we're at it) and map the window.
bgColor := rand.Intn(0xffffff + 1)
win.Create(X.RootWin(), 0, 0, 200, 200,
xproto.CwBackPixel|xproto.CwEventMask,
uint32(bgColor), xproto.EventMaskButtonRelease)
// WMGracefulClose does all of the work for us. It sets the appropriate
// values for WM_PROTOCOLS, and listens for ClientMessages that implement
// the WM_DELETE_WINDOW protocol. When one is found, the provided callback
// is executed.
win.WMGracefulClose(
func(w *xwindow.Window) {
// Detach all event handlers.
// This should always be done when a window can no longer
// receive events.
xevent.Detach(w.X, w.Id)
mousebind.Detach(w.X, w.Id)
w.Destroy()
// Exit if there are no more windows left.
counter--
if counter == 0 {
os.Exit(0)
}
})
// It's important that the map comes after setting WMGracefulClose, since
// the WM isn't obliged to watch updates to the WM_PROTOCOLS property.
win.Map()
// A mouse binding so that a left click will spawn a new window.
// Note that we don't issue a grab here. Typically, window managers will
// grab a button press on the client window (which usually activates the
// window), so that we'd end up competing with the window manager if we
// tried to grab it.
// Instead, we set a ButtonRelease mask when creating the window and attach
// a mouse binding *without* a grab.
err = mousebind.ButtonReleaseFun(
func(X *xgbutil.XUtil, ev xevent.ButtonReleaseEvent) {
newWindow(X)
}).Connect(X, win.Id, "1", false, false)
if err != nil {
log.Fatal(err)
}
}
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Anytime the mousebind (keybind) package is used, mousebind.Initialize
// *should* be called once. In the case of the mousebind package, this
// isn't strictly necessary (currently!), but the 'Drag' features of
// the mousebind package won't work without it.
mousebind.Initialize(X)
// Create two windows to prove we can close one while keeping the
// other alive.
newWindow(X)
newWindow(X)
xevent.Main(X)
}

View File

@ -0,0 +1,131 @@
//This demonstration shows drawing entire generated images to an x window
//and calculating the speed of the generation and the drawing operations
//It should be noted that redrawing the entire image is inefficient, this demo
//was made to show me how fast I could draw to the windows with this method.
package main
import (
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/keybind"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xgraphics"
"github.com/jezek/xgbutil/xwindow"
"image"
"log"
"time"
)
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Println(err)
return
}
//Initialize the keybind package
keybind.Initialize(X)
//Create a window
win, err := xwindow.Generate(X)
if err != nil {
log.Fatalf("Could not generate a new window X id: %s", err)
}
win.Create(X.RootWin(), 0, 0, 1024, 768, xproto.CwBackPixel, 0x606060ff)
// Listen for Key{Press,Release} events.
win.Listen(xproto.EventMaskKeyPress, xproto.EventMaskKeyRelease)
// Map the window. This is what makes it on the screen
win.Map()
//Make a ...callback... for the events and connect
xevent.KeyPressFun(
func(X *xgbutil.XUtil, e xevent.KeyPressEvent) {
modStr := keybind.ModifierString(e.State)
keyStr := keybind.LookupString(X, e.State, e.Detail)
if len(modStr) > 0 {
log.Printf("Key: %s-%s\n", modStr, keyStr)
} else {
log.Println("Key:", keyStr)
}
if keybind.KeyMatch(X, "Escape", e.State, e.Detail) {
if e.State&xproto.ModMaskControl > 0 {
log.Println("Control-Escape detected. Quitting...")
xevent.Quit(X)
}
}
}).Connect(X, win.Id)
//So here i'm going to try to make a image..'
img := xgraphics.New(X, image.Rect(0, 0, 1024, 768))
err = img.XSurfaceSet(win.Id)
if err != nil {
log.Printf("Error while setting window surface to image %d: %s\n",
win, err)
} else {
log.Printf("Window %d surface set to image OK\n", win)
}
// I /think/ XDraw actually sends data to server?
img.XDraw()
// I /think/ XPaint tells the server to paint image to window
img.XPaint(win.Id)
//Start the routine that updates the window
go updater(img, win)
//This seems to start a main loop for listening to xevents
xevent.Main(X)
}
func updater(img *xgraphics.Image, win *xwindow.Window) {
//We keep track of times based on 1024 frames
frame := 0
start := time.Now()
var genStart, drawStart time.Time
var genTotal, drawTotal time.Duration
for {
frame = frame + 1
if frame > 1024 {
frame = 0
log.Printf("Time elapsed: %s\n", time.Now().Sub(start))
log.Printf("Time generate: %s\n", genTotal)
log.Printf("Time drawing: %s\n", drawTotal)
start = time.Now()
drawTotal, genTotal = 0, 0
}
genStart = time.Now()
var x, y, i int
for y = 0; y < 768; y++ {
//set last pixel back to black
img.SetBGRA(frame-1, y, xgraphics.BGRA{0, 0, 0, 255})
for i = 0; i < 10; i++ {
x = i + frame
if x > 1024 {
x = 1024
}
img.SetBGRA(x, y, xgraphics.BGRA{0, 0, 255, 255})
}
}
genTotal += time.Now().Sub(genStart)
drawStart = time.Now()
//hopefully using checked will block us from drawing again before x
//draws although XDraw might block anyway, we can check for an error
//here
err := img.XDrawChecked()
if err != nil {
log.Println(err)
return
}
//img.XDraw()
img.XPaint(win.Id)
drawTotal += time.Now().Sub(drawStart)
}
}

View File

@ -0,0 +1,103 @@
// Example keypress-english shows how to convert the State (modifiers) and
// Detail (keycode) members of Key{Press,Release} events to an english
// string representation.
package main
import (
"flag"
"log"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/keybind"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xwindow"
)
var flagRoot = false
func init() {
log.SetFlags(0)
flag.BoolVar(&flagRoot, "root", flagRoot,
"When set, the keyboard will be grabbed on the root window. "+
"Make sure you have a way to kill the window created with "+
"the mouse.")
flag.Parse()
}
func main() {
// Connect to the X server using the DISPLAY environment variable.
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Anytime the keybind (mousebind) package is used, keybind.Initialize
// *should* be called once. It isn't strictly necessary, but allows your
// keybindings to persist even if the keyboard mapping is changed during
// run-time. (Assuming you're using the xevent package's event loop.)
// It also handles the case when your modifier map is changed.
keybind.Initialize(X)
// Create a new window. We will listen for key presses and translate them
// only when this window is in focus. (Similar to how `xev` works.)
win, err := xwindow.Generate(X)
if err != nil {
log.Fatalf("Could not generate a new window X id: %s", err)
}
win.Create(X.RootWin(), 0, 0, 500, 500, xproto.CwBackPixel, 0xffffffff)
// Listen for Key{Press,Release} events.
win.Listen(xproto.EventMaskKeyPress, xproto.EventMaskKeyRelease)
// Map the window.
win.Map()
// Notice that we use xevent.KeyPressFun instead of keybind.KeyPressFun,
// because we aren't trying to make a grab *and* because we want to listen
// to *all* key press events, rather than just a particular key sequence
// that has been pressed.
wid := win.Id
if flagRoot {
wid = X.RootWin()
}
xevent.KeyPressFun(
func(X *xgbutil.XUtil, e xevent.KeyPressEvent) {
// keybind.LookupString does the magic of implementing parts of
// the X Keyboard Encoding to determine an english representation
// of the modifiers/keycode tuple.
// N.B. It's working for me, but probably isn't 100% correct in
// all environments yet.
modStr := keybind.ModifierString(e.State)
keyStr := keybind.LookupString(X, e.State, e.Detail)
if len(modStr) > 0 {
log.Printf("Key: %s-%s\n", modStr, keyStr)
} else {
log.Println("Key:", keyStr)
}
if keybind.KeyMatch(X, "Escape", e.State, e.Detail) {
if e.State&xproto.ModMaskControl > 0 {
log.Println("Control-Escape detected. Quitting...")
xevent.Quit(X)
}
}
}).Connect(X, wid)
// If we want root, then we take over the entire keyboard.
if flagRoot {
if err := keybind.GrabKeyboard(X, X.RootWin()); err != nil {
log.Fatalf("Could not grab keyboard: %s", err)
}
log.Println("WARNING: We are taking *complete* control of the root " +
"window. The only way out is to press 'Control + Escape' or to " +
"close the window with the mouse.")
}
// Finally, start the main event loop. This will route any appropriate
// KeyPressEvents to your callback function.
log.Println("Program initialized. Start pressing keys!")
xevent.Main(X)
}

View File

@ -0,0 +1,93 @@
// Example multiple-source-event-loop shows how to use the xevent package to
// combine multiple sources in your main event loop. This is particularly
// useful if your application can respond to user input from other sources.
package main
import (
"fmt"
"log"
"time"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/ewmh"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xprop"
"github.com/jezek/xgbutil/xwindow"
)
// otherSource serves as a placeholder from some other source of user input.
func otherSource() chan int {
c := make(chan int, 0)
go func() {
defer close(c)
i := 1
for {
c <- i
i++
time.Sleep(time.Second)
}
}()
return c
}
// sendClientMessages is a goroutine that sends client messages to the root
// window. We then listen to them later as a demonstration of responding to
// X events. (They are sent with SubstructureNotify and SubstructureRedirect
// masks set. So in order to receive them, we'll have to explicitly listen
// to events of that type on the root window.)
func xSource(X *xgbutil.XUtil) {
i := 1
for {
ewmh.ClientEvent(X, X.RootWin(), "NOOP", i)
i++
time.Sleep(200 * time.Millisecond)
}
}
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Start generating other source events.
otherChan := otherSource()
// Start generating X events (by sending client messages to root window).
go xSource(X)
// Listen to those X events.
xwindow.New(X, X.RootWin()).Listen(xproto.EventMaskSubstructureNotify)
// Respond to those X events.
xevent.ClientMessageFun(
func(X *xgbutil.XUtil, ev xevent.ClientMessageEvent) {
atmName, err := xprop.AtomName(X, ev.Type)
if err != nil {
log.Fatal(err)
}
fmt.Printf("ClientMessage: %d. %s\n", ev.Data.Data32[0], atmName)
}).Connect(X, X.RootWin())
// Instead of using the usual xevent.Main, we use xevent.MainPing.
// It runs the main event loop inside a goroutine and returns ping
// channels, which are sent benign values right before an event is
// dequeued and right after that event has finished running all callbacks
// associated with it, respectively.
pingBefore, pingAfter, pingQuit := xevent.MainPing(X)
for {
select {
case <-pingBefore:
// Wait for the event to finish processing.
<-pingAfter
case otherVal := <-otherChan:
fmt.Printf("Processing other event: %d\n", otherVal)
case <-pingQuit:
fmt.Printf("xevent loop has quit")
return
}
}
}

View File

@ -0,0 +1,268 @@
// Example pointer-painting shows how to draw on a window, MS Paint style.
// This is an extremely involved example, but it showcases a lot of xgbutil
// and how pieces of it can be tied together.
//
// If you're just starting with xgbutil, I highly recommend checking out the
// other examples before attempting to digest this one.
package main
import (
"bytes"
"fmt"
"image"
_ "image/png"
"log"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/gopher"
"github.com/jezek/xgbutil/keybind"
"github.com/jezek/xgbutil/mousebind"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xgraphics"
"github.com/jezek/xgbutil/xwindow"
)
var (
// The color to use for the background.
bg = xgraphics.BGRA{0x0, 0x0, 0x0, 0xff}
// Different colors for drawing.
// The keys represent the key sequences that must be pressed to
// switch to the color value.
pencils = map[string]xgraphics.BGRA{
"1": xgraphics.BGRA{0xff, 0xff, 0xff, 0xff}, // white
"2": xgraphics.BGRA{0xff, 0x0, 0x0, 0xff}, // blue
"3": xgraphics.BGRA{0x0, 0xff, 0x0, 0xff}, // green
"4": xgraphics.BGRA{0x0, 0x0, 0xff, 0xff}, // red
"5": xgraphics.BGRA{0x0, 0x7f, 0xff, 0xff}, // orange
"6": xgraphics.BGRA{0xaa, 0x0, 0xff, 0x55}, // transparent pink
}
// The current pencil color.
pencil = xgraphics.BGRA{0xff, 0xff, 0xff, 0xff}
// The size of the tip of the pencil, in pixels.
pencilTip = 30
// The width and height of the canvas.
width, height = 1000, 1000
// The key sequence to use to clear the canvas.
clearKey = "c"
// Easter egg! Right click to draw a gopher with the following dimensions.
gopherWidth, gopherHeight = 100, 100
)
// drawPencil takes an (x, y) position (from a MotionNotify event) and draws
// a rectangle of size pencilTip on to canvas.
func drawPencil(canvas *xgraphics.Image, win *xwindow.Window, x, y int) {
// Create a subimage at (x, y) with pencilTip width and height from canvas.
// Creating subimages is very cheap---no pixels are copied.
// Moreover, when subimages are drawn to the screen, only the pixels in
// the sub-image are sent to X.
tipRect := midRect(x, y, pencilTip, pencilTip, width, height)
// If the rectangle contains no pixels, don't draw anything.
if tipRect.Empty() {
return
}
// Output a little message.
log.Printf("Drawing pencil point at (%d, %d)", x, y)
// Create the subimage of the canvas to draw to.
tip := canvas.SubImage(tipRect).(*xgraphics.Image)
fmt.Println(tip.Rect)
// Now color each pixel in tip with the pencil color.
tip.For(func(x, y int) xgraphics.BGRA {
return xgraphics.BlendBGRA(tip.At(x, y).(xgraphics.BGRA), pencil)
})
// Now draw the changes to the pixmap.
tip.XDraw()
// And paint them to the window.
tip.XPaint(win.Id)
}
// drawGopher draws the gopher image to the canvas.
func drawGopher(canvas *xgraphics.Image, gopher image.Image,
win *xwindow.Window, x, y int) {
// Find the rectangle of the canvas where we're going to draw the gopher.
gopherRect := midRect(x, y, gopherWidth, gopherHeight, width, height)
// If the rectangle contains no pixels, don't draw anything.
if gopherRect.Empty() {
return
}
// Output a little message.
log.Printf("Drawing gopher at (%d, %d)", x, y)
// Get a subimage of the gopher that's in sync with gopherRect.
gopherPt := image.Pt(gopher.Bounds().Min.X, gopher.Bounds().Min.Y)
if gopherRect.Min.X == 0 {
gopherPt.X = gopherWidth - gopherRect.Dx()
}
if gopherRect.Min.Y == 0 {
gopherPt.Y = gopherHeight - gopherRect.Dy()
}
// Create the canvas subimage.
subCanvas := canvas.SubImage(gopherRect).(*xgraphics.Image)
// Blend the gopher image into the sub-canvas.
// This does alpha blending.
xgraphics.Blend(subCanvas, gopher, gopherPt)
// Now draw the changes to the pixmap.
subCanvas.XDraw()
// And paint them to the window.
subCanvas.XPaint(win.Id)
}
// clearCanvas erases all your pencil marks.
func clearCanvas(canvas *xgraphics.Image, win *xwindow.Window) {
log.Println("Clearing canvas...")
canvas.For(func(x, y int) xgraphics.BGRA {
return bg
})
canvas.XDraw()
canvas.XPaint(win.Id)
}
func fatal(err error) {
if err != nil {
log.Panic(err)
}
}
func main() {
X, err := xgbutil.NewConn()
fatal(err)
// Whenever the mousebind package is used, you must call Initialize.
// Similarly for the keybind package.
keybind.Initialize(X)
mousebind.Initialize(X)
// Easter egg! Use a right click to draw a gopher.
gopherPng, _, err := image.Decode(bytes.NewBuffer(gopher.GopherPng()))
fatal(err)
// Now scale it to a reasonable size.
gopher := xgraphics.Scale(gopherPng, gopherWidth, gopherHeight)
// Create a new xgraphics.Image. It automatically creates an X pixmap for
// you, and handles drawing to windows in the XDraw, XPaint and
// XSurfaceSet functions.
// N.B. An error is possible since X pixmap allocation can fail.
canvas := xgraphics.New(X, image.Rect(0, 0, width, height))
// Color in the background color.
canvas.For(func(x, y int) xgraphics.BGRA {
return bg
})
// Use the convenience function XShowExtra to create and map the
// canvas window.
// XShowExtra will also set the surface window of canvas for us.
// We also use XShowExtra to set the name of the window and to quit the
// main event loop when the window is closed.
win := canvas.XShowExtra("Pointer painting", true)
// Listen for pointer motion events and key press events.
win.Listen(xproto.EventMaskButtonPress | xproto.EventMaskButtonRelease |
xproto.EventMaskKeyPress)
// The mousebind drag function runs three callbacks: one when the drag is
// first started, another at each "step" in the drag, and a final one when
// the drag is done.
// The button sequence (in this case '1') is pressed, the first callback
// is executed. If the first return value is true, the drag continues
// and a pointer grab is initiated with the cursor id specified in the
// second return value (use 0 to keep the cursor unchanged).
// If it's false, the drag stops.
// Note that Drag will automatically compress MotionNotify events.
mousebind.Drag(X, win.Id, win.Id, "1", false,
func(X *xgbutil.XUtil, rx, ry, ex, ey int) (bool, xproto.Cursor) {
drawPencil(canvas, win, ex, ey)
return true, 0
},
func(X *xgbutil.XUtil, rx, ry, ex, ey int) {
drawPencil(canvas, win, ex, ey)
},
func(X *xgbutil.XUtil, rx, ry, ex, ey int) {})
mousebind.Drag(X, win.Id, win.Id, "3", false,
func(X *xgbutil.XUtil, rx, ry, ex, ey int) (bool, xproto.Cursor) {
drawGopher(canvas, gopher, win, ex, ey)
return true, 0
},
func(X *xgbutil.XUtil, rx, ry, ex, ey int) {
drawGopher(canvas, gopher, win, ex, ey)
},
func(X *xgbutil.XUtil, rx, ry, ex, ey int) {})
// Bind to the clear key specified, and just redraw the bg color.
keybind.KeyPressFun(
func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) {
clearCanvas(canvas, win)
}).Connect(X, win.Id, clearKey, false)
// Bind a callback to each key specified in the 'pencils' color map.
// The response is to simply switch the pencil color.
for key, clr := range pencils {
c := clr
keybind.KeyPressFun(
func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) {
log.Printf("Changing pencil color to: %#v", c)
pencil = c
}).Connect(X, win.Id, key, false)
}
// Output some basic directions.
fmt.Println("Use the left or right buttons on your mouse to paint " +
"squares and gophers.")
fmt.Println("Pressing numbers 1, 2, 3, 4, 5 or 6 will switch your pencil " +
"color.")
fmt.Println("Pressing 'c' will clear the canvas.")
xevent.Main(X)
}
// midRect takes an (x, y) position where the pointer was clicked, along with
// the width and height of the thing being drawn and the width and height of
// the canvas, and returns a Rectangle whose midpoint (roughly) is (x, y) and
// whose width and height match the parameters when the rectangle doesn't
// extend past the border of the canvas. Make sure to check if the rectange is
// empty or not before using it!
func midRect(x, y, width, height, canWidth, canHeight int) image.Rectangle {
return image.Rect(
max(0, min(canWidth, x-width/2)), // top left x
max(0, min(canHeight, y-height/2)), // top left y
max(0, min(canWidth, x+width/2)), // bottom right x
max(0, min(canHeight, y+height/2)), // bottom right y
)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}

View File

@ -0,0 +1,43 @@
// Example screenshot shows how to take a screenshot of the current desktop
// and show it in a window. In a comment, it also shows how to save it as
// a png.
//
// It works by getting the image of the root window, which automatically
// includes all child windows.
package main
import (
"log"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xgraphics"
)
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Use the "NewDrawable" constructor to create an xgraphics.Image value
// from a drawable. (Usually this is done with pixmaps, but drawables
// can also be windows.)
ximg, err := xgraphics.NewDrawable(X, xproto.Drawable(X.RootWin()))
if err != nil {
log.Fatal(err)
}
// Shows the screenshot in a window.
ximg.XShowExtra("Screenshot", true)
// If you'd like to save it as a png, use:
// err = ximg.SavePng("screenshot.png")
// if err != nil {
// log.Fatal(err)
// }
xevent.Main(X)
}

View File

@ -0,0 +1,42 @@
// Example show-image is a very simple example to show how to use the
// xgraphics package to show an image in a window.
package main
import (
"bytes"
"image"
_ "image/png"
"log"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/gopher"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xgraphics"
)
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Read an example gopher image into a regular png image.
img, _, err := image.Decode(bytes.NewBuffer(gopher.GopherPng()))
if err != nil {
log.Fatal(err)
}
// Now convert it into an X image.
ximg := xgraphics.NewConvert(X, img)
// Now show it in a new window.
// We set the window title and tell the program to quit gracefully when
// the window is closed.
// There is also a convenience method, XShow, that requires no parameters.
ximg.XShowExtra("The Go Gopher!", true)
// If we don't block, the program will end and the window will disappear.
// We could use a 'select{}' here, but xevent.Main will emit errors if
// something went wrong, so use that instead.
xevent.Main(X)
}

View File

@ -0,0 +1,69 @@
// Example show-window-icons shows how to get a list of all top-level client
// windows managed by the currently running window manager, and show the icon
// for each window. (Each icon is shown by opening its own window.)
package main
import (
"image/color"
"log"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/ewmh"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xgraphics"
)
var (
// The icon width and height to use.
// _NET_WM_ICON will be searched for an icon closest to these values.
// The icon closest in size to what's specified here will be used.
// The resulting icon will be scaled to this size.
// (Set both to 0 to avoid scaling and use the biggest possible icon.)
iconWidth, iconHeight = 300, 300
)
func main() {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Get the list of window ids managed by the window manager.
clients, err := ewmh.ClientListGet(X)
if err != nil {
log.Fatal(err)
}
// For each client, try to find its icon. If we find one, blend it with
// a nice background color and show it in its own window.
// Otherwise, skip it.
for _, wid := range clients {
// FindIcon will find an icon closest to the size specified.
// If one can't be found, the resulting image will be scaled
// automatically.
// To avoid scaling the icon, specify '0' for both the width and height.
// In this case, the largest icon found will be returned.
xicon, err := xgraphics.FindIcon(X, wid, iconWidth, iconHeight)
if err != nil {
log.Printf("Could not find icon for window %d.", wid)
continue
}
// Get the name of this client. (It will be set as the icon window's
// name.)
name, err := ewmh.WmNameGet(X, wid)
if err != nil { // not a fatal error
log.Println(err)
name = ""
}
// Blend a pink background color so its easy to see that alpha blending
// works.
xgraphics.BlendBgColor(xicon, color.RGBA{0xff, 0x0, 0xff, 0xff})
xicon.XShowExtra(name, false)
}
// All we really need to do is block, so a 'select{}' would be sufficient.
// But running the event loop will emit errors if anything went wrong.
xevent.Main(X)
}

View File

@ -0,0 +1,82 @@
// Example simple-keybinding shows how to grab keys on the root window and
// respond to them via callback functions. It also shows how to remove such
// callbacks so that they no longer respond to the key events.
// Note that more documentation can be found in the keybind package.
package main
import (
"log"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/keybind"
"github.com/jezek/xgbutil/xevent"
)
func main() {
// Connect to the X server using the DISPLAY environment variable.
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Anytime the keybind (mousebind) package is used, keybind.Initialize
// *should* be called once. It isn't strictly necessary, but allows your
// keybindings to persist even if the keyboard mapping is changed during
// run-time. (Assuming you're using the xevent package's event loop.)
keybind.Initialize(X)
// Before attaching callbacks, wrap them in a callback function type.
// The keybind package exposes two such callback types: keybind.KeyPressFun
// and keybind.KeyReleaseFun.
cb1 := keybind.KeyPressFun(
func(X *xgbutil.XUtil, e xevent.KeyPressEvent) {
log.Println("Key press!")
})
// We can now attach the callback to a particular window and key
// combination. This particular example grabs a key on the root window,
// which makes it a global keybinding.
// Also, "Mod4-j" typically corresponds to pressing down the "Super" or
// "Windows" key on your keyboard, and then pressing the letter "j".
// N.B. This approach works by issuing a passive grab on the window
// specified. To respond to Key{Press,Release} events without a grab, use
// the xevent.Key{Press,Release}Fun callback function types instead.
err = cb1.Connect(X, X.RootWin(), "Mod4-j", true)
// A keybinding can fail if the key string could not be parsed, or if you're
// trying to bind a key that has already been grabbed by another client.
if err != nil {
log.Fatal(err)
}
// We can even attach multiple callbacks to the same key.
err = keybind.KeyPressFun(
func(X *xgbutil.XUtil, e xevent.KeyPressEvent) {
log.Println("A second handler always happens after the first.")
}).Connect(X, X.RootWin(), "Mod4-j", true)
if err != nil {
log.Fatal(err)
}
// Finally, if we want this client to stop responding to key events, we
// can attach another handler that, when run, detaches all previous
// handlers.
// This time, we'll show an example of a KeyRelease binding.
err = keybind.KeyReleaseFun(
func(X *xgbutil.XUtil, e xevent.KeyReleaseEvent) {
// Use keybind.Detach to detach the root window
// from all KeyPress *and* KeyRelease handlers.
keybind.Detach(X, X.RootWin())
log.Printf("Detached all Key{Press,Release}Events from the "+
"root window (%d).", X.RootWin())
}).Connect(X, X.RootWin(), "Mod4-Shift-q", true)
if err != nil {
log.Fatal(err)
}
// Finally, start the main event loop. This will route any appropriate
// KeyPressEvents to your callback function.
log.Println("Program initialized. Start pressing keys!")
xevent.Main(X)
}

View File

@ -0,0 +1,85 @@
// Example simple-mousebinding shows how to grab buttons on the root window and
// respond to them via callback functions. It also shows how to remove such
// callbacks so that they no longer respond to the button events.
// Note that more documentation can be found in the mousebind package.
package main
import (
"log"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/mousebind"
"github.com/jezek/xgbutil/xevent"
)
func main() {
// Connect to the X server using the DISPLAY environment variable.
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Anytime the mousebind (keybind) package is used, mousebind.Initialize
// *should* be called once. In the case of the mousebind package, this
// isn't strictly necessary, but the 'Drag' features of the mousebind
// package won't work without it.
mousebind.Initialize(X)
// Before attaching callbacks, wrap them in a callback function type.
// The mouse package exposes two such callback types:
// mousebind.ButtonPressFun and mousebind.ButtonReleaseFun.
cb1 := mousebind.ButtonPressFun(
func(X *xgbutil.XUtil, e xevent.ButtonPressEvent) {
log.Println("Button press!")
})
// We can now attach the callback to a particular window and button
// combination. This particular example grabs a button on the root window,
// which makes it a global mouse binding.
// Also, "Mod4-1" typically corresponds to pressing down the "Super" or
// "Windows" key on your keyboard, and then pressing the left mouse button.
// The last two parameters are whether to make a synchronous grab and
// whether to actually issue a grab, respectively.
// (The parameters used here are the common case.)
// See the documentation for the Connect method for more details.
err = cb1.Connect(X, X.RootWin(), "Mod4-1", false, true)
// A mouse binding can fail if the mouse string could not be parsed, or if
// you're trying to bind a button that has already been grabbed by another
// client.
if err != nil {
log.Fatal(err)
}
// We can even attach multiple callbacks to the same button.
err = mousebind.ButtonPressFun(
func(X *xgbutil.XUtil, e xevent.ButtonPressEvent) {
log.Println("A second handler always happens after the first.")
}).Connect(X, X.RootWin(), "Mod4-1", false, true)
if err != nil {
log.Fatal(err)
}
// Finally, if we want this client to stop responding to mouse events, we
// can attach another handler that, when run, detaches all previous
// handlers.
// This time, we'll show an example of a ButtonRelease binding.
err = mousebind.ButtonReleaseFun(
func(X *xgbutil.XUtil, e xevent.ButtonReleaseEvent) {
// Use mousebind.Detach to detach the root window
// from all ButtonPress *and* ButtonRelease handlers.
mousebind.Detach(X, X.RootWin())
mousebind.Detach(X, X.RootWin())
log.Printf("Detached all Button{Press,Release}Events from the "+
"root window (%d).", X.RootWin())
}).Connect(X, X.RootWin(), "Mod4-Shift-1", false, true)
if err != nil {
log.Fatal(err)
}
// Finally, start the main event loop. This will route any appropriate
// ButtonPressEvents to your callback function.
log.Println("Program initialized. Start pressing mouse buttons!")
xevent.Main(X)
}

View File

@ -0,0 +1,174 @@
// Example window-gradient demonstrates how to create several windows and draw
// gradients as their background. Namely, it shows how to use the
// xgraphics.Image type as a canvas that can change size. This example also
// demonstrates how to compress ConfigureNotify events so that the gradient
// drawing does not lag behind the rate of incoming ConfigureNotify events.
package main
import (
"image"
"image/color"
"log"
"math/rand"
"time"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/xevent"
"github.com/jezek/xgbutil/xgraphics"
"github.com/jezek/xgbutil/xwindow"
)
func main() {
rand.Seed(time.Now().UnixNano())
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Create three gradient windows of varying size with random colors.
// Waiting a little bit inbetween seems to increase the diversity of the
// random colors.
newGradientWindow(X, 200, 200, newRandomColor(), newRandomColor())
time.Sleep(500 * time.Millisecond)
newGradientWindow(X, 400, 400, newRandomColor(), newRandomColor())
time.Sleep(500 * time.Millisecond)
newGradientWindow(X, 600, 600, newRandomColor(), newRandomColor())
xevent.Main(X)
}
// newGradientWindow creates a new X window, paints the initial gradient
// image, and listens for ConfigureNotify events. (A new gradient image must
// be painted in response to each ConfigureNotify event, since a
// ConfigureNotify event corresponds to a change in the window's geometry.)
func newGradientWindow(X *xgbutil.XUtil, width, height int,
start, end color.RGBA) {
// Generate a new window id.
win, err := xwindow.Generate(X)
if err != nil {
log.Fatal(err)
}
// Create the window and die if it fails.
err = win.CreateChecked(X.RootWin(), 0, 0, width, height, 0)
if err != nil {
log.Fatal(err)
}
// In order to get ConfigureNotify events, we must listen to the window
// using the 'StructureNotify' mask.
win.Listen(xproto.EventMaskStructureNotify)
// Paint the initial gradient to the window and then map the window.
paintGradient(X, win.Id, width, height, start, end)
win.Map()
xevent.ConfigureNotifyFun(
func(X *xgbutil.XUtil, ev xevent.ConfigureNotifyEvent) {
// If the width and height have not changed, skip this one.
if int(ev.Width) == width && int(ev.Height) == height {
return
}
// Compress ConfigureNotify events so that we don't lag when
// drawing gradients in response.
ev = compressConfigureNotify(X, ev)
// Update the width and height and paint the gradient image.
width, height = int(ev.Width), int(ev.Height)
paintGradient(X, win.Id, width, height, start, end)
}).Connect(X, win.Id)
}
// paintGradient creates a new xgraphics.Image value and draws a gradient
// starting at color 'start' and ending at color 'end'.
//
// Since xgraphics.Image values use pixmaps and pixmaps cannot be resized,
// a new pixmap must be allocated for each resize event.
func paintGradient(X *xgbutil.XUtil, wid xproto.Window, width, height int,
start, end color.RGBA) {
ximg := xgraphics.New(X, image.Rect(0, 0, width, height))
// Now calculate the increment step between each RGB channel in
// the start and end colors.
rinc := (0xff * (int(end.R) - int(start.R))) / width
ginc := (0xff * (int(end.G) - int(start.G))) / width
binc := (0xff * (int(end.B) - int(start.B))) / width
// Now apply the increment to each "column" in the image.
// Using 'ForExp' allows us to bypass the creation of a color.BGRA value
// for each pixel in the image.
ximg.ForExp(func(x, y int) (uint8, uint8, uint8, uint8) {
return uint8(int(start.B) + (binc*x)/0xff),
uint8(int(start.G) + (ginc*x)/0xff),
uint8(int(start.R) + (rinc*x)/0xff),
0xff
})
// Set the surface to paint on for ximg.
// (This simply sets the background pixmap of the window to the pixmap
// used by ximg.)
ximg.XSurfaceSet(wid)
// XDraw will draw the contents of ximg to its corresponding pixmap.
ximg.XDraw()
// XPaint will "clear" the window provided so that it shows the updated
// pixmap.
ximg.XPaint(wid)
// Since we will not reuse ximg, we must destroy its pixmap.
ximg.Destroy()
}
// compressConfigureNotify "compresses" incoming ConfigureNotify events so that
// event processing never lags behind gradient drawing.
// This is necessary because drawing a gradient cannot keep up with the rate
// at which ConfigureNotify events are sent to us, thereby creating a "lag".
// Compression works by examining the "future" of the event queue, and skipping
// ahead to the most recent ConfigureNotify event and throwing away the rest.
//
// A more detailed treatment of event compression can be found in
// xgbutil/examples/compress-events.
func compressConfigureNotify(X *xgbutil.XUtil,
ev xevent.ConfigureNotifyEvent) xevent.ConfigureNotifyEvent {
// Catch up with all X events as much as we can.
X.Sync()
xevent.Read(X, false) // non-blocking
laste := ev
for i, ee := range xevent.Peek(X) {
if ee.Err != nil {
continue
}
if cn, ok := ee.Event.(xproto.ConfigureNotifyEvent); ok {
// Only compress this ConfigureNotify if it matches the window
// of the original event.
if ev.Event == cn.Event && ev.Window == cn.Window {
laste = xevent.ConfigureNotifyEvent{&cn}
defer func(i int) { xevent.DequeueAt(X, i) }(i)
}
}
}
return laste
}
// newRandomColor creates a new RGBA color where each channel (except alpha)
// is randomly generated.
func newRandomColor() color.RGBA {
return color.RGBA{
R: uint8(rand.Intn(256)),
G: uint8(rand.Intn(256)),
B: uint8(rand.Intn(256)),
A: 0xff,
}
}

View File

@ -0,0 +1,64 @@
// Example window-names fetches a list of all top-level client windows managed
// by the currently running window manager, and prints the name and size
// of each window.
//
// This example demonstrates how to use some aspects of the ewmh and icccm
// packages. It also shows how to use the xwindow package to find the
// geometry of a client window. In particular, finding the geometry is
// intelligent, as it includes the geometry of the decorations if they exist.
package main
import (
"fmt"
"log"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/ewmh"
"github.com/jezek/xgbutil/icccm"
"github.com/jezek/xgbutil/xwindow"
)
func main() {
// Connect to the X server using the DISPLAY environment variable.
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Get a list of all client ids.
clientids, err := ewmh.ClientListGet(X)
if err != nil {
log.Fatal(err)
}
// Iterate through each client, find its name and find its size.
for _, clientid := range clientids {
name, err := ewmh.WmNameGet(X, clientid)
// If there was a problem getting _NET_WM_NAME or if its empty,
// try the old-school version.
if err != nil || len(name) == 0 {
name, err = icccm.WmNameGet(X, clientid)
// If we still can't find anything, give up.
if err != nil || len(name) == 0 {
name = "N/A"
}
}
// Now find the geometry, including decorations, of the client window.
// Note that DecorGeometry actually traverses the window tree by
// issuing QueryTree requests until a top-level window (i.e., its
// parent is the root window) is found. The geometry of *that* window
// is then returned.
dgeom, err := xwindow.New(X, clientid).DecorGeometry()
if err != nil {
log.Printf("Could not get geometry for %s (0x%X) because: %s",
name, clientid, err)
continue
}
fmt.Printf("%s (0x%x)\n", name, clientid)
fmt.Printf("\tGeometry: %s\n", dgeom)
}
}

View File

@ -0,0 +1,88 @@
// Example workarea-struts shows how to find the all of the struts set by
// top-level clients and apply them to each active monitor to get the true
// workarea for each monitor.
//
// For example, consider a monitor with a 1920x1080 resolution and a panel
// on the bottom of the desktop 50 pixels tall. The actual workarea for the
// desktop then, is (0, 0) 1920x1030. (When there is more than one monitor,
// the x and y coordinates will be adjusted too.)
package main
import (
"fmt"
"log"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/ewmh"
"github.com/jezek/xgbutil/xinerama"
"github.com/jezek/xgbutil/xrect"
"github.com/jezek/xgbutil/xwindow"
)
func main() {
// Connect to the X server using the DISPLAY environment variable.
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// Wrap the root window in a nice Window type.
root := xwindow.New(X, X.RootWin())
// Get the geometry of the root window.
rgeom, err := root.Geometry()
if err != nil {
log.Fatal(err)
}
// Get the rectangles for each of the active physical heads.
// These are returned sorted in order from left to right and then top
// to bottom.
// But first check if Xinerama is enabled. If not, use root geometry.
var heads xinerama.Heads
if X.ExtInitialized("XINERAMA") {
heads, err = xinerama.PhysicalHeads(X)
if err != nil {
log.Fatal(err)
}
} else {
heads = xinerama.Heads{rgeom}
}
// Fetch the list of top-level client window ids currently managed
// by the running window manager.
clients, err := ewmh.ClientListGet(X)
if err != nil {
log.Fatal(err)
}
// Output the head geometry before modifying them.
fmt.Println("Workarea for each head:")
for i, head := range heads {
fmt.Printf("\tHead #%d: %s\n", i+1, head)
}
// For each client, check to see if it has struts, and if so, apply
// them to our list of head rectangles.
for _, clientid := range clients {
strut, err := ewmh.WmStrutPartialGet(X, clientid)
if err != nil { // no struts for this client
continue
}
// Apply the struts to our heads.
// This modifies 'heads' in place.
xrect.ApplyStrut(heads, uint(rgeom.Width()), uint(rgeom.Height()),
strut.Left, strut.Right, strut.Top, strut.Bottom,
strut.LeftStartY, strut.LeftEndY,
strut.RightStartY, strut.RightEndY,
strut.TopStartX, strut.TopEndX,
strut.BottomStartX, strut.BottomEndX)
}
// Now output the head geometry again after modification.
fmt.Println("Workarea for each head after applying struts:")
for i, head := range heads {
fmt.Printf("\tHead #%d: %s\n", i+1, head)
}
}

Some files were not shown because too many files have changed in this diff Show More