// // // C++ Implementation: $MODULE$ // // Description: // // // Author: (C) 2002 by Malte Starostik // Adaption : Gav Wood , (C) 2003 // // Copyright: See COPYING file that comes with this distribution // // #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "tdelircclient.h" KLircClient::KLircClient(TQWidget *parent, const char *name) : TQObject(parent, name), theSocket(0), listIsUpToDate(false) { connectToLirc(); } bool KLircClient::connectToLirc() { int sock = ::socket(PF_UNIX, SOCK_STREAM, 0); if(sock == -1) return false; sockaddr_un addr; addr.sun_family = AF_UNIX; #if defined(__FreeBSD__) strcpy(addr.sun_path, "/var/run/lirc/lircd"); #else strcpy(addr.sun_path, "/dev/lircd"); #endif if(::connect(sock, (struct sockaddr *)(&addr), sizeof(addr)) == -1) { ::close(sock); // in case of mandrake... strcpy(addr.sun_path, "/tmp/.lircd"); if(::connect(sock, (struct sockaddr *)(&addr), sizeof(addr)) == -1) { ::close(sock); return false; } } theSocket = new TQSocket; theSocket->setSocket(sock); connect(theSocket, TQ_SIGNAL(readyRead()), TQ_SLOT(slotRead())); connect(theSocket, TQ_SIGNAL(connectionClosed()), TQ_SLOT(slotClosed())); updateRemotes(); return true; } KLircClient::~KLircClient() { // if(theSocket) delete theSocket; } void KLircClient::slotClosed() { delete theSocket; theSocket = 0; emit connectionClosed(); } const TQStringList KLircClient::remotes() const { TQStringList remotes; for(TQMap::ConstIterator i = theRemotes.begin(); i != theRemotes.end(); ++i) remotes.append(i.key()); remotes.sort(); return remotes; } const TQStringList KLircClient::buttons(const TQString &theRemote) const { return theRemotes[theRemote]; } void KLircClient::slotRead() { while (theSocket->bytesAvailable()) { TQString line = readLine(); if (line == "BEGIN") { // BEGIN // // [SUCCESS|ERROR] // [DATA // n // n lines of data] // END line = readLine(); if (line == "SIGHUP") { // Configuration changed do line = readLine(); while (!line.isEmpty() && line != "END"); updateRemotes(); return; } else if (line == "LIST") { // remote control list if (readLine() != "SUCCESS" || readLine() != "DATA") { do line = readLine(); while (!line.isEmpty() && line != "END"); return; } TQStringList remotes; int count = readLine().toInt(); for (int i = 0; i < count; ++i) remotes.append(readLine()); do line = readLine(); while (!line.isEmpty() && line != "END"); if (line.isEmpty()) return; // abort on corrupt data for (TQStringList::ConstIterator it = remotes.begin(); it != remotes.end(); ++it) sendCommand("LIST " + *it); return; } else if (line.left(4) == "LIST") { // button list if (readLine() != "SUCCESS" || readLine() != "DATA") { do line = readLine(); while (!line.isEmpty() && line != "END"); return; } TQString remote = line.mid(5); TQStringList buttons; int count = readLine().toInt(); for (int i = 0; i < count; ++i) { // TQString btn = readLine().mid(17); if(btn.isNull()) break; if(btn.startsWith("'") && btn.endsWith("'")) btn = btn.mid(1, btn.length() - 2); buttons.append(btn); } theRemotes.insert(remote, buttons); } do line = readLine(); while (!line.isEmpty() && line != "END"); listIsUpToDate = true; emit remotesRead(); } else { //