commit 129718d71af9294792a1fe035aee63e4f4a2e182
Author: Oswald Buddenhagen <o...@kde.org>
Date:   Sun Mar 13 14:29:12 2011 +0100

    add simple mainloop implementation
    
    not used so far

 configure.in |    2 +-
 src/isync.h  |   14 ++++
 src/main.c   |    1 +
 src/util.c   |  170 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 1 deletions(-)

diff --git a/configure.in b/configure.in
index c0dbaa7..f63081a 100644
--- a/configure.in
+++ b/configure.in
@@ -9,7 +9,7 @@ if test "$GCC" = yes; then
     CFLAGS="$CFLAGS -pipe -W -Wall -Wshadow -Wstrict-prototypes"
 fi
 
-AC_CHECK_HEADERS([sys/filio.h])
+AC_CHECK_HEADERS(sys/filio.h sys/poll.h sys/select.h)
 AC_CHECK_FUNCS(vasprintf)
 
 AC_CHECK_LIB(socket, socket, [SOCK_LIBS="-lsocket"])
diff --git a/src/isync.h b/src/isync.h
index 91f0d12..afefd49 100644
--- a/src/isync.h
+++ b/src/isync.h
@@ -400,6 +400,20 @@ void sort_ints( int *arr, int len );
 void arc4_init( void );
 unsigned char arc4_getbyte( void );
 
+#ifdef HAVE_SYS_POLL_H
+# include <sys/poll.h>
+#else
+# define POLLIN 1
+# define POLLOUT 4
+# define POLLERR 8
+#endif
+
+void add_fd( int fd, void (*cb)( int events, void *aux ), void *aux );
+void conf_fd( int fd, int and_events, int or_events );
+void fake_fd( int fd, int events );
+void del_fd( int fd );
+void main_loop( void );
+
 /* sync.c */
 
 extern const char *str_ms[2], *str_hl[2];
diff --git a/src/main.c b/src/main.c
index 4bd45d2..903efd3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -485,6 +485,7 @@ main( int argc, char **argv )
        mvars->argv = argv;
        mvars->cben = 1;
        sync_chans( mvars, E_START );
+       main_loop();
        return mvars->ret;
 }
 
diff --git a/src/util.c b/src/util.c
index 8a718e4..b63508b 100644
--- a/src/util.c
+++ b/src/util.c
@@ -23,6 +23,7 @@
 
 #include "isync.h"
 
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -411,3 +412,172 @@ arc4_getbyte( void )
        rs.s[rs.j] = si;
        return rs.s[(si + sj) & 0xff];
 }
