From 07a6b2dec5574efbe3dd5055a92add0579c7cb11 Mon Sep 17 00:00:00 2001 From: "fires.gc" Date: Wed, 29 Oct 2008 11:23:40 +0000 Subject: [PATCH] added an report mode cursor position is now the same like the mouse position git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@998 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../Plugin_Wiimote_Test/Src/Wiimote.cpp | 337 +++++++++++++----- .../Plugin_Wiimote_Test/Src/wiimote_hid.h | 31 +- 2 files changed, 278 insertions(+), 90 deletions(-) diff --git a/Source/Plugins/Plugin_Wiimote_Test/Src/Wiimote.cpp b/Source/Plugins/Plugin_Wiimote_Test/Src/Wiimote.cpp index f86ee73eb5..0b5cd6ebd5 100644 --- a/Source/Plugins/Plugin_Wiimote_Test/Src/Wiimote.cpp +++ b/Source/Plugins/Plugin_Wiimote_Test/Src/Wiimote.cpp @@ -15,12 +15,22 @@ SWiimoteInitialize g_WiimoteInitialize; //****************************************************************************** // Definitions and variable declarations //****************************************************************************** + +//libogc bounding box, in smoothed IR coordinates: 232,284 792,704 +//we'll use it to scale our mouse coordinates +#define LEFT 232 +#define TOP 284 +#define RIGHT 792 +#define BOTTOM 704 +#define SENSOR_BAR_RADIUS 200 + +// vars #define WIIMOTE_EEPROM_SIZE (16*1024) #define WIIMOTE_REG_SPEAKER_SIZE 10 #define WIIMOTE_REG_EXT_SIZE 0x100 #define WIIMOTE_REG_IR_SIZE 0x34 -u8 g_Leds; +u8 g_Leds = 0x1; u8 g_Eeprom[WIIMOTE_EEPROM_SIZE]; @@ -72,6 +82,7 @@ void SendReadDataReply(u16 _channelID, void* _Base, u16 _Address, u8 _Size); void SendReportCoreAccel(u16 _channelID); void SendReportCoreAccelIr12(u16 _channelID); void SendReportCore(u16 _channelID); +void SendReportCoreAccelIr10Ext(u16 _channelID); int WriteWmReport(u8* dst, u8 channel); void WmSendAck(u16 _channelID, u8 _reportID); @@ -86,6 +97,30 @@ static u16 convert16bit(const u8* src) { #ifdef _WIN32 HINSTANCE g_hInstance; +void GetMousePos(float& x, float& y) +{ +#ifdef _WIN32 + POINT point; + + GetCursorPos(&point); + ScreenToClient(g_WiimoteInitialize.hWnd, &point); + + RECT Rect; + GetClientRect(g_WiimoteInitialize.hWnd, &Rect); + + int width = Rect.right - Rect.left; + int height = Rect.bottom - Rect.top; + + x = point.x / (float)width; + y = point.y / (float)height; +#else + x = 0.5f; + y = 0.5f; +#endif +} + + + class wxDLLApp : public wxApp { bool OnInit() @@ -160,6 +195,22 @@ extern "C" void DllConfig(HWND _hParent) { } +void CryptBuffer(u8* _buffer, u8 _size) +{ + for (int i=0; i<_size; i++) + { + _buffer[i] = ((_buffer[i] - 0x17) ^ 0x17) & 0xFF; + } +} + +void WriteCryped16(u8* _baseBlock, u16 _address, u16 _value) +{ + u16 cryptedValue = _value; + CryptBuffer((u8*)&cryptedValue, sizeof(u16)); + + *(u16*)(_baseBlock + _address) = cryptedValue; +} + extern "C" void Wiimote_Initialize(SWiimoteInitialize _WiimoteInitialize) { g_WiimoteInitialize = _WiimoteInitialize; @@ -169,6 +220,14 @@ extern "C" void Wiimote_Initialize(SWiimoteInitialize _WiimoteInitialize) memcpy(g_Eeprom + 0x16D0, EepromData_16D0, sizeof(EepromData_16D0)); g_ReportingMode = 0; + + + + WriteCryped16(g_RegExt, 0xfe, 0x0000); + +// g_RegExt[0xfd] = 0x1e; +// g_RegExt[0xfc] = 0x9a; + } extern "C" void Wiimote_DoState(void* ptr, int mode) @@ -289,6 +348,7 @@ extern "C" void Wiimote_Update() { case WM_REPORT_CORE: SendReportCore(g_ReportingChannel); break; case WM_REPORT_CORE_ACCEL: SendReportCoreAccel(g_ReportingChannel); break; case WM_REPORT_CORE_ACCEL_IR12: SendReportCoreAccelIr12(g_ReportingChannel);break; + case WM_REPORT_CORE_ACCEL_IR10_EXT6: SendReportCoreAccelIr10Ext(g_ReportingChannel);break; } // g_ReportingMode = 0; } @@ -307,7 +367,6 @@ void HidOutputReport(u16 _channelID, wm_report* sr) { { case WM_LEDS: WmLeds(_channelID, (wm_leds*)sr->data); - // WmSendAck(_channelID, WM_LEDS); break; case WM_READ_DATA: WmReadData(_channelID, (wm_read_data*)sr->data); @@ -318,7 +377,6 @@ void HidOutputReport(u16 _channelID, wm_report* sr) { case WM_IR_PIXEL_CLOCK: case WM_IR_LOGIC: LOG(WIIMOTE, " IR Enable 0x%02x 0x%02x", sr->channel, sr->data[0]); - // WmSendAck(_channelID, WM_IR_LOGIC); break; case WM_WRITE_DATA: WmWriteData(_channelID, (wm_write_data*)sr->data); @@ -327,6 +385,14 @@ void HidOutputReport(u16 _channelID, wm_report* sr) { WmDataReporting(_channelID, (wm_data_reporting*)sr->data); break; + case WM_SPEAKER_ENABLE: + LOG(WIIMOTE, " WM Speaker Enable 0x%02x 0x%02x", sr->channel, sr->data[0]); + break; + + case WM_SPEAKER_MUTE: + LOG(WIIMOTE, " WM Mute Enable 0x%02x 0x%02x", sr->channel, sr->data[0]); + break; + default: PanicAlert("HidOutputReport: Unknown channel 0x%02x", sr->channel); return; @@ -342,7 +408,6 @@ void WmLeds(u16 _channelID, wm_leds* leds) { } void WmSendAck(u16 _channelID, u8 _reportID) { - u8 DataFrame[1024]; u32 Offset = 0; @@ -380,9 +445,10 @@ void WmDataReporting(u16 _channelID, wm_data_reporting* dr) g_ReportingMode = dr->mode; g_ReportingChannel = _channelID; switch(dr->mode) { //see Wiimote_Update() - case 0x30: - case 0x31: - case 0x33: + case WM_REPORT_CORE: + case WM_REPORT_CORE_ACCEL: + case WM_REPORT_CORE_ACCEL_IR12: + case WM_REPORT_CORE_ACCEL_IR10_EXT6: break; default: PanicAlert("Wiimote: Unknown reporting mode 0x%x", dr->mode); @@ -394,8 +460,8 @@ void WmDataReporting(u16 _channelID, wm_data_reporting* dr) void FillReportInfo(wm_core& _core) { - static bool bb = true; - bb = !bb; + memset(&_core, 0x00, sizeof(wm_core)); + #ifdef _WIN32 _core.a = GetAsyncKeyState(VK_LBUTTON) ? 1 : 0; _core.b = GetAsyncKeyState(VK_RBUTTON) ? 1 : 0; @@ -404,6 +470,69 @@ void FillReportInfo(wm_core& _core) #endif } +void FillReportAcc(wm_accel& _acc) +{ + _acc.x = 0x00; + _acc.y = 0x00; + _acc.z = 0x00; +} + +void FillReportIR(wm_ir_extended& _ir0, wm_ir_extended& _ir1) +{ + memset(&_ir0, 0xFF, sizeof(wm_ir_extended)); + memset(&_ir1, 0xFF, sizeof(wm_ir_extended)); + + float MouseX, MouseY; + GetMousePos(MouseX, MouseY); + + int y0 = TOP + (MouseY * (BOTTOM - TOP)); + int y1 = TOP + (MouseY * (BOTTOM - TOP)); + + int x0 = LEFT + (MouseX * (RIGHT - LEFT)) - SENSOR_BAR_RADIUS; + int x1 = LEFT + (MouseX * (RIGHT - LEFT)) + SENSOR_BAR_RADIUS; + + x0 = 1023 - x0; + _ir0.x = x0 & 0xFF; + _ir0.y = y0 & 0xFF; + _ir0.size = 10; + _ir0.xHi = x0 >> 8; + _ir0.yHi = y0 >> 8; + + x1 = 1023 - x1; + _ir1.x = x1; + _ir1.y = y1 & 0xFF; + _ir1.size = 10; + _ir1.xHi = x1 >> 8; + _ir1.yHi = y1 >> 8; +} + +void FillReportIRBasic(wm_ir_basic& _ir0, wm_ir_basic& _ir1) +{ + memset(&_ir0, 0xFF, sizeof(wm_ir_basic)); + memset(&_ir1, 0xFF, sizeof(wm_ir_basic)); + + float MouseX, MouseY; + GetMousePos(MouseX, MouseY); + + int y1 = TOP + (MouseY * (BOTTOM - TOP)); + int y2 = TOP + (MouseY * (BOTTOM - TOP)); + + int x1 = LEFT + (MouseX * (RIGHT - LEFT)) - SENSOR_BAR_RADIUS; + int x2 = LEFT + (MouseX * (RIGHT - LEFT)) + SENSOR_BAR_RADIUS; + + x1 = 1023 - x1; + _ir0.x1 = x1 & 0xFF; + _ir0.y1 = y1 & 0xFF; + _ir0.x1High = (x1 >> 8) & 0x3; + _ir0.y1High = (y1 >> 8) & 0x3; + + x2 = 1023 - x2; + _ir1.x2 = x2 & 0xFF; + _ir1.y2 = y2 & 0xFF; + _ir1.x2High = (x2 >> 8) & 0x3; + _ir1.y2High = (y2 >> 8) & 0x3; +} + void SendReportCore(u16 _channelID) { @@ -429,59 +558,37 @@ void SendReportCoreAccelIr12(u16 _channelID) { wm_report_core_accel_ir12* pReport = (wm_report_core_accel_ir12*)(DataFrame + Offset); Offset += sizeof(wm_report_core_accel_ir12); memset(pReport, 0, sizeof(wm_report_core_accel_ir12)); - memset(pReport->ir, 0xFF, sizeof(pReport->ir)); - - pReport->a.x = 0x81; - pReport->a.y = 0x78; - pReport->a.z = 0xD9; - - int x0, y0, x1, y1; - -#ifdef _WIN32 - //libogc bounding box, in smoothed IR coordinates: 232,284 792,704 - //we'll use it to scale our mouse coordinates -#define LEFT 232 -#define TOP 284 -#define RIGHT 792 -#define BOTTOM 704 - -#define SENSOR_BAR_RADIUS 200 - - RECT screenRect; - POINT point; - GetClipCursor(&screenRect); - GetCursorPos(&point); - y0 = y1 = (point.y * (screenRect.bottom - screenRect.top)) / (BOTTOM - TOP); - int x = (point.x * (screenRect.right - screenRect.left)) / (RIGHT - LEFT); - x0 = x - SENSOR_BAR_RADIUS; - x1 = x + SENSOR_BAR_RADIUS; -#else - x0 = 600; - y0 = 440; - x1 = 100; - y1 = 450; -#endif - - x0 = 1023 - x0; - pReport->ir[0].x = x0 & 0xFF; - pReport->ir[0].y = y0 & 0xFF; - pReport->ir[0].size = 10; - pReport->ir[0].xHi = x0 >> 8; - pReport->ir[0].yHi = y0 >> 8; - - x1 = 1023 - x1; - pReport->ir[1].x = x1; - pReport->ir[1].y = y1 & 0xFF; - pReport->ir[1].size = 10; - pReport->ir[1].xHi = x1 >> 8; - pReport->ir[1].yHi = y1 >> 8; + + FillReportInfo(pReport->c); + FillReportAcc(pReport->a); + FillReportIR(pReport->ir[0], pReport->ir[1]); LOG(WIIMOTE, " SendReportCoreAccelIr12()"); g_WiimoteInitialize.pWiimoteInput(_channelID, DataFrame, Offset); } -void SendReportCoreAccel(u16 _channelID) { +void SendReportCoreAccelIr10Ext(u16 _channelID) +{ + u8 DataFrame[1024]; + u32 Offset = WriteWmReport(DataFrame, WM_REPORT_CORE_ACCEL_IR10_EXT6); + + wm_report_core_accel_ir10_ext6* pReport = (wm_report_core_accel_ir10_ext6*)(DataFrame + Offset); + Offset += sizeof(wm_report_core_accel_ir10_ext6); + memset(pReport, 0, sizeof(wm_report_core_accel_ir10_ext6)); + + FillReportInfo(pReport->c); + FillReportAcc(pReport->a); + FillReportIRBasic(pReport->ir[0], pReport->ir[1]); + + LOG(WIIMOTE, " SendReportCoreAccelIr10Ext()"); + + g_WiimoteInitialize.pWiimoteInput(_channelID, DataFrame, Offset); +} + + +void SendReportCoreAccel(u16 _channelID) +{ u8 DataFrame[1024]; u32 Offset = WriteWmReport(DataFrame, WM_REPORT_CORE_ACCEL); @@ -489,17 +596,16 @@ void SendReportCoreAccel(u16 _channelID) { Offset += sizeof(wm_report_core_accel); memset(pReport, 0, sizeof(wm_report_core_accel)); - pReport->c.a = 1; - pReport->a.x = 0x82; - pReport->a.y = 0x75; - pReport->a.z = 0xD6; + FillReportInfo(pReport->c); + FillReportAcc(pReport->a); LOG(WIIMOTE, " SendReportCoreAccel()"); g_WiimoteInitialize.pWiimoteInput(_channelID, DataFrame, Offset); } -void WmReadData(u16 _channelID, wm_read_data* rd) { +void WmReadData(u16 _channelID, wm_read_data* rd) +{ u32 address = convert24bit(rd->address); u16 size = convert16bit(rd->size); LOG(WIIMOTE, " Read data"); @@ -508,16 +614,54 @@ void WmReadData(u16 _channelID, wm_read_data* rd) { LOG(WIIMOTE, " Size: 0x%04x", size); LOG(WIIMOTE, " Rumble: %x", rd->rumble); - if(size <= 100 && rd->space == 0) { - SendReadDataReply(_channelID, g_Eeprom, address, (u8)size); + if(rd->space == 0) + { + if (address + size > WIIMOTE_EEPROM_SIZE) + { + PanicAlert("WmReadData: address + size out of bounds!"); + return; + } + SendReadDataReply(_channelID, g_Eeprom+address, address, (u8)size); } - else - { + else if(rd->space == WM_SPACE_REGS1 || rd->space == WM_SPACE_REGS2) + { + u8* block; + u32 blockSize; + switch((address >> 16) & 0xFE) + { +/* case 0xA2: + block = g_RegSpeaker; + blockSize = WIIMOTE_REG_SPEAKER_SIZE; + break;*/ + case 0xA4: + block = g_RegExt; + blockSize = WIIMOTE_REG_EXT_SIZE; + PanicAlert("fsfsd"); + break; +/* case 0xB0: + block = g_RegIr; + blockSize = WIIMOTE_REG_IR_SIZE; + break;*/ + default: + PanicAlert("WmWriteData: bad register block!"); + return; + } + address &= 0xFFFF; + if(address + size > blockSize) { + PanicAlert("WmReadData: address + size out of bounds!"); + return; + } + + SendReadDataReply(_channelID, block+address, address, (u8)size); + } + else + { PanicAlert("WmReadData: unimplemented parameters (size: %i, addr: 0x%x!", size, rd->space); } } -void WmWriteData(u16 _channelID, wm_write_data* wd) { +void WmWriteData(u16 _channelID, wm_write_data* wd) +{ u32 address = convert24bit(wd->address); LOG(WIIMOTE, " Write data"); LOG(WIIMOTE, " Address space: %x", wd->space); @@ -561,9 +705,8 @@ void WmWriteData(u16 _channelID, wm_write_data* wd) { PanicAlert("WmWriteData: address + size out of bounds!"); return; } - memcpy(block + address, wd->data, wd->size); + memcpy(wd->data, block + address, wd->size); - // WmSendAck(_channelID, WM_WRITE_DATA); } else { PanicAlert("WmWriteData: unimplemented parameters!"); } @@ -595,7 +738,8 @@ void WmRequestStatus(u16 _channelID, wm_request_status* rs) { memset(pStatus, 0, sizeof(wm_status_report)); pStatus->leds = g_Leds; pStatus->ir = 1; - pStatus->battery = 100; //arbitrary number + pStatus->battery = 0x4F; //arbitrary number + pStatus->extension = 0; LOG(WIIMOTE, " SendStatusReport()"); LOG(WIIMOTE, " Flags: 0x%02x", pStatus->padding1[2]); @@ -606,27 +750,44 @@ void WmRequestStatus(u16 _channelID, wm_request_status* rs) { void SendReadDataReply(u16 _channelID, void* _Base, u16 _Address, u8 _Size) { - u8 DataFrame[1024]; - u32 Offset = WriteWmReport(DataFrame, WM_READ_DATA_REPLY); + int dataOffset = 0; + while (_Size > 0) + { + u8 DataFrame[1024]; + u32 Offset = WriteWmReport(DataFrame, WM_READ_DATA_REPLY); + + int copySize = _Size; + if (copySize > 16) + { + copySize = 16; + } - _dbg_assert_(WIIMOTE, _Size <= 16); + wm_read_data_reply* pReply = (wm_read_data_reply*)(DataFrame + Offset); + Offset += sizeof(wm_read_data_reply); + pReply->buttons = 0; + pReply->error = 0; + pReply->size = (copySize - 1) & 0xF; + pReply->address = Common::swap16(_Address + dataOffset); + memcpy(pReply->data + dataOffset, _Base, copySize); + if(copySize < 16) + { + memset(pReply->data + copySize, 0, 16 - copySize); + } + dataOffset += copySize; - wm_read_data_reply* pReply = (wm_read_data_reply*)(DataFrame + Offset); - Offset += sizeof(wm_read_data_reply); - pReply->buttons = 0; - pReply->error = 0; - pReply->size = _Size - 1; - pReply->address = Common::swap16(_Address); - memcpy(pReply->data, _Base, _Size); - if(_Size < 16) { - memset(pReply->data + _Size, 0, 16 - _Size); + LOG(WIIMOTE, " SendReadDataReply()"); + LOG(WIIMOTE, " Buttons: 0x%04x", pReply->buttons); + LOG(WIIMOTE, " Error: 0x%x", pReply->error); + LOG(WIIMOTE, " Size: 0x%x", pReply->size); + LOG(WIIMOTE, " Address: 0x%04x", pReply->address); + + g_WiimoteInitialize.pWiimoteInput(_channelID, DataFrame, Offset); + + _Size -= copySize; } - LOG(WIIMOTE, " SendReadDataReply()"); - LOG(WIIMOTE, " Buttons: 0x%04x", pReply->buttons); - LOG(WIIMOTE, " Error: 0x%x", pReply->error); - LOG(WIIMOTE, " Size: 0x%x", pReply->size); - LOG(WIIMOTE, " Address: 0x%04x", pReply->address); - - g_WiimoteInitialize.pWiimoteInput(_channelID, DataFrame, Offset); + if (_Size != 0) + { + PanicAlert("WiiMote-Plugin: SendReadDataReply() failed"); + } } diff --git a/Source/Plugins/Plugin_Wiimote_Test/Src/wiimote_hid.h b/Source/Plugins/Plugin_Wiimote_Test/Src/wiimote_hid.h index 79e83789a6..3732fe5cc5 100644 --- a/Source/Plugins/Plugin_Wiimote_Test/Src/wiimote_hid.h +++ b/Source/Plugins/Plugin_Wiimote_Test/Src/wiimote_hid.h @@ -70,7 +70,8 @@ struct wm_status_report { }; #define WM_WRITE_DATA 0x16 -struct wm_write_data { +struct wm_write_data +{ u8 rumble : 1; u8 space : 2; //see WM_SPACE_* u8 : 5; @@ -137,7 +138,20 @@ struct wm_accel { }; //filled with 0xFF if empty -struct wm_ir_extended { +struct wm_ir_basic +{ + u8 x1; + u8 y1; + u8 x2High : 2; + u8 y2High : 2; + u8 x1High : 2; + u8 y1High : 2; + u8 x2; + u8 y2; +}; + +struct wm_ir_extended +{ u8 x; u8 y; u8 size : 4; @@ -168,11 +182,24 @@ struct wm_report_core_accel_ir12 { #define WM_REPORT_CORE_EXT19 0x34 #define WM_REPORT_CORE_ACCEL_EXT16 0x35 #define WM_REPORT_CORE_IR10_EXT9 0x36 + #define WM_REPORT_CORE_ACCEL_IR10_EXT6 0x37 +struct wm_report_core_accel_ir10_ext6 +{ + wm_core c; + wm_accel a; + wm_ir_basic ir[2]; + u8 ext[6]; +}; + #define WM_REPORT_EXT21 0x3d #define WM_REPORT_INTERLEAVE1 0x3e #define WM_REPORT_INTERLEAVE2 0x3f +#define WM_SPEAKER_ENABLE 0x14 +#define WM_SPEAKER_MUTE 0x19 + + #if defined(_MSC_VER) #pragma pack(pop) #endif