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