author | paule <paule> | 2007-01-28 04:04:14 (UTC) |
---|---|---|
committer | paule <paule> | 2007-01-28 04:04:14 (UTC) |
commit | ec2309cb69365847762db542c41951629cd06cbe (patch) (unidiff) | |
tree | 2f0101c1f0d6268207dbd15eb21f40c812a4fff5 /noncore/tools/remote/buttondialog.cpp | |
parent | 3cd37427b5c5f26f62cff583fbde914467ddafe3 (diff) | |
download | opie-ec2309cb69365847762db542c41951629cd06cbe.zip opie-ec2309cb69365847762db542c41951629cd06cbe.tar.gz opie-ec2309cb69365847762db542c41951629cd06cbe.tar.bz2 |
Refactored out all communication with LIRC into new LircHandler class
Diffstat (limited to 'noncore/tools/remote/buttondialog.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/tools/remote/buttondialog.cpp | 216 |
1 files changed, 9 insertions, 207 deletions
diff --git a/noncore/tools/remote/buttondialog.cpp b/noncore/tools/remote/buttondialog.cpp index 580f101..56738d1 100644 --- a/noncore/tools/remote/buttondialog.cpp +++ b/noncore/tools/remote/buttondialog.cpp | |||
@@ -33,17 +33,19 @@ ButtonDialog::ButtonDialog(QString buttonName, QWidget *parent, const char*name, | |||
33 | layout->addSpacing(5); | 33 | layout->addSpacing(5); |
34 | layout->addLayout(hlayout3); | 34 | layout->addLayout(hlayout3); |
35 | layout->addSpacing(5); | 35 | layout->addSpacing(5); |
36 | 36 | ||
37 | lh = new LircHandler(); | ||
38 | |||
37 | remote = new QComboBox(false, this, "remote"); | 39 | remote = new QComboBox(false, this, "remote"); |
38 | QLabel *remoteLabel = new QLabel(remote, "Remote: ", this, "remoteLabel"); | 40 | QLabel *remoteLabel = new QLabel(remote, "Remote: ", this, "remoteLabel"); |
39 | hlayout1->addSpacing(5); | 41 | hlayout1->addSpacing(5); |
40 | hlayout1->addWidget(remoteLabel); | 42 | hlayout1->addWidget(remoteLabel); |
41 | hlayout1->addSpacing(5); | 43 | hlayout1->addSpacing(5); |
42 | hlayout1->addWidget(remote); | 44 | hlayout1->addWidget(remote); |
43 | hlayout1->addSpacing(5); | 45 | hlayout1->addSpacing(5); |
44 | remote->insertItem("Remote "); | 46 | remote->insertItem("Remote "); |
45 | remote->insertStringList(getRemotes()); | 47 | remote->insertStringList(lh->getRemotes()); |
46 | connect(remote, SIGNAL(activated(const QString&)), this, SLOT(remoteSelected(const QString&)) ); | 48 | connect(remote, SIGNAL(activated(const QString&)), this, SLOT(remoteSelected(const QString&)) ); |
47 | 49 | ||
48 | button = new QComboBox(false, this, "button"); | 50 | button = new QComboBox(false, this, "button"); |
49 | QLabel *buttonLabel = new QLabel(remote, "Button: ", this, "buttonLabel"); | 51 | QLabel *buttonLabel = new QLabel(remote, "Button: ", this, "buttonLabel"); |
@@ -64,11 +66,16 @@ ButtonDialog::ButtonDialog(QString buttonName, QWidget *parent, const char*name, | |||
64 | hlayout3->addWidget(label); | 66 | hlayout3->addWidget(label); |
65 | hlayout3->addSpacing(5); | 67 | hlayout3->addSpacing(5); |
66 | } | 68 | } |
67 | 69 | ||
70 | ButtonDialog::~ButtonDialog() | ||
71 | { | ||
72 | delete lh; | ||
73 | } | ||
74 | |||
68 | void ButtonDialog::remoteSelected(const QString &string) | 75 | void ButtonDialog::remoteSelected(const QString &string) |
69 | { | 76 | { |
70 | button->insertStringList(getButtons(string.latin1()) ); | 77 | button->insertStringList(lh->getButtons(string.latin1()) ); |
71 | list="SEND_ONCE"; | 78 | list="SEND_ONCE"; |
72 | list+=string; | 79 | list+=string; |
73 | } | 80 | } |
74 | 81 | ||
@@ -85,209 +92,4 @@ QStringList ButtonDialog::getList() | |||
85 | QString ButtonDialog::getLabel() | 92 | QString ButtonDialog::getLabel() |
86 | { | 93 | { |
87 | return label->text(); | 94 | return label->text(); |
88 | } | 95 | } |
89 | |||
90 | QStringList ButtonDialog::getRemotes() | ||
91 | { | ||
92 | const char write_buffer[] = "LIST\n"; | ||
93 | const char *readbuffer; | ||
94 | int i, numlines; | ||
95 | QStringList list; | ||
96 | |||
97 | addr.sun_family=AF_UNIX; | ||
98 | strcpy(addr.sun_path,"/dev/lircd"); | ||
99 | |||
100 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
101 | if(fd == -1) | ||
102 | { | ||
103 | QMessageBox *mb = new QMessageBox("Error!", | ||
104 | "couldnt connect to socket", | ||
105 | QMessageBox::NoIcon, | ||
106 | QMessageBox::Ok, | ||
107 | QMessageBox::NoButton, | ||
108 | QMessageBox::NoButton); | ||
109 | mb->exec(); | ||
110 | perror("ButtonDialog::GetRemotes"); | ||
111 | return NULL; | ||
112 | } | ||
113 | |||
114 | if(::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1) | ||
115 | { | ||
116 | QMessageBox *mb = new QMessageBox("Error!", | ||
117 | "couldnt connect to socket", | ||
118 | QMessageBox::NoIcon, | ||
119 | QMessageBox::Ok, | ||
120 | QMessageBox::NoButton, | ||
121 | QMessageBox::NoButton); | ||
122 | mb->exec(); | ||
123 | perror("ButtonDialog::GetRemotes"); | ||
124 | return NULL; | ||
125 | } | ||
126 | |||
127 | write(fd, write_buffer, strlen(write_buffer)); | ||
128 | |||
129 | for(i=0; i<5; i++) | ||
130 | { | ||
131 | printf("%d\n", i); | ||
132 | readbuffer = readPacket(); | ||
133 | printf("%s", readbuffer); | ||
134 | printf("%d\n", i); | ||
135 | } | ||
136 | |||
137 | numlines = atoi(readbuffer); | ||
138 | |||
139 | for(i=0; i<numlines; i++) | ||
140 | { | ||
141 | list+=readPacket(); | ||
142 | } | ||
143 | |||
144 | if(strcasecmp(readPacket(), "END") != 0) | ||
145 | { | ||
146 | QMessageBox *mb = new QMessageBox("Error!", | ||
147 | "bad packet", | ||
148 | QMessageBox::NoIcon, | ||
149 | QMessageBox::Ok, | ||
150 | QMessageBox::NoButton, | ||
151 | QMessageBox::NoButton); | ||
152 | mb->exec(); | ||
153 | perror("ButtonDialog::GetRemotes"); | ||
154 | return NULL; | ||
155 | } | ||
156 | |||
157 | ::close(fd); | ||
158 | return list; | ||
159 | } | ||
160 | |||
161 | QStringList ButtonDialog::getButtons(const char *remoteName) | ||
162 | { | ||
163 | QString write_buffer = "LIST "; | ||
164 | const char *readbuffer; | ||
165 | int i, j, numlines; | ||
166 | QStringList list; | ||
167 | QString string; | ||
168 | |||
169 | write_buffer += remoteName; | ||
170 | write_buffer += '\n'; | ||
171 | |||
172 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
173 | if(fd == -1) | ||
174 | { | ||
175 | QMessageBox *mb = new QMessageBox("Error!", | ||
176 | "couldnt connect to socket", | ||
177 | QMessageBox::NoIcon, | ||
178 | QMessageBox::Ok, | ||
179 | QMessageBox::NoButton, | ||
180 | QMessageBox::NoButton); | ||
181 | mb->exec(); | ||
182 | perror("ButtonDialog::GetButtons"); | ||
183 | return NULL; | ||
184 | } | ||
185 | |||
186 | |||
187 | if(::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1) | ||
188 | { | ||
189 | QMessageBox *mb = new QMessageBox("Error!", | ||
190 | "couldnt connect to socket", | ||
191 | QMessageBox::NoIcon, | ||
192 | QMessageBox::Ok, | ||
193 | QMessageBox::NoButton, | ||
194 | QMessageBox::NoButton); | ||
195 | mb->exec(); | ||
196 | perror("ButtonDialog::GetButtons"); | ||
197 | return NULL; | ||
198 | } | ||
199 | |||
200 | write(fd, write_buffer.latin1(), strlen(write_buffer) ); | ||
201 | |||
202 | for(i=0; i<5; i++) | ||
203 | { | ||
204 | readbuffer = readPacket(); | ||
205 | } | ||
206 | |||
207 | numlines = atoi(readbuffer); | ||
208 | |||
209 | for(i=0; i<numlines; i++) | ||
210 | { | ||
211 | list+=readPacket(); | ||
212 | for(j=0; j<list[i].length(); j++) | ||
213 | { | ||
214 | if(list[i][j] == ' ') | ||
215 | break; | ||
216 | } | ||
217 | list[i].remove(0, j+1); | ||
218 | } | ||
219 | |||
220 | if(strcasecmp(readPacket(), "END") != 0) | ||
221 | { | ||
222 | QMessageBox *mb = new QMessageBox("Error!", | ||
223 | "bad packet", | ||
224 | QMessageBox::NoIcon, | ||
225 | QMessageBox::Ok, | ||
226 | QMessageBox::NoButton, | ||
227 | QMessageBox::NoButton); | ||
228 | mb->exec(); | ||
229 | perror("ButtonDialog::GetButtons"); | ||
230 | return NULL; | ||
231 | } | ||
232 | |||
233 | ::close(fd); | ||
234 | return list; | ||
235 | } | ||
236 | |||
237 | |||
238 | //this function was ripped for rc.c in xrc, it is available here: http://www.lirc.org/software.html | ||
239 | const char *ButtonDialog::readPacket() | ||
240 | { | ||
241 | static char buffer[PACKET_SIZE+1]=""; | ||
242 | char *end; | ||
243 | static int ptr=0,end_len=0; | ||
244 | ssize_t ret; | ||
245 | timeout = 0; | ||
246 | |||
247 | if(ptr>0) | ||
248 | { | ||
249 | memmove(buffer,buffer+ptr,strlen(buffer+ptr)+1); | ||
250 | ptr=strlen(buffer); | ||
251 | end=strchr(buffer,'\n'); | ||
252 | } | ||
253 | else | ||
254 | { | ||
255 | end=NULL; | ||
256 | } | ||
257 | alarm(TIMEOUT); | ||
258 | while(end==NULL) | ||
259 | { | ||
260 | if(PACKET_SIZE<=ptr) | ||
261 | { | ||
262 | fprintf(stderr,"bad packet\n"); | ||
263 | ptr=0; | ||
264 | return(NULL); | ||
265 | } | ||
266 | ret=read(fd,buffer+ptr,PACKET_SIZE-ptr); | ||
267 | |||
268 | if(ret<=0 || timeout) | ||
269 | { | ||
270 | if(timeout) | ||
271 | { | ||
272 | fprintf(stderr,"timeout\n"); | ||
273 | } | ||
274 | else | ||
275 | { | ||
276 | alarm(0); | ||
277 | } | ||
278 | ptr=0; | ||
279 | return(NULL); | ||
280 | } | ||
281 | buffer[ptr+ret]=0; | ||
282 | ptr=strlen(buffer); | ||
283 | end=strchr(buffer,'\n'); | ||
284 | } | ||
285 | alarm(0);timeout=0; | ||
286 | |||
287 | end[0]=0; | ||
288 | ptr=strlen(buffer)+1; | ||
289 | //# ifdef DEBUG | ||
290 | //printf("buffer: -%s-\n",buffer); | ||
291 | //# endif | ||
292 | return(buffer); | ||
293 | } | ||