From 6e83fe241698b477540267608172b237a3d24bf1 Mon Sep 17 00:00:00 2001 From: snzgoo Date: Sun, 20 Jun 2010 02:17:53 +0000 Subject: [PATCH] win32: Some more work on real wiimote automatic ingame pairup (automatic paired up wiimotes get connected now ingame as well, but it's not completely working yet, we're almost there tho:P) and changed the automatic unpair real wiimotes routine, so it won't get loaded via dll_detach anymore. minor code changes and adding comments. And issue fix 2792 with credits to Karloathian for finding and fixing it. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5750 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Src/PluginWiimote.cpp | 2 + Source/Core/Common/Src/PluginWiimote.h | 2 + Source/Core/Core/Src/ConfigManager.cpp | 3 ++ Source/Core/Core/Src/ConfigManager.h | 1 + .../Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp | 13 ++++- Source/Core/DolphinWX/Src/Main.cpp | 7 +++ Source/PluginSpecs/pluginspecs_wiimote.h | 8 +++ Source/Plugins/Plugin_VideoOGL/Src/GLUtil.cpp | 2 +- Source/Plugins/Plugin_Wiimote/Src/main.cpp | 25 ++++----- .../Plugin_Wiimote/Src/wiimote_real.cpp | 54 +++++++++++++------ .../Plugin_WiimoteNew/Src/WiimoteNew.cpp | 11 ++++ 11 files changed, 95 insertions(+), 33 deletions(-) diff --git a/Source/Core/Common/Src/PluginWiimote.cpp b/Source/Core/Common/Src/PluginWiimote.cpp index 57285cd6b5..b25589aa97 100644 --- a/Source/Core/Common/Src/PluginWiimote.cpp +++ b/Source/Core/Common/Src/PluginWiimote.cpp @@ -32,6 +32,8 @@ PluginWiimote::PluginWiimote(const char *_Filename) (LoadSymbol("Wiimote_Update")); Wiimote_GetAttachedControllers = reinterpret_cast (LoadSymbol("Wiimote_GetAttachedControllers")); + Wiimote_UnPairWiimotes = reinterpret_cast + (LoadSymbol("Wiimote_UnPairWiimotes")); if ((Wiimote_ControlChannel != 0) && (Wiimote_Input != 0) && diff --git a/Source/Core/Common/Src/PluginWiimote.h b/Source/Core/Common/Src/PluginWiimote.h index d634febd47..a2761a9fc3 100644 --- a/Source/Core/Common/Src/PluginWiimote.h +++ b/Source/Core/Common/Src/PluginWiimote.h @@ -28,6 +28,7 @@ typedef void (__cdecl* TWiimote_Update)(int _number); typedef void (__cdecl* TWiimote_Input)(u16 _Key, u8 _UpDown); typedef void (__cdecl* TWiimote_ControlChannel)(int _number, u16 _channelID, const void* _pData, u32 _Size); typedef void (__cdecl* TWiimote_InterruptChannel)(int _number, u16 _channelID, const void* _pData, u32 _Size); +typedef unsigned int (__cdecl* TWiimote_UnPairWiimotes)(); typedef unsigned int (__cdecl* TWiimote_GetAttachedControllers)(); class PluginWiimote : public CPlugin { @@ -40,6 +41,7 @@ public: TWiimote_Input Wiimote_Input; TWiimote_InterruptChannel Wiimote_InterruptChannel; TWiimote_Update Wiimote_Update; + TWiimote_UnPairWiimotes Wiimote_UnPairWiimotes; TWiimote_GetAttachedControllers Wiimote_GetAttachedControllers; private: diff --git a/Source/Core/Core/Src/ConfigManager.cpp b/Source/Core/Core/Src/ConfigManager.cpp index 475584096a..4b520555c5 100644 --- a/Source/Core/Core/Src/ConfigManager.cpp +++ b/Source/Core/Core/Src/ConfigManager.cpp @@ -310,4 +310,7 @@ void SConfig::LoadSettingsWii() sprintf(SectionName, "Wiimote%i", i + 1); ini.Get(SectionName, "AutoReconnectRealWiimote", &m_WiiAutoReconnect[i], false); } + ini.Load((std::string(File::GetUserPath(D_CONFIG_IDX)) + "wiimote.ini").c_str()); + ini.Get("Real", "Unpair", &m_WiiAutoUnpair, false); + } diff --git a/Source/Core/Core/Src/ConfigManager.h b/Source/Core/Core/Src/ConfigManager.h index cb775b0520..6a1839b07b 100644 --- a/Source/Core/Core/Src/ConfigManager.h +++ b/Source/Core/Core/Src/ConfigManager.h @@ -45,6 +45,7 @@ struct SConfig bool m_WiiSDCard; bool m_WiiKeyboard; bool m_WiiAutoReconnect[4]; + bool m_WiiAutoUnpair; // hard coded default plugins ... std::string m_DefaultGFXPlugin; diff --git a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp index 37689dfaf0..132b4cc560 100644 --- a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp +++ b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp @@ -280,8 +280,17 @@ void CWII_IPC_HLE_WiiMote::ExecuteL2capCmd(u8* _pData, u32 _Size) break; case HID_INTERRUPT_CHANNEL: - if (number < 4) - mote->Wiimote_InterruptChannel(number, pHeader->CID, pData, DataSize); + { + if (number < 4) + { + DEBUG_LOG(WIIMOTE, "Wiimote_InterruptChannel"); + DEBUG_LOG(WIIMOTE, " Channel ID: %04x", pHeader->CID); + std::string Temp = ArrayToString((const u8*)pData, DataSize); + DEBUG_LOG(WIIMOTE, " Data: %s", Temp.c_str()); + + mote->Wiimote_InterruptChannel(number, pHeader->CID, pData, DataSize); + } + } break; default: diff --git a/Source/Core/DolphinWX/Src/Main.cpp b/Source/Core/DolphinWX/Src/Main.cpp index e26f398a80..0996873498 100644 --- a/Source/Core/DolphinWX/Src/Main.cpp +++ b/Source/Core/DolphinWX/Src/Main.cpp @@ -444,6 +444,13 @@ void DolphinApp::OnEndSession() int DolphinApp::OnExit() { +#ifdef _WIN32 + if (SConfig::GetInstance().m_WiiAutoUnpair) + { + if (CPluginManager::GetInstance().GetWiimote()) + CPluginManager::GetInstance().GetWiimote()->Wiimote_UnPairWiimotes(); + } +#endif CPluginManager::Shutdown(); SConfig::Shutdown(); LogManager::Shutdown(); diff --git a/Source/PluginSpecs/pluginspecs_wiimote.h b/Source/PluginSpecs/pluginspecs_wiimote.h index 824000eff4..342ca2bfd2 100644 --- a/Source/PluginSpecs/pluginspecs_wiimote.h +++ b/Source/PluginSpecs/pluginspecs_wiimote.h @@ -71,6 +71,14 @@ EXPORT void CALL Wiimote_InterruptChannel(int _number, u16 _channelID, const voi // EXPORT void CALL Wiimote_Update(int _number); +// __________________________________________________________________________________________________ +// Function: Wiimote_UnPairWiimotes +// Purpose: Unpair real wiimotes to safe battery +// input: none +// output: number of unpaired wiimotes +// +EXPORT unsigned int CALL Wiimote_UnPairWiimotes(); + // __________________________________________________________________________________________________ // Function: PAD_GetAttachedPads // Purpose: Get mask of attached pads (eg: controller 1 & 4 -> 0x9) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.cpp b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.cpp index 8c31d7dd7a..572d2cc099 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/GLUtil.cpp @@ -164,7 +164,7 @@ THREAD_RETURN XEventThread(void *pArg) { XEvent event; KeySym key; - for (int num_events = XPending(GLWin.dpy); num_events > 0; num_events--) { + for (int num_events = XPending(GLWin.dpy) -1; num_events > 0; num_events--) { XNextEvent(GLWin.dpy, &event); switch(event.type) { case KeyPress: diff --git a/Source/Plugins/Plugin_Wiimote/Src/main.cpp b/Source/Plugins/Plugin_Wiimote/Src/main.cpp index 5a73b36ddd..03ab1c7f28 100644 --- a/Source/Plugins/Plugin_Wiimote/Src/main.cpp +++ b/Source/Plugins/Plugin_Wiimote/Src/main.cpp @@ -105,12 +105,6 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, // DLL module handle case DLL_PROCESS_DETACH: { -#ifdef _WIN32 - if (g_Config.bUnpairRealWiimote){ - WiiMoteReal::Shutdown(); - WiiMoteReal::WiimotePairUp(true); - } -#endif #if defined(HAVE_WX) && HAVE_WX wxUninitialize(); #endif @@ -320,15 +314,7 @@ void Wiimote_Input(u16 _Key, u8 _UpDown) */ void Wiimote_InterruptChannel(int _number, u16 _channelID, const void* _pData, u32 _Size) { - // Debugging -#if defined(_DEBUG) || defined(DEBUGFAST) - DEBUG_LOG(WIIMOTE, "Wiimote_InterruptChannel"); - DEBUG_LOG(WIIMOTE, " Channel ID: %04x", _channelID); - std::string Temp = ArrayToString((const u8*)_pData, _Size); - DEBUG_LOG(WIIMOTE, " Data: %s", Temp.c_str()); -#endif - - // Decice where to send the message + // Decide where to send the message if (WiiMoteEmu::WiiMapping[_number].Source <= 1) WiiMoteEmu::InterruptChannel(_number, _channelID, _pData, _Size); #if HAVE_WIIUSE @@ -425,6 +411,15 @@ unsigned int Wiimote_GetAttachedControllers() } +// Unpair Wiimotes, TODO: Add linux/osx un-pair function +unsigned int Wiimote_UnPairWiimotes() +{ +#ifdef _WIN32 + if (g_Config.bUnpairRealWiimote) + return WiiMoteReal::WiimotePairUp(true); +#endif + return 0; +} // Supporting functions diff --git a/Source/Plugins/Plugin_Wiimote/Src/wiimote_real.cpp b/Source/Plugins/Plugin_Wiimote/Src/wiimote_real.cpp index 36afafbe81..8652dd4e16 100644 --- a/Source/Plugins/Plugin_Wiimote/Src/wiimote_real.cpp +++ b/Source/Plugins/Plugin_Wiimote/Src/wiimote_real.cpp @@ -87,7 +87,8 @@ Common::Event g_StartAutopairThread; int stoprefresh = 0; unsigned int PairUpTimer = 2000; -int PaiUpRefreshWiimote(); +int PairUpRefreshWiimote(bool addwiimote); +int PairUpFindNewSlot(void); THREAD_RETURN PairUp_ThreadFunc(void* arg); THREAD_RETURN RunInvisibleMessageWindow_ThreadFunc(void* arg); #endif @@ -648,8 +649,9 @@ THREAD_RETURN SafeCloseReadWiimote_ThreadFunc(void* arg) return 0; } -// WiiMote Pair-Up + #ifdef WIN32 +// WiiMote Pair-Up, function will return amount of either new paired or unpaired devices int WiimotePairUp(bool unpair) { HANDLE hRadios[256]; @@ -763,6 +765,7 @@ int WiimotePairUp(bool unpair) } #ifdef HAVE_WIIUSE +// Listening for new installed wiimotes, and calling PaiUpRefreshWiimote() when found LRESULT CALLBACK CallBackDeviceChange(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) @@ -782,7 +785,7 @@ LRESULT CALLBACK CallBackDeviceChange(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA { stoprefresh = 0; - PaiUpRefreshWiimote(); + PairUpRefreshWiimote(true); break; } else stoprefresh = 1; //fake arrival wait for second go @@ -791,7 +794,7 @@ LRESULT CALLBACK CallBackDeviceChange(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA case 0x8004: if (!stoprefresh) // removal event will pop up only once (it will also pop up if we add a device: fake arrival, fake removal, real arrival. { - PaiUpRefreshWiimote(); + PairUpRefreshWiimote(false); } break; } @@ -805,11 +808,12 @@ LRESULT CALLBACK CallBackDeviceChange(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA return 0; } - +// Generating a new invisible message window and listening for new messages THREAD_RETURN RunInvisibleMessageWindow_ThreadFunc(void* arg) { MSG Msg; HWND hwnd; + BOOL ret; WNDCLASSEX WCEx; ZeroMemory(&WCEx, sizeof(WCEx)); @@ -831,23 +835,27 @@ THREAD_RETURN RunInvisibleMessageWindow_ThreadFunc(void* arg) wiiuse_register_system_notification(hwnd); //function moved into wiiuse to avoid ddk/wdk dependicies - while(GetMessage(&Msg, 0, 0, 0) > 0) + while((ret = GetMessage(&Msg, 0, 0, 0)) != 0) { - TranslateMessage(&Msg); - DispatchMessage(&Msg); + if ( ret == -1) { + return 1; + } + else { + TranslateMessage(&Msg); + DispatchMessage(&Msg); + } } UnregisterClass(WCEx.lpszClassName, g_hInstance); - if (g_Config.bUnpairRealWiimote) - WiiMoteReal::WiimotePairUp(true); - return (int)Msg.wParam; } - -int PaiUpRefreshWiimote() +// function gets called by windows callbacks if a wiimote was either installed or removed +int PairUpRefreshWiimote(bool addwiimote) { + int connectslot = -1; + if (g_EmulatorState != PLUGIN_EMUSTATE_PLAY) { Shutdown(); @@ -862,16 +870,32 @@ int PaiUpRefreshWiimote() PostMessage(GetParent(g_WiimoteInitialize.hWnd), WM_USER, WM_USER_PAUSE, 0); while (g_EmulatorState == PLUGIN_EMUSTATE_PLAY) Sleep(50); Shutdown(); + if (addwiimote) { + connectslot = PairUpFindNewSlot(); + } Initialize(); Allocate(); PostMessage(GetParent(g_WiimoteInitialize.hWnd), WM_USER, WM_USER_PAUSE, 0); while (g_EmulatorState != PLUGIN_EMUSTATE_PLAY) Sleep(50); + if (addwiimote) + PostMessage(GetParent(g_WiimoteInitialize.hWnd), WM_USER, WM_USER_KEYDOWN, (3 + connectslot)); } return 0; } - - +// returns first inactive wiimote slot to place new wiimote and set type to real wiimote +int PairUpFindNewSlot() { + for(int x=0; x