1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
38 ** The routine _nc_timed_wait().
40 ** (This file was originally written by Eric Raymond; however except for
41 ** comments, none of the original code remains - T.Dickey).
44 #include <curses.priv.h>
46 #if defined __HAIKU__ && defined __BEOS__
58 # include <sys/time.h>
61 # if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
62 # include <sys/time.h>
64 # if HAVE_SYS_SELECT_H
65 # include <sys/select.h>
69 # include <sys/time.h>
73 MODULE_ID("$Id: lib_twait.c,v 1.62 2011/10/22 16:34:50 tom Exp $")
76 _nc_gettime(TimeType * t0, int first)
82 gettimeofday(&t1, (struct timezone *) 0);
87 /* .tv_sec and .tv_usec are unsigned, be careful when subtracting */
88 if (t0->tv_usec > t1.tv_usec) {
89 t1.tv_usec += 1000000; /* Convert 1s in 1e6 microsecs */
92 res = (t1.tv_sec - t0->tv_sec) * 1000
93 + (t1.tv_usec - t0->tv_usec) / 1000;
96 time_t t1 = time((time_t *) 0);
100 res = (t1 - *t0) * 1000;
102 TR(TRACE_IEVENT, ("%s time: %ld msec", first ? "get" : "elapsed", res));
106 #ifdef NCURSES_WGETCH_EVENTS
108 _nc_eventlist_timeout(_nc_eventlist * evl)
110 int event_delay = -1;
115 for (n = 0; n < evl->count; ++n) {
116 _nc_event *ev = evl->events[n];
118 if (ev->type == _NC_EVENT_TIMEOUT_MSEC) {
119 event_delay = ev->data.timeout_msec;
121 event_delay = INT_MAX; /* FIXME Is this defined? */
127 #endif /* NCURSES_WGETCH_EVENTS */
129 #if (USE_FUNC_POLL || HAVE_SELECT)
130 # define MAYBE_UNUSED
132 # define MAYBE_UNUSED GCC_UNUSED
135 #if (USE_FUNC_POLL || HAVE_SELECT)
136 # define MAYBE_UNUSED
138 # define MAYBE_UNUSED GCC_UNUSED
142 * Wait a specified number of milliseconds, returning nonzero if the timer
143 * didn't expire before there is activity on the specified file descriptors.
144 * The file-descriptors are specified by the mode:
145 * TW_NONE 0 - none (absolute time)
146 * TW_INPUT 1 - ncurses' normal input-descriptor
147 * TW_MOUSE 2 - mouse descriptor, if any
148 * TW_ANY 3 - either input or mouse.
150 * Experimental: if NCURSES_WGETCH_EVENTS is defined, (mode & 4) determines
151 * whether to pay attention to evl argument. If set, the smallest of
152 * millisecond and of timeout of evl is taken.
154 * We return a mask that corresponds to the mode (e.g., 2 for mouse activity).
156 * If the milliseconds given are -1, the wait blocks until activity on the
160 _nc_timed_wait(SCREEN *sp MAYBE_UNUSED,
161 int mode MAYBE_UNUSED,
164 EVENTLIST_2nd(_nc_eventlist * evl))
167 int result = TW_NONE;
169 #if (USE_FUNC_POLL || HAVE_SELECT)
173 #ifdef NCURSES_WGETCH_EVENTS
174 int timeout_is_event = 0;
180 struct pollfd fd_list[MIN_FDS];
181 struct pollfd *fds = fd_list;
182 #elif defined(__BEOS__)
187 long starttime, returntime;
189 TR(TRACE_IEVENT, ("start twait: %d milliseconds, mode: %d",
190 milliseconds, mode));
192 #ifdef NCURSES_WGETCH_EVENTS
193 if (mode & TW_EVENT) {
194 int event_delay = _nc_eventlist_timeout(evl);
197 && (milliseconds >= event_delay || milliseconds < 0)) {
198 milliseconds = event_delay;
199 timeout_is_event = 1;
204 #if PRECISE_GETTIME && HAVE_NANOSLEEP
207 starttime = _nc_gettime(&t0, TRUE);
211 #ifdef NCURSES_WGETCH_EVENTS
212 if ((mode & TW_EVENT) && evl)
213 evl->result_flags = 0;
217 memset(fd_list, 0, sizeof(fd_list));
219 #ifdef NCURSES_WGETCH_EVENTS
220 if ((mode & TW_EVENT) && evl)
221 fds = typeMalloc(struct pollfd, MIN_FDS + evl->count);
224 if (mode & TW_INPUT) {
225 fds[count].fd = sp->_ifd;
226 fds[count].events = POLLIN;
229 if ((mode & TW_MOUSE)
230 && (fd = sp->_mouse_fd) >= 0) {
232 fds[count].events = POLLIN;
235 #ifdef NCURSES_WGETCH_EVENTS
236 if ((mode & TW_EVENT) && evl) {
237 for (n = 0; n < evl->count; ++n) {
238 _nc_event *ev = evl->events[n];
240 if (ev->type == _NC_EVENT_FILE
241 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
242 fds[count].fd = ev->data.fev.fd;
243 fds[count].events = POLLIN;
250 result = poll(fds, (size_t) count, milliseconds);
252 #ifdef NCURSES_WGETCH_EVENTS
253 if ((mode & TW_EVENT) && evl) {
259 for (n = 0; n < evl->count; ++n) {
260 _nc_event *ev = evl->events[n];
262 if (ev->type == _NC_EVENT_FILE
263 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
264 ev->data.fev.result = 0;
265 for (c = 0; c < count; c++)
266 if (fds[c].fd == ev->data.fev.fd
267 && fds[c].revents & POLLIN) {
268 ev->data.fev.result |= _NC_EVENT_FILE_READABLE;
269 evl->result_flags |= _NC_EVENT_FILE_READABLE;
271 } else if (ev->type == _NC_EVENT_TIMEOUT_MSEC
272 && !result && timeout_is_event) {
273 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
283 #elif defined(__BEOS__)
285 * BeOS's select() is declared in socket.h, so the configure script does
286 * not see it. That's just as well, since that function works only for
287 * sockets. This (using snooze and ioctl) was distilled from Be's patch
288 * for ncurses which uses a separate thread to simulate select().
290 * FIXME: the return values from the ioctl aren't very clear if we get
293 * FIXME: this assumes mode&1 if milliseconds < 0 (see lib_getch.c).
296 if (mode & TW_INPUT) {
297 int step = (milliseconds < 0) ? 0 : 5000;
299 bigtime_t useconds = milliseconds * 1000;
302 if (useconds <= 0) /* we're here to go _through_ the loop */
305 for (d = 0; d < useconds; d += step) {
307 howmany = ioctl(0, 'ichr', &n);
308 if (howmany >= 0 && n > 0) {
312 if (useconds > 1 && step > 0) {
314 milliseconds -= (step / 1000);
315 if (milliseconds <= 0) {
321 } else if (milliseconds > 0) {
322 snooze(milliseconds * 1000);
327 * select() modifies the fd_set arguments; do this in the
332 if (mode & TW_INPUT) {
333 FD_SET(sp->_ifd, &set);
334 count = sp->_ifd + 1;
336 if ((mode & TW_MOUSE)
337 && (fd = sp->_mouse_fd) >= 0) {
339 count = max(fd, count) + 1;
341 #ifdef NCURSES_WGETCH_EVENTS
342 if ((mode & TW_EVENT) && evl) {
343 for (n = 0; n < evl->count; ++n) {
344 _nc_event *ev = evl->events[n];
346 if (ev->type == _NC_EVENT_FILE
347 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
348 FD_SET(ev->data.fev.fd, &set);
349 count = max(ev->data.fev.fd + 1, count);
355 if (milliseconds >= 0) {
356 struct timeval ntimeout;
357 ntimeout.tv_sec = milliseconds / 1000;
358 ntimeout.tv_usec = (milliseconds % 1000) * 1000;
359 result = select(count, &set, NULL, NULL, &ntimeout);
361 result = select(count, &set, NULL, NULL, NULL);
364 #ifdef NCURSES_WGETCH_EVENTS
365 if ((mode & TW_EVENT) && evl) {
366 evl->result_flags = 0;
367 for (n = 0; n < evl->count; ++n) {
368 _nc_event *ev = evl->events[n];
370 if (ev->type == _NC_EVENT_FILE
371 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
372 ev->data.fev.result = 0;
373 if (FD_ISSET(ev->data.fev.fd, &set)) {
374 ev->data.fev.result |= _NC_EVENT_FILE_READABLE;
375 evl->result_flags |= _NC_EVENT_FILE_READABLE;
377 } else if (ev->type == _NC_EVENT_TIMEOUT_MSEC
378 && !result && timeout_is_event)
379 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
384 #endif /* USE_FUNC_POLL, etc */
386 returntime = _nc_gettime(&t0, FALSE);
388 if (milliseconds >= 0)
389 milliseconds -= (int) (returntime - starttime);
391 #ifdef NCURSES_WGETCH_EVENTS
393 evl->result_flags = 0;
394 for (n = 0; n < evl->count; ++n) {
395 _nc_event *ev = evl->events[n];
397 if (ev->type == _NC_EVENT_TIMEOUT_MSEC) {
398 long diff = (returntime - starttime);
399 if (ev->data.timeout_msec <= diff)
400 ev->data.timeout_msec = 0;
402 ev->data.timeout_msec -= diff;
409 #if PRECISE_GETTIME && HAVE_NANOSLEEP
411 * If the timeout hasn't expired, and we've gotten no data,
412 * this is probably a system where 'select()' needs to be left
413 * alone so that it can complete. Make this process sleep,
414 * then come back for more.
416 if (result == 0 && milliseconds > 100) {
417 napms(100); /* FIXME: this won't be right if I recur! */
423 /* return approximate time left in milliseconds */
425 *timeleft = milliseconds;
427 TR(TRACE_IEVENT, ("end twait: returned %d (%d), remaining time %d msec",
428 result, errno, milliseconds));
431 * Both 'poll()' and 'select()' return the number of file descriptors
432 * that are active. Translate this back to the mask that denotes which
433 * file-descriptors, so that we don't need all of this system-specific
440 for (count = 0; count < MIN_FDS; count++) {
441 if ((mode & (1 << count))
442 && (fds[count].revents & POLLIN)) {
443 result |= (1 << count);
446 #elif defined(__BEOS__)
447 result = TW_INPUT; /* redundant, but simple */
449 if ((mode & TW_MOUSE)
450 && (fd = sp->_mouse_fd) >= 0
451 && FD_ISSET(fd, &set))
453 if ((mode & TW_INPUT)
454 && FD_ISSET(sp->_ifd, &set))
460 #ifdef NCURSES_WGETCH_EVENTS
461 if ((mode & TW_EVENT) && evl && evl->result_flags)