Fix interrupt controller address masking
[qemu] / vnc.c
diff --git a/vnc.c b/vnc.c
index 088f9cf..70d30d9 100644 (file)
--- a/vnc.c
+++ b/vnc.c
@@ -42,6 +42,18 @@ typedef struct VncState VncState;
 
 typedef int VncReadEvent(VncState *vs, char *data, size_t len);
 
+typedef void VncWritePixels(VncState *vs, void *data, int size);
+
+typedef void VncSendHextileTile(VncState *vs,
+                                int x, int y, int w, int h,
+                                uint32_t *last_bg, 
+                                uint32_t *last_fg,
+                                int *has_bg, int *has_fg);
+
+#define VNC_MAX_WIDTH 2048
+#define VNC_MAX_HEIGHT 2048
+#define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
+
 struct VncState
 {
     QEMUTimer *timer;
@@ -51,19 +63,53 @@ struct VncState
     int need_update;
     int width;
     int height;
-    uint64_t dirty_row[768];
+    uint32_t dirty_row[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS];
     char *old_data;
-    int depth;
+    int depth; /* internal VNC frame buffer byte per pixel */
     int has_resize;
     int has_hextile;
+    int has_pointer_type_change;
+    int absolute;
+    int last_x;
+    int last_y;
+
+    const char *display;
+
     Buffer output;
     Buffer input;
     kbd_layout_t *kbd_layout;
+    /* current output mode information */
+    VncWritePixels *write_pixels;
+    VncSendHextileTile *send_hextile_tile;
+    int pix_bpp, pix_big_endian;
+    int red_shift, red_max, red_shift1;
+    int green_shift, green_max, green_shift1;
+    int blue_shift, blue_max, blue_shift1;
 
     VncReadEvent *read_handler;
     size_t read_handler_expect;
+    /* input */
+    uint8_t modifiers_state[256];
 };
 
+static VncState *vnc_state; /* needed for info vnc */
+
+void do_info_vnc(void)
+{
+    if (vnc_state == NULL)
+       term_printf("VNC server disabled\n");
+    else {
+       term_printf("VNC server active on: ");
+       term_print_filename(vnc_state->display);
+       term_printf("\n");
+
+       if (vnc_state->csock == -1)
+           term_printf("No client connected\n");
+       else
+           term_printf("Client connected\n");
+    }
+}
+
 /* TODO
    1) Get the queue working for IO.
    2) there is some weirdness when using the -S option (the screen is grey
@@ -80,6 +126,47 @@ static void vnc_flush(VncState *vs);
 static void vnc_update_client(void *opaque);
 static void vnc_client_read(void *opaque);
 
+static inline void vnc_set_bit(uint32_t *d, int k)
+{
+    d[k >> 5] |= 1 << (k & 0x1f);
+}
+
+static inline void vnc_clear_bit(uint32_t *d, int k)
+{
+    d[k >> 5] &= ~(1 << (k & 0x1f));
+}
+
+static inline void vnc_set_bits(uint32_t *d, int n, int nb_words)
+{
+    int j;
+
+    j = 0;
+    while (n >= 32) {
+        d[j++] = -1;
+        n -= 32;
+    }
+    if (n > 0) 
+        d[j++] = (1 << n) - 1;
+    while (j < nb_words)
+        d[j++] = 0;
+}
+
+static inline int vnc_get_bit(const uint32_t *d, int k)
+{
+    return (d[k >> 5] >> (k & 0x1f)) & 1;
+}
+
+static inline int vnc_and_bits(const uint32_t *d1, const uint32_t *d2, 
+                               int nb_words)
+{
+    int i;
+    for(i = 0; i < nb_words; i++) {
+        if ((d1[i] & d2[i]) != 0)
+            return 1;
+    }
+    return 0;
+}
+
 static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
 {
     VncState *vs = ds->opaque;
@@ -89,7 +176,7 @@ static void vnc_dpy_update(DisplayState *ds, int x, int y, int w, int h)
 
     for (; y < h; y++)
        for (i = 0; i < w; i += 16)
-           vs->dirty_row[y] |= (1ULL << ((x + i) / 16));
+           vnc_set_bit(vs->dirty_row[y], (x + i) / 16);
 }
 
 static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
@@ -105,6 +192,7 @@ static void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h,
 
 static void vnc_dpy_resize(DisplayState *ds, int w, int h)
 {
+    int size_changed;
     VncState *vs = ds->opaque;
 
     ds->data = realloc(ds->data, w * h * vs->depth);
@@ -116,10 +204,11 @@ static void vnc_dpy_resize(DisplayState *ds, int w, int h)
     }
 
     ds->depth = vs->depth * 8;
+    size_changed = ds->width != w || ds->height != h;
     ds->width = w;
     ds->height = h;
     ds->linesize = w * vs->depth;
-    if (vs->csock != -1 && vs->has_resize) {
+    if (vs->csock != -1 && vs->has_resize && size_changed) {
        vnc_write_u8(vs, 0);  /* msg id */
        vnc_write_u8(vs, 0);
        vnc_write_u16(vs, 1); /* number of rects */
