/src/mpv/osdep/poll_wrapper.c
Line | Count | Source |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <stdlib.h> |
19 | | #include <poll.h> |
20 | | #include <sys/select.h> |
21 | | #include <stdio.h> |
22 | | |
23 | | #include "common/common.h" |
24 | | #include "config.h" |
25 | | #include "poll_wrapper.h" |
26 | | #include "timer.h" |
27 | | |
28 | | |
29 | | int mp_poll(struct pollfd *fds, int nfds, int64_t timeout_ns) |
30 | 0 | { |
31 | 0 | #if HAVE_PPOLL |
32 | 0 | struct timespec ts; |
33 | 0 | ts.tv_sec = timeout_ns / MP_TIME_S_TO_NS(1); |
34 | 0 | ts.tv_nsec = timeout_ns % MP_TIME_S_TO_NS(1); |
35 | 0 | struct timespec *tsp = timeout_ns >= 0 ? &ts : NULL; |
36 | 0 | return ppoll(fds, nfds, tsp, NULL); |
37 | 0 | #endif |
38 | | // Round-up to 1ms for short timeouts (100us, 1000us] |
39 | 0 | if (timeout_ns > MP_TIME_US_TO_NS(100)) |
40 | 0 | timeout_ns = MPMAX(timeout_ns, MP_TIME_MS_TO_NS(1)); |
41 | 0 | if (timeout_ns > 0) |
42 | 0 | timeout_ns /= MP_TIME_MS_TO_NS(1); |
43 | 0 | return poll(fds, nfds, timeout_ns); |
44 | 0 | } |
45 | | |
46 | | // poll shim that supports device files on macOS. |
47 | | int polldev(struct pollfd fds[], nfds_t nfds, int timeout) |
48 | 2.60k | { |
49 | | #ifdef __APPLE__ |
50 | | int maxfd = 0; |
51 | | fd_set readfds, writefds; |
52 | | FD_ZERO(&readfds); |
53 | | FD_ZERO(&writefds); |
54 | | for (size_t i = 0; i < nfds; ++i) { |
55 | | struct pollfd *fd = &fds[i]; |
56 | | if (fd->fd > maxfd) { |
57 | | maxfd = fd->fd; |
58 | | } |
59 | | if ((fd->events & POLLIN)) { |
60 | | FD_SET(fd->fd, &readfds); |
61 | | } |
62 | | if ((fd->events & POLLOUT)) { |
63 | | FD_SET(fd->fd, &writefds); |
64 | | } |
65 | | } |
66 | | struct timeval _timeout = { |
67 | | .tv_sec = timeout / 1000, |
68 | | .tv_usec = (timeout % 1000) * 1000 |
69 | | }; |
70 | | int n = select(maxfd + 1, &readfds, &writefds, NULL, |
71 | | timeout != -1 ? &_timeout : NULL); |
72 | | if (n < 0) { |
73 | | return n; |
74 | | } |
75 | | for (size_t i = 0; i < nfds; ++i) { |
76 | | struct pollfd *fd = &fds[i]; |
77 | | fd->revents = 0; |
78 | | if (FD_ISSET(fd->fd, &readfds)) { |
79 | | fd->revents |= POLLIN; |
80 | | } |
81 | | if (FD_ISSET(fd->fd, &writefds)) { |
82 | | fd->revents |= POLLOUT; |
83 | | } |
84 | | } |
85 | | return n; |
86 | | #else |
87 | 2.60k | return poll(fds, nfds, timeout); |
88 | 2.60k | #endif |
89 | 2.60k | } |