+#ifdef NATIVE_WIN32
+
+static gint
+g_poll (GPollFD *fds, guint nfds, gint timeout)
+{
+ HANDLE handles[MAXIMUM_WAIT_OBJECTS];
+ GPollFD *f;
+ DWORD ready;
+ MSG msg;
+ UINT timer;
+ LONG prevcnt;
+ gint poll_msgs = -1;
+ gint nhandles = 0;
+
+ for (f = fds; f < &fds[nfds]; ++f)
+ if (f->fd >= 0)
+ {
+ if (f->events & G_IO_IN)
+ if (f->fd == G_WIN32_MSG_HANDLE)
+ poll_msgs = f - fds;
+ else
+ {
+ /* g_print ("g_poll: waiting for handle %#x\n", f->fd); */
+ handles[nhandles++] = (HANDLE) f->fd;
+ }
+ }
+
+ if (timeout == -1)
+ timeout = INFINITE;
+
+ if (poll_msgs >= 0)
+ {
+ /* Waiting for messages, and maybe events */
+ if (nhandles == 0)
+ {
+ if (timeout == INFINITE)
+ {
+ /* Waiting just for messages, infinite timeout
+ * -> Use PeekMessage, then WaitMessage
+ */
+ /* g_print ("WaitMessage, PeekMessage\n"); */
+ if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
+ ready = WAIT_OBJECT_0;
+ else if (!WaitMessage ())
+ g_warning ("g_poll: WaitMessage failed");
+ ready = WAIT_OBJECT_0;
+ }
+ else if (timeout == 0)
+ {
+ /* Waiting just for messages, zero timeout
+ * -> Use PeekMessage
+ */
+ /* g_print ("PeekMessage\n"); */
+ if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
+ ready = WAIT_OBJECT_0;
+ else
+ ready = WAIT_TIMEOUT;
+ }
+ else
+ {
+ /* Waiting just for messages, some timeout
+ * -> First try PeekMessage, then set a timer, wait for message,
+ * kill timer, use PeekMessage
+ */
+ /* g_print ("PeekMessage\n"); */
+ if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
+ ready = WAIT_OBJECT_0;
+ else if ((timer = SetTimer (NULL, 0, timeout, NULL)) == 0)
+ g_warning ("g_poll: SetTimer failed");
+ else
+ {
+ /* g_print ("WaitMessage\n"); */
+ WaitMessage ();
+ KillTimer (NULL, timer);
+ if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
+ ready = WAIT_OBJECT_0;
+ else
+ ready = WAIT_TIMEOUT;
+ }
+ }
+ }
+ else
+ {
+ /* Wait for either message or event
+ * -> Use MsgWaitForMultipleObjects
+ */
+ /* g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
+ ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
+ timeout, QS_ALLINPUT);
+ /* g_print("=%d\n", ready); */
+ if (ready == WAIT_FAILED)
+ g_warning ("g_poll: MsgWaitForMultipleObjects failed");
+ }
+ }
+ else if (nhandles == 0)
+ {
+ /* Wait for nothing (huh?) */
+ return 0;
+ }
+ else
+ {
+ /* Wait for just events
+ * -> Use WaitForMultipleObjects
+ */
+ /* g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
+ ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
+ /* g_print("=%d\n", ready); */
+ if (ready == WAIT_FAILED)
+ g_warning ("g_poll: WaitForMultipleObjects failed");
+ }
+
+ for (f = fds; f < &fds[nfds]; ++f)
+ f->revents = 0;
+
+ if (ready == WAIT_FAILED)
+ return -1;
+ else if (poll_msgs >= 0 && ready == WAIT_OBJECT_0 + nhandles)
+ {
+ fds[poll_msgs].revents |= G_IO_IN;
+ }
+ else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
+ for (f = fds; f < &fds[nfds]; ++f)
+ {
+ if ((f->events & G_IO_IN)
+ && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
+ {
+ f->revents |= G_IO_IN;
+ /* g_print ("event %#x\n", f->fd); */
+ ResetEvent ((HANDLE) f->fd);
+ }
+ }
+
+ if (ready == WAIT_TIMEOUT)
+ return 0;
+ else
+ return 1;
+}
+
+#else /* !NATIVE_WIN32 */