1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package x11 implements an X11 backend for the exp/gui package.
7 // The X protocol specification is at ftp://ftp.x.org/pub/X11R7.0/doc/PDF/proto.pdf.
8 // A summary of the wire format can be found in XCB's xproto.xml.
26 type resID uint32 // X resource IDs.
28 // TODO(nigeltao): Handle window resizes.
44 gc, window, root, visual resID
47 eventc chan interface{}
48 mouseState gui.MouseEvent
50 buf [256]byte // General purpose scratch buffer.
54 flushBuf1 [4 * 1024]byte
57 // writeSocket runs in its own goroutine, serving both FlushImage calls
58 // directly from the exp/gui client and indirectly from X expose events.
59 // It paints c.img to the X server via PutImage requests.
60 func (c *conn) writeSocket() {
62 for _ = range c.flush {
67 // Each X request has a 16-bit length (in terms of 4-byte units). To avoid going over
68 // this limit, we send PutImage for each row of the image, rather than trying to paint
69 // the entire image in one X request. This approach could easily be optimized (or the
70 // X protocol may have an escape sequence to delimit very large requests).
71 // TODO(nigeltao): See what XCB's xcb_put_image does in this situation.
73 if units > 0xffff || b.Dy() > 0xffff {
74 log.Print("x11: window is too large for PutImage")
78 c.flushBuf0[0] = 0x48 // PutImage opcode.
79 c.flushBuf0[1] = 0x02 // XCB_IMAGE_FORMAT_Z_PIXMAP.
80 c.flushBuf0[2] = uint8(units)
81 c.flushBuf0[3] = uint8(units >> 8)
82 setU32LE(c.flushBuf0[4:8], uint32(c.window))
83 setU32LE(c.flushBuf0[8:12], uint32(c.gc))
84 setU32LE(c.flushBuf0[12:16], 1<<16|uint32(b.Dx()))
85 c.flushBuf0[21] = 0x18 // depth = 24 bits.
87 for y := b.Min.Y; y < b.Max.Y; y++ {
88 setU32LE(c.flushBuf0[16:20], uint32(y<<16))
89 if _, err := c.w.Write(c.flushBuf0[:24]); err != nil {
91 log.Println("x11:", err)
95 p := c.img.Pix[(y-b.Min.Y)*c.img.Stride:]
96 for x, dx := 0, 4*b.Dx(); x < dx; {
98 if nx > len(c.flushBuf1) {
99 nx = len(c.flushBuf1) &^ 3
101 for i := 0; i < nx; i += 4 {
102 // X11's order is BGRX, not RGBA.
103 c.flushBuf1[i+0] = p[x+i+2]
104 c.flushBuf1[i+1] = p[x+i+1]
105 c.flushBuf1[i+2] = p[x+i+0]
108 if _, err := c.w.Write(c.flushBuf1[:nx]); err != nil {
110 log.Println("x11:", err)
116 if err := c.w.Flush(); err != nil {
118 log.Println("x11:", err)
125 func (c *conn) Screen() draw.Image { return c.img }
127 func (c *conn) FlushImage() {
129 case c.flush <- false:
130 // Flush notification sent.
133 // Flush notification must be pending already.
137 func (c *conn) Close() error {
138 // Shut down the writeSocket goroutine. This will close the socket to the
139 // X11 server, which will cause c.eventc to close.
141 for _ = range c.eventc {
142 // Drain the channel to allow the readSocket goroutine to shut down.
147 func (c *conn) EventChan() <-chan interface{} { return c.eventc }
149 // readSocket runs in its own goroutine, reading X events and sending gui
150 // events on c's EventChan.
151 func (c *conn) readSocket() {
154 keysymsPerKeycode int
156 defer close(c.eventc)
158 // X events are always 32 bytes long.
159 if _, err := io.ReadFull(c.r, c.buf[:32]); err != nil {
161 c.eventc <- gui.ErrEvent{err}
166 case 0x01: // Reply from a request (e.g. GetKeyboardMapping).
167 cookie := int(c.buf[3])<<8 | int(c.buf[2])
169 // We issued only one request (GetKeyboardMapping) with a cookie of 1,
170 // so we shouldn't get any other reply from the X server.
171 c.eventc <- gui.ErrEvent{errors.New("x11: unexpected cookie")}
174 keysymsPerKeycode = int(c.buf[1])
175 b := make([]int, 256*keysymsPerKeycode)
176 for i := range keymap {
177 keymap[i] = b[i*keysymsPerKeycode : (i+1)*keysymsPerKeycode]
179 for i := keymapLo; i <= keymapHi; i++ {
182 u, err := readU32LE(c.r, c.buf[:4])
185 c.eventc <- gui.ErrEvent{err}
192 case 0x02, 0x03: // Key press, key release.
193 // X Keyboard Encoding is documented at http://tronche.com/gui/x/xlib/input/keyboard-encoding.html
194 // TODO(nigeltao): Do we need to implement the "MODE SWITCH / group modifier" feature
195 // or is that some no-longer-used X construct?
196 if keysymsPerKeycode < 2 {
197 // Either we haven't yet received the GetKeyboardMapping reply or
198 // the X server has sent one that's too short.
201 keycode := int(c.buf[1])
202 shift := int(c.buf[28]) & 0x01
203 keysym := keymap[keycode][shift]
205 keysym = keymap[keycode][0]
207 // TODO(nigeltao): Should we send KeyEvents for Shift/Ctrl/Alt? Should Shift-A send
208 // the same int down the channel as the sent on just the A key?
209 // TODO(nigeltao): How should IME events (e.g. key presses that should generate CJK text) work? Or
210 // is that outside the scope of the gui.Window interface?
211 if c.buf[0] == 0x03 {
214 c.eventc <- gui.KeyEvent{keysym}
215 case 0x04, 0x05: // Button press, button release.
216 mask := 1 << (c.buf[1] - 1)
217 if c.buf[0] == 0x04 {
218 c.mouseState.Buttons |= mask
220 c.mouseState.Buttons &^= mask
222 c.mouseState.Nsec = time.Nanoseconds()
223 c.eventc <- c.mouseState
224 case 0x06: // Motion notify.
225 c.mouseState.Loc.X = int(int16(c.buf[25])<<8 | int16(c.buf[24]))
226 c.mouseState.Loc.Y = int(int16(c.buf[27])<<8 | int16(c.buf[26]))
227 c.mouseState.Nsec = time.Nanoseconds()
228 c.eventc <- c.mouseState
229 case 0x0c: // Expose.
230 // A single user action could trigger multiple expose events (e.g. if moving another
231 // window with XShape'd rounded corners over our window). In that case, the X server will
232 // send a uint16 count (in bytes 16-17) of the number of additional expose events coming.
233 // We could parse each event for the (x, y, width, height) and maintain a minimal dirty
234 // rectangle, but for now, the simplest approach is to paint the entire window, when
235 // receiving the final event in the series.
236 if c.buf[17] == 0 && c.buf[16] == 0 {
237 // TODO(nigeltao): Should we ignore the very first expose event? A freshly mapped window
238 // will trigger expose, but until the first c.FlushImage call, there's probably nothing to
239 // paint but black. For an 800x600 window, at 4 bytes per pixel, each repaint writes about
240 // 2MB over the socket.
243 // TODO(nigeltao): Should we listen to DestroyNotify (0x11) and ResizeRequest (0x19) events?
244 // What about EnterNotify (0x07) and LeaveNotify (0x08)?
249 // connect connects to the X server given by the full X11 display name (e.g.
250 // ":12.0") and returns the connection as well as the portion of the full name
251 // that is the display number (e.g. "12").
253 // connect(":1") // calls net.Dial("unix", "", "/tmp/.X11-unix/X1"), displayStr="1"
254 // connect("/tmp/launch-123/:0") // calls net.Dial("unix", "", "/tmp/launch-123/:0"), displayStr="0"
255 // connect("hostname:2.1") // calls net.Dial("tcp", "", "hostname:6002"), displayStr="2"
256 // connect("tcp/hostname:1.0") // calls net.Dial("tcp", "", "hostname:6001"), displayStr="1"
257 func connect(display string) (conn net.Conn, displayStr string, err error) {
258 colonIdx := strings.LastIndex(display, ":")
260 return nil, "", errors.New("bad display: " + display)
262 // Parse the section before the colon.
263 var protocol, host, socket string
264 if display[0] == '/' {
265 socket = display[:colonIdx]
267 if i := strings.LastIndex(display, "/"); i < 0 {
268 // The default protocol is TCP.
270 host = display[:colonIdx]
272 protocol = display[:i]
273 host = display[i+1 : colonIdx]
276 // Parse the section after the colon.
277 after := display[colonIdx+1:]
279 return nil, "", errors.New("bad display: " + display)
281 if i := strings.LastIndex(after, "."); i < 0 {
284 displayStr = after[:i]
286 displayInt, err := strconv.Atoi(displayStr)
287 if err != nil || displayInt < 0 {
288 return nil, "", errors.New("bad display: " + display)
290 // Make the connection.
292 conn, err = net.Dial("unix", socket+":"+displayStr)
293 } else if host != "" {
294 conn, err = net.Dial(protocol, host+":"+strconv.Itoa(6000+displayInt))
296 conn, err = net.Dial("unix", "/tmp/.X11-unix/X"+displayStr)
299 return nil, "", errors.New("cannot connect to " + display + ": " + err.Error())
304 // authenticate authenticates ourselves with the X server.
305 // displayStr is the "12" out of ":12.0".
306 func authenticate(w *bufio.Writer, displayStr string) error {
307 key, value, err := readAuth(displayStr)
311 // Assume that the authentication protocol is "MIT-MAGIC-COOKIE-1".
312 if len(key) != 18 || len(value) != 16 {
313 return errors.New("unsupported Xauth")
315 // 0x006c means little-endian. 0x000b, 0x0000 means X major version 11, minor version 0.
316 // 0x0012 and 0x0010 means the auth key and value have lengths 18 and 16.
317 // The final 0x0000 is padding, so that the string length is a multiple of 4.
318 _, err = io.WriteString(w, "\x6c\x00\x0b\x00\x00\x00\x12\x00\x10\x00\x00\x00")
322 _, err = io.WriteString(w, key)
326 // Again, the 0x0000 is padding.
327 _, err = io.WriteString(w, "\x00\x00")
331 _, err = io.WriteString(w, value)
342 // readU8 reads a uint8 from r, using b as a scratch buffer.
343 func readU8(r io.Reader, b []byte) (uint8, error) {
344 _, err := io.ReadFull(r, b[:1])
348 return uint8(b[0]), nil
351 // readU16LE reads a little-endian uint16 from r, using b as a scratch buffer.
352 func readU16LE(r io.Reader, b []byte) (uint16, error) {
353 _, err := io.ReadFull(r, b[:2])
357 return uint16(b[0]) | uint16(b[1])<<8, nil
360 // readU32LE reads a little-endian uint32 from r, using b as a scratch buffer.
361 func readU32LE(r io.Reader, b []byte) (uint32, error) {
362 _, err := io.ReadFull(r, b[:4])
366 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24, nil
369 // setU32LE sets b[:4] to be the little-endian representation of u.
370 func setU32LE(b []byte, u uint32) {
371 b[0] = byte((u >> 0) & 0xff)
372 b[1] = byte((u >> 8) & 0xff)
373 b[2] = byte((u >> 16) & 0xff)
374 b[3] = byte((u >> 24) & 0xff)
377 // checkPixmapFormats checks that we have an agreeable X pixmap Format.
378 func checkPixmapFormats(r io.Reader, b []byte, n int) (agree bool, err error) {
379 for i := 0; i < n; i++ {
380 _, err = io.ReadFull(r, b[:8])
384 // Byte 0 is depth, byte 1 is bits-per-pixel, byte 2 is scanline-pad, the rest (5) is padding.
385 if b[0] == 24 && b[1] == 32 {
392 // checkDepths checks that we have an agreeable X Depth (i.e. one that has an agreeable X VisualType).
393 func checkDepths(r io.Reader, b []byte, n int, visual uint32) (agree bool, err error) {
394 for i := 0; i < n; i++ {
395 var depth, visualsLen uint16
396 depth, err = readU16LE(r, b)
401 visualsLen, err = readU16LE(r, b)
405 // Ignore 4 bytes of padding.
406 _, err = io.ReadFull(r, b[:4])
410 for j := 0; j < int(visualsLen); j++ {
411 // Read 24 bytes: visual(4), class(1), bits per rgb value(1), colormap entries(2),
412 // red mask(4), green mask(4), blue mask(4), padding(4).
413 v, _ := readU32LE(r, b)
414 _, _ = readU32LE(r, b)
415 rm, _ := readU32LE(r, b)
416 gm, _ := readU32LE(r, b)
417 bm, _ := readU32LE(r, b)
418 _, err = readU32LE(r, b)
422 if v == visual && rm == 0xff0000 && gm == 0xff00 && bm == 0xff && depth == 24 {
430 // checkScreens checks that we have an agreeable X Screen.
431 func checkScreens(r io.Reader, b []byte, n int) (root, visual uint32, err error) {
432 for i := 0; i < n; i++ {
433 var root0, visual0, x uint32
434 root0, err = readU32LE(r, b)
438 // Ignore the next 7x4 bytes, which is: colormap, whitepixel, blackpixel, current input masks,
439 // width and height (pixels), width and height (mm), min and max installed maps.
440 _, err = io.ReadFull(r, b[:28])
444 visual0, err = readU32LE(r, b)
448 // Next 4 bytes: backing stores, save unders, root depth, allowed depths length.
449 x, err = readU32LE(r, b)
453 nDepths := int(x >> 24)
455 agree, err = checkDepths(r, b, nDepths, visual0)
459 if agree && root == 0 {
467 // handshake performs the protocol handshake with the X server, and ensures
468 // that the server provides a compatible Screen, Depth, etc.
469 func (c *conn) handshake() error {
470 _, err := io.ReadFull(c.r, c.buf[:8])
474 // Byte 0 should be 1 (success), bytes 2:6 should be 0xb0000000 (major/minor version 11.0).
475 if c.buf[0] != 1 || c.buf[2] != 11 || c.buf[3] != 0 || c.buf[4] != 0 || c.buf[5] != 0 {
476 return errors.New("unsupported X version")
478 // Ignore the release number.
479 _, err = io.ReadFull(c.r, c.buf[:4])
483 // Read the resource ID base.
484 resourceIdBase, err := readU32LE(c.r, c.buf[:4])
488 // Read the resource ID mask.
489 resourceIdMask, err := readU32LE(c.r, c.buf[:4])
493 if resourceIdMask < 256 {
494 return errors.New("X resource ID mask is too small")
496 // Ignore the motion buffer size.
497 _, err = io.ReadFull(c.r, c.buf[:4])
501 // Read the vendor length and round it up to a multiple of 4,
502 // for X11 protocol alignment reasons.
503 vendorLen, err := readU16LE(c.r, c.buf[:2])
507 vendorLen = (vendorLen + 3) &^ 3
508 // Read the maximum request length.
509 maxReqLen, err := readU16LE(c.r, c.buf[:2])
513 if maxReqLen != 0xffff {
514 return errors.New("unsupported X maximum request length")
516 // Read the roots length.
517 rootsLen, err := readU8(c.r, c.buf[:1])
521 // Read the pixmap formats length.
522 pixmapFormatsLen, err := readU8(c.r, c.buf[:1])
526 // Ignore some things that we don't care about (totaling 10 + vendorLen bytes):
527 // imageByteOrder(1), bitmapFormatBitOrder(1), bitmapFormatScanlineUnit(1) bitmapFormatScanlinePad(1),
528 // minKeycode(1), maxKeycode(1), padding(4), vendor (vendorLen).
529 if 10+int(vendorLen) > cap(c.buf) {
530 return errors.New("unsupported X vendor")
532 _, err = io.ReadFull(c.r, c.buf[:10+int(vendorLen)])
536 // Check that we have an agreeable pixmap format.
537 agree, err := checkPixmapFormats(c.r, c.buf[:8], int(pixmapFormatsLen))
542 return errors.New("unsupported X pixmap formats")
544 // Check that we have an agreeable screen.
545 root, visual, err := checkScreens(c.r, c.buf[:24], int(rootsLen))
549 if root == 0 || visual == 0 {
550 return errors.New("unsupported X screen")
552 c.gc = resID(resourceIdBase)
553 c.window = resID(resourceIdBase + 1)
555 c.visual = resID(visual)
559 // NewWindow calls NewWindowDisplay with $DISPLAY.
560 func NewWindow() (gui.Window, error) {
561 display := os.Getenv("DISPLAY")
562 if len(display) == 0 {
563 return nil, errors.New("$DISPLAY not set")
565 return NewWindowDisplay(display)
568 // NewWindowDisplay returns a new gui.Window, backed by a newly created and
569 // mapped X11 window. The X server to connect to is specified by the display
570 // string, such as ":1".
571 func NewWindowDisplay(display string) (gui.Window, error) {
572 socket, displayStr, err := connect(display)
578 c.r = bufio.NewReader(socket)
579 c.w = bufio.NewWriter(socket)
580 err = authenticate(c.w, displayStr)
589 // Now that we're connected, show a window, via three X protocol messages.
590 // First, issue a GetKeyboardMapping request. This is the first request, and
591 // will be associated with a cookie of 1.
592 setU32LE(c.buf[0:4], 0x00020065) // 0x65 is the GetKeyboardMapping opcode, and the message is 2 x 4 bytes long.
593 setU32LE(c.buf[4:8], uint32((keymapHi-keymapLo+1)<<8|keymapLo))
594 // Second, create a graphics context (GC).
595 setU32LE(c.buf[8:12], 0x00060037) // 0x37 is the CreateGC opcode, and the message is 6 x 4 bytes long.
596 setU32LE(c.buf[12:16], uint32(c.gc))
597 setU32LE(c.buf[16:20], uint32(c.root))
598 setU32LE(c.buf[20:24], 0x00010004) // Bit 2 is XCB_GC_FOREGROUND, bit 16 is XCB_GC_GRAPHICS_EXPOSURES.
599 setU32LE(c.buf[24:28], 0x00000000) // The Foreground is black.
600 setU32LE(c.buf[28:32], 0x00000000) // GraphicsExposures' value is unused.
601 // Third, create the window.
602 setU32LE(c.buf[32:36], 0x000a0001) // 0x01 is the CreateWindow opcode, and the message is 10 x 4 bytes long.
603 setU32LE(c.buf[36:40], uint32(c.window))
604 setU32LE(c.buf[40:44], uint32(c.root))
605 setU32LE(c.buf[44:48], 0x00000000) // Initial (x, y) is (0, 0).
606 setU32LE(c.buf[48:52], windowHeight<<16|windowWidth)
607 setU32LE(c.buf[52:56], 0x00010000) // Border width is 0, XCB_WINDOW_CLASS_INPUT_OUTPUT is 1.
608 setU32LE(c.buf[56:60], uint32(c.visual))
609 setU32LE(c.buf[60:64], 0x00000802) // Bit 1 is XCB_CW_BACK_PIXEL, bit 11 is XCB_CW_EVENT_MASK.
610 setU32LE(c.buf[64:68], 0x00000000) // The Back-Pixel is black.
611 setU32LE(c.buf[68:72], 0x0000804f) // Key/button press and release, pointer motion, and expose event masks.
612 // Fourth, map the window.
613 setU32LE(c.buf[72:76], 0x00020008) // 0x08 is the MapWindow opcode, and the message is 2 x 4 bytes long.
614 setU32LE(c.buf[76:80], uint32(c.window))
616 _, err = c.w.Write(c.buf[:80])
625 c.img = image.NewRGBA(image.Rect(0, 0, windowWidth, windowHeight))
626 c.eventc = make(chan interface{}, 16)
627 c.flush = make(chan bool, 1)