-#ifdef HAVE_POLL
-/* SunOS has poll, but doesn't provide a prototype. */
-# if defined (sun) && !defined (__SVR4)
-extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
-# endif /* !sun */
-#else /* !HAVE_POLL */
-
-#ifdef G_OS_WIN32
-
-static gint
-g_poll (GPollFD *fds,
- guint nfds,
- gint timeout)
-{
- HANDLE handles[MAXIMUM_WAIT_OBJECTS];
- gboolean poll_msgs = FALSE;
- GPollFD *f;
- DWORD ready;
- MSG msg;
- UINT timer;
- gint nhandles = 0;
-
- for (f = fds; f < &fds[nfds]; ++f)
- if (f->fd >= 0)
- {
- if (f->fd == G_WIN32_MSG_HANDLE)
- poll_msgs = TRUE;
- else if (nhandles == MAXIMUM_WAIT_OBJECTS)
- {
- g_warning (G_STRLOC ": Too many handles to wait for!\n");
- break;
- }
- else
- {
-#ifdef G_MAIN_POLL_DEBUG
- g_print ("g_poll: waiting for %#x\n", f->fd);
-#endif
- handles[nhandles++] = (HANDLE) f->fd;
- }
- }
-
- if (timeout == -1)
- timeout = INFINITE;
-
- if (poll_msgs)
- {
- /* Waiting for messages, and maybe events
- * -> First PeekMessage
- */
-#ifdef G_MAIN_POLL_DEBUG
- g_print ("PeekMessage\n");
-#endif
- if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
- ready = WAIT_OBJECT_0 + nhandles;
- else
- {
- if (nhandles == 0)
- {
- /* Waiting just for messages */
- if (timeout == INFINITE)
- {
- /* Infinite timeout
- * -> WaitMessage
- */
-#ifdef G_MAIN_POLL_DEBUG
- g_print ("WaitMessage\n");
-#endif
- if (!WaitMessage ())
- {
- gchar *emsg = g_win32_error_message (GetLastError ());
- g_warning (G_STRLOC ": WaitMessage() failed: %s", emsg);
- g_free (emsg);
- }
- ready = WAIT_OBJECT_0 + nhandles;
- }
- else if (timeout == 0)
- {
- /* Waiting just for messages, zero timeout.
- * If we got here, there was no message
- */
- ready = WAIT_TIMEOUT;
- }
- else
- {
- /* Waiting just for messages, some timeout
- * -> Set a timer, wait for message,
- * kill timer, use PeekMessage
- */
- timer = SetTimer (NULL, 0, timeout, NULL);
- if (timer == 0)
- {
- gchar *emsg = g_win32_error_message (GetLastError ());
- g_warning (G_STRLOC ": SetTimer() failed: %s", emsg);
- g_free (emsg);
- ready = WAIT_TIMEOUT;
- }
- else
- {
-#ifdef G_MAIN_POLL_DEBUG
- g_print ("WaitMessage\n");
-#endif
- WaitMessage ();
- KillTimer (NULL, timer);
-#ifdef G_MAIN_POLL_DEBUG
- g_print ("PeekMessage\n");
-#endif
- if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)
- && msg.message != WM_TIMER)
- ready = WAIT_OBJECT_0;
- else
- ready = WAIT_TIMEOUT;
- }
- }
- }
- else
- {
- /* Wait for either message or event
- * -> Use MsgWaitForMultipleObjects
- */
-#ifdef G_MAIN_POLL_DEBUG
- g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
-#endif
- ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
- timeout, QS_ALLINPUT);
-
- if (ready == WAIT_FAILED)
- {
- gchar *emsg = g_win32_error_message (GetLastError ());
- g_warning (G_STRLOC ": MsgWaitForMultipleObjects() failed: %s", emsg);
- g_free (emsg);
- }
- }
- }
- }
- else if (nhandles == 0)
- {
- /* Wait for nothing (huh?) */
- return 0;
- }
- else
- {
- /* Wait for just events
- * -> Use WaitForMultipleObjects
- */
-#ifdef G_MAIN_POLL_DEBUG
- g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
-#endif
- ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
- if (ready == WAIT_FAILED)
- {
- gchar *emsg = g_win32_error_message (GetLastError ());
- g_warning (G_STRLOC ": WaitForMultipleObjects() failed: %s", emsg);
- g_free (emsg);
- }
- }
-
-#ifdef G_MAIN_POLL_DEBUG
- g_print ("wait returns %ld%s\n",
- ready,
- (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
- (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
- (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
-#endif
- for (f = fds; f < &fds[nfds]; ++f)
- f->revents = 0;
-
- if (ready == WAIT_FAILED)
- return -1;
- else if (ready == WAIT_TIMEOUT)
- return 0;
- else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
- {
- for (f = fds; f < &fds[nfds]; ++f)
- if (f->fd >= 0)
- {
- if (f->events & G_IO_IN)
- if (f->fd == G_WIN32_MSG_HANDLE)
- f->revents |= G_IO_IN;
- }
- }
- else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
- for (f = fds; f < &fds[nfds]; ++f)
- {
- if (f->fd == (gint) handles[ready - WAIT_OBJECT_0])
- {
- f->revents = f->events;
-#ifdef G_MAIN_POLL_DEBUG
- g_print ("g_poll: got event %#x\n", f->fd);
-#endif
- }
- }
-
- return 1;
-}
-
-#else /* !G_OS_WIN32 */
-
-/* The following implementation of poll() comes from the GNU C Library.
- * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
- */
-
-#include <string.h> /* for bzero on BSD systems */
-
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif /* HAVE_SYS_SELECT_H */
-
-#ifdef G_OS_BEOS
-#undef NO_FD_SET
-#endif /* G_OS_BEOS */
-
-#ifndef NO_FD_SET
-# define SELECT_MASK fd_set
-#else /* !NO_FD_SET */
-# ifndef _AIX
-typedef long fd_mask;
-# endif /* _AIX */
-# ifdef _IBMR2
-# define SELECT_MASK void
-# else /* !_IBMR2 */
-# define SELECT_MASK int
-# endif /* !_IBMR2 */
-#endif /* !NO_FD_SET */
-
-static gint
-g_poll (GPollFD *fds,
- guint nfds,
- gint timeout)
-{
- struct timeval tv;
- SELECT_MASK rset, wset, xset;
- GPollFD *f;
- int ready;
- int maxfd = 0;
-
- FD_ZERO (&rset);
- FD_ZERO (&wset);
- FD_ZERO (&xset);
-
- for (f = fds; f < &fds[nfds]; ++f)
- if (f->fd >= 0)
- {
- if (f->events & G_IO_IN)
- FD_SET (f->fd, &rset);
- if (f->events & G_IO_OUT)
- FD_SET (f->fd, &wset);
- if (f->events & G_IO_PRI)
- FD_SET (f->fd, &xset);
- if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
- maxfd = f->fd;
- }
-
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
-
- ready = select (maxfd + 1, &rset, &wset, &xset,
- timeout == -1 ? NULL : &tv);
- if (ready > 0)
- for (f = fds; f < &fds[nfds]; ++f)
- {
- f->revents = 0;
- if (f->fd >= 0)
- {
- if (FD_ISSET (f->fd, &rset))
- f->revents |= G_IO_IN;
- if (FD_ISSET (f->fd, &wset))
- f->revents |= G_IO_OUT;
- if (FD_ISSET (f->fd, &xset))
- f->revents |= G_IO_PRI;
- }
- }
-
- return ready;
-}
-
-#endif /* !G_OS_WIN32 */
-
-#endif /* !HAVE_POLL */
-