[ecd39b]: guilib / common / LIRC.cpp Maximize Restore History

Repo status: analyzing...

Download this file

LIRC.cpp    340 lines (303 with data), 8.8 kB

  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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (C) 2007-2010 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/inotify.h>
#include <limits.h>
#include <unistd.h>
#include "LIRC.h"
#include "ButtonTranslator.h"
#include "log.h"
#include "AdvancedSettings.h"
#include "FileSystem/File.h"
#include "utils/TimeUtils.h"
CRemoteControl g_RemoteControl;
CRemoteControl::CRemoteControl()
{
m_fd = -1;
m_file = NULL;
m_bInitialized = false;
m_skipHold = false;
m_button = 0;
m_isHolding = false;
m_used = true;
m_deviceName = LIRC_DEVICE;
m_inotify_fd = -1;
m_inotify_wd = -1;
m_bLogConnectFailure = true;
m_lastInitAttempt = -5000;
m_initRetryPeriod = 5000;
m_inReply = false;
m_nrSending = 0;
Reset();
}
CRemoteControl::~CRemoteControl()
{
if (m_file != NULL)
fclose(m_file);
}
void CRemoteControl::setUsed(bool value)
{
m_used=value;
if (!value)
CLog::Log(LOGINFO, "LIRC %s: disabled", __FUNCTION__);
else
{
m_lastInitAttempt = -5000;
m_initRetryPeriod = 5000;
}
}
void CRemoteControl::Reset()
{
m_isHolding = false;
m_button = 0;
}
void CRemoteControl::Disconnect()
{
if (!m_used)
return;
if (m_fd != -1)
{
m_bInitialized = false;
if (m_file != NULL)
fclose(m_file);
if (m_fd != -1)
close(m_fd);
m_fd = -1;
m_file = NULL;
if (m_inotify_wd >= 0) {
inotify_rm_watch(m_inotify_fd, m_inotify_wd);
m_inotify_wd = -1;
}
if (m_inotify_fd >= 0)
close(m_inotify_fd);
m_inReply = false;
m_nrSending = 0;
m_sendData.clear();
}
}
void CRemoteControl::setDeviceName(const CStdString& value)
{
if (value.length()>0)
m_deviceName=value;
else
m_deviceName=LIRC_DEVICE;
}
void CRemoteControl::Initialize()
{
struct sockaddr_un addr;
int now = CTimeUtils::GetTimeMS();
if (!m_used || now < m_lastInitAttempt + m_initRetryPeriod)
return;
m_lastInitAttempt = now;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, m_deviceName.c_str());
CLog::Log(LOGINFO, "LIRC %s: using: %s", __FUNCTION__, addr.sun_path);
// Open the socket from which we will receive the remote commands
if ((m_fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1)
{
// Connect to the socket
if (connect(m_fd, (struct sockaddr *)&addr, sizeof(addr)) != -1)
{
int opts;
m_bLogConnectFailure = true;
if ((opts = fcntl(m_fd,F_GETFL)) != -1)
{
// Set the socket to non-blocking
opts = (opts | O_NONBLOCK);
if (fcntl(m_fd,F_SETFL,opts) != -1)
{
if ((m_file = fdopen(m_fd, "r+")) != NULL)
{
// Setup inotify so we can disconnect if lircd is restarted
if ((m_inotify_fd = inotify_init()) >= 0)
{
// Set the fd non-blocking
if ((opts = fcntl(m_inotify_fd, F_GETFL)) != -1)
{
opts |= O_NONBLOCK;
if (fcntl(m_inotify_fd, F_SETFL, opts) != -1)
{
// Set an inotify watch on the lirc device
if ((m_inotify_wd = inotify_add_watch(m_inotify_fd, m_deviceName.c_str(), IN_DELETE_SELF)) != -1)
{
m_bInitialized = true;
CLog::Log(LOGINFO, "LIRC %s: sucessfully started", __FUNCTION__);
}
else
CLog::Log(LOGDEBUG, "LIRC: Failed to initialize Inotify. LIRC device will not be monitored.");
}
}
}
}
else
CLog::Log(LOGERROR, "LIRC %s: fdopen failed: %s", __FUNCTION__, strerror(errno));
}
else
CLog::Log(LOGERROR, "LIRC %s: fcntl(F_SETFL) failed: %s", __FUNCTION__, strerror(errno));
}
else
CLog::Log(LOGERROR, "LIRC %s: fcntl(F_GETFL) failed: %s", __FUNCTION__, strerror(errno));
}
else
{
if (m_bLogConnectFailure)
{
CLog::Log(LOGINFO, "LIRC %s: connect failed: %s", __FUNCTION__, strerror(errno));
m_bLogConnectFailure = false;
}
}
}
else
CLog::Log(LOGINFO, "LIRC %s: socket failed: %s", __FUNCTION__, strerror(errno));
if (!m_bInitialized)
{
Disconnect();
m_initRetryPeriod *= 2;
if (m_initRetryPeriod > 60000)
{
m_used = false;
CLog::Log(LOGDEBUG, "Failed to connect to LIRC. Giving up.");
}
else
CLog::Log(LOGDEBUG, "Failed to connect to LIRC. Retry in %ds.", m_initRetryPeriod/1000);
}
else
m_initRetryPeriod = 5000;
}
bool CRemoteControl::CheckDevice() {
if (m_inotify_fd < 0 || m_inotify_wd < 0)
return true; // inotify wasn't setup for some reason, assume all is well
int bufsize = sizeof(struct inotify_event) + PATH_MAX;
char buf[bufsize];
int ret = read(m_inotify_fd, buf, bufsize);
for (int i = 0; i + (int)sizeof(struct inotify_event) <= ret;) {
struct inotify_event* e = (struct inotify_event*)(buf+i);
if (e->mask & IN_DELETE_SELF) {
CLog::Log(LOGDEBUG, "LIRC device removed, disconnecting...");
Disconnect();
return false;
}
i += sizeof(struct inotify_event)+e->len;
}
return true;
}
void CRemoteControl::Update()
{
if (!m_bInitialized || !m_used )
return;
if (!CheckDevice())
return;
uint32_t now = SDL_GetTicks();
// Read a line from the socket
while (fgets(m_buf, sizeof(m_buf), m_file) != NULL)
{
// Remove the \n
m_buf[strlen(m_buf)-1] = '\0';
// Parse the result. Sample line:
// 000000037ff07bdd 00 OK mceusb
char scanCode[128];
char buttonName[128];
char repeatStr[4];
char deviceName[128];
sscanf(m_buf, "%s %s %s %s", &scanCode[0], &repeatStr[0], &buttonName[0], &deviceName[0]);
//beginning of lirc reply packet
//we get one when lirc is done sending something
if (!m_inReply && strcmp("BEGIN", scanCode) == 0)
{
m_inReply = true;
continue;
}
else if (m_inReply && strcmp("END", scanCode) == 0) //end of lirc reply packet
{
m_inReply = false;
if (m_nrSending > 0)
m_nrSending--;
continue;
}
if (m_inReply)
continue;
// Some template LIRC configuration have button names in apostrophes or quotes.
// If we got a quoted button name, strip 'em
unsigned int buttonNameLen = strlen(buttonName);
if ( buttonNameLen > 2
&& ( (buttonName[0] == '\'' && buttonName[buttonNameLen-1] == '\'')
|| ((buttonName[0] == '"' && buttonName[buttonNameLen-1] == '"') ) ) )
{
memmove( buttonName, buttonName + 1, buttonNameLen - 2 );
buttonName[ buttonNameLen - 2 ] = '\0';
}
m_button = CButtonTranslator::GetInstance().TranslateLircRemoteString(deviceName, buttonName);
if (strcmp(repeatStr, "00") == 0)
{
CLog::Log(LOGDEBUG, "LIRC: %s - NEW at %d:%s (%s)", __FUNCTION__, now, m_buf, buttonName);
m_firstClickTime = now;
m_isHolding = false;
m_skipHold = true;
return;
}
else if (now - m_firstClickTime >= (uint32_t) g_advancedSettings.m_remoteRepeat && !m_skipHold)
{
m_isHolding = true;
}
else
{
m_isHolding = false;
m_button = 0;
}
}
//drop commands when already sending
//because keypresses come in faster than lirc can send we risk hammering the daemon with commands
if (m_nrSending > 0)
{
m_sendData.clear();
}
else if (!m_sendData.empty())
{
fputs(m_sendData.c_str(), m_file);
fflush(m_file);
//nr of newlines equals nr of commands
for (int i = 0; i < (int)m_sendData.size(); i++)
if (m_sendData[i] == '\n')
m_nrSending++;
m_sendData.clear();
}
if (feof(m_file) != 0)
Disconnect();
m_skipHold = false;
}
WORD CRemoteControl::GetButton()
{
return m_button;
}
bool CRemoteControl::IsHolding()
{
return m_isHolding;
}
void CRemoteControl::AddSendCommand(const CStdString& command)
{
if (!m_bInitialized || !m_used)
return;
m_sendData += command;
m_sendData += '\n';
}