1 /****************************************************************************
2 * Copyright (c) 1998-2012,2013 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__
63 # include <sys/time.h>
66 # if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
67 # include <sys/time.h>
69 # if HAVE_SYS_SELECT_H
70 # include <sys/select.h>
74 # include <sys/time.h>
78 MODULE_ID("$Id: lib_twait.c,v 1.67 2013/02/18 09:22:27 tom Exp $")
81 _nc_gettime(TimeType * t0, int first)
87 gettimeofday(&t1, (struct timezone *) 0);
92 /* .tv_sec and .tv_usec are unsigned, be careful when subtracting */
93 if (t0->tv_usec > t1.tv_usec) {
94 t1.tv_usec += 1000000; /* Convert 1s in 1e6 microsecs */
97 res = (t1.tv_sec - t0->tv_sec) * 1000
98 + (t1.tv_usec - t0->tv_usec) / 1000;
101 time_t t1 = time((time_t *) 0);
105 res = (t1 - *t0) * 1000;
107 TR(TRACE_IEVENT, ("%s time: %ld msec", first ? "get" : "elapsed", res));
111 #ifdef NCURSES_WGETCH_EVENTS
113 _nc_eventlist_timeout(_nc_eventlist * evl)
115 int event_delay = -1;
120 for (n = 0; n < evl->count; ++n) {
121 _nc_event *ev = evl->events[n];
123 if (ev->type == _NC_EVENT_TIMEOUT_MSEC) {
124 event_delay = ev->data.timeout_msec;
126 event_delay = INT_MAX; /* FIXME Is this defined? */
132 #endif /* NCURSES_WGETCH_EVENTS */
134 #if (USE_FUNC_POLL || HAVE_SELECT)
135 # define MAYBE_UNUSED
137 # define MAYBE_UNUSED GCC_UNUSED
140 #if (USE_FUNC_POLL || HAVE_SELECT)
141 # define MAYBE_UNUSED
143 # define MAYBE_UNUSED GCC_UNUSED
147 * Wait a specified number of milliseconds, returning nonzero if the timer
148 * didn't expire before there is activity on the specified file descriptors.
149 * The file-descriptors are specified by the mode:
150 * TW_NONE 0 - none (absolute time)
151 * TW_INPUT 1 - ncurses' normal input-descriptor
152 * TW_MOUSE 2 - mouse descriptor, if any
153 * TW_ANY 3 - either input or mouse.
155 * Experimental: if NCURSES_WGETCH_EVENTS is defined, (mode & 4) determines
156 * whether to pay attention to evl argument. If set, the smallest of
157 * millisecond and of timeout of evl is taken.
159 * We return a mask that corresponds to the mode (e.g., 2 for mouse activity).
161 * If the milliseconds given are -1, the wait blocks until activity on the
165 _nc_timed_wait(SCREEN *sp MAYBE_UNUSED,
166 int mode MAYBE_UNUSED,
169 EVENTLIST_2nd(_nc_eventlist * evl))
172 int result = TW_NONE;
174 #if (USE_FUNC_POLL || HAVE_SELECT)
178 #ifdef NCURSES_WGETCH_EVENTS
179 int timeout_is_event = 0;
185 struct pollfd fd_list[MIN_FDS];
186 struct pollfd *fds = fd_list;
187 #elif defined(__BEOS__)
198 long starttime, returntime;
200 TR(TRACE_IEVENT, ("start twait: %d milliseconds, mode: %d",
201 milliseconds, mode));
203 #ifdef NCURSES_WGETCH_EVENTS
204 if (mode & TW_EVENT) {
205 int event_delay = _nc_eventlist_timeout(evl);
208 && (milliseconds >= event_delay || milliseconds < 0)) {
209 milliseconds = event_delay;
210 timeout_is_event = 1;
215 #if PRECISE_GETTIME && HAVE_NANOSLEEP
218 starttime = _nc_gettime(&t0, TRUE);
223 #ifdef NCURSES_WGETCH_EVENTS
224 if ((mode & TW_EVENT) && evl)
225 evl->result_flags = 0;
229 memset(fd_list, 0, sizeof(fd_list));
231 #ifdef NCURSES_WGETCH_EVENTS
232 if ((mode & TW_EVENT) && evl) {
233 fds = typeMalloc(struct pollfd, MIN_FDS + evl->count);
239 if (mode & TW_INPUT) {
240 fds[count].fd = sp->_ifd;
241 fds[count].events = POLLIN;
244 if ((mode & TW_MOUSE)
245 && (fd = sp->_mouse_fd) >= 0) {
247 fds[count].events = POLLIN;
250 #ifdef NCURSES_WGETCH_EVENTS
251 if ((mode & TW_EVENT) && evl) {
252 for (n = 0; n < evl->count; ++n) {
253 _nc_event *ev = evl->events[n];
255 if (ev->type == _NC_EVENT_FILE
256 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
257 fds[count].fd = ev->data.fev.fd;
258 fds[count].events = POLLIN;
265 result = poll(fds, (size_t) count, milliseconds);
267 #ifdef NCURSES_WGETCH_EVENTS
268 if ((mode & TW_EVENT) && evl) {
274 for (n = 0; n < evl->count; ++n) {
275 _nc_event *ev = evl->events[n];
277 if (ev->type == _NC_EVENT_FILE
278 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
279 ev->data.fev.result = 0;
280 for (c = 0; c < count; c++)
281 if (fds[c].fd == ev->data.fev.fd
282 && fds[c].revents & POLLIN) {
283 ev->data.fev.result |= _NC_EVENT_FILE_READABLE;
284 evl->result_flags |= _NC_EVENT_FILE_READABLE;
286 } else if (ev->type == _NC_EVENT_TIMEOUT_MSEC
287 && !result && timeout_is_event) {
288 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
294 #elif defined(__BEOS__)
296 * BeOS's select() is declared in socket.h, so the configure script does
297 * not see it. That's just as well, since that function works only for
298 * sockets. This (using snooze and ioctl) was distilled from Be's patch
299 * for ncurses which uses a separate thread to simulate select().
301 * FIXME: the return values from the ioctl aren't very clear if we get
304 * FIXME: this assumes mode&1 if milliseconds < 0 (see lib_getch.c).
307 if (mode & TW_INPUT) {
308 int step = (milliseconds < 0) ? 0 : 5000;
310 bigtime_t useconds = milliseconds * 1000;
313 if (useconds <= 0) /* we're here to go _through_ the loop */
316 for (d = 0; d < useconds; d += step) {
318 howmany = ioctl(0, 'ichr', &n);
319 if (howmany >= 0 && n > 0) {
323 if (useconds > 1 && step > 0) {
325 milliseconds -= (step / 1000);
326 if (milliseconds <= 0) {
332 } else if (milliseconds > 0) {
333 snooze(milliseconds * 1000);
338 * select() modifies the fd_set arguments; do this in the
344 if (mode & TW_INPUT) {
345 FD_SET(sp->_ifd, &set);
346 count = sp->_ifd + 1;
349 if ((mode & TW_MOUSE)
350 && (fd = sp->_mouse_fd) >= 0) {
352 count = max(fd, count) + 1;
354 #ifdef NCURSES_WGETCH_EVENTS
355 if ((mode & TW_EVENT) && evl) {
356 for (n = 0; n < evl->count; ++n) {
357 _nc_event *ev = evl->events[n];
359 if (ev->type == _NC_EVENT_FILE
360 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
361 FD_SET(ev->data.fev.fd, &set);
362 count = max(ev->data.fev.fd + 1, count);
369 for (saved_set = set;; set = saved_set) {
370 if ((mode & TW_INPUT)
371 && (sp->_extended_key
372 || (KbdPeek(&ki, 0) == 0
373 && (ki.fbStatus & KBDTRF_FINAL_CHAR_IN)))) {
375 FD_SET(sp->_ifd, &set);
381 tv.tv_usec = (milliseconds == 0) ? 0 : (10 * 1000);
383 if ((result = select(count, &set, NULL, NULL, &tv)) != 0)
387 if (milliseconds >= 0 && _nc_gettime(&t0, FALSE) >= milliseconds) {
393 if (milliseconds >= 0) {
394 struct timeval ntimeout;
395 ntimeout.tv_sec = milliseconds / 1000;
396 ntimeout.tv_usec = (milliseconds % 1000) * 1000;
397 result = select(count, &set, NULL, NULL, &ntimeout);
399 result = select(count, &set, NULL, NULL, NULL);
403 #ifdef NCURSES_WGETCH_EVENTS
404 if ((mode & TW_EVENT) && evl) {
405 evl->result_flags = 0;
406 for (n = 0; n < evl->count; ++n) {
407 _nc_event *ev = evl->events[n];
409 if (ev->type == _NC_EVENT_FILE
410 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
411 ev->data.fev.result = 0;
412 if (FD_ISSET(ev->data.fev.fd, &set)) {
413 ev->data.fev.result |= _NC_EVENT_FILE_READABLE;
414 evl->result_flags |= _NC_EVENT_FILE_READABLE;
416 } else if (ev->type == _NC_EVENT_TIMEOUT_MSEC
417 && !result && timeout_is_event)
418 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
423 #endif /* USE_FUNC_POLL, etc */
425 returntime = _nc_gettime(&t0, FALSE);
427 if (milliseconds >= 0)
428 milliseconds -= (int) (returntime - starttime);
430 #ifdef NCURSES_WGETCH_EVENTS
432 evl->result_flags = 0;
433 for (n = 0; n < evl->count; ++n) {
434 _nc_event *ev = evl->events[n];
436 if (ev->type == _NC_EVENT_TIMEOUT_MSEC) {
437 long diff = (returntime - starttime);
438 if (ev->data.timeout_msec <= diff)
439 ev->data.timeout_msec = 0;
441 ev->data.timeout_msec -= diff;
448 #if PRECISE_GETTIME && HAVE_NANOSLEEP
450 * If the timeout hasn't expired, and we've gotten no data,
451 * this is probably a system where 'select()' needs to be left
452 * alone so that it can complete. Make this process sleep,
453 * then come back for more.
455 if (result == 0 && milliseconds > 100) {
456 napms(100); /* FIXME: this won't be right if I recur! */
462 /* return approximate time left in milliseconds */
464 *timeleft = milliseconds;
466 TR(TRACE_IEVENT, ("end twait: returned %d (%d), remaining time %d msec",
467 result, errno, milliseconds));
470 * Both 'poll()' and 'select()' return the number of file descriptors
471 * that are active. Translate this back to the mask that denotes which
472 * file-descriptors, so that we don't need all of this system-specific
479 for (count = 0; count < MIN_FDS; count++) {
480 if ((mode & (1 << count))
481 && (fds[count].revents & POLLIN)) {
482 result |= (1 << count);
485 #elif defined(__BEOS__)
486 result = TW_INPUT; /* redundant, but simple */
488 if ((mode & TW_MOUSE)
489 && (fd = sp->_mouse_fd) >= 0
490 && FD_ISSET(fd, &set))
492 if ((mode & TW_INPUT)
493 && FD_ISSET(sp->_ifd, &set))
499 #ifdef NCURSES_WGETCH_EVENTS
500 if ((mode & TW_EVENT) && evl && evl->result_flags)
505 #ifdef NCURSES_WGETCH_EVENTS