Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / apps / kinfocenter / info / info_fbsd.cpp
blob73dca6c91ccb36def72507c21fd729031e7335dc
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 */
11 #define INFO_CPU_AVAILABLE
12 #define INFO_IRQ_AVAILABLE
13 #define INFO_DMA_AVAILABLE
14 #define INFO_PCI_AVAILABLE
15 #define INFO_IOPORTS_AVAILABLE
16 #define INFO_SOUND_AVAILABLE
17 #define INFO_DEVICES_AVAILABLE
18 #define INFO_SCSI_AVAILABLE
19 #define INFO_PARTITIONS_AVAILABLE
20 #define INFO_XSERVER_AVAILABLE
24 * all following functions should return true, when the Information
25 * was filled into the lBox-Widget. Returning false indicates that
26 * information was not available.
28 #include "config-infocenter.h" // HAVE_DEVINFO_H
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
33 #ifdef HAVE_DEVINFO_H
34 extern "C" {
35 #include <devinfo.h>
37 #endif
39 #include <errno.h>
40 #include <fstab.h>
41 #include <string.h>
43 #include <Qt3Support/Q3Dict>
44 #include <QFile>
46 #include <QTextStream>
48 class Device {
49 public:
50 Device (QString n=QString(), QString d=QString())
51 {name=n; description=d;}
52 QString name, description;
55 void ProcessChildren(QString name);
56 QString GetController(const QString &line);
57 Device *GetDevice(const QString &line);
59 #ifdef HAVE_DEVINFO_H
60 extern "C" {
61 int print_irq(struct devinfo_rman *rman, void *arg);
62 int print_dma(struct devinfo_rman *rman, void *arg);
63 int print_ioports(struct devinfo_rman *rman, void *arg);
64 int print_resource(struct devinfo_res *res, void *arg);
66 #endif
68 bool GetInfo_CPU (Q3ListView *lBox)
70 // Modified 13 July 2000 for SMP by Brad Hughes - bhughes@trolltech.com
72 int ncpu;
73 size_t len;
75 len = sizeof(ncpu);
76 sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);
78 QString cpustring;
79 for (int i = ncpu; i > 0; i--) {
80 /* Stuff for sysctl */
81 char *buf;
82 int i_buf;
84 // get the processor model
85 sysctlbyname("hw.model", NULL, &len, NULL, 0);
86 buf = new char[len];
87 sysctlbyname("hw.model", buf, &len, NULL, 0);
89 // get the TSC speed if we can
90 len = sizeof(i_buf);
91 if (sysctlbyname("machdep.tsc_freq", &i_buf, &len, NULL, 0) != -1) {
92 cpustring = i18n("CPU %1: %2, %3 MHz", i, buf, i_buf/1000000);
93 } else {
94 cpustring = i18n("CPU %1: %2, unknown speed", i, buf);
97 /* Put everything in the listbox */
98 new Q3ListViewItem(lBox, cpustring);
100 /* Clean up after ourselves, this time I mean it ;-) */
101 delete buf;
104 return true;
107 bool GetInfo_IRQ (Q3ListView *lbox)
109 #ifdef HAVE_DEVINFO_H
110 /* systat lists the interrupts assigned to devices as well as how many were
111 generated. Parsing its output however is about as fun as a sandpaper
112 enema. The best idea would probably be to rip out the guts of systat.
113 Too bad it's not very well commented */
114 /* Oh neat, current now has a neat little utility called devinfo */
115 if (devinfo_init())
116 return false;
117 devinfo_foreach_rman(print_irq, lbox);
118 return true;
119 #else
120 return false;
121 #endif
124 bool GetInfo_DMA (Q3ListView *lbox)
126 #ifdef HAVE_DEVINFO_H
127 /* Oh neat, current now has a neat little utility called devinfo */
128 if (devinfo_init())
129 return false;
130 devinfo_foreach_rman(print_dma, lbox);
131 return true;
132 #else
133 return false;
134 #endif
137 bool GetInfo_IO_Ports (Q3ListView *lbox)
139 #ifdef HAVE_DEVINFO_H
140 /* Oh neat, current now has a neat little utility called devinfo */
141 if (devinfo_init())
142 return false;
143 devinfo_foreach_rman(print_ioports, lbox);
144 return true;
145 #else
146 return false;
147 #endif
150 bool GetInfo_Sound (Q3ListView *lbox)
152 QFile *sndstat = new QFile("/dev/sndstat");
153 QTextStream *t;
154 QString s;
155 Q3ListViewItem *olditem = 0;
157 if (!sndstat->exists() || !sndstat->open(QIODevice::ReadOnly)) {
159 s = i18n("Your sound system could not be queried. /dev/sndstat does not exist or is not readable.");
160 olditem = new Q3ListViewItem(lbox, olditem, s);
161 } else {
162 t = new QTextStream(sndstat);
163 while (!(s=t->readLine()).isNull()) {
164 olditem = new Q3ListViewItem(lbox, olditem, s);
167 delete t;
168 sndstat->close();
171 delete sndstat;
172 return true;
175 bool GetInfo_SCSI (Q3ListView *lbox)
177 FILE *pipe;
178 QFile *camcontrol = new QFile("/sbin/camcontrol");
179 QTextStream *t;
180 QString s;
181 Q3ListViewItem *olditem = 0;
183 if (!camcontrol->exists()) {
184 s = i18n ("SCSI subsystem could not be queried: /sbin/camcontrol could not be found");
185 olditem = new Q3ListViewItem(lbox, olditem, s);
186 } else if ((pipe = popen("/sbin/camcontrol devlist 2>&1", "r")) == NULL) {
187 s = i18n ("SCSI subsystem could not be queried: /sbin/camcontrol could not be executed");
188 olditem = new Q3ListViewItem(lbox, olditem, s);
189 } else {
191 /* This prints out a list of all the scsi devies, perhaps eventually we could
192 parse it as opposed to schlepping it into a listbox */
194 t = new QTextStream(pipe, QIODevice::ReadOnly);
196 while (true) {
197 s = t->readLine();
198 if ( s.isEmpty() )
199 break;
200 olditem = new Q3ListViewItem(lbox, olditem, s);
203 delete t;
204 pclose(pipe);
207 delete camcontrol;
209 if (!lbox->childCount())
210 return false;
212 return true;
215 bool GetInfo_PCI (Q3ListView *lbox)
217 FILE *pipe;
218 QFile *pcicontrol;
219 QString s, cmd;
220 Q3ListViewItem *olditem = 0;
222 pcicontrol = new QFile("/usr/sbin/pciconf");
224 if (!pcicontrol->exists()) {
225 delete pcicontrol;
226 pcicontrol = new QFile("/usr/X11R6/bin/scanpci");
227 if (!pcicontrol->exists()) {
228 delete pcicontrol;
229 pcicontrol = new QFile("/usr/X11R6/bin/pcitweak");
230 if (!pcicontrol->exists()) {
231 QString s;
232 s = i18n("Could not find any programs with which to query your system's PCI information");
233 (void) new Q3ListViewItem(lbox, 0, s);
234 delete pcicontrol;
235 return true;
236 } else {
237 cmd = "/usr/X11R6/bin/pcitweak -l 2>&1";
239 } else {
240 cmd = "/usr/X11R6/bin/scanpci";
242 } else {
243 cmd = "/usr/sbin/pciconf -l -v 2>&1";
245 delete pcicontrol;
247 if ((pipe = popen(cmd.toLatin1(), "r")) == NULL) {
248 s = i18n ("PCI subsystem could not be queried: %1 could not be executed", cmd);
249 olditem = new Q3ListViewItem(lbox, olditem, s);
250 } else {
252 /* This prints out a list of all the pci devies, perhaps eventually we could
253 parse it as opposed to schlepping it into a listbox */
255 pclose(pipe);
256 GetInfo_ReadfromPipe(lbox, cmd.toLatin1(), true);
259 if (!lbox->childCount()) {
260 s = i18n("The PCI subsystem could not be queried, this may need root privileges.");
261 olditem = new Q3ListViewItem(lbox, olditem, s);
262 return true;
265 return true;
268 bool GetInfo_Partitions (Q3ListView *lbox)
270 struct fstab *fstab_ent;
272 if (setfsent() != 1) /* Try to open fstab */ {
273 int s_err = errno;
274 QString s;
275 s = i18n("Could not check filesystem info: ");
276 s += strerror(s_err);
277 (void)new Q3ListViewItem(lbox, 0, s);
278 } else {
279 lbox->addColumn(i18n("Device"));
280 lbox->addColumn(i18n("Mount Point"));
281 lbox->addColumn(i18n("FS Type"));
282 lbox->addColumn(i18n("Mount Options"));
284 while ((fstab_ent=getfsent())!=NULL) {
285 new Q3ListViewItem(lbox, fstab_ent->fs_spec,
286 fstab_ent->fs_file, fstab_ent->fs_vfstype,
287 fstab_ent->fs_mntops);
290 lbox->setSorting(0);
291 lbox->header()->setClickEnabled(true);
293 endfsent(); /* Close fstab */
295 return true;
298 bool GetInfo_XServer_and_Video (Q3ListView *lBox)
300 return GetInfo_XServer_Generic( lBox );
303 bool GetInfo_Devices (Q3ListView *lbox)
305 QFile *f = new QFile("/var/run/dmesg.boot");
306 if (f->open(QIODevice::ReadOnly)) {
307 QTextStream qts(f);
308 Q3Dict<Q3ListViewItem> lv_items;
309 Device *dev;
310 QString line, controller;
311 lbox->setRootIsDecorated(true);
312 lbox->addColumn("Device");
313 lbox->addColumn("Description");
314 while ( !(line=qts.readLine()).isNull() ) {
315 controller = GetController(line);
316 if (controller.isNull())
317 continue;
318 dev=GetDevice(line);
319 if (!dev)
320 continue;
321 // Ewww assuing motherboard is the only toplevel controller is rather gross
322 if (controller == "motherboard") {
323 if (!lv_items[dev->name]) {
324 lv_items.insert(dev->name, new Q3ListViewItem(lbox, dev->name, dev->description) );
326 } else {
327 Q3ListViewItem *parent=lv_items[controller];
328 if (parent && !lv_items[dev->name]) {
329 lv_items.insert(dev->name, new Q3ListViewItem(parent, dev->name, dev->description) );
333 return true;
335 return false;
338 QString GetController(const QString &line)
340 if ( ( (line.startsWith("ad")) || (line.startsWith("afd")) || (line.startsWith("acd")) ) && (line.find(":") < 6) ) {
341 QString controller = line;
342 controller.remove(0, controller.find(" at ")+4);
343 if (controller.find("-slave") != -1) {
344 controller.remove(controller.find("-slave"), controller.length());
345 } else if (controller.find("-master") != -1) {
346 controller.remove(controller.find("-master"), controller.length());
347 } else
348 controller=QString();
349 if (!controller.isNull())
350 return controller;
352 if (line.find(" on ") != -1) {
353 QString controller;
354 controller = line;
355 controller.remove(0, controller.find(" on ")+4);
356 if (controller.find(" ") != -1)
357 controller.remove(controller.find(" "), controller.length());
358 return controller;
360 return QString();
363 Device *GetDevice(const QString &line)
365 Device *dev;
366 int colon = line.find(":");
367 if (colon == -1)
368 return 0;
369 dev = new Device;
370 dev->name = line.mid(0, colon);
371 dev->description = line.mid(line.find("<")+1, line.length());
372 dev->description.remove(dev->description.find(">"), dev->description.length());
373 return dev;
376 #ifdef HAVE_DEVINFO_H
378 int print_irq(struct devinfo_rman *rman, void *arg)
380 Q3ListView *lbox = (Q3ListView *)arg;
381 if (strcmp(rman->dm_desc, "Interrupt request lines")==0) {
382 (void)new Q3ListViewItem(lbox, 0, rman->dm_desc);
383 devinfo_foreach_rman_resource(rman, print_resource, arg);
385 return(0);
388 int print_dma(struct devinfo_rman *rman, void *arg)
390 Q3ListView *lbox = (Q3ListView *)arg;
391 if (strcmp(rman->dm_desc, "DMA request lines")==0) {
392 (void)new Q3ListViewItem(lbox, lbox->lastItem(), rman->dm_desc);
393 devinfo_foreach_rman_resource(rman, print_resource, arg);
395 return(0);
398 int print_ioports(struct devinfo_rman *rman, void *arg)
400 Q3ListView *lbox = (Q3ListView *)arg;
402 if (strcmp(rman->dm_desc, "I/O ports")==0) {
403 (void)new Q3ListViewItem(lbox, lbox->lastItem(), rman->dm_desc);
404 devinfo_foreach_rman_resource(rman, print_resource, arg);
406 else if (strcmp(rman->dm_desc, "I/O memory addresses")==0) {
407 (void)new Q3ListViewItem(lbox, lbox->lastItem(), rman->dm_desc);
408 devinfo_foreach_rman_resource(rman, print_resource, arg);
410 return(0);
413 int print_resource(struct devinfo_res *res, void *arg)
415 struct devinfo_dev *dev;
416 struct devinfo_rman *rman;
417 int hexmode;
419 Q3ListView *lbox;
421 lbox = (Q3ListView *)arg;
423 QString s, tmp;
425 rman = devinfo_handle_to_rman(res->dr_rman);
426 hexmode = (rman->dm_size > 100) || (rman->dm_size == 0);
427 tmp.sprintf(hexmode ? "0x%lx" : "%lu", res->dr_start);
428 s += tmp;
429 if (res->dr_size > 1) {
430 tmp.sprintf(hexmode ? "-0x%lx" : "-%lu",
431 res->dr_start + res->dr_size - 1);
432 s += tmp;
435 dev = devinfo_handle_to_device(res->dr_device);
436 if ((dev != NULL) && (dev->dd_name[0] != 0)) {
437 tmp.sprintf(" (%s)", dev->dd_name);
438 } else {
439 tmp.sprintf(" ----");
441 s += tmp;
443 (void)new Q3ListViewItem(lbox, lbox->lastItem(), s);
444 return(0);
447 #endif