1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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>
54 # include <sys/time.h>
57 # if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
58 # include <sys/time.h>
60 # if HAVE_SYS_SELECT_H
61 # include <sys/select.h>
67 MODULE_ID("$Id: lib_twait.c,v 1.58 2008/05/24 15:08:01 tom Exp $")
70 _nc_gettime(TimeType * t0, bool first)
76 gettimeofday(&t1, (struct timezone *) 0);
81 /* .tv_sec and .tv_usec are unsigned, be careful when subtracting */
82 if (t0->tv_usec > t1.tv_usec) {
83 t1.tv_usec += 1000000; /* Convert 1s in 1e6 microsecs */
86 res = (t1.tv_sec - t0->tv_sec) * 1000
87 + (t1.tv_usec - t0->tv_usec) / 1000;
90 time_t t1 = time((time_t *) 0);
94 res = (t1 - *t0) * 1000;
96 TR(TRACE_IEVENT, ("%s time: %ld msec", first ? "get" : "elapsed", res));
100 #ifdef NCURSES_WGETCH_EVENTS
102 _nc_eventlist_timeout(_nc_eventlist * evl)
104 int event_delay = -1;
109 for (n = 0; n < evl->count; ++n) {
110 _nc_event *ev = evl->events[n];
112 if (ev->type == _NC_EVENT_TIMEOUT_MSEC) {
113 event_delay = ev->data.timeout_msec;
115 event_delay = INT_MAX; /* FIXME Is this defined? */
121 #endif /* NCURSES_WGETCH_EVENTS */
124 * Wait a specified number of milliseconds, returning nonzero if the timer
125 * didn't expire before there is activity on the specified file descriptors.
126 * The file-descriptors are specified by the mode:
127 * 0 - none (absolute time)
128 * 1 - ncurses' normal input-descriptor
129 * 2 - mouse descriptor, if any
130 * 3 - either input or mouse.
132 * Experimental: if NCURSES_WGETCH_EVENTS is defined, (mode & 4) determines
133 * whether to pay attention to evl argument. If set, the smallest of
134 * millisecond and of timeout of evl is taken.
136 * We return a mask that corresponds to the mode (e.g., 2 for mouse activity).
138 * If the milliseconds given are -1, the wait blocks until activity on the
142 _nc_timed_wait(SCREEN *sp,
146 EVENTLIST_2nd(_nc_eventlist * evl))
153 #ifdef NCURSES_WGETCH_EVENTS
154 int timeout_is_event = 0;
160 struct pollfd fd_list[MIN_FDS];
161 struct pollfd *fds = fd_list;
162 #elif defined(__BEOS__)
167 long starttime, returntime;
169 TR(TRACE_IEVENT, ("start twait: %d milliseconds, mode: %d",
170 milliseconds, mode));
172 #ifdef NCURSES_WGETCH_EVENTS
174 int event_delay = _nc_eventlist_timeout(evl);
177 && (milliseconds >= event_delay || milliseconds < 0)) {
178 milliseconds = event_delay;
179 timeout_is_event = 1;
184 #if PRECISE_GETTIME && HAVE_NANOSLEEP
187 starttime = _nc_gettime(&t0, TRUE);
191 #ifdef NCURSES_WGETCH_EVENTS
192 if ((mode & 4) && evl)
193 evl->result_flags = 0;
197 memset(fd_list, 0, sizeof(fd_list));
199 #ifdef NCURSES_WGETCH_EVENTS
200 if ((mode & 4) && evl)
201 fds = typeMalloc(struct pollfd, MIN_FDS + evl->count);
205 fds[count].fd = sp->_ifd;
206 fds[count].events = POLLIN;
210 && (fd = sp->_mouse_fd) >= 0) {
212 fds[count].events = POLLIN;
215 #ifdef NCURSES_WGETCH_EVENTS
216 if ((mode & 4) && evl) {
217 for (n = 0; n < evl->count; ++n) {
218 _nc_event *ev = evl->events[n];
220 if (ev->type == _NC_EVENT_FILE
221 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
222 fds[count].fd = ev->data.fev.fd;
223 fds[count].events = POLLIN;
230 result = poll(fds, (unsigned) count, milliseconds);
232 #ifdef NCURSES_WGETCH_EVENTS
233 if ((mode & 4) && evl) {
239 for (n = 0; n < evl->count; ++n) {
240 _nc_event *ev = evl->events[n];
242 if (ev->type == _NC_EVENT_FILE
243 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
244 ev->data.fev.result = 0;
245 for (c = 0; c < count; c++)
246 if (fds[c].fd == ev->data.fev.fd
247 && fds[c].revents & POLLIN) {
248 ev->data.fev.result |= _NC_EVENT_FILE_READABLE;
249 evl->result_flags |= _NC_EVENT_FILE_READABLE;
251 } else if (ev->type == _NC_EVENT_TIMEOUT_MSEC
252 && !result && timeout_is_event) {
253 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
263 #elif defined(__BEOS__)
265 * BeOS's select() is declared in socket.h, so the configure script does
266 * not see it. That's just as well, since that function works only for
267 * sockets. This (using snooze and ioctl) was distilled from Be's patch
268 * for ncurses which uses a separate thread to simulate select().
270 * FIXME: the return values from the ioctl aren't very clear if we get
273 * FIXME: this assumes mode&1 if milliseconds < 0 (see lib_getch.c).
277 int step = (milliseconds < 0) ? 0 : 5000;
279 bigtime_t useconds = milliseconds * 1000;
282 if (useconds <= 0) /* we're here to go _through_ the loop */
285 for (d = 0; d < useconds; d += step) {
287 howmany = ioctl(0, 'ichr', &n);
288 if (howmany >= 0 && n > 0) {
292 if (useconds > 1 && step > 0) {
294 milliseconds -= (step / 1000);
295 if (milliseconds <= 0) {
301 } else if (milliseconds > 0) {
302 snooze(milliseconds * 1000);
307 * select() modifies the fd_set arguments; do this in the
313 FD_SET(sp->_ifd, &set);
314 count = sp->_ifd + 1;
317 && (fd = sp->_mouse_fd) >= 0) {
319 count = max(fd, count) + 1;
321 #ifdef NCURSES_WGETCH_EVENTS
322 if ((mode & 4) && evl) {
323 for (n = 0; n < evl->count; ++n) {
324 _nc_event *ev = evl->events[n];
326 if (ev->type == _NC_EVENT_FILE
327 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
328 FD_SET(ev->data.fev.fd, &set);
329 count = max(ev->data.fev.fd + 1, count);
335 if (milliseconds >= 0) {
336 struct timeval ntimeout;
337 ntimeout.tv_sec = milliseconds / 1000;
338 ntimeout.tv_usec = (milliseconds % 1000) * 1000;
339 result = select(count, &set, NULL, NULL, &ntimeout);
341 result = select(count, &set, NULL, NULL, NULL);
344 #ifdef NCURSES_WGETCH_EVENTS
345 if ((mode & 4) && evl) {
346 evl->result_flags = 0;
347 for (n = 0; n < evl->count; ++n) {
348 _nc_event *ev = evl->events[n];
350 if (ev->type == _NC_EVENT_FILE
351 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
352 ev->data.fev.result = 0;
353 if (FD_ISSET(ev->data.fev.fd, &set)) {
354 ev->data.fev.result |= _NC_EVENT_FILE_READABLE;
355 evl->result_flags |= _NC_EVENT_FILE_READABLE;
357 } else if (ev->type == _NC_EVENT_TIMEOUT_MSEC
358 && !result && timeout_is_event)
359 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
364 #endif /* USE_FUNC_POLL, etc */
366 returntime = _nc_gettime(&t0, FALSE);
368 if (milliseconds >= 0)
369 milliseconds -= (returntime - starttime);
371 #ifdef NCURSES_WGETCH_EVENTS
373 evl->result_flags = 0;
374 for (n = 0; n < evl->count; ++n) {
375 _nc_event *ev = evl->events[n];
377 if (ev->type == _NC_EVENT_TIMEOUT_MSEC) {
378 long diff = (returntime - starttime);
379 if (ev->data.timeout_msec <= diff)
380 ev->data.timeout_msec = 0;
382 ev->data.timeout_msec -= diff;
389 #if PRECISE_GETTIME && HAVE_NANOSLEEP
391 * If the timeout hasn't expired, and we've gotten no data,
392 * this is probably a system where 'select()' needs to be left
393 * alone so that it can complete. Make this process sleep,
394 * then come back for more.
396 if (result == 0 && milliseconds > 100) {
397 napms(100); /* FIXME: this won't be right if I recur! */
403 /* return approximate time left in milliseconds */
405 *timeleft = milliseconds;
407 TR(TRACE_IEVENT, ("end twait: returned %d (%d), remaining time %d msec",
408 result, errno, milliseconds));
411 * Both 'poll()' and 'select()' return the number of file descriptors
412 * that are active. Translate this back to the mask that denotes which
413 * file-descriptors, so that we don't need all of this system-specific
420 for (count = 0; count < MIN_FDS; count++) {
421 if ((mode & (1 << count))
422 && (fds[count].revents & POLLIN)) {
423 result |= (1 << count);
426 #elif defined(__BEOS__)
427 result = 1; /* redundant, but simple */
430 && (fd = sp->_mouse_fd) >= 0
431 && FD_ISSET(fd, &set))
434 && FD_ISSET(sp->_ifd, &set))
440 #ifdef NCURSES_WGETCH_EVENTS
441 if ((mode & 4) && evl && evl->result_flags)