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
|
diff -Naur wmcms-0.3.5/state_linux.h wmcms-0.3.5/state_linux.h
--- wmcms-0.3.5/state_linux.h 2001-09-24 00:56:28.000000000 +0200
+++ wmcms-0.3.5/state_linux.h 1970-01-01 01:00:00.000000000 +0100
@@ -1,47 +0,0 @@
-/* wmcms - WindowMaker CPU / Mem Usage Monitor DockApp
- * state_linux.h
- * Linux specific functions to get system usage, may someone write it to another plataform
- * Copyright (C) 2001 Fabio Pugliese Ornellas <fabio.ornellas@poli.usp.br>
- *
- * 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.
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
-int cpu_user;
-int cpu_nice;
-int cpu_kernel;
-int cpu_idle;
-
-int mem_total;
-int mem_used;
-int mem_buffers;
-int mem_cached;
-
-int swap_total;
-int swap_used;
-
-void get_cpuinfo ( void )
-{
- FILE *fp;
- fp = fopen ( "/proc/stat", "r" );
- fscanf ( fp, "%*s %d %d %d %d", &cpu_user, &cpu_nice, &cpu_kernel, &cpu_idle );
- fclose ( fp );
-}
-
-void get_meminfo ( void )
-{
- FILE *fp;
- fp = fopen ( "/proc/meminfo", "r" );
- fscanf ( fp, "%*s %*s %*s %*s %*s %*s %*s %d %d %*d %*d %d %d %*s %d %d", &mem_total, &mem_used, &mem_buffers, &mem_cached, &swap_total, &swap_used );
- fclose ( fp );
-}
diff -Naur wmcms-0.3.5/state_linux_2.4.h wmcms-0.3.5/state_linux_2.4.h
--- wmcms-0.3.5/state_linux_2.4.h 1970-01-01 01:00:00.000000000 +0100
+++ wmcms-0.3.5/state_linux_2.4.h 2004-07-20 22:09:11.157182296 +0200
@@ -0,0 +1,47 @@
+/* wmcms - WindowMaker CPU / Mem Usage Monitor DockApp
+ * state_linux.h
+ * Linux specific functions to get system usage, may someone write it to another plataform
+ * Copyright (C) 2001 Fabio Pugliese Ornellas <fabio.ornellas@poli.usp.br>
+ *
+ * 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.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+int cpu_user;
+int cpu_nice;
+int cpu_kernel;
+int cpu_idle;
+
+int mem_total;
+int mem_used;
+int mem_buffers;
+int mem_cached;
+
+int swap_total;
+int swap_used;
+
+void get_cpuinfo_24 ( void )
+{
+ FILE *fp;
+ fp = fopen ( "/proc/stat", "r" );
+ fscanf ( fp, "%*s %d %d %d %d", &cpu_user, &cpu_nice, &cpu_kernel, &cpu_idle );
+ fclose ( fp );
+}
+
+void get_meminfo_24 ( void )
+{
+ FILE *fp;
+ fp = fopen ( "/proc/meminfo", "r" );
+ fscanf ( fp, "%*s %*s %*s %*s %*s %*s %*s %d %d %*d %*d %d %d %*s %d %d", &mem_total, &mem_used, &mem_buffers, &mem_cached, &swap_total, &swap_used );
+ fclose ( fp );
+}
diff -Naur wmcms-0.3.5/state_linux_2.6.h wmcms-0.3.5/state_linux_2.6.h
--- wmcms-0.3.5/state_linux_2.6.h 1970-01-01 01:00:00.000000000 +0100
+++ wmcms-0.3.5/state_linux_2.6.h 2004-07-20 22:08:54.256751552 +0200
@@ -0,0 +1,55 @@
+/* wmcms - WindowMaker CPU / Mem Usage Monitor DockApp
+ * state_linux.h
+ * Linux specific functions to get system usage, may someone write it to another plataform
+ * Copyright (C) 2001 Fabio Pugliese Ornellas <fabio.ornellas@poli.usp.br>
+ *
+ * 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.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+int cpu_user;
+int cpu_nice;
+int cpu_kernel;
+int cpu_idle;
+
+int mem_total;
+int mem_free;
+int mem_used;
+int mem_buffers;
+int mem_cached;
+
+int swap_total;
+int swap_free;
+int swap_used;
+
+void get_cpuinfo_26 ( void )
+{
+ FILE *fp;
+ fp = fopen ( "/proc/stat", "r" );
+ fscanf ( fp, "%*s %d %d %d %d", &cpu_user, &cpu_nice, &cpu_kernel, &cpu_idle );
+ fclose ( fp );
+}
+
+void get_meminfo_26 ( void )
+{
+ FILE *fp;
+ fp = fopen ( "/proc/meminfo", "r" );
+/* The format of /proc/meminfo has changed with the 2.6 kernel; this new line will work for 2.6 -- idh
+ fscanf ( fp, "%*s %*s %*s %*s %*s %*s %*s %d %d %*d %*d %d %d %*s %d %d", &mem_total, &mem_used, &mem_buffers, &mem_cached, &swap_total, &swap_used ); */
+ fscanf ( fp, "%*s %d %*s %*s %d %*s %*s %d %*s %*s %d %*s %*s %*d %*s %*s %*d %*s %*s %*d %*s %*s %*d %*s %*s %*d %*s %*s %*d %*s %*s %*d %*s %*s %d %*s %*s %d %*s",
+ &mem_total, &mem_free, &mem_buffers, &mem_cached, &swap_total, &swap_free );
+ fclose ( fp );
+
+ mem_used = mem_total - mem_free;
+ swap_used = swap_total - swap_free;
+}
diff -Naur wmcms-0.3.5/wmcms.c wmcms-0.3.5/wmcms.c
--- wmcms-0.3.5/wmcms.c 2001-09-24 00:57:06.000000000 +0200
+++ wmcms-0.3.5/wmcms.c 2004-07-20 22:34:26.917751664 +0200
@@ -19,7 +19,8 @@
#include <stdio.h>
#include <unistd.h>
#include <dockapp.h>
-#include "state_linux.h"
+#include "state_linux_2.6.h"
+#include "state_linux_2.4.h"
#include "wmcms.xpm"
char *displayName = "";
@@ -34,6 +35,13 @@
int no_nice;
char *exe = "";
+typedef enum
+{
+ IS_2_6 = 0,
+ IS_OTHER
+} kernel_versions;
+kernel_versions kernel_version;
+
/* command line */
static DAProgramOption options[] = {
{"-d", "--display", "display to use", DOString, False, {&displayName} },
@@ -44,6 +52,8 @@
{"-p", "--program", "program to run on CPU click", DOString, False, {&exe} }
};
+kernel_versions Get_Kernel_version(void);
+
/* draw bars */
void bar ( int size, int x, int y, int barx, int bary )
{
@@ -156,6 +166,9 @@
/* define callbacks */
DACallbacks callbacks = { NULL, change, NULL, NULL, NULL, NULL };
+ /* determine kernel version */
+ kernel_version = Get_Kernel_version();
+
advanced = 0;
up = 0;
no_nice = 0;
@@ -204,14 +217,23 @@
XCopyArea ( DADisplay, backdrop, pixmap, gc, 3, 140, 58, 58, 0, 0);
DASetPixmap(pixmap);
- /* initial cpu total while splash */
- get_cpuinfo();
+ /* initial cpu total while splash */
+ if (kernel_version == IS_2_6)
+ get_cpuinfo_26();
+ else
+ get_cpuinfo_24();
+
old_cpu_user = cpu_user;
old_cpu_nice = cpu_nice;
old_cpu_kernel = cpu_kernel;
old_cpu_idle = cpu_idle;
usleep ( delay );
- get_cpuinfo();
+
+ if (kernel_version == IS_2_6)
+ get_cpuinfo_26();
+ else
+ get_cpuinfo_24();
+
cpu_total = cpu_user - old_cpu_user + cpu_nice - old_cpu_nice + cpu_kernel - old_cpu_kernel + cpu_idle - old_cpu_idle;
/* draw background */
@@ -230,8 +252,16 @@
XCopyArea ( DADisplay, backdrop, pixmap, gc, 4, 14, 56, 8, 1, 49 );
/* read values */
- get_cpuinfo ();
- get_meminfo ();
+ if (kernel_version == IS_2_6)
+ {
+ get_cpuinfo_26 ();
+ get_meminfo_26 ();
+ }
+ else
+ {
+ get_cpuinfo_24 ();
+ get_meminfo_24 ();
+ }
/* cpu total */
cpu_total = cpu_user - old_cpu_user + cpu_nice - old_cpu_nice + cpu_kernel - old_cpu_kernel + cpu_idle - old_cpu_idle;
@@ -328,3 +358,20 @@
fprintf(stderr, "Congratulations, something truely awful has happened.\n");
return -1;
}
+
+kernel_versions Get_Kernel_version(void)
+{
+ FILE *fp = fopen("/proc/version", "r");
+ char buf[512];
+
+ if (!fp) return IS_OTHER;
+ if (!fgets(buf, 512, fp))
+ {
+ fclose(fp);
+ return IS_OTHER;
+ }
+ fclose(fp);
+
+ if (strstr(buf, "2.6.")) return IS_2_6;
+ return IS_OTHER;
+}
|