-rw-r--r-- | noncore/tools/remote/lirchandler.cpp | 22 | ||||
-rw-r--r-- | noncore/tools/remote/lirchandler.h | 2 |
2 files changed, 12 insertions, 12 deletions
diff --git a/noncore/tools/remote/lirchandler.cpp b/noncore/tools/remote/lirchandler.cpp index f44806e..263b740 100644 --- a/noncore/tools/remote/lirchandler.cpp +++ b/noncore/tools/remote/lirchandler.cpp | |||
@@ -4,267 +4,267 @@ Copyright (C) 2007 Paul Eggleton & Thomas Stephens | |||
4 | 4 | ||
5 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public | 5 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public |
6 | License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later | 6 | License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later |
7 | version. | 7 | version. |
8 | 8 | ||
9 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the | 9 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
10 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | 10 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
11 | Public License for more details. | 11 | Public License for more details. |
12 | 12 | ||
13 | You should have received a copy of the GNU General Public License along with this program; if not, write to the Free | 13 | You should have received a copy of the GNU General Public License along with this program; if not, write to the Free |
14 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 14 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
15 | */ | 15 | */ |
16 | 16 | ||
17 | #include <stdio.h> | 17 | #include <stdio.h> |
18 | #include <stdlib.h> | 18 | #include <stdlib.h> |
19 | #include <string.h> | 19 | #include <string.h> |
20 | #include <sys/socket.h> | 20 | #include <sys/socket.h> |
21 | #include <sys/types.h> | 21 | #include <sys/types.h> |
22 | #include <sys/un.h> | 22 | #include <sys/un.h> |
23 | #include <unistd.h> | 23 | #include <unistd.h> |
24 | #include <errno.h> | 24 | #include <errno.h> |
25 | 25 | ||
26 | #include <qstring.h> | 26 | #include <qstring.h> |
27 | #include <qmessagebox.h> | 27 | #include <qmessagebox.h> |
28 | #include <qobject.h> | 28 | #include <qobject.h> |
29 | #include <opie2/oprocess.h> | 29 | #include <opie2/oprocess.h> |
30 | 30 | ||
31 | #include "lirchandler.h" | 31 | #include "lirchandler.h" |
32 | 32 | ||
33 | #define PACKET_SIZE 256 | 33 | #define PACKET_SIZE 256 |
34 | #define TIMEOUT 3 | 34 | #define TIMEOUT 3 |
35 | #define LIRCD_SOCKET "/dev/lircd" | 35 | #define LIRCD_SOCKET "/dev/lircd" |
36 | #define LIRCD_SERVICECMD "/etc/init.d/lircd" | 36 | #define LIRCD_SERVICECMD "/etc/init.d/lircd" |
37 | 37 | ||
38 | using namespace Opie::Core; | 38 | using namespace Opie::Core; |
39 | 39 | ||
40 | LircHandler::LircHandler(void) | 40 | LircHandler::LircHandler(void) |
41 | { | 41 | { |
42 | fd = 0; | 42 | fd = 0; |
43 | addr.sun_family=AF_UNIX; | 43 | addr.sun_family=AF_UNIX; |
44 | strcpy(addr.sun_path, LIRCD_SOCKET); | 44 | strcpy(addr.sun_path, LIRCD_SOCKET); |
45 | } | 45 | } |
46 | 46 | ||
47 | bool LircHandler::connectLirc(void) | 47 | bool LircHandler::connectLirc(void) |
48 | { | 48 | { |
49 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | 49 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
50 | if(fd == -1) | 50 | if(fd == -1) |
51 | { | 51 | { |
52 | QMessageBox *mb = new QMessageBox(QObject::tr("Error"), | 52 | QMessageBox mb(QObject::tr("Error"), |
53 | QObject::tr("Unable to create socket"), | 53 | QObject::tr("Unable to create socket"), |
54 | QMessageBox::Critical, | 54 | QMessageBox::Critical, |
55 | QMessageBox::Ok, | 55 | QMessageBox::Ok, |
56 | QMessageBox::NoButton, | 56 | QMessageBox::NoButton, |
57 | QMessageBox::NoButton); | 57 | QMessageBox::NoButton); |
58 | mb->exec(); | 58 | mb.exec(); |
59 | perror("LircHandler::connectLirc"); | 59 | perror("LircHandler::connectLirc"); |
60 | return false; | 60 | return false; |
61 | } | 61 | } |
62 | 62 | ||
63 | if(::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1) | 63 | if(::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1) |
64 | { | 64 | { |
65 | QMessageBox *mb = new QMessageBox(QObject::tr("Error"), | 65 | QMessageBox mb(QObject::tr("Error"), |
66 | QObject::tr("Could not connect to lircd"), | 66 | QObject::tr("Could not connect to lircd"), |
67 | QMessageBox::Critical, | 67 | QMessageBox::Critical, |
68 | QMessageBox::Ok, | 68 | QMessageBox::Ok, |
69 | QMessageBox::NoButton, | 69 | QMessageBox::NoButton, |
70 | QMessageBox::NoButton); | 70 | QMessageBox::NoButton); |
71 | mb->exec(); | 71 | mb.exec(); |
72 | perror("LircHandler::connectLirc"); | 72 | perror("LircHandler::connectLirc"); |
73 | return false; | 73 | return false; |
74 | } | 74 | } |
75 | 75 | ||
76 | return true; | 76 | return true; |
77 | } | 77 | } |
78 | 78 | ||
79 | //this function was ripped for rc.c in xrc, it is available here: http://www.lirc.org/software.html | 79 | //this function was ripped for rc.c in xrc, it is available here: http://www.lirc.org/software.html |
80 | const char *LircHandler::readPacket() | 80 | const char *LircHandler::readPacket() |
81 | { | 81 | { |
82 | static char buffer[PACKET_SIZE+1]=""; | 82 | static char buffer[PACKET_SIZE+1]=""; |
83 | char *end; | 83 | char *end; |
84 | static int ptr=0; | 84 | static int ptr=0; |
85 | ssize_t ret; | 85 | ssize_t ret; |
86 | int timeout = 0; | 86 | int timeout = 0; |
87 | 87 | ||
88 | if(ptr>0) | 88 | if(ptr>0) |
89 | { | 89 | { |
90 | memmove(buffer,buffer+ptr,strlen(buffer+ptr)+1); | 90 | memmove(buffer,buffer+ptr,strlen(buffer+ptr)+1); |
91 | ptr=strlen(buffer); | 91 | ptr=strlen(buffer); |
92 | end=strchr(buffer,'\n'); | 92 | end=strchr(buffer,'\n'); |
93 | } | 93 | } |
94 | else | 94 | else |
95 | { | 95 | { |
96 | end=NULL; | 96 | end=NULL; |
97 | } | 97 | } |
98 | alarm(TIMEOUT); | 98 | alarm(TIMEOUT); |
99 | while(end==NULL) | 99 | while(end==NULL) |
100 | { | 100 | { |
101 | if(PACKET_SIZE<=ptr) | 101 | if(PACKET_SIZE<=ptr) |
102 | { | 102 | { |
103 | fprintf(stderr,"readPacket: bad packet\n"); | 103 | fprintf(stderr,"readPacket: bad packet\n"); |
104 | ptr=0; | 104 | ptr=0; |
105 | return(NULL); | 105 | return(NULL); |
106 | } | 106 | } |
107 | ret=read(fd,buffer+ptr,PACKET_SIZE-ptr); | 107 | ret=read(fd,buffer+ptr,PACKET_SIZE-ptr); |
108 | 108 | ||
109 | if(ret<=0 || timeout) | 109 | if(ret<=0 || timeout) |
110 | { | 110 | { |
111 | if(timeout) | 111 | if(timeout) |
112 | { | 112 | { |
113 | fprintf(stderr,"readPacket: timeout\n"); | 113 | fprintf(stderr,"readPacket: timeout\n"); |
114 | } | 114 | } |
115 | else | 115 | else |
116 | { | 116 | { |
117 | alarm(0); | 117 | alarm(0); |
118 | } | 118 | } |
119 | ptr=0; | 119 | ptr=0; |
120 | return(NULL); | 120 | return(NULL); |
121 | } | 121 | } |
122 | buffer[ptr+ret]=0; | 122 | buffer[ptr+ret]=0; |
123 | ptr=strlen(buffer); | 123 | ptr=strlen(buffer); |
124 | end=strchr(buffer,'\n'); | 124 | end=strchr(buffer,'\n'); |
125 | } | 125 | } |
126 | alarm(0); | 126 | alarm(0); |
127 | 127 | ||
128 | end[0]=0; | 128 | end[0]=0; |
129 | ptr=strlen(buffer)+1; | 129 | ptr=strlen(buffer)+1; |
130 | //# ifdef DEBUG | 130 | //# ifdef DEBUG |
131 | //printf("buffer: -%s-\n",buffer); | 131 | //printf("buffer: -%s-\n",buffer); |
132 | //# endif | 132 | //# endif |
133 | return(buffer); | 133 | return(buffer); |
134 | } | 134 | } |
135 | 135 | ||
136 | QStringList LircHandler::getRemotes(void) | 136 | QStringList LircHandler::getRemotes(void) |
137 | { | 137 | { |
138 | const char write_buffer[] = "LIST\n"; | 138 | const char write_buffer[] = "LIST\n"; |
139 | const char *readbuffer; | 139 | const char *readbuffer; |
140 | int i, numlines; | 140 | int i, numlines; |
141 | QStringList list; | 141 | QStringList list; |
142 | 142 | ||
143 | if(connectLirc()) { | 143 | if(connectLirc()) { |
144 | write(fd, write_buffer, strlen(write_buffer) ); | 144 | write(fd, write_buffer, strlen(write_buffer) ); |
145 | 145 | ||
146 | for(i=0; i<5; i++) | 146 | for(i=0; i<5; i++) |
147 | { | 147 | { |
148 | readbuffer = readPacket(); | 148 | readbuffer = readPacket(); |
149 | } | 149 | } |
150 | 150 | ||
151 | numlines = atoi(readbuffer); | 151 | numlines = atoi(readbuffer); |
152 | 152 | ||
153 | for(i=0; i<numlines; i++) | 153 | for(i=0; i<numlines; i++) |
154 | { | 154 | { |
155 | list+=readPacket(); | 155 | list+=readPacket(); |
156 | } | 156 | } |
157 | 157 | ||
158 | if(strcasecmp(readPacket(), "END") != 0) | 158 | if(strcasecmp(readPacket(), "END") != 0) |
159 | { | 159 | { |
160 | QMessageBox *mb = new QMessageBox(QObject::tr("Error"), | 160 | QMessageBox mb(QObject::tr("Error"), |
161 | QObject::tr("Bad packet while communicating with lircd"), | 161 | QObject::tr("Bad packet while communicating with lircd"), |
162 | QMessageBox::Critical, | 162 | QMessageBox::Critical, |
163 | QMessageBox::Ok, | 163 | QMessageBox::Ok, |
164 | QMessageBox::NoButton, | 164 | QMessageBox::NoButton, |
165 | QMessageBox::NoButton); | 165 | QMessageBox::NoButton); |
166 | mb->exec(); | 166 | mb.exec(); |
167 | perror("LircHandler::getRemotes"); | 167 | perror("LircHandler::getRemotes"); |
168 | return NULL; | 168 | return NULL; |
169 | } | 169 | } |
170 | 170 | ||
171 | ::close(fd); | 171 | ::close(fd); |
172 | } | 172 | } |
173 | 173 | ||
174 | return list; | 174 | return list; |
175 | } | 175 | } |
176 | 176 | ||
177 | QStringList LircHandler::getButtons(const char *remoteName) | 177 | QStringList LircHandler::getButtons(const char *remoteName) |
178 | { | 178 | { |
179 | QString write_buffer = "LIST "; | 179 | QString write_buffer = "LIST "; |
180 | const char *readbuffer; | 180 | const char *readbuffer; |
181 | int i, j, numlines; | 181 | int i, j, numlines; |
182 | QStringList list; | 182 | QStringList list; |
183 | QString string; | 183 | QString string; |
184 | 184 | ||
185 | write_buffer += remoteName; | 185 | write_buffer += remoteName; |
186 | write_buffer += '\n'; | 186 | write_buffer += '\n'; |
187 | 187 | ||
188 | if(connectLirc()) { | 188 | if(connectLirc()) { |
189 | write(fd, write_buffer.latin1(), strlen(write_buffer) ); | 189 | write(fd, write_buffer.latin1(), strlen(write_buffer) ); |
190 | 190 | ||
191 | for(i=0; i<5; i++) | 191 | for(i=0; i<5; i++) |
192 | { | 192 | { |
193 | readbuffer = readPacket(); | 193 | readbuffer = readPacket(); |
194 | } | 194 | } |
195 | 195 | ||
196 | numlines = atoi(readbuffer); | 196 | numlines = atoi(readbuffer); |
197 | 197 | ||
198 | for(i=0; i<numlines; i++) | 198 | for(i=0; i<numlines; i++) |
199 | { | 199 | { |
200 | list+=readPacket(); | 200 | list+=readPacket(); |
201 | for(j=0; j<list[i].length(); j++) | 201 | for(j=0; j<list[i].length(); j++) |
202 | { | 202 | { |
203 | if(list[i][j] == ' ') | 203 | if(list[i][j] == ' ') |
204 | break; | 204 | break; |
205 | } | 205 | } |
206 | list[i].remove(0, j+1); | 206 | list[i].remove(0, j+1); |
207 | } | 207 | } |
208 | 208 | ||
209 | if(strcasecmp(readPacket(), "END") != 0) | 209 | if(strcasecmp(readPacket(), "END") != 0) |
210 | { | 210 | { |
211 | QMessageBox *mb = new QMessageBox(QObject::tr("Error"), | 211 | QMessageBox mb(QObject::tr("Error"), |
212 | QObject::tr("Bad packet while communicating with lircd"), | 212 | QObject::tr("Bad packet while communicating with lircd"), |
213 | QMessageBox::Critical, | 213 | QMessageBox::Critical, |
214 | QMessageBox::Ok, | 214 | QMessageBox::Ok, |
215 | QMessageBox::NoButton, | 215 | QMessageBox::NoButton, |
216 | QMessageBox::NoButton); | 216 | QMessageBox::NoButton); |
217 | mb->exec(); | 217 | mb.exec(); |
218 | perror("LircHandler::getRemotes"); | 218 | perror("LircHandler::getRemotes"); |
219 | return NULL; | 219 | return NULL; |
220 | } | 220 | } |
221 | 221 | ||
222 | ::close(fd); | 222 | ::close(fd); |
223 | } | 223 | } |
224 | 224 | ||
225 | return list; | 225 | return list; |
226 | } | 226 | } |
227 | 227 | ||
228 | int LircHandler::sendIR(const char *irbutton) | 228 | int LircHandler::sendIR(const char *lircaction) |
229 | { | 229 | { |
230 | const char *read_buffer; | 230 | const char *read_buffer; |
231 | bool done=false; | 231 | bool done=false; |
232 | 232 | ||
233 | if(connectLirc()) { | 233 | if(connectLirc()) { |
234 | printf("fd2: %d\n", fd); | 234 | printf("fd2: %d\n", fd); |
235 | printf("%s", irbutton); | 235 | printf("%s", lircaction); |
236 | 236 | ||
237 | printf("1\n"); | 237 | printf("1\n"); |
238 | printf("%d\n", write(fd, irbutton, strlen(irbutton) ) ); | 238 | printf("%d\n", write(fd, lircaction, strlen(lircaction) ) ); |
239 | printf("2\n"); | 239 | printf("2\n"); |
240 | while(!done) | 240 | while(!done) |
241 | { | 241 | { |
242 | read_buffer=readPacket(); | 242 | read_buffer=readPacket(); |
243 | printf("%s\n", read_buffer); | 243 | printf("%s\n", read_buffer); |
244 | if(strcasecmp(read_buffer, "END") == 0) | 244 | if(strcasecmp(read_buffer, "END") == 0) |
245 | { | 245 | { |
246 | printf("done reading packet\n"); | 246 | printf("done reading packet\n"); |
247 | done=true; | 247 | done=true; |
248 | } | 248 | } |
249 | } | 249 | } |
250 | ::close(fd); | 250 | ::close(fd); |
251 | return 1; | 251 | return 1; |
252 | } | 252 | } |
253 | else | 253 | else |
254 | return 0; | 254 | return 0; |
255 | } | 255 | } |
256 | 256 | ||
257 | bool LircHandler::startLircd(void) | 257 | bool LircHandler::startLircd(void) |
258 | { | 258 | { |
259 | return (system(LIRCD_SERVICECMD " start") == 0); | 259 | return (system(LIRCD_SERVICECMD " start") == 0); |
260 | } | 260 | } |
261 | 261 | ||
262 | bool LircHandler::stopLircd(void) | 262 | bool LircHandler::stopLircd(void) |
263 | { | 263 | { |
264 | return (system(LIRCD_SERVICECMD " stop") == 0); | 264 | return (system(LIRCD_SERVICECMD " stop") == 0); |
265 | } | 265 | } |
266 | 266 | ||
267 | bool LircHandler::isLircdRunning(void) | 267 | bool LircHandler::isLircdRunning(void) |
268 | { | 268 | { |
269 | return (OProcess::processPID("lircd") != 0); | 269 | return (OProcess::processPID("lircd") != 0); |
270 | } | 270 | } |
diff --git a/noncore/tools/remote/lirchandler.h b/noncore/tools/remote/lirchandler.h index 29894b4..c7665cb 100644 --- a/noncore/tools/remote/lirchandler.h +++ b/noncore/tools/remote/lirchandler.h | |||
@@ -1,38 +1,38 @@ | |||
1 | /* | 1 | /* |
2 | Opie-Remote. emulates remote controls on an iPaq (and maybe a Zaurus) in Opie. | 2 | Opie-Remote. emulates remote controls on an iPaq (and maybe a Zaurus) in Opie. |
3 | Copyright (C) 2007 Paul Eggleton & Thomas Stephens | 3 | Copyright (C) 2007 Paul Eggleton & Thomas Stephens |
4 | 4 | ||
5 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public | 5 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public |
6 | License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later | 6 | License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later |
7 | version. | 7 | version. |
8 | 8 | ||
9 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the | 9 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
10 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | 10 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
11 | Public License for more details. | 11 | Public License for more details. |
12 | 12 | ||
13 | You should have received a copy of the GNU General Public License along with this program; if not, write to the Free | 13 | You should have received a copy of the GNU General Public License along with this program; if not, write to the Free |
14 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 14 | 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 | class LircHandler | 20 | class LircHandler |
21 | { | 21 | { |
22 | private: | 22 | private: |
23 | bool connectLirc(void); | 23 | bool connectLirc(void); |
24 | const char *readPacket(); | 24 | const char *readPacket(); |
25 | 25 | ||
26 | struct sockaddr_un addr; | 26 | struct sockaddr_un addr; |
27 | int fd; | 27 | int fd; |
28 | public: | 28 | public: |
29 | LircHandler(void); | 29 | LircHandler(void); |
30 | QStringList getRemotes(void); | 30 | QStringList getRemotes(void); |
31 | QStringList getButtons(const char *remoteName); | 31 | QStringList getButtons(const char *remoteName); |
32 | int sendIR(const char *irbutton); | 32 | int sendIR(const char *lircaction); |
33 | bool startLircd(void); | 33 | bool startLircd(void); |
34 | bool stopLircd(void); | 34 | bool stopLircd(void); |
35 | bool isLircdRunning(void); | 35 | bool isLircdRunning(void); |
36 | }; | 36 | }; |
37 | 37 | ||
38 | #endif | 38 | #endif |