]> git.proxmox.com Git - spiceterm.git/blobdiff - spiceterm.c
make clipboard fully functional
[spiceterm.git] / spiceterm.c
index c3c7b7e8057a90309bf36707e1ffaf96705a1fc1..afbda2b3c06fa3744459ca2f4f5a9ab3ba7cb53a 100644 (file)
@@ -79,6 +79,9 @@ unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
                                8,12,10,14, 9,13,11,15 };
 
 
+static void vdagent_grab_clipboard(spiceTerm *vt, uint8_t selection);
+static void vdagent_request_clipboard(spiceTerm *vt, uint8_t selection);
+
 static void
 print_usage(const char *msg)
 {
@@ -86,34 +89,8 @@ print_usage(const char *msg)
     fprintf(stderr, "USAGE: spiceterm [spiceopts] [-c command [args]]\n");
 }
 
-/* Convert UCS2 to UTF8 sequence, trailing zero */
-/*
-static int
-ucs2_to_utf8 (gunichar2 c, char *out)
-{
-  if (c < 0x80) {
-    out[0] = c;                        //  0*******
-    out[1] = 0;
-    return 1;
-  } else if (c < 0x800) {
-    out[0] = 0xc0 | (c >> 6);  //  110***** 10******
-    out[1] = 0x80 | (c & 0x3f);
-    out[2] = 0;
-    return 2;
-  } else {
-    out[0] = 0xe0 | (c >> 12);         //  1110**** 10****** 10******
-    out[1] = 0x80 | ((c >> 6) & 0x3f);
-    out[2] = 0x80 | (c & 0x3f);
-    out[3] = 0;
-    return 3;
-  }
-
-  return 0;
-}
-*/
-
 static void
-draw_char_at (spiceTerm *vt, int x, int y, gunichar2 ch, TextAttributes attrib)
+draw_char_at(spiceTerm *vt, int x, int y, gunichar2 ch, TextAttributes attrib)
 {
     if (x < 0 || y < 0 || x >= vt->width || y >= vt->height) {
         return;
@@ -123,7 +100,7 @@ draw_char_at (spiceTerm *vt, int x, int y, gunichar2 ch, TextAttributes attrib)
 }
 
 static void
-spiceterm_update_xy (spiceTerm *vt, int x, int y)
+spiceterm_update_xy(spiceTerm *vt, int x, int y)
 {
     if (x < 0 || y < 0 || x >= vt->width || y >= vt->height) { return; }
 
@@ -134,12 +111,12 @@ spiceterm_update_xy (spiceTerm *vt, int x, int y)
     }
     if (y2 < vt->height) {
         TextCell *c = &vt->cells[y1 * vt->width + x];
-        draw_char_at (vt, x, y2, c->ch, c->attrib);
+        draw_char_at(vt, x, y2, c->ch, c->attrib);
     }
 }
 
 static void
-spiceterm_clear_xy (spiceTerm *vt, int x, int y)
+spiceterm_clear_xy(spiceTerm *vt, int x, int y)
 {
     if (x < 0 || y < 0 || x >= vt->width || y >= vt->height) { return; }
 
@@ -155,12 +132,30 @@ spiceterm_clear_xy (spiceTerm *vt, int x, int y)
         c->attrib.fgcol = vt->cur_attrib.fgcol;
         c->attrib.bgcol = vt->cur_attrib.bgcol;
 
-        draw_char_at (vt, x, y, c->ch, c->attrib);
+        draw_char_at(vt, x, y, c->ch, c->attrib);
+    }
+}
+
+void
+spiceterm_toggle_marked_cell(spiceTerm *vt, int pos)
+{
+    int x = (pos%vt->width);
+    int y = (pos/vt->width);
+
+    if (x < 0 || y < 0 || x >= vt->width || y >= vt->height) { return; }
+
+    int y1 = (vt->y_displ + y) % vt->total_height;
+
+    TextCell *c = &vt->cells[y1 * vt->width + x];
+    c->attrib.selected =  c->attrib.selected ? 0 : 1;
+
+    if (y < vt->height) {
+        draw_char_at(vt, x, y, c->ch, c->attrib);
     }
 }
 
 static void
-spiceterm_show_cursor (spiceTerm *vt, int show)
+spiceterm_show_cursor(spiceTerm *vt, int show)
 {
     int x = vt->cx;
     if (x >= vt->width) {
@@ -180,15 +175,15 @@ spiceterm_show_cursor (spiceTerm *vt, int show)
         if (show) {
             TextAttributes attrib = vt->default_attrib;
             attrib.invers = !(attrib.invers); /* invert fg and bg */
-            draw_char_at (vt, x, y, c->ch, attrib);
+            draw_char_at(vt, x, y, c->ch, attrib);
         } else {
-            draw_char_at (vt, x, y, c->ch, c->attrib);
+            draw_char_at(vt, x, y, c->ch, c->attrib);
         }
     }
 }
 
 static void
-spiceterm_refresh (spiceTerm *vt)
+spiceterm_refresh(spiceTerm *vt)
 {
     int x, y, y1;
 
@@ -196,18 +191,32 @@ spiceterm_refresh (spiceTerm *vt)
     for(y = 0; y < vt->height; y++) {
         TextCell *c = vt->cells + y1 * vt->width;
         for(x = 0; x < vt->width; x++) {
-            draw_char_at (vt, x, y, c->ch, c->attrib);
+            draw_char_at(vt, x, y, c->ch, c->attrib);
             c++;
         }
         if (++y1 == vt->total_height)
             y1 = 0;
     }
 
-    spiceterm_show_cursor (vt, 1);
+    spiceterm_show_cursor(vt, 1);
+}
+
+void
+spiceterm_unselect_all(spiceTerm *vt)
+{
+    int i;
+
+    for (i = 0; i < vt->width*vt->total_height; i++) {
+        if (vt->cells[i].attrib.selected) {
+            vt->cells[i].attrib.selected = 0;
+        }
+    }
+
+    spiceterm_refresh(vt);
 }
 
 static void
