Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kinfocenter / view1394 / view1394.cpp
blob803fd53817bc72a63fb7f0cc50f47e516fde970d
1 /*
2 * view1394.cpp
4 * Copyright (C) 2003 Alexander Neundorf <neundorf@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "view1394.h"
23 #include <QLayout>
24 #include <QPushButton>
25 #include <QFile>
26 #include <QTextStream>
27 #include <QTreeWidgetItem>
28 #include <QTreeWidget>
29 #include <QVBoxLayout>
31 #include <kcomponentdata.h>
32 #include <kdialog.h>
33 #include <kglobal.h>
34 #include <klocale.h>
35 #include <kstandarddirs.h>
36 #include <kpluginfactory.h>
37 #include <kpluginloader.h>
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42 #include <netinet/in.h>
43 #include <stdio.h>
44 #include <string.h>
46 #include <libraw1394/csr.h>
48 #define CONFIGROM_BASE 0x00
49 #define CONFIGROM_CAP 0x08
50 #define CONFIGROM_GUID_HI 0x0c
51 #define CONFIGROM_GUID_LO 0x10
52 #define MAX_1394_PORTS 16
54 int my_reset_handler(raw1394handle_t handle, unsigned int) {
55 View1394* view1394=(View1394*)raw1394_get_userdata(handle);
56 if (view1394!=0)
57 view1394->rescanBus();
58 return 0;
61 K_PLUGIN_FACTORY(View1394Factory, registerPlugin<View1394>();)
62 K_EXPORT_PLUGIN(View1394Factory("kcmview1394"))
64 View1394::View1394(QWidget *parent, const QVariantList &) :
65 KCModule(View1394Factory::componentData(), parent), m_insideRescanBus(false) {
66 setQuickHelp(i18n("<qt>On the right hand side you can see some information about "
67 "your IEEE 1394 configuration. "
68 "The meaning of the columns:"
69 "<ul>"
70 "<li><b>Name</b>: port or node name, the number can change with each bus reset</li>"
71 "<li><b>GUID</b>: the 64 bit GUID of the node</li>"
72 "<li><b>Local</b>: checked if the node is an IEEE 1394 port of your computer</li>"
73 "<li><b>IRM</b>: checked if the node is isochronous resource manager capable</li>"
74 "<li><b>CRM</b>: checked if the node is cycle master capable</li>"
75 "<li><b>ISO</b>: checked if the node supports isochronous transfers</li>"
76 "<li><b>BM</b>: checked if the node is bus manager capable</li>"
77 "<li><b>PM</b>: checked if the node is power management capable</li>"
78 "<li><b>Acc</b>: the cycle clock accuracy of the node, valid from 0 to 100</li>"
79 "<li><b>Speed</b>: the speed of the node</li>"
80 "</ul></qt>"
81 ));
83 m_ouiDb=new OuiDb();
84 QVBoxLayout *box=new QVBoxLayout(this);
85 box->setSpacing(KDialog::spacingHint());
86 box->setMargin(0);
87 m_view=new View1394Widget(this);
89 for (int i=2; i<8; i++)
90 m_view->m_listview->setColumnAlignment(i, Qt::AlignHCenter);
91 m_view->m_listview->setColumnAlignment(8, Qt::AlignRight);
92 m_view->m_listview->setColumnAlignment(9, Qt::AlignRight);
94 box->addWidget(m_view);
95 m_view->layout()->setMargin(0);
96 connect(m_view->m_busResetPb, SIGNAL(clicked()), this, SLOT(generateBusReset()));
97 connect(&m_rescanTimer, SIGNAL(timeout()), this, SLOT(rescanBus()));
98 rescanBus();
101 View1394::~View1394() {
102 foreach(QSocketNotifier* notifier, m_notifiers) {
103 delete notifier;
106 m_notifiers.clear();
107 delete m_ouiDb;
110 bool View1394::readConfigRom(raw1394handle_t handle, nodeid_t nodeid, quadlet_t& firstQuad, quadlet_t& cap, octlet_t& guid) {
111 quadlet_t q=0;
112 firstQuad=0;
113 cap=0;
114 guid=0;
115 nodeaddr_t addr=CSR_REGISTER_BASE + CSR_CONFIG_ROM + CONFIGROM_BASE;
116 for (int count=0; count<5; count++) {
117 struct timeval tv;
118 q=0;
119 int res=raw1394_read(handle, nodeid|0xffc0, addr, sizeof(q), &q);
120 if (res==0) {
121 firstQuad=ntohl(q);
122 break;
124 tv.tv_sec=0;
125 tv.tv_usec=10*1000;
126 select(0, 0, 0, 0, &tv);
128 if (firstQuad==0)
129 return false;
131 addr=CSR_REGISTER_BASE + CSR_CONFIG_ROM + CONFIGROM_CAP;
132 if (raw1394_read(handle, nodeid|0xffc0, addr, sizeof(q), &q)!=0)
133 return false;
135 cap=ntohl(q);
137 addr=CSR_REGISTER_BASE + CSR_CONFIG_ROM + CONFIGROM_GUID_HI;
138 if (raw1394_read(handle, nodeid|0xffc0, addr, sizeof(q), &q)!=0)
139 return false;
141 guid=octlet_t(ntohl(q))<<32;
143 addr=CSR_REGISTER_BASE + CSR_CONFIG_ROM + CONFIGROM_GUID_LO;
144 if (raw1394_read(handle, nodeid|0xffc0, addr, sizeof(q), &q)!=0)
145 return false;
147 guid=guid|ntohl(q);
149 return true;
152 void View1394::callRaw1394EventLoop(int fd) {
153 for (QList<raw1394handle_t>::iterator it= m_handles.begin(); it!=m_handles.end(); ++it)
154 if (raw1394_get_fd(*it)==fd) {
155 raw1394_loop_iterate(*it);
156 break;
160 void View1394::rescanBus() {
161 if (m_insideRescanBus) {
162 m_rescanTimer.setSingleShot(true);
163 m_rescanTimer.start(100);
164 return;
166 m_insideRescanBus=true;
167 // static int depth=0;
168 // depth++;
169 m_notifiers.clear();
170 for (QList<raw1394handle_t>::iterator it=m_handles.begin(); it!=m_handles.end(); ++it)
171 raw1394_destroy_handle(*it);
172 m_handles.clear();
173 m_view->m_listview->clear();
175 raw1394handle_t handle=raw1394_new_handle();
176 if (handle==NULL) {
177 m_insideRescanBus=false;
178 return;
180 //now search for new stuff
181 struct raw1394_portinfo p_info[MAX_1394_PORTS];
182 int num_of_cards=raw1394_get_port_info(handle, p_info, MAX_1394_PORTS);
183 raw1394_destroy_handle(handle);
184 //iterate over all cards
185 for (int i=0; i<num_of_cards; i++) {
186 handle=raw1394_new_handle();
187 if (raw1394_set_port(handle, i)!=0) {
188 raw1394_destroy_handle(handle);
189 continue;
191 raw1394_set_userdata(handle, this);
192 raw1394_set_bus_reset_handler(handle, my_reset_handler);
193 QSocketNotifier *notif=new QSocketNotifier(raw1394_get_fd(handle),QSocketNotifier::Read);
194 connect(notif, SIGNAL(activated(int)), this, SLOT(callRaw1394EventLoop(int)));
195 m_notifiers.append(notif);
196 m_handles.append(handle);
198 QStringList cardContents;
199 cardContents << i18n("Port %1:\"%2\"", i, p_info[i].name);
200 QTreeWidgetItem* card = new QTreeWidgetItem(m_view->m_listview, cardContents);
202 int num_of_nodes=raw1394_get_nodecount(handle);
204 int localNodeId=raw1394_get_local_id(handle);
205 //iterate over all nodes connected to this card
206 for (int j=0; j<num_of_nodes; j++) {
207 //get the guid of the node
208 octlet_t guid=0;
209 quadlet_t firstQuad=0;
210 quadlet_t cap=0;
211 bool success=readConfigRom(handle, j, firstQuad, cap, guid);
213 QString nodeStr=i18n("Node %1", j);
214 if (!success) {
215 QStringList notReadyList;
216 notReadyList << nodeStr << i18n("Not ready");
217 new QTreeWidgetItem(card, notReadyList);
218 continue;
220 //minimal config rom
221 if (((firstQuad>>24) & 0xff)==1) {
222 QString guidStr=QString::number(firstQuad, 16);
223 guidStr="0x"+guidStr.rightJustified(6, '0');
225 QStringList romList;
226 romList << nodeStr << guidStr;
227 new QTreeWidgetItem(card, romList);
229 //general config rom
230 else {
231 QString guidStr;
232 char buf[32];
233 snprintf(buf, 32, "%llX", guid);
234 guidStr=buf;
235 guidStr="0x"+guidStr.rightJustified(16, '0');
236 QString local=((j | 0xffc0) == localNodeId) ? "X" : "";
237 QString irmStr=(cap & 0x80000000) ? "X" : "";
238 QString cmStr=(cap & 0x40000000) ? "X" : "";
239 QString isStr=(cap & 0x20000000) ? "X" : "";
240 QString bmStr=(cap & 0x10000000) ? "X" : "";
241 QString pmStr=(cap & 0x08000000) ? "X" : "";
242 QString accStr=QString::number((cap &0x00ff0000)>>16);
243 int speed=(cap & 0x00000007);
244 QString speedStr;
245 switch (speed) {
246 case (3):
247 speedStr="800";
248 break;
249 case (2):
250 speedStr="400";
251 break;
252 case (1):
253 speedStr="200";
254 break;
255 case (0):
256 default:
257 speedStr="100";
258 break;
261 QStringList nodeContents;
262 nodeContents << nodeStr << guidStr << local << irmStr << cmStr << isStr << bmStr << pmStr << accStr << speedStr << m_ouiDb->vendor(guid);
264 new QTreeWidgetItem(card, nodeContents);
267 card->setExpanded(true);
269 // depth--;
270 m_insideRescanBus=false;
273 void View1394::generateBusReset() {
274 for (QList<raw1394handle_t>::iterator it=m_handles.begin(); it!=m_handles.end(); ++it)
275 raw1394_reset_bus(*it);
278 OuiDb::OuiDb() {
279 QString filename=KStandardDirs::locate("data", "kcmview1394/oui.db");
280 if (filename.isEmpty())
281 return;
282 QFile f(filename);
283 if (!f.open(QIODevice::ReadOnly))
284 return;
286 QByteArray ba=f.readAll();
287 int bytesLeft=ba.size();
288 char* data=ba.data();
289 while (bytesLeft>8) {
290 char *eol=(char*)memchr((const void*)data, '\n', bytesLeft);
291 if (eol==0)
292 break;
293 if ((eol-data)<8)
294 break;
295 data[6]='\0';
296 *eol='\0';
297 m_vendorIds.insert(QLatin1String(data), QString::fromUtf8(data+7));
298 bytesLeft-=(eol+1-data);
299 data=eol+1;
302 f.close();
305 QString OuiDb::vendor(octlet_t guid) {
306 guid=(guid & 0xffffff0000000000LL)>>40;
307 QString key=QString::number((unsigned int)(guid), 16);
308 key=key.rightJustified(6, '0').toUpper();
309 QString v=m_vendorIds[key];
310 if (v.isEmpty())
311 v=i18n("Unknown");
312 return v;
315 // ------------------------------------------------------------------------
317 #include "view1394.moc"