poll_windows.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Windows compat: POSIX compatibility wrapper
  3. * Copyright © 2012-2013 RealVNC Ltd.
  4. * Copyright © 2009-2010 Pete Batard <pete@akeo.ie>
  5. * With contributions from Michael Plante, Orin Eman et al.
  6. * Parts of poll implementation from libusb-win32, by Stephan Meyer et al.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #pragma once
  24. #if defined(_MSC_VER)
  25. // disable /W4 MSVC warnings that are benign
  26. #pragma warning(disable:4127) // conditional expression is constant
  27. #endif
  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. #if defined(_WIN32_WCE)
  34. // WinCE doesn't have a HasOverlappedIoCompleted() macro, so attempt to emulate it
  35. #define HasOverlappedIoCompleted(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) != STATUS_PENDING)
  36. #endif
  37. #define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
  38. #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
  39. enum windows_version {
  40. WINDOWS_UNSUPPORTED,
  41. WINDOWS_CE,
  42. WINDOWS_XP,
  43. WINDOWS_2003, // also includes XP 64
  44. WINDOWS_VISTA_AND_LATER,
  45. };
  46. extern enum windows_version windows_version;
  47. #define MAX_FDS 256
  48. #define POLLIN 0x0001 /* There is data to read */
  49. #define POLLPRI 0x0002 /* There is urgent data to read */
  50. #define POLLOUT 0x0004 /* Writing now will not block */
  51. #define POLLERR 0x0008 /* Error condition */
  52. #define POLLHUP 0x0010 /* Hung up */
  53. #define POLLNVAL 0x0020 /* Invalid request: fd not open */
  54. struct pollfd {
  55. int fd; /* file descriptor */
  56. short events; /* requested events */
  57. short revents; /* returned events */
  58. };
  59. // access modes
  60. enum rw_type {
  61. RW_NONE,
  62. RW_READ,
  63. RW_WRITE,
  64. };
  65. // fd struct that can be used for polling on Windows
  66. typedef int cancel_transfer(struct usbi_transfer *itransfer);
  67. struct winfd {
  68. int fd; // what's exposed to libusb core
  69. HANDLE handle; // what we need to attach overlapped to the I/O op, so we can poll it
  70. OVERLAPPED* overlapped; // what will report our I/O status
  71. struct usbi_transfer *itransfer; // Associated transfer, or NULL if completed
  72. cancel_transfer *cancel_fn; // Function pointer to cancel transfer API
  73. enum rw_type rw; // I/O transfer direction: read *XOR* write (NOT BOTH)
  74. };
  75. extern const struct winfd INVALID_WINFD;
  76. int usbi_pipe(int pipefd[2]);
  77. int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
  78. ssize_t usbi_write(int fd, const void *buf, size_t count);
  79. ssize_t usbi_read(int fd, void *buf, size_t count);
  80. int usbi_close(int fd);
  81. void init_polling(void);
  82. void exit_polling(void);
  83. struct winfd usbi_create_fd(HANDLE handle, int access_mode,
  84. struct usbi_transfer *transfer, cancel_transfer *cancel_fn);
  85. void usbi_free_fd(struct winfd* winfd);
  86. struct winfd fd_to_winfd(int fd);
  87. struct winfd handle_to_winfd(HANDLE handle);
  88. struct winfd overlapped_to_winfd(OVERLAPPED* overlapped);
  89. /*
  90. * Timeval operations
  91. */
  92. #if defined(DDKBUILD)
  93. #include <winsock.h> // defines timeval functions on DDK
  94. #endif
  95. #if !defined(TIMESPEC_TO_TIMEVAL)
  96. #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
  97. (tv)->tv_sec = (long)(ts)->tv_sec; \
  98. (tv)->tv_usec = (long)(ts)->tv_nsec / 1000; \
  99. }
  100. #endif
  101. #if !defined(timersub)
  102. #define timersub(a, b, result) \
  103. do { \
  104. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  105. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  106. if ((result)->tv_usec < 0) { \
  107. --(result)->tv_sec; \
  108. (result)->tv_usec += 1000000; \
  109. } \
  110. } while (0)
  111. #endif