-spiceterm_scroll_down (spiceTerm *vt, int top, int bottom, int lines)
+spiceterm_scroll_down(spiceTerm *vt, int top, int bottom, int lines)
 {
     if ((top + lines) >= bottom) {
         lines = bottom - top -1;
@@ -222,7 +231,7 @@ spiceterm_scroll_down (spiceTerm *vt, int top, int bottom, int lines)
         int src = ((vt->y_base + top + i) % vt->total_height)*vt->width;
         int dst = ((vt->y_base + top + lines + i) % vt->total_height)*vt->width;
 
-        memmove(vt->cells + dst, vt->cells + src, vt->width*sizeof (TextCell));
+        memmove(vt->cells + dst, vt->cells + src, vt->width*sizeof(TextCell));
     }
 
     for (i = 0; i < lines; i++) {
@@ -245,7 +254,7 @@ spiceterm_scroll_down (spiceTerm *vt, int top, int bottom, int lines)
 }
 
 static void
-spiceterm_scroll_up (spiceTerm *vt, int top, int bottom, int lines, int moveattr)
+spiceterm_scroll_up(spiceTerm *vt, int top, int bottom, int lines, int moveattr)
 {
     if ((top + lines) >= bottom) {
         lines = bottom - top - 1;
@@ -275,7 +284,7 @@ spiceterm_scroll_up (spiceTerm *vt, int top, int bottom, int lines, int moveattr
         int dst = ((vt->y_base + top + i) % vt->total_height)*vt->width;
         int src = ((vt->y_base + top + lines + i) % vt->total_height)*vt->width;
 
-        memmove(vt->cells + dst, vt->cells + src, vt->width*sizeof (TextCell));
+        memmove(vt->cells + dst, vt->cells + src, vt->width*sizeof(TextCell));
     }
 
     for (i = 1; i <= lines; i++) {
@@ -290,7 +299,7 @@ spiceterm_scroll_up (spiceTerm *vt, int top, int bottom, int lines, int moveattr
 }
 
 static void
-spiceterm_virtual_scroll (spiceTerm *vt, int lines)
+spiceterm_virtual_scroll(spiceTerm *vt, int lines)
 {
     if (vt->altbuf || lines == 0) return;
 
@@ -318,13 +327,13 @@ spiceterm_virtual_scroll (spiceTerm *vt, int lines)
         }
     }
 
-    spiceterm_refresh (vt);
+    spiceterm_refresh(vt);
 }
 
 static void
-spiceterm_respond_esc (spiceTerm *vt, const char *esc)
+spiceterm_respond_esc(spiceTerm *vt, const char *esc)
 {
-    int len = strlen (esc);
+    int len = strlen(esc);
     int i;
 
     if (vt->ibuf_count < (IBUFSIZE - 1 - len)) {
@@ -336,17 +345,17 @@ spiceterm_respond_esc (spiceTerm *vt, const char *esc)
 }
 
 static void
-spiceterm_put_lf (spiceTerm *vt)
+spiceterm_put_lf(spiceTerm *vt)
 {
     if (vt->cy + 1 == vt->region_bottom) {
 
         if (vt->altbuf || vt->region_top != 0 || vt->region_bottom != vt->height) {
-            spiceterm_scroll_up (vt, vt->region_top, vt->region_bottom, 1, 1);
+            spiceterm_scroll_up(vt, vt->region_top, vt->region_bottom, 1, 1);
             return;
         }
 
         if (vt->y_displ == vt->y_base) {
-            spiceterm_scroll_up (vt, vt->region_top, vt->region_bottom, 1, 0);
+            spiceterm_scroll_up(vt, vt->region_top, vt->region_bottom, 1, 0);
         }
 
         if (vt->y_displ == vt->y_base) {
@@ -380,7 +389,7 @@ spiceterm_put_lf (spiceTerm *vt)
 }
 
 static void
-spiceterm_csi_m (spiceTerm *vt)
+spiceterm_csi_m(spiceTerm *vt)
 {
     int i;
 
@@ -478,7 +487,7 @@ spiceterm_csi_m (spiceTerm *vt)
 }
 
 static void
-spiceterm_save_cursor (spiceTerm *vt)
+spiceterm_save_cursor(spiceTerm *vt)
 {
     vt->cx_saved = vt->cx;
     vt->cy_saved = vt->cy;
@@ -490,7 +499,7 @@ spiceterm_save_cursor (spiceTerm *vt)
 }
 
 static void
-spiceterm_restore_cursor (spiceTerm *vt)
+spiceterm_restore_cursor(spiceTerm *vt)
 {
     vt->cx = vt->cx_saved;
     vt->cy = vt->cy_saved;
@@ -502,7 +511,7 @@ spiceterm_restore_cursor (spiceTerm *vt)
 }
 
 static void
-spiceterm_set_alternate_buffer (spiceTerm *vt, int on_off)
+spiceterm_set_alternate_buffer(spiceTerm *vt, int on_off)
 {
     int x, y;
 
@@ -516,7 +525,7 @@ spiceterm_set_alternate_buffer (spiceTerm *vt, int on_off)
 
         /* alternate buffer & cursor */
 
-        spiceterm_save_cursor (vt);
+        spiceterm_save_cursor(vt);
         /* save screen to altcels */
         for (y = 0; y < vt->height; y++) {
             int y1 = (vt->y_base + y) % vt->total_height;
@@ -528,7 +537,7 @@ spiceterm_set_alternate_buffer (spiceTerm *vt, int on_off)
         /* clear screen */
         for (y = 0; y <= vt->height; y++) {
             for (x = 0; x < vt->width; x++) {
-                spiceterm_clear_xy (vt, x, y);
+                spiceterm_clear_xy(vt, x, y);
             }
         }
 
@@ -546,14 +555,14 @@ spiceterm_set_alternate_buffer (spiceTerm *vt, int on_off)
             }
         }
 
-        spiceterm_restore_cursor (vt);
+        spiceterm_restore_cursor(vt);
     }
 
-    spiceterm_refresh (vt);
+    spiceterm_refresh(vt);
 }
 
 static void
-spiceterm_set_mode (spiceTerm *vt, int on_off)
+spiceterm_set_mode(spiceTerm *vt, int on_off)
 {
     int i;
 
@@ -586,7 +595,7 @@ spiceterm_set_mode (spiceTerm *vt, int on_off)
 }
 
 static void
-spiceterm_gotoxy (spiceTerm *vt, int x, int y)
+spiceterm_gotoxy(spiceTerm *vt, int x, int y)
 {
     /* verify all boundaries */
 
@@ -611,7 +620,7 @@ spiceterm_gotoxy (spiceTerm *vt, int x, int y)
     vt->cy = y;
 }
 
-static void 
+static void
 debug_print_escape_buffer(spiceTerm *vt, const char *func, const char *prefix,
                           const char *qes, gunichar2 ch)
 {
@@ -636,7 +645,7 @@ enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
        ESpalette, ESidquery, ESosc1, ESosc2};
 
 static void
-spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
+spiceterm_putchar(spiceTerm *vt, gunichar2 ch)
 {
     int x, y, i, c;
 
@@ -659,10 +668,10 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
             vt->tty_state = ESpercent;
             break;
         case '7':
-            spiceterm_save_cursor (vt);
+            spiceterm_save_cursor(vt);
             break;
         case '8':
-            spiceterm_restore_cursor (vt);
+            spiceterm_restore_cursor(vt);
             break;
         case '(':
             vt->tty_state = ESsetG0; // SET G0
@@ -673,7 +682,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
         case 'M':
             /* cursor up (ri) */
             if (vt->cy == vt->region_top)
-                spiceterm_scroll_down (vt, vt->region_top, vt->region_bottom, 1);
+                spiceterm_scroll_down(vt, vt->region_top, vt->region_bottom, 1);
             else if (vt->cy > 0) {
                 vt->cy--;
             }
@@ -815,7 +824,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
             if (!vt->esc_count) {
                 vt->esc_count++; // default parameter 0
             }
-            spiceterm_csi_m (vt);
+            spiceterm_csi_m(vt);
             break;
         case 'n':
             /* report cursor position */
@@ -866,16 +875,16 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
         case 'G':
         case '`':
             /* move cursor to column */
-            spiceterm_gotoxy (vt, vt->esc_buf[0] - 1, vt->cy);
+            spiceterm_gotoxy(vt, vt->esc_buf[0] - 1, vt->cy);
             break;
         case 'd':
             /* move cursor to row */
-            spiceterm_gotoxy (vt, vt->cx , vt->esc_buf[0] - 1);
+            spiceterm_gotoxy(vt, vt->cx , vt->esc_buf[0] - 1);
             break;
         case 'f':
         case 'H':
             /* move cursor to row, column */
-            spiceterm_gotoxy (vt, vt->esc_buf[1] - 1,  vt->esc_buf[0] - 1);
+            spiceterm_gotoxy(vt, vt->esc_buf[1] - 1,  vt->esc_buf[0] - 1);
             break;
         case 'J':
             switch (vt->esc_buf[0]) {
@@ -897,7 +906,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
                         if (y == vt->cy && x > vt->cx) {
                             break;
                         }
-                        spiceterm_clear_xy (vt, x, y);
+                        spiceterm_clear_xy(vt, x, y);
                     }
                 }
                 break;
@@ -905,7 +914,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
                 /* clear entire screen */
                 for (y = 0; y <= vt->height; y++) {
                     for (x = 0; x < vt->width; x++) {
-                        spiceterm_clear_xy (vt, x, y);
+                        spiceterm_clear_xy(vt, x, y);
                     }
                 }
                 break;
@@ -916,19 +925,19 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
             case 0:
                 /* clear to eol */
                 for(x = vt->cx; x < vt->width; x++) {
-                    spiceterm_clear_xy (vt, x, vt->cy);
+                    spiceterm_clear_xy(vt, x, vt->cy);
                 }
                 break;
             case 1:
                 /* clear from beginning of line */
                 for (x = 0; x <= vt->cx; x++) {
-                    spiceterm_clear_xy (vt, x, vt->cy);
+                    spiceterm_clear_xy(vt, x, vt->cy);
                 }
                 break;
             case 2:
                 /* clear entire line */
                 for(x = 0; x < vt->width; x++) {
-                    spiceterm_clear_xy (vt, x, vt->cy);
+                    spiceterm_clear_xy(vt, x, vt->cy);
                 }
                 break;
             }
@@ -942,7 +951,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
             else if (!c)
                 c = 1;
 
-            spiceterm_scroll_down (vt, vt->cy, vt->region_bottom, c);
+            spiceterm_scroll_down(vt, vt->cy, vt->region_bottom, c);
             break;
         case 'M':
             /* delete line */
@@ -953,19 +962,19 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
             else if (!c)
                 c = 1;
 
-            spiceterm_scroll_up (vt, vt->cy, vt->region_bottom, c, 1);
+            spiceterm_scroll_up(vt, vt->cy, vt->region_bottom, c, 1);
             break;
         case 'T':
             /* scroll down */
             c = vt->esc_buf[0];
             if (!c) c = 1;
-            spiceterm_scroll_down (vt, vt->region_top, vt->region_bottom, c);
+            spiceterm_scroll_down(vt, vt->region_top, vt->region_bottom, c);
             break;
         case 'S':
             /* scroll up */
             c = vt->esc_buf[0];
             if (!c) c = 1;
-            spiceterm_scroll_up (vt, vt->region_top, vt->region_bottom, c, 1);
+            spiceterm_scroll_up(vt, vt->region_top, vt->region_bottom, c, 1);
             break;
         case 'P':
             /* delete c character */
@@ -981,19 +990,19 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
                 TextCell *dst = &vt->cells[y1 * vt->width + x];
                 TextCell *src = dst + c;
                 *dst = *src;
-                spiceterm_update_xy (vt, x + c, vt->cy);
+                spiceterm_update_xy(vt, x + c, vt->cy);
                 src->ch = ' ';
                 src->attrib = vt->default_attrib;
-                spiceterm_update_xy (vt, x, vt->cy);
+                spiceterm_update_xy(vt, x, vt->cy);
             }
             break;
         case 's':
             /* save cursor position */
-            spiceterm_save_cursor (vt);
+            spiceterm_save_cursor(vt);
             break;
         case 'u':
             /* restore cursor position */
-            spiceterm_restore_cursor (vt);
+            spiceterm_restore_cursor(vt);
             break;
         case 'X':
             /* erase c characters */
@@ -1003,7 +1012,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
             if (c > (vt->width - vt->cx)) c = vt->width - vt->cx;
 
             for(i = 0; i < c; i++) {
-                spiceterm_clear_xy (vt, vt->cx + i, vt->cy);
+                spiceterm_clear_xy(vt, vt->cx + i, vt->cy);
             }
             break;
         case '@':
@@ -1022,7 +1031,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
                 spiceterm_update_xy (vt, x + c, vt->cy);
                 src->ch = ' ';
                 src->attrib = vt->cur_attrib;
-                spiceterm_update_xy (vt, x, vt->cy);
+                spiceterm_update_xy(vt, x, vt->cy);
             }
 
             break;
