src/core/unix/SDL_poll.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 19 Dec 2017 11:14:06 -0800
changeset 11783 c7577c45151a
parent 11297 905d34b4033c
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Fixed bug 4003 - HAVE_POLL undefined in SDL_poll.c, making it impossible to use.

tomwardio

HAVE_POLL is correctly defined in SDL_config.h when running configure. However, in the only place where it's used, it's undefined at the start of the file.
slouken@11297
     1
/*
slouken@11297
     2
  Simple DirectMedia Layer
slouken@11297
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@11297
     4
slouken@11297
     5
  This software is provided 'as-is', without any express or implied
slouken@11297
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@11297
     7
  arising from the use of this software.
slouken@11297
     8
slouken@11297
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@11297
    10
  including commercial applications, and to alter it and redistribute it
slouken@11297
    11
  freely, subject to the following restrictions:
slouken@11297
    12
slouken@11297
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@11297
    14
     claim that you wrote the original software. If you use this software
slouken@11297
    15
     in a product, an acknowledgment in the product documentation would be
slouken@11297
    16
     appreciated but is not required.
slouken@11297
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@11297
    18
     misrepresented as being the original software.
slouken@11297
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@11297
    20
*/
slouken@11297
    21
slouken@11297
    22
#include "../../SDL_internal.h"
slouken@11297
    23
slouken@11297
    24
#include "SDL_assert.h"
slouken@11297
    25
#include "SDL_poll.h"
slouken@11297
    26
slouken@11297
    27
#ifdef HAVE_POLL
slouken@11297
    28
#include <poll.h>
slouken@11297
    29
#else
slouken@11297
    30
#include <sys/time.h>
slouken@11297
    31
#include <sys/types.h>
slouken@11297
    32
#include <unistd.h>
slouken@11297
    33
#endif
slouken@11297
    34
#include <errno.h>
slouken@11297
    35
slouken@11297
    36
slouken@11297
    37
int
slouken@11297
    38
SDL_IOReady(int fd, SDL_bool forWrite, int timeoutMS)
slouken@11297
    39
{
slouken@11297
    40
    int result;
slouken@11297
    41
slouken@11297
    42
    /* Note: We don't bother to account for elapsed time if we get EINTR */
slouken@11297
    43
    do
slouken@11297
    44
    {
slouken@11297
    45
#ifdef HAVE_POLL
slouken@11297
    46
        struct pollfd info;
slouken@11297
    47
slouken@11297
    48
        info.fd = fd;
slouken@11297
    49
        if (forWrite) {
slouken@11297
    50
            info.events = POLLOUT;
slouken@11297
    51
        } else {
slouken@11297
    52
            info.events = POLLIN | POLLPRI;
slouken@11297
    53
        }
slouken@11297
    54
        result = poll(&info, 1, timeoutMS);
slouken@11297
    55
#else
slouken@11297
    56
        fd_set rfdset, *rfdp = NULL;
slouken@11297
    57
        fd_set wfdset, *wfdp = NULL;
slouken@11297
    58
        struct timeval tv, *tvp = NULL;
slouken@11297
    59
slouken@11297
    60
        /* If this assert triggers we'll corrupt memory here */
slouken@11297
    61
        SDL_assert(fd >= 0 && fd < FD_SETSIZE);
slouken@11297
    62
slouken@11297
    63
        if (forWrite) {
slouken@11297
    64
            FD_ZERO(&wfdset);
slouken@11297
    65
            FD_SET(fd, &wfdset);
slouken@11297
    66
            wfdp = &wfdset;
slouken@11297
    67
        } else {
slouken@11297
    68
            FD_ZERO(&rfdset);
slouken@11297
    69
            FD_SET(fd, &rfdset);
slouken@11297
    70
            rfdp = &rfdset;
slouken@11297
    71
        }
slouken@11297
    72
slouken@11297
    73
        if (timeoutMS >= 0) {
slouken@11297
    74
            tv.tv_sec = timeoutMS / 1000;
slouken@11297
    75
            tv.tv_usec = (timeoutMS % 1000) * 1000;
slouken@11297
    76
            tvp = &tv;
slouken@11297
    77
        }
slouken@11297
    78
slouken@11297
    79
        result = select(fd + 1, rfdp, wfdp, NULL, tvp);
slouken@11297
    80
#endif /* HAVE_POLL */
slouken@11297
    81
slouken@11297
    82
    } while ( result < 0 && errno == EINTR );
slouken@11297
    83
slouken@11297
    84
    return result;
slouken@11297
    85
}
slouken@11297
    86
slouken@11297
    87
/* vi: set ts=4 sw=4 expandtab: */