/* * The Rubik's cube. * * Sed - april 1999 / december 2003. * * This program is in the public domain. *-------------------- * The wait event function, or how to show the superiority of select in face of threads shit. */ #include "event.h" #include #include #include #include #include #include #include "screen.h" #include "cube.h" int can_mix(int fd) { fd_set w; struct timeval t; FD_ZERO(&w); t.tv_sec=0; t.tv_usec=0; FD_SET(fd, &w); select(fd+1, 0, &w, 0, &t); return FD_ISSET(fd, &w); } /* return -1 of error, and what it has to, read it */ int wait_event(SCREEN *s, CUBE *c) { fd_set r, w; struct timeval t; /* , u; */ int ret; XEvent ev; FD_ZERO(&r); FD_ZERO(&w); t.tv_sec=0; t.tv_usec=0; /* 50*1000; */ if (XCheckMaskEvent(s->d, -1L, &ev)!=False) { XPutBackEvent(s->d, &ev); return SCREEN_EVENT; } FD_SET(ConnectionNumber(s->d), &r); if (c->anim) while((ret=select(FD_SETSIZE, &r, &w, 0, &t))==-1) { if (errno!=EINTR) { perror("select"); return -1; } } else while((ret=select(FD_SETSIZE, &r, &w, 0, (struct timeval *)0))==-1) { if (errno!=EINTR) { perror("select"); return -1; } } if (!ret) { return CUBE_EVENT; } ret=0; if (FD_ISSET(ConnectionNumber(s->d), &r)) ret|=SCREEN_EVENT; else { /* I have to do this XCheckMaskEvent because X functions might already have taken * away from the socket some events, that I may see too late, producing * bad results for the player. */ if (XCheckMaskEvent(s->d, -1L, &ev)!=False) { ret |= SCREEN_EVENT; XPutBackEvent(s->d, &ev); } } return ret; }