Visual studio 2005 thinks that long long int is 64 bit. The difference between
[dasher.git] / Src / Win32 / BTSocketInput.cpp
blob3fa2b2fa7f43cd21c45e962a54cf855921a99cf4
1 #include "Common\WinCommon.h"
3 #include "BTSocketInput.h"
5 #include <cstdlib>
6 #include <cstring>
7 #include <iostream>
9 using namespace Dasher;
11 // Track memory leaks on Windows to the line that new'd the memory
12 #ifdef _WIN32
13 #ifdef _DEBUG
14 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
15 #define new DEBUG_NEW
16 #undef THIS_FILE
17 static char THIS_FILE[] = __FILE__;
18 #endif
19 #endif
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...
47 char szBuffer[2048];
48 int iBytes = recv(m_oSocket, szBuffer, 2047, 0);
49 szBuffer[iBytes] = 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, '\"');
57 *szXEnd = 0;
58 *szYEnd = 0;
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;
66 return 1;
69 void CBTSocketInput::Activate() {
70 // Initialise Winsock
71 WSADATA wsaData;
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);
85 freeaddrinfo(sAddr);
89 void CBTSocketInput::Deactivate() {
90 shutdown(m_oSocket, SD_BOTH);
91 closesocket(m_oSocket);
92 WSACleanup();