same interface for all wiiuse_os_write's

this also means we moved platform-sepcific write code into the wiiuse_os_write implementation of each platform.
This commit is contained in:
Lysann Schlegel
2012-12-08 22:04:07 +01:00
parent f2bf07c18c
commit d7a5d77489
7 changed files with 34 additions and 53 deletions

View File

@@ -54,7 +54,7 @@ void wiiuse_os_disconnect(struct wiimote_t* wm);
int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes); int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes);
/* buf[0] will be the report type, buf+1 the rest of the report */ /* buf[0] will be the report type, buf+1 the rest of the report */
int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len); int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len);
int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len); int wiiuse_os_write(struct wiimote_t* wm, byte report_type, byte* buf, int len);
/** @} */ /** @} */
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -68,7 +68,7 @@
- (void) disconnect; - (void) disconnect;
- (int) readBuffer: (byte*) buffer length: (NSUInteger) bufferLength; - (int) readBuffer: (byte*) buffer length: (NSUInteger) bufferLength;
- (int) writeBuffer: (byte*) buffer length: (NSUInteger) length; - (int) writeReport: (byte) report_type buffer: (byte*) buffer length: (NSUInteger) length;
@end @end

View File

@@ -209,13 +209,18 @@
return result >= 0 ? result : 0; return result >= 0 ? result : 0;
} }
- (int) writeBuffer: (byte*) buffer length: (NSUInteger) length { - (int) writeReport: (byte) report_type buffer: (byte*) buffer length: (NSUInteger) length {
if(interruptChannel == nil) { if(interruptChannel == nil) {
WIIUSE_ERROR("Attempted to write to nil interrupt channel [id %i].", wm->unid); WIIUSE_ERROR("Attempted to write to nil interrupt channel [id %i].", wm->unid);
return 0; return 0;
} }
IOReturn error = [interruptChannel writeSync:buffer length:length]; byte write_buffer[MAX_PAYLOAD];
write_buffer[0] = WM_SET_DATA | WM_BT_OUTPUT;
write_buffer[1] = report_type;
memcpy(write_buffer+2, buffer, length);
IOReturn error = [interruptChannel writeSync:write_buffer length:length+2];
if (error != kIOReturnSuccess) { if (error != kIOReturnSuccess) {
WIIUSE_ERROR("Error writing to interrupt channel [id %i].", wm->unid); WIIUSE_ERROR("Error writing to interrupt channel [id %i].", wm->unid);
@@ -227,7 +232,7 @@
[self disconnect]; [self disconnect];
} else { } else {
WIIUSE_DEBUG("Attempting to write again to the interrupt channel [id %i].", wm->unid); WIIUSE_DEBUG("Attempting to write again to the interrupt channel [id %i].", wm->unid);
error = [interruptChannel writeSync:buffer length:length]; error = [interruptChannel writeSync:write_buffer length:length+2];
if (error != kIOReturnSuccess) if (error != kIOReturnSuccess)
WIIUSE_ERROR("Unable to write again to the interrupt channel [id %i].", wm->unid); WIIUSE_ERROR("Unable to write again to the interrupt channel [id %i].", wm->unid);
} }

View File

@@ -182,7 +182,7 @@ int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) {
return result; return result;
} }
int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len) { int wiiuse_os_write(struct wiimote_t* wm, byte report_type, byte* buf, int len) {
if(!wm || !wm->objc_wm) return 0; if(!wm || !wm->objc_wm) return 0;
if(!WIIMOTE_IS_CONNECTED(wm)) { if(!WIIMOTE_IS_CONNECTED(wm)) {
WIIUSE_ERROR("Attempting to write to unconnected Wiimote"); WIIUSE_ERROR("Attempting to write to unconnected Wiimote");
@@ -192,7 +192,7 @@ int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
WiiuseWiimote* objc_wm = (WiiuseWiimote*) wm->objc_wm; WiiuseWiimote* objc_wm = (WiiuseWiimote*) wm->objc_wm;
int result = [objc_wm writeBuffer: buf length: (NSUInteger)len]; int result = [objc_wm writeReport: report_type buffer: buf length: (NSUInteger)len];
[pool drain]; [pool drain];
return result; return result;

View File

@@ -343,9 +343,14 @@ int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) {
return rc; return rc;
} }
int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len) { int wiiuse_os_write(struct wiimote_t* wm, byte report_type, byte* buf, int len) {
int rc; int rc;
rc = write(wm->out_sock, buf, len); byte write_buffer[MAX_PAYLOAD];
write_buffer[0] = WM_SET_REPORT | WM_BT_OUTPUT;
write_buffer[1] = report_type;
memcpy(write_buffer+2, buf, len);
rc = write(wm->out_sock, write_buffer, len+2);
if(rc < 0) if(rc < 0)
wiiuse_disconnected(wm); wiiuse_disconnected(wm);

View File

@@ -258,24 +258,28 @@ int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) {
} }
int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len) { int wiiuse_os_write(struct wiimote_t* wm, byte report_type, byte* buf, int len) {
DWORD bytes; DWORD bytes;
int i; int i;
byte write_buffer[MAX_PAYLOAD];
if (!wm || !WIIMOTE_IS_CONNECTED(wm)) if (!wm || !WIIMOTE_IS_CONNECTED(wm))
return 0; return 0;
write_buffer[0] = report_type;
memcpy(write_buffer+1, buf, len);
switch (wm->stack) { switch (wm->stack) {
case WIIUSE_STACK_UNKNOWN: case WIIUSE_STACK_UNKNOWN:
{ {
/* try to auto-detect the stack type */ /* try to auto-detect the stack type */
if (i = WriteFile(wm->dev_handle, buf, 22, &bytes, &wm->hid_overlap)) { if (i = WriteFile(wm->dev_handle, write_buffer, 22, &bytes, &wm->hid_overlap)) {
/* bluesoleil will always return 1 here, even if it's not connected */ /* bluesoleil will always return 1 here, even if it's not connected */
wm->stack = WIIUSE_STACK_BLUESOLEIL; wm->stack = WIIUSE_STACK_BLUESOLEIL;
return i; return i;
} }
if (i = HidD_SetOutputReport(wm->dev_handle, buf, len)) { if (i = HidD_SetOutputReport(wm->dev_handle, write_buffer, len+1)) {
wm->stack = WIIUSE_STACK_MS; wm->stack = WIIUSE_STACK_MS;
return i; return i;
} }
@@ -285,10 +289,10 @@ int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len) {
} }
case WIIUSE_STACK_MS: case WIIUSE_STACK_MS:
return HidD_SetOutputReport(wm->dev_handle, buf, len); return HidD_SetOutputReport(wm->dev_handle, write_buffer, len+1);
case WIIUSE_STACK_BLUESOLEIL: case WIIUSE_STACK_BLUESOLEIL:
return WriteFile(wm->dev_handle, buf, 22, &bytes, &wm->hid_overlap); return WriteFile(wm->dev_handle, write_buffer, 22, &bytes, &wm->hid_overlap);
} }
return 0; return 0;

View File

@@ -616,26 +616,12 @@ void wiiuse_send_next_pending_write_request(struct wiimote_t* wm) {
* *
* @param wm Pointer to a wiimote_t structure. * @param wm Pointer to a wiimote_t structure.
* @param report_type The report type to send (WIIMOTE_CMD_LED, WIIMOTE_CMD_RUMBLE, etc). Found in wiiuse.h * @param report_type The report type to send (WIIMOTE_CMD_LED, WIIMOTE_CMD_RUMBLE, etc). Found in wiiuse.h
* @param msg The payload. * @param msg The payload. Might be changed by the callee.
* @param len Length of the payload in bytes. * @param len Length of the payload in bytes.
* *
* This function should replace any write()s directly to the wiimote device. * This function should replace any write()s directly to the wiimote device.
*/ */
int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) { int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) {
byte buf[MAX_PAYLOAD]; /* no payload is better than this */
int rumble = 0;
#ifdef WIIUSE_WIN32
buf[0] = report_type;
#else
#ifdef WIIUSE_MAC
buf[0] = WM_SET_DATA | WM_BT_OUTPUT;
#else // *NIX
buf[0] = WM_SET_REPORT | WM_BT_OUTPUT;
#endif
buf[1] = report_type;
#endif
switch (report_type) { switch (report_type) {
case WM_CMD_LED: case WM_CMD_LED:
case WM_CMD_RUMBLE: case WM_CMD_RUMBLE:
@@ -643,43 +629,24 @@ int wiiuse_send(struct wiimote_t* wm, byte report_type, byte* msg, int len) {
{ {
/* Rumble flag for: 0x11, 0x13, 0x14, 0x15, 0x19 or 0x1a */ /* Rumble flag for: 0x11, 0x13, 0x14, 0x15, 0x19 or 0x1a */
if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE))
rumble = 1; msg[1] |= 0x01;
break; break;
} }
default: default:
break; break;
} }
#ifndef WIIUSE_WIN32
memcpy(buf+2, msg, len);
if (rumble)
buf[2] |= 0x01;
#else
memcpy(buf+1, msg, len);
if (rumble)
buf[1] |= 0x01;
#endif
#ifdef WITH_WIIUSE_DEBUG #ifdef WITH_WIIUSE_DEBUG
{ {
int x; int x;
#ifndef WIIUSE_WIN32 printf("[DEBUG] (id %i) SEND: (%.2x) %.2x ", wm->unid, report_type, msg[0]);
printf("[DEBUG] (id %i) SEND: (%x) %.2x ", wm->unid, buf[1], buf[2]); for (x = 1; x < len; ++x)
for (x = 3; x < len+2; ++x) printf("%.2x ", msg[x]);
#else
printf("[DEBUG] (id %i) SEND: (%x) %.2x ", wm->unid, buf[0], buf[1]);
for (x = 2; x < len+1; ++x)
#endif
printf("%.2x ", buf[x]);
printf("\n"); printf("\n");
} }
#endif #endif
#ifndef WIIUSE_WIN32 return wiiuse_os_write(wm, report_type, msg, len);
return wiiuse_os_write(wm, buf, len+2);
#else
return wiiuse_os_write(wm, buf, len+1);
#endif
} }