f461de6a9f5e55354e5e8750bc212115e44e6a6f
[qemu] / vnc.c
1 #include "vl.h"
2
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include <netinet/tcp.h>
6 #include <fcntl.h>
7
8 #define VNC_REFRESH_INTERVAL (1000 / 30)
9
10 #include "vnc_keysym.h"
11 #include "keymaps.c"
12
13 typedef struct Buffer
14 {
15     size_t capacity;
16     size_t offset;
17     char *buffer;
18 } Buffer;
19
20 typedef struct VncState VncState;
21
22 typedef int VncReadEvent(VncState *vs, char *data, size_t len);
23
24 struct VncState
25 {
26     QEMUTimer *timer;
27     int lsock;
28     int csock;
29     DisplayState *ds;
30     int need_update;
31     int width;
32     int height;
33     uint64_t dirty_row[768];
34     char *old_data;
35     int depth;
36     int has_resize;
37     int has_hextile;
38     Buffer output;
39     Buffer input;
40     kbd_layout_t *kbd_layout;
41
42     VncReadEvent *read_handler;
43     size_t read_handler_expect;
44 };
45
46 /* TODO
47    1) Get the queue working for IO.
48    2) there is some weirdness when using the -S option (the screen is grey
49       and not totally invalidated
50    3) resolutions > 1024
51 */
52
53 static void vnc_write(VncState *vs, const void *data, size_t len);
54 static void vnc_write_u32(VncState *vs, uint32_t value);
55 static void vnc_write_s32(VncState *vs, int32_t value);
56 static void vnc_write_u16(VncState *vs, uint16_t value);
57 static void vnc_write_u8(VncState *vs, uint8_t value);
58 static void vnc_flush(VncState *vs);
59 static void vnc_update_client(void *opaque);
60 static void vnc_client_read(void *opaque);
61
62 static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
63 {
64     VncState *vs = ds->opaque;
65     int i;
66
67     h += y;
68
69     for (; y < h; y++)
70         for (i = 0; i < w; i += 16)
71             vs->dirty_row[y] |= (1ULL << ((x + i) / 16));
72 }
73
74 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
75                                    int32_t encoding)
76 {
77     vnc_write_u16(vs, x);
78     vnc_write_u16(vs, y);
79     vnc_write_u16(vs, w);
80     vnc_write_u16(vs, h);
81
82     vnc_write_s32(vs, encoding);
83 }
84
85 static void vnc_dpy_resize(DisplayState *ds, int w, int h)
86 {
87     VncState *vs = ds->opaque;
88
89     ds->data = realloc(ds->data, w * h * vs->depth);
90     vs->old_data = realloc(vs->old_data, w * h * vs->depth);
91
92     if (ds->data == NULL || vs->old_data == NULL) {
93         fprintf(stderr, "vnc: memory allocation failed\n");
94         exit(1);
95     }
96
97     ds->depth = vs->depth * 8;
98     ds->width = w;
99     ds->height = h;
100     ds->linesize = w * vs->depth;
101     if (vs->csock != -1 && vs->has_resize) {
102         vnc_write_u8(vs, 0);  /* msg id */
103         vnc_write_u8(vs, 0);
104         vnc_write_u16(vs, 1); /* number of rects */
105         vnc_framebuffer_update(vs, 0, 0, ds->width, ds->height, -223);
106         vnc_flush(vs);
107         vs->width = ds->width;
108         vs->height = ds->height;
109     }
110 }
111
112 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
113 {
114     int i;
115     char *row;
116
117     vnc_framebuffer_update(vs, x, y, w, h, 0);
118
119     row = vs->ds->data + y * vs->ds->linesize + x * vs->depth;
120     for (i = 0; i < h; i++) {
121         vnc_write(vs, row, w * vs->depth);
122         row += vs->ds->linesize;
123     }
124 }
125
126 static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h)
127 {
128     ptr[0] = ((x & 0x0F) << 4) | (y & 0x0F);
129     ptr[1] = (((w - 1) & 0x0F) << 4) | ((h - 1) & 0x0F);
130 }
131
132 #define BPP 8
133 #include "vnchextile.h"
134 #undef BPP
135
136 #define BPP 16
137 #include "vnchextile.h"
138 #undef BPP
139
140 #define BPP 32
141 #include "vnchextile.h"
142 #undef BPP
143
144 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
145 {
146     int i, j;
147     int has_fg, has_bg;
148     uint32_t last_fg32, last_bg32;
149     uint16_t last_fg16, last_bg16;
150     uint8_t last_fg8, last_bg8;
151
152     vnc_framebuffer_update(vs, x, y, w, h, 5);
153
154     has_fg = has_bg = 0;
155     for (j = y; j < (y + h); j += 16) {
156         for (i = x; i < (x + w); i += 16) {
157             switch (vs->depth) {
158             case 1:
159                 send_hextile_tile_8(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
160                                     &last_bg8, &last_fg8, &has_bg, &has_fg);
161                 break;
162             case 2:
163                 send_hextile_tile_16(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
164                                      &last_bg16, &last_fg16, &has_bg, &has_fg);
165                 break;
166             case 4:
167                 send_hextile_tile_32(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
168                                      &last_bg32, &last_fg32, &has_bg, &has_fg);
169                 break;
170             default:
171                 break;
172             }
173         }
174     }
175 }
176
177 static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
178 {
179         if (vs->has_hextile)
180             send_framebuffer_update_hextile(vs, x, y, w, h);
181         else
182             send_framebuffer_update_raw(vs, x, y, w, h);
183 }
184
185 static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
186 {
187     int src, dst;
188     char *src_row;
189     char *dst_row;
190     char *old_row;
191     int y = 0;
192     int pitch = ds->linesize;
193     VncState *vs = ds->opaque;
194
195     vnc_update_client(vs);
196
197     if (dst_y > src_y) {
198         y = h - 1;
199         pitch = -pitch;
200     }
201
202     src = (ds->linesize * (src_y + y) + vs->depth * src_x);
203     dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x);
204
205     src_row = ds->data + src;
206     dst_row = ds->data + dst;
207     old_row = vs->old_data + dst;
208
209     for (y = 0; y < h; y++) {
210         memmove(old_row, src_row, w * vs->depth);
211         memmove(dst_row, src_row, w * vs->depth);
212         src_row += pitch;
213         dst_row += pitch;
214         old_row += pitch;
215     }
216
217     vnc_write_u8(vs, 0);  /* msg id */
218     vnc_write_u8(vs, 0);
219     vnc_write_u16(vs, 1); /* number of rects */
220     vnc_framebuffer_update(vs, dst_x, dst_y, w, h, 1);
221     vnc_write_u16(vs, src_x);
222     vnc_write_u16(vs, src_y);
223     vnc_flush(vs);
224 }
225
226 static int find_dirty_height(VncState *vs, int y, int last_x, int x)
227 {
228     int h;
229
230     for (h = 1; h < (vs->height - y); h++) {
231         int tmp_x;
232         if (!(vs->dirty_row[y + h] & (1ULL << last_x)))
233             break;
234         for (tmp_x = last_x; tmp_x < x; tmp_x++)
235             vs->dirty_row[y + h] &= ~(1ULL << tmp_x);
236     }
237
238     return h;
239 }
240
241 static void vnc_update_client(void *opaque)
242 {
243     VncState *vs = opaque;
244
245     if (vs->need_update && vs->csock != -1) {
246         int y;
247         char *row;
248         char *old_row;
249         uint64_t width_mask;
250         int n_rectangles;
251         int saved_offset;
252         int has_dirty = 0;
253
254         width_mask = (1ULL << (vs->width / 16)) - 1;
255
256         if (vs->width == 1024)
257             width_mask = ~(0ULL);
258
259         /* Walk through the dirty map and eliminate tiles that
260            really aren't dirty */
261         row = vs->ds->data;
262         old_row = vs->old_data;
263
264         for (y = 0; y < vs->height; y++) {
265             if (vs->dirty_row[y] & width_mask) {
266                 int x;
267                 char *ptr, *old_ptr;
268
269                 ptr = row;
270                 old_ptr = old_row;
271
272                 for (x = 0; x < vs->ds->width; x += 16) {
273                     if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) {
274                         vs->dirty_row[y] &= ~(1ULL << (x / 16));
275                     } else {
276                         has_dirty = 1;
277                         memcpy(old_ptr, ptr, 16 * vs->depth);
278                     }
279
280                     ptr += 16 * vs->depth;
281                     old_ptr += 16 * vs->depth;
282                 }
283             }
284
285             row += vs->ds->linesize;
286             old_row += vs->ds->linesize;
287         }
288
289         if (!has_dirty) {
290             qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
291             return;
292         }
293
294         /* Count rectangles */
295         n_rectangles = 0;
296         vnc_write_u8(vs, 0);  /* msg id */
297         vnc_write_u8(vs, 0);
298         saved_offset = vs->output.offset;
299         vnc_write_u16(vs, 0);
300
301         for (y = 0; y < vs->height; y++) {
302             int x;
303             int last_x = -1;
304             for (x = 0; x < vs->width / 16; x++) {
305                 if (vs->dirty_row[y] & (1ULL << x)) {
306                     if (last_x == -1) {
307                         last_x = x;
308                     }
309                     vs->dirty_row[y] &= ~(1ULL << x);
310                 } else {
311                     if (last_x != -1) {
312                         int h = find_dirty_height(vs, y, last_x, x);
313                         send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
314                         n_rectangles++;
315                     }
316                     last_x = -1;
317                 }
318             }
319             if (last_x != -1) {
320                 int h = find_dirty_height(vs, y, last_x, x);
321                 send_framebuffer_update(vs, last_x * 16, y, (x - last_x) * 16, h);
322                 n_rectangles++;
323             }
324         }
325         vs->output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
326         vs->output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
327         vnc_flush(vs);
328
329     }
330     qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock) + VNC_REFRESH_INTERVAL);
331 }
332
333 static void vnc_timer_init(VncState *vs)
334 {
335     if (vs->timer == NULL) {
336         vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);
337         qemu_mod_timer(vs->timer, qemu_get_clock(rt_clock));
338     }
339 }
340
341 static void vnc_dpy_refresh(DisplayState *ds)
342 {
343     VncState *vs = ds->opaque;
344     vnc_timer_init(vs);
345     vga_hw_update();
346 }
347
348 static int vnc_listen_poll(void *opaque)
349 {
350     VncState *vs = opaque;
351     if (vs->csock == -1)
352         return 1;
353     return 0;
354 }
355
356 static void buffer_reserve(Buffer *buffer, size_t len)
357 {
358     if ((buffer->capacity - buffer->offset) < len) {
359         buffer->capacity += (len + 1024);
360         buffer->buffer = realloc(buffer->buffer, buffer->capacity);
361         if (buffer->buffer == NULL) {
362             fprintf(stderr, "vnc: out of memory\n");
363             exit(1);
364         }
365     }
366 }
367
368 static int buffer_empty(Buffer *buffer)
369 {
370     return buffer->offset == 0;
371 }
372
373 static char *buffer_end(Buffer *buffer)
374 {
375     return buffer->buffer + buffer->offset;
376 }
377
378 static void buffer_reset(Buffer *buffer)
379 {
380         buffer->offset = 0;
381 }
382
383 static void buffer_append(Buffer *buffer, const void *data, size_t len)
384 {
385     memcpy(buffer->buffer + buffer->offset, data, len);
386     buffer->offset += len;
387 }
388
389 static int vnc_client_io_error(VncState *vs, int ret)
390 {
391     if (ret == 0 || ret == -1) {
392         if (ret == -1 && (errno == EINTR || errno == EAGAIN))
393             return 0;
394
395         qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
396         close(vs->csock);
397         vs->csock = -1;
398         buffer_reset(&vs->input);
399         buffer_reset(&vs->output);
400         vs->need_update = 0;
401         return 0;
402     }
403     return ret;
404 }
405
406 static void vnc_client_error(VncState *vs)
407 {
408     errno = EINVAL;
409     vnc_client_io_error(vs, -1);
410 }
411
412 static void vnc_client_write(void *opaque)
413 {
414     ssize_t ret;
415     VncState *vs = opaque;
416
417     ret = write(vs->csock, vs->output.buffer, vs->output.offset);
418     ret = vnc_client_io_error(vs, ret);
419     if (!ret)
420         return;
421
422     memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
423     vs->output.offset -= ret;
424
425     if (vs->output.offset == 0) {
426         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
427     }
428 }
429
430 static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
431 {
432     vs->read_handler = func;
433     vs->read_handler_expect = expecting;
434 }
435
436 static void vnc_client_read(void *opaque)
437 {
438     VncState *vs = opaque;
439     ssize_t ret;
440
441     buffer_reserve(&vs->input, 4096);
442
443     ret = read(vs->csock, buffer_end(&vs->input), 4096);
444     ret = vnc_client_io_error(vs, ret);
445     if (!ret)
446         return;
447
448     vs->input.offset += ret;
449
450     while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
451         size_t len = vs->read_handler_expect;
452         int ret;
453
454         ret = vs->read_handler(vs, vs->input.buffer, len);
455         if (vs->csock == -1)
456             return;
457
458         if (!ret) {
459             memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
460             vs->input.offset -= len;
461         } else {
462             vs->read_handler_expect = ret;
463         }
464     }
465 }
466
467 static void vnc_write(VncState *vs, const void *data, size_t len)
468 {
469     buffer_reserve(&vs->output, len);
470
471     if (buffer_empty(&vs->output)) {
472         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
473     }
474
475     buffer_append(&vs->output, data, len);
476 }
477
478 static void vnc_write_s32(VncState *vs, int32_t value)
479 {
480     vnc_write_u32(vs, *(uint32_t *)&value);
481 }
482
483 static void vnc_write_u32(VncState *vs, uint32_t value)
484 {
485     uint8_t buf[4];
486
487     buf[0] = (value >> 24) & 0xFF;
488     buf[1] = (value >> 16) & 0xFF;
489     buf[2] = (value >>  8) & 0xFF;
490     buf[3] = value & 0xFF;
491
492     vnc_write(vs, buf, 4);
493 }
494
495 static void vnc_write_u16(VncState *vs, uint16_t value)
496 {
497     char buf[2];
498
499     buf[0] = (value >> 8) & 0xFF;
500     buf[1] = value & 0xFF;
501
502     vnc_write(vs, buf, 2);
503 }
504
505 static void vnc_write_u8(VncState *vs, uint8_t value)
506 {
507     vnc_write(vs, (char *)&value, 1);
508 }
509
510 static void vnc_flush(VncState *vs)
511 {
512     if (vs->output.offset)
513         vnc_client_write(vs);
514 }
515
516 static uint8_t read_u8(char *data, size_t offset)
517 {
518     return data[offset];
519 }
520
521 static uint16_t read_u16(char *data, size_t offset)
522 {
523     return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
524 }
525
526 static int32_t read_s32(char *data, size_t offset)
527 {
528     return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
529                      (data[offset + 2] << 8) | data[offset + 3]);
530 }
531
532 static uint32_t read_u32(char *data, size_t offset)
533 {
534     return ((data[offset] << 24) | (data[offset + 1] << 16) |
535             (data[offset + 2] << 8) | data[offset + 3]);
536 }
537
538 static void client_cut_text(VncState *vs, size_t len, char *text)
539 {
540 }
541
542 static void pointer_event(VncState *vs, int button_mask, int x, int y)
543 {
544     int buttons = 0;
545     int dz = 0;
546
547     if (button_mask & 0x01)
548         buttons |= MOUSE_EVENT_LBUTTON;
549     if (button_mask & 0x02)
550         buttons |= MOUSE_EVENT_MBUTTON;
551     if (button_mask & 0x04)
552         buttons |= MOUSE_EVENT_RBUTTON;
553     if (button_mask & 0x08)
554         dz = -1;
555     if (button_mask & 0x10)
556         dz = 1;
557             
558     if (kbd_mouse_is_absolute()) {
559         kbd_mouse_event(x * 0x7FFF / vs->ds->width,
560                         y * 0x7FFF / vs->ds->height,
561                         dz, buttons);
562     } else {
563         static int last_x = -1;
564         static int last_y = -1;
565
566         if (last_x != -1)
567             kbd_mouse_event(x - last_x, y - last_y, dz, buttons);
568
569         last_x = x;
570         last_y = y;
571     }
572 }
573
574 static void key_event(VncState *vs, int down, uint32_t sym)
575 {
576     int keycode;
577
578     keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF);
579
580     if (keycode & 0x80)
581         kbd_put_keycode(0xe0);
582     if (down)
583         kbd_put_keycode(keycode & 0x7f);
584     else
585         kbd_put_keycode(keycode | 0x80);
586 }
587
588 static void framebuffer_update_request(VncState *vs, int incremental,
589                                        int x_position, int y_position,
590                                        int w, int h)
591 {
592     int i;
593     vs->need_update = 1;
594     if (!incremental) {
595         char *old_row = vs->old_data + y_position * vs->ds->linesize;
596
597         for (i = 0; i < h; i++) {
598             vs->dirty_row[y_position + i] = (1ULL << (vs->ds->width / 16)) - 1;
599             if (vs->ds->width == 1024) {
600               vs->dirty_row[y_position + i] = ~(0ULL);
601             }
602             memset(old_row, 42, vs->ds->width * vs->depth);
603             old_row += vs->ds->linesize;
604         }
605     }
606 }
607
608 static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
609 {
610     int i;
611
612     vs->has_hextile = 0;
613     vs->has_resize = 0;
614     vs->ds->dpy_copy = NULL;
615
616     for (i = n_encodings - 1; i >= 0; i--) {
617         switch (encodings[i]) {
618         case 0: /* Raw */
619             vs->has_hextile = 0;
620             break;
621         case 1: /* CopyRect */
622             vs->ds->dpy_copy = vnc_copy;
623             break;
624         case 5: /* Hextile */
625             vs->has_hextile = 1;
626             break;
627         case -223: /* DesktopResize */
628             vs->has_resize = 1;
629             break;
630         default:
631             break;
632         }
633     }
634 }
635
636 static void set_pixel_format(VncState *vs,
637                              int bits_per_pixel, int depth,
638                              int big_endian_flag, int true_color_flag,
639                              int red_max, int green_max, int blue_max,
640                              int red_shift, int green_shift, int blue_shift)
641 {
642     switch (bits_per_pixel) {
643     case 32:
644     case 24:
645         vs->depth = 4;
646         break;
647     case 16:
648         vs->depth = 2;
649         break;
650     case 8:
651         vs->depth = 1;
652         break;
653     default:
654         vnc_client_error(vs);
655         break;
656     }
657
658     if (!true_color_flag)
659         vnc_client_error(vs);
660
661     vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height);
662     memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
663     memset(vs->old_data, 42, vs->ds->linesize * vs->ds->height);
664
665     vga_hw_invalidate();
666     vga_hw_update();
667 }
668
669 static int protocol_client_msg(VncState *vs, char *data, size_t len)
670 {
671     int i;
672     uint16_t limit;
673
674     switch (data[0]) {
675     case 0:
676         if (len == 1)
677             return 20;
678
679         set_pixel_format(vs, read_u8(data, 4), read_u8(data, 5),
680                          read_u8(data, 6), read_u8(data, 7),
681                          read_u16(data, 8), read_u16(data, 10),
682                          read_u16(data, 12), read_u8(data, 14),
683                          read_u8(data, 15), read_u8(data, 16));
684         break;
685     case 2:
686         if (len == 1)
687             return 4;
688
689         if (len == 4)
690             return 4 + (read_u16(data, 2) * 4);
691
692         limit = read_u16(data, 2);
693         for (i = 0; i < limit; i++) {
694             int32_t val = read_s32(data, 4 + (i * 4));
695             memcpy(data + 4 + (i * 4), &val, sizeof(val));
696         }
697
698         set_encodings(vs, (int32_t *)(data + 4), limit);
699         break;
700     case 3:
701         if (len == 1)
702             return 10;
703
704         framebuffer_update_request(vs,
705                                    read_u8(data, 1), read_u16(data, 2), read_u16(data, 4),
706                                    read_u16(data, 6), read_u16(data, 8));
707         break;
708     case 4:
709         if (len == 1)
710             return 8;
711
712         key_event(vs, read_u8(data, 1), read_u32(data, 4));
713         break;
714     case 5:
715         if (len == 1)
716             return 6;
717
718         pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4));
719         break;
720     case 6:
721         if (len == 1)
722             return 8;
723
724         if (len == 8)
725             return 8 + read_u32(data, 4);
726
727         client_cut_text(vs, read_u32(data, 4), data + 8);
728         break;
729     default:
730         printf("Msg: %d\n", data[0]);
731         vnc_client_error(vs);
732         break;
733     }
734         
735     vnc_read_when(vs, protocol_client_msg, 1);
736     return 0;
737 }
738
739 static int protocol_client_init(VncState *vs, char *data, size_t len)
740 {
741     char pad[3] = { 0, 0, 0 };
742
743     vs->width = vs->ds->width;
744     vs->height = vs->ds->height;
745     vnc_write_u16(vs, vs->ds->width);
746     vnc_write_u16(vs, vs->ds->height);
747
748     vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */
749     vnc_write_u8(vs, vs->depth * 8); /* depth */
750     vnc_write_u8(vs, 0);             /* big-endian-flag */
751     vnc_write_u8(vs, 1);             /* true-color-flag */
752     if (vs->depth == 4) {
753         vnc_write_u16(vs, 0xFF);     /* red-max */
754         vnc_write_u16(vs, 0xFF);     /* green-max */
755         vnc_write_u16(vs, 0xFF);     /* blue-max */
756         vnc_write_u8(vs, 16);        /* red-shift */
757         vnc_write_u8(vs, 8);         /* green-shift */
758         vnc_write_u8(vs, 0);         /* blue-shift */
759     } else if (vs->depth == 2) {
760         vnc_write_u16(vs, 31);       /* red-max */
761         vnc_write_u16(vs, 63);       /* green-max */
762         vnc_write_u16(vs, 31);       /* blue-max */
763         vnc_write_u8(vs, 11);        /* red-shift */
764         vnc_write_u8(vs, 5);         /* green-shift */
765         vnc_write_u8(vs, 0);         /* blue-shift */
766     } else if (vs->depth == 1) {
767         vnc_write_u16(vs, 3);        /* red-max */
768         vnc_write_u16(vs, 7);        /* green-max */
769         vnc_write_u16(vs, 3);        /* blue-max */
770         vnc_write_u8(vs, 5);         /* red-shift */
771         vnc_write_u8(vs, 2);         /* green-shift */
772         vnc_write_u8(vs, 0);         /* blue-shift */
773     }
774         
775     vnc_write(vs, pad, 3);           /* padding */
776
777     vnc_write_u32(vs, 4);        
778     vnc_write(vs, "QEMU", 4);
779     vnc_flush(vs);
780
781     vnc_read_when(vs, protocol_client_msg, 1);
782
783     return 0;
784 }
785
786 static int protocol_version(VncState *vs, char *version, size_t len)
787 {
788     char local[13];
789     int maj, min;
790
791     memcpy(local, version, 12);
792     local[12] = 0;
793
794     if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) {
795         vnc_client_error(vs);
796         return 0;
797     }
798
799     vnc_write_u32(vs, 1); /* None */
800     vnc_flush(vs);
801
802     vnc_read_when(vs, protocol_client_init, 1);
803
804     return 0;
805 }
806
807 static void vnc_listen_read(void *opaque)
808 {
809     VncState *vs = opaque;
810     struct sockaddr_in addr;
811     socklen_t addrlen = sizeof(addr);
812
813     vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
814     if (vs->csock != -1) {
815         fcntl(vs->csock, F_SETFL, O_NONBLOCK);
816         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque);
817         vnc_write(vs, "RFB 003.003\n", 12);
818         vnc_flush(vs);
819         vnc_read_when(vs, protocol_version, 12);
820         memset(vs->old_data, 0, vs->ds->linesize * vs->ds->height);
821         memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
822         vs->has_resize = 0;
823         vs->has_hextile = 0;
824         vs->ds->dpy_copy = NULL;
825     }
826 }
827
828 void vnc_display_init(DisplayState *ds, int display)
829 {
830     struct sockaddr_in addr;
831     int reuse_addr, ret;
832     VncState *vs;
833
834     vs = qemu_mallocz(sizeof(VncState));
835     if (!vs)
836         exit(1);
837
838     ds->opaque = vs;
839
840     vs->lsock = -1;
841     vs->csock = -1;
842     vs->depth = 4;
843
844     vs->ds = ds;
845
846     if (!keyboard_layout)
847         keyboard_layout = "en-us";
848
849     vs->kbd_layout = init_keyboard_layout(keyboard_layout);
850     if (!vs->kbd_layout)
851         exit(1);
852
853     vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
854     if (vs->lsock == -1) {
855         fprintf(stderr, "Could not create socket\n");
856         exit(1);
857     }
858
859     addr.sin_family = AF_INET;
860     addr.sin_port = htons(5900 + display);
861     memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
862
863     reuse_addr = 1;
864     ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
865                      &reuse_addr, sizeof(reuse_addr));
866     if (ret == -1) {
867         fprintf(stderr, "setsockopt() failed\n");
868         exit(1);
869     }
870
871     if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
872         fprintf(stderr, "bind() failed\n");
873         exit(1);
874     }
875
876     if (listen(vs->lsock, 1) == -1) {
877         fprintf(stderr, "listen() failed\n");
878         exit(1);
879     }
880
881     ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
882     if (ret == -1) {
883         exit(1);
884     }
885
886     vs->ds->data = NULL;
887     vs->ds->dpy_update = vnc_dpy_update;
888     vs->ds->dpy_resize = vnc_dpy_resize;
889     vs->ds->dpy_refresh = vnc_dpy_refresh;
890
891     memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
892
893     vnc_dpy_resize(vs->ds, 640, 400);
894 }