@@ -130,6 +219,66 @@ static void vnc_dpy_resize(DisplayState *ds, int w, int h)
     }
 }
 
+/* fastest code */
+static void vnc_write_pixels_copy(VncState *vs, void *pixels, int size)
+{
+    vnc_write(vs, pixels, size);
+}
+
+/* slowest but generic code. */
+static void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v)
+{
+    unsigned int r, g, b;
+
+    r = (v >> vs->red_shift1) & vs->red_max;
+    g = (v >> vs->green_shift1) & vs->green_max;
+    b = (v >> vs->blue_shift1) & vs->blue_max;
+    v = (r << vs->red_shift) | 
+        (g << vs->green_shift) | 
+        (b << vs->blue_shift);
+    switch(vs->pix_bpp) {
+    case 1:
+        buf[0] = v;
+        break;
+    case 2:
+        if (vs->pix_big_endian) {
+            buf[0] = v >> 8;
+            buf[1] = v;
+        } else {
+            buf[1] = v >> 8;
+            buf[0] = v;
+        }
+        break;
+    default:
+    case 4:
+        if (vs->pix_big_endian) {
+            buf[0] = v >> 24;
+            buf[1] = v >> 16;
+            buf[2] = v >> 8;
+            buf[3] = v;
+        } else {
+            buf[3] = v >> 24;
+            buf[2] = v >> 16;
+            buf[1] = v >> 8;
+            buf[0] = v;
+        }
+        break;
+    }
+}
+
+static void vnc_write_pixels_generic(VncState *vs, void *pixels1, int size)
+{
+    uint32_t *pixels = pixels1;
+    uint8_t buf[4];
+    int n, i;
+
+    n = size >> 2;
+    for(i = 0; i < n; i++) {
+        vnc_convert_pixel(vs, buf, pixels[i]);
+        vnc_write(vs, buf, vs->pix_bpp);
+    }
+}
+
 static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h)
 {
     int i;
@@ -139,7 +288,7 @@ static void send_framebuffer_update_raw(VncState *vs, int x, int y, int w, int h
 
     row = vs->ds->data + y * vs->ds->linesize + x * vs->depth;
     for (i = 0; i < h; i++) {
-       vnc_write(vs, row, w * vs->depth);
+       vs->write_pixels(vs, row, w * vs->depth);
        row += vs->ds->linesize;
     }
 }
@@ -162,35 +311,26 @@ static void hextile_enc_cord(uint8_t *ptr, int x, int y, int w, int h)
 #include "vnchextile.h"
 #undef BPP
 
+#define GENERIC
+#define BPP 32
+#include "vnchextile.h"
+#undef BPP
+#undef GENERIC
+
 static void send_framebuffer_update_hextile(VncState *vs, int x, int y, int w, int h)
 {
     int i, j;
     int has_fg, has_bg;
     uint32_t last_fg32, last_bg32;
-    uint16_t last_fg16, last_bg16;
-    uint8_t last_fg8, last_bg8;
 
     vnc_framebuffer_update(vs, x, y, w, h, 5);
 
     has_fg = has_bg = 0;
     for (j = y; j < (y + h); j += 16) {
        for (i = x; i < (x + w); i += 16) {
-           switch (vs->depth) {
-           case 1:
-               send_hextile_tile_8(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
-                                   &last_bg8, &last_fg8, &has_bg, &has_fg);
-               break;
-           case 2:
-               send_hextile_tile_16(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
-                                    &last_bg16, &last_fg16, &has_bg, &has_fg);
-               break;
-           case 4:
-               send_hextile_tile_32(vs, i, j, MIN(16, x + w - i), MIN(16, y + h - j),
-                                    &last_bg32, &last_fg32, &has_bg, &has_fg);
-               break;
-           default:
-               break;
-           }
+            vs->send_hextile_tile(vs, i, j, 
+                                  MIN(16, x + w - i), MIN(16, y + h - j),
+                                  &last_bg32, &last_fg32, &has_bg, &has_fg);
        }
     }
 }
@@ -250,10 +390,10 @@ static int find_dirty_height(VncState *vs, int y, int last_x, int x)
 
     for (h = 1; h < (vs->height - y); h++) {
        int tmp_x;
-       if (!(vs->dirty_row[y + h] & (1ULL << last_x)))
+       if (!vnc_get_bit(vs->dirty_row[y + h], last_x))
            break;
        for (tmp_x = last_x; tmp_x < x; tmp_x++)
-           vs->dirty_row[y + h] &= ~(1ULL << tmp_x);
+           vnc_clear_bit(vs->dirty_row[y + h], tmp_x);
     }
 
     return h;
@@ -267,15 +407,12 @@ static void vnc_update_client(void *opaque)
        int y;
        char *row;
        char *old_row;
-       uint64_t width_mask;
+       uint32_t width_mask[VNC_DIRTY_WORDS];
        int n_rectangles;
        int saved_offset;
        int has_dirty = 0;
 
-       width_mask = (1ULL << (vs->width / 16)) - 1;
-
-       if (vs->width == 1024)
-           width_mask = ~(0ULL);
+        vnc_set_bits(width_mask, (vs->width / 16), VNC_DIRTY_WORDS);
 
        /* Walk through the dirty map and eliminate tiles that
           really aren't dirty */
@@ -283,7 +420,7 @@ static void vnc_update_client(void *opaque)
        old_row = vs->old_data;
 
        for (y = 0; y < vs->height; y++) {
-           if (vs->dirty_row[y] & width_mask) {
+           if (vnc_and_bits(vs->dirty_row[y], width_mask, VNC_DIRTY_WORDS)) {
                int x;
                char *ptr, *old_ptr;
 
@@ -292,7 +429,7 @@ static void vnc_update_client(void *opaque)
 
                for (x = 0; x < vs->ds->width; x += 16) {
                    if (memcmp(old_ptr, ptr, 16 * vs->depth) == 0) {
-                       vs->dirty_row[y] &= ~(1ULL << (x / 16));
+                       vnc_clear_bit(vs->dirty_row[y], (x / 16));
                    } else {
                        has_dirty = 1;
                        memcpy(old_ptr, ptr, 16 * vs->depth);
@@ -323,11 +460,11 @@ static void vnc_update_client(void *opaque)
            int x;
            int last_x = -1;
            for (x = 0; x < vs->width / 16; x++) {
-               if (vs->dirty_row[y] & (1ULL << x)) {
+               if (vnc_get_bit(vs->dirty_row[y], x)) {
                    if (last_x == -1) {
                        last_x = x;
                    }
-                   vs->dirty_row[y] &= ~(1ULL << x);
+                   vnc_clear_bit(vs->dirty_row[y], x);
                } else {
                    if (last_x != -1) {
                        int h = find_dirty_height(vs, y, last_x, x);
@@ -431,7 +568,7 @@ static void vnc_client_error(VncState *vs)
 
 static void vnc_client_write(void *opaque)
 {
-    ssize_t ret;
+    long ret;
     VncState *vs = opaque;
 
     ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
@@ -456,7 +593,7 @@ static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
 static void vnc_client_read(void *opaque)
 {
     VncState *vs = opaque;
-    ssize_t ret;
+    long ret;
 
     buffer_reserve(&vs->input, 4096);
 
@@ -514,7 +651,7 @@ static void vnc_write_u32(VncState *vs, uint32_t value)
 
 static void vnc_write_u16(VncState *vs, uint16_t value)
 {
-    char buf[2];
+    uint8_t buf[2];
 
     buf[0] = (value >> 8) & 0xFF;
     buf[1] = value & 0xFF;
@@ -533,23 +670,23 @@ static void vnc_flush(VncState *vs)
        vnc_client_write(vs);
 }
 
-static uint8_t read_u8(char *data, size_t offset)
+static uint8_t read_u8(uint8_t *data, size_t offset)
 {
     return data[offset];
 }
 
-static uint16_t read_u16(char *data, size_t offset)
+static uint16_t read_u16(uint8_t *data, size_t offset)
 {
     return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
 }
 
-static int32_t read_s32(char *data, size_t offset)
+static int32_t read_s32(uint8_t *data, size_t offset)
 {
     return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
                     (data[offset + 2] << 8) | data[offset + 3]);
 }
 
-static uint32_t read_u32(char *data, size_t offset)
+static uint32_t read_u32(uint8_t *data, size_t offset)
 {
     return ((data[offset] << 24) | (data[offset + 1] << 16) |
            (data[offset + 2] << 8) | data[offset + 3]);
@@ -559,6 +696,19 @@ static void client_cut_text(VncState *vs, size_t len, char *text)
 {
 }
 
+static void check_pointer_type_change(VncState *vs, int absolute)
+{
+    if (vs->has_pointer_type_change && vs->absolute != absolute) {
+       vnc_write_u8(vs, 0);
+       vnc_write_u8(vs, 0);
+       vnc_write_u16(vs, 1);
+       vnc_framebuffer_update(vs, absolute, 0,
+                              vs->ds->width, vs->ds->height, -257);
+       vnc_flush(vs);
+    }
+    vs->absolute = absolute;
+}
+
 static void pointer_event(VncState *vs, int button_mask, int x, int y)
 {
     int buttons = 0;
@@ -574,20 +724,38 @@ static void pointer_event(VncState *vs, int button_mask, int x, int y)
        dz = -1;
     if (button_mask & 0x10)
        dz = 1;
-           
-    if (kbd_mouse_is_absolute()) {
+
+    if (vs->absolute) {
        kbd_mouse_event(x * 0x7FFF / vs->ds->width,
                        y * 0x7FFF / vs->ds->height,
                        dz, buttons);
+    } else if (vs->has_pointer_type_change) {
+       x -= 0x7FFF;
+       y -= 0x7FFF;
+
+       kbd_mouse_event(x, y, dz, buttons);
     } else {
-       static int last_x = -1;
-       static int last_y = -1;
+       if (vs->last_x != -1)
+           kbd_mouse_event(x - vs->last_x,
+                           y - vs->last_y,
+                           dz, buttons);
+       vs->last_x = x;
+       vs->last_y = y;
+    }
 
-       if (last_x != -1)
-           kbd_mouse_event(x - last_x, y - last_y, dz, buttons);
+    check_pointer_type_change(vs, kbd_mouse_is_absolute());
+}
 
-       last_x = x;
-       last_y = y;
+static void reset_keys(VncState *vs)
+{
+    int i;
+    for(i = 0; i < 256; i++) {
+        if (vs->modifiers_state[i]) {
+            if (i & 0x80)
+                kbd_put_keycode(0xe0);
+            kbd_put_keycode(i | 0x80);
+            vs->modifiers_state[i] = 0;
+        }
     }
 }
 
@@ -596,13 +764,81 @@ static void do_key_event(VncState *vs, int down, uint32_t sym)
     int keycode;
 
     keycode = keysym2scancode(vs->kbd_layout, sym & 0xFFFF);
+    
+    /* QEMU console switch */
+    switch(keycode) {
+    case 0x2a:                          /* Left Shift */
+    case 0x36:                          /* Right Shift */
+    case 0x1d:                          /* Left CTRL */
+    case 0x9d:                          /* Right CTRL */
+    case 0x38:                          /* Left ALT */
+    case 0xb8:                          /* Right ALT */
+        if (down)
+            vs->modifiers_state[keycode] = 1;
+        else
+            vs->modifiers_state[keycode] = 0;
+        break;
+    case 0x02 ... 0x0a: /* '1' to '9' keys */ 
+        if (down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) {
+            /* Reset the modifiers sent to the current console */
+            reset_keys(vs);
+            console_select(keycode - 0x02);
+            return;
+        }
+        break;
+    }
 
-    if (keycode & 0x80)
-       kbd_put_keycode(0xe0);
-    if (down)
-       kbd_put_keycode(keycode & 0x7f);
-    else
-       kbd_put_keycode(keycode | 0x80);
+    if (is_graphic_console()) {
+        if (keycode & 0x80)
+            kbd_put_keycode(0xe0);
+        if (down)
+            kbd_put_keycode(keycode & 0x7f);
+        else
+            kbd_put_keycode(keycode | 0x80);
+    } else {
+        /* QEMU console emulation */
+        if (down) {
+            switch (keycode) {
+            case 0x2a:                          /* Left Shift */
+            case 0x36:                          /* Right Shift */
+            case 0x1d:                          /* Left CTRL */
+            case 0x9d:                          /* Right CTRL */
+            case 0x38:                          /* Left ALT */
+            case 0xb8:                          /* Right ALT */
+                break;
+            case 0xc8:
+                kbd_put_keysym(QEMU_KEY_UP);
+                break;
+            case 0xd0:
+                kbd_put_keysym(QEMU_KEY_DOWN);
+                break;
+            case 0xcb:
+                kbd_put_keysym(QEMU_KEY_LEFT);
+                break;
+            case 0xcd:
+                kbd_put_keysym(QEMU_KEY_RIGHT);
+                break;
+            case 0xd3:
+                kbd_put_keysym(QEMU_KEY_DELETE);
+                break;
+            case 0xc7:
+                kbd_put_keysym(QEMU_KEY_HOME);
+                break;
+            case 0xcf:
+                kbd_put_keysym(QEMU_KEY_END);
+                break;
+            case 0xc9:
+                kbd_put_keysym(QEMU_KEY_PAGEUP);
+                break;
+            case 0xd1:
+                kbd_put_keysym(QEMU_KEY_PAGEDOWN);
+                break;
+            default:
+                kbd_put_keysym(sym);
+                break;
+            }
+        }
+    }
 }
 
 static void key_event(VncState *vs, int down, uint32_t sym)
@@ -616,16 +852,23 @@ static void framebuffer_update_request(VncState *vs, int incremental,
                                       int x_position, int y_position,
                                       int w, int h)
 {
+    if (x_position > vs->ds->width)
+        x_position = vs->ds->width;
+    if (y_position > vs->ds->height)
+        y_position = vs->ds->height;
+    if (x_position + w >= vs->ds->width)
+        w = vs->ds->width  - x_position;
+    if (y_position + h >= vs->ds->height)
+        h = vs->ds->height - y_position;
+
     int i;
     vs->need_update = 1;
     if (!incremental) {
        char *old_row = vs->old_data + y_position * vs->ds->linesize;
 
        for (i = 0; i < h; i++) {
-           vs->dirty_row[y_position + i] = (1ULL << (vs->ds->width / 16)) - 1;
-           if (vs->ds->width == 1024) {
-             vs->dirty_row[y_position + i] = ~(0ULL);
-           }
+            vnc_set_bits(vs->dirty_row[y_position + i], 
+                         (vs->ds->width / 16), VNC_DIRTY_WORDS);
            memset(old_row, 42, vs->ds->width * vs->depth);
            old_row += vs->ds->linesize;
        }
@@ -638,6 +881,8 @@ static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
 
     vs->has_hextile = 0;
     vs->has_resize = 0;
+    vs->has_pointer_type_change = 0;
+    vs->absolute = -1;
     vs->ds->dpy_copy = NULL;
 
     for (i = n_encodings - 1; i >= 0; i--) {
@@ -654,10 +899,26 @@ static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
        case -223: /* DesktopResize */
            vs->has_resize = 1;
            break;
+       case -257:
+           vs->has_pointer_type_change = 1;
+           break;
        default:
            break;
        }
     }
+
+    check_pointer_type_change(vs, kbd_mouse_is_absolute());
+}
+
+static int compute_nbits(unsigned int val)
+{
+    int n;
+    n = 0;
+    while (val != 0) {
+        n++;
+        val >>= 1;
+    }
+    return n;
 }
 
 static void set_pixel_format(VncState *vs,
@@ -666,24 +927,62 @@ static void set_pixel_format(VncState *vs,
                             int red_max, int green_max, int blue_max,
                             int red_shift, int green_shift, int blue_shift)
 {
-    switch (bits_per_pixel) {
-    case 32:
-    case 24:
-       vs->depth = 4;
-       break;
-    case 16:
-       vs->depth = 2;
-       break;
-    case 8:
-       vs->depth = 1;
-       break;
-    default:
+    int host_big_endian_flag;
+
+#ifdef WORDS_BIGENDIAN
+    host_big_endian_flag = 1;
+#else
+    host_big_endian_flag = 0;
+#endif
+    if (!true_color_flag) {
+    fail:
        vnc_client_error(vs);
-       break;
+        return;
+    }
+    if (bits_per_pixel == 32 && 
+        host_big_endian_flag == big_endian_flag &&
+        red_max == 0xff && green_max == 0xff && blue_max == 0xff &&
+        red_shift == 16 && green_shift == 8 && blue_shift == 0) {
+        vs->depth = 4;
+        vs->write_pixels = vnc_write_pixels_copy;
+        vs->send_hextile_tile = send_hextile_tile_32;
+    } else 
+    if (bits_per_pixel == 16 && 
+        host_big_endian_flag == big_endian_flag &&
+        red_max == 31 && green_max == 63 && blue_max == 31 &&
+        red_shift == 11 && green_shift == 5 && blue_shift == 0) {
+        vs->depth = 2;
+        vs->write_pixels = vnc_write_pixels_copy;
+        vs->send_hextile_tile = send_hextile_tile_16;
+    } else 
+    if (bits_per_pixel == 8 && 
+        red_max == 7 && green_max == 7 && blue_max == 3 &&
+        red_shift == 5 && green_shift == 2 && blue_shift == 0) {
+        vs->depth = 1;
+        vs->write_pixels = vnc_write_pixels_copy;
+        vs->send_hextile_tile = send_hextile_tile_8;
+    } else 
+    {
+        /* generic and slower case */
+        if (bits_per_pixel != 8 &&
+            bits_per_pixel != 16 &&
+            bits_per_pixel != 32)
+            goto fail;
+        vs->depth = 4;
+        vs->red_shift = red_shift;
+        vs->red_max = red_max;
+        vs->red_shift1 = 24 - compute_nbits(red_max);
+        vs->green_shift = green_shift;
+        vs->green_max = green_max;
+        vs->green_shift1 = 16 - compute_nbits(green_max);
+        vs->blue_shift = blue_shift;
+        vs->blue_max = blue_max;
+        vs->blue_shift1 = 8 - compute_nbits(blue_max);
+        vs->pix_bpp = bits_per_pixel / 8;
+        vs->pix_big_endian = big_endian_flag;
+        vs->write_pixels = vnc_write_pixels_generic;
+        vs->send_hextile_tile = send_hextile_tile_generic;
     }
-
-    if (!true_color_flag)
-       vnc_client_error(vs);
 
     vnc_dpy_resize(vs->ds, vs->ds->width, vs->ds->height);
     memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
@@ -766,6 +1065,8 @@ static int protocol_client_msg(VncState *vs, char *data, size_t len)
 static int protocol_client_init(VncState *vs, char *data, size_t len)
 {
     char pad[3] = { 0, 0, 0 };
+    char buf[1024];
+    int size;
 
     vs->width = vs->ds->width;
     vs->height = vs->ds->height;
@@ -774,7 +1075,11 @@ static int protocol_client_init(VncState *vs, char *data, size_t len)
 
     vnc_write_u8(vs, vs->depth * 8); /* bits-per-pixel */
     vnc_write_u8(vs, vs->depth * 8); /* depth */
+#ifdef WORDS_BIGENDIAN
+    vnc_write_u8(vs, 1);             /* big-endian-flag */
+#else
     vnc_write_u8(vs, 0);             /* big-endian-flag */
+#endif
     vnc_write_u8(vs, 1);             /* true-color-flag */
     if (vs->depth == 4) {
        vnc_write_u16(vs, 0xFF);     /* red-max */
@@ -783,6 +1088,7 @@ static int protocol_client_init(VncState *vs, char *data, size_t len)
        vnc_write_u8(vs, 16);        /* red-shift */
        vnc_write_u8(vs, 8);         /* green-shift */
        vnc_write_u8(vs, 0);         /* blue-shift */
+        vs->send_hextile_tile = send_hextile_tile_32;
     } else if (vs->depth == 2) {
        vnc_write_u16(vs, 31);       /* red-max */
        vnc_write_u16(vs, 63);       /* green-max */
@@ -790,19 +1096,28 @@ static int protocol_client_init(VncState *vs, char *data, size_t len)
        vnc_write_u8(vs, 11);        /* red-shift */
        vnc_write_u8(vs, 5);         /* green-shift */
        vnc_write_u8(vs, 0);         /* blue-shift */
+        vs->send_hextile_tile = send_hextile_tile_16;
     } else if (vs->depth == 1) {
-       vnc_write_u16(vs, 3);        /* red-max */
+        /* XXX: change QEMU pixel 8 bit pixel format to match the VNC one ? */
+       vnc_write_u16(vs, 7);        /* red-max */
        vnc_write_u16(vs, 7);        /* green-max */
        vnc_write_u16(vs, 3);        /* blue-max */
        vnc_write_u8(vs, 5);         /* red-shift */
        vnc_write_u8(vs, 2);         /* green-shift */
        vnc_write_u8(vs, 0);         /* blue-shift */
+        vs->send_hextile_tile = send_hextile_tile_8;
     }
+    vs->write_pixels = vnc_write_pixels_copy;
        
     vnc_write(vs, pad, 3);           /* padding */
 
-    vnc_write_u32(vs, 4);        
-    vnc_write(vs, "QEMU", 4);
+    if (qemu_name)
+        size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
+    else
+        size = snprintf(buf, sizeof(buf), "QEMU");
+
+    vnc_write_u32(vs, size);
+    vnc_write(vs, buf, size);
     vnc_flush(vs);
 
     vnc_read_when(vs, protocol_client_msg, 1);
@@ -852,10 +1167,18 @@ static void vnc_listen_read(void *opaque)
     }
 }
 
-void vnc_display_init(DisplayState *ds, int display)
+extern int parse_host_port(struct sockaddr_in *saddr, const char *str);
+
+void vnc_display_init(DisplayState *ds, const char *arg)
 {
-    struct sockaddr_in addr;
+    struct sockaddr *addr;
+    struct sockaddr_in iaddr;
+#ifndef _WIN32
+    struct sockaddr_un uaddr;
+#endif
     int reuse_addr, ret;
+    socklen_t addrlen;
+    const char *p;
     VncState *vs;
 
     vs = qemu_mallocz(sizeof(VncState));
@@ -863,10 +1186,14 @@ void vnc_display_init(DisplayState *ds, int display)
        exit(1);
 
     ds->opaque = vs;
+    vnc_state = vs;
+    vs->display = arg;
 
     vs->lsock = -1;
     vs->csock = -1;
     vs->depth = 4;
+    vs->last_x = -1;
+    vs->last_y = -1;
 
     vs->ds = ds;
 
@@ -877,25 +1204,60 @@ void vnc_display_init(DisplayState *ds, int display)
     if (!vs->kbd_layout)
        exit(1);
 
-    vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
-    if (vs->lsock == -1) {
-       fprintf(stderr, "Could not create socket\n");
-       exit(1);
-    }
+    vs->ds->data = NULL;
+    vs->ds->dpy_update = vnc_dpy_update;
+    vs->ds->dpy_resize = vnc_dpy_resize;
+    vs->ds->dpy_refresh = vnc_dpy_refresh;
 
-    addr.sin_family = AF_INET;
-    addr.sin_port = htons(5900 + display);
-    memset(&addr.sin_addr, 0, sizeof(addr.sin_addr));
+    memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
 
-    reuse_addr = 1;
-    ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
-                    (const char *)&reuse_addr, sizeof(reuse_addr));
-    if (ret == -1) {
-       fprintf(stderr, "setsockopt() failed\n");
-       exit(1);
+    vnc_dpy_resize(vs->ds, 640, 400);
+
+#ifndef _WIN32
+    if (strstart(arg, "unix:", &p)) {
+       addr = (struct sockaddr *)&uaddr;
+       addrlen = sizeof(uaddr);
+
+       vs->lsock = socket(PF_UNIX, SOCK_STREAM, 0);
+       if (vs->lsock == -1) {
+           fprintf(stderr, "Could not create socket\n");
+           exit(1);
+       }
+
+       uaddr.sun_family = AF_UNIX;
+       memset(uaddr.sun_path, 0, 108);
+       snprintf(uaddr.sun_path, 108, "%s", p);
+
+       unlink(uaddr.sun_path);
+    } else
+#endif
+    {
+       addr = (struct sockaddr *)&iaddr;
+       addrlen = sizeof(iaddr);
+
+       vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
+       if (vs->lsock == -1) {
+           fprintf(stderr, "Could not create socket\n");
+           exit(1);
+       }
+
+       if (parse_host_port(&iaddr, arg) < 0) {
+           fprintf(stderr, "Could not parse VNC address\n");
+           exit(1);
+       }
+           
+       iaddr.sin_port = htons(ntohs(iaddr.sin_port) + 5900);
+
+       reuse_addr = 1;
+       ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
+                        (const char *)&reuse_addr, sizeof(reuse_addr));
+       if (ret == -1) {
+           fprintf(stderr, "setsockopt() failed\n");
+           exit(1);
+       }
     }
 
-    if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
+    if (bind(vs->lsock, addr, addrlen) == -1) {
        fprintf(stderr, "bind() failed\n");
        exit(1);
     }
@@ -909,13 +1271,4 @@ void vnc_display_init(DisplayState *ds, int display)
     if (ret == -1) {
        exit(1);
     }
-
-    vs->ds->data = NULL;
-    vs->ds->dpy_update = vnc_dpy_update;
-    vs->ds->dpy_resize = vnc_dpy_resize;
-    vs->ds->dpy_refresh = vnc_dpy_refresh;
-
-    memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
-
-    vnc_dpy_resize(vs->ds, 640, 400);
 }