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>
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>
71 MODULE_ID("$Id: lib_twait.c,v 1.59 2008/08/30 20:08:19 tom Exp $")
74 _nc_gettime(TimeType * t0, bool first)
80 gettimeofday(&t1, (struct timezone *) 0);
85 /* .tv_sec and .tv_usec are unsigned, be careful when subtracting */
86 if (t0->tv_usec > t1.tv_usec) {
87 t1.tv_usec += 1000000; /* Convert 1s in 1e6 microsecs */
90 res = (t1.tv_sec - t0->tv_sec) * 1000
91 + (t1.tv_usec - t0->tv_usec) / 1000;
94 time_t t1 = time((time_t *) 0);
98 res = (t1 - *t0) * 1000;
100 TR(TRACE_IEVENT, ("%s time: %ld msec", first ? "get" : "elapsed", res));
104 #ifdef NCURSES_WGETCH_EVENTS
106 _nc_eventlist_timeout(_nc_eventlist * evl)
108 int event_delay = -1;
113 for (n = 0; n < evl->count; ++n) {
114 _nc_event *ev = evl->events[n];
116 if (ev->type == _NC_EVENT_TIMEOUT_MSEC) {
117 event_delay = ev->data.timeout_msec;
119 event_delay = INT_MAX; /* FIXME Is this defined? */
125 #endif /* NCURSES_WGETCH_EVENTS */
128 * Wait a specified number of milliseconds, returning nonzero if the timer
129 * didn't expire before there is activity on the specified file descriptors.
130 * The file-descriptors are specified by the mode:
131 * 0 - none (absolute time)
132 * 1 - ncurses' normal input-descriptor
133 * 2 - mouse descriptor, if any
134 * 3 - either input or mouse.
136 * Experimental: if NCURSES_WGETCH_EVENTS is defined, (mode & 4) determines
137 * whether to pay attention to evl argument. If set, the smallest of
138 * millisecond and of timeout of evl is taken.
140 * We return a mask that corresponds to the mode (e.g., 2 for mouse activity).
142 * If the milliseconds given are -1, the wait blocks until activity on the
146 _nc_timed_wait(SCREEN *sp,
150 EVENTLIST_2nd(_nc_eventlist * evl))
157 #ifdef NCURSES_WGETCH_EVENTS
158 int timeout_is_event = 0;
164 struct pollfd fd_list[MIN_FDS];
165 struct pollfd *fds = fd_list;
166 #elif defined(__BEOS__)
171 long starttime, returntime;
173 TR(TRACE_IEVENT, ("start twait: %d milliseconds, mode: %d",
174 milliseconds, mode));
176 #ifdef NCURSES_WGETCH_EVENTS
178 int event_delay = _nc_eventlist_timeout(evl);
181 && (milliseconds >= event_delay || milliseconds < 0)) {
182 milliseconds = event_delay;
183 timeout_is_event = 1;
188 #if PRECISE_GETTIME && HAVE_NANOSLEEP
191 starttime = _nc_gettime(&t0, TRUE);
195 #ifdef NCURSES_WGETCH_EVENTS
196 if ((mode & 4) && evl)
197 evl->result_flags = 0;
201 memset(fd_list, 0, sizeof(fd_list));
203 #ifdef NCURSES_WGETCH_EVENTS
204 if ((mode & 4) && evl)
205 fds = typeMalloc(struct pollfd, MIN_FDS + evl->count);
209 fds[count].fd = sp->_ifd;
210 fds[count].events = POLLIN;
214 && (fd = sp->_mouse_fd) >= 0) {
216 fds[count].events = POLLIN;
219 #ifdef NCURSES_WGETCH_EVENTS
220 if ((mode & 4) && evl) {
221 for (n = 0; n < evl->count; ++n) {
222 _nc_event *ev = evl->events[n];
224 if (ev->type == _NC_EVENT_FILE
225 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
226 fds[count].fd = ev->data.fev.fd;
227 fds[count].events = POLLIN;
234 result = poll(fds, (unsigned) count, milliseconds);
236 #ifdef NCURSES_WGETCH_EVENTS
237 if ((mode & 4) && evl) {
243 for (n = 0; n < evl->count; ++n) {
244 _nc_event *ev = evl->events[n];
246 if (ev->type == _NC_EVENT_FILE
247 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
248 ev->data.fev.result = 0;
249 for (c = 0; c < count; c++)
250 if (fds[c].fd == ev->data.fev.fd
251 && fds[c].revents & POLLIN) {
252 ev->data.fev.result |= _NC_EVENT_FILE_READABLE;
253 evl->result_flags |= _NC_EVENT_FILE_READABLE;
255 } else if (ev->type == _NC_EVENT_TIMEOUT_MSEC
256 && !result && timeout_is_event) {
257 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
267 #elif defined(__BEOS__)
269 * BeOS's select() is declared in socket.h, so the configure script does
270 * not see it. That's just as well, since that function works only for
271 * sockets. This (using snooze and ioctl) was distilled from Be's patch
272 * for ncurses which uses a separate thread to simulate select().
274 * FIXME: the return values from the ioctl aren't very clear if we get
277 * FIXME: this assumes mode&1 if milliseconds < 0 (see lib_getch.c).
281 int step = (milliseconds < 0) ? 0 : 5000;
283 bigtime_t useconds = milliseconds * 1000;
286 if (useconds <= 0) /* we're here to go _through_ the loop */
289 for (d = 0; d < useconds; d += step) {
291 howmany = ioctl(0, 'ichr', &n);
292 if (howmany >= 0 && n > 0) {
296 if (useconds > 1 && step > 0) {
298 milliseconds -= (step / 1000);
299 if (milliseconds <= 0) {
305 } else if (milliseconds > 0) {
306 snooze(milliseconds * 1000);
311 * select() modifies the fd_set arguments; do this in the
317 FD_SET(sp->_ifd, &set);
318 count = sp->_ifd + 1;
321 && (fd = sp->_mouse_fd) >= 0) {
323 count = max(fd, count) + 1;
325 #ifdef NCURSES_WGETCH_EVENTS
326 if ((mode & 4) && evl) {
327 for (n = 0; n < evl->count; ++n) {
328 _nc_event *ev = evl->events[n];
330 if (ev->type == _NC_EVENT_FILE
331 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
332 FD_SET(ev->data.fev.fd, &set);
333 count = max(ev->data.fev.fd + 1, count);
339 if (milliseconds >= 0) {
340 struct timeval ntimeout;
341 ntimeout.tv_sec = milliseconds / 1000;
342 ntimeout.tv_usec = (milliseconds % 1000) * 1000;
343 result = select(count, &set, NULL, NULL, &ntimeout);
345 result = select(count, &set, NULL, NULL, NULL);
348 #ifdef NCURSES_WGETCH_EVENTS
349 if ((mode & 4) && evl) {
350 evl->result_flags = 0;
351 for (n = 0; n < evl->count; ++n) {
352 _nc_event *ev = evl->events[n];
354 if (ev->type == _NC_EVENT_FILE
355 && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
356 ev->data.fev.result = 0;
357 if (FD_ISSET(ev->data.fev.fd, &set)) {
358 ev->data.fev.result |= _NC_EVENT_FILE_READABLE;
359 evl->result_flags |= _NC_EVENT_FILE_READABLE;
361 } else if (ev->type == _NC_EVENT_TIMEOUT_MSEC
362 && !result && timeout_is_event)
363 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
368 #endif /* USE_FUNC_POLL, etc */
370 returntime = _nc_gettime(&t0, FALSE);
372 if (milliseconds >= 0)
373 milliseconds -= (returntime - starttime);
375 #ifdef NCURSES_WGETCH_EVENTS
377 evl->result_flags = 0;
378 for (n = 0; n < evl->count; ++n) {
379 _nc_event *ev = evl->events[n];
381 if (ev->type == _NC_EVENT_TIMEOUT_MSEC) {
382 long diff = (returntime - starttime);
383 if (ev->data.timeout_msec <= diff)
384 ev->data.timeout_msec = 0;
386 ev->data.timeout_msec -= diff;
393 #if PRECISE_GETTIME && HAVE_NANOSLEEP
395 * If the timeout hasn't expired, and we've gotten no data,
396 * this is probably a system where 'select()' needs to be left
397 * alone so that it can complete. Make this process sleep,
398 * then come back for more.
400 if (result == 0 && milliseconds > 100) {
401 napms(100); /* FIXME: this won't be right if I recur! */
407 /* return approximate time left in milliseconds */
409 *timeleft = milliseconds;
411 TR(TRACE_IEVENT, ("end twait: returned %d (%d), remaining time %d msec",
412 result, errno, milliseconds));
415 * Both 'poll()' and 'select()' return the number of file descriptors
416 * that are active. Translate this back to the mask that denotes which
417 * file-descriptors, so that we don't need all of this system-specific
424 for (count = 0; count < MIN_FDS; count++) {
425 if ((mode & (1 << count))
426 && (fds[count].revents & POLLIN)) {
427 result |= (1 << count);
430 #elif defined(__BEOS__)
431 result = 1; /* redundant, but simple */
434 && (fd = sp->_mouse_fd) >= 0
435 && FD_ISSET(fd, &set))
438 && FD_ISSET(sp->_ifd, &set))
444 #ifdef NCURSES_WGETCH_EVENTS
445 if ((mode & 4) && evl && evl->result_flags)