@@ -1088,7 +1097,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
 
         if (ch == 'c') {
             DPRINTF(1, "ESC[>c   Query term ID");
-            spiceterm_respond_esc (vt, TERMIDCODE);
+            spiceterm_respond_esc(vt, TERMIDCODE);
          }
         break;
     case ESpercent:
@@ -1120,7 +1129,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
         case 9:  /* tabspace */
             if (vt->cx + (8 - (vt->cx % 8)) > vt->width) {
                 vt->cx = 0;
-                spiceterm_put_lf (vt);
+                spiceterm_put_lf(vt);
             } else {
                 vt->cx = vt->cx + (8 - (vt->cx % 8));
             }
@@ -1128,7 +1137,7 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
         case 10:  /* LF,*/
         case 11:  /* VT */
         case 12:  /* FF */
-            spiceterm_put_lf (vt);
+            spiceterm_put_lf(vt);
             break;
         case 13:  /* carriage return */
             vt->cx = 0;
@@ -1158,14 +1167,14 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
             if (vt->cx >= vt->width) {
                 /* line wrap */
                 vt->cx = 0;
-                spiceterm_put_lf (vt);
+                spiceterm_put_lf(vt);
             }
 
             int y1 = (vt->y_base + vt->cy) % vt->total_height;
             TextCell *c = &vt->cells[y1*vt->width + vt->cx];
             c->attrib = vt->cur_attrib;
             c->ch = ch;
