1 #include "Common\WinCommon.h"
3 #include "BTSocketInput.h"
9 using namespace Dasher
;
11 // Track memory leaks on Windows to the line that new'd the memory
14 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
17 static char THIS_FILE
[] = __FILE__
;
21 // Configuration optiond here:
22 static const char *szPort
= "9999";
23 static const char *szHostname
= "localhost";
25 static const int xmin
= -32;
26 static const int xmax
= 32;
28 static const int ymin
= 32;
29 static const int ymax
= -32;
31 // TODO: This doesn't do any error handling at all
32 // TODO: Probably incompatable with the socket server module
34 CBTSocketInput::CBTSocketInput(CEventHandler
* pEventHandler
, CSettingsStore
* pSettingsStore
)
35 : CDasherInput(pEventHandler
, pSettingsStore
, 100, 0, "BT Tilt Socket"){
38 CBTSocketInput::~CBTSocketInput(void) {
41 int CBTSocketInput::GetCoordinates(int iN
, myint
*pCoordinates
) {
42 // Send the magic command...
43 const char *szCommand
= "<message command=\"orientation\"></message>\r\n";
44 int iRetVal
= send(m_oSocket
, szCommand
, strlen(szCommand
), 0);
46 // And wait for the reply...
48 int iBytes
= recv(m_oSocket
, szBuffer
, 2047, 0);
51 char *szXStart
= strstr(szBuffer
, "x=\"") + 3;
52 char *szXEnd
= strchr(szXStart
, '\"');
54 char *szYStart
= strstr(szBuffer
, "y=\"") + 3;
55 char *szYEnd
= strchr(szXStart
, '\"');
60 int xrange
= xmax
- xmin
;
61 int yrange
= ymax
- ymin
;
63 pCoordinates
[0] = (atoi(szXStart
) - xmin
) * 4096 / xrange
;
64 pCoordinates
[1] = (atoi(szYStart
) - ymin
) * 4096 / yrange
;
69 void CBTSocketInput::Activate() {
72 WSAStartup(MAKEWORD(2,2), &wsaData
);
74 struct addrinfo sHints
;
75 ZeroMemory(&sHints
, sizeof(sHints
));
76 sHints
.ai_family
= AF_UNSPEC
;
77 sHints
.ai_socktype
= SOCK_STREAM
;
78 sHints
.ai_protocol
= IPPROTO_TCP
;
80 struct addrinfo
*sAddr
;
81 getaddrinfo(szHostname
, szPort
, &sHints
, &sAddr
);
83 m_oSocket
= socket(sAddr
->ai_family
, sAddr
->ai_socktype
, sAddr
->ai_protocol
);
84 connect(m_oSocket
, sAddr
->ai_addr
, (int)sAddr
->ai_addrlen
);
89 void CBTSocketInput::Deactivate() {
90 shutdown(m_oSocket
, SD_BOTH
);
91 closesocket(m_oSocket
);