You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
242 lines
5.3 KiB
242 lines
5.3 KiB
15 years ago
|
//
|
||
|
//
|
||
|
// C++ Implementation: $MODULE$
|
||
|
//
|
||
|
// Description:
|
||
|
//
|
||
|
//
|
||
|
// Author: (C) 2002 by Malte Starostik
|
||
|
// Adaption : Gav Wood <gav@kde.org>, (C) 2003
|
||
|
//
|
||
|
// Copyright: See COPYING file that comes with this distribution
|
||
|
//
|
||
|
//
|
||
|
|
||
|
#include <unistd.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <cstring>
|
||
|
#include <sys/un.h>
|
||
|
#include <sys/socket.h>
|
||
|
#include <errno.h>
|
||
|
|
||
14 years ago
|
#include <tqwidget.h>
|
||
|
#include <tqdialog.h>
|
||
|
#include <tqtooltip.h>
|
||
|
#include <tqsocket.h>
|
||
|
#include <tqsocketnotifier.h>
|
||
|
#include <tqfile.h>
|
||
15 years ago
|
|
||
12 years ago
|
#include <tdeapplication.h>
|
||
15 years ago
|
#include <ksystemtray.h>
|
||
|
#include <kiconloader.h>
|
||
|
#include <kpassivepopup.h>
|
||
12 years ago
|
#include <tdemessagebox.h>
|
||
12 years ago
|
#include <tdepopupmenu.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
12 years ago
|
#include <tdeaboutdialog.h>
|
||
|
#include <tdeabouttde.h>
|
||
15 years ago
|
|
||
|
#include <dcopclient.h>
|
||
|
#include <dcopref.h>
|
||
|
|
||
11 years ago
|
#include "tdelircclient.h"
|
||
15 years ago
|
|
||
|
|
||
13 years ago
|
KLircClient::KLircClient(TQWidget *parent, const char *name) : TQObject(parent, name), theSocket(0), listIsUpToDate(false)
|
||
15 years ago
|
{
|
||
|
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;
|
||
|
strcpy(addr.sun_path, "/dev/lircd");
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
theSocket = new TQSocket;
|
||
15 years ago
|
theSocket->setSocket(sock);
|
||
14 years ago
|
connect(theSocket, TQT_SIGNAL(readyRead()), TQT_SLOT(slotRead()));
|
||
|
connect(theSocket, TQT_SIGNAL(connectionClosed()), TQT_SLOT(slotClosed()));
|
||
15 years ago
|
updateRemotes();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
KLircClient::~KLircClient()
|
||
|
{
|
||
|
// if(theSocket)
|
||
|
delete theSocket;
|
||
|
}
|
||
|
|
||
|
void KLircClient::slotClosed()
|
||
|
{
|
||
|
delete theSocket;
|
||
|
theSocket = 0;
|
||
|
emit connectionClosed();
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQStringList KLircClient::remotes() const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQStringList remotes;
|
||
|
for(TQMap<TQString, TQStringList>::ConstIterator i = theRemotes.begin(); i != theRemotes.end(); ++i)
|
||
15 years ago
|
remotes.append(i.key());
|
||
|
remotes.sort();
|
||
|
return remotes;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQStringList KLircClient::buttons(const TQString &theRemote) const
|
||
15 years ago
|
{
|
||
|
return theRemotes[theRemote];
|
||
|
}
|
||
|
|
||
|
void KLircClient::slotRead()
|
||
|
{
|
||
|
while (theSocket->bytesAvailable())
|
||
|
{
|
||
14 years ago
|
TQString line = readLine();
|
||
15 years ago
|
if (line == "BEGIN")
|
||
|
{
|
||
|
// BEGIN
|
||
|
// <command>
|
||
|
// [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;
|
||
|
}
|
||
14 years ago
|
TQStringList remotes;
|
||
15 years ago
|
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
|
||
14 years ago
|
for (TQStringList::ConstIterator it = remotes.begin(); it != remotes.end(); ++it)
|
||
15 years ago
|
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;
|
||
|
}
|
||
14 years ago
|
TQString remote = line.mid(5);
|
||
|
TQStringList buttons;
|
||
15 years ago
|
int count = readLine().toInt();
|
||
|
for (int i = 0; i < count; ++i)
|
||
|
{
|
||
|
// <code> <name>
|
||
14 years ago
|
TQString btn = readLine().mid(17);
|
||
15 years ago
|
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
|
||
|
{
|
||
|
// <code> <repeat> <button name> <remote control name>
|
||
|
line.remove(0, 17); // strip code
|
||
13 years ago
|
int pos = line.find(' ');
|
||
15 years ago
|
if (pos < 0) return;
|
||
|
bool ok;
|
||
|
int repeat = line.left(pos).toInt(&ok, 16);
|
||
|
if (!ok) return;
|
||
|
line.remove(0, pos + 1);
|
||
|
|
||
13 years ago
|
pos = line.find(' ');
|
||
15 years ago
|
if (pos < 0) return;
|
||
14 years ago
|
TQString btn = line.left(pos);
|
||
15 years ago
|
if(btn.startsWith("'") && btn.endsWith("'"))
|
||
|
btn = btn.mid(1, btn.length() - 2);
|
||
|
line.remove(0, pos + 1);
|
||
|
|
||
|
emit commandReceived(line, btn, repeat);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KLircClient::updateRemotes()
|
||
|
{
|
||
|
listIsUpToDate = false;
|
||
|
theRemotes.clear();
|
||
|
sendCommand("LIST");
|
||
|
}
|
||
|
|
||
|
bool KLircClient::isConnected() const
|
||
|
{
|
||
|
if(!theSocket) return false;
|
||
14 years ago
|
return theSocket->state() == TQSocket::Connected;
|
||
15 years ago
|
}
|
||
|
|
||
|
bool KLircClient::haveFullList() const
|
||
|
{
|
||
|
return listIsUpToDate;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQString KLircClient::readLine()
|
||
15 years ago
|
{
|
||
|
if (!theSocket->canReadLine())
|
||
|
{ bool timeout;
|
||
|
// FIXME: possible race condition -
|
||
|
// more might have arrived between canReadLine and waitForMore
|
||
|
theSocket->waitForMore(500, &timeout);
|
||
|
if (timeout)
|
||
|
{ // something's wrong. there ain't no line comin!
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
}
|
||
|
}
|
||
14 years ago
|
TQString line = theSocket->readLine();
|
||
15 years ago
|
line.truncate(line.length() - 1);
|
||
|
return line;
|
||
|
}
|
||
|
|
||
14 years ago
|
void KLircClient::sendCommand(const TQString &command)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString cmd = command + "\n";
|
||
|
theSocket->writeBlock(TQFile::encodeName( cmd ), cmd.length());
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
11 years ago
|
#include "tdelircclient.moc"
|