From d7a5d7748955e9ef0fea94739d42aa6d034a5994 Mon Sep 17 00:00:00 2001 From: Lysann Schlegel Date: Sat, 8 Dec 2012 22:04:07 +0100 Subject: [PATCH] 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. --- src/os.h | 2 +- src/os_mac/os_mac.h | 2 +- src/os_mac/os_mac.m | 11 ++++++--- src/os_mac/os_mac_interface.m | 4 ++-- src/os_nix.c | 9 +++++-- src/os_win.c | 14 +++++++---- src/wiiuse.c | 45 +++++------------------------------ 7 files changed, 34 insertions(+), 53 deletions(-) diff --git a/src/os.h b/src/os.h index 7b86c44..a1b6b60 100644 --- a/src/os.h +++ b/src/os.h @@ -54,7 +54,7 @@ void wiiuse_os_disconnect(struct wiimote_t* wm); int wiiuse_os_poll(struct wiimote_t** wm, int wiimotes); /* 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_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 diff --git a/src/os_mac/os_mac.h b/src/os_mac/os_mac.h index 273d6a0..819d526 100644 --- a/src/os_mac/os_mac.h +++ b/src/os_mac/os_mac.h @@ -68,7 +68,7 @@ - (void) disconnect; - (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 diff --git a/src/os_mac/os_mac.m b/src/os_mac/os_mac.m index 8e2f57a..4a9c99b 100644 --- a/src/os_mac/os_mac.m +++ b/src/os_mac/os_mac.m @@ -209,13 +209,18 @@ 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) { WIIUSE_ERROR("Attempted to write to nil interrupt channel [id %i].", wm->unid); 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) { WIIUSE_ERROR("Error writing to interrupt channel [id %i].", wm->unid); @@ -227,7 +232,7 @@ [self disconnect]; } else { 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) WIIUSE_ERROR("Unable to write again to the interrupt channel [id %i].", wm->unid); } diff --git a/src/os_mac/os_mac_interface.m b/src/os_mac/os_mac_interface.m index ed5adfa..ca67408 100644 --- a/src/os_mac/os_mac_interface.m +++ b/src/os_mac/os_mac_interface.m @@ -182,7 +182,7 @@ int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) { 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(!WIIMOTE_IS_CONNECTED(wm)) { 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]; 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]; return result; diff --git a/src/os_nix.c b/src/os_nix.c index c454f58..88588d5 100644 --- a/src/os_nix.c +++ b/src/os_nix.c @@ -343,9 +343,14 @@ int wiiuse_os_read(struct wiimote_t* wm, byte* buf, int len) { 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; - 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) wiiuse_disconnected(wm); diff --git a/src/os_win.c b/src/os_win.c index 489ff40..0173e04 100644 --- a/src/os_win.c +++ b/src/os_win.c @@ -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; int i; + byte write_buffer[MAX_PAYLOAD]; if (!wm || !WIIMOTE_IS_CONNECTED(wm)) return 0; + write_buffer[0] = report_type; + memcpy(write_buffer+1, buf, len); + switch (wm->stack) { case WIIUSE_STACK_UNKNOWN: { /* 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 */ wm->stack = WIIUSE_STACK_BLUESOLEIL; 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; return i; } @@ -285,10 +289,10 @@ int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len) { } 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: - 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; diff --git a/src/wiiuse.c b/src/wiiuse.c index ee127c9..2b813d3 100644 --- a/src/wiiuse.c +++ b/src/wiiuse.c @@ -616,26 +616,12 @@ void wiiuse_send_next_pending_write_request(struct wiimote_t* wm) { * * @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 msg The payload. + * @param msg The payload. Might be changed by the callee. * @param len Length of the payload in bytes. * * 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) { - 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) { case WM_CMD_LED: 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 */ if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_RUMBLE)) - rumble = 1; + msg[1] |= 0x01; break; } default: 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 { int x; - #ifndef WIIUSE_WIN32 - printf("[DEBUG] (id %i) SEND: (%x) %.2x ", wm->unid, buf[1], buf[2]); - for (x = 3; x < len+2; ++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("[DEBUG] (id %i) SEND: (%.2x) %.2x ", wm->unid, report_type, msg[0]); + for (x = 1; x < len; ++x) + printf("%.2x ", msg[x]); printf("\n"); } #endif - #ifndef WIIUSE_WIN32 - return wiiuse_os_write(wm, buf, len+2); - #else - return wiiuse_os_write(wm, buf, len+1); - #endif + return wiiuse_os_write(wm, report_type, msg, len); }