~ubuntu-branches/ubuntu/hardy/kdebase-workspace/hardy

« back to all changes in this revision

Viewing changes to kcontrol/infocenter/info/info_netbsd.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-03-03 11:37:30 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080303113730-ppdchskh93rr77le
Tags: 4:4.0.2-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * info_netbsd.cpp is part of the KDE program kcminfo.  This displays
3
 
 * various information about the NetBSD system it's running on.
4
 
 *
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.
10
 
 *
11
 
 * The code is placed into public domain. Do whatever you want with it.
12
 
 */
13
 
 
14
 
#define INFO_CPU_AVAILABLE
15
 
#define INFO_IRQ_AVAILABLE
16
 
#define INFO_DMA_AVAILABLE
17
 
#define INFO_PCI_AVAILABLE
18
 
#define INFO_IOPORTS_AVAILABLE
19
 
#define INFO_SOUND_AVAILABLE
20
 
#define INFO_DEVICES_AVAILABLE
21
 
#define INFO_SCSI_AVAILABLE
22
 
#define INFO_PARTITIONS_AVAILABLE
23
 
#define INFO_XSERVER_AVAILABLE
24
 
 
25
 
 
26
 
/*
27
 
 * all following functions should return true, when the Information
28
 
 * was filled into the lBox-Widget. Returning false indicates that
29
 
 * information was not available.
30
 
 */
31
 
 
32
 
#include <sys/types.h>
33
 
#include <sys/param.h>
34
 
#include <sys/sysctl.h>
35
 
#include <sys/mount.h>
36
 
#include <stdio.h>      /* for NULL */
37
 
#include <stdlib.h>     /* for malloc(3) */
38
 
#include <fstab.h>
39
 
 
40
 
#include <QFile>
41
 
#include <QFontMetrics>
42
 
#include <Q3StrList>
43
 
#include <QTextStream>
44
 
 
45
 
#include <kdebug.h>
46
 
#include <kio/global.h> /* for KIO::convertSize() */
47
 
 
48
 
typedef struct
49
 
  {
50
 
  int   string;
51
 
  int   name;
52
 
  const char    *title;
53
 
  } hw_info_mib_list_t;
54
 
 
55
 
bool GetInfo_CPU(Q3ListView *lBox)
56
 
{
57
 
  static hw_info_mib_list_t hw_info_mib_list[]= {
58
 
        { 1, HW_MODEL,          "Model" },
59
 
        { 1, HW_MACHINE,        "Machine" },
60
 
        { 1, HW_MACHINE_ARCH,   "Architecture" },
61
 
        { 0, HW_NCPU,           "Number of CPUs" },
62
 
        { 0, HW_PAGESIZE,       "Pagesize" },
63
 
        { 0,0,0 }
64
 
        };
65
 
  hw_info_mib_list_t *hw_info_mib;
66
 
 
67
 
  int mib[2], num;
68
 
  char *buf;
69
 
  size_t len;
70
 
  QString value;
71
 
 
72
 
  lBox->addColumn(i18n("Information"));
73
 
  lBox->addColumn(i18n("Value"));
74
 
 
75
 
  for ( hw_info_mib = hw_info_mib_list ;  hw_info_mib->title ; ++hw_info_mib )
76
 
  {
77
 
        mib[0] = CTL_HW;
78
 
        mib[1] = hw_info_mib->name;
79
 
        if ( hw_info_mib->string ) {
80
 
                sysctl(mib,2,NULL,&len,NULL,0);
81
 
                if ( (buf = (char*)malloc(len)) ) {
82
 
                        sysctl(mib,2,buf,&len,NULL,0);
83
 
                        value = QString::fromLocal8Bit(buf);
84
 
                        free(buf);
85
 
                }
86
 
                else {
87
 
                        value = QString("Unknown");
88
 
                }
89
 
        }
90
 
        else {
91
 
                len = sizeof(num);
92
 
                sysctl(mib,2,&num,&len,NULL,0);
93
 
                value = QString::number(num);
94
 
        }
95
 
        new Q3ListViewItem(lBox, hw_info_mib->title, value);
96
 
   }
97
 
 
98
 
   return true;
99
 
}
100
 
 
101
 
// this is used to find out which devices are currently
102
 
// on system
103
 
static bool GetDmesgInfo(Q3ListView *lBox, const char *filter,
104
 
        void func(Q3ListView *, QString s))
105
 
