Updated German translation
[dasher.git] / Src / DasherCore / SocketInputBase.h
blobc6dfb6d28411ca00a881c9b926c069934f299c83
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 "DasherInput.h"
11 #include "SettingsStore.h"
12 #include "Messages.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;
22 using namespace std;
23 /// \ingroup Input
24 /// \{
25 class CSocketInputBase : public CScreenCoordInput, public CSettingsUserObserver {
27 public:
29 CSocketInputBase(CSettingsUser *pCreator, CMessageDisplay *pMsgs);
31 virtual ~CSocketInputBase();
33 virtual void HandleEvent(int iParameter);
35 virtual void SetDebug(bool _debug);
37 virtual bool StartListening();
39 virtual void StopListening();
41 virtual bool isListening() {
42 return readerRunning;
45 virtual void SetReaderPort(int port);
47 virtual int GetPort() {
48 return port;
51 void SetCoordinateCount(int _coordinateCount) {
52 DASHER_ASSERT(_coordinateCount <= DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT);
53 coordinateCount = _coordinateCount;
56 /// Gets the last coordinates received; if only one coordinate is being read, this is put
57 /// into iDasherY (and iDasherX set to 0).
58 bool GetScreenCoords(screenint &iScreenX, screenint &iScreenY, CDasherView *pView) {
60 //update max values for reader thread...(note any changes here won't be incorporated
61 // until values are next received over socket, but never mind)
62 dasherMaxCoordinateValues[0] = pView->Screen()->GetWidth();
63 dasherMaxCoordinateValues[1] = pView->Screen()->GetHeight();
65 if (coordinateCount==1) {
66 iScreenX = 0;
67 iScreenY = dasherCoordinates[0];
68 } else if (coordinateCount==2) {
69 iScreenX = dasherCoordinates[0];
70 iScreenY = dasherCoordinates[1];
71 } else {
72 //Aiieee, we're receiving >2 coords? Don't know what to do...
73 return false;
75 return true;
78 void Activate() {
79 StartListening();
82 void Deactivate() {
83 StopListening();
86 // Defines the label used in the input stream for a particular coordinate.
87 // We make our own copy of the label, in our own buffer. This should ensure thread-safety.
88 // Even if this method is called while our other thread is doing a strcmp on the label,
89 // the buffer will always be null-terminated somewhere (even if the last byte of the buffer, which is
90 // never overwritten), so won't segfault.
91 virtual void SetCoordinateLabel(int iWhichCoordinate, const char *Label);
93 virtual void SetRawRange(int iWhich, double dMin, double dMax);
95 bool GetSettings(SModuleSettings **pSettings, int *iCount);
97 protected:
99 myint dasherCoordinates[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT];
100 myint dasherMaxCoordinateValues[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT];
101 double rawMinValues[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT];
102 double rawMaxValues[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT];
103 int coordinateCount;
104 char coordinateNames[DASHER_SOCKET_INPUT_MAX_COORDINATE_COUNT][DASHER_SOCKET_INPUT_MAX_COORDINATE_LABEL_LENGTH + 1];
106 int port;
107 bool debug_socket_input;
109 int sock;
111 char buffer[4096];
113 bool readerRunning;
115 virtual bool LaunchReaderThread() =0;
117 virtual void CancelReaderThread() =0;
119 virtual void ReadForever();
121 virtual void ParseMessage(char *message);
123 //Reports an error by appending an error message obtained from strerror(errno) onto the provided prefix
124 void ReportErrnoError(const std::string &prefix);
126 virtual void SocketDebugMsg(const char *pszFormat, ...);
128 CMessageDisplay *const m_pMsgs;
132 /// \}
133 #endif