Skip to content

Commit 10cd001

Browse files
author
Daniel Borca
committed
improve profile parsing
1 parent 77e6b3e commit 10cd001

4 files changed

Lines changed: 178 additions & 19 deletions

File tree

SMCSense/AppDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@property (nonatomic, readonly) NSStatusItem *statusItem;
1515
@property (nonatomic, readonly) SMCSensors *smcSensors;
16-
@property (nonatomic, readonly) NSDictionary *profile;
16+
@property (nonatomic, readonly) NSMutableDictionary *profile;
1717
@property (atomic, readonly) BOOL isMenuOpen;
1818

1919
@end

SMCSense/AppDelegate.m

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ @interface AppDelegate ()
1919

2020
@property (nonatomic, strong, readwrite) NSStatusItem *statusItem;
2121
@property (nonatomic, strong, readwrite) SMCSensors *smcSensors;
22-
@property (nonatomic, strong, readwrite) NSDictionary *profile;
22+
@property (nonatomic, strong, readwrite) NSMutableDictionary *profile;
2323
@property (atomic, readwrite) BOOL isMenuOpen;
2424

2525
@end
@@ -34,13 +34,13 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification
3434
self.smcSensors = [[SMCSensors alloc] init];
3535
self.profile = [self loadProfile];
3636

37+
NSMenu *menu = [[NSMenu alloc] init];
3738
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
38-
self.statusItem.menu = [[NSMenu alloc] init];
39-
[self.statusItem.menu setDelegate:self];
39+
self.statusItem.menu = menu;
40+
menu.delegate = self;
4041
#ifdef NON_SELECTABLE
41-
[self.statusItem.menu setAutoenablesItems:NO];
42+
[menu setAutoenablesItems:NO];
4243
#endif
43-
[self.statusItem.menu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"Q"];
4444
self.statusItem.button.image = [NSImage imageNamed:@"StatusItem-Image"];
4545

4646
[self updateStatusItemMenu:[NSApplication sharedApplication]];
@@ -79,6 +79,9 @@ - (void)updateStatusItemMenu:(id)sender
7979
continue;
8080
}
8181
NSString *humanReadableName = [self getHumanReadableString:key];
82+
if (![humanReadableName isKindOfClass:[NSString class]]) {
83+
continue;
84+
}
8285
// NSLog(@"%@: %.1f C\n", humanReadableName, temperature);
8386
if (self.isMenuOpen && temperature >= 20) {
8487
color = [self getTempColor:temperature];
@@ -190,27 +193,74 @@ - (NSString *)sysctlByName:(const char *)name
190193
return str;
191194
}
192195

193-
- (NSDictionary *)loadProfile
196+
- (NSMutableDictionary *)loadProfile
194197
{
195198
NSString *config;
196199
NSString *machine = [self sysctlByName:"hw.model"];
197200
if (machine) {
198201
config = [[NSBundle mainBundle] pathForResource:machine ofType:@"plist" inDirectory:@"Profiles"];
199202
if (config) {
200-
NSDictionary *profile = [NSDictionary dictionaryWithContentsOfFile:config];
203+
NSMutableDictionary *profile = [NSMutableDictionary dictionaryWithContentsOfFile:config];
201204
if (profile) {
202205
return profile;
203206
}
204207
}
205208
}
206209
config = [[NSBundle mainBundle] pathForResource:@"Default" ofType:@"plist" inDirectory:@"Profiles"];
207-
return config ? [NSDictionary dictionaryWithContentsOfFile:config] : nil;
210+
return config ? [NSMutableDictionary dictionaryWithContentsOfFile:config] : nil;
208211
}
209212

210213
- (NSString *)getHumanReadableString:(NSString *)key
211214
{
212-
NSString *val = self.profile ? self.profile[key] : nil;
213-
return val ?: [self.smcSensors humanReadableNameForKey:key];
215+
if (self.profile) {
216+
const char *k;
217+
NSString *val = self.profile[key];
218+
if (val) {
219+
return val;
220+
}
221+
k = [key UTF8String];
222+
for (NSString *it in self.profile.allKeys) {
223+
ssize_t i, j;
224+
const char *p = [it UTF8String];
225+
for (i = 0, j = -1; ; i++) {
226+
if (p[i] != k[i]) {
227+
if (p[i] != '?' || !isdigit(k[i]) || j != -1) {
228+
break;
229+
}
230+
j = i;
231+
}
232+
if (p[i] == '\0') {
233+
const char *v;
234+
char buf[1024];
235+
int n = k[j] - '0';
236+
int replacements = 0;
237+
val = self.profile[it];
238+
if (![val isKindOfClass:[NSString class]]) {
239+
return val;
240+
}
241+
v = [val UTF8String];
242+
for (i = 0, j = 0; v[i] && j < 1000; i++) {
243+
if (v[i] == '\\' && isxdigit(v[i + 1])) {
244+
int c = v[++i];
245+
if (n + c > '9' && c <= '9') {
246+
c += 'A' - '9' - 1;
247+
}
248+
buf[j++] = n + c;
249+
replacements++;
250+
continue;
251+
}
252+
buf[j++] = v[i];
253+
}
254+
buf[j] = '\0';
255+
if (replacements == 0) {
256+
sprintf(&buf[j], " #%d", n);
257+
}
258+
return self.profile[key] = [NSString stringWithUTF8String:buf];
259+
}
260+
}
261+
}
262+
}
263+
return [self.smcSensors humanReadableNameForKey:key];
214264
}
215265

216266
#pragma mark - NSMenuDelegate

