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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
/*
* 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-2025 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <errno.h>
#include <time.h>
#if defined(HAVE_GETTIMEOFDAY) || defined(HAVE_SETTIMEOFDAY)
#include <sys/time.h>
#endif /* HAVE_(GET|SET)TIMEOFDAY */
#ifdef HAVE_SYS_POLL_H
#include <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) {
now->seconds = 0;
now->nanoseconds = 0;
#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) && !defined(__MINGW32__)
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) != -1) {
now->seconds = ts.tv_sec;
now->nanoseconds = ts.tv_nsec;
} else {
//logSystemError("clock_gettime");
}
#elif defined(HAVE_GETTIMEOFDAY)
struct timeval tv;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
int result = gettimeofday(&tv, NULL);
#pragma GCC diagnostic pop
if (result != -1) {
now->seconds = tv.tv_sec;
now->nanoseconds = tv.tv_usec * NSECS_PER_USEC;
} else {
//logSystemError("gettimeofday");
}
#elif defined(HAVE_TIME)
now->seconds = time(NULL);
#else /* get current time */
#warning get current time not supported on this platform
#endif /* get current time */
}
void
setCurrentTime (const TimeValue *now) {
#if defined(HAVE_CLOCK_SETTIME) && defined(CLOCK_REALTIME)
const struct timespec ts = {
.tv_sec = now->seconds,
.tv_nsec = now->nanoseconds
};
if (clock_settime(CLOCK_REALTIME, &ts) == -1) {
logSystemError("clock_settime");
}
#elif defined(HAVE_SETTIMEOFDAY)
struct timeval tv = {
.tv_sec = now->seconds,
.tv_usec = now->nanoseconds / NSECS_PER_USEC
};
if (settimeofday(&tv, NULL) == -1) {
logSystemError("settimeofday");
}
#elif defined(__MINGW32__)
TimeComponents components;
expandTimeValue(now, &components);
SYSTEMTIME time = {
.wYear = components.year,
.wMonth = components.month + 1,
.wDay = components.day + 1,
.wHour = components.hour,
.wMinute = components.minute,
.wSecond = components.second,
.wMilliseconds = now->nanoseconds / NSECS_PER_MSEC
};
if (!SetLocalTime(&time)) {
logWindowsSystemError("SetLocalTime");
}
#elif defined(HAVE_STIME)
const time_t seconds = now->seconds;
if (stime(&seconds) == -1) {
logSystemError("stime");
}
#else /* set current time */
#warning set current time not supported on this platform
#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;
components->nanosecond = value->nanoseconds;
struct tm *time = &components->time;
localtime_r(&seconds, time);
#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 ((long int)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 /* approximate delay */
}
}
void
accurateDelay (const TimeValue *duration) {
TimeValue delay = *duration;
normalizeTimeValue(&delay);
if ((delay.seconds > 0) || ((delay.seconds == 0) && (delay.nanoseconds > 0))) {
#if defined(HAVE_NANOSLEEP)
const struct timespec timeout = {
.tv_sec = delay.seconds,
.tv_nsec = delay.nanoseconds
};
if (nanosleep(&timeout, NULL) == -1) {
if (errno != EINTR) logSystemError("nanosleep");
}
#elif defined(HAVE_SELECT)
struct timeval timeout = {
.tv_sec = delay.seconds,
.tv_usec = (delay.nanoseconds + (NSECS_PER_USEC - 1)) / NSECS_PER_USEC
};
if (timeout.tv_usec == USECS_PER_SEC) {
timeout.tv_sec += 1;
timeout.tv_usec = 0;
}
if (select(0, NULL, NULL, NULL, &timeout) == -1) {
if (errno != EINTR) logSystemError("select");
}
#else /* accurate delay */
approximateDelay((delay.seconds * MSECS_PER_SEC) + ((delay.nanoseconds + (NSECS_PER_MSEC - 1)) / NSECS_PER_MSEC));
#endif /* accurate delay */
}
}
|