55package x11driver
66
77import (
8+ "errors"
89 "fmt"
910 "image"
1011 "image/color"
@@ -28,6 +29,8 @@ import (
2829// For example, its Conn.WaitForEvent concept is a method, not a channel, so
2930// it's not obvious how to interrupt it to service a NewWindow request.
3031
32+ var XConnectionClosed = errors .New ("x11driver: X server connection closed" )
33+
3134type screenImpl struct {
3235 xc * xgb.Conn
3336 xsi * xproto.ScreenInfo
@@ -125,6 +128,11 @@ func (s *screenImpl) run() {
125128
126129 noWindowFound := false
127130 switch ev := ev .(type ) {
131+ case nil :
132+ s .xc = nil
133+ log .Printf ("x11driver: X server connection closed" )
134+ return
135+
128136 case xproto.DestroyNotifyEvent :
129137 s .mu .Lock ()
130138 delete (s .windows , ev .Window )
@@ -284,6 +292,9 @@ const (
284292)
285293
286294func (s * screenImpl ) NewBuffer (size image.Point ) (retBuf screen.Buffer , retErr error ) {
295+ if s .xc == nil {
296+ return nil , XConnectionClosed
297+ }
287298
288299 w , h := int64 (size .X ), int64 (size .Y )
289300 if w < 0 || maxShmSide < w || h < 0 || maxShmSide < h || maxShmSize < 4 * w * h {
@@ -354,6 +365,9 @@ func (s *screenImpl) NewBuffer(size image.Point) (retBuf screen.Buffer, retErr e
354365}
355366
356367func (s * screenImpl ) NewTexture (size image.Point ) (screen.Texture , error ) {
368+ if s .xc == nil {
369+ return nil , XConnectionClosed
370+ }
357371 w , h := int64 (size .X ), int64 (size .Y )
358372 if w < 0 || maxShmSide < w || h < 0 || maxShmSide < h || maxShmSize < 4 * w * h {
359373 return nil , fmt .Errorf ("x11driver: invalid texture size %v" , size )
@@ -391,6 +405,10 @@ func (s *screenImpl) NewTexture(size image.Point) (screen.Texture, error) {
391405}
392406
393407func (s * screenImpl ) NewWindow (opts * screen.NewWindowOptions ) (screen.Window , error ) {
408+ if s .xc == nil {
409+ return nil , XConnectionClosed
410+ }
411+
394412 width , height := 1024 , 768
395413 if opts != nil {
396414 if opts .Width > 0 {
@@ -654,6 +672,10 @@ func (s *screenImpl) setProperty(xw xproto.Window, prop xproto.Atom, values ...x
654672}
655673
656674func (s * screenImpl ) drawUniform (xp render.Picture , src2dst * f64.Aff3 , src color.Color , sr image.Rectangle , op draw.Op , opts * screen.DrawOptions ) {
675+ if s .xc == nil {
676+ return
677+ }
678+
657679 if sr .Empty () {
658680 return
659681 }
0 commit comments