Misc: Trim and consolidate header file usage
[platform/upstream/libusb.git] / libusb / os / poll_windows.h
1 /*
2  * Windows compat: POSIX compatibility wrapper
3  * Copyright © 2012-2013 RealVNC Ltd.
4  * Copyright © 2009-2010 Pete Batard <pete@akeo.ie>
5  * Copyright © 2016-2018 Chris Dickens <christopher.a.dickens@gmail.com>
6  * With contributions from Michael Plante, Orin Eman et al.
7  * Parts of poll implementation from libusb-win32, by Stephan Meyer et al.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24
25 #ifndef LIBUSB_POLL_WINDOWS_H
26 #define LIBUSB_POLL_WINDOWS_H
27
28 // Handle synchronous completion through the overlapped structure
29 #if !defined(STATUS_REPARSE)    // reuse the REPARSE status code
30 #define STATUS_REPARSE ((LONG)0x00000104L)
31 #endif
32 #define STATUS_COMPLETED_SYNCHRONOUSLY  STATUS_REPARSE
33 #define HasOverlappedIoCompletedSync(lpOverlapped)      (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
34
35 #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
36
37 #define POLLIN          0x0001  /* There is data to read */
38 #define POLLPRI         0x0002  /* There is urgent data to read */
39 #define POLLOUT         0x0004  /* Writing now will not block */
40 #define POLLERR         0x0008  /* Error condition */
41 #define POLLHUP         0x0010  /* Hung up */
42 #define POLLNVAL        0x0020  /* Invalid request: fd not open */
43
44 struct pollfd {
45         int fd;         /* file descriptor */
46         short events;   /* requested events */
47         short revents;  /* returned events */
48 };
49
50 struct winfd {
51         int fd;                         // what's exposed to libusb core
52         OVERLAPPED *overlapped;         // what will report our I/O status
53 };
54
55 extern const struct winfd INVALID_WINFD;
56
57 struct winfd usbi_create_fd(void);
58
59 int usbi_pipe(int pipefd[2]);
60 int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
61 ssize_t usbi_write(int fd, const void *buf, size_t count);
62 ssize_t usbi_read(int fd, void *buf, size_t count);
63 int usbi_close(int fd);
64
65 /*
66  * Timeval operations
67  */
68 #if !defined(TIMESPEC_TO_TIMEVAL)
69 #define TIMESPEC_TO_TIMEVAL(tv, ts)                             \
70 do {                                                            \
71         (tv)->tv_sec = (long)(ts)->tv_sec;                      \
72         (tv)->tv_usec = (long)(ts)->tv_nsec / 1000;             \
73 } while (0)
74 #endif
75 #if !defined(timersub)
76 #define timersub(a, b, result)                                  \
77 do {                                                            \
78         (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;           \
79         (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;        \
80         if ((result)->tv_usec < 0) {                            \
81                 --(result)->tv_sec;                             \
82                 (result)->tv_usec += 1000000;                   \
83         }                                                       \
84 } while (0)
85 #endif
86
87 #endif