merge the formfield patch from ooo-build
[ooovba.git] / solenv / unxmacxp / inc / poll.h
blobb30bfd393a36818a8ae52dc0dd9ccab67674ddd9
1 // poll.h
2 // MacOS X does not implement poll(). Therefore, this replacement
3 // is required. It uses select().
5 #ifndef _FAKE_POLL_H
6 #define _FAKE_POLL_H
8 #include <sys/errno.h>
9 #include <string.h>
10 #include <limits.h>
11 #undef FD_SETSIZE
12 #define FD_SETSIZE OPEN_MAX
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <unistd.h>
16 #include <stdlib.h>
18 typedef struct pollfd {
19 int fd; /* file desc to poll */
20 short events; /* events of interest on fd */
21 short revents; /* events that occurred on fd */
22 } pollfd_t;
25 // poll flags
26 #define POLLIN 0x0001
27 #define POLLOUT 0x0004
28 #define POLLERR 0x0008
30 // synonyms
31 #define POLLNORM POLLIN
32 #define POLLPRI POLLIN
33 #define POLLRDNORM POLLIN
34 #define POLLRDBAND POLLIN
35 #define POLLWRNORM POLLOUT
36 #define POLLWRBAND POLLOUT
38 // ignored
39 #define POLLHUP 0x0010
40 #define POLLNVAL 0x0020
42 inline int poll(struct pollfd *pollSet, int pollCount, int pollTimeout)
44 struct timeval tv;
45 struct timeval *tvp;
46 fd_set readFDs, writeFDs, exceptFDs;
47 fd_set *readp, *writep, *exceptp;
48 struct pollfd *pollEnd, *p;
49 int selected;
50 int result;
51 int maxFD;
53 if ( !pollSet )
55 pollEnd = NULL;
56 readp = NULL;
57 writep = NULL;
58 exceptp = NULL;
59 maxFD = 0;
61 else
63 pollEnd = pollSet + pollCount;
64 readp = &readFDs;
65 writep = &writeFDs;
66 exceptp = &exceptFDs;
68 FD_ZERO(readp);
69 FD_ZERO(writep);
70 FD_ZERO(exceptp);
72 // Find the biggest fd in the poll set
73 maxFD = 0;
74 for (p = pollSet; p < pollEnd; p++)
76 if (p->fd > maxFD)
77 maxFD = p->fd;
80 if (maxFD >= FD_SETSIZE)
82 // At least one fd is too big
83 errno = EINVAL;
84 return -1;
87 // Transcribe flags from the poll set to the fd sets
88 for (p = pollSet; p < pollEnd; p++)
90 if (p->fd < 0)
92 // Negative fd checks nothing and always reports zero
94 else
96 if (p->events & POLLIN)
97 FD_SET(p->fd, readp);
98 if (p->events & POLLOUT)
99 FD_SET(p->fd, writep);
100 if (p->events != 0)
101 FD_SET(p->fd, exceptp);
102 // POLLERR is never set coming in; poll() always reports errors
103 // But don't report if we're not listening to anything at all.
108 // poll timeout is in milliseconds. Convert to struct timeval.
109 // poll timeout == -1 : wait forever : select timeout of NULL
110 // poll timeout == 0 : return immediately : select timeout of zero
111 if (pollTimeout >= 0)
113 tv.tv_sec = pollTimeout / 1000;
114 tv.tv_usec = (pollTimeout % 1000) * 1000;
115 tvp = &tv;
117 else
119 tvp = NULL;
122 selected = select(maxFD+1, readp, writep, exceptp, tvp);
124 if (selected < 0)
126 // Error during select
127 result = -1;
129 else if (selected > 0)
131 // Select found something
132 // Transcribe result from fd sets to poll set.
133 // Also count the number of selected fds. poll returns the
134 // number of ready fds; select returns the number of bits set.
135 int polled = 0;
136 for (p = pollSet; p < pollEnd; p++)
138 p->revents = 0;
139 if (p->fd < 0) {
140 // Negative fd always reports zero
142 else
144 if ( (p->events & POLLIN) && FD_ISSET(p->fd, readp) )
145 p->revents |= POLLIN;
146 if ( (p->events & POLLOUT) && FD_ISSET(p->fd, writep) )
147 p->revents |= POLLOUT;
148 if ( (p->events != 0) && FD_ISSET(p->fd, exceptp) )
149 p->revents |= POLLERR;
151 if (p->revents)
152 polled++;
155 result = polled;
157 else
159 // selected == 0, select timed out before anything happened
160 // Clear all result bits and return zero.
161 for (p = pollSet; p < pollEnd; p++)
162 p->revents = 0;
164 result = 0;
167 return result;
171 #undef FD_SETSIZE
173 #endif