summaryrefslogtreecommitdiff
path: root/sysutils/torsmo/patches/patch-ab
blob: 11243ed45b44624b41acb2a867bafd567ecba21c (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
$NetBSD: patch-ab,v 1.1.1.1 2006/03/29 21:42:55 ghen Exp $

--- netbsd.c.orig	2004-08-25 19:55:57.000000000 +0200
+++ netbsd.c
@@ -9,6 +9,7 @@
 #include <err.h>
 #include <limits.h>
 #include <paths.h>
+#include <ctype.h>
 
 #include <kvm.h>
 #include <nlist.h>
@@ -221,6 +222,33 @@ void update_net_stats() 
     }
 }
 
+static char *freq = NULL;
+char * get_freq()
+{
+    size_t val_len;
+    u_int32_t val;
+
+    if (freq == NULL) {
+        freq = malloc(10);
+        if (freq == NULL) {
+            return NULL;
+        } else {
+            freq[0] = '\0';
+        }
+    }
+
+    /* Laptops with enhanced speed step. */
+    if (sysctlbyname("machdep.est.frequency.current",
+                     NULL, &val_len, NULL, 0) == 0) {
+        sysctlbyname("machdep.est.frequency.current", &val, &val_len, NULL, 0);
+        sprintf(freq, "%d", val);
+    } else {
+        /* XXX: Parse out the clockspeed from machdep.cpu_brand */
+    }
+
+    return freq;
+}
+
 void update_total_processes() 
 {
     /* It's easier to use kvm here than sysctl */
@@ -302,6 +330,7 @@ void update_cpu_usage() 
 }
 
 double get_i2c_info(int fd, int div) {
+    (void) fd; (void) div;
     return -1;
 }
 
@@ -315,24 +344,96 @@ void update_load_average() {
 }
 
 double get_acpi_temperature(int fd) {
+    (void) fd;
     return -1;
 }
 
-void get_battery_stuff(char *buf, unsigned int n, const char *bat) {
-}
-
 int open_i2c_sensor(const char *dev, const char *type, int n, int *div)
 {
+    (void) dev; (void) type; (void) n; (void) div;
     return -1;
 }
 
 int open_acpi_temperature(const char *name) {
+    (void) name;
     return -1;
 }
 
+static char acpi_ac_str[64] = "N/A";
 char * get_acpi_ac_adapter(void)
 {
-    return "N/A";
+    /* get_battery_stuff() actually populates this for us. */
+    return acpi_ac_str;
+}
+
+static char last_battery_str[64];
+static double last_battery_time;
+
+void get_battery_stuff(char *buf, unsigned int n, const char *bat) {
+    FILE *f;
+    char *foo;
+    char b[4096];
+    char b_name[32];
+    int bat_num;
+    int found_acpibat = 0;
+    int found_acpiacad = 0;
+    float bat_charge = 0.0;
+
+    /* Don't update battery too often. */
+    if (current_update_time - last_battery_time < 29.5) {
+        snprintf(buf, n, "%s", last_battery_str);
+        return;
+    }
+    last_battery_time = current_update_time;
+
+    sscanf(bat, "BAT%d", &bat_num);
+    sprintf(b_name, "acpibat%d", bat_num);
+
+    /*
+     * Using the envsys API is like pulling teeth without anesthetic.
+     * so just popen envstat and parse the output instead.
+     */
+    f = popen("/usr/sbin/envstat -r", "r");
+    if (f != NULL) {
+        while(!feof(f)) {
+            fgets(b, 4096, f);
+
+            /* AC adapter state. */
+            if (strstr(b, "acpiacad0")) {
+                if (strstr(b, "disconnected")) {
+                    sprintf(acpi_ac_str, "disconnected");
+                } else {
+                    sprintf(acpi_ac_str, "connected");
+                }
+                found_acpiacad = 1;
+            }
+
+            if (bat && strstr(b, b_name)) {
+                if (strstr(b, "charge:")) {
+                    foo = strstr(b, "%)");
+                    while (*foo != ' ' && *foo != '(') {
+                        foo--;
+                    }
+                    foo = foo + 1;
+                    sscanf(foo, "%f", &bat_charge);
+                    found_acpibat = 1;
+                }
+            }
+        }
+        pclose(f);
+    }
+
+    if (found_acpibat) {
+        snprintf(last_battery_str, 64, "%.2f%%", bat_charge);
+        snprintf(buf, n, "%s", last_battery_str);
+    } else {
+        /* XXX: If that failed, try to use APM. */
+    }
+
+    if (!found_acpiacad) {
+        sprintf(acpi_ac_str, "N/A");
+    }
+
 }
 
 char* get_acpi_fan() {