Diffstat (limited to 'noncore/tools/remote/buttondialog.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/tools/remote/buttondialog.cpp | 268 |
1 files changed, 268 insertions, 0 deletions
diff --git a/noncore/tools/remote/buttondialog.cpp b/noncore/tools/remote/buttondialog.cpp new file mode 100644 index 0000000..e7ff4b4 --- a/dev/null +++ b/noncore/tools/remote/buttondialog.cpp | |||
@@ -0,0 +1,268 @@ | |||
1 | /* | ||
2 | Opie-Remote. emulates remote controlls on an iPaq (and maybe a Zaurus) in Opie. | ||
3 | Copyright (C) 2002 Thomas Stephens | ||
4 | |||
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 | ||
7 | version. | ||
8 | |||
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 | ||
11 | Public License for more details. | ||
12 | |||
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 | ||
15 | */ | ||
16 | |||
17 | #include "buttondialog.h" | ||
18 | |||
19 | ButtonDialog::ButtonDialog(QString buttonName, QWidget *parent=0, const char*name=0, bool modal=FALSE, WFlags f=0):QDialog(parent, name, modal, f) | ||
20 | { | ||
21 | setCaption(tr(buttonName)); | ||
22 | |||
23 | QVBoxLayout *layout = new QVBoxLayout(this); | ||
24 | |||
25 | QHBoxLayout *hlayout1 = new QHBoxLayout(this); | ||
26 | QHBoxLayout *hlayout2 = new QHBoxLayout(this); | ||
27 | QHBoxLayout *hlayout3 = new QHBoxLayout(this); | ||
28 | |||
29 | layout->addSpacing(5); | ||
30 | layout->addLayout(hlayout1); | ||
31 | layout->addSpacing(5); | ||
32 | layout->addLayout(hlayout2); | ||
33 | layout->addSpacing(5); | ||
34 | layout->addLayout(hlayout3); | ||
35 | layout->addSpacing(5); | ||
36 | |||
37 | remote = new QComboBox(false, this, "remote"); | ||
38 | QLabel *remoteLabel = new QLabel(remote, "Remote: ", this, "remoteLabel"); | ||
39 | hlayout1->addSpacing(5); | ||
40 | hlayout1->addWidget(remoteLabel); | ||
41 | hlayout1->addSpacing(5); | ||
42 | hlayout1->addWidget(remote); | ||
43 | hlayout1->addSpacing(5); | ||
44 | remote->insertItem("Remote "); | ||
45 | remote->insertStringList(getRemotes()); | ||
46 | connect(remote, SIGNAL(activated(const QString &)), this, SLOT(remoteSelected(const QString&)) ); | ||
47 | |||
48 | button = new QComboBox(false, this, "button"); | ||
49 | QLabel *buttonLabel = new QLabel(remote, "Button: ", this, "buttonLabel"); | ||
50 | hlayout2->addSpacing(5); | ||
51 | hlayout2->addWidget(buttonLabel); | ||
52 | hlayout2->addSpacing(5); | ||
53 | hlayout2->addWidget(button); | ||
54 | hlayout2->addSpacing(5); | ||
55 | button->insertItem("Button "); | ||
56 | connect(button, SIGNAL(activated(const QString &)), this, SLOT(buttonSelected(const QString&)) ); | ||
57 | |||
58 | label = new QLineEdit(this, "label"); | ||
59 | label->setText(buttonName); | ||
60 | QLabel *labelLabel = new QLabel(label, "Label: ", this, "labelLabel"); | ||
61 | hlayout3->addSpacing(5); | ||
62 | hlayout3->addWidget(labelLabel); | ||
63 | hlayout3->addSpacing(5); | ||
64 | hlayout3->addWidget(label); | ||
65 | hlayout3->addSpacing(5); | ||
66 | } | ||
67 | |||
68 | void ButtonDialog::remoteSelected(const QString &string) | ||
69 | { | ||
70 | button->insertStringList(getButtons(string.latin1()) ); | ||
71 | list="SEND_ONCE"; | ||
72 | list+=string; | ||
73 | } | ||
74 | |||
75 | void ButtonDialog::buttonSelected(const QString &string) | ||
76 | { | ||
77 | list+=string; | ||
78 | } | ||
79 | |||
80 | QStringList ButtonDialog::getList() | ||
81 | { | ||
82 | return list; | ||
83 | } | ||
84 | |||
85 | QString ButtonDialog::getLabel() | ||
86 | { | ||
87 | return label->text(); | ||
88 | } | ||
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 | |||
102 | if(std::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1) | ||
103 | { | ||
104 | QMessageBox *mb = new QMessageBox("Error!", | ||
105 | "couldnt connect to socket", | ||
106 | QMessageBox::NoIcon, | ||
107 | QMessageBox::Ok, | ||
108 | QMessageBox::NoButton, | ||
109 | QMessageBox::NoButton); | ||
110 | mb->exec(); | ||
111 | perror("ButtonDialog::GetRemotes"); | ||
112 | return NULL; | ||
113 | } | ||
114 | |||
115 | write(fd, write_buffer, strlen(write_buffer)); | ||
116 | |||
117 | for(i=0; i<5; i++) | ||
118 | { | ||
119 | printf("%d\n", i); | ||
120 | readbuffer = readPacket(); | ||
121 | printf("%s", readbuffer); | ||
122 | printf("%d\n", i); | ||
123 | } | ||
124 | |||
125 | numlines = atoi(readbuffer); | ||
126 | |||
127 | for(i=0; i<numlines; i++) | ||
128 | { | ||
129 | list+=readPacket(); | ||
130 | } | ||
131 | |||
132 | if(strcasecmp(readPacket(), "END") != 0) | ||
133 | { | ||
134 | QMessageBox *mb = new QMessageBox("Error!", | ||
135 | "bad packet", | ||
136 | QMessageBox::NoIcon, | ||
137 | QMessageBox::Ok, | ||
138 | QMessageBox::NoButton, | ||
139 | QMessageBox::NoButton); | ||
140 | mb->exec(); | ||
141 | perror("ButtonDialog::GetRemotes"); | ||
142 | return NULL; | ||
143 | } | ||
144 | |||
145 | std::close(fd); | ||
146 | return list; | ||
147 | } | ||
148 | |||
149 | QStringList ButtonDialog::getButtons(const char *remoteName) | ||
150 | { | ||
151 | QString write_buffer = "LIST "; | ||
152 | const char *readbuffer; | ||
153 | int i, j, numlines; | ||
154 | QStringList list; | ||
155 | QString string; | ||
156 | |||
157 | write_buffer += remoteName; | ||
158 | write_buffer += '\n'; | ||
159 | |||
160 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
161 | |||
162 | if(std::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1) | ||
163 | { | ||
164 | QMessageBox *mb = new QMessageBox("Error!", | ||
165 | "couldnt connect to socket", | ||
166 | QMessageBox::NoIcon, | ||
167 | QMessageBox::Ok, | ||
168 | QMessageBox::NoButton, | ||
169 | QMessageBox::NoButton); | ||
170 | mb->exec(); | ||
171 | perror("ButtonDialog::GetButtons"); | ||
172 | return NULL; | ||
173 | } | ||
174 | |||
175 | write(fd, write_buffer.latin1(), strlen(write_buffer) ); | ||
176 | |||
177 | for(i=0; i<5; i++) | ||
178 | { | ||
179 | readbuffer = readPacket(); | ||
180 | } | ||
181 | |||
182 | numlines = atoi(readbuffer); | ||
183 | |||
184 | for(i=0; i<numlines; i++) | ||
185 | { | ||
186 | list+=readPacket(); | ||
187 | for(j=0; j<list[i].length(); j++) | ||
188 | { | ||
189 | if(list[i][j] == ' ') | ||
190 | break; | ||
191 | } | ||
192 | list[i].remove(0, j+1); | ||
193 | } | ||
194 | |||
195 | if(strcasecmp(readPacket(), "END") != 0) | ||
196 | { | ||
197 | QMessageBox *mb = new QMessageBox("Error!", | ||
198 | "bad packet", | ||
199 | QMessageBox::NoIcon, | ||
200 | QMessageBox::Ok, | ||
201 | QMessageBox::NoButton, | ||
202 | QMessageBox::NoButton); | ||
203 | mb->exec(); | ||
204 | perror("ButtonDialog::GetButtons"); | ||
205 | return NULL; | ||
206 | } | ||
207 | |||
208 | std::close(fd); | ||
209 | return list; | ||
210 | } | ||
211 | |||
212 | |||
213 | //this function was ripped for rc.c in xrc, it is available here: http://www.lirc.org/software.html | ||
214 | const char *ButtonDialog::readPacket() | ||
215 | { | ||
216 | static char buffer[PACKET_SIZE+1]=""; | ||
217 | char *end; | ||
218 | static int ptr=0,end_len=0; | ||
219 | ssize_t ret; | ||
220 | timeout = 0; | ||
221 | |||
222 | if(ptr>0) | ||
223 | { | ||
224 | memmove(buffer,buffer+ptr,strlen(buffer+ptr)+1); | ||
225 | ptr=strlen(buffer); | ||
226 | end=strchr(buffer,'\n'); | ||
227 | } | ||
228 | else | ||
229 | { | ||
230 | end=NULL; | ||
231 | } | ||
232 | alarm(TIMEOUT); | ||
233 | while(end==NULL) | ||
234 | { | ||
235 | if(PACKET_SIZE<=ptr) | ||
236 | { | ||
237 | fprintf(stderr,"bad packet\n"); | ||
238 | ptr=0; | ||
239 | return(NULL); | ||
240 | } | ||
241 | ret=read(fd,buffer+ptr,PACKET_SIZE-ptr); | ||
242 | |||
243 | if(ret<=0 || timeout) | ||
244 | { | ||
245 | if(timeout) | ||
246 | { | ||
247 | fprintf(stderr,"timeout\n"); | ||
248 | } | ||
249 | else | ||
250 | { | ||
251 | alarm(0); | ||
252 | } | ||
253 | ptr=0; | ||
254 | return(NULL); | ||
255 | } | ||
256 | buffer[ptr+ret]=0; | ||
257 | ptr=strlen(buffer); | ||
258 | end=strchr(buffer,'\n'); | ||
259 | } | ||
260 | alarm(0);timeout=0; | ||
261 | |||
262 | end[0]=0; | ||
263 | ptr=strlen(buffer)+1; | ||
264 | //# ifdef DEBUG | ||
265 | //printf("buffer: -%s-\n",buffer); | ||
266 | //# endif | ||
267 | return(buffer); | ||
268 | } | ||