コード例 #1
0
/*
 * Create a new FakeDev entry, and open a file descriptor that actually
 * works.
 */
FakeDev* wsCreateRealFakeDev(const char* debugName)
{
    FakeDev* newDev = wsCreateFakeDev(debugName);
    if (newDev == NULL)
        return newDev;
    
    int fds[2];

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
        wsLog("socketpair() failed: %s\n", strerror(errno));
        wsFreeFakeDev(newDev);
        return NULL;
    }

    if (dup2(fds[0], newDev->fd) < 0) {
        wsLog("dup2(%d,%d) failed: %s\n",
            fds[0], newDev->fd, strerror(errno));
        wsFreeFakeDev(newDev);
        return NULL;
    }
    close(fds[0]);

    /* okay to leave this one in the "normal" range; not visible to app */
    newDev->otherFd = fds[1];

    return newDev;
}
コード例 #2
0
/*
 * Open an audio output device.
 */
FakeDev* wsOpenDevAudio(const char* pathName, int flags)
{
    FakeDev* newDev = wsCreateFakeDev(pathName);
    if (newDev != NULL) {
        newDev->write = writeAudio;
        newDev->ioctl = ioctlAudio;
        newDev->close = closeAudio;

        AudioState* eventState = calloc(1, sizeof(AudioState));

        if (configureInitialState(pathName, eventState) != 0) {
            free(eventState);
            return NULL;
        }
        newDev->state = eventState;
    }

    return newDev;
}