X-Git-Url: https://ncurses.scripts.mit.edu/?a=blobdiff_plain;f=ncurses%2Ftty%2Flib_twait.c;h=6fa73eedb4cedc06b0b301fffde2371f0e92cee7;hb=bd2d9c5734d2c66abe0b2ddd766695b200c154fe;hp=1d6cf10ecd820733189eedf71b49c3ca711d3a6d;hpb=f486c68b1efe3bab5739c3f464fde6685a52bee5;p=ncurses.git diff --git a/ncurses/tty/lib_twait.c b/ncurses/tty/lib_twait.c index 1d6cf10e..6fa73eed 100644 --- a/ncurses/tty/lib_twait.c +++ b/ncurses/tty/lib_twait.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 1998-2012,2013 Free Software Foundation, Inc. * + * Copyright 2018-2020,2023 Thomas E. Dickey * + * Copyright 1998-2015,2016 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -70,13 +71,16 @@ # include # endif #endif -#ifdef __MINGW32__ +#if HAVE_SYS_TIME_H # include #endif #undef CUR -MODULE_ID("$Id: lib_twait.c,v 1.66 2013/02/16 20:52:07 tom Exp $") +MODULE_ID("$Id: lib_twait.c,v 1.81 2023/09/16 16:30:40 tom Exp $") +/* + * Returns an elapsed time, in milliseconds (if possible). + */ static long _nc_gettime(TimeType * t0, int first) { @@ -84,25 +88,27 @@ _nc_gettime(TimeType * t0, int first) #if PRECISE_GETTIME TimeType t1; - gettimeofday(&t1, (struct timezone *) 0); - if (first) { + if (GetClockTime(&t1) == -1) { + *t0 = t1; + res = first ? 0 : 1; + } else if (first) { *t0 = t1; res = 0; } else { /* .tv_sec and .tv_usec are unsigned, be careful when subtracting */ - if (t0->tv_usec > t1.tv_usec) { - t1.tv_usec += 1000000; /* Convert 1s in 1e6 microsecs */ + if (t0->sub_secs > t1.sub_secs) { + t1.sub_secs += TimeScale; t1.tv_sec--; } - res = (t1.tv_sec - t0->tv_sec) * 1000 - + (t1.tv_usec - t0->tv_usec) / 1000; + res = (long) ((t1.tv_sec - t0->tv_sec) * 1000L + + (t1.sub_secs - t0->sub_secs) / (TimeScale / 1000L)); } #else time_t t1 = time((time_t *) 0); if (first) { *t0 = t1; } - res = (t1 - *t0) * 1000; + res = (long) ((t1 - *t0) * 1000); #endif TR(TRACE_IEVENT, ("%s time: %ld msec", first ? "get" : "elapsed", res)); return res; @@ -113,15 +119,15 @@ NCURSES_EXPORT(int) _nc_eventlist_timeout(_nc_eventlist * evl) { int event_delay = -1; - int n; if (evl != 0) { + int n; for (n = 0; n < evl->count; ++n) { _nc_event *ev = evl->events[n]; if (ev->type == _NC_EVENT_TIMEOUT_MSEC) { - event_delay = ev->data.timeout_msec; + event_delay = (int) ev->data.timeout_msec; if (event_delay < 0) event_delay = INT_MAX; /* FIXME Is this defined? */ } @@ -197,6 +203,10 @@ _nc_timed_wait(SCREEN *sp MAYBE_UNUSED, long starttime, returntime; +#ifdef NCURSES_WGETCH_EVENTS + (void) timeout_is_event; +#endif + TR(TRACE_IEVENT, ("start twait: %d milliseconds, mode: %d", milliseconds, mode)); @@ -230,7 +240,8 @@ _nc_timed_wait(SCREEN *sp MAYBE_UNUSED, #ifdef NCURSES_WGETCH_EVENTS if ((mode & TW_EVENT) && evl) { - fds = typeMalloc(struct pollfd, MIN_FDS + evl->count); + if (fds == fd_list) + fds = typeMalloc(struct pollfd, MIN_FDS + evl->count); if (fds == 0) return TW_NONE; } @@ -349,7 +360,7 @@ _nc_timed_wait(SCREEN *sp MAYBE_UNUSED, if ((mode & TW_MOUSE) && (fd = sp->_mouse_fd) >= 0) { FD_SET(fd, &set); - count = max(fd, count) + 1; + count = Max(fd, count) + 1; } #ifdef NCURSES_WGETCH_EVENTS if ((mode & TW_EVENT) && evl) { @@ -359,7 +370,7 @@ _nc_timed_wait(SCREEN *sp MAYBE_UNUSED, if (ev->type == _NC_EVENT_FILE && (ev->data.fev.flags & _NC_EVENT_FILE_READABLE)) { FD_SET(ev->data.fev.fd, &set); - count = max(ev->data.fev.fd + 1, count); + count = Max(ev->data.fev.fd + 1, count); } } } @@ -501,9 +512,11 @@ _nc_timed_wait(SCREEN *sp MAYBE_UNUSED, result |= TW_EVENT; #endif +#if USE_FUNC_POLL #ifdef NCURSES_WGETCH_EVENTS if (fds != fd_list) free((char *) fds); +#endif #endif return (result);