-            spiceterm_update_xy (vt, vt->cx, vt->cy);
+            spiceterm_update_xy(vt, vt->cx, vt->cy);
             vt->cx++;
             break;
         }
@@ -1174,11 +1183,11 @@ spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
 }
 
 static int
-spiceterm_puts (spiceTerm *vt, const char *buf, int len)
+spiceterm_puts(spiceTerm *vt, const char *buf, int len)
 {
     gunichar2 tc;
 
-    spiceterm_show_cursor (vt, 0);
+    spiceterm_show_cursor(vt, 0);
 
     while (len) {
         unsigned char c = *buf;
@@ -1238,17 +1247,17 @@ spiceterm_puts (spiceTerm *vt, const char *buf, int len)
             }
         }
 
-        spiceterm_putchar (vt, tc);
+        spiceterm_putchar(vt, tc);
     }
 
-    spiceterm_show_cursor (vt, 1);
+    spiceterm_show_cursor(vt, 1);
 
     return len;
 }
 
 /* fixme:
 void
-spiceterm_set_xcut_text (char* str, int len, struct _rfbClientRec* cl)
+spiceterm_set_xcut_text(char* str, int len, struct _rfbClientRec* cl)
 {
   spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
 
@@ -1282,7 +1291,7 @@ mouse_report(spiceTerm *vt, int butt, int mrx, int mry)
 {
     char buf[8];
 
-    sprintf (buf, "[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
+    sprintf(buf, "[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
              (char)('!' + mry));
 
     spiceterm_respond_esc(vt, buf);
@@ -1290,198 +1299,33 @@ mouse_report(spiceTerm *vt, int butt, int mrx, int mry)
     spiceterm_update_watch_mask(vt, TRUE);
 }
 
-void
-spiceterm_toggle_marked_cell (spiceTerm *vt, int pos)
-{
-
-/* fixme:
-  int x= (pos%vt->width)*8;
-  int y= (pos/vt->width)*16;
-
-  int i,j;
-  rfbScreenInfoPtr s=vt->screen;
-
-  char *b = s->frameBuffer+y*s->width+x;
-
-  for (j=0; j < 16; j++) {
-    for(i=0; i < 8; i++) {
-      b[j*s->width+i] ^= 0x0f;
-      rfbMarkRectAsModified (s, x, y, x+8, y+16);
-    }
-  }
-*/
-}
-
-/* fixme:
-
-void
-spiceterm_pointer_event (int buttonMask, int x, int y, rfbClientPtr cl)
-{
-
-  spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
-  static int button2_released = 1;
-  static int last_mask = 0;
-  static int sel_start_pos = 0;
-  static int sel_end_pos = 0;
-  int i;
-
-  int cx = x/8;
-  int cy = y/16;
-
-  if (cx < 0) cx = 0;
-  if (cx >= vt->width) cx = vt->width - 1;
-  if (cy < 0) cy = 0;
-  if (cy >= vt->height) cy = vt->height - 1;
-
-  if (vt->report_mouse && buttonMask != last_mask) {
-    last_mask = buttonMask;
-    if (buttonMask & 1) {
-      mouse_report (vt, 0, cx, cy);
-    }
-    if (buttonMask & 2) {
-      mouse_report (vt, 1, cx, cy);
-    }
-    if (buttonMask & 4) {
-      mouse_report (vt, 2, cx, cy);
-    }
-    if (!buttonMask) {
-      mouse_report (vt, 3, cx, cy);
-    }
-  }
-
-  if (buttonMask & 2) {
-    if(button2_released && vt->selection) {
-      int i;
-      for(i = 0; i < vt->selection_len; i++) {
-       if (vt->ibuf_count < IBUFSIZE - 6) { // uft8 is max 6 characters wide
-         if (vt->utf8) {
-           vt->ibuf_count += ucs2_to_utf8 (vt->selection[i], &vt->ibuf[vt->ibuf_count]);
-         } else  {
-           vt->ibuf[vt->ibuf_count++] = vt->selection[i];
-         }
-       }
-      }
-      if (vt->y_displ != vt->y_base) {
-       vt->y_displ = vt->y_base;
-       spiceterm_refresh (vt);
-      }
-    }
-    button2_released = 0;
-  } else {
-    button2_released = 1;
-  }
-
-  if (buttonMask & 1) {
-    int pos = cy*vt->width + cx;
-
-    // code borrowed from libvncserver (VNCconsole.c)
-
-    if (!vt->mark_active) {
-
-      vt->mark_active = 1;
-      sel_start_pos = sel_end_pos = pos;
-      spiceterm_toggle_marked_cell (vt, pos);
-
-    } else {
-
-      if (pos != sel_end_pos) {
-
-       if (pos > sel_end_pos) {
-         cx = sel_end_pos; cy=pos;
-       } else {
-         cx=pos; cy=sel_end_pos;
-       }
-
-       if (cx < sel_start_pos) {
-         if (cy < sel_start_pos) cy--;
-       } else {
-         cx++;
-       }
-
-       while (cx <= cy) {
-         spiceterm_toggle_marked_cell (vt, cx);
-         cx++;
-       }
-
-       sel_end_pos = pos;
-      }
-    }
-
-  } else if (vt->mark_active) {
-    vt->mark_active = 0;
-
-    if (sel_start_pos > sel_end_pos) {
-      int tmp = sel_start_pos - 1;
-      sel_start_pos = sel_end_pos;
-      sel_end_pos = tmp;
-    }
-
-    int len = sel_end_pos - sel_start_pos + 1;
-
-    if (vt->selection) free (vt->selection);
-    vt->selection = (gunichar2 *)malloc (len*sizeof (gunichar2));
-    vt->selection_len = len;
-    char *sel_latin1 = (char *)malloc (len + 1);
-
-    for (i = 0; i < len; i++) {
-      int pos = sel_start_pos + i;
-      int x = pos % vt->width;
-      int y1 = ((pos / vt->width) + vt->y_displ) % vt->total_height;
-      TextCell *c = &vt->cells[y1*vt->width + x];
-      vt->selection[i] = c->ch;
-      sel_latin1[i] = (char)c->ch;
-      c++;
-    }
-    sel_latin1[len] = 0;
-    rfbGotXCutText (vt->screen, sel_latin1, len);
-    free (sel_latin1);
-
-    while (sel_start_pos <= sel_end_pos) {
-      spiceterm_toggle_marked_cell (vt, sel_start_pos++);
-    }
-
-  }
-
-  rfbDefaultPtrAddEvent (buttonMask, x, y, cl);
-
-}
-*/
-
 static void
