make wiimote_poll a platform-specific function, move implementations to os_*.c/os_mac.m

This commit is contained in:
Lysann Schlegel
2012-11-10 13:56:06 +01:00
parent 6f6ba68ed6
commit 71569818e5
6 changed files with 142 additions and 138 deletions

View File

@@ -44,20 +44,13 @@
#include "nunchuk.h" /* for nunchuk_disconnected, etc */
#include "wiiboard.h" /* for wii_board_disconnected, etc */
#include "motion_plus.h" /* for motion_plus_disconnected, etc */
#include "io.h" /* for wiiuse_os_read, etc */
#ifndef WIIUSE_WIN32
#include <sys/time.h> /* for timeval */
#include <sys/select.h> /* for select, fd_set */
#include <unistd.h> /* for read */
#endif
#include "os.h" /* for wiiuse_os_poll */
#include <errno.h> /* for errno */
#include <stdio.h> /* for printf, perror */
#include <stdlib.h> /* for free, malloc */
#include <string.h> /* for memcpy, memset */
static void idle_cycle(struct wiimote_t* wm);
static void clear_dirty_reads(struct wiimote_t* wm);
static void event_data_read(struct wiimote_t* wm, byte* msg);
static void event_data_write(struct wiimote_t *wm, byte *msg);
@@ -80,130 +73,7 @@ static int state_changed(struct wiimote_t* wm);
* the event variable will be set.
*/
int wiiuse_poll(struct wiimote_t** wm, int wiimotes) {
int evnt = 0;
#ifdef WIIUSE_BLUEZ
/*
* *nix
*/
struct timeval tv;
fd_set fds;
int r;
int i;
int highest_fd = -1;
if (!wm) return 0;
/* block select() for 1/2000th of a second */
tv.tv_sec = 0;
tv.tv_usec = 500;
FD_ZERO(&fds);
for (i = 0; i < wiimotes; ++i) {
/* only poll it if it is connected */
if (WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_CONNECTED)) {
FD_SET(wm[i]->in_sock, &fds);
/* find the highest fd of the connected wiimotes */
if (wm[i]->in_sock > highest_fd)
highest_fd = wm[i]->in_sock;
}
wm[i]->event = WIIUSE_NONE;
}
if (highest_fd == -1)
/* nothing to poll */
return 0;
if (select(highest_fd + 1, &fds, NULL, NULL, &tv) == -1) {
WIIUSE_ERROR("Unable to select() the wiimote interrupt socket(s).");
perror("Error Details");
return 0;
}
/* check each socket for an event */
for (i = 0; i < wiimotes; ++i) {
/* if this wiimote is not connected, skip it */
if (!WIIMOTE_IS_CONNECTED(wm[i]))
continue;
if (FD_ISSET(wm[i]->in_sock, &fds)) {
/* clear out the event buffer */
memset(wm[i]->event_buf, 0, sizeof(wm[i]->event_buf));
/* clear out any old read requests */
clear_dirty_reads(wm[i]);
/* read the pending message into the buffer */
r = read(wm[i]->in_sock, wm[i]->event_buf, sizeof(wm[i]->event_buf));
if (r == -1) {
/* error reading data */
WIIUSE_ERROR("Receiving wiimote data (id %i).", wm[i]->unid);
perror("Error Details");
if (errno == ENOTCONN) {
/* this can happen if the bluetooth dongle is disconnected */
WIIUSE_ERROR("Bluetooth appears to be disconnected. Wiimote unid %i will be disconnected.", wm[i]->unid);
wiiuse_disconnect(wm[i]);
wiiuse_disconnected(wm[i]);
wm[i]->event = WIIUSE_UNEXPECTED_DISCONNECT;
}
continue;
}
if (!r) {
/* remote disconnect */
wiiuse_disconnected(wm[i]);
evnt = 1;
continue;
}
/* propagate the event */
propagate_event(wm[i], wm[i]->event_buf[1], wm[i]->event_buf+2);
evnt += (wm[i]->event != WIIUSE_NONE);
} else {
idle_cycle(wm[i]);
}
}
#elif defined(WIIUSE_WIN32)
/*
* Windows
*/
int i;
if (!wm) return 0;
for (i = 0; i < wiimotes; ++i) {
wm[i]->event = WIIUSE_NONE;
if (wiiuse_os_read(wm[i])) {
/* propagate the event */
propagate_event(wm[i], wm[i]->event_buf[0], wm[i]->event_buf+1);
evnt += (wm[i]->event != WIIUSE_NONE);
/* clear out the event buffer */
memset(wm[i]->event_buf, 0, sizeof(wm[i]->event_buf));
} else {
idle_cycle(wm[i]);
}
}
#elif defined(WIIUSE_MAC)
/*
* Mac
*/
int i;
if (!wm) return 0;
for (i = 0; i < wiimotes; ++i) {
wm[i]->event = WIIUSE_NONE;
idle_cycle(wm[i]);
}
#endif
return evnt;
return wiiuse_os_poll(wm, wiimotes);
}
int wiiuse_update(struct wiimote_t** wiimotes, int nwiimotes, wiiuse_update_cb callback) {
@@ -244,7 +114,7 @@ int wiiuse_update(struct wiimote_t** wiimotes, int nwiimotes, wiiuse_update_cb c
*
* @param wm Pointer to a wiimote_t structure.
*/
static void idle_cycle(struct wiimote_t* wm) {
void idle_cycle(struct wiimote_t* wm) {
/*
* Smooth the angles.
*