summaryrefslogtreecommitdiffstats
path: root/kpersonalizer/ksysinfo.cpp
blob: 551dad3f8dccc112d737f4eb0c8ba7a702aa76e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***************************************************************************
                          ksysinfo.cpp  -  description
                             -------------------
    begin                : Don Jul 11 2002
    copyright            : (C) 2002 by Carsten Wolff, Christoph Held
    email                :             wolff@kde.org, c-held@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/*
 * Code partly taken from kcontrol/info and kcontrol/fonts
 */

#include <tqfontdatabase.h>
#include <tqfont.h>
#include <tqstring.h>
#include <tqstringlist.h>

#include <kdebug.h>

#include <X11/Xlib.h>

#include "ksysinfo.h"

KSysInfo::KSysInfo() {
	m_fdb = new TQFontDatabase();
	initXInfo();
	initFontFamilies();
	initHWInfo();
	kdDebug() << "KSysInfo: XServer Vendor: " << m_xvendor << endl
	          << "KSysInfo: XServer from XFree Inc: " << m_xfree_inc << endl
	          << "KSysInfo: XServer Release Number: " << m_xrelease << endl
	          << "KSysInfo: XRENDER support is: " << m_xrender << endl
	          << "KSysInfo: Chosen normal font: "<< m_normal_font << endl
	          << "KSysInfo: Chosen fixed width font: "<< m_fixed_font << endl
	          << "KSysInfo: CPU speed: " << m_cpu_speed << " MHz" << endl;
}

KSysInfo::~KSysInfo() {
	delete m_fdb;
}

/*
 * XServer - Info
 */

void KSysInfo::initXInfo() {
	Display *dpy = XOpenDisplay(0);
	// vendor
	m_xvendor = !dpy ? TQString::null : (TQString)ServerVendor(dpy);
	// XFree-Inc?
	m_xfree_inc = m_xvendor.contains("XFree86");
	// X.org ?
	m_xorg = m_xvendor.contains("X.Org");
	// release-number
	m_xrelease = !dpy ? 0 : VendorRelease(dpy);
	// RENDER-support
	m_xrender = false;
	int extCount;
	TQString extension;
	char **extensions = XListExtensions( dpy, &extCount );
	for (int i = 0; i < extCount; i++ ) {
		extension=TQString( extensions[i] );
		extension=extension.stripWhiteSpace();
		if (!extension.compare("RENDER"))
			m_xrender=true;
	}
	XFreeExtensionList(extensions);
	XCloseDisplay (dpy);
}

bool KSysInfo::isXfromXFreeInc() {
	return m_xfree_inc;
}

bool KSysInfo::isXfromXOrg() {
	return m_xorg;
}

int KSysInfo::getXRelease() {
	return m_xrelease;
}

bool KSysInfo::getRenderSupport(){
	return m_xrender;
}

/*
 * Font - Info
 */

void KSysInfo::initFontFamilies() {
	TQFontDatabase fdb;
	TQStringList families = fdb.families();
	m_normal_font = TQString::null;
	m_fixed_font = TQString::null;
	int normal_priority = 0, fixed_priority = 0;
	for (uint i=0; i < families.count(); i++) {
		TQString font = *families.at(i);
		//add further NORMAL fonts here
		if ( (font.contains("Arial [") || font=="Arial") && normal_priority < 15 ) {
			m_normal_font = font;
			normal_priority = 15;
		} else if ( font.contains("Vera Sans") && normal_priority < 12 ) {
			m_normal_font = font;
			normal_priority = 12;
		} else if ( (font.contains("Luxi Sans") || font.contains("Lucidux Sans")) && normal_priority < 10 ) {
			m_normal_font = font;
			normal_priority = 10;
		} else if ( font.contains("Helmet") && normal_priority < 7 ) {
			m_normal_font = font;
			normal_priority = 7;
		} else if ( font.contains("Nimbus Sans") && normal_priority < 5 ) {
			m_normal_font = font;
			normal_priority = 5;
		} else if ( font.contains("Sans") & m_fdb->isSmoothlyScalable(font) && normal_priority < 3 ) {
			m_normal_font = font;
			normal_priority = 3;
		} else if ( m_fdb->isSmoothlyScalable(font) && !(m_fdb->isFixedPitch(font,"Normal") && m_fdb->isFixedPitch(font,"Bold")) && normal_priority < 2) {
			m_normal_font = font;
			normal_priority = 2;
		} else if ( m_fdb->isSmoothlyScalable(font) && normal_priority < 1 ) {
			m_normal_font = font;
			normal_priority = 1;
		}
		//add further FIXED fonts here
		if (font.contains("Courier New") && fixed_priority < 15){
			m_fixed_font = font;
			fixed_priority = 15;
		} else if ( (font.contains("Luxi Mono") || font.contains("Lucidux Mono")) && fixed_priority < 10 ) {
			m_fixed_font = font;
			fixed_priority = 10;
		} else if (font.contains("Andale Mono") && fixed_priority < 5) {
			m_fixed_font = font;
			fixed_priority = 5;
		} else if ( font.contains("Mono") && m_fdb->isSmoothlyScalable(font) && fixed_priority < 3 ) {
			m_fixed_font = font;
			fixed_priority = 3;
		} else if ( m_fdb->isSmoothlyScalable(font) && m_fdb->isFixedPitch(font,"Normal") && fixed_priority < 2 ) {
			m_fixed_font = font;
			fixed_priority = 2;                        
		} else if ( m_fdb->isSmoothlyScalable(font) && fixed_priority < 1 ) {
			m_fixed_font = font;
			fixed_priority = 1;
		}
	}
}

