summaryrefslogtreecommitdiff
path: root/x11/lxpanel/files/patch-src-plugin-cpu-cpu.c
blob: a3e060757fbd2d41ed49ad4ca7a74b0bdf57a021 (plain) (blame)
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
--- src/plugins/cpu/cpu.c.orig	Sat Dec 23 17:32:42 2006
+++ src/plugins/cpu/cpu.c	Sat Dec 23 17:32:49 2006
@@ -24,7 +24,15 @@
 #include <string.h>
 #include <sys/time.h>
 #include <time.h>
-#include <sys/sysinfo.h>
+
+#ifdef __FreeBSD__
+# include <sys/types.h>
+# include <sys/resource.h>
+# include <sys/sysctl.h>
+#else
+# include <sys/sysinfo.h>
+#endif
+
 #include <stdlib.h>
 #include <glib/gi18n.h>
 
@@ -61,6 +69,38 @@
     struct cpu_stat cpu_anterior;
 } cpu_t;
 
+#ifdef __FreeBSD__
+static void
+get_procstat(unsigned long *u, unsigned long *n, unsigned long *s,
+        unsigned long *i)
+{
+  static int mib[2] = { -1, -1 }, init = 0, j, realhz;
+    long ct[CPUSTATES];
+
+    
+        if(init == 0) {
+             struct clockinfo ci;
+             j = sizeof(ci);
+             sysctlbyname("kern.clockrate", &ci, &j, NULL, 0);
+             realhz = ci.stathz ? ci.stathz : ci.hz;
+      
+             j = 2;
+             sysctlnametomib("kern.cp_time", mib, &j);
+      
+             init = 1;
+             j = sizeof(ct);
+        }
+    
+    	sysctl(mib, 2, ct, &j, NULL, 0);
+         *u = ct[CP_USER] / realhz;
+         *n = ct[CP_NICE] / realhz;
+         *s = ct[CP_SYS]  / realhz;
+         *i = ct[CP_IDLE] / realhz;
+
+        return;
+      }
+#endif
+
 
 static int
 cpu_update(cpu_t *c)
@@ -68,18 +108,28 @@
     int cpu_u=0, cpu_s=0, cpu_n=0, cpu_i=100;
     unsigned int i;
     struct cpu_stat cpu, cpu_r;
+
+#ifndef __FreeBSD__
+           FILE *stat;
+#endif
+
     FILE *stat;
     float total;
     
     ENTER;
     if(!c->pixmap)
         RET(TRUE); 
+
+#ifdef __FreeBSD__
+    get_procstat(&cpu.u, &cpu.n, &cpu.s, &cpu.i);
+#else
      
     stat = fopen("/proc/stat", "r");
     if(!stat)
         RET(TRUE);
     fscanf(stat, "cpu %lu %lu %lu %lu", &cpu.u, &cpu.n, &cpu.s, &cpu.i);
     fclose(stat);
+#endif
 
     cpu_r.u = cpu.u - c->cpu_anterior.u;
     cpu_r.n = cpu.n - c->cpu_anterior.n;