]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - unix/uxsel.c
first pass
[PuTTY.git] / unix / uxsel.c
index aaedc02228636890085865a05a64148c8de8ebb6..ef25cdb574e2a6cf7fe4bb95112be1e05e52b947 100644 (file)
@@ -19,6 +19,7 @@ struct fd {
     int fd;
     int rwx;                          /* 4=except 2=write 1=read */
     uxsel_callback_fn callback;
+    uxsel_id *id;                      /* for uxsel_input_remove */
 };
 
 static tree234 *fds;
@@ -61,26 +62,28 @@ void uxsel_init(void)
 
 void uxsel_set(int fd, int rwx, uxsel_callback_fn callback)
 {
-    struct fd *newfd = snew(struct fd);
-    struct fd *oldfd;
+    struct fd *newfd;
 
-    newfd->fd = fd;
-    newfd->rwx = rwx;
-    newfd->callback = callback;
+    assert(fd >= 0);
 
-    oldfd = find234(fds, newfd, NULL);
-    if (oldfd) {
-       del234(fds, oldfd);
-       sfree(oldfd);
-    }
+    uxsel_del(fd);
 
-    add234(fds, newfd);
+    if (rwx) {
+       newfd = snew(struct fd);
+       newfd->fd = fd;
+       newfd->rwx = rwx;
+       newfd->callback = callback;
+       newfd->id = uxsel_input_add(fd, rwx);
+       add234(fds, newfd);
+    }
 }
 
 void uxsel_del(int fd)
 {
     struct fd *oldfd = find234(fds, &fd, uxsel_fd_findcmp);
     if (oldfd) {
+       if (oldfd->id)
+            uxsel_input_remove(oldfd->id);
        del234(fds, oldfd);
        sfree(oldfd);
     }
@@ -111,6 +114,13 @@ int first_fd(int *state, int *rwx)
 int select_result(int fd, int event)
 {
     struct fd *fdstruct = find234(fds, &fd, uxsel_fd_findcmp);
-    assert(fdstruct != NULL);
-    return fdstruct->callback(fd, event);
+    /*
+     * Apparently this can sometimes be NULL. Can't see how, but I
+     * assume it means I need to ignore the event since it's on an
+     * fd I've stopped being interested in. Sigh.
+     */
+    if (fdstruct)
+        return fdstruct->callback(fd, event);
+    else
+        return 1;
 }