{
106
 
        QFile *dmesg = new QFile("/var/run/dmesg.boot");
107
 
        bool usepipe = false;
108
 
        FILE *pipe = NULL;
109
 
        QTextStream *t;
110
 
        bool seencpu = false;
111
 
        QString s;
112
 
        bool found = false;
113
 
 
114
 
        if (dmesg->exists() && dmesg->open(QIODevice::ReadOnly)) {
115
 
                t = new QTextStream(dmesg);
116
 
        }
117
 
        else {
118
 
                delete dmesg;
119
 
                pipe = popen("/sbin/dmesg", "r");
120
 
                if (!pipe) return false;
121
 
                usepipe = true;
122
 
                t = new QTextStream(pipe, QIODevice::ReadOnly);
123
 
        }
124
 
        Q3ListViewItem *olditem = NULL;
125
 
        while(!(s = t->readLine().toLocal8Bit()).isNull()) {
126
 
                if (!seencpu) {
127
 
                        if (s.contains("cpu"))
128
 
                                seencpu = true;
129
 
                        else
130
 
                                continue;
131
 
                }
132
 
                if (s.contains("boot device") ||
133
 
                        s.contains("WARNING: old BSD partition ID!"))
134
 
                        break;
135
 
 
136
 
                if (!filter || s.contains(QRegExp(filter))) {
137
 
                        if (func)
138
 
                                func(lBox, s);
139
 
                        else
140
 
                                olditem = new Q3ListViewItem(lBox, olditem, s);
141
 
                        found = true;
142
 
                }
143
 
        }
144
 
 
145
 
        delete t;
146
 
        if (pipe)
147
 
                pclose(pipe);
148
 
        else {
149
 
                dmesg->close();
150
 
                delete dmesg;
151
 
        }
152
 
 
153
 
        return found;
154
 
}
155
 
 
156
 
 
157
 
void
158
 
AddIRQLine(Q3ListView *lBox, QString s)
159
 
{
160
 
        int irqnum;
161
 
        QString s2;
162
 
        char numstr[3];
163
 
        bool ok;
164
 
 
165
 
        s2 = s.mid(s.indexOf(QRegExp("[ (]irq "))+5);
166
 
        irqnum = s2.remove(QRegExp("[^0-9].*")).toInt(&ok);
167
 
        if (ok)
168
 
                snprintf(numstr, 3, "%02d", irqnum);
169
 
        else {
170
 
                // this should never happen
171
 
                strcpy(numstr, "??");
172
 
        }
173
 
 
174
 
        new Q3ListViewItem(lBox, numstr, s);
175
 
}
176
 
 
177
 
bool GetInfo_IRQ (Q3ListView *lBox)
178
 
{
179
 
        lBox->addColumn(i18n("IRQ"));
180
 
        lBox->addColumn(i18n("Device"));
181
 
        lBox->setSorting(0);
182
 
        lBox->setShowSortIndicator(false);
183
 
        (void) GetDmesgInfo(lBox, "[ (]irq ", AddIRQLine);
184
 
        return true;
185
 
}
186
 
 
187
 
bool GetInfo_DMA (Q3ListView *)
188
 
{
189
 
        return false;
190
 
}
191
 
 
192
 
bool GetInfo_PCI (Q3ListView *lbox)
193
 
{
194
 
        if (!GetDmesgInfo(lbox, "at pci", NULL))
195
 
                new Q3ListViewItem(lbox, i18n("No PCI devices found."));
196
 
        return true;
197
 
}
198
 
 
199
 
bool GetInfo_IO_Ports (Q3ListView *lbox)
200
 
{
201
 
        if (!GetDmesgInfo(lbox, "port 0x", NULL))
202
 
                new Q3ListViewItem(lbox, i18n("No I/O port devices found."));
203
 
        return true;
204
 
}
205
 
 
206
 
bool GetInfo_Sound (Q3ListView *lbox)
207
 
{
208
 
        lbox->setSorting(false);
209
 
 
210
 
        if (!GetDmesgInfo(lbox, "audio", NULL))
211
 
                new Q3ListViewItem(lbox, i18n("No audio devices found."));
212
 
 
213
 
        // append information for each audio devices found
214
 
        Q3ListViewItem *lvitem = lbox->firstChild();
215
 
        for(; lvitem; lvitem = lvitem->nextSibling()) {
216
 
                QString s, s2;
217
 
                int pos;
218
 
                char *dev;
219
 
 
220
 
                s = lvitem->text(0);
221
 
                // The autoconf message is in form 'audio0 at auvia0: ...'
222
 
                if (s.find("audio") == 0 && (pos = s.find(" at ")) > 0) {
223
 
                        s2 = s.mid(pos+4); // skip " at "
224
 
                        s2.remove(QRegExp("[:\n\t ].*"));
225
 
                        dev = strdup(s2.toAscii().data());
226
 
 
227
 
                        GetDmesgInfo(lbox, dev, NULL);
228
 
 
229
 
                        free(dev);
230
 
                }
231
 
        }
232
 
 
233
 
        return true;
234
 
}
235
 
 
236
 
bool GetInfo_Devices (Q3ListView *lBox)
237
 
{
238
 
        (void) GetDmesgInfo(lBox, NULL, NULL);
239
 
        return true;
240
 
}
241
 
 
242
 
bool GetInfo_SCSI (Q3ListView *lbox)
243
 
