/
info.c
105 lines (86 loc) · 2.09 KB
/
info.c
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
#include "info.h"
#include "meter.h"
static gboolean
cpu_meter_update (gpointer data)
{
GtkMeter *meter = GTK_METER (data);
gchar *contents;
if (g_file_get_contents("/proc/loadavg", &contents, NULL, NULL)) {
gtk_meter_set_value (meter, g_ascii_strtod (contents, NULL));
g_free(contents);
}
return TRUE;
}
GtkWidget*
cpu_init ()
{
GtkWidget *meter = gtk_meter_new ("cpu");
gdouble processors = 0.0;
gchar *contents, *tmp;
if (g_file_get_contents("/proc/cpuinfo", &contents, NULL, NULL)) {
tmp = contents;
while ((tmp = g_strstr_len (tmp, -1, "processor"))) {
tmp++;
processors++;
}
g_free(contents);
}
gtk_meter_set_high (GTK_METER (meter), processors);
cpu_meter_update (meter);
g_timeout_add (1000, cpu_meter_update, meter);
return meter;
}
static gboolean
mem_meter_update (gpointer data)
{
GtkMeter *meter = GTK_METER (data);
gchar *contents, **lines, *tmp;
gint i = 0;
if (g_file_get_contents("/proc/meminfo", &contents, NULL, NULL)) {
lines = g_strsplit (contents, "\n", 0);
while (lines[i] != NULL) {
if (!g_ascii_strncasecmp (lines[i], "MemFree:", 8)) {
tmp = g_strstr_len (lines[i], -1, " ");
if (tmp != NULL) {
while (g_ascii_isspace (*tmp))
tmp++;
gtk_meter_set_value (GTK_METER (meter),
gtk_meter_get_high (GTK_METER (meter)) - g_ascii_strtod (tmp, NULL));
}
break;
}
i++;
}
g_strfreev (lines);
g_free (contents);
}
return TRUE;
}
GtkWidget*
mem_init ()
{
GtkWidget *meter = gtk_meter_new ("mem");
gchar *contents, **lines, *tmp;
gint i = 0;
if (g_file_get_contents("/proc/meminfo", &contents, NULL, NULL)) {
lines = g_strsplit (contents, "\n", 0);
while (lines[i] != NULL) {
if (!g_ascii_strncasecmp (lines[i], "MemTotal:", 9)) {
tmp = g_strstr_len (lines[i], -1, " ");
if (tmp != NULL) {
while (g_ascii_isspace (*tmp))
tmp++;
gtk_meter_set_high (GTK_METER (meter),
g_ascii_strtod (tmp, NULL));
}
break;
}
i++;
}
g_strfreev (lines);
g_free (contents);
}
mem_meter_update (meter);
g_timeout_add (1000, mem_meter_update, meter);
return meter;
}