33 #include <sys/socket.h>
39 #include <QLocalSocket>
45 class LircClientPrivate
51 K_GLOBAL_STATIC(LircClientPrivate, theInstancePrivate)
58 return &theInstancePrivate->instance;
63 int sock = ::socket(PF_UNIX, SOCK_STREAM, 0);
64 if (sock == -1)
return false;
67 addr.sun_family = AF_UNIX;
69 strcpy(addr.sun_path,
"/var/run/lirc/lircd");
70 if (::connect(sock, (
struct sockaddr *)(&addr),
sizeof(addr)) == -1) {
73 sock = ::socket(PF_UNIX, SOCK_STREAM, 0);
74 strcpy(addr.sun_path,
"/dev/lircd");
75 if (::connect(sock, (
struct sockaddr *)(&addr),
sizeof(addr)) == -1) {
77 kDebug() <<
"no lircd socket found...";
80 kDebug() <<
"lircd < 0.8.6 socket found...";
83 kDebug() <<
"lircd >= 0.8.6 socket found...";
87 theSocket =
new QLocalSocket();
89 theSocket->setSocketDescriptor(sock);
90 kDebug() <<
"updating remotes";
92 kDebug() <<
"waiting for lirc";
93 theSocket->waitForReadyRead();
94 kDebug() <<
"reading...";
96 connect(theSocket, SIGNAL(readyRead()), SLOT(slotRead()));
97 connect(theSocket, SIGNAL(disconnected()), SLOT(slotClosed()));
103 kDebug() <<
"deleting theSocket";
107 void LircClient::slotClosed()
109 kDebug() <<
"connection closed";
116 return theRemotes.keys();
121 return theRemotes.value(theRemote);
124 void LircClient::slotRead()
126 while (theSocket->bytesAvailable()) {
127 QString line = readLine();
128 if (line ==
"BEGIN") {
138 if (line ==
"SIGHUP") {
140 do line = readLine();
141 while (!line.isEmpty() && line !=
"END");
143 theSocket->waitForReadyRead();
146 }
else if (line ==
"LIST") {
148 if (readLine() !=
"SUCCESS" || readLine() !=
"DATA") {
149 do line = readLine();
150 while (!line.isEmpty() && line !=
"END");
154 int count = readLine().toInt();
155 for (
int i = 0; i < count; ++i)
156 remotes.append(readLine());
158 do line = readLine();
159 while (!line.isEmpty() && line !=
"END");
162 for (QStringList::ConstIterator it = remotes.constBegin(); it != remotes.constEnd(); ++it) {
163 sendCommand(
"LIST " + *it);
164 theSocket->waitForReadyRead();
168 }
else if (line.left(4) ==
"LIST") {
170 if (readLine() !=
"SUCCESS" || readLine() !=
"DATA") {
171 do line = readLine();
172 while (!line.isEmpty() && line !=
"END");
175 QString remote = line.mid(5);
177 int count = readLine().toInt();
178 for (
int i = 0; i < count; ++i) {
180 QString btn = readLine().mid(17);
181 if (btn.isNull())
break;
182 if (btn.startsWith(
'\'') && btn.endsWith(
'\''))
183 btn = btn.mid(1, btn.length() - 2);
186 theRemotes.insert(remote, buttons);
188 do line = readLine();
189 while (!line.isEmpty() && line !=
"END");
190 kDebug() <<
"Remotes read!";
195 int pos = line.indexOf(
' ');
198 int repeat = line.left(pos).toInt(&ok, 16);
200 line.remove(0, pos + 1);
202 pos = line.indexOf(
' ');
204 QString btn = line.left(pos);
205 if (btn.startsWith(
'\'') && btn.endsWith(
'\''))
206 btn = btn.mid(1, btn.length() - 2);
207 line.remove(0, pos + 1);
208 kDebug() <<
"Command received!";
214 void LircClient::updateRemotes()
222 kDebug() <<
"theSocket" << theSocket;
223 if (!theSocket)
return false;
224 kDebug() <<
"state:" << theSocket->state();
225 return theSocket->state() == QLocalSocket::ConnectedState;
228 const QString LircClient::readLine()
230 if (!theSocket->canReadLine()) {
231 theSocket->waitForReadyRead(500);
232 if (!theSocket->canReadLine()) {
236 QString line = theSocket->readLine();
237 line.truncate(line.length() - 1);
241 void LircClient::sendCommand(
const QString &command)
243 QByteArray cmd = QFile::encodeName(command +
'\n');
244 theSocket->write(cmd);
248 #include "lircclient.moc"
const QStringList buttons(const QString &theRemote) const
Retrieve list of buttons of a praticular remote control.
void commandReceived(const QString &remote, const QString &button, int repeatCounter)
Emitted when a IR command was received.
static LircClient * self()
Get the instance,.
void connectionClosed()
Emitted when the Lirc connection is closed.
bool connectToLirc()
Connects to lirc.
const QStringList remotes() const
Retrieve list of remote controls.
bool isConnected() const
Query status of connection.
void newRemoteList(const QStringList &remoteList)
Emitted when the list of controls / buttons was changed by lircd (SIGHUP)