-spiceterm_motion_event(spiceTerm *vt, uint32_t x, uint32_t y, uint32_t buttons)
+spiceterm_respond_unichar2(spiceTerm *vt, gunichar2 uc)
 {
-    DPRINTF(0, "mask=%08x x=%d y=%d", buttons, x ,y);
-
-    static int last_mask = 0;
-
-    int cx = x/8;
-    int cy = y/16;
-
-    if (cx < 0) cx = 0;
-    if (cx >= vt->width) cx = vt->width - 1;
-    if (cy < 0) cy = 0;
-    if (cy >= vt->height) cy = vt->height - 1;
-
-    if (vt->report_mouse && buttons != last_mask) {
-        DPRINTF(0, "report=%d", vt->report_mouse);
-
-        last_mask = buttons;
-        if (buttons & 2) {
-            mouse_report(vt, 0, cx, cy);
-        }
-        if (buttons & 4) {
-            mouse_report (vt, 1, cx, cy);
-        }
-        if (buttons & 8) {
-            mouse_report (vt, 2, cx, cy);
+    if (vt->utf8) {
+        gchar buf[10];
+        gint len = g_unichar_to_utf8(uc, buf);
+
+        if (len > 0) {
+            if ((vt->ibuf_count + len) < IBUFSIZE) {
+                int i;
+                for (i = 0; i < len; i++) {
+                    vt->ibuf[vt->ibuf_count++] = buf[i];
+                }
+            } else {
+                fprintf(stderr, "warning: input buffer overflow\n");
+            }
         }
-        if (!buttons) {
-            mouse_report (vt, 3, cx, cy);
+    } else {
+        if ((vt->ibuf_count + 1) < IBUFSIZE) {
+            vt->ibuf[vt->ibuf_count++] = (char)uc;
+        } else {
+            fprintf(stderr, "warning: input buffer overflow\n");
         }
     }
 }
 
-static void 
+static void
 my_kbd_push_key(SpiceKbdInstance *sin, uint8_t frag)
 {
     // spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, keyboard_sin);
@@ -1491,7 +1335,7 @@ my_kbd_push_key(SpiceKbdInstance *sin, uint8_t frag)
     return;
 }
 
-static void 
+static void
 my_kbd_push_keyval(SpiceKbdInstance *sin, uint32_t keySym, int flags)
 {
     spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, keyboard_sin);
@@ -1499,7 +1343,7 @@ my_kbd_push_keyval(SpiceKbdInstance *sin, uint32_t keySym, int flags)
     static int shift = 0;
     char *esc = NULL;
 
-    guint uc = 0;
+    gunichar2 uc = 0;
 
     DPRINTF(1, "flags=%d keySym=%08x", flags, keySym);
 
@@ -1606,19 +1450,7 @@ my_kbd_push_keyval(SpiceKbdInstance *sin, uint32_t keySym, int flags)
             if (esc) {
                 spiceterm_respond_esc(vt, esc);
             } else if (uc > 0) {
-                if (vt->utf8) {
-                    gchar buf[10];
-                    gint len = g_unichar_to_utf8(uc, buf);
-
-                    if (len > 0) {
-                        int i;
-                        for (i = 0; i < len; i++) {
-                            vt->ibuf[vt->ibuf_count++] = buf[i];
-                        }
-                    }
-                } else {
-                    vt->ibuf[vt->ibuf_count++] = (char)uc;
-                }
+                spiceterm_respond_unichar2(vt, uc);
             }
         }
     }
@@ -1637,7 +1469,7 @@ ret:
     spiceterm_update_watch_mask(vt, TRUE);
 }
 
