Get Wiimote working in Linux, events.c works the same under Linux as OSX, Windows strips the first byte of the packet? Probably should be handled in WiiUse instead of our code

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@3535 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Sonicadvance1 2009-06-22 10:11:31 +00:00
parent c14548c8db
commit cbff6dfde3
2 changed files with 59 additions and 123 deletions

View File

@ -85,113 +85,25 @@ int wiiuse_poll(struct wiimote_t** wm, int wiimotes) {
int evnt = 0; int evnt = 0;
#ifndef WIN32 #ifndef WIN32
#if defined(__APPLE__)
int i;
if (!wm)
return 0;
for (i = 0; i < wiimotes; ++i) {
wm[i]->event = WIIUSE_NONE;
if (wiiuse_io_read(wm[i])) {
/* propagate the event */
propagate_event(wm[i], wm[i]->event_buf[1], wm[i]->event_buf+2);
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]);
}
}
#else
/*
* *nix
*/
struct timeval tv;
fd_set fds;
int r;
int i; int i;
int highest_fd = -1;
if (!wm) return 0; 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) { for (i = 0; i < wiimotes; ++i) {
/* only poll it if it is connected */ wm[i]->event = WIIUSE_NONE;
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 (wiiuse_io_read(wm[i])) {
if (wm[i]->in_sock > highest_fd) /* propagate the event */
highest_fd = wm[i]->in_sock; propagate_event(wm[i], wm[i]->event_buf[1], wm[i]->event_buf+2);
} evnt += (wm[i]->event != WIIUSE_NONE);
wm[i]->event = WIIUSE_NONE; /* clear out the event buffer */
} memset(wm[i]->event_buf, 0, sizeof(wm[i]->event_buf));
} else {
if (highest_fd == -1) idle_cycle(wm[i]);
/* 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]);
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]);
}
} }
#endif
#else #else
/* /*
* Windows * Windows

View File

@ -268,37 +268,61 @@ int wiiuse_io_read(struct wiimote_t* wm) {
WIIUSE_INFO("Wiimote is Null0x%x\n", wm); WIIUSE_INFO("Wiimote is Null0x%x\n", wm);
return 0; return 0;
} }
/*
* *nix struct timeval tv;
*/ fd_set fds;
int r; int r;
int i; int i;
if (!wm) return 0;
/* read the pending message into the buffer */ /* block select() for 1/2000th of a second */
r = read(wm->in_sock, wm->event_buf, sizeof(wm->event_buf)); tv.tv_sec = 0;
if (r == -1) tv.tv_usec = 500;
{
/* error reading data */ FD_ZERO(&fds);
WIIUSE_INFO("Receiving wiimote data (id %i).", wm->unid); /* only poll it if it is connected */
if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED)) {
FD_SET(wm->in_sock, &fds);
//highest_fd = wm[i]->in_sock;
}
else
/* nothing to poll */
return 0;
if (select(wm->in_sock + 1, &fds, NULL, NULL, &tv) == -1) {
WIIUSE_ERROR("Unable to select() the wiimote interrupt socket(s).");
perror("Error Details"); perror("Error Details");
return 0;
}
if (errno == ENOTCONN) { /* if this wiimote is not connected, skip it */
/* this can happen if the bluetooth dongle is disconnected */ if (!WIIMOTE_IS_CONNECTED(wm))
WIIUSE_INFO("Bluetooth appears to be disconnected. Wiimote unid %i will be disconnected.", wm->unid); return 0;
wiiuse_disconnect(wm);
wm->event = WIIUSE_UNEXPECTED_DISCONNECT; if (FD_ISSET(wm->in_sock, &fds))
{
/* read the pending message into the buffer */
r = read(wm->in_sock, wm->event_buf, sizeof(wm->event_buf));
if (r == -1) {
/* error reading data */
WIIUSE_ERROR("Receiving wiimote data (id %i).", wm->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->unid);
wiiuse_disconnect(wm);
wm->event = WIIUSE_UNEXPECTED_DISCONNECT;
}
return 0;
}
if (!r) {
/* remote disconnect */
wiiuse_disconnected(wm);
return 0;
} }
return 0;
} }
//WIIUSE_INFO("Size %d, first 4 0x%02X%02X%02X%02X\n",r, wm->event_buf[0],wm->event_buf[1], wm->event_buf[2],wm->event_buf[3]);
if (!r) {
/* remote disconnect */
WIIUSE_INFO("Wiimote Disconnect\n");
wiiuse_disconnected(wm);
return 0;
}
return 1; return 1;
} }