+
+
+#ifdef HAVE_SYS_POLL_H
+static struct pollfd *pollfds;
+#else
+# ifdef HAVE_SYS_SELECT_H
+#  include <sys/select.h>
+# endif
+# define pollfds fdparms
+#endif
+static struct {
+       void (*cb)( int what, void *aux );
+       void *aux;
+#ifndef HAVE_SYS_POLL_H
+       int fd, events;
+#endif
+       int faked;
+} *fdparms;
+static int npolls, rpolls, changed, faking;
+
+static int
+find_fd( int fd )
+{
+       int n;
+
+       for (n = 0; n < npolls; n++)
+               if (pollfds[n].fd == fd)
+                       return n;
+       return -1;
+}
+
+void
+add_fd( int fd, void (*cb)( int events, void *aux ), void *aux )
+{
+       int n;
+
+       assert( find_fd( fd ) < 0 );
+       n = npolls++;
+       if (rpolls < npolls) {
+               rpolls = npolls;
+#ifdef HAVE_SYS_POLL_H
+               pollfds = nfrealloc(pollfds, npolls * sizeof(*pollfds));
+#endif
+               fdparms = nfrealloc(fdparms, npolls * sizeof(*fdparms));
+       }
+       pollfds[n].fd = fd;
+       pollfds[n].events = 0; /* POLLERR & POLLHUP implicit */
+       fdparms[n].faked = 0;
+       fdparms[n].cb = cb;
+       fdparms[n].aux = aux;
+       changed = 1;
+}
+
+void
+conf_fd( int fd, int and_events, int or_events )
+{
+       int n = find_fd( fd );
+       assert( n >= 0 );
+       pollfds[n].events = (pollfds[n].events & and_events) | or_events;
+}
+
+void
+fake_fd( int fd, int events )
+{
+       int n = find_fd( fd );
+       assert( n >= 0 );
+       fdparms[n].faked |= events;
+       faking = 1;
+}
+
+void
+del_fd( int fd )
+{
+       int n = find_fd( fd );
+       assert( n >= 0 );
+       npolls--;
+#ifdef HAVE_SYS_POLL_H
+       memcpy(pollfds + n, pollfds + n + 1, (npolls - n) * sizeof(*pollfds));
+#endif
+       memcpy(fdparms + n, fdparms + n + 1, (npolls - n) * sizeof(*fdparms));
+       changed = 1;
+}
+
+#define shifted_bit(in, from, to) \
+       (((unsigned)(in) & from) \
+               / (from > to ? from / to : 1) \
+               * (to > from ? to / from : 1))
+
+static void
+event_wait( void )
+{
+#ifndef HAVE_SYS_POLL_H
+       fd_set rfds, wfds, efds;
+       int fd;
+#endif
+       int m, n;
+
+       if (faking) {
+               faking = 0;
+               for (n = 0; n < npolls; n++)
+                       if ((m = fdparms[n].faked)) {
+                               fdparms[n].faked = 0;
+                               fdparms[n].cb( m, fdparms[n].aux );
+                               if (changed) {
+                                       changed = 0;
+                                       break;
+                               }
+                       }
+               return;
+       }
+#ifdef HAVE_SYS_POLL_H
+       if (poll( pollfds, npolls, -1 ) < 0) {
+               perror( "poll() failed in event loop" );
+               abort();
+       }
+       for (n = 0; n < npolls; n++)
+               if ((m = pollfds[n].revents)) {
+                       assert( !(m & POLLNVAL) );
+                       fdparms[n].cb( m | shifted_bit( m, POLLHUP, POLLIN ), 
fdparms[n].aux );
+                       if (changed) {
+                               changed = 0;
+                               break;
+                       }
+               }
+#else
+       FD_ZERO( &rfds );
+       FD_ZERO( &wfds );
+       FD_ZERO( &efds );
+       m = -1;
+       for (n = 0; n < npolls; n++) {
+               fd = fdparms[n].fd;
+               if (fdparms[n].events & POLLIN)
+                       FD_SET( fd, &rfds );
+               if (fdparms[n].events & POLLOUT)
+                       FD_SET( fd, &wfds );
+               FD_SET( fd, &efds );
+               if (fd > m)
+                       m = fd;
+       }
+       if (select( m + 1, &rfds, &wfds, &efds, 0 ) < 0) {
+               perror( "select() failed in event loop" );
+               abort();
+       }
+       for (n = 0; n < npolls; n++) {
+               fd = fdparms[n].fd;
+               m = 0;
+               if (FD_ISSET( fd, &rfds ))
+                       m = POLLIN;
+               if (FD_ISSET( fd, &wfds ))
+                       m |= POLLOUT;
+               if (FD_ISSET( fd, &efds ))
+                       m |= POLLERR;
+               if (m) {
+                       fdparms[n].cb( m, fdparms[n].aux );
+                       if (changed) {
+                               changed = 0;
+                               break;
+                       }
+               }
+       }
+#endif
+}
+
+void
+main_loop( void )
+{
+       while (npolls)
+               event_wait();
+}

------------------------------------------------------------------------------
Xperia(TM) PLAY
It's a major breakthrough. An authentic gaming
smartphone on the nation's most reliable network.
And it wants your games.
http://p.sf.net/sfu/verizon-sfdev
_______________________________________________
isync-devel mailing list
isync-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to