tagging release
[dasher.git] / trunk / Src / DasherCore / SocketInputBase.h
blobb5bef0bec2df0b278842a1e9b6bb3284844f63e3
1 // SocketInputBase.h
2 //
3 // (C) Copyright Seb Wills 2005
4 //
5 // Abstract base class for socket input: parent of non-abstract classes in each implementation (Windows, Linux, ...),
7 #ifndef __socketinputbase_h__
8 #define __socketinputbase_h__
10 #include "../DasherCore/DasherInput.h"
11 #include "../DasherCore/DasherComponent.h"
12 #include "../DasherCore/EventHandler.h"
14 #include <iostream>
16 #define DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT 2 // just X and Y for now
17 #define DASHER_SOCKET_INPUT_MAX_COORDINATE_LABEL_LENGTH 128
19 namespace Dasher {
20 class CSocketInputBase;
23 using namespace Dasher;
24 using namespace std;
25 /// \ingroup Input
26 /// \{
27 class Dasher::CSocketInputBase : public CDasherInput {
29 public:
31 CSocketInputBase(CEventHandler * pEventHandler, CSettingsStore * pSettingsStore);
33 virtual ~CSocketInputBase();
35 virtual void HandleEvent(Dasher::CEvent * pEvent);
37 virtual void SetDebug(bool _debug);
39 virtual bool StartListening();
41 virtual void StopListening();
43 virtual bool isListening() {
44 return readerRunning;
47 virtual void SetReaderPort(int port);
49 virtual int GetPort() {
50 return port;
53 void SetCoordinateCount(int _coordinateCount) {
54 DASHER_ASSERT(_coordinateCount <= DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT);
55 coordinateCount = _coordinateCount;
58 // Fills pCoordinates with iN coordinate values, return 0 if the
59 // values were in screen coordinates or 1 if the values were in
60 // Dasher coordinates.
62 int GetCoordinates(int iN, myint * pCoordinates) {
64 for(int i = 0; i < iN && i < coordinateCount; i++) {
65 pCoordinates[i] = dasherCoordinates[i];
67 return 1;
70 // Get the number of co-ordinates that this device supplies
72 int GetCoordinateCount() {
73 return coordinateCount;
76 void SetMaxCoordinates(int iN, myint * iDasherMax) {
77 for(int i = 0; i < iN && i < DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT; i++) {
78 dasherMaxCoordinateValues[i] = iDasherMax[i];
82 void Activate() {
83 StartListening();
86 void Deactivate() {
87 StopListening();
90 // Defines the label used in the input stream for a particular coordinate.
91 // We make our own copy of the label, in our own buffer. This should ensure thread-safety.
92 // Even if this method is called while our other thread is doing a strcmp on the label,
93 // the buffer will always be null-terminated somewhere (even if the last byte of the buffer, which is
94 // never overwritten), so won't segfault.
95 virtual void SetCoordinateLabel(int iWhichCoordinate, const char *Label);
97 virtual void SetRawRange(int iWhich, double dMin, double dMax);
99 bool GetSettings(SModuleSettings **pSettings, int *iCount);
101 protected:
103 myint dasherCoordinates[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT];
104 myint dasherMaxCoordinateValues[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT];
105 double rawMinValues[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT];
106 double rawMaxValues[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT];
107 int coordinateCount;
108 char coordinateNames[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT][DASHER_SOCKET_INPUT_MAX_COORDINATE_LABEL_LENGTH + 1];
110 int port;
111 bool debug_socket_input;
113 int sock;
115 char buffer[4096];
117 bool readerRunning;
119 virtual bool LaunchReaderThread() =0;
121 virtual void CancelReaderThread() =0;
123 virtual void ReadForever();
125 virtual void ParseMessage(char *message);
127 virtual void ReportErrnoError(std::string prefix); // override as appropriate for each platform
129 virtual void ReportError(std::string s); // override as appropriate for each platform
131 virtual void SocketDebugMsg(const char *pszFormat, ...);
134 /// \}
135 #endif