1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 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 ****************************************************************************/
37 ** The routine _nc_timed_wait().
39 ** (This file was originally written by Eric Raymond; however except for
40 ** comments, none of the original code remains - T.Dickey).
43 #include <curses.priv.h>
53 # include <sys/time.h>
56 # if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
57 # include <sys/time.h>
59 # if HAVE_SYS_SELECT_H
60 # include <sys/select.h>
64 MODULE_ID("$Id: lib_twait.c,v 1.46 2002/09/01 00:28:18 tom Exp $")
67 _nc_gettime(bool first)
72 # define PRECISE_GETTIME 1
73 static struct timeval t0;
75 gettimeofday(&t1, (struct timezone *) 0);
79 res = (t1.tv_sec - t0.tv_sec) * 1000
80 + (t1.tv_usec - t0.tv_usec) / 1000;
82 # define PRECISE_GETTIME 0
84 time_t t1 = time((time_t *) 0);
88 res = (t1 - t0) * 1000;
90 TR(TRACE_IEVENT, ("%s time: %ld msec", first ? "get" : "elapsed", res));
94 #ifdef NCURSES_WGETCH_EVENTS
96 _nc_eventlist_timeout(_nc_eventlist * evl)
98 _nc_event **ev, **last;
104 last = ev + evl->count;
107 if ((*ev)->type == _NC_EVENT_TIMEOUT_MSEC) {
108 event_delay = (*ev)->data.timeout_msec;
110 event_delay = LONG_MAX; /* FIXME Is this defined? */
116 #endif /* NCURSES_WGETCH_EVENTS */
119 * Wait a specified number of milliseconds, returning nonzero if the timer
120 * didn't expire before there is activity on the specified file descriptors.
121 * The file-descriptors are specified by the mode:
122 * 0 - none (absolute time)
123 * 1 - ncurses' normal input-descriptor
124 * 2 - mouse descriptor, if any
125 * 3 - either input or mouse.
127 * Experimental: if NCURSES_WGETCH_EVENTS is defined, (mode & 4) determines
128 * whether to pay attention to evl argument. If set, the smallest of
129 * millisecond and of timeout of evl is taken.
131 * We return a mask that corresponds to the mode (e.g., 2 for mouse activity).
133 * If the milliseconds given are -1, the wait blocks until activity on the
137 _nc_timed_wait(int mode,
140 EVENTLIST_2nd(_nc_eventlist * evl))
146 #ifdef NCURSES_WGETCH_EVENTS
147 int timeout_is_event = 0;
151 struct pollfd fd_list[2];
152 struct pollfd *fds = fd_list;
153 #elif defined(__BEOS__)
158 long starttime, returntime;
160 TR(TRACE_IEVENT, ("start twait: %d milliseconds, mode: %d",
161 milliseconds, mode));
163 #ifdef NCURSES_WGETCH_EVENTS
165 int event_delay = _nc_eventlist_timeout(evl);
168 && (milliseconds >= event_delay || milliseconds < 0)) {
169 milliseconds = event_delay;
170 timeout_is_event = 1;
178 starttime = _nc_gettime(TRUE);
182 #ifdef NCURSES_WGETCH_EVENTS
183 if ((mode & 4) && evl)
184 evl->result_flags = 0;
188 memset(fd_list, 0, sizeof(fd_list));
190 #ifdef NCURSES_WGETCH_EVENTS
191 if ((mode & 4) && evl)
192 fds = typeMalloc(struct pollfd, 2 + evl->count);
196 fds[count].fd = SP->_ifd;
197 fds[count].events = POLLIN;
201 && (fd = SP->_mouse_fd) >= 0) {
203 fds[count].events = POLLIN;
206 #ifdef NCURSES_WGETCH_EVENTS
207 if ((mode & 4) && evl) {
208 _nc_event **ev = evl->events;
209 _nc_event **last = ev + evl->count;
212 if ((*ev)->type == _NC_EVENT_FILE
213 && ((*ev)->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
214 fds[count].fd = (*ev)->data.fev.fd;
215 fds[count].events = POLLIN;
222 result = poll(fds, count, milliseconds);
224 #ifdef NCURSES_WGETCH_EVENTS
225 if ((mode & 4) && evl) {
226 _nc_event **ev = evl->events;
227 _nc_event **last = ev + evl->count;
233 if ((*ev)->type == _NC_EVENT_FILE
234 && ((*ev)->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
235 (*ev)->data.fev.result = 0;
236 for (c = 0; c < count; c++)
237 if (fds[c].fd == (*ev)->data.fev.fd
238 && fds[c].revents & POLLIN) {
239 (*ev)->data.fev.result |= _NC_EVENT_FILE_READABLE;
240 evl->result_flags |= _NC_EVENT_FILE_READABLE;
242 } else if ((*ev)->type == _NC_EVENT_TIMEOUT_MSEC
243 && !result && timeout_is_event) {
244 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
254 #elif defined(__BEOS__)
256 * BeOS's select() is declared in socket.h, so the configure script does
257 * not see it. That's just as well, since that function works only for
258 * sockets. This (using snooze and ioctl) was distilled from Be's patch
259 * for ncurses which uses a separate thread to simulate select().
261 * FIXME: the return values from the ioctl aren't very clear if we get
264 * FIXME: this assumes mode&1 if milliseconds < 0 (see lib_getch.c).
268 int step = (milliseconds < 0) ? 0 : 5000;
270 bigtime_t useconds = milliseconds * 1000;
273 if (useconds <= 0) /* we're here to go _through_ the loop */
276 for (d = 0; d < useconds; d += step) {
278 howmany = ioctl(0, 'ichr', &n);
279 if (howmany >= 0 && n > 0) {
283 if (useconds > 1 && step > 0) {
285 milliseconds -= (step / 1000);
286 if (milliseconds <= 0) {
292 } else if (milliseconds > 0) {
293 snooze(milliseconds * 1000);
298 * select() modifies the fd_set arguments; do this in the
304 FD_SET(SP->_ifd, &set);
305 count = SP->_ifd + 1;
308 && (fd = SP->_mouse_fd) >= 0) {
310 count = max(fd, count) + 1;
312 #ifdef NCURSES_WGETCH_EVENTS
313 if ((mode & 4) && evl) {
314 _nc_event **ev = evl->events;
315 _nc_event **last = ev + evl->count;
318 if ((*ev)->type == _NC_EVENT_FILE
319 && ((*ev)->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
320 FD_SET((*ev)->data.fev.fd, &set);
321 count = max((*ev)->data.fev.fd + 1, count);
327 if (milliseconds >= 0) {
328 struct timeval ntimeout;
329 ntimeout.tv_sec = milliseconds / 1000;
330 ntimeout.tv_usec = (milliseconds % 1000) * 1000;
331 result = select(count, &set, NULL, NULL, &ntimeout);
333 result = select(count, &set, NULL, NULL, NULL);
336 #ifdef NCURSES_WGETCH_EVENTS
337 if ((mode & 4) && evl) {
338 _nc_event **ev = evl->events;
339 _nc_event **last = ev + evl->count;
341 evl->result_flags = 0;
343 if ((*ev)->type == _NC_EVENT_FILE
344 && ((*ev)->data.fev.flags & _NC_EVENT_FILE_READABLE)) {
345 (*ev)->data.fev.result = 0;
346 if (FD_ISSET((*ev)->data.fev.fd, &set)) {
347 (*ev)->data.fev.result |= _NC_EVENT_FILE_READABLE;
348 evl->result_flags |= _NC_EVENT_FILE_READABLE;
350 } else if ((*ev)->type == _NC_EVENT_TIMEOUT_MSEC
351 && !result && timeout_is_event)
352 evl->result_flags |= _NC_EVENT_TIMEOUT_MSEC;
357 #endif /* USE_FUNC_POLL, etc */
359 returntime = _nc_gettime(FALSE);
361 if (milliseconds >= 0)
362 milliseconds -= (returntime - starttime);
364 #ifdef NCURSES_WGETCH_EVENTS
366 _nc_event **ev = evl->events;
367 _nc_event **last = ev + evl->count;
369 evl->result_flags = 0;
371 if ((*ev)->type == _NC_EVENT_TIMEOUT_MSEC) {
372 long diff = (returntime - starttime);
373 if ((*ev)->data.timeout_msec <= diff)
374 (*ev)->data.timeout_msec = 0;
376 (*ev)->data.timeout_msec -= diff;
385 * If the timeout hasn't expired, and we've gotten no data,
386 * this is probably a system where 'select()' needs to be left
387 * alone so that it can complete. Make this process sleep,
388 * then come back for more.
390 if (result == 0 && milliseconds > 100) {
397 /* return approximate time left in milliseconds */
399 *timeleft = milliseconds;
401 TR(TRACE_IEVENT, ("end twait: returned %d (%d), remaining time %d msec",
402 result, errno, milliseconds));
405 * Both 'poll()' and 'select()' return the number of file descriptors
406 * that are active. Translate this back to the mask that denotes which
407 * file-descriptors, so that we don't need all of this system-specific
414 for (count = 0; count < 2; count++) {
415 if ((mode & (1 << count))
416 && (fds[count].revents & POLLIN)) {
417 result |= (1 << count);
420 #elif defined(__BEOS__)
421 result = 1; /* redundant, but simple */
424 && (fd = SP->_mouse_fd) >= 0
425 && FD_ISSET(fd, &set))
428 && FD_ISSET(SP->_ifd, &set))
434 #ifdef NCURSES_WGETCH_EVENTS
435 if ((mode & 4) && evl && evl->result_flags)