Merge pull request #4857 from t-nelson/Gotham_13.2_backports
[vuplus_xbmc] / xbmc / network / EventClient.h
1 #ifndef __EVENT_CLIENT_H__
2 #define __EVENT_CLIENT_H__
3
4 /*
5  *      Copyright (C) 2005-2013 Team XBMC
6  *      http://xbmc.org
7  *
8  *  This Program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2, or (at your option)
11  *  any later version.
12  *
13  *  This Program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with XBMC; see the file COPYING.  If not, see
20  *  <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "threads/Thread.h"
25 #include "threads/CriticalSection.h"
26 #include "Socket.h"
27 #include "EventPacket.h"
28 #include "settings/Settings.h"
29
30 #include <list>
31 #include <map>
32 #include <queue>
33
34 namespace EVENTCLIENT
35 {
36
37   #define ES_FLAG_UNICODE    0x80000000 // new 16bit key flag to support real unicode over EventServer
38
39   class CEventAction
40   {
41   public:
42     CEventAction()
43     {
44       actionType = 0;
45     }
46     CEventAction(const char* action, unsigned char type)
47     {
48       actionName = action;
49       actionType = type;
50     }
51
52     std::string    actionName;
53     unsigned char  actionType;
54   };
55
56   class CEventButtonState
57   {
58   public:
59     CEventButtonState()
60     {
61       m_iKeyCode   = 0;
62       m_mapName    = "";
63       m_buttonName = "";
64       m_fAmount    = 0.0f;
65       m_bUseAmount = false;
66       m_bRepeat    = false;
67       m_bActive    = false;
68       m_bAxis      = false;
69       m_iControllerNumber = 0;
70       m_iNextRepeat = 0;
71     }
72
73     CEventButtonState(unsigned int iKeyCode,
74                       std::string mapName,
75                       std::string buttonName,
76                       float fAmount,
77                       bool isAxis,
78                       bool bRepeat,
79                       bool bUseAmount
80       )
81     {
82       m_iKeyCode   = iKeyCode;
83       m_buttonName = buttonName;
84       m_mapName    = mapName;
85       m_fAmount    = fAmount;
86       m_bUseAmount = bUseAmount;
87       m_bRepeat    = bRepeat;
88       m_bActive    = true;
89       m_bAxis      = isAxis;
90       m_iControllerNumber = 0;
91       m_iNextRepeat = 0;
92       Load();
93     }
94
95     void Reset()     { m_bActive = false; }
96     void SetActive() { m_bActive = true; }
97     bool Active() const { return m_bActive; }
98     bool Repeat() const { return m_bRepeat; }
99     int  ControllerNumber() const { return m_iControllerNumber; }
100     bool Axis() const { return m_bAxis; }
101     unsigned int KeyCode() const { return m_iKeyCode; }
102     float Amount() const  { return m_fAmount; }
103     void Load();
104     const std::string& JoystickName() const { return m_joystickName; }
105
106     // data
107     unsigned int      m_iKeyCode;
108     unsigned short    m_iControllerNumber;
109     std::string       m_buttonName;
110     std::string       m_mapName;
111     std::string       m_joystickName;
112     float             m_fAmount;
113     bool              m_bUseAmount;
114     bool              m_bRepeat;
115     bool              m_bActive;
116     bool              m_bAxis;
117     unsigned int      m_iNextRepeat;
118   };
119
120
121   /**********************************************************************/
122   /* UDP EventClient Class                                              */
123   /**********************************************************************/
124   // - clients timeout if they don't receive at least 1 ping in 1 minute
125   // - sequence packets timeout after 5 seconds
126   class CEventClient
127   {
128   public:
129     CEventClient()
130     {
131       Initialize();
132     }
133
134     CEventClient(SOCKETS::CAddress& addr)
135     {
136       m_remoteAddr = addr;
137       Initialize();
138     }
139
140     void Initialize()
141     {
142       m_bGreeted = false;
143       m_iMouseX = 0;
144       m_iMouseY = 0;
145       m_iCurrentSeqLen = 0;
146       m_lastPing = 0;
147       m_lastSeq = 0;
148       m_iRemotePort = 0;
149       m_bMouseMoved = false;
150       m_bSequenceError = false;
151       RefreshSettings();
152     }
153
154     const std::string& Name() const
155     {
156       return m_deviceName;
157     }
158
159     void RefreshSettings()
160     {
161       m_iRepeatDelay = CSettings::Get().GetInt("services.esinitialdelay");
162       m_iRepeatSpeed = CSettings::Get().GetInt("services.escontinuousdelay");
163     }
164
165     SOCKETS::CAddress& Address()
166     {
167       return m_remoteAddr;
168     }
169
170     virtual ~CEventClient()
171     {
172       FreePacketQueues();
173     }
174
175     // add packet to queue
176     bool AddPacket(EVENTPACKET::CEventPacket *packet);
177
178     // return true if client received ping with the last 1 minute
179     bool Alive() const;
180
181     // process the packet queue
182     bool ProcessQueue();
183
184     // process the queued up events (packets)
185     void ProcessEvents();
186
187     // gets the next action in the action queue
188     bool GetNextAction(CEventAction& action);
189
190     // deallocate all packets in the queues
191     void FreePacketQueues();
192
193     // return event states
194     unsigned int GetButtonCode(std::string& strMapName, bool& isAxis, float& amount);
195
196     // update mouse position
197     bool GetMousePos(float& x, float& y);
198
199   protected:
200     bool ProcessPacket(EVENTPACKET::CEventPacket *packet);
201
202     // packet handlers
203     virtual bool OnPacketHELO(EVENTPACKET::CEventPacket *packet);
204     virtual bool OnPacketBYE(EVENTPACKET::CEventPacket *packet);
205     virtual bool OnPacketBUTTON(EVENTPACKET::CEventPacket *packet);
206     virtual bool OnPacketMOUSE(EVENTPACKET::CEventPacket *packet);
207     virtual bool OnPacketNOTIFICATION(EVENTPACKET::CEventPacket *packet);
208     virtual bool OnPacketLOG(EVENTPACKET::CEventPacket *packet);
209     virtual bool OnPacketACTION(EVENTPACKET::CEventPacket *packet);
210     bool CheckButtonRepeat(unsigned int &next);
211
212     // returns true if the client has received the HELO packet
213     bool Greeted() { return m_bGreeted; }
214
215     // reset the timeout counter
216     void ResetTimeout()
217     {
218       m_lastPing = time(NULL);
219     }
220
221     // helper functions
222
223     // Parses a null terminated string from payload.
224     // After parsing successfully:
225     //   1. payload is incremented to end of string
226     //   2. psize is decremented by length of string
227     //   3. parsedVal contains the parsed string
228     //   4. true is returned
229     bool ParseString(unsigned char* &payload, int &psize, std::string& parsedVal);
230
231     // Parses a single byte (same behavior as ParseString)
232     bool ParseByte(unsigned char* &payload, int &psize, unsigned char& parsedVal);
233
234     // Parse a single 32-bit integer (converts from network order to host order)
235     bool ParseUInt32(unsigned char* &payload, int &psize, unsigned int& parsedVal);
236
237     // Parse a single 16-bit integer (converts from network order to host order)
238     bool ParseUInt16(unsigned char* &payload, int &psize, unsigned short& parsedVal);
239
240     std::string       m_deviceName;
241     int               m_iCurrentSeqLen;
242     time_t            m_lastPing;
243     time_t            m_lastSeq;
244     int               m_iRemotePort;
245     bool              m_bGreeted;
246     unsigned int      m_iRepeatDelay;
247     unsigned int      m_iRepeatSpeed;
248     unsigned int      m_iMouseX;
249     unsigned int      m_iMouseY;
250     bool              m_bMouseMoved;
251     bool              m_bSequenceError;
252
253     SOCKETS::CAddress m_remoteAddr;
254
255     EVENTPACKET::LogoType m_eLogoType;
256     CCriticalSection  m_critSection;
257
258     std::map <unsigned int, EVENTPACKET::CEventPacket*>  m_seqPackets;
259     std::queue <EVENTPACKET::CEventPacket*> m_readyPackets;
260
261     // button and mouse state
262     std::list<CEventButtonState>  m_buttonQueue;
263     std::queue<CEventAction>      m_actionQueue;
264     CEventButtonState m_currentButton;
265   };
266
267 } // EVENTCLIENT
268
269 #endif // __EVENT_CLIENT_H__