diff options
Diffstat (limited to 'test.go')
-rw-r--r-- | test.go | 68 |
1 files changed, 67 insertions, 1 deletions
@@ -1,9 +1,75 @@ package main import ( + "fmt" "xgob" ) +func toUint32 (buf []byte) uint32 { + rv := uint32(buf[0]) << 24 + rv |= uint32(buf[1]) << 16 + rv |= uint32(buf[2]) << 8 + rv |= uint32(buf[3]) + return rv +} + +func decodeGif (in chan interface{}, out chan uint32, err chan xgob.Error) { + r := <- in + switch reply := r.(type) { + case xgob.Reply: + focus := toUint32(reply.Remainder[:4]) + out <- focus + case xgob.Error: + err <- reply + out <- 0x80000000 + } +} + func main () { - xgob.Connect("") + c, _ := xgob.Connect("") + + error := make(chan xgob.Error) + + var gifRequest [8]byte + gifRequest[0] = 43 /* X_GetInputFocus */ + gifRequest[3] = 1 /* request length */ + + done := make(chan uint32) + + cookie1 := c.WriteRequest(gifRequest[:4]) + go decodeGif(cookie1, done, error) + + gifRequest[3] = 2 // bad length + cookie2 := c.WriteRequest(gifRequest[:]) + go decodeGif(cookie2, done, error) + + c.Flush() + + if c.HasError() { + return + } + + /* Wait for both replies, process events */ + var i int + for i < 2 { + select { + case win := <- done: + if win < 0x80000000 { + fmt.Printf("Window with focus: 0x%X", win) + fmt.Println() + } + i++ + case e := <- error: + fmt.Printf("Error of type %d", e.Error) + fmt.Println() + fmt.Printf(" request %d.%d", e.Major, e.Minor) + fmt.Println() + fmt.Printf(" sequence %d", e.Sequence) + fmt.Println() + fmt.Printf(" value 0x%X (%d)", e.BadValue, e.BadValue) + fmt.Println() + case event := <- c.Event: + println("Unexpected event", event.EventType) + } + } } |