---
 src/spiceqxl_main_loop.c |   21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/spiceqxl_main_loop.c b/src/spiceqxl_main_loop.c
index b06d5eb..3154384 100644
--- a/src/spiceqxl_main_loop.c
+++ b/src/spiceqxl_main_loop.c
@@ -241,12 +241,14 @@ static void channel_event(int event, SpiceChannelEventInfo *info)
     NOT_IMPLEMENTED
 }
 
-static int set_watch_fds(fd_set *rfds, fd_set *wfds)
+static int set_watch_fds(fd_set *rfds, fd_set *wfds, int *wcount)
 {
     SpiceWatch *watch;
     RingItem *link;
     RingItem *next;
     int max_fd = -1;
+    if (wcount)
+        *wcount = 0;
 
     RING_FOREACH_SAFE(link, next, &watches) {
         watch = (SpiceWatch*)link;
@@ -257,6 +259,8 @@ static int set_watch_fds(fd_set *rfds, fd_set *wfds)
         if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
             FD_SET(watch->fd, wfds);
             max_fd = watch->fd > max_fd ? watch->fd : max_fd;
+            if (wcount)
+                (*wcount)++;
         }
     }
     return max_fd;
@@ -267,10 +271,21 @@ static int set_watch_fds(fd_set *rfds, fd_set *wfds)
  * readmask is just an fdset on linux, but something totally different on windows (etc).
  * DIX has a comment about it using a special type to hide this (so we break that here)
  */
+static struct timeval write_timeout;
 static void xspice_block_handler(pointer data, OSTimePtr timeout, pointer readmask)
 {
+    int wcount;
     /* set all our fd's */
-    set_watch_fds((fd_set*)readmask, (fd_set*)readmask);
+    set_watch_fds((fd_set*)readmask, (fd_set*)readmask, &wcount);
+
+    /* If we have any write watches, we have to force the X server into a polling
+       select loop */
+    if (wcount > 0)
+    {
+        if (*timeout == NULL)
+            *timeout = &write_timeout;
+        (*timeout)->tv_sec = (*timeout)->tv_usec = 0;
+    }
 }
 
 /*
@@ -292,7 +307,7 @@ static void select_and_check_watches(void)
 
     FD_ZERO(&rfds);
     FD_ZERO(&wfds);
-    max_fd = set_watch_fds(&rfds, &wfds);
+    max_fd = set_watch_fds(&rfds, &wfds, NULL);
     watch = (SpiceWatch*)watches.next;
     timeout.tv_sec = timeout.tv_usec = 0;
     retval = select(max_fd + 1, &rfds, &wfds, NULL, &timeout);
-- 
1.7.9.5

