Commit 6c8b8527 authored by Nigel Tao's avatar Nigel Tao

exp/draw: rename Context to Window, and add a Close method.

exp/draw/x11: allow clean shutdown when the user closes the window.

R=r
CC=golang-dev
https://golang.org/cl/2134045
parent 1d62becb
...@@ -6,19 +6,20 @@ package draw ...@@ -6,19 +6,20 @@ package draw
import ( import (
"image" "image"
"os"
) )
// A Context represents a single graphics window. // A Window represents a single graphics window.
type Context interface { type Window interface {
// Screen returns an editable Image of window. // Screen returns an editable Image for the window.
Screen() Image Screen() Image
// FlushImage flushes changes made to Screen() back to screen. // FlushImage flushes changes made to Screen() back to screen.
FlushImage() FlushImage()
// EventChan returns a channel carrying UI events such as key presses, // EventChan returns a channel carrying UI events such as key presses,
// mouse movements and window resizes. // mouse movements and window resizes.
EventChan() <-chan interface{} EventChan() <-chan interface{}
// Close closes the window.
Close() os.Error
} }
// A KeyEvent is sent for a key press or release. // A KeyEvent is sent for a key press or release.
...@@ -44,7 +45,12 @@ type MouseEvent struct { ...@@ -44,7 +45,12 @@ type MouseEvent struct {
} }
// A ConfigEvent is sent each time the window's color model or size changes. // A ConfigEvent is sent each time the window's color model or size changes.
// The client should respond by calling Context.Screen to obtain a new image. // The client should respond by calling Window.Screen to obtain a new image.
type ConfigEvent struct { type ConfigEvent struct {
Config image.Config Config image.Config
} }
// An ErrEvent is sent when an error occurs.
type ErrEvent struct {
Err os.Error
}
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"exp/draw" "exp/draw"
"image" "image"
"io" "io"
"log"
"net" "net"
"os" "os"
"strconv" "strconv"
...@@ -36,9 +37,6 @@ const ( ...@@ -36,9 +37,6 @@ const (
) )
type conn struct { type conn struct {
// TODO(nigeltao): Figure out which goroutine should be responsible for closing c,
// or if there is a race condition if one goroutine calls c.Close whilst another one
// is reading from r, or writing to w.
c io.Closer c io.Closer
r *bufio.Reader r *bufio.Reader
w *bufio.Writer w *bufio.Writer
...@@ -56,15 +54,12 @@ type conn struct { ...@@ -56,15 +54,12 @@ type conn struct {
flushBuf1 [4 * 1024]byte flushBuf1 [4 * 1024]byte
} }
// flusher runs in its own goroutine, serving both FlushImage calls directly from the exp/draw client // writeSocket runs in its own goroutine, serving both FlushImage calls
// and indirectly from X expose events. It paints c.img to the X server via PutImage requests. // directly from the exp/draw client and indirectly from X expose events.
func (c *conn) flusher() { // It paints c.img to the X server via PutImage requests.
for { func (c *conn) writeSocket() {
_ = <-c.flush defer c.c.Close()
if closed(c.flush) { for _ = range c.flush {
return
}
b := c.img.Bounds() b := c.img.Bounds()
if b.Empty() { if b.Empty() {
continue continue
...@@ -76,8 +71,7 @@ func (c *conn) flusher() { ...@@ -76,8 +71,7 @@ func (c *conn) flusher() {
// TODO(nigeltao): See what XCB's xcb_put_image does in this situation. // TODO(nigeltao): See what XCB's xcb_put_image does in this situation.
units := 6 + b.Dx() units := 6 + b.Dx()
if units > 0xffff || b.Dy() > 0xffff { if units > 0xffff || b.Dy() > 0xffff {
// This window is too large for X. log.Stderr("x11: window is too large for PutImage")
close(c.flush)
return return
} }
...@@ -92,9 +86,10 @@ func (c *conn) flusher() { ...@@ -92,9 +86,10 @@ func (c *conn) flusher() {
for y := b.Min.Y; y < b.Max.Y; y++ { for y := b.Min.Y; y < b.Max.Y; y++ {
setU32LE(c.flushBuf0[16:20], uint32(y<<16)) setU32LE(c.flushBuf0[16:20], uint32(y<<16))
_, err := c.w.Write(c.flushBuf0[0:24]) if _, err := c.w.Write(c.flushBuf0[0:24]); err != nil {
if err != nil { if err != os.EOF {
close(c.flush) log.Stderr("x11: " + err.String())
}
return return
} }
p := c.img.Pix[y*c.img.Stride : (y+1)*c.img.Stride] p := c.img.Pix[y*c.img.Stride : (y+1)*c.img.Stride]
...@@ -109,15 +104,18 @@ func (c *conn) flusher() { ...@@ -109,15 +104,18 @@ func (c *conn) flusher() {
c.flushBuf1[4*i+2] = rgba.R c.flushBuf1[4*i+2] = rgba.R
} }
x += nx x += nx
_, err := c.w.Write(c.flushBuf1[0 : 4*nx]) if _, err := c.w.Write(c.flushBuf1[0 : 4*nx]); err != nil {
if err != nil { if err != os.EOF {
close(c.flush) log.Stderr("x11: " + err.String())
}
return return
} }
} }
} }
if c.w.Flush() != nil { if err := c.w.Flush(); err != nil {
close(c.flush) if err != os.EOF {
log.Stderr("x11: " + err.String())
}
return return
} }
} }
...@@ -132,26 +130,32 @@ func (c *conn) FlushImage() { ...@@ -132,26 +130,32 @@ func (c *conn) FlushImage() {
_ = c.flush <- false _ = c.flush <- false
} }
func (c *conn) Close() os.Error {
// Shut down the writeSocket goroutine. This will close the socket to the
// X11 server, which will cause c.eventc to close.
close(c.flush)
for _ = range c.eventc {
// Drain the channel to allow the readSocket goroutine to shut down.
}
return nil
}
func (c *conn) EventChan() <-chan interface{} { return c.eventc } func (c *conn) EventChan() <-chan interface{} { return c.eventc }
// pumper runs in its own goroutine, reading X events and demuxing them over the kbd / mouse / resize / quit chans. // readSocket runs in its own goroutine, reading X events and sending draw
func (c *conn) pumper() { // events on c's EventChan.
func (c *conn) readSocket() {
var ( var (
keymap [256][]int keymap [256][]int
keysymsPerKeycode int keysymsPerKeycode int
) )
defer close(c.flush) defer close(c.eventc)
// TODO(nigeltao): Is this the right place for defer c.c.Close()?
// TODO(nigeltao): Should we explicitly defer close our kbd/mouse/resize/quit chans?
for { for {
// X events are always 32 bytes long. // X events are always 32 bytes long.
_, err := io.ReadFull(c.r, c.buf[0:32]) if _, err := io.ReadFull(c.r, c.buf[0:32]); err != nil {
if err != nil { if err != os.EOF {
// TODO(nigeltao): should draw.Context expose err? c.eventc <- draw.ErrEvent{err}
// TODO(nigeltao): should we do c.quit<-true? Should c.quit be a buffered channel? }
// Or is c.quit only for non-exceptional closing (e.g. when the window manager destroys
// our window), and not for e.g. an I/O error?
os.Stderr.Write([]byte(err.String()))
return return
} }
switch c.buf[0] { switch c.buf[0] {
...@@ -160,7 +164,7 @@ func (c *conn) pumper() { ...@@ -160,7 +164,7 @@ func (c *conn) pumper() {
if cookie != 1 { if cookie != 1 {
// We issued only one request (GetKeyboardMapping) with a cookie of 1, // We issued only one request (GetKeyboardMapping) with a cookie of 1,
// so we shouldn't get any other reply from the X server. // so we shouldn't get any other reply from the X server.
os.Stderr.Write([]byte("exp/draw/x11: unexpected cookie\n")) c.eventc <- draw.ErrEvent{os.NewError("x11: unexpected cookie")}
return return
} }
keysymsPerKeycode = int(c.buf[1]) keysymsPerKeycode = int(c.buf[1])
...@@ -173,7 +177,9 @@ func (c *conn) pumper() { ...@@ -173,7 +177,9 @@ func (c *conn) pumper() {
for j := range m { for j := range m {
u, err := readU32LE(c.r, c.buf[0:4]) u, err := readU32LE(c.r, c.buf[0:4])
if err != nil { if err != nil {
os.Stderr.Write([]byte(err.String())) if err != os.EOF {
c.eventc <- draw.ErrEvent{err}
}
return return
} }
m[j] = int(u) m[j] = int(u)
...@@ -194,10 +200,10 @@ func (c *conn) pumper() { ...@@ -194,10 +200,10 @@ func (c *conn) pumper() {
if keysym == 0 { if keysym == 0 {
keysym = keymap[keycode][0] keysym = keymap[keycode][0]
} }
// TODO(nigeltao): Should we send KeyboardChan ints for Shift/Ctrl/Alt? Should Shift-A send // TODO(nigeltao): Should we send KeyEvents for Shift/Ctrl/Alt? Should Shift-A send
// the same int down the channel as the sent on just the A key? // the same int down the channel as the sent on just the A key?
// TODO(nigeltao): How should IME events (e.g. key presses that should generate CJK text) work? Or // TODO(nigeltao): How should IME events (e.g. key presses that should generate CJK text) work? Or
// is that outside the scope of the draw.Context interface? // is that outside the scope of the draw.Window interface?
if c.buf[0] == 0x03 { if c.buf[0] == 0x03 {
keysym = -keysym keysym = -keysym
} }
...@@ -545,7 +551,7 @@ func (c *conn) handshake() os.Error { ...@@ -545,7 +551,7 @@ func (c *conn) handshake() os.Error {
} }
// NewWindow calls NewWindowDisplay with $DISPLAY. // NewWindow calls NewWindowDisplay with $DISPLAY.
func NewWindow() (draw.Context, os.Error) { func NewWindow() (draw.Window, os.Error) {
display := os.Getenv("DISPLAY") display := os.Getenv("DISPLAY")
if len(display) == 0 { if len(display) == 0 {
return nil, os.NewError("$DISPLAY not set") return nil, os.NewError("$DISPLAY not set")
...@@ -553,10 +559,10 @@ func NewWindow() (draw.Context, os.Error) { ...@@ -553,10 +559,10 @@ func NewWindow() (draw.Context, os.Error) {
return NewWindowDisplay(display) return NewWindowDisplay(display)
} }
// NewWindowDisplay returns a new draw.Context, backed by a newly created and // NewWindowDisplay returns a new draw.Window, backed by a newly created and
// mapped X11 window. The X server to connect to is specified by the display // mapped X11 window. The X server to connect to is specified by the display
// string, such as ":1". // string, such as ":1".
func NewWindowDisplay(display string) (draw.Context, os.Error) { func NewWindowDisplay(display string) (draw.Window, os.Error) {
socket, displayStr, err := connect(display) socket, displayStr, err := connect(display)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -611,9 +617,9 @@ func NewWindowDisplay(display string) (draw.Context, os.Error) { ...@@ -611,9 +617,9 @@ func NewWindowDisplay(display string) (draw.Context, os.Error) {
} }
c.img = image.NewRGBA(windowWidth, windowHeight) c.img = image.NewRGBA(windowWidth, windowHeight)
c.eventc = make(chan interface{}) c.eventc = make(chan interface{}, 16)
c.flush = make(chan bool, 1) c.flush = make(chan bool, 1)
go c.flusher() go c.readSocket()
go c.pumper() go c.writeSocket()
return c, nil return c, nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment