1b877906bSopenharmony_ci//========================================================================
2b877906bSopenharmony_ci// GLFW 3.5 POSIX - www.glfw.org
3b877906bSopenharmony_ci//------------------------------------------------------------------------
4b877906bSopenharmony_ci// Copyright (c) 2022 Camilla Löwy <elmindreda@glfw.org>
5b877906bSopenharmony_ci//
6b877906bSopenharmony_ci// This software is provided 'as-is', without any express or implied
7b877906bSopenharmony_ci// warranty. In no event will the authors be held liable for any damages
8b877906bSopenharmony_ci// arising from the use of this software.
9b877906bSopenharmony_ci//
10b877906bSopenharmony_ci// Permission is granted to anyone to use this software for any purpose,
11b877906bSopenharmony_ci// including commercial applications, and to alter it and redistribute it
12b877906bSopenharmony_ci// freely, subject to the following restrictions:
13b877906bSopenharmony_ci//
14b877906bSopenharmony_ci// 1. The origin of this software must not be misrepresented; you must not
15b877906bSopenharmony_ci//    claim that you wrote the original software. If you use this software
16b877906bSopenharmony_ci//    in a product, an acknowledgment in the product documentation would
17b877906bSopenharmony_ci//    be appreciated but is not required.
18b877906bSopenharmony_ci//
19b877906bSopenharmony_ci// 2. Altered source versions must be plainly marked as such, and must not
20b877906bSopenharmony_ci//    be misrepresented as being the original software.
21b877906bSopenharmony_ci//
22b877906bSopenharmony_ci// 3. This notice may not be removed or altered from any source
23b877906bSopenharmony_ci//    distribution.
24b877906bSopenharmony_ci//
25b877906bSopenharmony_ci//========================================================================
26b877906bSopenharmony_ci
27b877906bSopenharmony_ci#if !defined(_GNU_SOURCE)
28b877906bSopenharmony_ci#define _GNU_SOURCE
29b877906bSopenharmony_ci#endif
30b877906bSopenharmony_ci
31b877906bSopenharmony_ci#include "internal.h"
32b877906bSopenharmony_ci
33b877906bSopenharmony_ci#if defined(GLFW_BUILD_POSIX_POLL)
34b877906bSopenharmony_ci
35b877906bSopenharmony_ci#include <signal.h>
36b877906bSopenharmony_ci#include <time.h>
37b877906bSopenharmony_ci#include <errno.h>
38b877906bSopenharmony_ci
39b877906bSopenharmony_ciGLFWbool _glfwPollPOSIX(struct pollfd* fds, nfds_t count, double* timeout)
40b877906bSopenharmony_ci{
41b877906bSopenharmony_ci    for (;;)
42b877906bSopenharmony_ci    {
43b877906bSopenharmony_ci        if (timeout)
44b877906bSopenharmony_ci        {
45b877906bSopenharmony_ci            const uint64_t base = _glfwPlatformGetTimerValue();
46b877906bSopenharmony_ci
47b877906bSopenharmony_ci#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__CYGWIN__)
48b877906bSopenharmony_ci            const time_t seconds = (time_t) *timeout;
49b877906bSopenharmony_ci            const long nanoseconds = (long) ((*timeout - seconds) * 1e9);
50b877906bSopenharmony_ci            const struct timespec ts = { seconds, nanoseconds };
51b877906bSopenharmony_ci            const int result = ppoll(fds, count, &ts, NULL);
52b877906bSopenharmony_ci#elif defined(__NetBSD__)
53b877906bSopenharmony_ci            const time_t seconds = (time_t) *timeout;
54b877906bSopenharmony_ci            const long nanoseconds = (long) ((*timeout - seconds) * 1e9);
55b877906bSopenharmony_ci            const struct timespec ts = { seconds, nanoseconds };
56b877906bSopenharmony_ci            const int result = pollts(fds, count, &ts, NULL);
57b877906bSopenharmony_ci#else
58b877906bSopenharmony_ci            const int milliseconds = (int) (*timeout * 1e3);
59b877906bSopenharmony_ci            const int result = poll(fds, count, milliseconds);
60b877906bSopenharmony_ci#endif
61b877906bSopenharmony_ci            const int error = errno; // clock_gettime may overwrite our error
62b877906bSopenharmony_ci
63b877906bSopenharmony_ci            *timeout -= (_glfwPlatformGetTimerValue() - base) /
64b877906bSopenharmony_ci                (double) _glfwPlatformGetTimerFrequency();
65b877906bSopenharmony_ci
66b877906bSopenharmony_ci            if (result > 0)
67b877906bSopenharmony_ci                return GLFW_TRUE;
68b877906bSopenharmony_ci            else if (result == -1 && error != EINTR && error != EAGAIN)
69b877906bSopenharmony_ci                return GLFW_FALSE;
70b877906bSopenharmony_ci            else if (*timeout <= 0.0)
71b877906bSopenharmony_ci                return GLFW_FALSE;
72b877906bSopenharmony_ci        }
73b877906bSopenharmony_ci        else
74b877906bSopenharmony_ci        {
75b877906bSopenharmony_ci            const int result = poll(fds, count, -1);
76b877906bSopenharmony_ci            if (result > 0)
77b877906bSopenharmony_ci                return GLFW_TRUE;
78b877906bSopenharmony_ci            else if (result == -1 && errno != EINTR && errno != EAGAIN)
79b877906bSopenharmony_ci                return GLFW_FALSE;
80b877906bSopenharmony_ci        }
81b877906bSopenharmony_ci    }
82b877906bSopenharmony_ci}
83b877906bSopenharmony_ci
84b877906bSopenharmony_ci#endif // GLFW_BUILD_POSIX_POLL
85b877906bSopenharmony_ci
86