OSDN Git Service

libgo: Update to weekly.2011-11-02.
[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         "errors"
14         "exp/gui"
15         "image"
16         "image/draw"
17         "io"
18         "log"
19         "net"
20         "os"
21         "strconv"
22         "strings"
23         "time"
24 )
25
26 type resID uint32 // X resource IDs.
27
28 // TODO(nigeltao): Handle window resizes.
29 const (
30         windowHeight = 600
31         windowWidth  = 800
32 )
33
34 const (
35         keymapLo = 8
36         keymapHi = 255
37 )
38
39 type conn struct {
40         c io.Closer
41         r *bufio.Reader
42         w *bufio.Writer
43
44         gc, window, root, visual resID
45
46         img        *image.RGBA
47         eventc     chan interface{}
48         mouseState gui.MouseEvent
49
50         buf [256]byte // General purpose scratch buffer.
51
52         flush     chan bool
53         flushBuf0 [24]byte
54         flushBuf1 [4 * 1024]byte
55 }
56
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() {
61         defer c.c.Close()
62         for _ = range c.flush {
63                 b := c.img.Bounds()
64                 if b.Empty() {
65                         continue
66                 }
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.
72                 units := 6 + b.Dx()
73                 if units > 0xffff || b.Dy() > 0xffff {
74                         log.Print("x11: window is too large for PutImage")
75                         return
76                 }
77
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.
86
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 {
90                                 if err != io.EOF {
91                                         log.Println("x11:", err)
92                                 }
93                                 return
94                         }
95                         p := c.img.Pix[(y-b.Min.Y)*c.img.Stride:]
96                         for x, dx := 0, 4*b.Dx(); x < dx; {
97                                 nx := dx - x
98                                 if nx > len(c.flushBuf1) {
99                                         nx = len(c.flushBuf1) &^ 3
100                                 }
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]
106                                 }
107                                 x += nx
108                                 if _, err := c.w.Write(c.flushBuf1[:nx]); err != nil {
109                                         if err != io.EOF {
110                                                 log.Println("x11:", err)
111                                         }
112                                         return
113                                 }
114                         }
115                 }
116                 if err := c.w.Flush(); err != nil {
117                         if err != io.EOF {
118                                 log.Println("x11:", err)
119                         }
120                         return
121                 }
122         }
123 }
124
125 func (c *conn) Screen() draw.Image { return c.img }
126
127 func (c *conn) FlushImage() {
128         select {
129         case c.flush <- false:
130                 // Flush notification sent.
131         default:
132                 // Could not send.
133                 // Flush notification must be pending already.
134         }
135 }
136
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.
140         close(c.flush)
141         for _ = range c.eventc {
142                 // Drain the channel to allow the readSocket goroutine to shut down.
143         }
144         return nil
145 }
146
147 func (c *conn) EventChan() <-chan interface{} { return c.eventc }
148
149 // readSocket runs in its own goroutine, reading X events and sending gui
150 // events on c's EventChan.
151 func (c *conn) readSocket() {
152         var (
153                 keymap            [256][]int
154                 keysymsPerKeycode int
155         )
156         defer close(c.eventc)
157         for {
158                 // X events are always 32 bytes long.
159                 if _, err := io.ReadFull(c.r, c.buf[:32]); err != nil {
160                         if err != io.EOF {
161                                 c.eventc <- gui.ErrEvent{err}
162                         }
163                         return
164                 }
165                 switch c.buf[0] {
166                 case 0x01: // Reply from a request (e.g. GetKeyboardMapping).
167                         cookie := int(c.buf[3])<<8 | int(c.buf[2])
168                         if cookie != 1 {
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")}
172                                 return
173                         }
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]
178                         }
179                         for i := keymapLo; i <= keymapHi; i++ {
180                                 m := keymap[i]
181                                 for j := range m {
182                                         u, err := readU32LE(c.r, c.buf[:4])
183                                         if err != nil {
184                                                 if err != io.EOF {
185                                                         c.eventc <- gui.ErrEvent{err}
186                                                 }
187                                                 return
188                                         }
189                                         m[j] = int(u)
190                                 }
191                         }
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.
199                                 continue
200                         }
201                         keycode := int(c.buf[1])
202                         shift := int(c.buf[28]) & 0x01
203                         keysym := keymap[keycode][shift]
204                         if keysym == 0 {
205                                 keysym = keymap[keycode][0]
206                         }
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 {
212                                 keysym = -keysym
213                         }
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
219                         } else {
220                                 c.mouseState.Buttons &^= mask
221                         }
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.
241                                 c.FlushImage()
242                         }
243                         // TODO(nigeltao): Should we listen to DestroyNotify (0x11) and ResizeRequest (0x19) events?
244                         // What about EnterNotify (0x07) and LeaveNotify (0x08)?
245                 }
246         }
247 }
248
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").
252 // Examples:
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, ":")
259         if colonIdx < 0 {
260                 return nil, "", errors.New("bad display: " + display)
261         }
262         // Parse the section before the colon.
263         var protocol, host, socket string
264         if display[0] == '/' {
265                 socket = display[:colonIdx]
266         } else {
267                 if i := strings.LastIndex(display, "/"); i < 0 {
268                         // The default protocol is TCP.
269                         protocol = "tcp"
270                         host = display[:colonIdx]
271                 } else {
272                         protocol = display[:i]
273                         host = display[i+1 : colonIdx]
274                 }
275         }
276         // Parse the section after the colon.
277         after := display[colonIdx+1:]
278         if after == "" {
279                 return nil, "", errors.New("bad display: " + display)
280         }
281         if i := strings.LastIndex(after, "."); i < 0 {
282                 displayStr = after
283         } else {
284                 displayStr = after[:i]
285         }
286         displayInt, err := strconv.Atoi(displayStr)
287         if err != nil || displayInt < 0 {
288                 return nil, "", errors.New("bad display: " + display)
289         }
290         // Make the connection.
291         if socket != "" {
292                 conn, err = net.Dial("unix", socket+":"+displayStr)
293         } else if host != "" {
294                 conn, err = net.Dial(protocol, host+":"+strconv.Itoa(6000+displayInt))
295         } else {
296                 conn, err = net.Dial("unix", "/tmp/.X11-unix/X"+displayStr)
297         }
298         if err != nil {
299                 return nil, "", errors.New("cannot connect to " + display + ": " + err.Error())
300         }
301         return
302 }
303
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)
308         if err != nil {
309                 return err
310         }
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")
314         }
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")
319         if err != nil {
320                 return err
321         }
322         _, err = io.WriteString(w, key)
323         if err != nil {
324                 return err
325         }
326         // Again, the 0x0000 is padding.
327         _, err = io.WriteString(w, "\x00\x00")
328         if err != nil {
329                 return err
330         }
331         _, err = io.WriteString(w, value)
332         if err != nil {
333                 return err
334         }
335         err = w.Flush()
336         if err != nil {
337                 return err
338         }
339         return nil
340 }
341
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])
345         if err != nil {
346                 return 0, err
347         }
348         return uint8(b[0]), nil
349 }
350
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])
354         if err != nil {
355                 return 0, err
356         }
357         return uint16(b[0]) | uint16(b[1])<<8, nil
358 }
359
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])
363         if err != nil {
364                 return 0, err
365         }
366         return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24, nil
367 }
368
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)
375 }
376
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])
381                 if err != nil {
382                         return
383                 }
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 {
386                         agree = true
387                 }
388         }
389         return
390 }
391
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)
397                 if err != nil {
398                         return
399                 }
400                 depth &= 0xff
401                 visualsLen, err = readU16LE(r, b)
402                 if err != nil {
403                         return
404                 }
405                 // Ignore 4 bytes of padding.
406                 _, err = io.ReadFull(r, b[:4])
407                 if err != nil {
408                         return
409                 }
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)
419                         if err != nil {
420                                 return
421                         }
422                         if v == visual && rm == 0xff0000 && gm == 0xff00 && bm == 0xff && depth == 24 {
423                                 agree = true
424                         }
425                 }
426         }
427         return
428 }
429
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)
435                 if err != nil {
436                         return
437                 }
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])
441                 if err != nil {
442                         return
443                 }
444                 visual0, err = readU32LE(r, b)
445                 if err != nil {
446                         return
447                 }
448                 // Next 4 bytes: backing stores, save unders, root depth, allowed depths length.
449                 x, err = readU32LE(r, b)
450                 if err != nil {
451                         return
452                 }
453                 nDepths := int(x >> 24)
454                 var agree bool
455                 agree, err = checkDepths(r, b, nDepths, visual0)
456                 if err != nil {
457                         return
458                 }
459                 if agree && root == 0 {
460                         root = root0
461                         visual = visual0
462                 }
463         }
464         return
465 }
466
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])
471         if err != nil {
472                 return err
473         }
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")
477         }
478         // Ignore the release number.
479         _, err = io.ReadFull(c.r, c.buf[:4])
480         if err != nil {
481                 return err
482         }
483         // Read the resource ID base.
484         resourceIdBase, err := readU32LE(c.r, c.buf[:4])
485         if err != nil {
486                 return err
487         }
488         // Read the resource ID mask.
489         resourceIdMask, err := readU32LE(c.r, c.buf[:4])
490         if err != nil {
491                 return err
492         }
493         if resourceIdMask < 256 {
494                 return errors.New("X resource ID mask is too small")
495         }
496         // Ignore the motion buffer size.
497         _, err = io.ReadFull(c.r, c.buf[:4])
498         if err != nil {
499                 return err
500         }
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])
504         if err != nil {
505                 return err
506         }
507         vendorLen = (vendorLen + 3) &^ 3
508         // Read the maximum request length.
509         maxReqLen, err := readU16LE(c.r, c.buf[:2])
510         if err != nil {
511                 return err
512         }
513         if maxReqLen != 0xffff {
514                 return errors.New("unsupported X maximum request length")
515         }
516         // Read the roots length.
517         rootsLen, err := readU8(c.r, c.buf[:1])
518         if err != nil {
519                 return err
520         }
521         // Read the pixmap formats length.
522         pixmapFormatsLen, err := readU8(c.r, c.buf[:1])
523         if err != nil {
524                 return err
525         }
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")
531         }
532         _, err = io.ReadFull(c.r, c.buf[:10+int(vendorLen)])
533         if err != nil {
534                 return err
535         }
536         // Check that we have an agreeable pixmap format.
537         agree, err := checkPixmapFormats(c.r, c.buf[:8], int(pixmapFormatsLen))
538         if err != nil {
539                 return err
540         }
541         if !agree {
542                 return errors.New("unsupported X pixmap formats")
543         }
544         // Check that we have an agreeable screen.
545         root, visual, err := checkScreens(c.r, c.buf[:24], int(rootsLen))
546         if err != nil {
547                 return err
548         }
549         if root == 0 || visual == 0 {
550                 return errors.New("unsupported X screen")
551         }
552         c.gc = resID(resourceIdBase)
553         c.window = resID(resourceIdBase + 1)
554         c.root = resID(root)
555         c.visual = resID(visual)
556         return nil
557 }
558
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")
564         }
565         return NewWindowDisplay(display)
566 }
567
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)
573         if err != nil {
574                 return nil, err
575         }
576         c := new(conn)
577         c.c = socket
578         c.r = bufio.NewReader(socket)
579         c.w = bufio.NewWriter(socket)
580         err = authenticate(c.w, displayStr)
581         if err != nil {
582                 return nil, err
583         }
584         err = c.handshake()
585         if err != nil {
586                 return nil, err
587         }
588
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))
615         // Write the bytes.
616         _, err = c.w.Write(c.buf[:80])
617         if err != nil {
618                 return nil, err
619         }
620         err = c.w.Flush()
621         if err != nil {
622                 return nil, err
623         }
624
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)
628         go c.readSocket()
629         go c.writeSocket()
630         return c, nil
631 }