2 /***************************************************************************
4 ****************************************************************************
5 * ncurses is copyright (C) 1992-1995 *
7 * zmbenhal@netcom.com *
9 * esr@snark.thyrsus.com *
11 * Permission is hereby granted to reproduce and distribute ncurses *
12 * by any means and for any fee, whether alone or as part of a *
13 * larger distribution, in source or in binary form, PROVIDED *
14 * this notice is included with any such distribution, and is not *
15 * removed from any of its header files. Mention of ncurses in any *
16 * applications linked with it is highly appreciated. *
18 * ncurses comes AS IS with no warranty, implied or expressed. *
20 ***************************************************************************/
25 ** The routine _nc_timed_wait().
29 #include <curses.priv.h>
38 /* on SCO, <sys/time.h> conflicts with <sys/select.h> */
39 #if HAVE_SYS_TIME_H && ! SYSTEM_LOOKS_LIKE_SCO
43 #include <sys/select.h>
47 MODULE_ID("$Id: lib_twait.c,v 1.18 1997/02/15 18:27:51 tom Exp $")
50 * We want to define GOOD_SELECT if the last argument of select(2) is
51 * modified to indicate time left. The code will deal gracefully with
52 * the other case, this is just an optimization to reduce the number
53 * of system calls per input event.
55 * In general, expect System-V-like UNIXes to have this behavior and BSD-like
56 * ones to not have it. Check your manual page. If it doesn't explicitly
57 * say the last argument is modified, assume it's not.
59 * (We'd really like configure to autodetect this, but writing a proper test
60 * turns out to be hard.)
64 #if (defined(TRACE) && !HAVE_USLEEP) || ! GOOD_SELECT
65 static void _nc_gettime(struct timeval *tp)
67 gettimeofday(tp, (struct timezone *)0);
68 T(("time: %ld.%06ld", tp->tv_sec, tp->tv_usec));
74 int _nc_usleep(unsigned int usec)
79 #if defined(TRACE) && HAVE_GETTIMEOFDAY
85 code = poll(fds, 0, usec / 1000);
88 tval.tv_sec = usec / 1000000;
89 tval.tv_usec = usec % 1000000;
90 code = select(0, NULL, NULL, NULL, &tval);
93 #if defined(TRACE) && HAVE_GETTIMEOFDAY
98 #endif /* !HAVE_USLEEP */
101 * Wait a specified number of milliseconds, returning true if the timer
102 * didn't expire before there is activity on the specified file descriptors.
103 * The file-descriptors are specified by the mode:
104 * 0 - none (absolute time)
105 * 1 - ncurses' normal input-descriptor
106 * 2 - mouse descriptor, if any
107 * 3 - either input or mouse.
109 * If the milliseconds given are -1, the wait blocks until activity on the
112 int _nc_timed_wait(int mode, int milliseconds, int *timeleft)
116 long whole_secs = milliseconds / 1000;
117 long micro_secs = (milliseconds % 1000) * 1000;
120 struct timeval ntimeout;
123 struct pollfd fds[2];
128 #if !GOOD_SELECT && HAVE_GETTIMEOFDAY
129 struct timeval starttime, returntime;
132 _nc_gettime(&starttime);
135 if (milliseconds >= 0) {
136 ntimeout.tv_sec = whole_secs;
137 ntimeout.tv_usec = micro_secs;
140 ntimeout.tv_usec = 0;
143 T(("start twait: %lu.%06lu secs", (long) ntimeout.tv_sec, (long) ntimeout.tv_usec));
146 * The do loop tries to make it look like we have restarting signals,
154 fds[count].fd = SP->_ifd;
155 fds[count].events = POLLIN;
159 && (fd = _nc_mouse_fd()) >= 0) {
161 fds[count].events = POLLIN;
165 result = poll(fds, count, milliseconds);
168 * Some systems modify the fd_set arguments; do this in the
174 FD_SET(SP->_ifd, &set);
175 count = SP->_ifd + 1;
178 && (fd = _nc_mouse_fd()) >= 0) {
180 count = max(fd, count) + 1;
184 result = select(count, &set, NULL, NULL, milliseconds >= 0 ? &ntimeout : 0);
187 #if !GOOD_SELECT && HAVE_GETTIMEOFDAY
188 _nc_gettime(&returntime);
190 /* The contents of ntimeout aren't guaranteed after return from
191 * 'select()', so we disregard its contents. Also, note that
192 * on some systems, tv_sec and tv_usec are unsigned.
194 ntimeout.tv_sec = whole_secs;
195 ntimeout.tv_usec = micro_secs;
197 #define DELTA(f) (long)ntimeout.f - (long)returntime.f + (long)starttime.f
199 delta = DELTA(tv_sec);
202 ntimeout.tv_sec = delta;
204 delta = DELTA(tv_usec);
205 while (delta < 0 && ntimeout.tv_sec != 0) {
209 ntimeout.tv_usec = delta;
211 ntimeout.tv_sec = ntimeout.tv_usec = 0;
214 * If the timeout hasn't expired, and we've gotten no data,
215 * this is probably a system where 'select()' needs to be left
216 * alone so that it can complete. Make this process sleep,
217 * then come back for more.
220 && (ntimeout.tv_sec != 0 || ntimeout.tv_usec > 100000)) {
225 } while (result == -1 && errno == EINTR);
227 /* return approximate time left on the ntimeout, in milliseconds */
229 *timeleft = (ntimeout.tv_sec * 1000) + (ntimeout.tv_usec / 1000);
231 T(("end twait: returned %d, remaining time %lu.%06lu secs (%d msec)",
232 result, (long) ntimeout.tv_sec, (long) ntimeout.tv_usec,
233 timeleft ? *timeleft : -1));
236 * Both 'poll()' and 'select()' return the number of file descriptors
237 * that are active. Translate this back to the mask that denotes which
238 * file-descriptors, so that we don't need all of this system-specific
245 for (count = 0; count < 2; count++) {
246 if ((mode & (1 << count))
247 && (fds[count].revents & POLLIN)) {
248 result |= (1 << count);
254 && (fd = _nc_mouse_fd()) >= 0
255 && FD_ISSET(fd, &set))
258 && FD_ISSET(SP->_ifd, &set))