SMCSense/Profiles/Default.plist

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,95 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
6+
<!--
7+
https://logi.wiki/index.php/SMC_Sensor_Codes
8+
https://github.com/acidanthera/VirtualSMC/blob/master/Docs/SMCSensorKeys.txt
9+
https://github.com/xythobuz/JSystemInfoKit/blob/master/SystemInfoKit/JSKSMC.m
10+
https://github.com/mikepj/XRG/blob/main/Resources/SMCSensorNames.plist
11+
12+
TBXT == TB0T (ok)
13+
TH0A == TH0a (HDD cooked vs raw)
14+
TH0B == TH0b (HDD cooked vs raw)
15+
TC1F == TC1f (CPU cooked vs raw)
16+
TG0D == TG0F (GPU Die vs Filtered)
17+
TaSP == Ta0P (Airflow)
18+
19+
A single '?' inside key matches a digit [0-9]. If the description string contains
20+
a '\' followed by a hex digit, the hex digit is incremented by the matched value;
21+
otherwise the matched value is appended to the description:
22+
key="XY?Z", string="Something cool" => "XY2Z", "Something cool #2"
23+
key="XY?Z", string="Something \0 cool" => "XY1Z", "Something 1 cool"
24+
key="XY?Z", string="Something \3 cool" => "XY1Z", "Something 4 cool"
25+
-->
26+
27+
<key>TaSP</key>
28+
<string>Airflow</string>
529
<key>TBXT</key>
630
<string>Battery</string>
7-
<key>TC0E</key>
8-
<string>CPU 1</string>
9-
<key>TC0F</key>
10-
<string>CPU 2</string>
1131
<key>TCGC</key>
1232
<string>PECI GPU</string>
33+
<key>TCMX</key>
34+
<string>Max PECI</string>
35+
<key>TCSA</key>
36+
<string>PECI SA</string>
37+
<key>TCXC</key>
38+
<string>PECI CPU</string>
39+
<key>TPCD</key>
40+
<string>PCH Die</string>
41+
<key>Ts0S</key>
42+
<string>Syntethic Bottom</string>
43+
<key>Ts1S</key>
44+
<string>Synthetic Top</string>
45+
<key>TTLD</key>
46+
<string>Thunderbolt Left</string>
47+
<key>TTRD</key>
48+
<string>Thunderbolt Right</string>
49+
50+
<key>TA?P</key>
51+
<string>Airflow</string>
52+
<key>Ta?P</key>
53+
<string>Airflow</string>
54+
<key>TA?V</key>
55+
<string>Ambient</string>
56+
<key>TB?T</key>
57+
<string>Battery</string>
58+
<key>TC?C</key>
59+
<string>CPU Core \0</string>
60+
<key>TC?E</key>
61+
<string>CPU (E)</string>
62+
<key>TC?F</key>
63+
<string>CPU (F)</string>
64+
<key>TC?P</key>
65+
<string>CPU Proximity</string>
66+
<key>TG?D</key>
67+
<string>GPU Die</string>
68+
<key>TG?F</key>
69+
<string>GPU (F)</string>
70+
<key>TG?P</key>
71+
<string>GPU Proximity</string>
72+
<key>TH?A</key>
73+
<string>Drive \0 (A)</string>
74+
<key>TH?B</key>
75+
<string>Drive \0 (B)</string>
76+
<key>TH?C</key>
77+
<string>Drive \0 (C)</string>
78+
<key>TH?V</key>
79+
<string>Drive \0 (V)</string>
80+
<key>Th?H</key>
81+
<string>Heatsink</string>
82+
<key>TM?P</key>
83+
<string>DIMM \0 Proximity</string>
84+
<key>TM?S</key>
85+
<string>DIMM \0</string>
86+
<key>TP?P</key>
87+
<string>PCH Proximity</string>
88+
<key>Ts?P</key>
89+
<string>Palm Rest</string>
90+
<key>TW?P</key>
91+
<string>Wireless Proximity</string>
92+
93+
<key>TCFC</key>
94+
<false/>
1395
</dict>
1496
</plist>

SMCSense/Profiles/MacBookPro11,1.plist

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,38 @@
44
<dict>
55
<key>TBXT</key>
66
<string>Battery</string>
7-
<key>TC0E</key>
8-
<string>CPU 1</string>
9-
<key>TC0F</key>
10-
<string>CPU 2</string>
117
<key>TCGC</key>
128
<string>PECI GPU</string>
9+
<key>TCSA</key>
10+
<string>PECI SA</string>
11+
<key>TCXC</key>
12+
<string>PECI CPU</string>
13+
<key>TPCD</key>
14+
<string>PCH Die</string>
15+
16+
<key>TA?P</key>
17+
<string>Airflow</string>
18+
<key>TB?T</key>
19+
<string>Battery</string>
20+
<key>TC?C</key>
21+
<string>CPU Core \0</string>
22+
<key>TC?E</key>
23+
<string>CPU (E)</string>
24+
<key>TC?F</key>
25+
<string>CPU (F)</string>
26+
<key>TC?P</key>
27+
<string>CPU Proximity</string>
28+
<key>TH?A</key>
29+
<string>Drive \0 (A)</string>
30+
<key>TH?B</key>
31+
<string>Drive \0 (B)</string>
32+
<key>TH?V</key>
33+
<string>Drive \0 (V)</string>
34+
<key>Th?H</key>
35+
<string>Heatsink</string>
36+
<key>TM?P</key>
37+
<string>DIMM \0 Proximity</string>
38+
<key>Ts?P</key>
39+
<string>Palm Rest</string>
1340
</dict>
1441
</plist>

0 commit comments

Comments
 (0)