OSDN Git Service

libgo: Update to weekly.2011-11-01.
[pf3gnuchains/gcc-fork.git] / libgo / go / exp / gui / x11 / conn.go
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.
4
5 // Package x11 implements an X11 backend for the exp/gui package.
6 //
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.
9 package x11
10
11 import (
12         "bufio"
13         "exp/gui"
14         "image"
15         "image/draw"
16         "io"
17         "log"
18         "net"
19         "os"
20         "strconv"
21         "strings"
22         "time"
23 )
24
25 type resID uint32 // X resource IDs.
26
27 // TODO(nigeltao): Handle window resizes.
28 const (
29         windowHeight = 600
30         windowWidth  = 800
31 )
32
33 const (
34         keymapLo = 8
35         keymapHi = 255
36 )
37
38 type conn struct {
39         c io.Closer
40         r *bufio.Reader
41         w *bufio.Writer
42
43         gc, window, root, visual resID
44
45         img        *image.RGBA
46         eventc     chan interface{}
47         mouseState gui.MouseEvent
48
49         buf [256]byte // General purpose scratch buffer.
50
51         flush     chan bool
52         flushBuf0 [24]byte
53         flushBuf1 [4 * 1024]byte
54 }
55
56 // writeSocket runs in its own goroutine, serving both FlushImage calls
57 // directly from the exp/gui client and indirectly from X expose events.
58 // It paints c.img to the X server via PutImage requests.
59 func (c *conn) writeSocket() {
60         defer c.c.Close()
61         for _ = range c.flush {
62                 b := c.img.Bounds()
63                 if b.Empty() {
64                         continue
65                 }
66                 // Each X request has a 16-bit length (in terms of 4-byte units). To avoid going over
67                 // this limit, we send PutImage for each row of the image, rather than trying to paint
68                 // the entire image in one X request. This approach could easily be optimized (or the
69                 // X protocol may have an escape sequence to delimit very large requests).
70                 // TODO(nigeltao): See what XCB's xcb_put_image does in this situation.
71                 units := 6 + b.Dx()
72                 if units > 0xffff || b.Dy() > 0xffff {
73                         log.Print("x11: window is too large for PutImage")
74                         return
75                 }
76
77                 c.flushBuf0[0] = 0x48 // PutImage opcode.
78                 c.flushBuf0[1] = 0x02 // XCB_IMAGE_FORMAT_Z_PIXMAP.
79                 c.flushBuf0[2] = uint8(units)
80                 c.flushBuf0[3] = uint8(units >> 8)
81                 setU32LE(c.flushBuf0[4:8], uint32(c.window))
82                 setU32LE(c.flushBuf0[8:12], uint32(c.gc))
83                 setU32LE(c.flushBuf0[12:16], 1<<16|uint32(b.Dx()))
84                 c.flushBuf0[21] = 0x18 // depth = 24 bits.
85
86                 for y := b.Min.Y; y < b.Max.Y; y++ {
87                         setU32LE(c.flushBuf0[16:20], uint32(y<<16))
88                         if _, err := c.w.Write(c.flushBuf0[:24]); err != nil {
89                                 if err != os.EOF {
90                                         log.Println("x11:", err)
91                                 }
92                                 return
93                         }
94                         p := c.img.Pix[(y-b.Min.Y)*c.img.Stride:]
95                         for x, dx := 0, 4*b.Dx(); x < dx; {
96                                 nx := dx - x
97                                 if nx > len(c.flushBuf1) {
98                                         nx = len(c.flushBuf1) &^ 3
99                                 }
100                                 for i := 0; i < nx; i += 4 {
101                                         // X11's order is BGRX, not RGBA.
102                                         c.flushBuf1[i+0] = p[x+i+2]
103                                         c.flushBuf1[i+1] = p[x+i+1]
104                                         c.flushBuf1[i+2] = p[x+i+0]
105                                 }
106                                 x += nx
107                                 if _, err := c.w.Write(c.flushBuf1[:nx]); err != nil {
108                                         if err != os.EOF {
109                                                 log.Println("x11:", err)
110                                         }
111                                         return
112                                 }
113                         }
114                 }
115                 if err := c.w.Flush(); err != nil {
116                         if err != os.EOF {
117                                 log.Println("x11:", err)
118                         }
119                         return
120                 }
121         }
122 }
123
124 func (c *conn) Screen() draw.Image { return c.img }
125
126 func (c *conn) FlushImage() {
127         select {
128         case c.flush <- false:
129                 // Flush notification sent.
130         default:
131                 // Could not send.
132                 // Flush notification must be pending already.
133         }
134 }
135
136 func (c *conn) Close() os.Error {
137         // Shut down the writeSocket goroutine. This will close the socket to the
138         // X11 server, which will cause c.eventc to close.
139         close(c.flush)
140         for _ = range c.eventc {
141                 // Drain the channel to allow the readSocket goroutine to shut down.
142         }
143         return nil
144 }
145
146 func (c *conn) EventChan() <-chan interface{} { return c.eventc }
147
148 // readSocket runs in its own goroutine, reading X events and sending gui
149 // events on c's EventChan.
150 func (c *conn) readSocket() {
151         var (
152                 keymap            [256][]int
153                 keysymsPerKeycode int
154         )
155         defer close(c.eventc)
156         for {
157                 // X events are always 32 bytes long.
158                 if _, err := io.ReadFull(c.r, c.buf[:32]); err != nil {
159                         if err != os.EOF {
160                                 c.eventc <- gui.ErrEvent{err}
161                         }
162                         return
163                 }
164                 switch c.buf[0] {
165                 case 0x01: // Reply from a request (e.g. GetKeyboardMapping).
166                         cookie := int(c.buf[3])<<8 | int(c.buf[2])
167                         if cookie != 1 {
168                                 // We issued only one request (GetKeyboardMapping) with a cookie of 1,
169                                 // so we shouldn't get any other reply from the X server.
170                                 c.eventc <- gui.ErrEvent{os.NewError("x11: unexpected cookie")}
171                                 return
172                         }
173                         keysymsPerKeycode = int(c.buf[1])
174                         b := make([]int, 256*keysymsPerKeycode)
175                         for i := range keymap {
176                                 keymap[i] = b[i*keysymsPerKeycode : (i+1)*keysymsPerKeycode]
177                         }
178                         for i := keymapLo; i <= keymapHi; i++ {
179                                 m := keymap[i]
180                                 for j := range m {
181                                         u, err := readU32LE(c.r, c.buf[:4])
182                                         if err != nil {
183                                                 if err != os.EOF {
184                                                         c.eventc <- gui.ErrEvent{err}
185                                                 }
186                                                 return
187                                         }
188                                         m[j] = int(u)
189                                 }
190                         }
191                 case 0x02, 0x03: // Key press, key release.
192                         // X Keyboard Encoding is documented at http://tronche.com/gui/x/xlib/input/keyboard-encoding.html
193                         // TODO(nigeltao): Do we need to implement the "MODE SWITCH / group modifier" feature
194                         // or is that some no-longer-used X construct?
195                         if keysymsPerKeycode < 2 {
196                                 // Either we haven't yet received the GetKeyboardMapping reply or
197                                 // the X server has sent one that's too short.
198                                 continue
199                         }
200                         keycode := int(c.buf[1])
201                         shift := int(c.buf[28]) & 0x01
202                         keysym := keymap[keycode][shift]
203                         if keysym == 0 {
204                                 keysym = keymap[keycode][0]
205                         }
206                         // TODO(nigeltao): Should we send KeyEvents for Shift/Ctrl/Alt? Should Shift-A send
207                         // the same int down the channel as the sent on just the A key?
208                         // TODO(nigeltao): How should IME events (e.g. key presses that should generate CJK text) work? Or
209                         // is that outside the scope of the gui.Window interface?
210                         if c.buf[0] == 0x03 {
211                                 keysym = -keysym
212                         }
213                         c.eventc <- gui.KeyEvent{keysym}
214                 case 0x04, 0x05: // Button press, button release.
215                         mask := 1 << (c.buf[1] - 1)
216                         if c.buf[0] == 0x04 {
217                                 c.mouseState.Buttons |= mask
218                         } else {
219                                 c.mouseState.Buttons &^= mask
220                         }
221                         c.mouseState.Nsec = time.Nanoseconds()
222                         c.eventc <- c.mouseState
223                 case 0x06: // Motion notify.
224                         c.mouseState.Loc.X = int(int16(c.buf[25])<<8 | int16(c.buf[24]))
225                         c.mouseState.Loc.Y = int(int16(c.buf[27])<<8 | int16(c.buf[26]))
226                         c.mouseState.Nsec = time.Nanoseconds()
227                         c.eventc <- c.mouseState
228                 case 0x0c: // Expose.
229                         // A single user action could trigger multiple expose events (e.g. if moving another
230                         // window with XShape'd rounded corners over our window). In that case, the X server will
231                         // send a uint16 count (in bytes 16-17) of the number of additional expose events coming.
232                         // We could parse each event for the (x, y, width, height) and maintain a minimal dirty
233                         // rectangle, but for now, the simplest approach is to paint the entire window, when
234                         // receiving the final event in the series.
235                         if c.buf[17] == 0 && c.buf[16] == 0 {
236                                 // TODO(nigeltao): Should we ignore the very first expose event? A freshly mapped window
237                                 // will trigger expose, but until the first c.FlushImage call, there's probably nothing to
238                                 // paint but black. For an 800x600 window, at 4 bytes per pixel, each repaint writes about
239                                 // 2MB over the socket.
240                                 c.FlushImage()
241                         }
242                         // TODO(nigeltao): Should we listen to DestroyNotify (0x11) and ResizeRequest (0x19) events?
243                         // What about EnterNotify (0x07) and LeaveNotify (0x08)?
244                 }
245         }
246 }
247
248 // connect connects to the X server given by the full X11 display name (e.g.
249 // ":12.0") and returns the connection as well as the portion of the full name
250 // that is the display number (e.g. "12").
251 // Examples:
252 //      connect(":1")                 // calls net.Dial("unix", "", "/tmp/.X11-unix/X1"), displayStr="1"
253 //      connect("/tmp/launch-123/:0") // calls net.Dial("unix", "", "/tmp/launch-123/:0"), displayStr="0"
254 //      connect("hostname:2.1")       // calls net.Dial("tcp", "", "hostname:6002"), displayStr="2"
255 //      connect("tcp/hostname:1.0")   // calls net.Dial("tcp", "", "hostname:6001"), displayStr="1"
256 func connect(display string) (conn net.Conn, displayStr string, err os.Error) {
257         colonIdx := strings.LastIndex(display, ":")
258         if colonIdx < 0 {
259                 return nil, "", os.NewError("bad display: " + display)
260         }
261         // Parse the section before the colon.
262         var protocol, host, socket string
263         if display[0] == '/' {
264                 socket = display[:colonIdx]
265         } else {
266                 if i := strings.LastIndex(display, "/"); i < 0 {
267                         // The default protocol is TCP.
268                         protocol = "tcp"
269                         host = display[:colonIdx]
270                 } else {
271                         protocol = display[:i]
272                         host = display[i+1 : colonIdx]
273                 }
274         }
275         // Parse the section after the colon.
276         after := display[colonIdx+1:]
277         if after == "" {
278                 return nil, "", os.NewError("bad display: " + display)
279         }
280         if i := strings.LastIndex(after, "."); i < 0 {
281                 displayStr = after
282         } else {
283                 displayStr = after[:i]
284         }
285         displayInt, err := strconv.Atoi(displayStr)
286         if err != nil || displayInt < 0 {
287                 return nil, "", os.NewError("bad display: " + display)
288         }
289         // Make the connection.
290         if socket != "" {
291                 conn, err = net.Dial("unix", socket+":"+displayStr)
292         } else if host != "" {
293                 conn, err = net.Dial(protocol, host+":"+strconv.Itoa(6000+displayInt))
294         } else {
295                 conn, err = net.Dial("unix", "/tmp/.X11-unix/X"+displayStr)
296         }
297         if err != nil {
298                 return nil, "", os.NewError("cannot connect to " + display + ": " + err.String())
299         }
300         return
301 }
302
303 // authenticate authenticates ourselves with the X server.
304 // displayStr is the "12" out of ":12.0".
305 func authenticate(w *bufio.Writer, displayStr string) os.Error {
306         key, value, err := readAuth(displayStr)
307         if err != nil {
308                 return err
309         }
310         // Assume that the authentication protocol is "MIT-MAGIC-COOKIE-1".
311         if len(key) != 18 || len(value) != 16 {
312                 return os.NewError("unsupported Xauth")
313         }
314         // 0x006c means little-endian. 0x000b, 0x0000 means X major version 11, minor version 0.
315         // 0x0012 and 0x0010 means the auth key and value have lengths 18 and 16.
316         // The final 0x0000 is padding, so that the string length is a multiple of 4.
317         _, err = io.WriteString(w, "\x6c\x00\x0b\x00\x00\x00\x12\x00\x10\x00\x00\x00")
318         if err != nil {
319                 return err
320         }
321         _, err = io.WriteString(w, key)
322         if err != nil {
323                 return err
324         }
325         // Again, the 0x0000 is padding.
326         _, err = io.WriteString(w, "\x00\x00")
327         if err != nil {
328                 return err
329         }
330         _, err = io.WriteString(w, value)
331         if err != nil {
332                 return err
333         }
334         err = w.Flush()
335         if err != nil {
336                 return err
337         }
338         return nil
339 }
340
341 // readU8 reads a uint8 from r, using b as a scratch buffer.
342 func readU8(r io.Reader, b []byte) (uint8, os.Error) {
343         _, err := io.ReadFull(r, b[:1])
344         if err != nil {
345                 return 0, err
346         }
347         return uint8(b[0]), nil
348 }
349
350 // readU16LE reads a little-endian uint16 from r, using b as a scratch buffer.
351 func readU16LE(r io.Reader, b []byte) (uint16, os.Error) {
352         _, err := io.ReadFull(r, b[:2])
353         if err != nil {
354                 return 0, err
355         }
356         return uint16(b[0]) | uint16(b[1])<<8, nil
357 }
358
359 // readU32LE reads a little-endian uint32 from r, using b as a scratch buffer.
360 func readU32LE(r io.Reader, b []byte) (uint32, os.Error) {
361         _, err := io.ReadFull(r, b[:4])
362         if err != nil {
363                 return 0, err
364         }
365         return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24, nil
366 }
367
368 // setU32LE sets b[:4] to be the little-endian representation of u.
369 func setU32LE(b []byte, u uint32) {
370         b[0] = byte((u >> 0) & 0xff)
371         b[1] = byte((u >> 8) & 0xff)
372         b[2] = byte((u >> 16) & 0xff)
373         b[3] = byte((u >> 24) & 0xff)
374 }
375
376 // checkPixmapFormats checks that we have an agreeable X pixmap Format.
377 func checkPixmapFormats(r io.Reader, b []byte, n int) (agree bool, err os.Error) {
378         for i := 0; i < n; i++ {
379                 _, err = io.ReadFull(r, b[:8])
380                 if err != nil {
381                         return
382                 }
383                 // Byte 0 is depth, byte 1 is bits-per-pixel, byte 2 is scanline-pad, the rest (5) is padding.
384                 if b[0] == 24 && b[1] == 32 {
385                         agree = true
386                 }
387         }
388         return
389 }
390
391 // checkDepths checks that we have an agreeable X Depth (i.e. one that has an agreeable X VisualType).
392 func checkDepths(r io.Reader, b []byte, n int, visual uint32) (agree bool, err os.Error) {
393         for i := 0; i < n; i++ {
394                 var depth, visualsLen uint16
395                 depth, err = readU16LE(r, b)
396                 if err != nil {
397                         return
398                 }
399                 depth &= 0xff
400                 visualsLen, err = readU16LE(r, b)
401                 if err != nil {
402                         return
403                 }
404                 // Ignore 4 bytes of padding.
405                 _, err = io.ReadFull(r, b[:4])
406                 if err != nil {
407                         return
408                 }
409                 for j := 0; j < int(visualsLen); j++ {
410                         // Read 24 bytes: visual(4), class(1), bits per rgb value(1), colormap entries(2),
411                         // red mask(4), green mask(4), blue mask(4), padding(4).
412                         v, _ := readU32LE(r, b)
413                         _, _ = readU32LE(r, b)
414                         rm, _ := readU32LE(r, b)
415                         gm, _ := readU32LE(r, b)
416                         bm, _ := readU32LE(r, b)
417                         _, err = readU32LE(r, b)
418                         if err != nil {
419                                 return
420                         }
421                         if v == visual && rm == 0xff0000 && gm == 0xff00 && bm == 0xff && depth == 24 {
422                                 agree = true
423                         }
424                 }
425         }
426         return
427 }
428
429 // checkScreens checks that we have an agreeable X Screen.
430 func checkScreens(r io.Reader, b []byte, n int) (root, visual uint32, err os.Error) {
431         for i := 0; i < n; i++ {
432                 var root0, visual0, x uint32
433                 root0, err = readU32LE(r, b)
434                 if err != nil {
435                         return
436                 }
437                 // Ignore the next 7x4 bytes, which is: colormap, whitepixel, blackpixel, current input masks,
438                 // width and height (pixels), width and height (mm), min and max installed maps.
439                 _, err = io.ReadFull(r, b[:28])
440                 if err != nil {
441                         return
442                 }
443                 visual0, err = readU32LE(r, b)
444                 if err != nil {
445                         return
446                 }
447                 // Next 4 bytes: backing stores, save unders, root depth, allowed depths length.
448                 x, err = readU32LE(r, b)
449                 if err != nil {
450                         return
451                 }
452                 nDepths := int(x >> 24)
453                 var agree bool
454                 agree, err = checkDepths(r, b, nDepths, visual0)
455                 if err != nil {
456                         return
457                 }
458                 if agree && root == 0 {
459                         root = root0
460                         visual = visual0
461                 }
462         }
463         return
464 }
465
466 // handshake performs the protocol handshake with the X server, and ensures
467 // that the server provides a compatible Screen, Depth, etc.
468 func (c *conn) handshake() os.Error {
469         _, err := io.ReadFull(c.r, c.buf[:8])
470         if err != nil {
471                 return err
472         }
473         // Byte 0 should be 1 (success), bytes 2:6 should be 0xb0000000 (major/minor version 11.0).
474         if c.buf[0] != 1 || c.buf[2] != 11 || c.buf[3] != 0 || c.buf[4] != 0 || c.buf[5] != 0 {
475                 return os.NewError("unsupported X version")
476         }
477         // Ignore the release number.
478         _, err = io.ReadFull(c.r, c.buf[:4])
479         if err != nil {
480                 return err
481         }
482         // Read the resource ID base.
483         resourceIdBase, err := readU32LE(c.r, c.buf[:4])
484         if err != nil {
485                 return err
486         }
487         // Read the resource ID mask.
488         resourceIdMask, err := readU32LE(c.r, c.buf[:4])
489         if err != nil {
490                 return err
491         }
492         if resourceIdMask < 256 {
493                 return os.NewError("X resource ID mask is too small")
494         }
495         // Ignore the motion buffer size.
496         _, err = io.ReadFull(c.r, c.buf[:4])
497         if err != nil {
498                 return err
499         }
500         // Read the vendor length and round it up to a multiple of 4,
501         // for X11 protocol alignment reasons.
502         vendorLen, err := readU16LE(c.r, c.buf[:2])
503         if err != nil {
504                 return err
505         }
506         vendorLen = (vendorLen + 3) &^ 3
507         // Read the maximum request length.
508         maxReqLen, err := readU16LE(c.r, c.buf[:2])
509         if err != nil {
510                 return err
511         }
512         if maxReqLen != 0xffff {
513                 return os.NewError("unsupported X maximum request length")
514         }
515         // Read the roots length.
516         rootsLen, err := readU8(c.r, c.buf[:1])
517         if err != nil {
518                 return err
519         }
520         // Read the pixmap formats length.
521         pixmapFormatsLen, err := readU8(c.r, c.buf[:1])
522         if err != nil {
523                 return err
524         }
525         // Ignore some things that we don't care about (totaling 10 + vendorLen bytes):
526         // imageByteOrder(1), bitmapFormatBitOrder(1), bitmapFormatScanlineUnit(1) bitmapFormatScanlinePad(1),
527         // minKeycode(1), maxKeycode(1), padding(4), vendor (vendorLen).
528         if 10+int(vendorLen) > cap(c.buf) {
529                 return os.NewError("unsupported X vendor")
530         }
531         _, err = io.ReadFull(c.r, c.buf[:10+int(vendorLen)])
532         if err != nil {
533                 return err
534         }
535         // Check that we have an agreeable pixmap format.
536         agree, err := checkPixmapFormats(c.r, c.buf[:8], int(pixmapFormatsLen))
537         if err != nil {
538                 return err
539         }
540         if !agree {
541                 return os.NewError("unsupported X pixmap formats")
542         }
543         // Check that we have an agreeable screen.
544         root, visual, err := checkScreens(c.r, c.buf[:24], int(rootsLen))
545         if err != nil {
546                 return err
547         }
548         if root == 0 || visual == 0 {
549                 return os.NewError("unsupported X screen")
550         }
551         c.gc = resID(resourceIdBase)
552         c.window = resID(resourceIdBase + 1)
553         c.root = resID(root)
554         c.visual = resID(visual)
555         return nil
556 }
557
558 // NewWindow calls NewWindowDisplay with $DISPLAY.
559 func NewWindow() (gui.Window, os.Error) {
560         display := os.Getenv("DISPLAY")
561         if len(display) == 0 {
562                 return nil, os.NewError("$DISPLAY not set")
563         }
564         return NewWindowDisplay(display)
565 }
566
567 // NewWindowDisplay returns a new gui.Window, backed by a newly created and
568 // mapped X11 window. The X server to connect to is specified by the display
569 // string, such as ":1".
570 func NewWindowDisplay(display string) (gui.Window, os.Error) {
571         socket, displayStr, err := connect(display)
572         if err != nil {
573                 return nil, err
574         }
575         c := new(conn)
576         c.c = socket
577         c.r = bufio.NewReader(socket)
578         c.w = bufio.NewWriter(socket)
579         err = authenticate(c.w, displayStr)
580         if err != nil {
581                 return nil, err
582         }
583         err = c.handshake()
584         if err != nil {
585                 return nil, err
586         }
587
588         // Now that we're connected, show a window, via three X protocol messages.
589         // First, issue a GetKeyboardMapping request. This is the first request, and
590         // will be associated with a cookie of 1.
591         setU32LE(c.buf[0:4], 0x00020065) // 0x65 is the GetKeyboardMapping opcode, and the message is 2 x 4 bytes long.
592         setU32LE(c.buf[4:8], uint32((keymapHi-keymapLo+1)<<8|keymapLo))
593         // Second, create a graphics context (GC).
594         setU32LE(c.buf[8:12], 0x00060037) // 0x37 is the CreateGC opcode, and the message is 6 x 4 bytes long.
595         setU32LE(c.buf[12:16], uint32(c.gc))
596         setU32LE(c.buf[16:20], uint32(c.root))
597         setU32LE(c.buf[20:24], 0x00010004) // Bit 2 is XCB_GC_FOREGROUND, bit 16 is XCB_GC_GRAPHICS_EXPOSURES.
598         setU32LE(c.buf[24:28], 0x00000000) // The Foreground is black.
599         setU32LE(c.buf[28:32], 0x00000000) // GraphicsExposures' value is unused.
600         // Third, create the window.
601         setU32LE(c.buf[32:36], 0x000a0001) // 0x01 is the CreateWindow opcode, and the message is 10 x 4 bytes long.
602         setU32LE(c.buf[36:40], uint32(c.window))
603         setU32LE(c.buf[40:44], uint32(c.root))
604         setU32LE(c.buf[44:48], 0x00000000) // Initial (x, y) is (0, 0).
605         setU32LE(c.buf[48:52], windowHeight<<16|windowWidth)
606         setU32LE(c.buf[52:56], 0x00010000) // Border width is 0, XCB_WINDOW_CLASS_INPUT_OUTPUT is 1.
607         setU32LE(c.buf[56:60], uint32(c.visual))
608         setU32LE(c.buf[60:64], 0x00000802) // Bit 1 is XCB_CW_BACK_PIXEL, bit 11 is XCB_CW_EVENT_MASK.
609         setU32LE(c.buf[64:68], 0x00000000) // The Back-Pixel is black.
610         setU32LE(c.buf[68:72], 0x0000804f) // Key/button press and release, pointer motion, and expose event masks.
611         // Fourth, map the window.
612         setU32LE(c.buf[72:76], 0x00020008) // 0x08 is the MapWindow opcode, and the message is 2 x 4 bytes long.
613         setU32LE(c.buf[76:80], uint32(c.window))
614         // Write the bytes.
615         _, err = c.w.Write(c.buf[:80])
616         if err != nil {
617                 return nil, err
618         }
619         err = c.w.Flush()
620         if err != nil {
621                 return nil, err
622         }
623
624         c.img = image.NewRGBA(image.Rect(0, 0, windowWidth, windowHeight))
625         c.eventc = make(chan interface{}, 16)
626         c.flush = make(chan bool, 1)
627         go c.readSocket()
628         go c.writeSocket()
629         return c, nil
630 }