1 #include "UDPWiimote.h"
9 #define ERRNO WSAGetLastError()
10 #define EWOULDBLOCK WSAEWOULDBLOCK
11 #define BAD_SOCK INVALID_SOCKET
12 #define close(x) closesocket(x)
13 #define cleanup do {noinst--; if (noinst==0) WSACleanup();} while (0)
14 #define blockingoff(sock) ioctlsocket(sock, FIONBIO, &iMode)
17 #pragma comment (lib, "Ws2_32.lib")
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
34 #define cleanup noinst--
35 #define blockingoff(sock) fcntl(sock, F_SETFL, O_NONBLOCK)
50 int UDPWiimote::noinst
=0;
52 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)
57 struct addrinfo hints
, *servinfo
, *p
;
65 sockVersion
= MAKEWORD(2, 2);
66 WSAStartup(sockVersion
, &wsaData
);
72 memset(&hints
, 0, sizeof hints
);
73 hints
.ai_family
= AF_INET
;
74 hints
.ai_socktype
= SOCK_DGRAM
;
75 hints
.ai_flags
= AI_PASSIVE
; // use my IP
77 if ((rv
= getaddrinfo(NULL
, port
, &hints
, &servinfo
)) != 0) {
78 // fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
84 // loop through all the results and bind to the first we can
85 for(p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
86 if (((d
->sockfd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
)),d
->sockfd
) == BAD_SOCK
) {
90 if (bind(d
->sockfd
, p
->ai_addr
, p
->ai_addrlen
) == -1) {
104 freeaddrinfo(servinfo
);
105 blockingoff(d
->sockfd
);
110 UDPWiimote::~UDPWiimote()
117 int UDPWiimote::readPack(void * data
, int *size
)
121 struct sockaddr_storage their_addr
;
122 addr_len
= sizeof their_addr
;
123 if ((numbytes
= recvfrom(d
->sockfd
, (dataz
)data
, (*size
) , 0,
124 (struct sockaddr
*)&their_addr
, (socklen_t
*)&addr_len
)) == -1) {
125 if (ERRNO
==EWOULDBLOCK
)
133 #define ACCEL_FLAG (1<<0)
134 #define BUTT_FLAG (1<<1)
135 #define IR_FLAG (1<<2)
136 #define NUN_FLAG (1<<3)
138 void UDPWiimote::update()
145 for (int i
=0; (res
=readPack(&bf
,&size
)),(i
<100)&&(res
!=-1); (res
<-1)?i
++:0)
153 if (bf
[1]>=time
) //packet timestamp. assures order is maintained
157 u32
*p
=(u32
*)(&bf
[3]);
158 if (bf
[2]&ACCEL_FLAG
)
161 ux
=(double)((s32
)ntohl(*p
)); p
++;
162 uy
=(double)((s32
)ntohl(*p
)); p
++;
163 uz
=(double)((s32
)ntohl(*p
)); p
++;
164 x
=ux
/1048576; //packet accel data
174 pointerX
=((double)((s32
)ntohl(*p
)))/1048576; p
++;
175 pointerY
=((double)((s32
)ntohl(*p
)))/1048576; p
++;
179 nunMask
=*((u8
*)p
); p
=(u32
*)(((u8
*)p
)+1);
180 nunX
=((double)((s32
)ntohl(*p
)))/1048576; p
++;
181 nunY
=((double)((s32
)ntohl(*p
)))/1048576; p
++;
188 ERROR_LOG(WIIMOTE
,"UDPWii Packet error");
191 //NOTICE_LOG(WIIMOTE,"UDPWii update result:np:%d x:%f y:%f z:%f nx:%f ny:%f px:%f py:%f bmask:%x nmask:%x",
192 // nopack, x, y, z, nunX, nunY, pointerX, pointerY, mask, nunMask);
195 void UDPWiimote::getAccel(int &_x
, int &_y
, int &_z
)
197 //NOTICE_LOG(WIIMOTE,"%lf %lf %lf",_x, _y, _z);
198 float xg
= WiiMoteEmu::g_wm
.cal_g
.x
;
199 float yg
= WiiMoteEmu::g_wm
.cal_g
.y
;
200 float zg
= WiiMoteEmu::g_wm
.cal_g
.z
;
201 _x
= WiiMoteEmu::g_wm
.cal_zero
.x
+ (int)(xg
* x
);
202 _y
= WiiMoteEmu::g_wm
.cal_zero
.y
+ (int)(yg
* y
);
203 _z
= WiiMoteEmu::g_wm
.cal_zero
.z
+ (int)(zg
* z
);
206 u32
UDPWiimote::getButtons()
211 void UDPWiimote::getIR(float &_x
, float &_y
)
217 void UDPWiimote::getNunchuck(float &_x
, float &_y
, u8
&_mask
)