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.
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.
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.
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
27 // disable /W4 MSVC warnings that are benign
28 #pragma warning(disable:4127) // conditional expression is constant
31 // Handle synchronous completion through the overlapped structure
32 #if !defined(STATUS_REPARSE) // reuse the REPARSE status code
33 #define STATUS_REPARSE ((LONG)0x00000104L)
35 #define STATUS_COMPLETED_SYNCHRONOUSLY STATUS_REPARSE
36 #if defined(_WIN32_WCE)
37 // WinCE doesn't have a HasOverlappedIoCompleted() macro, so attempt to emulate it
38 #define HasOverlappedIoCompleted(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) != STATUS_PENDING)
40 #define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
42 #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
46 #define POLLIN 0x0001 /* There is data to read */
47 #define POLLPRI 0x0002 /* There is urgent data to read */
48 #define POLLOUT 0x0004 /* Writing now will not block */
49 #define POLLERR 0x0008 /* Error condition */
50 #define POLLHUP 0x0010 /* Hung up */
51 #define POLLNVAL 0x0020 /* Invalid request: fd not open */
54 int fd; /* file descriptor */
55 short events; /* requested events */
56 short revents; /* returned events */
60 int fd; // what's exposed to libusb core
61 OVERLAPPED *overlapped; // what will report our I/O status
64 extern const struct winfd INVALID_WINFD;
66 struct winfd usbi_create_fd(void);
68 int usbi_pipe(int pipefd[2]);
69 int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
70 ssize_t usbi_write(int fd, const void *buf, size_t count);
71 ssize_t usbi_read(int fd, void *buf, size_t count);
72 int usbi_close(int fd);
78 #include <winsock.h> // defines timeval functions on DDK
81 #if !defined(TIMESPEC_TO_TIMEVAL)
82 #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
83 (tv)->tv_sec = (long)(ts)->tv_sec; \
84 (tv)->tv_usec = (long)(ts)->tv_nsec / 1000; \
87 #if !defined(timersub)
88 #define timersub(a, b, result) \
90 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
91 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
92 if ((result)->tv_usec < 0) { \
94 (result)->tv_usec += 1000000; \