-static uint8_t 
+static uint8_t
 my_kbd_get_leds(SpiceKbdInstance *sin)
 {
     return 0;
@@ -1653,8 +1485,320 @@ static SpiceKbdInterface my_keyboard_sif = {
     .get_leds           = my_kbd_get_leds,
 };
 
+
 /* vdagent interface - to get mouse/clipboarde support */
-static int 
+
+#define VDAGENT_WBUF_SIZE (1024*50)
+static unsigned char vdagent_write_buffer[VDAGENT_WBUF_SIZE];
+static int vdagent_write_buffer_pos = 0;
+static int agent_owns_clipboard[256] = { 0, };
+
+static void
+spiceterm_motion_event(spiceTerm *vt, uint32_t x, uint32_t y, uint32_t buttons)
+{
+    DPRINTF(1, "mask=%08x x=%d y=%d", buttons, x ,y);
+
+    static int last_mask = 0;
+    static int sel_start_pos = 0;
+    static int sel_end_pos = 0;
+    static int button2_released = 1;
+
+    int i;
+    int cx = x/8;
+    int cy = y/16;
+
+    if (cx < 0) cx = 0;
+    if (cx >= vt->width) cx = vt->width - 1;
+    if (cy < 0) cy = 0;
+    if (cy >= vt->height) cy = vt->height - 1;
+
+    if (vt->report_mouse && buttons != last_mask) {
+        last_mask = buttons;
+        if (buttons & 2) {
+            mouse_report(vt, 0, cx, cy);
+        }
+        if (buttons & 4) {
+            mouse_report (vt, 1, cx, cy);
+        }
+        if (buttons & 8) {
+            mouse_report(vt, 2, cx, cy);
+        }
+        if(!buttons) {
+            mouse_report(vt, 3, cx, cy);
+        }
+    }
+
+    if (buttons & 4) {
+
+        if(button2_released) {
+
+            if (agent_owns_clipboard[VD_AGENT_CLIPBOARD_SELECTION_PRIMARY]) {
+                if (vt->selection) {
+                    int i;
+                    for(i = 0; i < vt->selection_len; i++) {
+                        spiceterm_respond_unichar2(vt, vt->selection[i]);
+                    }
+                    spiceterm_update_watch_mask(vt, TRUE);
+                    if (vt->y_displ != vt->y_base) {
+                        vt->y_displ = vt->y_base;
+                        spiceterm_refresh(vt);
+                    }
+                }
+            } else {
+                vdagent_request_clipboard(vt, VD_AGENT_CLIPBOARD_SELECTION_PRIMARY);
+            } 
+        }
+
+        button2_released = 0;
+    } else {
+        button2_released = 1;
+    }
+
+    if (buttons & 2) {
+        int pos = cy*vt->width + cx;
+
+        // code borrowed from libvncserver (VNCconsole.c)
+
+        if (!vt->mark_active) {
+
+            spiceterm_unselect_all(vt);
+
+            vt->mark_active = 1;
+            sel_start_pos = sel_end_pos = pos;
+            spiceterm_toggle_marked_cell(vt, pos);
+
+        } else {
+
+            if (pos != sel_end_pos) {
+                if (pos > sel_end_pos) {
+                    cx = sel_end_pos; cy=pos;
+                } else {
+                    cx=pos; cy=sel_end_pos;
+                }
+
+                if (cx < sel_start_pos) {
+                    if (cy < sel_start_pos) cy--;
+                } else {
+                    cx++;
+                }
+
+                while (cx <= cy) {
+                    spiceterm_toggle_marked_cell(vt, cx);
+                    cx++;
+                }
+
+                sel_end_pos = pos;
+            }
+        }
+
+    } else if (vt->mark_active) {
+        vt->mark_active = 0;
+
+        if (sel_start_pos > sel_end_pos) {
+            int tmp = sel_start_pos - 1;
+            sel_start_pos = sel_end_pos;
+            sel_end_pos = tmp;
+        }
+
+        int len = sel_end_pos - sel_start_pos + 1;
+
+        if (vt->selection) free (vt->selection);
+        vt->selection = (gunichar2 *)malloc (len*sizeof(gunichar2));
+        vt->selection_len = len;
+
+        for (i = 0; i < len; i++) {
+            int pos = sel_start_pos + i;
+            int x = pos % vt->width;
+            int y1 = ((pos / vt->width) + vt->y_displ) % vt->total_height;
+            TextCell *c = &vt->cells[y1*vt->width + x];
+            vt->selection[i] = c->ch;
+            c++;
+        }
+
+        DPRINTF(1, "selection length = %d", vt->selection_len);
+
+        vdagent_grab_clipboard(vt, VD_AGENT_CLIPBOARD_SELECTION_PRIMARY);
+    }
+}
+
+static void vdagent_send_capabilities(spiceTerm *vt, uint32_t request)
+{
+    VDAgentAnnounceCapabilities *caps;
+    uint32_t size;
+
+    size = sizeof(*caps) + VD_AGENT_CAPS_BYTES;
+    caps = calloc(1, size);
+    g_assert(caps != NULL);
+
+    caps->request = request;
+    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_MOUSE_STATE);
+    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_MONITORS_CONFIG);
+    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_REPLY);
+    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_CLIPBOARD_BY_DEMAND);
+    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_CLIPBOARD_SELECTION);
+    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_SPARSE_MONITORS_CONFIG);
+    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_GUEST_LINEEND_LF);
+
+    int msg_size =  sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) + size;
+    g_assert((vdagent_write_buffer_pos + msg_size) < VDAGENT_WBUF_SIZE);
+
+    unsigned char *buf = vdagent_write_buffer + vdagent_write_buffer_pos;
+    vdagent_write_buffer_pos += msg_size;
+
+    VDIChunkHeader *hdr = (VDIChunkHeader *)buf;
+    VDAgentMessage *msg = (VDAgentMessage *)&hdr[1];
+    hdr->port = VDP_CLIENT_PORT;
+    hdr->size = sizeof(VDAgentMessage) + size;
+    msg->protocol = VD_AGENT_PROTOCOL;
+    msg->type = VD_AGENT_ANNOUNCE_CAPABILITIES;
+    msg->opaque = 0;
+    msg->size = size;
+
+    memcpy(buf + sizeof(VDIChunkHeader) + sizeof(VDAgentMessage), (uint8_t *)caps, size);
+    
+    spice_server_char_device_wakeup(&vt->vdagent_sin);
+
+    free(caps);
+}
+
+static void
+dump_message(unsigned char *buf, int size)
+{
+    int i;
+
+    for (i = 0; i < size; i++) {
+        printf("%d  %02X\n", i, buf[i]);
+    }
+
+    // exit(0);
+}
+
+static void vdagent_grab_clipboard(spiceTerm *vt, uint8_t selection)
+{
+    uint32_t size;
+    
+    agent_owns_clipboard[selection] = 1;
+
+    size = 8;
+
+    int msg_size =  sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) + size;
+    g_assert((vdagent_write_buffer_pos + msg_size) < VDAGENT_WBUF_SIZE);
+
+    unsigned char *buf = vdagent_write_buffer + vdagent_write_buffer_pos;
+    vdagent_write_buffer_pos += msg_size;
+
+    memset(buf, 0, msg_size);
+
+    VDIChunkHeader *hdr = (VDIChunkHeader *)buf;
+    VDAgentMessage *msg = (VDAgentMessage *)&hdr[1];
+    uint8_t *grab = (uint8_t *)&msg[1];
+    *((uint8_t *)grab) = selection;
+    *((uint32_t *)(grab + 4)) = VD_AGENT_CLIPBOARD_UTF8_TEXT;
+
+    hdr->port = VDP_CLIENT_PORT;
+    hdr->size = sizeof(VDAgentMessage) + size;
+
+    msg->protocol = VD_AGENT_PROTOCOL;
+    msg->type = VD_AGENT_CLIPBOARD_GRAB;
+    msg->opaque = 0;
+    msg->size = size;
+
+    if (0) dump_message(buf, msg_size);
+
+    spice_server_char_device_wakeup(&vt->vdagent_sin);
+}
+
+static void vdagent_request_clipboard(spiceTerm *vt, uint8_t selection)
+{
+    uint32_t size;
+    
+    agent_owns_clipboard[selection] = 1;
+
+    size = 4 + sizeof(VDAgentClipboardRequest);
+
+    int msg_size =  sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) + size;
+    g_assert((vdagent_write_buffer_pos + msg_size) < VDAGENT_WBUF_SIZE);
+
+    unsigned char *buf = vdagent_write_buffer + vdagent_write_buffer_pos;
+    vdagent_write_buffer_pos += msg_size;
+
+    memset(buf, 0, msg_size);
+
+    VDIChunkHeader *hdr = (VDIChunkHeader *)buf;
+    VDAgentMessage *msg = (VDAgentMessage *)&hdr[1];
+    uint8_t *data = (uint8_t *)&msg[1];
+    *((uint32_t *)data) = 0;
+    data[0] = selection;
+    ((uint32_t *)data)[1] = VD_AGENT_CLIPBOARD_UTF8_TEXT;
+
+    hdr->port = VDP_CLIENT_PORT;
+    hdr->size = sizeof(VDAgentMessage) + size;
+
+    msg->protocol = VD_AGENT_PROTOCOL;
+    msg->type = VD_AGENT_CLIPBOARD_REQUEST;
+    msg->opaque = 0;
+    msg->size = size;
+
+    if (0) dump_message(buf, msg_size);
+
+    spice_server_char_device_wakeup(&vt->vdagent_sin);
+}
+
+static void vdagent_send_clipboard(spiceTerm *vt, uint8_t selection)
+{
+    uint32_t size;
+    
+    if (selection != VD_AGENT_CLIPBOARD_SELECTION_PRIMARY) {
+        fprintf(stderr, "clipboard select %d is not supported\n", selection);
+        return;
+    }
+
+    gchar *sel_data;
+    glong sel_len;
+    if (vt->utf8) {
+        sel_data = g_utf16_to_utf8(vt->selection, vt->selection_len, NULL, &sel_len, NULL);
+    } else {
+        sel_len = vt->selection_len;
+        sel_data = g_malloc(sel_len);
+        int i;
+        for (i = 0; i < sel_len; i++) { sel_data[i] =  (char)vt->selection[i]; }
+        sel_data[sel_len] = 0;
+    }
+
+    size = 8 + sel_len;
+
+    int msg_size =  sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) + size;
+    g_assert((vdagent_write_buffer_pos + msg_size) < VDAGENT_WBUF_SIZE);
+
+    unsigned char *buf = vdagent_write_buffer + vdagent_write_buffer_pos;
+    vdagent_write_buffer_pos += msg_size;
+
+    memset(buf, 0, msg_size);
+   
+    VDIChunkHeader *hdr = (VDIChunkHeader *)buf;
+    VDAgentMessage *msg = (VDAgentMessage *)&hdr[1];
+    uint8_t *data = (uint8_t *)&msg[1];
+    *((uint8_t *)data) = selection;
+    data += 4;
+    *((uint32_t *)data) = VD_AGENT_CLIPBOARD_UTF8_TEXT;
+    data += 4;
+
+    memcpy(data, sel_data, sel_len);
+    g_free(sel_data);
+
+    hdr->port = VDP_CLIENT_PORT;
+    hdr->size = sizeof(VDAgentMessage) + size;
+
+    msg->protocol = VD_AGENT_PROTOCOL;
+    msg->type = VD_AGENT_CLIPBOARD;
+    msg->opaque = 0;
+    msg->size = size;
+
+    spice_server_char_device_wakeup(&vt->vdagent_sin);
+}
+
+static int
 vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
 {
     spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, vdagent_sin);
