diff options
Diffstat (limited to 'src/pkg/exp/draw')
-rw-r--r-- | src/pkg/exp/draw/Makefile | 12 | ||||
-rw-r--r-- | src/pkg/exp/draw/draw.go | 363 | ||||
-rw-r--r-- | src/pkg/exp/draw/draw_test.go | 228 | ||||
-rw-r--r-- | src/pkg/exp/draw/event.go | 56 | ||||
-rw-r--r-- | src/pkg/exp/draw/x11/Makefile | 12 | ||||
-rw-r--r-- | src/pkg/exp/draw/x11/auth.go | 93 | ||||
-rw-r--r-- | src/pkg/exp/draw/x11/conn.go | 625 |
7 files changed, 0 insertions, 1389 deletions
diff --git a/src/pkg/exp/draw/Makefile b/src/pkg/exp/draw/Makefile deleted file mode 100644 index 6f0f0b2f5..000000000 --- a/src/pkg/exp/draw/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -# Copyright 2009 The Go Authors. All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. - -include ../../../Make.inc - -TARG=exp/draw -GOFILES=\ - draw.go\ - event.go\ - -include ../../../Make.pkg diff --git a/src/pkg/exp/draw/draw.go b/src/pkg/exp/draw/draw.go deleted file mode 100644 index 1d0729d92..000000000 --- a/src/pkg/exp/draw/draw.go +++ /dev/null @@ -1,363 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package draw provides basic graphics and drawing primitives, -// in the style of the Plan 9 graphics library -// (see http://plan9.bell-labs.com/magic/man2html/2/draw) -// and the X Render extension. -package draw - -import "image" - -// m is the maximum color value returned by image.Color.RGBA. -const m = 1<<16 - 1 - -// A Porter-Duff compositing operator. -type Op int - -const ( - // Over specifies ``(src in mask) over dst''. - Over Op = iota - // Src specifies ``src in mask''. - Src -) - -var zeroColor image.Color = image.AlphaColor{0} - -// A draw.Image is an image.Image with a Set method to change a single pixel. -type Image interface { - image.Image - Set(x, y int, c image.Color) -} - -// Draw calls DrawMask with a nil mask and an Over op. -func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) { - DrawMask(dst, r, src, sp, nil, image.ZP, Over) -} - -// DrawMask aligns r.Min in dst with sp in src and mp in mask and then replaces the rectangle r -// in dst with the result of a Porter-Duff composition. A nil mask is treated as opaque. -func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) { - sb := src.Bounds() - dx, dy := sb.Max.X-sp.X, sb.Max.Y-sp.Y - if mask != nil { - mb := mask.Bounds() - if dx > mb.Max.X-mp.X { - dx = mb.Max.X - mp.X - } - if dy > mb.Max.Y-mp.Y { - dy = mb.Max.Y - mp.Y - } - } - if r.Dx() > dx { - r.Max.X = r.Min.X + dx - } - if r.Dy() > dy { - r.Max.Y = r.Min.Y + dy - } - r = r.Intersect(dst.Bounds()) - if r.Empty() { - return - } - - // Fast paths for special cases. If none of them apply, then we fall back to a general but slow implementation. - if dst0, ok := dst.(*image.RGBA); ok { - if op == Over { - if mask == nil { - if src0, ok := src.(*image.ColorImage); ok { - drawFillOver(dst0, r, src0) - return - } - if src0, ok := src.(*image.RGBA); ok { - drawCopyOver(dst0, r, src0, sp) - return - } - } else if mask0, ok := mask.(*image.Alpha); ok { - if src0, ok := src.(*image.ColorImage); ok { - drawGlyphOver(dst0, r, src0, mask0, mp) - return - } - } - } else { - if mask == nil { - if src0, ok := src.(*image.ColorImage); ok { - drawFillSrc(dst0, r, src0) - return - } - if src0, ok := src.(*image.RGBA); ok { - drawCopySrc(dst0, r, src0, sp) - return - } - } - } - drawRGBA(dst0, r, src, sp, mask, mp, op) - return - } - - x0, x1, dx := r.Min.X, r.Max.X, 1 - y0, y1, dy := r.Min.Y, r.Max.Y, 1 - if image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) { - // Rectangles overlap: process backward? - if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X { - x0, x1, dx = x1-1, x0-1, -1 - y0, y1, dy = y1-1, y0-1, -1 - } - } - - var out *image.RGBA64Color - sy := sp.Y + y0 - r.Min.Y - my := mp.Y + y0 - r.Min.Y - for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { - sx := sp.X + x0 - r.Min.X - mx := mp.X + x0 - r.Min.X - for x := x0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx { - ma := uint32(m) - if mask != nil { - _, _, _, ma = mask.At(mx, my).RGBA() - } - switch { - case ma == 0: - if op == Over { - // No-op. - } else { - dst.Set(x, y, zeroColor) - } - case ma == m && op == Src: - dst.Set(x, y, src.At(sx, sy)) - default: - sr, sg, sb, sa := src.At(sx, sy).RGBA() - if out == nil { - out = new(image.RGBA64Color) - } - if op == Over { - dr, dg, db, da := dst.At(x, y).RGBA() - a := m - (sa * ma / m) - out.R = uint16((dr*a + sr*ma) / m) - out.G = uint16((dg*a + sg*ma) / m) - out.B = uint16((db*a + sb*ma) / m) - out.A = uint16((da*a + sa*ma) / m) - } else { - out.R = uint16(sr * ma / m) - out.G = uint16(sg * ma / m) - out.B = uint16(sb * ma / m) - out.A = uint16(sa * ma / m) - } - dst.Set(x, y, out) - } - } - } -} - -func drawFillOver(dst *image.RGBA, r image.Rectangle, src *image.ColorImage) { - cr, cg, cb, ca := src.RGBA() - // The 0x101 is here for the same reason as in drawRGBA. - a := (m - ca) * 0x101 - x0, x1 := r.Min.X, r.Max.X - y0, y1 := r.Min.Y, r.Max.Y - for y := y0; y != y1; y++ { - dbase := y * dst.Stride - dpix := dst.Pix[dbase+x0 : dbase+x1] - for i, rgba := range dpix { - dr := (uint32(rgba.R)*a)/m + cr - dg := (uint32(rgba.G)*a)/m + cg - db := (uint32(rgba.B)*a)/m + cb - da := (uint32(rgba.A)*a)/m + ca - dpix[i] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} - } - } -} - -func drawCopyOver(dst *image.RGBA, r image.Rectangle, src *image.RGBA, sp image.Point) { - dx0, dx1 := r.Min.X, r.Max.X - dy0, dy1 := r.Min.Y, r.Max.Y - nrows := dy1 - dy0 - sx0, sx1 := sp.X, sp.X+dx1-dx0 - d0 := dy0*dst.Stride + dx0 - d1 := dy0*dst.Stride + dx1 - s0 := sp.Y*src.Stride + sx0 - s1 := sp.Y*src.Stride + sx1 - var ( - ddelta, sdelta int - i0, i1, idelta int - ) - if r.Min.Y < sp.Y || r.Min.Y == sp.Y && r.Min.X <= sp.X { - ddelta = dst.Stride - sdelta = src.Stride - i0, i1, idelta = 0, d1-d0, +1 - } else { - // If the source start point is higher than the destination start point, or equal height but to the left, - // then we compose the rows in right-to-left, bottom-up order instead of left-to-right, top-down. - d0 += (nrows - 1) * dst.Stride - d1 += (nrows - 1) * dst.Stride - s0 += (nrows - 1) * src.Stride - s1 += (nrows - 1) * src.Stride - ddelta = -dst.Stride - sdelta = -src.Stride - i0, i1, idelta = d1-d0-1, -1, -1 - } - for ; nrows > 0; nrows-- { - dpix := dst.Pix[d0:d1] - spix := src.Pix[s0:s1] - for i := i0; i != i1; i += idelta { - // For unknown reasons, even though both dpix[i] and spix[i] are - // image.RGBAColors, on an x86 CPU it seems fastest to call RGBA - // for the source but to do it manually for the destination. - sr, sg, sb, sa := spix[i].RGBA() - rgba := dpix[i] - dr := uint32(rgba.R) - dg := uint32(rgba.G) - db := uint32(rgba.B) - da := uint32(rgba.A) - // The 0x101 is here for the same reason as in drawRGBA. - a := (m - sa) * 0x101 - dr = (dr*a)/m + sr - dg = (dg*a)/m + sg - db = (db*a)/m + sb - da = (da*a)/m + sa - dpix[i] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} - } - d0 += ddelta - d1 += ddelta - s0 += sdelta - s1 += sdelta - } -} - -func drawGlyphOver(dst *image.RGBA, r image.Rectangle, src *image.ColorImage, mask *image.Alpha, mp image.Point) { - x0, x1 := r.Min.X, r.Max.X - y0, y1 := r.Min.Y, r.Max.Y - cr, cg, cb, ca := src.RGBA() - for y, my := y0, mp.Y; y != y1; y, my = y+1, my+1 { - dbase := y * dst.Stride - dpix := dst.Pix[dbase+x0 : dbase+x1] - mbase := my * mask.Stride - mpix := mask.Pix[mbase+mp.X:] - for i, rgba := range dpix { - ma := uint32(mpix[i].A) - if ma == 0 { - continue - } - ma |= ma << 8 - dr := uint32(rgba.R) - dg := uint32(rgba.G) - db := uint32(rgba.B) - da := uint32(rgba.A) - // The 0x101 is here for the same reason as in drawRGBA. - a := (m - (ca * ma / m)) * 0x101 - dr = (dr*a + cr*ma) / m - dg = (dg*a + cg*ma) / m - db = (db*a + cb*ma) / m - da = (da*a + ca*ma) / m - dpix[i] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} - } - } -} - -func drawFillSrc(dst *image.RGBA, r image.Rectangle, src *image.ColorImage) { - if r.Dy() < 1 { - return - } - cr, cg, cb, ca := src.RGBA() - color := image.RGBAColor{uint8(cr >> 8), uint8(cg >> 8), uint8(cb >> 8), uint8(ca >> 8)} - // The built-in copy function is faster than a straightforward for loop to fill the destination with - // the color, but copy requires a slice source. We therefore use a for loop to fill the first row, and - // then use the first row as the slice source for the remaining rows. - dx0, dx1 := r.Min.X, r.Max.X - dy0, dy1 := r.Min.Y, r.Max.Y - dbase := dy0 * dst.Stride - i0, i1 := dbase+dx0, dbase+dx1 - firstRow := dst.Pix[i0:i1] - for i := range firstRow { - firstRow[i] = color - } - for y := dy0 + 1; y < dy1; y++ { - i0 += dst.Stride - i1 += dst.Stride - copy(dst.Pix[i0:i1], firstRow) - } -} - -func drawCopySrc(dst *image.RGBA, r image.Rectangle, src *image.RGBA, sp image.Point) { - dx0, dx1 := r.Min.X, r.Max.X - dy0, dy1 := r.Min.Y, r.Max.Y - nrows := dy1 - dy0 - sx0, sx1 := sp.X, sp.X+dx1-dx0 - d0 := dy0*dst.Stride + dx0 - d1 := dy0*dst.Stride + dx1 - s0 := sp.Y*src.Stride + sx0 - s1 := sp.Y*src.Stride + sx1 - var ddelta, sdelta int - if r.Min.Y <= sp.Y { - ddelta = dst.Stride - sdelta = src.Stride - } else { - // If the source start point is higher than the destination start point, then we compose the rows - // in bottom-up order instead of top-down. Unlike the drawCopyOver function, we don't have to - // check the x co-ordinates because the built-in copy function can handle overlapping slices. - d0 += (nrows - 1) * dst.Stride - d1 += (nrows - 1) * dst.Stride - s0 += (nrows - 1) * src.Stride - s1 += (nrows - 1) * src.Stride - ddelta = -dst.Stride - sdelta = -src.Stride - } - for ; nrows > 0; nrows-- { - copy(dst.Pix[d0:d1], src.Pix[s0:s1]) - d0 += ddelta - d1 += ddelta - s0 += sdelta - s1 += sdelta - } -} - -func drawRGBA(dst *image.RGBA, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) { - x0, x1, dx := r.Min.X, r.Max.X, 1 - y0, y1, dy := r.Min.Y, r.Max.Y, 1 - if image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) { - if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X { - x0, x1, dx = x1-1, x0-1, -1 - y0, y1, dy = y1-1, y0-1, -1 - } - } - - sy := sp.Y + y0 - r.Min.Y - my := mp.Y + y0 - r.Min.Y - for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy { - sx := sp.X + x0 - r.Min.X - mx := mp.X + x0 - r.Min.X - dpix := dst.Pix[y*dst.Stride : (y+1)*dst.Stride] - for x := x0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx { - ma := uint32(m) - if mask != nil { - _, _, _, ma = mask.At(mx, my).RGBA() - } - sr, sg, sb, sa := src.At(sx, sy).RGBA() - var dr, dg, db, da uint32 - if op == Over { - rgba := dpix[x] - dr = uint32(rgba.R) - dg = uint32(rgba.G) - db = uint32(rgba.B) - da = uint32(rgba.A) - // dr, dg, db and da are all 8-bit color at the moment, ranging in [0,255]. - // We work in 16-bit color, and so would normally do: - // dr |= dr << 8 - // and similarly for dg, db and da, but instead we multiply a - // (which is a 16-bit color, ranging in [0,65535]) by 0x101. - // This yields the same result, but is fewer arithmetic operations. - a := (m - (sa * ma / m)) * 0x101 - dr = (dr*a + sr*ma) / m - dg = (dg*a + sg*ma) / m - db = (db*a + sb*ma) / m - da = (da*a + sa*ma) / m - } else { - dr = sr * ma / m - dg = sg * ma / m - db = sb * ma / m - da = sa * ma / m - } - dpix[x] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} - } - } -} diff --git a/src/pkg/exp/draw/draw_test.go b/src/pkg/exp/draw/draw_test.go deleted file mode 100644 index 90c9e823d..000000000 --- a/src/pkg/exp/draw/draw_test.go +++ /dev/null @@ -1,228 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package draw - -import ( - "image" - "testing" -) - -func eq(c0, c1 image.Color) bool { - r0, g0, b0, a0 := c0.RGBA() - r1, g1, b1, a1 := c1.RGBA() - return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1 -} - -func fillBlue(alpha int) image.Image { - return image.NewColorImage(image.RGBAColor{0, 0, uint8(alpha), uint8(alpha)}) -} - -func fillAlpha(alpha int) image.Image { - return image.NewColorImage(image.AlphaColor{uint8(alpha)}) -} - -func vgradGreen(alpha int) image.Image { - m := image.NewRGBA(16, 16) - for y := 0; y < 16; y++ { - for x := 0; x < 16; x++ { - m.Set(x, y, image.RGBAColor{0, uint8(y * alpha / 15), 0, uint8(alpha)}) - } - } - return m -} - -func vgradAlpha(alpha int) image.Image { - m := image.NewAlpha(16, 16) - for y := 0; y < 16; y++ { - for x := 0; x < 16; x++ { - m.Set(x, y, image.AlphaColor{uint8(y * alpha / 15)}) - } - } - return m -} - -func hgradRed(alpha int) Image { - m := image.NewRGBA(16, 16) - for y := 0; y < 16; y++ { - for x := 0; x < 16; x++ { - m.Set(x, y, image.RGBAColor{uint8(x * alpha / 15), 0, 0, uint8(alpha)}) - } - } - return m -} - -func gradYellow(alpha int) Image { - m := image.NewRGBA(16, 16) - for y := 0; y < 16; y++ { - for x := 0; x < 16; x++ { - m.Set(x, y, image.RGBAColor{uint8(x * alpha / 15), uint8(y * alpha / 15), 0, uint8(alpha)}) - } - } - return m -} - -type drawTest struct { - desc string - src image.Image - mask image.Image - op Op - expected image.Color -} - -var drawTests = []drawTest{ - // Uniform mask (0% opaque). - {"nop", vgradGreen(255), fillAlpha(0), Over, image.RGBAColor{136, 0, 0, 255}}, - {"clear", vgradGreen(255), fillAlpha(0), Src, image.RGBAColor{0, 0, 0, 0}}, - // Uniform mask (100%, 75%, nil) and uniform source. - // At (x, y) == (8, 8): - // The destination pixel is {136, 0, 0, 255}. - // The source pixel is {0, 0, 90, 90}. - {"fill", fillBlue(90), fillAlpha(255), Over, image.RGBAColor{88, 0, 90, 255}}, - {"fillSrc", fillBlue(90), fillAlpha(255), Src, image.RGBAColor{0, 0, 90, 90}}, - {"fillAlpha", fillBlue(90), fillAlpha(192), Over, image.RGBAColor{100, 0, 68, 255}}, - {"fillAlphaSrc", fillBlue(90), fillAlpha(192), Src, image.RGBAColor{0, 0, 68, 68}}, - {"fillNil", fillBlue(90), nil, Over, image.RGBAColor{88, 0, 90, 255}}, - {"fillNilSrc", fillBlue(90), nil, Src, image.RGBAColor{0, 0, 90, 90}}, - // Uniform mask (100%, 75%, nil) and variable source. - // At (x, y) == (8, 8): - // The destination pixel is {136, 0, 0, 255}. - // The source pixel is {0, 48, 0, 90}. - {"copy", vgradGreen(90), fillAlpha(255), Over, image.RGBAColor{88, 48, 0, 255}}, - {"copySrc", vgradGreen(90), fillAlpha(255), Src, image.RGBAColor{0, 48, 0, 90}}, - {"copyAlpha", vgradGreen(90), fillAlpha(192), Over, image.RGBAColor{100, 36, 0, 255}}, - {"copyAlphaSrc", vgradGreen(90), fillAlpha(192), Src, image.RGBAColor{0, 36, 0, 68}}, - {"copyNil", vgradGreen(90), nil, Over, image.RGBAColor{88, 48, 0, 255}}, - {"copyNilSrc", vgradGreen(90), nil, Src, image.RGBAColor{0, 48, 0, 90}}, - // Variable mask and variable source. - // At (x, y) == (8, 8): - // The destination pixel is {136, 0, 0, 255}. - // The source pixel is {0, 0, 255, 255}. - // The mask pixel's alpha is 102, or 40%. - {"generic", fillBlue(255), vgradAlpha(192), Over, image.RGBAColor{81, 0, 102, 255}}, - {"genericSrc", fillBlue(255), vgradAlpha(192), Src, image.RGBAColor{0, 0, 102, 102}}, -} - -func makeGolden(dst, src, mask image.Image, op Op) image.Image { - // Since golden is a newly allocated image, we don't have to check if the - // input source and mask images and the output golden image overlap. - b := dst.Bounds() - sx0 := src.Bounds().Min.X - b.Min.X - sy0 := src.Bounds().Min.Y - b.Min.Y - var mx0, my0 int - if mask != nil { - mx0 = mask.Bounds().Min.X - b.Min.X - my0 = mask.Bounds().Min.Y - b.Min.Y - } - golden := image.NewRGBA(b.Max.X, b.Max.Y) - for y := b.Min.Y; y < b.Max.Y; y++ { - my, sy := my0+y, sy0+y - for x := b.Min.X; x < b.Max.X; x++ { - mx, sx := mx0+x, sx0+x - const M = 1<<16 - 1 - var dr, dg, db, da uint32 - if op == Over { - dr, dg, db, da = dst.At(x, y).RGBA() - } - sr, sg, sb, sa := src.At(sx, sy).RGBA() - ma := uint32(M) - if mask != nil { - _, _, _, ma = mask.At(mx, my).RGBA() - } - a := M - (sa * ma / M) - golden.Set(x, y, image.RGBA64Color{ - uint16((dr*a + sr*ma) / M), - uint16((dg*a + sg*ma) / M), - uint16((db*a + sb*ma) / M), - uint16((da*a + sa*ma) / M), - }) - } - } - golden.Rect = b - return golden -} - -func TestDraw(t *testing.T) { -loop: - for _, test := range drawTests { - dst := hgradRed(255) - // Draw the (src, mask, op) onto a copy of dst using a slow but obviously correct implementation. - golden := makeGolden(dst, test.src, test.mask, test.op) - b := dst.Bounds() - if !b.Eq(golden.Bounds()) { - t.Errorf("draw %s: bounds %v versus %v", test.desc, dst.Bounds(), golden.Bounds()) - continue - } - // Draw the same combination onto the actual dst using the optimized DrawMask implementation. - DrawMask(dst, b, test.src, image.ZP, test.mask, image.ZP, test.op) - // Check that the resultant pixel at (8, 8) matches what we expect - // (the expected value can be verified by hand). - if !eq(dst.At(8, 8), test.expected) { - t.Errorf("draw %s: at (8, 8) %v versus %v", test.desc, dst.At(8, 8), test.expected) - continue - } - // Check that the resultant dst image matches the golden output. - for y := b.Min.Y; y < b.Max.Y; y++ { - for x := b.Min.X; x < b.Max.X; x++ { - if !eq(dst.At(x, y), golden.At(x, y)) { - t.Errorf("draw %s: at (%d, %d), %v versus golden %v", test.desc, x, y, dst.At(x, y), golden.At(x, y)) - continue loop - } - } - } - } -} - -func TestDrawOverlap(t *testing.T) { - for _, op := range []Op{Over, Src} { - for yoff := -2; yoff <= 2; yoff++ { - loop: - for xoff := -2; xoff <= 2; xoff++ { - m := gradYellow(127).(*image.RGBA) - dst := &image.RGBA{ - Pix: m.Pix, - Stride: m.Stride, - Rect: image.Rect(5, 5, 10, 10), - } - src := &image.RGBA{ - Pix: m.Pix, - Stride: m.Stride, - Rect: image.Rect(5+xoff, 5+yoff, 10+xoff, 10+yoff), - } - // Draw the (src, mask, op) onto a copy of dst using a slow but obviously correct implementation. - golden := makeGolden(dst, src, nil, op) - b := dst.Bounds() - if !b.Eq(golden.Bounds()) { - t.Errorf("drawOverlap xoff=%d,yoff=%d: bounds %v versus %v", xoff, yoff, dst.Bounds(), golden.Bounds()) - continue - } - // Draw the same combination onto the actual dst using the optimized DrawMask implementation. - DrawMask(dst, b, src, src.Bounds().Min, nil, image.ZP, op) - // Check that the resultant dst image matches the golden output. - for y := b.Min.Y; y < b.Max.Y; y++ { - for x := b.Min.X; x < b.Max.X; x++ { - if !eq(dst.At(x, y), golden.At(x, y)) { - t.Errorf("drawOverlap xoff=%d,yoff=%d: at (%d, %d), %v versus golden %v", xoff, yoff, x, y, dst.At(x, y), golden.At(x, y)) - continue loop - } - } - } - } - } - } -} - -// TestIssue836 verifies http://code.google.com/p/go/issues/detail?id=836. -func TestIssue836(t *testing.T) { - a := image.NewRGBA(1, 1) - b := image.NewRGBA(2, 2) - b.Set(0, 0, image.RGBAColor{0, 0, 0, 5}) - b.Set(1, 0, image.RGBAColor{0, 0, 5, 5}) - b.Set(0, 1, image.RGBAColor{0, 5, 0, 5}) - b.Set(1, 1, image.RGBAColor{5, 0, 0, 5}) - Draw(a, image.Rect(0, 0, 1, 1), b, image.Pt(1, 1)) - if !eq(image.RGBAColor{5, 0, 0, 5}, a.At(0, 0)) { - t.Errorf("Issue 836: want %v got %v", image.RGBAColor{5, 0, 0, 5}, a.At(0, 0)) - } -} diff --git a/src/pkg/exp/draw/event.go b/src/pkg/exp/draw/event.go deleted file mode 100644 index b777d912e..000000000 --- a/src/pkg/exp/draw/event.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package draw - -import ( - "image" - "os" -) - -// A Window represents a single graphics window. -type Window interface { - // Screen returns an editable Image for the window. - Screen() Image - // FlushImage flushes changes made to Screen() back to screen. - FlushImage() - // EventChan returns a channel carrying UI events such as key presses, - // mouse movements and window resizes. - EventChan() <-chan interface{} - // Close closes the window. - Close() os.Error -} - -// A KeyEvent is sent for a key press or release. -type KeyEvent struct { - // The value k represents key k being pressed. - // The value -k represents key k being released. - // The specific set of key values is not specified, - // but ordinary characters represent themselves. - Key int -} - -// A MouseEvent is sent for a button press or release or for a mouse movement. -type MouseEvent struct { - // Buttons is a bit mask of buttons: 1<<0 is left, 1<<1 middle, 1<<2 right. - // It represents button state and not necessarily the state delta: bit 0 - // being on means that the left mouse button is down, but does not imply - // that the same button was up in the previous MouseEvent. - Buttons int - // Loc is the location of the cursor. - Loc image.Point - // Nsec is the event's timestamp. - Nsec int64 -} - -// A ConfigEvent is sent each time the window's color model or size changes. -// The client should respond by calling Window.Screen to obtain a new image. -type ConfigEvent struct { - Config image.Config -} - -// An ErrEvent is sent when an error occurs. -type ErrEvent struct { - Err os.Error -} diff --git a/src/pkg/exp/draw/x11/Makefile b/src/pkg/exp/draw/x11/Makefile deleted file mode 100644 index 205b3a65b..000000000 --- a/src/pkg/exp/draw/x11/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -# Copyright 2009 The Go Authors. All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. - -include ../../../../Make.inc - -TARG=exp/draw/x11 -GOFILES=\ - auth.go\ - conn.go\ - -include ../../../../Make.pkg diff --git a/src/pkg/exp/draw/x11/auth.go b/src/pkg/exp/draw/x11/auth.go deleted file mode 100644 index d48936ac1..000000000 --- a/src/pkg/exp/draw/x11/auth.go +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package x11 - -import ( - "bufio" - "io" - "os" -) - -// readU16BE reads a big-endian uint16 from r, using b as a scratch buffer. -func readU16BE(r io.Reader, b []byte) (uint16, os.Error) { - _, err := io.ReadFull(r, b[0:2]) - if err != nil { - return 0, err - } - return uint16(b[0])<<8 + uint16(b[1]), nil -} - -// readStr reads a length-prefixed string from r, using b as a scratch buffer. -func readStr(r io.Reader, b []byte) (string, os.Error) { - n, err := readU16BE(r, b) - if err != nil { - return "", err - } - if int(n) > len(b) { - return "", os.NewError("Xauthority entry too long for buffer") - } - _, err = io.ReadFull(r, b[0:n]) - if err != nil { - return "", err - } - return string(b[0:n]), nil -} - -// readAuth reads the X authority file and returns the name/data pair for the display. -// displayStr is the "12" out of a $DISPLAY like ":12.0". -func readAuth(displayStr string) (name, data string, err os.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). - var b [256]byte - // As per /usr/include/X11/Xauth.h. - const familyLocal = 256 - - fn := os.Getenv("XAUTHORITY") - if fn == "" { - home := os.Getenv("HOME") - if home == "" { - err = os.NewError("Xauthority not found: $XAUTHORITY, $HOME not set") - return - } - fn = home + "/.Xauthority" - } - r, err := os.Open(fn) - if err != nil { - return - } - defer r.Close() - br := bufio.NewReader(r) - - hostname, err := os.Hostname() - if err != nil { - return - } - for { - family, err := readU16BE(br, b[0:2]) - if err != nil { - return - } - addr, err := readStr(br, b[0:]) - if err != nil { - return - } - disp, err := readStr(br, b[0:]) - if err != nil { - return - } - name0, err := readStr(br, b[0:]) - if err != nil { - return - } - data0, err := readStr(br, b[0:]) - if err != nil { - return - } - if family == familyLocal && addr == hostname && disp == displayStr { - return name0, data0, nil - } - } - panic("unreachable") -} diff --git a/src/pkg/exp/draw/x11/conn.go b/src/pkg/exp/draw/x11/conn.go deleted file mode 100644 index 81c67267d..000000000 --- a/src/pkg/exp/draw/x11/conn.go +++ /dev/null @@ -1,625 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package x11 implements an X11 backend for the exp/draw package. -// -// The X protocol specification is at ftp://ftp.x.org/pub/X11R7.0/doc/PDF/proto.pdf. -// A summary of the wire format can be found in XCB's xproto.xml. -package x11 - -import ( - "bufio" - "exp/draw" - "image" - "io" - "log" - "net" - "os" - "strconv" - "strings" - "time" -) - -type resID uint32 // X resource IDs. - -// TODO(nigeltao): Handle window resizes. -const ( - windowHeight = 600 - windowWidth = 800 -) - -const ( - keymapLo = 8 - keymapHi = 255 -) - -type conn struct { - c io.Closer - r *bufio.Reader - w *bufio.Writer - - gc, window, root, visual resID - - img *image.RGBA - eventc chan interface{} - mouseState draw.MouseEvent - - buf [256]byte // General purpose scratch buffer. - - flush chan bool - flushBuf0 [24]byte - flushBuf1 [4 * 1024]byte -} - -// writeSocket runs in its own goroutine, serving both FlushImage calls -// directly from the exp/draw client and indirectly from X expose events. -// It paints c.img to the X server via PutImage requests. -func (c *conn) writeSocket() { - defer c.c.Close() - for _ = range c.flush { - b := c.img.Bounds() - if b.Empty() { - continue - } - // Each X request has a 16-bit length (in terms of 4-byte units). To avoid going over - // this limit, we send PutImage for each row of the image, rather than trying to paint - // the entire image in one X request. This approach could easily be optimized (or the - // X protocol may have an escape sequence to delimit very large requests). - // TODO(nigeltao): See what XCB's xcb_put_image does in this situation. - units := 6 + b.Dx() - if units > 0xffff || b.Dy() > 0xffff { - log.Print("x11: window is too large for PutImage") - return - } - - c.flushBuf0[0] = 0x48 // PutImage opcode. - c.flushBuf0[1] = 0x02 // XCB_IMAGE_FORMAT_Z_PIXMAP. - c.flushBuf0[2] = uint8(units) - c.flushBuf0[3] = uint8(units >> 8) - setU32LE(c.flushBuf0[4:8], uint32(c.window)) - setU32LE(c.flushBuf0[8:12], uint32(c.gc)) - setU32LE(c.flushBuf0[12:16], 1<<16|uint32(b.Dx())) - c.flushBuf0[21] = 0x18 // depth = 24 bits. - - for y := b.Min.Y; y < b.Max.Y; y++ { - setU32LE(c.flushBuf0[16:20], uint32(y<<16)) - if _, err := c.w.Write(c.flushBuf0[0:24]); err != nil { - if err != os.EOF { - log.Println("x11:", err.String()) - } - return - } - p := c.img.Pix[y*c.img.Stride : (y+1)*c.img.Stride] - for x := b.Min.X; x < b.Max.X; { - nx := b.Max.X - x - if nx > len(c.flushBuf1)/4 { - nx = len(c.flushBuf1) / 4 - } - for i, rgba := range p[x : x+nx] { - c.flushBuf1[4*i+0] = rgba.B - c.flushBuf1[4*i+1] = rgba.G - c.flushBuf1[4*i+2] = rgba.R - } - x += nx - if _, err := c.w.Write(c.flushBuf1[0 : 4*nx]); err != nil { - if err != os.EOF { - log.Println("x11:", err.String()) - } - return - } - } - } - if err := c.w.Flush(); err != nil { - if err != os.EOF { - log.Println("x11:", err.String()) - } - return - } - } -} - -func (c *conn) Screen() draw.Image { return c.img } - -func (c *conn) FlushImage() { - select { - case c.flush <- false: - // Flush notification sent. - default: - // Could not send. - // Flush notification must be pending already. - } -} - -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 } - -// readSocket runs in its own goroutine, reading X events and sending draw -// events on c's EventChan. -func (c *conn) readSocket() { - var ( - keymap [256][]int - keysymsPerKeycode int - ) - defer close(c.eventc) - for { - // X events are always 32 bytes long. - if _, err := io.ReadFull(c.r, c.buf[0:32]); err != nil { - if err != os.EOF { - c.eventc <- draw.ErrEvent{err} - } - return - } - switch c.buf[0] { - case 0x01: // Reply from a request (e.g. GetKeyboardMapping). - cookie := int(c.buf[3])<<8 | int(c.buf[2]) - if cookie != 1 { - // We issued only one request (GetKeyboardMapping) with a cookie of 1, - // so we shouldn't get any other reply from the X server. - c.eventc <- draw.ErrEvent{os.NewError("x11: unexpected cookie")} - return - } - keysymsPerKeycode = int(c.buf[1]) - b := make([]int, 256*keysymsPerKeycode) - for i := range keymap { - keymap[i] = b[i*keysymsPerKeycode : (i+1)*keysymsPerKeycode] - } - for i := keymapLo; i <= keymapHi; i++ { - m := keymap[i] - for j := range m { - u, err := readU32LE(c.r, c.buf[0:4]) - if err != nil { - if err != os.EOF { - c.eventc <- draw.ErrEvent{err} - } - return - } - m[j] = int(u) - } - } - case 0x02, 0x03: // Key press, key release. - // X Keyboard Encoding is documented at http://tronche.com/gui/x/xlib/input/keyboard-encoding.html - // TODO(nigeltao): Do we need to implement the "MODE SWITCH / group modifier" feature - // or is that some no-longer-used X construct? - if keysymsPerKeycode < 2 { - // Either we haven't yet received the GetKeyboardMapping reply or - // the X server has sent one that's too short. - continue - } - keycode := int(c.buf[1]) - shift := int(c.buf[28]) & 0x01 - keysym := keymap[keycode][shift] - if keysym == 0 { - keysym = keymap[keycode][0] - } - // 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? - // 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.Window interface? - if c.buf[0] == 0x03 { - keysym = -keysym - } - c.eventc <- draw.KeyEvent{keysym} - case 0x04, 0x05: // Button press, button release. - mask := 1 << (c.buf[1] - 1) - if c.buf[0] == 0x04 { - c.mouseState.Buttons |= mask - } else { - c.mouseState.Buttons &^= mask - } - c.mouseState.Nsec = time.Nanoseconds() - c.eventc <- c.mouseState - case 0x06: // Motion notify. - c.mouseState.Loc.X = int(int16(c.buf[25])<<8 | int16(c.buf[24])) - c.mouseState.Loc.Y = int(int16(c.buf[27])<<8 | int16(c.buf[26])) - c.mouseState.Nsec = time.Nanoseconds() - c.eventc <- c.mouseState - case 0x0c: // Expose. - // A single user action could trigger multiple expose events (e.g. if moving another - // window with XShape'd rounded corners over our window). In that case, the X server will - // send a uint16 count (in bytes 16-17) of the number of additional expose events coming. - // We could parse each event for the (x, y, width, height) and maintain a minimal dirty - // rectangle, but for now, the simplest approach is to paint the entire window, when - // receiving the final event in the series. - if c.buf[17] == 0 && c.buf[16] == 0 { - // TODO(nigeltao): Should we ignore the very first expose event? A freshly mapped window - // will trigger expose, but until the first c.FlushImage call, there's probably nothing to - // paint but black. For an 800x600 window, at 4 bytes per pixel, each repaint writes about - // 2MB over the socket. - c.FlushImage() - } - // TODO(nigeltao): Should we listen to DestroyNotify (0x11) and ResizeRequest (0x19) events? - // What about EnterNotify (0x07) and LeaveNotify (0x08)? - } - } -} - -// connect connects to the X server given by the full X11 display name (e.g. -// ":12.0") and returns the connection as well as the portion of the full name -// that is the display number (e.g. "12"). -// Examples: -// connect(":1") // calls net.Dial("unix", "", "/tmp/.X11-unix/X1"), displayStr="1" -// connect("/tmp/launch-123/:0") // calls net.Dial("unix", "", "/tmp/launch-123/:0"), displayStr="0" -// connect("hostname:2.1") // calls net.Dial("tcp", "", "hostname:6002"), displayStr="2" -// connect("tcp/hostname:1.0") // calls net.Dial("tcp", "", "hostname:6001"), displayStr="1" -func connect(display string) (conn net.Conn, displayStr string, err os.Error) { - colonIdx := strings.LastIndex(display, ":") - if colonIdx < 0 { - return nil, "", os.NewError("bad display: " + display) - } - // Parse the section before the colon. - var protocol, host, socket string - if display[0] == '/' { - socket = display[0:colonIdx] - } else { - if i := strings.LastIndex(display, "/"); i < 0 { - // The default protocol is TCP. - protocol = "tcp" - host = display[0:colonIdx] - } else { - protocol = display[0:i] - host = display[i+1 : colonIdx] - } - } - // Parse the section after the colon. - after := display[colonIdx+1:] - if after == "" { - return nil, "", os.NewError("bad display: " + display) - } - if i := strings.LastIndex(after, "."); i < 0 { - displayStr = after - } else { - displayStr = after[0:i] - } - displayInt, err := strconv.Atoi(displayStr) - if err != nil || displayInt < 0 { - return nil, "", os.NewError("bad display: " + display) - } - // Make the connection. - if socket != "" { - conn, err = net.Dial("unix", socket+":"+displayStr) - } else if host != "" { - conn, err = net.Dial(protocol, host+":"+strconv.Itoa(6000+displayInt)) - } else { - conn, err = net.Dial("unix", "/tmp/.X11-unix/X"+displayStr) - } - if err != nil { - return nil, "", os.NewError("cannot connect to " + display + ": " + err.String()) - } - return -} - -// authenticate authenticates ourselves with the X server. -// displayStr is the "12" out of ":12.0". -func authenticate(w *bufio.Writer, displayStr string) os.Error { - key, value, err := readAuth(displayStr) - if err != nil { - return err - } - // Assume that the authentication protocol is "MIT-MAGIC-COOKIE-1". - if len(key) != 18 || len(value) != 16 { - return os.NewError("unsupported Xauth") - } - // 0x006c means little-endian. 0x000b, 0x0000 means X major version 11, minor version 0. - // 0x0012 and 0x0010 means the auth key and value have lenths 18 and 16. - // The final 0x0000 is padding, so that the string length is a multiple of 4. - _, err = io.WriteString(w, "\x6c\x00\x0b\x00\x00\x00\x12\x00\x10\x00\x00\x00") - if err != nil { - return err - } - _, err = io.WriteString(w, key) - if err != nil { - return err - } - // Again, the 0x0000 is padding. - _, err = io.WriteString(w, "\x00\x00") - if err != nil { - return err - } - _, err = io.WriteString(w, value) - if err != nil { - return err - } - err = w.Flush() - if err != nil { - return err - } - return nil -} - -// readU8 reads a uint8 from r, using b as a scratch buffer. -func readU8(r io.Reader, b []byte) (uint8, os.Error) { - _, err := io.ReadFull(r, b[0:1]) - if err != nil { - return 0, err - } - return uint8(b[0]), nil -} - -// readU16LE reads a little-endian uint16 from r, using b as a scratch buffer. -func readU16LE(r io.Reader, b []byte) (uint16, os.Error) { - _, err := io.ReadFull(r, b[0:2]) - if err != nil { - return 0, err - } - return uint16(b[0]) | uint16(b[1])<<8, nil -} - -// readU32LE reads a little-endian uint32 from r, using b as a scratch buffer. -func readU32LE(r io.Reader, b []byte) (uint32, os.Error) { - _, err := io.ReadFull(r, b[0:4]) - if err != nil { - return 0, err - } - return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24, nil -} - -// setU32LE sets b[0:4] to be the little-endian representation of u. -func setU32LE(b []byte, u uint32) { - b[0] = byte((u >> 0) & 0xff) - b[1] = byte((u >> 8) & 0xff) - b[2] = byte((u >> 16) & 0xff) - b[3] = byte((u >> 24) & 0xff) -} - -// checkPixmapFormats checks that we have an agreeable X pixmap Format. -func checkPixmapFormats(r io.Reader, b []byte, n int) (agree bool, err os.Error) { - for i := 0; i < n; i++ { - _, err = io.ReadFull(r, b[0:8]) - if err != nil { - return - } - // Byte 0 is depth, byte 1 is bits-per-pixel, byte 2 is scanline-pad, the rest (5) is padding. - if b[0] == 24 && b[1] == 32 { - agree = true - } - } - return -} - -// checkDepths checks that we have an agreeable X Depth (i.e. one that has an agreeable X VisualType). -func checkDepths(r io.Reader, b []byte, n int, visual uint32) (agree bool, err os.Error) { - for i := 0; i < n; i++ { - depth, err := readU16LE(r, b) - if err != nil { - return - } - depth &= 0xff - visualsLen, err := readU16LE(r, b) - if err != nil { - return - } - // Ignore 4 bytes of padding. - _, err = io.ReadFull(r, b[0:4]) - if err != nil { - return - } - for j := 0; j < int(visualsLen); j++ { - // Read 24 bytes: visual(4), class(1), bits per rgb value(1), colormap entries(2), - // red mask(4), green mask(4), blue mask(4), padding(4). - v, err := readU32LE(r, b) - _, err = readU32LE(r, b) - rm, err := readU32LE(r, b) - gm, err := readU32LE(r, b) - bm, err := readU32LE(r, b) - _, err = readU32LE(r, b) - if err != nil { - return - } - if v == visual && rm == 0xff0000 && gm == 0xff00 && bm == 0xff && depth == 24 { - agree = true - } - } - } - return -} - -// checkScreens checks that we have an agreeable X Screen. -func checkScreens(r io.Reader, b []byte, n int) (root, visual uint32, err os.Error) { - for i := 0; i < n; i++ { - root0, err := readU32LE(r, b) - if err != nil { - return - } - // Ignore the next 7x4 bytes, which is: colormap, whitepixel, blackpixel, current input masks, - // width and height (pixels), width and height (mm), min and max installed maps. - _, err = io.ReadFull(r, b[0:28]) - if err != nil { - return - } - visual0, err := readU32LE(r, b) - if err != nil { - return - } - // Next 4 bytes: backing stores, save unders, root depth, allowed depths length. - x, err := readU32LE(r, b) - if err != nil { - return - } - nDepths := int(x >> 24) - agree, err := checkDepths(r, b, nDepths, visual0) - if err != nil { - return - } - if agree && root == 0 { - root = root0 - visual = visual0 - } - } - return -} - -// handshake performs the protocol handshake with the X server, and ensures -// that the server provides a compatible Screen, Depth, etc. -func (c *conn) handshake() os.Error { - _, err := io.ReadFull(c.r, c.buf[0:8]) - if err != nil { - return err - } - // Byte 0:1 should be 1 (success), bytes 2:6 should be 0xb0000000 (major/minor version 11.0). - if c.buf[0] != 1 || c.buf[2] != 11 || c.buf[3] != 0 || c.buf[4] != 0 || c.buf[5] != 0 { - return os.NewError("unsupported X version") - } - // Ignore the release number. - _, err = io.ReadFull(c.r, c.buf[0:4]) - if err != nil { - return err - } - // Read the resource ID base. - resourceIdBase, err := readU32LE(c.r, c.buf[0:4]) - if err != nil { - return err - } - // Read the resource ID mask. - resourceIdMask, err := readU32LE(c.r, c.buf[0:4]) - if err != nil { - return err - } - if resourceIdMask < 256 { - return os.NewError("X resource ID mask is too small") - } - // Ignore the motion buffer size. - _, err = io.ReadFull(c.r, c.buf[0:4]) - if err != nil { - return err - } - // Read the vendor length and round it up to a multiple of 4, - // for X11 protocol alignment reasons. - vendorLen, err := readU16LE(c.r, c.buf[0:2]) - if err != nil { - return err - } - vendorLen = (vendorLen + 3) &^ 3 - // Read the maximum request length. - maxReqLen, err := readU16LE(c.r, c.buf[0:2]) - if err != nil { - return err - } - if maxReqLen != 0xffff { - return os.NewError("unsupported X maximum request length") - } - // Read the roots length. - rootsLen, err := readU8(c.r, c.buf[0:1]) - if err != nil { - return err - } - // Read the pixmap formats length. - pixmapFormatsLen, err := readU8(c.r, c.buf[0:1]) - if err != nil { - return err - } - // Ignore some things that we don't care about (totalling 10 + vendorLen bytes): - // imageByteOrder(1), bitmapFormatBitOrder(1), bitmapFormatScanlineUnit(1) bitmapFormatScanlinePad(1), - // minKeycode(1), maxKeycode(1), padding(4), vendor (vendorLen). - if 10+int(vendorLen) > cap(c.buf) { - return os.NewError("unsupported X vendor") - } - _, err = io.ReadFull(c.r, c.buf[0:10+int(vendorLen)]) - if err != nil { - return err - } - // Check that we have an agreeable pixmap format. - agree, err := checkPixmapFormats(c.r, c.buf[0:8], int(pixmapFormatsLen)) - if err != nil { - return err - } - if !agree { - return os.NewError("unsupported X pixmap formats") - } - // Check that we have an agreeable screen. - root, visual, err := checkScreens(c.r, c.buf[0:24], int(rootsLen)) - if err != nil { - return err - } - if root == 0 || visual == 0 { - return os.NewError("unsupported X screen") - } - c.gc = resID(resourceIdBase) - c.window = resID(resourceIdBase + 1) - c.root = resID(root) - c.visual = resID(visual) - return nil -} - -// NewWindow calls NewWindowDisplay with $DISPLAY. -func NewWindow() (draw.Window, os.Error) { - display := os.Getenv("DISPLAY") - if len(display) == 0 { - return nil, os.NewError("$DISPLAY not set") - } - return NewWindowDisplay(display) -} - -// 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 -// string, such as ":1". -func NewWindowDisplay(display string) (draw.Window, os.Error) { - socket, displayStr, err := connect(display) - if err != nil { - return nil, err - } - c := new(conn) - c.c = socket - c.r = bufio.NewReader(socket) - c.w = bufio.NewWriter(socket) - err = authenticate(c.w, displayStr) - if err != nil { - return nil, err - } - err = c.handshake() - if err != nil { - return nil, err - } - - // Now that we're connected, show a window, via three X protocol messages. - // First, issue a GetKeyboardMapping request. This is the first request, and - // will be associated with a cookie of 1. - setU32LE(c.buf[0:4], 0x00020065) // 0x65 is the GetKeyboardMapping opcode, and the message is 2 x 4 bytes long. - setU32LE(c.buf[4:8], uint32((keymapHi-keymapLo+1)<<8|keymapLo)) - // Second, create a graphics context (GC). - setU32LE(c.buf[8:12], 0x00060037) // 0x37 is the CreateGC opcode, and the message is 6 x 4 bytes long. - setU32LE(c.buf[12:16], uint32(c.gc)) - setU32LE(c.buf[16:20], uint32(c.root)) - setU32LE(c.buf[20:24], 0x00010004) // Bit 2 is XCB_GC_FOREGROUND, bit 16 is XCB_GC_GRAPHICS_EXPOSURES. - setU32LE(c.buf[24:28], 0x00000000) // The Foreground is black. - setU32LE(c.buf[28:32], 0x00000000) // GraphicsExposures' value is unused. - // Third, create the window. - setU32LE(c.buf[32:36], 0x000a0001) // 0x01 is the CreateWindow opcode, and the message is 10 x 4 bytes long. - setU32LE(c.buf[36:40], uint32(c.window)) - setU32LE(c.buf[40:44], uint32(c.root)) - setU32LE(c.buf[44:48], 0x00000000) // Initial (x, y) is (0, 0). - setU32LE(c.buf[48:52], windowHeight<<16|windowWidth) - setU32LE(c.buf[52:56], 0x00010000) // Border width is 0, XCB_WINDOW_CLASS_INPUT_OUTPUT is 1. - setU32LE(c.buf[56:60], uint32(c.visual)) - setU32LE(c.buf[60:64], 0x00000802) // Bit 1 is XCB_CW_BACK_PIXEL, bit 11 is XCB_CW_EVENT_MASK. - setU32LE(c.buf[64:68], 0x00000000) // The Back-Pixel is black. - setU32LE(c.buf[68:72], 0x0000804f) // Key/button press and release, pointer motion, and expose event masks. - // Fourth, map the window. - setU32LE(c.buf[72:76], 0x00020008) // 0x08 is the MapWindow opcode, and the message is 2 x 4 bytes long. - setU32LE(c.buf[76:80], uint32(c.window)) - // Write the bytes. - _, err = c.w.Write(c.buf[0:80]) - if err != nil { - return nil, err - } - err = c.w.Flush() - if err != nil { - return nil, err - } - - c.img = image.NewRGBA(windowWidth, windowHeight) - c.eventc = make(chan interface{}, 16) - c.flush = make(chan bool, 1) - go c.readSocket() - go c.writeSocket() - return c, nil -} |