-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxSysInfo.cpp
204 lines (171 loc) · 5.14 KB
/
LinuxSysInfo.cpp
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
#include "LinuxSysInfo.h"
#include "QDiskInfo.h"
#include <QString>
#include <fcntl.h>
#include <linux/fb.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <unistd.h>
#include <xcb/xcb.h>
const uint GB = 1024 * 1024 * 1024;
LinuxSysInfo::LinuxSysInfo() {}
LinuxSysInfo::~LinuxSysInfo() {}
QString LinuxSysInfo::cpuType()
{
m_cpuDescribe = "";
FILE *fp = fopen("/proc/cpuinfo", "r");
if (nullptr == fp)
return m_cpuDescribe;
char szTest[1000] = { 0 };
// read file line by line
while (!feof(fp))
{
memset(szTest, 0, sizeof(szTest));
fgets(szTest, sizeof(szTest) - 1, fp);
QString str(szTest);
if (str.contains("model name"))
{
QStringList list = str.split(":");
m_cpuDescribe = list.at(1);
break;
}
}
fclose(fp);
return m_cpuDescribe.trimmed();
}
QString LinuxSysInfo::displayCard()
{
m_cardDescribe = "";
QString strCommand = "lspci | grep -i vga";
const char *str = strCommand.toUtf8().data();
FILE *fp = popen(str, "r");
if (nullptr == fp)
return m_cardDescribe;
char name[512] = { 0 };
while (!feof(fp))
{
memset(name, 0, sizeof(name));
fgets(name, sizeof(name) - 1, fp);
QString strName(name);
if (strName.isEmpty())
return m_cardDescribe;
QStringList list = strName.split(":");
m_cardDescribe = list.last().trimmed();
}
return m_cardDescribe;
}
QString LinuxSysInfo::memory()
{
bool bTotal = false;
bool bFree = false;
m_memDescribe = "";
FILE *fp = fopen("/proc/meminfo", "r");
if (nullptr == fp)
return m_memDescribe;
char szTest[1000] = { 0 };
while (!feof(fp))
{
memset(szTest, 0, sizeof(szTest));
fgets(szTest, sizeof(szTest) - 1, fp);
QString str(szTest);
if (str.contains("MemTotal"))
{
m_totalMem = getValueByString(szTest);
bTotal = true;
}
else if (str.contains("MemFree"))
{
m_freeMem = getValueByString(szTest);
bFree = true;
}
if (bTotal && bFree)
{
m_memDescribe = QString("可用 %1 GB / 共 %2 GB").arg(QString::asprintf("%.2f", m_freeMem)).arg(QString::asprintf("%.2f", m_totalMem));
break;
}
}
fclose(fp);
return m_memDescribe;
}
QString LinuxSysInfo::osVersion()
{
m_osDescirbe = "";
FILE *fp = fopen("/proc/version_signature", "r");
if (nullptr == fp)
return m_osDescirbe;
char szTest[1000] = { 0 };
while (!feof(fp))
{
memset(szTest, 0, sizeof(szTest));
fgets(szTest, sizeof(szTest) - 1, fp); // leave out \n
m_osDescirbe = QString(szTest).trimmed();
break;
}
fclose(fp);
return m_osDescirbe;
}
QString LinuxSysInfo::screen()
{
QString screenInfo = "";
#if 0
int i, screenNum;
xcb_connection_t *connection = xcb_connect(nullptr, &screenNum);
/* Get the screen whose number is screenNum */
const xcb_setup_t *setup = xcb_get_setup(connection);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
// we want the screen at index screenNum of the iterator
for (i = 0; i < screenNum; ++i)
{
xcb_screen_next(&iter);
}
xcb_screen_t *screen = iter.data;
/* report */
qDebug("Informations of screen %u:\n", screen->root);
qDebug(" width.........: %d\n", screen->width_in_pixels);
qDebug(" height........: %d\n", screen->height_in_pixels);
qDebug(" white pixel...: %u\n", screen->white_pixel);
qDebug(" black pixel...: %u\n", screen->black_pixel);
screenInfo = QString("%1像素 x %2像素) x %3个").arg(screen->width_in_pixels).arg(screen->height_in_pixels).arg(1);
#else
int fd;
struct fb_var_screeninfo fb_var;
fd = open("/dev/fb0", O_RDWR);
ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
screenInfo = QString("(%1像素 x %2像素) x %3个").arg(fb_var.xres).arg(fb_var.yres).arg(1);
close(fd);
#endif
return screenInfo;
}
QString LinuxSysInfo::disk()
{
m_diskDescribe = "";
#if 0
struct statfs diskInfo;
statfs("/", &diskInfo);
unsigned long long totalBlocks = diskInfo.f_bsize;
unsigned long long freeDisk = diskInfo.f_bfree * totalBlocks;
unsigned long long totalDisk = diskInfo.f_bsize * totalBlocks;
m_diskDescribe = QString("%1G / %2G").arg(freeDisk).arg();
#else
QDiskInfo diskInfo("/");
m_diskDescribe = QString("可用 %1 GB / 共 %2 GB")
.arg(QString::asprintf("%.2f", diskInfo.getLeftSize() * 1.0 / GB))
.arg(QString::asprintf("%.2f", diskInfo.getTotalSize() * 1.0 / GB));
#endif
return m_diskDescribe;
}
double LinuxSysInfo::getValueByString(char *buff)
{
MEM_OCCUPY *mem = new MEM_OCCUPY;
sscanf(buff, "%s %lu %s", &mem->name, &mem->total, &mem->unit_name);
QString total = QString::asprintf("%.1f", (float)mem->total / (1024 * 1024));
delete mem;
return total.toDouble();
}