@@ -1662,34 +1806,110 @@ vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
     VDIChunkHeader *hdr = (VDIChunkHeader *)buf;
     VDAgentMessage *msg = (VDAgentMessage *)&hdr[1];
 
-    //g_assert(hdr->port == VDP_SERVER_PORT); 
+    //g_assert(hdr->port == VDP_SERVER_PORT);
     g_assert(msg->protocol == VD_AGENT_PROTOCOL);
 
     DPRINTF(1, "%d %d %d %d", len, hdr->port, msg->protocol, msg->type);
 
-    if (msg->type == VD_AGENT_MOUSE_STATE) {
+    switch (msg->type) {
+    case VD_AGENT_MOUSE_STATE: { 
         VDAgentMouseState *info = (VDAgentMouseState *)&msg[1];
         spiceterm_motion_event(vt, info->x, info->y, info->buttons);
-    } else if (msg->type == VD_AGENT_ANNOUNCE_CAPABILITIES) {
-        /* ignore for now */
-    } else if (msg->type == VD_AGENT_MONITORS_CONFIG) {
+        break;
+    }
+    case VD_AGENT_ANNOUNCE_CAPABILITIES: {
+        VDAgentAnnounceCapabilities *caps = (VDAgentAnnounceCapabilities *)&msg[1];
+        DPRINTF(1, "VD_AGENT_ANNOUNCE_CAPABILITIES %d", caps->request);
+        int i;
+        
+        int caps_size = VD_AGENT_CAPS_SIZE_FROM_MSG_SIZE(hdr->size);
+        for (i = 0; i < VD_AGENT_END_CAP; i++) {
+            DPRINTF(1, "CAPABILITIES %d %d", i, VD_AGENT_HAS_CAPABILITY(caps->caps, caps_size, i));
+        }
+
+        vdagent_send_capabilities(vt, 0);
+        break;
+    }
+    case VD_AGENT_CLIPBOARD_GRAB: {
+        VDAgentClipboardGrab *grab = (VDAgentClipboardGrab *)&msg[1];
+        uint8_t selection = *((uint8_t *)grab);
+        DPRINTF(1, "VD_AGENT_CLIPBOARD_GRAB %d", selection);
+        agent_owns_clipboard[selection] = 0;
+        break;
+    }
+    case VD_AGENT_CLIPBOARD_REQUEST: {
+        uint8_t *req = (uint8_t *)&msg[1];
+        uint8_t selection = *((uint8_t *)req);
+        uint32_t type = *((uint32_t *)(req + 4));
+
+        DPRINTF(1, "VD_AGENT_CLIPBOARD_REQUEST %d %d", selection, type);
+
+        vdagent_send_clipboard(vt, selection);
+        
+        break;
+    }
+    case VD_AGENT_CLIPBOARD: {
+        uint8_t *data = (uint8_t *)&msg[1];
+        uint8_t selection = data[0];
+        uint32_t type = *(uint32_t *)(data + 4);
+        int size = msg->size - 8;
+        DPRINTF(1, "VD_AGENT_CLIPBOARD %d %d %d", selection, type, size);
+
+        if (type == VD_AGENT_CLIPBOARD_UTF8_TEXT) {
+            int i;
+            for (i = 0; i < size; i++) {
+                if ((vt->ibuf_count + 1) < IBUFSIZE) {
+                    vt->ibuf[vt->ibuf_count++] = *(char *)(data + 8 + i);
+                } else {
+                    fprintf(stderr, "warning: input buffer overflow\n");
+                }
+            }
+            spiceterm_update_watch_mask(vt, TRUE);
+        }
+        break;
+    }
+    case VD_AGENT_CLIPBOARD_RELEASE: {
+        uint8_t *data = (uint8_t *)&msg[1];
+        uint8_t selection = data[0];
+        
+        DPRINTF(0, "VD_AGENT_CLIPBOARD_RELEASE %d", selection);
+     
+        break;
+    }
+    case VD_AGENT_MONITORS_CONFIG:
         /* ignore for now */
-    } else {
+        break;
+    default:
         DPRINTF(0, "got uknown vdagent message type %d\n", msg->type);
     }
 
     return len;
 }
 