TQFont KSysInfo::getNormalFont() {
	return m_fdb->font(m_normal_font,"Normal",12); // this will return the current font, if !m_normal_font
}

TQFont KSysInfo::getSmallFont(){
	return m_fdb->font(m_normal_font,"Normal",11);
}

TQFont KSysInfo::getBoldFont(){
	return m_fdb->font(m_normal_font,"Bold",12);
}

TQFont KSysInfo::getFixedWidthFont(){
	return m_fdb->font(m_fixed_font,"Normal",10);
}

/**
 * Hardware - Info
 * (Architecture - dependent code)
 */

///////////////////
#ifdef __linux__
///////////////////

	#include <tqfile.h>
	#include <math.h>

	void KSysInfo::initHWInfo() {
		char buf[512];
		TQFile *file = new TQFile("/proc/cpuinfo");

		m_cpu_speed = 0;

		if (!file->exists()) {  //CPU info file does not exist, use default value
			delete file;   //only the object :o)
			return;
		}

		if (!file->open(IO_ReadOnly)) {   //No read access, use default value
			delete file;
			return;
		}

		// File Parser
		while (file->readLine(buf, sizeof(buf) - 1) > 0) {
			TQString s1 = TQString::fromLocal8Bit(buf);
			TQString s2 = s1.mid(s1.find(":") + 1);
			s1.truncate(s1.find(":"));
			s1=s1.stripWhiteSpace();
			s2=s2.stripWhiteSpace();
			if(s1.contains("MHz")){
				float fspeed = s2.toFloat(0);
				fspeed = floor(fspeed);
				m_cpu_speed = (int)fspeed;
			}
		}
		delete file;
	}

///////////////////
//#elif sgi
///////////////////

///////////////////
//#elif __FreeBSD__
///////////////////

///////////////////
//#elif hpux
///////////////////

///////////////////
//#elif __NetBSD__
///////////////////

///////////////////
//#elif __OpenBSD__
///////////////////

///////////////////
#elif defined(Q_OS_SOLARIS)
///////////////////
#include <kstat.h>

	void KSysInfo::initHWInfo() {
		kstat_ctl_t	*kctl;
		kstat_t		*ksp;
		kstat_named_t	*kdata;

		m_cpu_speed = 0;
		/* cpu_info:0:cpu_info0:current_clock_Hz */

		kctl = kstat_open();
		if (kctl == NULL)
			return;
		if (kstat_chain_update(kctl) != 0)
			goto out;
		ksp = kstat_lookup(kctl, "cpu_info", 0, "cpu_info0");
		if (ksp == NULL)
			goto out;
		if (kstat_read(kctl, ksp, NULL) == -1 )
			goto out;

		kdata = (kstat_named_t *)kstat_data_lookup(ksp,
			"clock_Hz");
		if (kdata != NULL)
			m_cpu_speed = kdata->value.ui64;
		out:
		kstat_close(kctl);
	}

///////////////////
//#elif __svr4__
///////////////////

///////////////////
//#elif _AIX
///////////////////

///////////////////
#else
///////////////////

	void KSysInfo::initHWInfo() {
		m_cpu_speed = 0;
	}

#endif

int KSysInfo::getCpuSpeed() {
	return m_cpu_speed;
}