add more spacing
[personal-kdebase.git] / apps / kinfocenter / base / os_base.h
blobc7aff973eed9c2d93642a751785fc3451d3ae226
1 #ifndef OS_BASE_H_
2 #define OS_BASE_H_
4 #include <QString>
5 #include <QStringList>
7 #include <QProcess>
8 #include <QTextStream>
9 #include <QTreeWidget>
10 #include <QTreeWidgetItem>
12 #include <kiconloader.h>
13 #include <kglobal.h>
14 #include <klocale.h>
16 #include <X11/Xlib.h>
18 #include "config-infocenter.h"
20 static const QString Value(int val, int numbers=1) {
21 return KGlobal::locale()->formatNumber(val, 0).rightJustified(numbers);
24 static const QString HexStr(unsigned long val, int digits) {
25 QString hexstr;
26 int i;
27 hexstr = QString::fromLatin1("0x%1").arg(val, digits, 16/*=HEX*/);
28 for (i=hexstr.length()-1; i>0; --i)
29 if (hexstr[i]==' ')
30 hexstr[i] = '0';
31 return hexstr;
34 static const QString Order(int order) {
35 if (order==LSBFirst)
36 return i18n("LSBFirst");
37 else if (order==MSBFirst)
38 return i18n("MSBFirst");
39 else
40 return i18n("Unknown Order %1", order);
43 static const QString BitString(unsigned long n) {
44 return i18np("1 Bit", "%1 Bits", n); // singular & plural form of "%d Bit"
47 static const QString ByteString(unsigned long n) {
48 /* explanation in BR #52640 (http://bugs.kde.org/show_bug.cgi?id=52640) */
49 if (n == 1)
50 return i18nc("singular form: '1 Byte' (yes, it is '1', not '%1'!)", "1 Byte");
52 return i18nc("plural form: '%1 Bytes'", "%1 Bytes", KGlobal::locale()->formatNumber(n, 0));
55 static struct _event_table {
56 const char *name;
57 long value;
58 } event_table[] = { { "KeyPressMask", KeyPressMask }, { "KeyReleaseMask", KeyReleaseMask }, { "ButtonPressMask", ButtonPressMask }, { "ButtonReleaseMask", ButtonReleaseMask }, { "EnterWindowMask", EnterWindowMask }, { "LeaveWindowMask", LeaveWindowMask }, { "PointerMotionMask", PointerMotionMask },
59 { "PointerMotionHintMask", PointerMotionHintMask }, { "Button1MotionMask", Button1MotionMask }, { "Button2MotionMask", Button2MotionMask }, { "Button3MotionMask", Button3MotionMask }, { "Button4MotionMask", Button4MotionMask }, { "Button5MotionMask", Button5MotionMask }, {
60 "ButtonMotionMask", ButtonMotionMask }, { "KeymapStateMask", KeymapStateMask }, { "ExposureMask", ExposureMask }, { "VisibilityChangeMask", VisibilityChangeMask }, { "StructureNotifyMask", StructureNotifyMask }, { "ResizeRedirectMask", ResizeRedirectMask }, {
61 "SubstructureNotifyMask", SubstructureNotifyMask }, { "SubstructureRedirectMask", SubstructureRedirectMask }, { "FocusChangeMask", FocusChangeMask }, { "PropertyChangeMask", PropertyChangeMask }, { "ColormapChangeMask", ColormapChangeMask }, { "OwnerGrabButtonMask",
62 OwnerGrabButtonMask }, { 0L, 0 } };
64 /* easier to read with such a define ! */
65 #define I18N_MAX(txt,in,fm,maxw) \
66 { int n = fm.width(txt=in); if (n>maxw) maxw=n; }
68 #define PIXEL_ADD 20 // add x Pixel to multicolumns..
69 #define HEXDIGITS (sizeof(int)*8/4) /* 4 Bytes = 32 Bits = 8 Hex-Digits */
71 static QTreeWidgetItem* XServer_fill_screen_info(QTreeWidgetItem *lBox, QTreeWidgetItem *last, Display *dpy, int scr, int default_scr) {
72 unsigned width, height;
73 double xres, yres;
74 int i, ndepths, *depths;
75 Screen *s= ScreenOfDisplay(dpy,scr); /* opaque structure */
76 QTreeWidgetItem *item;
79 * there are 2.54 centimeters to an inch; so there are 25.4 millimeters.
81 * dpi = N pixels / (M millimeters / (25.4 millimeters / 1 inch))
82 * = N pixels / (M inch / 25.4)
83 * = N * 25.4 pixels / M inch
86 xres = ((double)(DisplayWidth(dpy,scr) *25.4)/DisplayWidthMM(dpy,scr) );
87 yres = ((double)(DisplayHeight(dpy,scr)*25.4)/DisplayHeightMM(dpy,scr));
89 QStringList screenList;
90 screenList << ki18n("Screen # %1").subs((int)scr,-1).toString();
91 if (scr==default_scr)
92 screenList << i18n("(Default Screen)");
93 else
94 screenList << QString();
96 item = new QTreeWidgetItem(lBox, screenList);
97 if (scr==default_scr)
98 item->setExpanded(true);
100 QStringList dimensionList;
101 dimensionList << i18n("Dimensions") << i18n("%1 x %2 Pixel (%3 x %4 mm)", (int)DisplayWidth(dpy,scr) , (int)DisplayHeight(dpy,scr) , (int)DisplayWidthMM(dpy,scr) , (int)DisplayHeightMM (dpy,scr) );
102 last = new QTreeWidgetItem(item, dimensionList);
104 QStringList resolutionList;
105 resolutionList << i18n("Resolution") << i18n("%1 x %2 dpi", (int)(xres+0.5) , (int)(yres+0.5) );
107 last = new QTreeWidgetItem(item, resolutionList);
109 ndepths = 0;
110 depths = 0;
111 depths = XListDepths(dpy, scr, &ndepths);
112 if (depths) {
113 QString txt;
115 for (i = 0; i < ndepths; i++) {
116 txt = txt + Value(depths[i]);
117 if (i < ndepths - 1)
118 txt = txt + QLatin1String(", ");
121 QStringList depthList;
122 depthList << ki18n("Depths (%1)").subs(ndepths,-1).toString() << txt;
123 last = new QTreeWidgetItem(item, depthList);
124 XFree((char *) depths);
127 QStringList rootList;
128 rootList << i18n("Root Window ID") << HexStr((unsigned long)RootWindow(dpy,scr), HEXDIGITS);
129 last = new QTreeWidgetItem(item, rootList);
131 QStringList depthRootList;
132 depthRootList << i18n("Depth of Root Window") << i18np("%1 plane", "%1 planes", DisplayPlanes(dpy,scr));
133 last = new QTreeWidgetItem(item, depthRootList);
135 QStringList colormapList;
136 colormapList << i18n("Number of Colormaps") << i18n("minimum %1, maximum %2", (int)MinCmapsOfScreen(s), (int)MaxCmapsOfScreen(s));
137 last = new QTreeWidgetItem(item, colormapList);
139 QStringList defaultColormap;
140 defaultColormap << i18n("Default Colormap"), Value((int)DefaultColormap(dpy,scr));
141 last = new QTreeWidgetItem(item, defaultColormap);
143 QStringList colormapCellList;
144 colormapCellList << i18n("Default Number of Colormap Cells") << Value((int)DisplayCells(dpy, scr));
145 last = new QTreeWidgetItem(item, colormapCellList);
147 QStringList preallocatedList;
148 preallocatedList << i18n("Preallocated Pixels") << i18n("Black %1, White %2", KGlobal::locale()->formatNumber(BlackPixel(dpy,scr), 0), KGlobal::locale()->formatNumber(WhitePixel(dpy,scr), 0));
149 last = new QTreeWidgetItem(item, preallocatedList);
151 QString YES(i18n("Yes"));
152 QString NO(i18n("No"));
154 QStringList optionList;
155 optionList << i18n("Options") << i18n("backing-store: %1, save-unders: %2", (DoesBackingStore(s) == NotUseful) ? NO : ((DoesBackingStore(s) == Always) ? YES : i18n("When mapped")),
156 DoesSaveUnders(s) ? YES : NO);
157 last = new QTreeWidgetItem(item, optionList);
159 XQueryBestSize(dpy, CursorShape, RootWindow(dpy,scr), 65535, 65535, &width, &height);
160 QStringList largestList;
161 largestList << i18n("Largest Cursor");
162 if (width == 65535 && height == 65535)
163 largestList << i18n("unlimited");
164 else
165 largestList << QString::fromLatin1("%1 x %2").arg(width).arg(height);
166 last = new QTreeWidgetItem(item, largestList);
168 QStringList currentInput;
169 currentInput << i18n("Current Input Event Mask") << HexStr((unsigned long)EventMaskOfScreen(s), HEXDIGITS);
170 last = new QTreeWidgetItem(item, currentInput);
171 struct _event_table *etp;
172 for (etp=event_table; etp->name; etp++) {
173 if (EventMaskOfScreen(s) & etp->value) {
174 QStringList eventList;
175 eventList << i18n("Event = %1", HexStr(etp->value, HEXDIGITS)) << etp->name;
176 new QTreeWidgetItem(last, eventList);
180 if (last->childCount()==0)
181 return last;
182 else
183 return last->child(last->childCount()-1);
186 static bool GetInfo_XServer_Generic(QTreeWidget *lBox) {
187 /* Many parts of this source are taken from the X11-program "xdpyinfo" */
189 int i, n;
190 long req_size;
192 Display *dpy;
193 XPixmapFormatValues *pmf;
195 QString str, txt;
196 QTreeWidgetItem *last, *item, *next;
198 dpy = XOpenDisplay(0);
199 if (!dpy)
200 return false;
202 QStringList headers;
203 headers << i18n("Information") << i18n("Value");
204 lBox->setHeaderLabels(headers);
205 lBox->setSortingEnabled(false);
207 QStringList serverInfo;
208 serverInfo << i18n("Server Information");
209 next = new QTreeWidgetItem(lBox, serverInfo);
210 next->setIcon(0, SmallIcon("xorg"));
211 next->setExpanded(true);
213 QStringList displayNameList;
214 displayNameList << i18n("Name of the Display") << DisplayString(dpy);
215 last = new QTreeWidgetItem(next, displayNameList);
217 QStringList vendorList;
218 vendorList << i18n("Vendor String") << QLatin1String(ServerVendor(dpy));
219 last = new QTreeWidgetItem(next, vendorList);
221 QStringList vendorReleaseList;
222 vendorReleaseList << i18n("Vendor Release Number") << Value((int)VendorRelease(dpy));
223 last = new QTreeWidgetItem(next, vendorReleaseList);
225 QStringList versionList;
226 versionList << i18n("Version Number") << QString::fromLatin1("%1.%2").arg((int)ProtocolVersion(dpy)).arg((int)ProtocolRevision(dpy));
227 last = new QTreeWidgetItem(next, versionList);
229 QStringList screenList;
230 screenList << i18n("Available Screens");
231 last = item = new QTreeWidgetItem(next, screenList);
232 last->setExpanded(true);
233 for (i=0; i<ScreenCount(dpy); i++) {
234 item = XServer_fill_screen_info(last, item, dpy, i, (int)DefaultScreen(dpy));
235 if (i==0)
236 item->setExpanded(true);
239 QStringList extensionList;
240 extensionList << i18n("Supported Extensions");
241 last = new QTreeWidgetItem(next, extensionList );
242 item = last;
244 int extCount;
245 char **extensions = XListExtensions(dpy, &extCount);
246 for (i = 0; i < extCount; i++) {
247 QStringList extensionInfoList;
248 extensionInfoList << QLatin1String(extensions[i]);
249 item = new QTreeWidgetItem( last, extensionInfoList );
251 XFreeExtensionList(extensions);
253 pmf = XListPixmapFormats(dpy, &n);
254 QStringList pixmapFormatList;
255 pixmapFormatList << i18n("Supported Pixmap Formats");
256 last = item = new QTreeWidgetItem(next, pixmapFormatList);
258 if (pmf) {
260 for (i=0; i<n; i++) {
261 QStringList pixmapList;
262 pixmapList << i18n("Pixmap Format #%1", i+1) << i18n("%1 BPP, Depth: %2, Scanline padding: %3", pmf[i].bits_per_pixel, BitString(pmf[i].depth), BitString(pmf[i].scanline_pad));
264 item = new QTreeWidgetItem(last, pixmapList);
266 XFree((char *)pmf);
269 req_size = XExtendedMaxRequestSize(dpy);
270 if (!req_size)
271 req_size = XMaxRequestSize(dpy);
273 QStringList requestSizeList;
274 requestSizeList << i18n("Maximum Request Size") << ByteString(req_size*4);
275 last = new QTreeWidgetItem(next, requestSizeList);
277 QStringList motionList;
278 motionList << i18n("Motion Buffer Size") << ByteString(XDisplayMotionBufferSize(dpy));
279 last = new QTreeWidgetItem(next, motionList);
281 QStringList bitmapList;
282 bitmapList << i18n("Bitmap");
283 last = item = new QTreeWidgetItem(next, bitmapList);
285 QStringList unitList;
286 unitList << i18n("Unit") << Value(BitmapUnit(dpy));
287 item = new QTreeWidgetItem(last, unitList);
289 QStringList orderList;
290 orderList << i18n("Order") << Order(BitmapBitOrder(dpy));
291 item = new QTreeWidgetItem(last, orderList);
293 QStringList paddingList;
294 paddingList << i18n("Padding") << Value(BitmapPad(dpy));
295 item = new QTreeWidgetItem(last, paddingList);
297 QStringList imageByteList;
298 imageByteList << i18n("Image Byte Order") << Order(ImageByteOrder(dpy));
299 last = new QTreeWidgetItem(next, imageByteList);
301 XCloseDisplay(dpy);
302 return true;
305 /* Helper-function to read output from an external program */
306 static int GetInfo_ReadfromPipe(QTreeWidget* tree, const char *FileName, bool WithEmptyLines = true) {
307 QProcess proc;
308 QTreeWidgetItem* olditem= NULL;
309 QString s;
311 proc.start(FileName, QIODevice::ReadOnly);
312 if (!proc.waitForFinished()) {
313 // Process hanged or did not start
314 return 0;
317 QTextStream t(&proc);
319 while (!t.atEnd()) {
320 s = t.readLine();
321 if (!WithEmptyLines && s.length()==0)
322 continue;
323 QStringList list;
324 list << s;
325 olditem = new QTreeWidgetItem(tree, list);
328 return tree->topLevelItemCount();
331 #endif /*OS_BASE_H_*/