1 #ifndef __EVENT_CLIENT_H__
2 #define __EVENT_CLIENT_H__
5 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
24 #include "threads/Thread.h"
25 #include "threads/CriticalSection.h"
27 #include "EventPacket.h"
28 #include "settings/Settings.h"
37 #define ES_FLAG_UNICODE 0x80000000 // new 16bit key flag to support real unicode over EventServer
46 CEventAction(const char* action, unsigned char type)
52 std::string actionName;
53 unsigned char actionType;
56 class CEventButtonState
69 m_iControllerNumber = 0;
73 CEventButtonState(unsigned int iKeyCode,
75 std::string buttonName,
82 m_iKeyCode = iKeyCode;
83 m_buttonName = buttonName;
86 m_bUseAmount = bUseAmount;
90 m_iControllerNumber = 0;
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; }
104 const std::string& JoystickName() const { return m_joystickName; }
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;
117 unsigned int m_iNextRepeat;
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
134 CEventClient(SOCKETS::CAddress& addr)
145 m_iCurrentSeqLen = 0;
149 m_bMouseMoved = false;
150 m_bSequenceError = false;
154 const std::string& Name() const
159 void RefreshSettings()
161 m_iRepeatDelay = CSettings::Get().GetInt("services.esinitialdelay");
162 m_iRepeatSpeed = CSettings::Get().GetInt("services.escontinuousdelay");
165 SOCKETS::CAddress& Address()
170 virtual ~CEventClient()
175 // add packet to queue
176 bool AddPacket(EVENTPACKET::CEventPacket *packet);
178 // return true if client received ping with the last 1 minute
181 // process the packet queue
184 // process the queued up events (packets)
185 void ProcessEvents();
187 // gets the next action in the action queue
188 bool GetNextAction(CEventAction& action);
190 // deallocate all packets in the queues
191 void FreePacketQueues();
193 // return event states
194 unsigned int GetButtonCode(std::string& strMapName, bool& isAxis, float& amount);
196 // update mouse position
197 bool GetMousePos(float& x, float& y);
200 bool ProcessPacket(EVENTPACKET::CEventPacket *packet);
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);
212 // returns true if the client has received the HELO packet
213 bool Greeted() { return m_bGreeted; }
215 // reset the timeout counter
218 m_lastPing = time(NULL);
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);
231 // Parses a single byte (same behavior as ParseString)
232 bool ParseByte(unsigned char* &payload, int &psize, unsigned char& parsedVal);
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);
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);
240 std::string m_deviceName;
241 int m_iCurrentSeqLen;
246 unsigned int m_iRepeatDelay;
247 unsigned int m_iRepeatSpeed;
248 unsigned int m_iMouseX;
249 unsigned int m_iMouseY;
251 bool m_bSequenceError;
253 SOCKETS::CAddress m_remoteAddr;
255 EVENTPACKET::LogoType m_eLogoType;
256 CCriticalSection m_critSection;
258 std::map <unsigned int, EVENTPACKET::CEventPacket*> m_seqPackets;
259 std::queue <EVENTPACKET::CEventPacket*> m_readyPackets;
261 // button and mouse state
262 std::list<CEventButtonState> m_buttonQueue;
263 std::queue<CEventAction> m_actionQueue;
264 CEventButtonState m_currentButton;
269 #endif // __EVENT_CLIENT_H__