Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kinfocenter / base / info_fbsd.cpp
blobc9d0861085ccb34b8317709d79224ae6fd0967bc
1 /*
2 * info_fbsd.cpp is part of the KDE program kcminfo. This displays
3 * various information about the system (hopefully a FreeBSD system)
4 * it's running on.
6 * All of the devinfo bits were blatantly stolen from the devinfo utility
7 * provided with FreeBSD 5.0 (and later). No gross hacks were harmed
8 * during the creation of info_fbsd.cpp. Thanks Mike.
9 */
12 * all following functions should return true, when the Information
13 * was filled into the QTreeWidget. Returning false indicates that
14 * information was not available.
17 #include "config-infocenter.h" // HAVE_DEVINFO_H
18 #include <sys/types.h>
19 #include <sys/sysctl.h>
21 #ifdef HAVE_DEVINFO_H
22 extern "C" {
23 #include <devinfo.h>
25 #endif
27 #include <errno.h>
28 #include <fstab.h>
29 #include <string.h>
31 #include <QMap>
32 #include <QFile>
34 #include <QTextStream>
36 class Device {
37 public:
38 Device(QString n=QString(), QString d=QString()) {
39 name=n;
40 description=d;
42 QString name, description;
45 void ProcessChildren(QString name);
46 QString GetController(const QString &line);
47 Device *GetDevice(const QString &line);
49 #ifdef HAVE_DEVINFO_H
50 extern "C" {
51 int print_irq(struct devinfo_rman *rman, void *arg);
52 int print_dma(struct devinfo_rman *rman, void *arg);
53 int print_ioports(struct devinfo_rman *rman, void *arg);
54 int print_resource(struct devinfo_res *res, void *arg);
56 #endif
58 bool GetInfo_CPU(QTreeWidget* tree) {
59 // Modified 13 July 2000 for SMP by Brad Hughes - bhughes@trolltech.com
61 int ncpu;
62 size_t len;
64 len = sizeof(ncpu);
65 sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);
67 QString cpustring;
68 for (int i = ncpu; i > 0; i--) {
69 /* Stuff for sysctl */
70 char *buf;
71 int i_buf;
73 // get the processor model
74 sysctlbyname("hw.model", NULL, &len, NULL, 0);
75 buf = new char[len];
76 sysctlbyname("hw.model", buf, &len, NULL, 0);
78 // get the TSC speed if we can
79 len = sizeof(i_buf);
80 if (sysctlbyname("machdep.tsc_freq", &i_buf, &len, NULL, 0) != -1) {
81 cpustring = i18n("CPU %1: %2, %3 MHz", i, buf, i_buf/1000000);
82 } else {
83 cpustring = i18n("CPU %1: %2, unknown speed", i, buf);
86 /* Put everything in the listbox */
87 QStringList list;
88 list << cpustring;
89 new QTreeWidgetItem(tree, list);
91 /* Clean up after ourselves, this time I mean it ;-) */
92 delete buf;
95 return true;
98 bool GetInfo_IRQ(QTreeWidget* tree) {
99 #ifdef HAVE_DEVINFO_H
100 /* systat lists the interrupts assigned to devices as well as how many were
101 generated. Parsing its output however is about as fun as a sandpaper
102 enema. The best idea would probably be to rip out the guts of systat.
103 Too bad it's not very well commented */
104 /* Oh neat, current now has a neat little utility called devinfo */
105 if (devinfo_init())
106 return false;
107 devinfo_foreach_rman(print_irq, tree);
108 return true;
109 #else
110 return false;
111 #endif
114 bool GetInfo_DMA(QTreeWidget* tree) {
115 #ifdef HAVE_DEVINFO_H
116 /* Oh neat, current now has a neat little utility called devinfo */
117 if (devinfo_init())
118 return false;
119 devinfo_foreach_rman(print_dma, tree);
120 return true;
121 #else
122 return false;
123 #endif
126 bool GetInfo_IO_Ports(QTreeWidget* tree) {
127 #ifdef HAVE_DEVINFO_H
128 /* Oh neat, current now has a neat little utility called devinfo */
129 if (devinfo_init())
130 return false;
131 devinfo_foreach_rman(print_ioports, tree);
132 return true;
133 #else
134 return false;
135 #endif
138 bool GetInfo_Sound(QTreeWidget* tree) {
139 QFile *sndstat = new QFile("/dev/sndstat");
140 QTextStream *t;
141 QString s;
143 if (!sndstat->exists() || !sndstat->open(QIODevice::ReadOnly)) {
145 s = i18n("Your sound system could not be queried. /dev/sndstat does not exist or is not readable.");
146 QStringList list;
147 list << s;
148 new QTreeWidgetItem(tree, list);
149 } else {
150 t = new QTextStream(sndstat);
151 while (!(s=t->readLine()).isNull()) {
152 QStringList list;
153 list << s;
155 new QTreeWidgetItem(tree, list);
158 delete t;
159 sndstat->close();
162 delete sndstat;
163 return true;
166 bool GetInfo_SCSI(QTreeWidget* tree) {
167 FILE *pipe;
168 QFile *camcontrol = new QFile("/sbin/camcontrol");
169 QTextStream *t;
170 QString s;
172 if (!camcontrol->exists()) {
173 s = i18n("SCSI subsystem could not be queried: /sbin/camcontrol could not be found");
174 QStringList list;
175 list << s;
176 new QTreeWidgetItem(tree, list);
177 } else if ((pipe = popen("/sbin/camcontrol devlist 2>&1", "r")) == NULL) {
178 s = i18n("SCSI subsystem could not be queried: /sbin/camcontrol could not be executed");
179 QStringList list;
180 list << s;
181 new QTreeWidgetItem(tree, list);
182 } else {
184 /* This prints out a list of all the scsi devies, perhaps eventually we could
185 parse it as opposed to schlepping it into a listbox */
187 t = new QTextStream(pipe, QIODevice::ReadOnly);
189 while (true) {
190 s = t->readLine();
191 if (s.isEmpty() )
192 break;
193 QStringList list;
194 list << s;
195 new QTreeWidgetItem(tree, list);
198 delete t;
199 pclose(pipe);
202 delete camcontrol;
204 if (!tree->topLevelItemCount())
205 return false;
207 return true;
210 bool GetInfo_PCI(QTreeWidget* tree) {
211 FILE *pipe;
212 QFile *pcicontrol;
213 QString s, cmd;
214 QTreeWidgetItem *olditem= NULL;
216 pcicontrol = new QFile("/usr/sbin/pciconf");
218 if (!pcicontrol->exists()) {
219 delete pcicontrol;
220 pcicontrol = new QFile("/usr/X11R6/bin/scanpci");
221 if (!pcicontrol->exists()) {
222 delete pcicontrol;
223 pcicontrol = new QFile("/usr/X11R6/bin/pcitweak");
224 if (!pcicontrol->exists()) {
225 QStringList list;
226 list << i18n("Could not find any programs with which to query your system's PCI information");
227 new QTreeWidgetItem(tree, list);
228 delete pcicontrol;
229 return true;
230 } else {
231 cmd = "/usr/X11R6/bin/pcitweak -l 2>&1";
233 } else {
234 cmd = "/usr/X11R6/bin/scanpci";
236 } else {
237 cmd = "/usr/sbin/pciconf -l -v 2>&1";
239 delete pcicontrol;
241 if ((pipe = popen(cmd.toLatin1(), "r")) == NULL) {
242 QStringList list;
243 list << i18n("PCI subsystem could not be queried: %1 could not be executed", cmd);
244 olditem = new QTreeWidgetItem(olditem, list);
245 } else {
247 /* This prints out a list of all the pci devies, perhaps eventually we could
248 parse it as opposed to schlepping it into a listbox */
250 pclose(pipe);
251 GetInfo_ReadfromPipe(tree, cmd.toLatin1(), true);
254 if (!tree->topLevelItemCount()) {
255 QString str = i18n("The PCI subsystem could not be queried, this may need root privileges.");
256 olditem = new QTreeWidgetItem(tree, olditem);
257 olditem->setText(0, str);
258 return true;
261 return true;
264 bool GetInfo_Partitions(QTreeWidget* tree) {
265 struct fstab *fstab_ent;
267 if (setfsent() != 1) /* Try to open fstab */{
268 int s_err= errno;
269 QString s;
270 s = i18n("Could not check file system info: ");
271 s += strerror(s_err);
272 QStringList list;
273 list << s;
274 new QTreeWidgetItem(tree, list);
275 } else {
276 QStringList headers;
277 headers << i18n("Device") << i18n("Mount Point") << i18n("FS Type") << i18n("Mount Options");
279 while ((fstab_ent=getfsent())!=NULL) {
280 QStringList list;
281 list << fstab_ent->fs_spec << fstab_ent->fs_file << fstab_ent->fs_vfstype << fstab_ent->fs_mntops;
282 new QTreeWidgetItem(tree, list);
285 tree->sortItems(0, Qt::AscendingOrder);
287 endfsent(); /* Close fstab */
289 return true;
292 bool GetInfo_XServer_and_Video(QTreeWidget* tree) {
293 return GetInfo_XServer_Generic(tree);
296 bool GetInfo_Devices(QTreeWidget* tree) {
297 QFile *f = new QFile("/var/run/dmesg.boot");
298 if (f->open(QIODevice::ReadOnly)) {
299 QTextStream qts(f);
300 QMap<QString, QTreeWidgetItem*> lv_items;
301 Device *dev;
302 QString line, controller;
303 tree->setRootIsDecorated(true);
304 QStringList headers;
305 headers << i18n("Device") << i18n("Description");
306 tree->setHeaderLabels(headers);
307 while ( !(line=qts.readLine()).isNull() ) {
308 controller = GetController(line);
309 if (controller.isNull())
310 continue;
311 dev=GetDevice(line);
312 if (!dev)
313 continue;
314 // Ewww assuing motherboard is the only toplevel controller is rather gross
315 if (controller == "motherboard") {
316 if (lv_items.contains(QString(dev->name))==false) {
317 QStringList list;
318 list << dev->name << dev->description;
320 lv_items.insert(QString(dev->name), new QTreeWidgetItem(tree, list));
322 } else {
323 QTreeWidgetItem* parent=lv_items[controller];
324 if (parent && lv_items.contains(dev->name)==false) {
325 QStringList list;
326 list << dev->name << dev->description;
327 lv_items.insert(QString(dev->name), new QTreeWidgetItem(parent, list));
331 delete f;
332 return true;
334 delete f;
335 return false;
338 QString GetController(const QString &line) {
339 if ( ( (line.startsWith("ad")) || (line.startsWith("afd")) || (line.startsWith("acd")) ) && (line.indexOf(":") < 6)) {
340 QString controller = line;
341 controller.remove(0, controller.indexOf(" at ")+4);
342 if (controller.indexOf("-slave") != -1) {
343 controller.remove(controller.indexOf("-slave"), controller.length());
344 } else if (controller.indexOf("-master") != -1) {
345 controller.remove(controller.indexOf("-master"), controller.length());
346 } else
347 controller=QString();
348 if (!controller.isNull())
349 return controller;
351 if (line.indexOf(" on ") != -1) {
352 QString controller;
353 controller = line;
354 controller.remove(0, controller.indexOf(" on ")+4);
355 if (controller.indexOf(" ") != -1)
356 controller.remove(controller.indexOf(" "), controller.length());
357 return controller;
359 return QString();
362 Device *GetDevice(const QString &line) {
363 Device *dev;
364 int colon = line.indexOf(":");
365 if (colon == -1)
366 return 0;
367 dev = new Device;
368 dev->name = line.mid(0, colon);
369 dev->description = line.mid(line.indexOf("<")+1, line.length());
370 dev->description.remove(dev->description.indexOf(">"), dev->description.length());
371 return dev;
374 #ifdef HAVE_DEVINFO_H
376 int print_irq(struct devinfo_rman *rman, void *arg) {
377 QTreeWidget* tree = (QTreeWidget *)arg;
378 if (strcmp(rman->dm_desc, "Interrupt request lines")==0) {
380 QStringList list;
381 list << rman->dm_desc;
382 new QTreeWidgetItem(tree, list);
383 devinfo_foreach_rman_resource(rman, print_resource, arg);
385 return 0;
388 int print_dma(struct devinfo_rman *rman, void *arg)
390 QTreeWidget* tree = (QTreeWidget *)arg;
391 if (strcmp(rman->dm_desc, "DMA request lines")==0) {
392 QStringList list;
393 list << rman->dm_desc;
394 new QTreeWidgetItem(tree, list);
395 devinfo_foreach_rman_resource(rman, print_resource, arg);
397 return(0);
400 int print_ioports(struct devinfo_rman *rman, void *arg)
402 QTreeWidget* tree = (QTreeWidget*) arg;
404 if (strcmp(rman->dm_desc, "I/O ports")==0) {
405 QStringList list;
406 list << rman->dm_desc;
407 new QTreeWidgetItem(tree, list);
409 devinfo_foreach_rman_resource(rman, print_resource, arg);
411 else if (strcmp(rman->dm_desc, "I/O memory addresses")==0) {
412 QStringList list;
413 list << rman->dm_desc;
414 new QTreeWidgetItem(tree, list);
416 devinfo_foreach_rman_resource(rman, print_resource, arg);
418 return 0;
421 int print_resource(struct devinfo_res *res, void *arg)
423 struct devinfo_dev *dev;
424 struct devinfo_rman *rman;
425 int hexmode;
427 QTreeWidget* tree = (QTreeWidget*) arg;
429 QString s, tmp;
431 rman = devinfo_handle_to_rman(res->dr_rman);
432 hexmode = (rman->dm_size > 100) || (rman->dm_size == 0);
433 tmp.sprintf(hexmode ? "0x%lx" : "%lu", res->dr_start);
434 s += tmp;
435 if (res->dr_size > 1) {
436 tmp.sprintf(hexmode ? "-0x%lx" : "-%lu",
437 res->dr_start + res->dr_size - 1);
438 s += tmp;
441 dev = devinfo_handle_to_device(res->dr_device);
442 if ((dev != NULL) && (dev->dd_name[0] != 0)) {
443 tmp.sprintf(" (%s)", dev->dd_name);
444 } else {
445 tmp.sprintf(" ----");
447 s += tmp;
449 QStringList list;
450 list << s;
451 new QTreeWidgetItem(tree, list);
453 return 0;
456 #endif