{
244
 
        if (!GetDmesgInfo(lbox, "scsibus", NULL))
245
 
                new Q3ListViewItem(lbox, i18n("No SCSI devices found."));
246
 
 
247
 
        // remove the 'waiting %d seconds for devices to settle' message
248
 
        Q3ListViewItem *lvitem = lbox->firstChild();
249
 
        for(; lvitem; lvitem = lvitem->nextSibling()) {
250
 
                QString s = lvitem->text(0);
251
 
 
252
 
                if (s.contains("seconds for devices to settle")) {
253
 
                        lbox->removeItem(lvitem);
254
 
                        break;
255
 
                }
256
 
        }
257
 
        
258
 
        return true;
259
 
}
260
 
 
261
 
bool GetInfo_Partitions (Q3ListView *lbox)
262
 
{
263
 
        int num; // number of mounts
264
 
#ifdef HAVE_STATVFS
265
 
        struct statvfs *mnt; // mount data pointer
266
 
#else
267
 
        struct statfs *mnt; // mount data pointer
268
 
#endif
269
 
 
270
 
        // get mount info
271
 
        if (!(num=getmntinfo(&mnt, MNT_WAIT))) {
272
 
                kError() << "getmntinfo failed" << endl;
273
 
                return false;
274
 
        }
275
 
 
276
 
        // table headers
277
 
        lbox->addColumn(i18n("Device"));
278
 
        lbox->addColumn(i18n("Mount Point"));
279
 
        lbox->addColumn(i18n("FS Type"));
280
 
        lbox->addColumn(i18n("Total Size"));
281
 
        lbox->addColumn(i18n("Free Size"));
282
 
        lbox->addColumn(i18n("Total Nodes"));
283
 
        lbox->addColumn(i18n("Free Nodes"));
284
 
        lbox->addColumn(i18n("Flags"));
285
 
 
286
 
        // mnt points into a static array (no need to free it)
287
 
        for(; num--; ++mnt) {
288
 
                unsigned long long big[2];
289
 
                QString vv[5];
290
 
 
291
 
#ifdef HAVE_STATVFS
292
 
                big[0] = big[1] = mnt->f_frsize; // coerce the product
293
 
#else
294
 
                big[0] = big[1] = mnt->f_bsize; // coerce the product
295
 
#endif
296
 
                big[0] *= mnt->f_blocks;
297
 
                big[1] *= mnt->f_bavail; // FIXME: use f_bfree if root?
298
 
 
299
 
                // convert to strings
300
 
                vv[0] = KIO::convertSize(big[0]);
301
 
                vv[1] = QString("%1 (%2%)")
302
 
                                .arg(KIO::convertSize(big[1]))
303
 
                                .arg(mnt->f_blocks ? mnt->f_bavail*100/mnt->f_blocks : 0);
304
 
 
305
 
                vv[2] = QString("%L1").arg(mnt->f_files);
306
 
                vv[3] = QString("%L1 (%2%) ")
307
 
                                .arg(mnt->f_ffree)
308
 
                                .arg(mnt->f_files ? mnt->f_ffree*100/mnt->f_files : 0);
309
 
 
310
 
                vv[4].clear();
311
 
#ifdef HAVE_STATVFS
312
 
#define MNTF(x) if (mnt->f_flag & ST_##x) vv[4] += QLatin1String(#x " ");
313
 
#else
314
 
#define MNTF(x) if (mnt->f_flags & MNT_##x) vv[4] += QLatin1String(#x " ");
315
 
#endif
316
 
                MNTF(ASYNC)
317
 
                MNTF(DEFEXPORTED)
318
 
                MNTF(EXKERB)
319
 
                MNTF(EXNORESPORT)
320
 
                MNTF(EXPORTANON)
321
 
                MNTF(EXPORTED)
322
 
                MNTF(EXPUBLIC)
323
 
                MNTF(EXRDONLY)
324
 
#ifndef HAVE_STATVFS
325
 
                MNTF(IGNORE)
326
 
#endif
327
 
                MNTF(LOCAL)
328
 
                MNTF(NOATIME)
329
 
                MNTF(NOCOREDUMP)
330
 
                MNTF(NODEV)
331
 
                MNTF(NODEVMTIME)
332
 
                MNTF(NOEXEC)
333
 
                MNTF(NOSUID)
334
 
                MNTF(QUOTA)
335
 
                MNTF(RDONLY)
336
 
                MNTF(ROOTFS)
337
 
                MNTF(SOFTDEP)
338
 
                MNTF(SYMPERM)
339
 
                MNTF(SYNCHRONOUS)
340
 
                MNTF(UNION)
341
 
#undef MNTF
342
 
 
343
 
                // put it in the table
344
 
                // FIXME: there're more data but we have limited args (this is wrong! just add!)
345
 
                new Q3ListViewItem(lbox,
346
 
                        // FIXME: names need pad space
347
 
                        mnt->f_mntfromname,
348
 
                        mnt->f_mntonname,
349
 
                        mnt->f_fstypename,
350
 
                        vv[0], vv[1], vv[2], vv[3], vv[4]);
351
 
        }
352
 
 
353
 
        // job well done
354
 
        return true;
355
 
}
356
 
 
357
 
bool GetInfo_XServer_and_Video (Q3ListView *lBox)
358
 
{
359
 
        return GetInfo_XServer_Generic( lBox );
360
 
}