From ca827b99309b0aea15df07a176ab3cf3c5840d95 Mon Sep 17 00:00:00 2001 From: dapetcu21 Date: Mon, 5 Jul 2010 10:46:32 +0000 Subject: [PATCH] Added UDPWii support to the new plugin. Hopefully I didn't made a mess... Nunchuck support not implemented yet. I want to make it a separate extension. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5835 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/InputCommon/InputCommon.vcproj | 18 +- Source/Core/InputCommon/Src/ControllerEmu.h | 9 +- Source/Core/InputCommon/Src/SConscript | 4 +- Source/Core/InputCommon/Src/UDPWiimote.cpp | 302 ++++++++++++++++++ Source/Core/InputCommon/Src/UDPWiimote.h | 50 +++ Source/Core/InputCommon/Src/UDPWrapper.cpp | 191 +++++++++++ Source/Core/InputCommon/Src/UDPWrapper.h | 34 ++ Source/Core/InputUICommon/Src/ConfigDiag.cpp | 13 + Source/Core/InputUICommon/Src/ConfigDiag.h | 14 + .../Plugin_Wiimote/Src/EmuDynamics.cpp | 11 +- .../Plugins/Plugin_Wiimote/Src/UDPWiimote.cpp | 11 +- .../Plugins/Plugin_Wiimote/Src/UDPWiimote.h | 2 +- .../Plugin_WiimoteNew.vcproj | 6 +- .../Src/WiimoteEmu/UDPTLayer.h | 53 +++ .../Src/WiimoteEmu/WiimoteEmu.cpp | 11 +- .../Src/WiimoteEmu/WiimoteEmu.h | 4 + 16 files changed, 715 insertions(+), 18 deletions(-) create mode 100644 Source/Core/InputCommon/Src/UDPWiimote.cpp create mode 100644 Source/Core/InputCommon/Src/UDPWiimote.h create mode 100644 Source/Core/InputCommon/Src/UDPWrapper.cpp create mode 100644 Source/Core/InputCommon/Src/UDPWrapper.h create mode 100644 Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/UDPTLayer.h diff --git a/Source/Core/InputCommon/InputCommon.vcproj b/Source/Core/InputCommon/InputCommon.vcproj index 5150ecb61f..e9c73840be 100644 --- a/Source/Core/InputCommon/InputCommon.vcproj +++ b/Source/Core/InputCommon/InputCommon.vcproj @@ -1,7 +1,7 @@ + + + + + + + + diff --git a/Source/Core/InputCommon/Src/ControllerEmu.h b/Source/Core/InputCommon/Src/ControllerEmu.h index 616a459b7b..c798087caa 100644 --- a/Source/Core/InputCommon/Src/ControllerEmu.h +++ b/Source/Core/InputCommon/Src/ControllerEmu.h @@ -45,6 +45,7 @@ enum GROUP_TYPE_TILT, GROUP_TYPE_CURSOR, GROUP_TYPE_TRIGGERS, + GROUP_TYPE_UDPWII }; const char * const named_directions[] = @@ -117,8 +118,8 @@ public: ControlGroup( const char* const _name, const unsigned int _type = GROUP_TYPE_OTHER ) : name(_name), type(_type) {} virtual ~ControlGroup(); - void LoadConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" ); - void SaveConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" ); + virtual void LoadConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" ); + virtual void SaveConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" ); const char* const name; const unsigned int type; @@ -422,8 +423,8 @@ public: virtual void LoadDefaults() {} - void LoadConfig(IniFile::Section *sec, const std::string& base = ""); - void SaveConfig(IniFile::Section *sec, const std::string& base = ""); + virtual void LoadConfig(IniFile::Section *sec, const std::string& base = ""); + virtual void SaveConfig(IniFile::Section *sec, const std::string& base = ""); void UpdateDefaultDevice(); void UpdateReferences( ControllerInterface& devi ); diff --git a/Source/Core/InputCommon/Src/SConscript b/Source/Core/InputCommon/Src/SConscript index 09b580ef4e..d87d3be4df 100644 --- a/Source/Core/InputCommon/Src/SConscript +++ b/Source/Core/InputCommon/Src/SConscript @@ -8,7 +8,9 @@ files = [ 'InputConfig.cpp', 'SDL_Util.cpp', 'ControllerInterface/ControllerInterface.cpp', - 'ControllerInterface/SDL/SDL.cpp' + 'ControllerInterface/SDL/SDL.cpp', + 'UDPWiimote.cpp', + 'UDPWrapper.cpp' ] diff --git a/Source/Core/InputCommon/Src/UDPWiimote.cpp b/Source/Core/InputCommon/Src/UDPWiimote.cpp new file mode 100644 index 0000000000..943f16f3c9 --- /dev/null +++ b/Source/Core/InputCommon/Src/UDPWiimote.cpp @@ -0,0 +1,302 @@ +#include "UDPWiimote.h" + +#ifdef _WIN32 + +#include +#include +#define sock_t SOCKET +#define ERRNO WSAGetLastError() +#define EWOULDBLOCK WSAEWOULDBLOCK +#define BAD_SOCK INVALID_SOCKET +#define close(x) closesocket(x) +#define cleanup do {noinst--; if (noinst==0) WSACleanup();} while (0) +#define blockingoff(sock) ioctlsocket(sock, FIONBIO, &iMode) +#define dataz char* +#ifdef _MSC_VER +#pragma comment (lib, "Ws2_32.lib") +#endif + +#else + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define BAD_SOCK -1 +#define ERRNO errno +#define cleanup noinst-- +#define blockingoff(sock) fcntl(sock, F_SETFL, O_NONBLOCK) +#define dataz void* +#define sock_t int + +#endif + +#include "Thread.h" +#include +#include +#include + +struct UDPWiimote::_d +{ + Common::Thread * thread; + std::list sockfds; + Common::CriticalSection termLock,mutex; + volatile bool exit; +}; + +int UDPWiimote::noinst=0; + +void _UDPWiiThread(void* arg) +{ + ((UDPWiimote*)arg)->mainThread(); + //NOTICE_LOG(WIIMOTE,"UDPWii thread stopped"); +} + +THREAD_RETURN UDPWiiThread(void* arg) +{ + _UDPWiiThread(arg); + return 0; +} + +UDPWiimote::UDPWiimote(const char *_port) : +d(new _d) ,x(0),y(0),z(0),nunX(0),nunY(0), +pointerX(-0.1),pointerY(-0.1),nunMask(0),mask(0),time(0),port(_port) +{ + #ifdef _WIN32 + u_long iMode = 1; + #endif + struct addrinfo hints, *servinfo, *p; + int rv; + d->thread=NULL; + + #ifdef _WIN32 + if (noinst==0) + { + WORD sockVersion; + WSADATA wsaData; + sockVersion = MAKEWORD(2, 2); + WSAStartup(sockVersion, &wsaData); + } + #endif + + noinst++; + //PanicAlert("UDPWii instantiated"); + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_flags = AI_PASSIVE; // use my IP + + if ((rv = getaddrinfo(NULL, _port, &hints, &servinfo)) != 0) { + cleanup; + err=-1; + return; + } + + // loop through all the results and bind to everything we can + for(p = servinfo; p != NULL; p = p->ai_next) { + sock_t sock; + if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == BAD_SOCK) { + continue; + } + + if (bind(sock, p->ai_addr, p->ai_addrlen) == -1) { + close(sock); + continue; + } + + + //NOTICE_LOG(WIIMOTE,"UDPWii new listening sock"); + d->sockfds.push_back(sock); + } + + if (d->sockfds.empty()) { + cleanup; + err=-2; + return; + } + + freeaddrinfo(servinfo); + err=0; + d->exit=false; +// NOTICE_LOG(WIIMOTE,"UDPWii thread starting"); + d->termLock.Enter(); + d->thread = new Common::Thread(UDPWiiThread,this); + d->termLock.Leave(); + return; +} + +void UDPWiimote::mainThread() +{ + d->termLock.Enter(); +// NOTICE_LOG(WIIMOTE,"UDPWii thread started"); + fd_set fds; + struct timeval timeout; + timeout.tv_sec=1; + timeout.tv_usec=500000; + //Common::Thread * thisthr= d->thread; + do + { + int maxfd=0; + FD_ZERO(&fds); + for (std::list::iterator i=d->sockfds.begin(); i!=d->sockfds.end(); i++) + { + FD_SET(*i,&fds); +#ifndef _WIN32 + if (*i>=maxfd) + maxfd=(*i)+1; +#endif + } + d->termLock.Leave(); + if (d->exit) return; + int rt=select(maxfd,&fds,NULL,NULL,&timeout); + if (d->exit) return; + d->termLock.Enter(); + if (d->exit) return; + if (rt) + { + for (std::list::iterator i=d->sockfds.begin(); i!=d->sockfds.end(); i++) + if (FD_ISSET(*i,&fds)) + { + sock_t fd=*i; + u8 bf[64]; + int size=60; + size_t addr_len; + struct sockaddr_storage their_addr; + addr_len = sizeof their_addr; + if ((size = recvfrom(fd, (dataz)bf, size , 0,(struct sockaddr *)&their_addr, (socklen_t*)&addr_len)) == -1) + { + ERROR_LOG(WIIMOTE,"UDPWii Packet error"); + } + else + { + d->mutex.Enter(); + if (pharsePacket(bf,size)==0) + { + //NOTICE_LOG(WIIMOTE,"UDPWII New pack"); + } else { + //NOTICE_LOG(WIIMOTE,"UDPWII Wrong pack format... ignoring"); + } + d->mutex.Leave(); + } + } + } else { + broadcastPresence(); + } + } while (!(d->exit)); + d->termLock.Leave(); + //delete thisthr; +} + +UDPWiimote::~UDPWiimote() +{ + d->exit=true; + d->termLock.Enter(); + d->termLock.Leave(); + for (std::list::iterator i=d->sockfds.begin(); i!=d->sockfds.end(); i++) + close(*i); + cleanup; + delete d; + //PanicAlert("UDPWii destructed"); +} + +#define ACCEL_FLAG (1<<0) +#define BUTT_FLAG (1<<1) +#define IR_FLAG (1<<2) +#define NUN_FLAG (1<<3) + +int UDPWiimote::pharsePacket(u8 * bf, size_t size) +{ + if (size<3) return -1; + if (bf[0]!=0xde) + return -1; + if (bf[1]==0) + time=0; + if (bf[1]mutex.Enter(); + _x=x; + _y=y; + _z=z; + d->mutex.Leave(); + //NOTICE_LOG(WIIMOTE,"%lf %lf %lf",_x, _y, _z); +} + +u32 UDPWiimote::getButtons() +{ + u32 msk; + d->mutex.Enter(); + msk=mask; + d->mutex.Leave(); + return msk; +} + +void UDPWiimote::getIR(float &_x, float &_y) +{ + d->mutex.Enter(); + _x=(float)pointerX; + _y=(float)pointerY; + d->mutex.Leave(); +} + +void UDPWiimote::getNunchuck(float &_x, float &_y, u8 &_mask) +{ + d->mutex.Enter(); + _x=(float)nunX; + _y=(float)nunY; + _mask=nunMask; + d->mutex.Leave(); +} + +const char * UDPWiimote::getPort() +{ + return port.c_str(); +} \ No newline at end of file diff --git a/Source/Core/InputCommon/Src/UDPWiimote.h b/Source/Core/InputCommon/Src/UDPWiimote.h new file mode 100644 index 0000000000..bd8478da49 --- /dev/null +++ b/Source/Core/InputCommon/Src/UDPWiimote.h @@ -0,0 +1,50 @@ +#ifndef UDPWIIMOTE_H +#define UDPWIIMOTE_H + +#include "Common.h" +#include + +#define UDPWM_B1 (1<<0) +#define UDPWM_B2 (1<<1) +#define UDPWM_BA (1<<2) +#define UDPWM_BB (1<<3) +#define UDPWM_BP (1<<4) +#define UDPWM_BM (1<<5) +#define UDPWM_BH (1<<6) +#define UDPWM_BU (1<<7) +#define UDPWM_BD (1<<8) +#define UDPWM_BL (1<<9) +#define UDPWM_BR (1<<10) +#define UDPWM_SK (1<<11) +#define UDPWM_NC (1<<0) +#define UDPWM_NZ (1<<1) + +class UDPWiimote +{ +public: + UDPWiimote(const char * port); + virtual ~UDPWiimote(); + void getAccel(float &x, float &y, float &z); + u32 getButtons(); + void getNunchuck(float &x, float &y, u8 &mask); + void getIR(float &x, float &y); + int getErrNo() {return err;}; + const char * getPort(); +private: + std::string port; + int pharsePacket(u8 * data, size_t size); + void mainThread(); + struct _d; //using pimpl because Winsock2.h doesen't have include guards -_- + _d *d; + double x,y,z; + double nunX,nunY; + double pointerX,pointerY; + u8 nunMask; + u32 mask; + int err; + static int noinst; + friend void _UDPWiiThread(void* arg); + void broadcastPresence(); + u8 time; +}; +#endif diff --git a/Source/Core/InputCommon/Src/UDPWrapper.cpp b/Source/Core/InputCommon/Src/UDPWrapper.cpp new file mode 100644 index 0000000000..76527ee601 --- /dev/null +++ b/Source/Core/InputCommon/Src/UDPWrapper.cpp @@ -0,0 +1,191 @@ +#include "UDPWrapper.h" +#include "UDPWiimote.h" +#include +#include +#include + + +#if defined(HAVE_WX) && HAVE_WX +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +UDPWrapper::UDPWrapper(int indx, const char* const _name) : +updIR(false),updAccel(false), +updButt(false),udpEn(false),inst(NULL), +index(indx),ControllerEmu::ControlGroup(_name,GROUP_TYPE_UDPWII) +{ + char s[5]; + sprintf(s,"%d",4432+index); + port=s; + //PanicAlert("UDPWrapper #%d ctor",index); +} + + +void UDPWrapper::LoadConfig(IniFile::Section *sec, const std::string& defdev, const std::string& base ) +{ + ControlGroup::LoadConfig(sec,defdev,base); + + std::string group( base + name ); group += "/"; + + int _updAccel,_updIR,_updButt,_udpEn; + sec->Get((group + "Enable").c_str(),&_udpEn,0); + + char s[5]; + sprintf(s,"%d",4432+index); + sec->Get((group + "Port").c_str(),&port,s); + + sec->Get((group + "Update_Accel").c_str(),&_updAccel,1); + sec->Get((group + "Update_IR").c_str(),&_updIR,1); + sec->Get((group + "Update_Butt").c_str(),&_updButt,1); + + udpEn=(_udpEn>0); + updAccel=(_updAccel>0); + updIR=(_updIR>0); + updButt=(_updButt>0); + + Refresh(); +} + + +void UDPWrapper::SaveConfig(IniFile::Section *sec, const std::string& defdev, const std::string& base ) +{ + ControlGroup::SaveConfig(sec,defdev,base); + std::string group( base + name ); group += "/"; + sec->Set((group + "Enable").c_str(),(int)udpEn); + sec->Set((group + "Port").c_str(),port.c_str()); + sec->Set((group + "Update_Accel").c_str(),(int)updAccel); + sec->Set((group + "Update_IR").c_str(),(int)updIR); + sec->Set((group + "Update_Butt").c_str(),(int)updButt); +} + + +void UDPWrapper::Refresh() +{ + bool udpAEn=(inst!=NULL); + if (udpEn&&udpAEn) + { + if (strcmp(inst->getPort(),port.c_str())) + { + delete inst; + inst= new UDPWiimote(port.c_str()); + } + return; + } + if (!udpEn) + { + if (inst) + delete inst; + inst=NULL; + return; + } + //else + inst= new UDPWiimote(port.c_str()); +} + +UDPWrapper::~UDPWrapper() +{ + if (inst) + delete inst; +} + +#if defined(HAVE_WX) && HAVE_WX + +#define _connect_macro_(b, f, c, s) (b)->Connect(wxID_ANY, (c), wxCommandEventHandler( f ), (wxObject*)0, (wxEvtHandler*)s) + +class UDPConfigDiag : public wxDialog +{ +public: + UDPConfigDiag(wxWindow * const parent, UDPWrapper * _wrp); + UDPWrapper * wrp; + void ChangeUpdateFlags(wxCommandEvent & event); + void ChangeState(wxCommandEvent & event); + void OKPressed(wxCommandEvent & event); + wxCheckBox * enable; + wxCheckBox * butt; + wxCheckBox * accel; + wxCheckBox * point; + wxTextCtrl * port_tbox; +}; + +UDPConfigDiag::UDPConfigDiag(wxWindow * const parent, UDPWrapper * _wrp) : + wxDialog(parent, -1, wxT("UDP Wiimote"), wxDefaultPosition, wxDefaultSize), + wrp(_wrp) +{ + wxBoxSizer * outer_sizer = new wxBoxSizer(wxVERTICAL); + wxBoxSizer * sizer1 = new wxBoxSizer(wxVERTICAL); + wxBoxSizer * sizer2 = new wxBoxSizer(wxVERTICAL); + outer_sizer ->Add(sizer1, 0, wxUP | wxLEFT| wxRIGHT | wxEXPAND, 10); + outer_sizer ->Add(sizer2, 1, wxALL | wxEXPAND, 10); + + enable = new wxCheckBox(this,wxID_ANY,wxT("Enable")); + butt = new wxCheckBox(this,wxID_ANY,wxT("Update Buttons")); + accel = new wxCheckBox(this,wxID_ANY,wxT("Update Acceleration")); + point = new wxCheckBox(this,wxID_ANY,wxT("Update IR Pointer")); + + wxButton * ok_butt = new wxButton(this,wxID_ANY,wxT("OK")); + + wxBoxSizer * port_sizer = new wxBoxSizer(wxHORIZONTAL); + port_sizer->Add(new wxStaticText(this , wxID_ANY, wxT("UDP Port:")),0,wxALIGN_CENTER); + port_tbox = new wxTextCtrl(this,wxID_ANY,wxString::FromUTF8(wrp->port.c_str())); + port_sizer->Add(port_tbox,1, wxLEFT | wxEXPAND , 5); + + _connect_macro_(enable,UDPConfigDiag::ChangeState ,wxEVT_COMMAND_CHECKBOX_CLICKED, this); + _connect_macro_(butt,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this); + _connect_macro_(accel,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this); + _connect_macro_(point,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this); + _connect_macro_(ok_butt,UDPConfigDiag::OKPressed, wxEVT_COMMAND_BUTTON_CLICKED, this); + _connect_macro_(port_tbox, UDPConfigDiag::ChangeState, wxEVT_COMMAND_TEXT_UPDATED, this); + + enable->SetValue(wrp->udpEn); + butt->SetValue(wrp->updButt); + accel->SetValue(wrp->updAccel); + point->SetValue(wrp->updIR); + + sizer1->Add(enable,1,wxALL | wxEXPAND,5); + sizer1->Add(port_sizer, 1,wxDOWN | wxLEFT| wxRIGHT | wxEXPAND,5); + + sizer2->Add(butt,1,wxALL | wxEXPAND,5); + sizer2->Add(accel,1,wxALL | wxEXPAND,5); + sizer2->Add(point,1,wxALL | wxEXPAND,5); + + outer_sizer->Add(ok_butt,0, wxDOWN | wxLEFT| wxRIGHT | wxALIGN_RIGHT,10); + + SetSizerAndFit(outer_sizer); + Layout(); +} + +void UDPConfigDiag::ChangeUpdateFlags(wxCommandEvent & event) +{ + wrp->updAccel=accel->GetValue(); + wrp->updButt=butt->GetValue(); + wrp->updIR=point->GetValue(); +} + +void UDPConfigDiag::ChangeState(wxCommandEvent & event) +{ + wrp->udpEn=enable->GetValue(); + wrp->port=port_tbox->GetValue().mb_str(wxConvUTF8); + wrp->Refresh(); +} + +void UDPConfigDiag::OKPressed(wxCommandEvent & event) +{ + Close(); +} + +void UDPWrapper::Configure(wxWindow * parent) +{ + wxDialog * diag = new UDPConfigDiag(parent,this); + diag->ShowModal(); + diag->Destroy(); +} +#endif \ No newline at end of file diff --git a/Source/Core/InputCommon/Src/UDPWrapper.h b/Source/Core/InputCommon/Src/UDPWrapper.h new file mode 100644 index 0000000000..b637f97edd --- /dev/null +++ b/Source/Core/InputCommon/Src/UDPWrapper.h @@ -0,0 +1,34 @@ +#ifndef UDPWRAPPER_H +#define UDPWRAPPER_H + +#include +#include "ControllerEmu.h" +#include +#include + +#if defined(HAVE_WX) && HAVE_WX +class wxWindow; +#endif + +class UDPWiimote; +class Wiimote; + +class UDPWrapper : public ControllerEmu::ControlGroup +{ +public: + UDPWiimote * inst; + int index; + bool updIR,updAccel,updButt,udpEn; //upd from update and udp from... well... UDP + std::string port; + + UDPWrapper(int index, const char* const _name); + virtual void LoadConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" ); + virtual void SaveConfig(IniFile::Section *sec, const std::string& defdev = "", const std::string& base = "" ); + void Refresh(); +#if defined(HAVE_WX) && HAVE_WX + void Configure(wxWindow * parent); +#endif + virtual ~UDPWrapper(); +}; + +#endif diff --git a/Source/Core/InputUICommon/Src/ConfigDiag.cpp b/Source/Core/InputUICommon/Src/ConfigDiag.cpp index 7d0446e9e7..d73f1ac9a0 100644 --- a/Source/Core/InputUICommon/Src/ConfigDiag.cpp +++ b/Source/Core/InputUICommon/Src/ConfigDiag.cpp @@ -16,12 +16,18 @@ // http://code.google.com/p/dolphin-emu/ #include "ConfigDiag.h" +#include "UDPWrapper.h" #define _connect_macro_(b, f, c, s) (b)->Connect(wxID_ANY, (c), wxCommandEventHandler( f ), (wxObject*)0, (wxEvtHandler*)s) #define WXSTR_FROM_STR(s) (wxString::From8BitData((s).c_str())) // ToAscii was causing probs with some extended ascii characters, To8BitData seems to work #define STR_FROM_WXSTR(w) (std::string((w).To8BitData())) +void GamepadPage::ConfigUDPWii(wxCommandEvent &event) +{ + UDPWrapper* const wrp = ((UDPConfigButton*)event.GetEventObject())->wrapper; + wrp->Configure(this); +} void GamepadPage::ConfigExtension( wxCommandEvent& event ) { @@ -796,6 +802,13 @@ ControlGroupBox::ControlGroupBox( ControllerEmu::ControlGroup* const group, wxWi Add(configure_btn, 0, wxALL|wxEXPAND, 3 ); } break; + case GROUP_TYPE_UDPWII: + { + wxButton* const btn = new UDPConfigButton ( parent, (UDPWrapper*)group ); + _connect_macro_(btn, GamepadPage::ConfigUDPWii, wxEVT_COMMAND_BUTTON_CLICKED, eventsink); + Add(btn, 0, wxALL|wxEXPAND, 3); + } + break; default : { //options diff --git a/Source/Core/InputUICommon/Src/ConfigDiag.h b/Source/Core/InputUICommon/Src/ConfigDiag.h index a5d4de3db6..bf4b83f4e7 100644 --- a/Source/Core/InputUICommon/Src/ConfigDiag.h +++ b/Source/Core/InputUICommon/Src/ConfigDiag.h @@ -44,6 +44,8 @@ #include "InputConfig.h" #include "FileSearch.h" +class UDPWrapper; + class PadSetting { protected: @@ -145,6 +147,16 @@ public: ControllerInterface::ControlReference* const control_reference; }; + +class UDPConfigButton : public wxButton +{ +public: + UDPWrapper * wrapper; + UDPConfigButton( wxWindow* const parent, UDPWrapper * udp) : wrapper(udp), + wxButton( parent, -1, wxT("Configure"), wxDefaultPosition ) + {} +}; + class ControlGroupBox : public wxStaticBoxSizer { public: @@ -189,6 +201,8 @@ public: void ConfigExtension( wxCommandEvent& event ); + void ConfigUDPWii( wxCommandEvent& event ); + void SetDevice( wxCommandEvent& event ); void ClearAll( wxCommandEvent& event ); diff --git a/Source/Plugins/Plugin_Wiimote/Src/EmuDynamics.cpp b/Source/Plugins/Plugin_Wiimote/Src/EmuDynamics.cpp index e365123bfe..d2d7bd3828 100644 --- a/Source/Plugins/Plugin_Wiimote/Src/EmuDynamics.cpp +++ b/Source/Plugins/Plugin_Wiimote/Src/EmuDynamics.cpp @@ -257,7 +257,16 @@ void TiltWiimote(int &_x, int &_y, int &_z) { // Select input method and return the x, y, x values if ((WiiMapping[g_ID].UDPWM.instance)&&(WiiMapping[g_ID].UDPWM.enableAccel)) - WiiMapping[g_ID].UDPWM.instance->getAccel(_x,_y,_z); + { + float x,y,z; + WiiMapping[g_ID].UDPWM.instance->getAccel(x,y,z); + float xg = WiiMoteEmu::g_wm.cal_g.x; + float yg = WiiMoteEmu::g_wm.cal_g.y; + float zg = WiiMoteEmu::g_wm.cal_g.z; + _x = WiiMoteEmu::g_wm.cal_zero.x + (int)(xg * x); + _y = WiiMoteEmu::g_wm.cal_zero.y + (int)(yg * y); + _z = WiiMoteEmu::g_wm.cal_zero.z + (int)(zg * z); + } else { if (WiiMapping[g_ID].Tilt.InputWM == FROM_KEYBOARD) diff --git a/Source/Plugins/Plugin_Wiimote/Src/UDPWiimote.cpp b/Source/Plugins/Plugin_Wiimote/Src/UDPWiimote.cpp index 54b0cb055d..30da54859d 100644 --- a/Source/Plugins/Plugin_Wiimote/Src/UDPWiimote.cpp +++ b/Source/Plugins/Plugin_Wiimote/Src/UDPWiimote.cpp @@ -260,15 +260,12 @@ void UDPWiimote::broadcastPresence() // NOTICE_LOG(WIIMOTE,"UDPWii broadcasting presence"); } -void UDPWiimote::getAccel(int &_x, int &_y, int &_z) +void UDPWiimote::getAccel(float &_x, float &_y, float &_z) { - float xg = WiiMoteEmu::g_wm.cal_g.x; - float yg = WiiMoteEmu::g_wm.cal_g.y; - float zg = WiiMoteEmu::g_wm.cal_g.z; d->mutex.Enter(); - _x = WiiMoteEmu::g_wm.cal_zero.x + (int)(xg * x); - _y = WiiMoteEmu::g_wm.cal_zero.y + (int)(yg * y); - _z = WiiMoteEmu::g_wm.cal_zero.z + (int)(zg * z); + _x=x; + _y=y; + _z=z; d->mutex.Leave(); //NOTICE_LOG(WIIMOTE,"%lf %lf %lf",_x, _y, _z); } diff --git a/Source/Plugins/Plugin_Wiimote/Src/UDPWiimote.h b/Source/Plugins/Plugin_Wiimote/Src/UDPWiimote.h index 8e4e2d28fc..4609ccfa4c 100644 --- a/Source/Plugins/Plugin_Wiimote/Src/UDPWiimote.h +++ b/Source/Plugins/Plugin_Wiimote/Src/UDPWiimote.h @@ -23,7 +23,7 @@ class UDPWiimote public: UDPWiimote(const char * port); virtual ~UDPWiimote(); - void getAccel(int &x, int &y, int &z); + void getAccel(float &x, float &y, float &z); u32 getButtons(); void getNunchuck(float &x, float &y, u8 &mask); void getIR(float &x, float &y); diff --git a/Source/Plugins/Plugin_WiimoteNew/Plugin_WiimoteNew.vcproj b/Source/Plugins/Plugin_WiimoteNew/Plugin_WiimoteNew.vcproj index fdb3f8e401..01c0ade09d 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Plugin_WiimoteNew.vcproj +++ b/Source/Plugins/Plugin_WiimoteNew/Plugin_WiimoteNew.vcproj @@ -1,7 +1,7 @@ + + diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/UDPTLayer.h b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/UDPTLayer.h new file mode 100644 index 0000000000..7754240a95 --- /dev/null +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/UDPTLayer.h @@ -0,0 +1,53 @@ +//UDP Wiimote Translation Layer + +#ifndef UDPTLAYER_H +#define UDPTLAYER_H + +#include "UDPWiimote.h" + +namespace UDPTLayer +{ + void GetButtons(UDPWrapper * m , wm_core * butt) + { + if (!(m->inst)) return; + if (!(m->updButt)) return; + u32 mask=m->inst->getButtons(); + *butt|=(mask&UDPWM_BA)?WIIMOTE_A:0; + *butt|=(mask&UDPWM_BB)?WIIMOTE_B:0; + *butt|=(mask&UDPWM_B1)?WIIMOTE_ONE:0; + *butt|=(mask&UDPWM_B2)?WIIMOTE_TWO:0; + *butt|=(mask&UDPWM_BP)?WIIMOTE_PLUS:0; + *butt|=(mask&UDPWM_BM)?WIIMOTE_MINUS:0; + *butt|=(mask&UDPWM_BH)?WIIMOTE_HOME:0; + *butt|=(mask&UDPWM_BU)?WIIMOTE_PAD_UP:0; + *butt|=(mask&UDPWM_BD)?WIIMOTE_PAD_DOWN:0; + *butt|=(mask&UDPWM_BL)?WIIMOTE_PAD_LEFT:0; + *butt|=(mask&UDPWM_BR)?WIIMOTE_PAD_RIGHT:0; + } + + void GetAcceleration(UDPWrapper * m , wm_accel * data, accel_cal * calib) + { + if (!(m->inst)) return; + if (!(m->updAccel)) return; + float x,y,z; + m->inst->getAccel(x,y,z); + data->x=u8(x*(calib->one_g.x-calib->zero_g.x)+calib->zero_g.x); + data->y=u8(y*(calib->one_g.y-calib->zero_g.y)+calib->zero_g.y); + data->z=u8(z*(calib->one_g.z-calib->zero_g.z)+calib->zero_g.z); + } + + void GetIR( UDPWrapper * m, float * x, float * y, float * z) + { + if (!(m->inst)) return; + if (!(m->updIR)) return; + if ((*x>-1)&&(*x<1)&&(*y>-1)&&(*y<1)) return; //the recieved values are used ONLY when the normal pointer is offscreen + float _x,_y; + m->inst->getIR(_x,_y); + *x=_x*2-1; + *y=-(_y*2-1); + *z=0; + } + +} + +#endif \ No newline at end of file diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.cpp b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.cpp index 518dd2d8d8..24da0a828f 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.cpp +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.cpp @@ -25,6 +25,8 @@ #define WIIMOTE_MINUS 0x1000 #define WIIMOTE_HOME 0x8000 +#include "UDPTLayer.h" //this must be included after the buttons + namespace WiimoteEmu { @@ -274,6 +276,9 @@ Wiimote::Wiimote( const unsigned int index ) // swing //groups.push_back( m_swing = new Force( "Swing" ) ); + //udp + groups.push_back( m_udp = new UDPWrapper( m_index , "UDP Wiimote" ) ); + // shake groups.push_back( m_shake = new Buttons( "Shake" ) ); m_shake->controls.push_back( new ControlGroup::Input( "X" ) ); @@ -388,7 +393,7 @@ void Wiimote::Update() m_buttons->GetState( &m_status.buttons, button_bitmasks ); m_dpad->GetState( &m_status.buttons, is_sideways ? dpad_sideways_bitmasks : dpad_bitmasks ); } - + UDPTLayer::GetButtons( m_udp, &m_status.buttons ); // check if there is a read data request if (m_read_requests.size()) { @@ -481,7 +486,7 @@ void Wiimote::Update() // ----SHAKE---- if (is_focus) EmulateShake(data + rpt.accel, m_shake, m_shake_step); - + UDPTLayer::GetAcceleration( m_udp, (wm_accel*)&data[rpt.accel], (accel_cal*)&m_eeprom[0x16]); } // ----ir---- @@ -491,6 +496,7 @@ void Wiimote::Update() if (is_focus) m_ir->GetState(&xx, &yy, &zz, true); + UDPTLayer::GetIR( m_udp, &xx, &yy, &zz); xx *= (-256 * 0.95f); xx += 512; @@ -779,3 +785,4 @@ void Wiimote::Register::Write( size_t address, void* src, size_t length ) } + diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.h b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.h index 740d6d60dc..b79ecc62e0 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.h +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.h @@ -13,6 +13,7 @@ #include "WiimoteHid.h" #include "Encryption.h" +#include "UDPWrapper.h" #include #include @@ -86,6 +87,9 @@ private: Extension* m_extension; ControlGroup* m_options; + //UDPWiimote + UDPWrapper* m_udp; + // wiimote index, 0-3 const unsigned int m_index;