add more spacing
[personal-kdebase.git] / apps / kinfocenter / base / info_openbsd.cpp
blob8d9015dec85f113de04b1651c1ba9f6c2ec9cc0e
1 /*
2 * info_netbsd.cpp is part of the KDE program kcminfo. This displays
3 * various information about the OpenBSD system it's running on.
5 * Originally written by Jaromir Dolecek <dolecek@ics.muni.cz>. CPU info
6 * code has been imported from implementation of processor.cpp for KDE 1.0
7 * by David Brownlee <abs@NetBSD.org> as found in NetBSD packages collection.
8 * Hubert Feyer <hubertf@NetBSD.org> enhanced the sound information printing
9 * quite a lot, too.
11 * The code is placed into public domain. Do whatever you want with it.
15 * all following functions should return true, when the Information
16 * was filled into the QTreeWidget. Returning false indicates that
17 * information was not available.
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/sysctl.h>
23 #include <stdio.h> /* for NULL */
24 #include <stdlib.h> /* for malloc(3) */
26 #include <QFile>
27 #include <QFontMetrics>
28 #include <QTextStream>
29 #include <QTreeWidgetItemIterator>
31 #include <kdebug.h>
33 typedef struct {
34 int string;
35 int name;
36 const char *title;
37 } hw_info_mib_list_t;
39 bool GetInfo_CPU(QTreeWidget* tree) {
40 static hw_info_mib_list_t hw_info_mib_list[]= { { 1, HW_MODEL, "Model" }, { 1, HW_MACHINE, "Machine" }, { 0, HW_NCPU, "Number of CPUs" }, { 0, HW_PAGESIZE, "Pagesize" }, { 0, 0, 0 } };
41 hw_info_mib_list_t *hw_info_mib;
43 int mib[2], num;
44 char *buf;
45 size_t len;
46 QString value;
48 QStringList headers;
49 headers << i18n("Information") << i18n("Value");
50 tree->setHeaderLabels(headers);
52 for (hw_info_mib = hw_info_mib_list; hw_info_mib->title; ++hw_info_mib) {
53 mib[0] = CTL_HW;
54 mib[1] = hw_info_mib->name;
55 if (hw_info_mib->string) {
56 sysctl(mib, 2, NULL, &len, NULL, 0);
57 if ( (buf = (char*)malloc(len))) {
58 sysctl(mib, 2, buf, &len, NULL, 0);
59 value = QString::fromLocal8Bit(buf);
60 free(buf);
61 } else {
62 value = QString("Unknown");
64 } else {
65 len = sizeof(num);
66 sysctl(mib, 2, &num, &len, NULL, 0);
67 value.sprintf("%d", num);
70 QStringList list;
71 list << hw_info_mib->title << value;
72 new QTreeWidgetItem(tree, list);
75 return true;
78 // this is used to find out which devices are currently
79 // on system
80 static bool GetDmesgInfo(QTreeWidget* tree, const char *filter, void func(QTreeWidget *, QString s, void **, bool)) {
81 QFile *dmesg = new QFile("/var/run/dmesg.boot");
82 bool usepipe=false;
83 FILE *pipe=NULL;
84 QTextStream *t;
85 bool seencpu=false;
86 void *opaque=NULL;
87 QString s;
88 bool found=false;
90 if (dmesg->exists() && dmesg->open(QIODevice::ReadOnly)) {
91 t = new QTextStream(dmesg);
92 } else {
93 delete dmesg;
94 pipe = popen("/sbin/dmesg", "r");
95 if (!pipe)
96 return false;
97 usepipe = true;
98 t = new QTextStream(pipe, QIODevice::ReadOnly);
101 while (!(s = t->readLine()).isNull()) {
102 if (!seencpu) {
103 if (s.contains("cpu"))
104 seencpu = true;
105 else
106 continue;
108 if (s.contains("boot device") || s.contains("WARNING: old BSD partition ID!"))
109 break;
111 if (!filter || s.contains(filter)) {
112 if (func) {
113 func(tree, s, &opaque, false);
114 } else {
115 QStringList list;
116 list << s;
117 new QTreeWidgetItem(tree, list);
119 found = true;
122 if (func) {
123 func(tree, s, &opaque, true);
125 //tree->triggerUpdate();
127 delete t;
128 if (pipe) {
129 pclose(pipe);
130 } else {
131 dmesg->close();
132 delete dmesg;
135 return found;
138 void AddIRQLine(QTreeWidget* tree, QString s, void **opaque, bool ending) {
139 QStringList *strlist = (QStringList *) *opaque;
140 QString str;
141 int pos, irqnum=0;
142 const char *p;
143 p = s.toLatin1();
145 if (!strlist) {
146 strlist = new QStringList();
147 *opaque = (void *) strlist;
149 if (ending) {
150 foreach(QString temp, *strlist) {
151 QStringList tempList;
152 tempList << temp;
153 new QTreeWidgetItem(tree, tempList);
155 delete strlist;
156 return;
159 pos = s.find(" irq ");
160 irqnum = (pos < 0) ? 0 : atoi(&p[pos+5]);
161 if (irqnum) {
162 s.sprintf("%02d%s", irqnum, p);
163 } else {
164 s.sprintf("??%s", p);
166 strlist->append(s);
167 strlist->sort();
170 bool GetInfo_IRQ(QTreeWidget* tree) {
171 QStringList headers;
172 headers << i18n("IRQ") << i18n("Device");
173 tree->setHeaderLabels(headers);
174 (void) GetDmesgInfo(tree, " irq ", AddIRQLine);
175 return true;
178 bool GetInfo_DMA(QTreeWidget *) {
179 return false;
182 bool GetInfo_PCI(QTreeWidget* tree) {
183 if (!GetDmesgInfo(tree, "at pci", NULL)) {
184 QStringList list;
185 list << i18n("No PCI devices found.");
186 new QTreeWidgetItem(tree, list);
188 return true;
191 bool GetInfo_IO_Ports(QTreeWidget* tree) {
192 if (!GetDmesgInfo(tree, "port 0x", NULL)) {
193 QStringList list;
194 list << i18n("No I/O port devices found.");
195 new QTreeWidgetItem(tree, list);
197 return true;
200 bool GetInfo_Sound(QTreeWidget* tree) {
201 if (!GetDmesgInfo(tree, "audio", NULL)) {
202 QStringList list;
203 list << i18n("No audio devices found.");
204 new QTreeWidgetItem(tree, list);
207 // append information on any audio devices found
208 QTreeWidgetItemIterator it(tree, QTreeWidgetItemIterator::All);
209 while ( *it != NULL ) {
210 QTreeWidgetItem* lvitem = *it;
212 QString s;
213 int pos, len;
214 const char *start, *end;
215 char *dev;
217 s = lvitem->text(0);
218 if ((pos = s.find("at ")) >= 0) {
219 pos += 3; // skip "at "
220 start = end = s.toAscii();
221 for (; *end && (*end!=':') && (*end!='\n'); end++)
223 len = end - start;
224 dev = (char *) malloc(len + 1);
225 strncpy(dev, start, len);
226 dev[len] = '\0';
228 GetDmesgInfo(tree, dev, NULL);
230 free(dev);
233 ++it;
236 return true;
239 bool GetInfo_Devices(QTreeWidget* tree) {
240 (void) GetDmesgInfo(tree, NULL, NULL);
241 return true;
244 bool GetInfo_SCSI(QTreeWidget* tree) {
245 if (!GetDmesgInfo(tree, "scsibus", NULL)) {
246 QStringList list;
247 list << i18n("No SCSI devices found.");
248 new QTreeWidgetItem(tree, list);
250 return true;
253 bool GetInfo_Partitions(QTreeWidget* tree) {
254 QString s;
255 char *line, *orig_line;
256 const char *device, *mountpoint, *type, *flags;
257 FILE *pipe = popen("/sbin/mount", "r");
258 QTextStream *t;
260 if (!pipe) {
261 kError(0) << i18n("Unable to run /sbin/mount.") << endl;
262 return false;
264 t = new QTextStream(pipe, QIODevice::ReadOnly);
266 QStringList headers;
267 headers << i18n("Device") << i18n("Mount Point") << i18n("FS Type") << i18n("Mount Options");
268 tree->setHeaderLabels(headers);
270 while (!(s = t->readLine()).isNull()) {
271 orig_line = line = strdup(s.toLatin1());
273 device = strsep(&line, " ");
275 (void) strsep(&line, " "); // consume word "on"
276 mountpoint = strsep(&line, " ");
278 (void) strsep(&line, " "); // consume word "type"
279 type = strsep(&line, " ");
281 flags = line;
283 QStringList mountList;
284 mountList << device << mountpoint << type << flags;
285 new QTreeWidgetItem(tree, mountList);
287 free(orig_line);
290 delete t;
291 pclose(pipe);
292 return true;
295 bool GetInfo_XServer_and_Video(QTreeWidget* tree) {
296 return GetInfo_XServer_Generic(tree);