1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2014 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU General Public License, as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any
* later version. Please see the file LICENSE-GPL for details.
*
* Web Page: http://mielke.cc/brltty/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <errno.h>
#include <time.h>
#ifdef HAVE_GETTIMEOFDAY
#include <sys/time.h>
#endif /* HAVE_GETTIMEOFDAY */
#ifdef HAVE_SYS_POLL_H
#include <sys/poll.h>
#endif /* HAVE_SYS_POLL_H */
#ifdef HAVE_SELECT
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#else /* HAVE_SYS_SELECT_H */
#include <sys/time.h>
#endif /* HAVE_SYS_SELECT_H */
#endif /* HAVE_SELECT */
#include "log.h"
#include "timing.h"
#ifdef __MSDOS__
#include "system_msdos.h"
#endif /* __MSDOS__ */
#if !HAVE_DECL_LOCALTIME_R
static inline struct tm *
localtime_r (const time_t *timep, struct tm *result) {
*result = *localtime(timep);
return result;
}
#endif /* HAVE_DECL_LOCALTIME_R */
void
getCurrentTime (TimeValue *now) {
#if defined(GRUB_RUNTIME)
static time_t baseSeconds = 0;
static uint64_t baseMilliseconds;
if (!baseSeconds) {
baseSeconds = time(NULL);
baseMilliseconds = grub_get_time_ms();
}
{
uint64_t milliseconds = grub_get_time_ms() - baseMilliseconds;
now->seconds = baseSeconds + (milliseconds / MSECS_PER_SEC);
now->nanoseconds = (milliseconds % MSECS_PER_SEC) * NSECS_PER_MSEC;
}
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
now->seconds = ts.tv_sec;
now->nanoseconds = ts.tv_nsec;
#elif defined(HAVE_GETTIMEOFDAY)
struct timeval tv;
gettimeofday(&tv, NULL);
now->seconds = tv.tv_sec;
now->nanoseconds = tv.tv_usec * USECS_PER_MSEC;
#elif defined(HAVE_TIME)
now->seconds = time(NULL);
now->nanoseconds = 0;
#else /* get current time */
now->seconds = 0;
now->nanoseconds = 0;
#endif /* get current time */
}
void
makeTimeValue (TimeValue *value, const TimeComponents *components) {
value->nanoseconds = components->nanosecond;
#if defined(GRUB_RUNTIME)
value->seconds = 0;
#else /* make seconds */
struct tm time = {
.tm_year = components->year - 1900,
.tm_mon = components->month,
.tm_mday = components->day + 1,
.tm_hour = components->hour,
.tm_min = components->minute,
.tm_sec = components->second,
.tm_isdst = -1
};
value->seconds = mktime(&time);
#endif /* make seconds */
}
void
expandTimeValue (const TimeValue *value, TimeComponents *components) {
time_t seconds = value->seconds;
struct tm time;
localtime_r(&seconds, &time);
components->nanosecond = value->nanoseconds;
#if defined(GRUB_RUNTIME)
components->year = time.tm.year;
components->month = time.tm.month - 1;
components->day = time.tm.day - 1;
components->hour = time.tm.hour;
components->minute = time.tm.minute;
components->second = time.tm.second;
#else /* expand seconds */
components->year = time.tm_year + 1900;
components->month = time.tm_mon;
components->day = time.tm_mday - 1;
components->hour = time.tm_hour;
components->minute = time.tm_min;
components->second = time.tm_sec;
#endif /* expand seconds */
}
size_t
formatSeconds (char *buffer, size_t size, const char *format, int32_t seconds) {
time_t time = seconds;
struct tm description;
localtime_r(&time, &description);
return strftime(buffer, size, format, &description);
}
void
normalizeTimeValue (TimeValue *time) {
while (time->nanoseconds < 0) {
time->seconds -= 1;
time->nanoseconds += NSECS_PER_SEC;
}
while (time->nanoseconds >= NSECS_PER_SEC) {
time->seconds += 1;
time->nanoseconds -= NSECS_PER_SEC;
}
}
void
adjustTimeValue (TimeValue *time, int milliseconds) {
TimeValue amount = {
.seconds = milliseconds / MSECS_PER_SEC,
.nanoseconds = (milliseconds % MSECS_PER_SEC) * NSECS_PER_MSEC
};
normalizeTimeValue(time);
normalizeTimeValue(&amount);
time->seconds += amount.seconds;
time->nanoseconds += amount.nanoseconds;
normalizeTimeValue(time);
}
int
compareTimeValues (const TimeValue *first, const TimeValue *second) {
if (first->seconds < second->seconds) return -1;
if (first->seconds > second->seconds) return 1;
if (first->nanoseconds < second->nanoseconds) return -1;
if (first->nanoseconds > second->nanoseconds) return 1;
return 0;
}
long int
millisecondsBetween (const TimeValue *from, const TimeValue *to) {
TimeValue elapsed = {
.seconds = to->seconds - from->seconds,
.nanoseconds = to->nanoseconds - from->nanoseconds
};
normalizeTimeValue(&elapsed);
return (elapsed.seconds * MSECS_PER_SEC)
+ (elapsed.nanoseconds / NSECS_PER_MSEC);
}
long int
millisecondsTillNextSecond (const TimeValue *reference) {
TimeValue time = *reference;
time.nanoseconds = 0;
time.seconds += 1;
return millisecondsBetween(reference, &time);
}
long int
millisecondsTillNextMinute (const TimeValue *reference) {
TimeValue time = *reference;
int32_t *seconds = &time.seconds;
time.nanoseconds = 0;
*seconds /= SECS_PER_MIN;
*seconds += 1;
*seconds *= SECS_PER_MIN;
return millisecondsBetween(reference, &time);
}
void
getMonotonicTime (TimeValue *now) {
#if defined(GRUB_RUNTIME)
grub_uint64_t milliseconds = grub_get_time_ms();
now->seconds = milliseconds / MSECS_PER_SEC;
now->nanoseconds = (milliseconds % MSECS_PER_SEC) * NSECS_PER_MSEC;
#elif defined(CLOCK_REALTIME)
static const clockid_t clocks[] = {
#ifdef CLOCK_MONOTONIC_RAW
CLOCK_MONOTONIC_RAW,
#endif /* CLOCK_MONOTONIC_RAW */
#ifdef CLOCK_MONOTONIC_HR
CLOCK_MONOTONIC_HR,
#endif /* CLOCK_MONOTONIC_HR */
#ifdef CLOCK_MONOTONIC
CLOCK_MONOTONIC,
#endif /* CLOCK_MONOTONIC */
CLOCK_REALTIME
};
static const clockid_t *clock = clocks;
while (*clock != CLOCK_REALTIME) {
struct timespec ts;
if (clock_gettime(*clock, &ts) != -1) {
now->seconds = ts.tv_sec;
now->nanoseconds = ts.tv_nsec;
return;
}
logMessage(LOG_WARNING, "clock not available: %u", (unsigned int)*clock);
clock += 1;
}
#endif /* get monotonic time */
getCurrentTime(now);
}
long int
getMonotonicElapsed (const TimeValue *start) {
TimeValue now;
getMonotonicTime(&now);
return millisecondsBetween(start, &now);
}
void
restartTimePeriod (TimePeriod *period) {
getMonotonicTime(&period->start);
}
void
startTimePeriod (TimePeriod *period, long int length) {
period->length = length;
restartTimePeriod(period);
}
int
afterTimePeriod (const TimePeriod *period, long int *elapsed) {
long int milliseconds = getMonotonicElapsed(&period->start);
if (elapsed) *elapsed = milliseconds;
return milliseconds >= period->length;
}
void
approximateDelay (int milliseconds) {
if (milliseconds > 0) {
#if defined(__MINGW32__)
Sleep(milliseconds);
#elif defined(__MSDOS__)
msdosUSleep(milliseconds * USECS_PER_MSEC);
#elif defined (GRUB_RUNTIME)
grub_millisleep(milliseconds);
#elif defined(HAVE_NANOSLEEP)
const struct timespec timeout = {
.tv_sec = milliseconds / MSECS_PER_SEC,
.tv_nsec = (milliseconds % MSECS_PER_SEC) * NSECS_PER_MSEC
};
if (nanosleep(&timeout, NULL) == -1) {
if (errno != EINTR) logSystemError("nanosleep");
}
#elif defined(HAVE_SYS_POLL_H)
if (poll(NULL, 0, milliseconds) == -1) {
if (errno != EINTR) logSystemError("poll");
}
#elif defined(HAVE_SELECT)
struct timeval timeout = {
.tv_sec = milliseconds / MSECS_PER_SEC,
.tv_usec = (milliseconds % MSECS_PER_SEC) * USECS_PER_MSEC
};
if (select(0, NULL, NULL, NULL, &timeout) == -1) {
if (errno != EINTR) logSystemError("select");
}
#endif /* delay */
}
}
|