-rw-r--r-- | noncore/tools/remote/lirchandler.cpp | 166 | ||||
-rw-r--r-- | noncore/tools/remote/lirchandler.h | 8 |
2 files changed, 174 insertions, 0 deletions
diff --git a/noncore/tools/remote/lirchandler.cpp b/noncore/tools/remote/lirchandler.cpp index ec1e275..6022573 100644 --- a/noncore/tools/remote/lirchandler.cpp +++ b/noncore/tools/remote/lirchandler.cpp | |||
@@ -22,33 +22,38 @@ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
22 | #include <unistd.h> | 22 | #include <unistd.h> |
23 | #include <errno.h> | 23 | #include <errno.h> |
24 | 24 | ||
25 | #include <qstring.h> | 25 | #include <qstring.h> |
26 | #include <qmessagebox.h> | 26 | #include <qmessagebox.h> |
27 | #include <qobject.h> | 27 | #include <qobject.h> |
28 | #include <qtextstream.h> | ||
28 | #include <opie2/oprocess.h> | 29 | #include <opie2/oprocess.h> |
29 | #include <qpe/qcopenvelope_qws.h> | 30 | #include <qpe/qcopenvelope_qws.h> |
30 | 31 | ||
31 | #include "lirchandler.h" | 32 | #include "lirchandler.h" |
32 | 33 | ||
33 | #define PACKET_SIZE 256 | 34 | #define PACKET_SIZE 256 |
34 | #define TIMEOUT 3 | 35 | #define TIMEOUT 3 |
35 | #define LIRCD_SOCKET "/dev/lircd" | 36 | #define LIRCD_SOCKET "/dev/lircd" |
36 | #define LIRCD_SERVICECMD "/etc/init.d/lircd" | 37 | #define LIRCD_SERVICECMD "/etc/init.d/lircd" |
38 | #define LIRCD_CONF "/etc/lircd.conf" | ||
37 | 39 | ||
38 | using namespace Opie::Core; | 40 | using namespace Opie::Core; |
39 | 41 | ||
40 | LircHandler::LircHandler(void) | 42 | LircHandler::LircHandler(void) |
41 | { | 43 | { |
42 | fd = 0; | 44 | fd = 0; |
43 | addr.sun_family=AF_UNIX; | 45 | addr.sun_family=AF_UNIX; |
44 | strcpy(addr.sun_path, LIRCD_SOCKET); | 46 | strcpy(addr.sun_path, LIRCD_SOCKET); |
45 | } | 47 | } |
46 | 48 | ||
47 | bool LircHandler::connectLirc(void) | 49 | bool LircHandler::connectLirc(void) |
48 | { | 50 | { |
51 | if(!checkLircdConfValid(false)) | ||
52 | return false; | ||
53 | |||
49 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | 54 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
50 | if(fd == -1) | 55 | if(fd == -1) |
51 | { | 56 | { |
52 | QMessageBox mb(QObject::tr("Error"), | 57 | QMessageBox mb(QObject::tr("Error"), |
53 | QObject::tr("Unable to create socket"), | 58 | QObject::tr("Unable to create socket"), |
54 | QMessageBox::Critical, | 59 | QMessageBox::Critical, |
@@ -266,12 +271,21 @@ bool LircHandler::stopLircd(void) | |||
266 | 271 | ||
267 | bool LircHandler::isLircdRunning(void) | 272 | bool LircHandler::isLircdRunning(void) |
268 | { | 273 | { |
269 | return (OProcess::processPID("lircd") != 0); | 274 | return (OProcess::processPID("lircd") != 0); |
270 | } | 275 | } |
271 | 276 | ||
277 | void LircHandler::reloadLircdConf(void) | ||
278 | { | ||
279 | int pid = OProcess::processPID("lircd"); | ||
280 | if(pid > -1) | ||
281 | kill(pid, SIGHUP); | ||
282 | else | ||
283 | startLircd(); | ||
284 | } | ||
285 | |||
272 | bool LircHandler::setupModules(void) | 286 | bool LircHandler::setupModules(void) |
273 | { | 287 | { |
274 | // Remove IrDA modules which get in the way | 288 | // Remove IrDA modules which get in the way |
275 | system("rmmod ircomm-tty ircomm"); | 289 | system("rmmod ircomm-tty ircomm"); |
276 | // Load LIRC device driver | 290 | // Load LIRC device driver |
277 | system("modprobe lirc_sir"); | 291 | system("modprobe lirc_sir"); |
@@ -288,6 +302,158 @@ bool LircHandler::cleanupModules(void) | |||
288 | } | 302 | } |
289 | 303 | ||
290 | void LircHandler::disableIrDA(void) | 304 | void LircHandler::disableIrDA(void) |
291 | { | 305 | { |
292 | QCopEnvelope e("QPE/IrDaApplet", "disableIrda()"); | 306 | QCopEnvelope e("QPE/IrDaApplet", "disableIrda()"); |
293 | } | 307 | } |
308 | |||
309 | void LircHandler::mergeRemoteConfig(const QString &newconfig) | ||
310 | { | ||
311 | QStringList contents; | ||
312 | QStringList newcontents; | ||
313 | QFile conf(LIRCD_CONF); | ||
314 | QFile newconf(newconfig); | ||
315 | |||
316 | readFromFile(conf, contents); | ||
317 | readFromFile(newconf, newcontents); | ||
318 | contents += newcontents; | ||
319 | |||
320 | writeToFile(conf, contents); | ||
321 | } | ||
322 | |||
323 | void LircHandler::removeRemote(const QString &remotetodelete) | ||
324 | { | ||
325 | QStringList contents; | ||
326 | QFile conf(LIRCD_CONF); | ||
327 | bool found = false; | ||
328 | bool inremote = false; | ||
329 | QString remotename(""); | ||
330 | int lineidx = 0; | ||
331 | int startidx = 0; | ||
332 | int lastendidx = 0; | ||
333 | |||
334 | readFromFile(conf, contents); | ||
335 | |||
336 | for (QStringList::Iterator it = contents.begin(); it != contents.end(); ++it ) { | ||
337 | QString line = (*it).stripWhiteSpace(); | ||
338 | if(line == "begin remote") { | ||
339 | startidx = lastendidx; | ||
340 | inremote = true; | ||
341 | } | ||
342 | else if(line == "end remote") { | ||
343 | lastendidx = lineidx + 1; | ||
344 | if(remotename == remotetodelete) { | ||
345 | found = true; | ||
346 | break; | ||
347 | } | ||
348 | inremote = false; | ||
349 | } | ||
350 | else if(inremote && line.left(4) == "name") { | ||
351 | remotename = line.mid(4).stripWhiteSpace(); | ||
352 | } | ||
353 | lineidx++; | ||
354 | } | ||
355 | |||
356 | if(found) { | ||
357 | // Remove the remote and any preceding lines (most likely associated comments) | ||
358 | int linecount = lastendidx - startidx; | ||
359 | QStringList::Iterator it = contents.at(startidx); | ||
360 | for (int i = 0; i < linecount; i++ ) { | ||
361 | it = contents.remove(it); | ||
362 | } | ||
363 | |||
364 | // Check if there is at least one remote still defined | ||
365 | found = false; | ||
366 | for (it = contents.begin(); it != contents.end(); ++it ) { | ||
367 | QString line = (*it).stripWhiteSpace(); | ||
368 | if(line == "begin remote") { | ||
369 | found = true; | ||
370 | break; | ||
371 | } | ||
372 | } | ||
373 | |||
374 | if(found) | ||
375 | writeToFile(conf, contents); | ||
376 | else | ||
377 | conf.remove(); | ||
378 | } | ||
379 | } | ||
380 | |||
381 | bool LircHandler::checkRemoteExists(const QString &remote) | ||
382 | { | ||
383 | QStringList contents; | ||
384 | QFile conf(LIRCD_CONF); | ||
385 | bool inremote = false; | ||
386 | |||
387 | readFromFile(conf, contents); | ||
388 | |||
389 | for (QStringList::Iterator it = contents.begin(); it != contents.end(); ++it ) { | ||
390 | QString line = (*it).stripWhiteSpace(); | ||
391 | if(line == "begin remote") { | ||
392 | inremote = true; | ||
393 | } | ||
394 | else if(line == "end remote") { | ||
395 | inremote = false; | ||
396 | } | ||
397 | else if(inremote && line.left(4) == "name") { | ||
398 | QString rname = line.mid(4).stripWhiteSpace(); | ||
399 | if(rname == remote) | ||
400 | return true; | ||
401 | } | ||
402 | } | ||
403 | return false; | ||
404 | } | ||
405 | |||
406 | bool LircHandler::checkLircdConfValid(bool silent) | ||
407 | { | ||
408 | QStringList contents; | ||
409 | QFile conf(LIRCD_CONF); | ||
410 | bool inremote = false; | ||
411 | |||
412 | if(conf.exists()) { | ||
413 | readFromFile(conf, contents); | ||
414 | |||
415 | for (QStringList::Iterator it = contents.begin(); it != contents.end(); ++it ) { | ||
416 | QString line = (*it).stripWhiteSpace(); | ||
417 | if(line == "begin remote") { | ||
418 | inremote = true; | ||
419 | } | ||
420 | else if(line == "end remote") { | ||
421 | if(inremote) | ||
422 | return true; | ||
423 | } | ||
424 | } | ||
425 | } | ||
426 | |||
427 | if(!silent) { | ||
428 | QMessageBox::information(NULL, QObject::tr("No remote"), | ||
429 | QObject::tr("No remotes have been learned.\nPlease go to the Learn tab\nand learn a remote."), | ||
430 | QMessageBox::Ok, QMessageBox::NoButton); | ||
431 | } | ||
432 | |||
433 | return false; | ||
434 | } | ||
435 | |||
436 | bool LircHandler::readFromFile(QFile &file, QStringList &strlist) | ||
437 | { | ||
438 | if(file.open(IO_ReadOnly)) { | ||
439 | QTextStream in(&file); | ||
440 | |||
441 | strlist = QStringList::split('\n', in.read(), true); | ||
442 | file.close(); | ||
443 | if(strlist.count() > 0) | ||
444 | strlist.remove(strlist.at(strlist.count() - 1)); // remove extra blank line | ||
445 | } | ||
446 | return true; | ||
447 | } | ||
448 | |||
449 | bool LircHandler::writeToFile(QFile &file, QStringList &strlist) | ||
450 | { | ||
451 | if(file.open(IO_WriteOnly | IO_Truncate)) { | ||
452 | QTextStream out(&file); | ||
453 | for (QStringList::Iterator it = strlist.begin(); it != strlist.end(); ++it ) { | ||
454 | out << (*it) << "\n"; | ||
455 | } | ||
456 | file.close(); | ||
457 | } | ||
458 | return true; | ||
459 | } | ||
diff --git a/noncore/tools/remote/lirchandler.h b/noncore/tools/remote/lirchandler.h index 4e56b6b..5ba14b7 100644 --- a/noncore/tools/remote/lirchandler.h +++ b/noncore/tools/remote/lirchandler.h | |||
@@ -15,29 +15,37 @@ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
15 | */ | 15 | */ |
16 | 16 | ||
17 | #ifndef LIRCHANDLER_H | 17 | #ifndef LIRCHANDLER_H |
18 | #define LIRCHANDLER_H | 18 | #define LIRCHANDLER_H |
19 | 19 | ||
20 | #include <sys/un.h> | 20 | #include <sys/un.h> |
21 | #include <qfile.h> | ||
21 | 22 | ||
22 | class LircHandler | 23 | class LircHandler |
23 | { | 24 | { |
24 | private: | 25 | private: |
25 | bool connectLirc(void); | 26 | bool connectLirc(void); |
26 | const char *readPacket(); | 27 | const char *readPacket(); |
28 | bool readFromFile(QFile &file, QStringList &strlist); | ||
29 | bool writeToFile(QFile &file, QStringList &strlist); | ||
27 | 30 | ||
28 | struct sockaddr_un addr; | 31 | struct sockaddr_un addr; |
29 | int fd; | 32 | int fd; |
30 | public: | 33 | public: |
31 | LircHandler(void); | 34 | LircHandler(void); |
32 | QStringList getRemotes(void); | 35 | QStringList getRemotes(void); |
33 | QStringList getButtons(const char *remoteName); | 36 | QStringList getButtons(const char *remoteName); |
34 | int sendIR(const char *lircaction); | 37 | int sendIR(const char *lircaction); |
35 | bool startLircd(void); | 38 | bool startLircd(void); |
36 | bool stopLircd(void); | 39 | bool stopLircd(void); |
37 | bool isLircdRunning(void); | 40 | bool isLircdRunning(void); |
41 | void reloadLircdConf(void); | ||
38 | bool setupModules(void); | 42 | bool setupModules(void); |
39 | bool cleanupModules(void); | 43 | bool cleanupModules(void); |
40 | void disableIrDA(void); | 44 | void disableIrDA(void); |
45 | void mergeRemoteConfig(const QString &newconfig); | ||
46 | void removeRemote(const QString &remotetodelete); | ||
47 | bool checkRemoteExists(const QString &remote); | ||
48 | bool checkLircdConfValid(bool silent); | ||
41 | }; | 49 | }; |
42 | 50 | ||
43 | #endif | 51 | #endif |