-static int 
+static int
 vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
 {
-    DPRINTF(1, "%d", len);
+    DPRINTF(0, "%d %d", len,  vdagent_write_buffer_pos);
+    g_assert(len >= 8);
 
-    return 0;
+    if (!vdagent_write_buffer_pos) {
+        return 0;
+    }
+     
+    int size = (len >= vdagent_write_buffer_pos) ? vdagent_write_buffer_pos : len;
+    memcpy(buf, vdagent_write_buffer, size);
+    if (size < vdagent_write_buffer_pos) {
+        DPRINTF(0, "MOVE %d", size);
+        memmove(vdagent_write_buffer, vdagent_write_buffer + size, 
+                vdagent_write_buffer_pos - size);
+    }
+    vdagent_write_buffer_pos -= size;
+
+    DPRINTF(0, "RET %d %d", size,  vdagent_write_buffer_pos);
+    return size;
 }
 
-static void 
+static void
 vmc_state(SpiceCharDeviceInstance *sin, int connected)
 {
     /* IGNORE */
@@ -1774,8 +1994,8 @@ create_spiceterm(int argc, char** argv, int maxx, int maxy, guint timeout)
     return vt;
 }
 
-static gboolean 
-master_error_callback(GIOChannel *channel, GIOCondition condition, 
+static gboolean
+master_error_callback(GIOChannel *channel, GIOCondition condition,
                       gpointer data)
 {
     //spiceTerm *vt = (spiceTerm *)data;
@@ -1787,7 +2007,7 @@ master_error_callback(GIOChannel *channel, GIOCondition condition,
     return FALSE;
 }
 
-static void 
+static void
 master_watch(int master, int event, void *opaque)
 {
     spiceTerm *vt = (spiceTerm *)opaque;
@@ -1809,11 +2029,11 @@ master_watch(int master, int event, void *opaque)
             DPRINTF(1, "write input %x %d", vt->ibuf[0], vt->ibuf_count);
             if ((c = write (master, vt->ibuf, vt->ibuf_count)) >= 0) {
                 if (c == vt->ibuf_count) {
-                    vt->ibuf_count = 0;                   
+                    vt->ibuf_count = 0;
                 } else if (c > 0) {
                     // not all data written
                     memmove(vt->ibuf, vt->ibuf + c, vt->ibuf_count - c);
-                    vt->ibuf_count -= c; 
+                    vt->ibuf_count -= c;
                 } else {
                     // nothing written -ignore and try later
                 }
@@ -1896,7 +2116,7 @@ main (int argc, char** argv)
     g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
     g_io_channel_set_encoding(channel, NULL, NULL);
     g_io_add_watch(channel, G_IO_ERR|G_IO_HUP, master_error_callback, vt);
+
     vt->screen->mwatch = vt->screen->core->watch_add(
         master, SPICE_WATCH_EVENT_READ /* |SPICE_WATCH_EVENT_WRITE */,
         master_watch, vt);