mirror of
https://github.com/hholtmann/smcFanControl.git
synced 2025-11-04 19:49:16 +01:00
No longer read unnecessary data in -readFanData
-readFanData was always reading the temperature and the speeds of all of the fans regardless of what data was displayed in the menu bar. Now, -readFanData only reads the data necessary to update the text in the menubar, so, if only the temp is displayed, only the temp is read. The updating of the fan speeds in the menu is now only done when the user clicks on the menu. Also: cleaned up some test code and deleted an unnecessary comment.
This commit is contained in:
@ -38,11 +38,8 @@
|
||||
|
||||
#define kMenuBarHeight 22
|
||||
|
||||
// Max number of fans supported.
|
||||
#define kMaxFanRpms 100
|
||||
|
||||
|
||||
@interface FanControl : NSObject
|
||||
@interface FanControl : NSObject <NSMenuDelegate>
|
||||
|
||||
{
|
||||
IBOutlet id currentSpeed;
|
||||
@ -134,6 +131,7 @@
|
||||
- (void) syncBinder:(Boolean)bind;
|
||||
- (IBAction) changeMenu:(id)sender;
|
||||
- (IBAction)menuSelect:(id)sender;
|
||||
- (void)menuNeedsUpdate:(NSMenu*)menu;
|
||||
@end
|
||||
|
||||
|
||||
|
||||
@ -236,6 +236,11 @@ NSUserDefaults *defaults;
|
||||
for(i=0;i<[s_menus count];i++) {
|
||||
[theMenu insertItem:[s_menus objectAtIndex:i] atIndex:i];
|
||||
};
|
||||
|
||||
// Sign up for menuNeedsUpdate call
|
||||
// so that the fan speeds in the menu can be updated
|
||||
// only when needed.
|
||||
[theMenu setDelegate:self];
|
||||
}
|
||||
|
||||
|
||||
@ -303,52 +308,66 @@ NSUserDefaults *defaults;
|
||||
-(void) readFanData:(NSTimer*)timer{
|
||||
|
||||
int i = 0;
|
||||
NSString *temp;
|
||||
NSString *fan;
|
||||
|
||||
|
||||
//on init handling
|
||||
if (s_sed==nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Store fan speeds in simple array so that they only need to be read once.
|
||||
int fanRpms[kMaxFanRpms] = { 0 };
|
||||
if (g_numFans > kMaxFanRpms)
|
||||
return;
|
||||
// Determine what data is actually needed to keep the energy impact
|
||||
// as low as possible.
|
||||
bool bNeedTemp = false;
|
||||
bool bNeedRpm = false;
|
||||
const int menuBarSetting = [[defaults objectForKey:@"MenuBar"] intValue];
|
||||
switch (menuBarSetting) {
|
||||
default:
|
||||
case 1:
|
||||
bNeedTemp = true;
|
||||
bNeedRpm = true;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
bNeedTemp = true;
|
||||
bNeedRpm = true;
|
||||
break;
|
||||
|
||||
// Determine which fan speed to show in the menubar.
|
||||
int selected = 0;
|
||||
case 3:
|
||||
bNeedTemp = true;
|
||||
bNeedRpm = false;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
bNeedTemp = false;
|
||||
bNeedRpm = true;
|
||||
break;
|
||||
}
|
||||
|
||||
NSString *temp = nil;
|
||||
NSString *fan = nil;
|
||||
float c_temp = 0.0f;
|
||||
int selectedRpm = 0;
|
||||
|
||||
if (bNeedRpm == true) {
|
||||
// Read the current fan speed for the desired fan and format text for display in the menubar.
|
||||
NSArray *fans = [[[FavoritesController arrangedObjects] objectAtIndex:[FavoritesController selectionIndex]] objectForKey:@"FanData"];
|
||||
for (i=0;i<[fans count];i++)
|
||||
for (i=0; i<g_numFans && i<[fans count]; i++)
|
||||
{
|
||||
if ([[[fans objectAtIndex:i] objectForKey:@"menu"] boolValue]==YES) {
|
||||
selected = i;
|
||||
selectedRpm = [smcWrapper get_fan_rpm:i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (selected < 0 || selected >= g_numFans)
|
||||
{
|
||||
// Bad selected fan index.
|
||||
return;
|
||||
|
||||
NSNumberFormatter *nc=[[[NSNumberFormatter alloc] init] autorelease];
|
||||
//avoid jumping in menu bar
|
||||
[nc setFormat:@"000;000;-000"];
|
||||
|
||||
fan = [NSString stringWithFormat:@"%@rpm",[nc stringForObjectValue:[NSNumber numberWithFloat:selectedRpm]]];
|
||||
}
|
||||
|
||||
//read the current fan speeds
|
||||
for(i=0; i<g_numFans; ++i){
|
||||
fanRpms[i] = [smcWrapper get_fan_rpm:i];
|
||||
}
|
||||
|
||||
float c_temp=[smcWrapper get_maintemp];
|
||||
const int selectedRpm = fanRpms[selected];
|
||||
|
||||
//populate Menu Items with recent Data
|
||||
|
||||
// Proceed with building the strings, etc. to update the text in the menubar.
|
||||
for(i=0; i<g_numFans; ++i){
|
||||
NSString *fandesc=[[[s_sed objectForKey:@"Fans"] objectAtIndex:i] objectForKey:@"Description"];
|
||||
[[theMenu itemWithTag:(i+1)*10] setTitle:[NSString stringWithFormat:@"%@: %@ rpm",fandesc,[[NSNumber numberWithInt:fanRpms[i]] stringValue]]];
|
||||
}
|
||||
if (bNeedTemp == true) {
|
||||
// Read current temperature and format text for the menubar.
|
||||
c_temp = [smcWrapper get_maintemp];
|
||||
|
||||
if ([[defaults objectForKey:@"Unit"] intValue]==0) {
|
||||
temp = [NSString stringWithFormat:@"%@%CC",[NSNumber numberWithFloat:c_temp],(unsigned short)0xb0];
|
||||
@ -357,16 +376,17 @@ NSUserDefaults *defaults;
|
||||
[ncf setFormat:@"00;00;-00"];
|
||||
temp = [NSString stringWithFormat:@"%@%CF",[ncf stringForObjectValue:[[NSNumber numberWithFloat:c_temp] celsius_fahrenheit]],(unsigned short)0xb0];
|
||||
}
|
||||
NSNumberFormatter *nc=[[[NSNumberFormatter alloc] init] autorelease];
|
||||
//avoid jumping in menu bar
|
||||
[nc setFormat:@"000;000;-000"];
|
||||
}
|
||||
|
||||
fan=[NSString stringWithFormat:@"%@rpm",[nc stringForObjectValue:[NSNumber numberWithFloat:selectedRpm]]];
|
||||
// Update the temp and/or fan speed text in the menubar.
|
||||
NSMutableAttributedString *s_status = nil;
|
||||
NSMutableParagraphStyle *paragraphStyle = nil;
|
||||
|
||||
const int menuBarSetting = [[defaults objectForKey:@"MenuBar"] intValue];
|
||||
if (menuBarSetting <= 1) {
|
||||
NSString *add;
|
||||
int fsize;
|
||||
switch (menuBarSetting) {
|
||||
default:
|
||||
case 1: {
|
||||
int fsize = 0;
|
||||
NSString *add = nil;
|
||||
if (menuBarSetting==0) {
|
||||
add=@"\n";
|
||||
fsize=9;
|
||||
@ -377,8 +397,8 @@ NSUserDefaults *defaults;
|
||||
[statusItem setLength:96];
|
||||
}
|
||||
|
||||
NSMutableAttributedString *s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",temp,add,fan]];
|
||||
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
||||
s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",temp,add,fan]];
|
||||
paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
||||
[paragraphStyle setAlignment:NSLeftTextAlignment];
|
||||
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:fsize] range:NSMakeRange(0,[s_status length])];
|
||||
[s_status addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[s_status length])];
|
||||
@ -386,40 +406,42 @@ NSUserDefaults *defaults;
|
||||
[statusItem setAttributedTitle:s_status];
|
||||
[statusItem setImage:nil];
|
||||
[statusItem setAlternateImage:nil];
|
||||
[paragraphStyle release];
|
||||
[s_status release];
|
||||
break;
|
||||
}
|
||||
else if (menuBarSetting==2) {
|
||||
|
||||
case 2:
|
||||
// TODO: Big waste of energy to update this tooltip every X seconds when the user
|
||||
// is unlikely to hover the smcFanControl icon over and over again.
|
||||
[statusItem setLength:26];
|
||||
[statusItem setTitle:nil];
|
||||
[statusItem setToolTip:[NSString stringWithFormat:@"%@\n%@",temp,fan]];
|
||||
[statusItem setImage:menu_image];
|
||||
[statusItem setAlternateImage:menu_image_alt];
|
||||
break;
|
||||
|
||||
}
|
||||
else if (menuBarSetting==3) {
|
||||
case 3:
|
||||
[statusItem setLength:46];
|
||||
NSMutableAttributedString *s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",temp]];
|
||||
s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",temp]];
|
||||
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:12] range:NSMakeRange(0,[s_status length])];
|
||||
[s_status addAttribute:NSForegroundColorAttributeName value:(NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]] range:NSMakeRange(0,[s_status length])];
|
||||
[statusItem setAttributedTitle:s_status];
|
||||
[statusItem setImage:nil];
|
||||
[statusItem setAlternateImage:nil];
|
||||
[s_status release];
|
||||
break;
|
||||
|
||||
}
|
||||
else if (menuBarSetting==4) {
|
||||
//TODO: Main temp not used in this case, don't bother reading the main temp in this case.
|
||||
case 4:
|
||||
[statusItem setLength:65];
|
||||
NSMutableAttributedString *s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",fan]];
|
||||
s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",fan]];
|
||||
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:12] range:NSMakeRange(0,[s_status length])];
|
||||
[s_status addAttribute:NSForegroundColorAttributeName value:(NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]] range:NSMakeRange(0,[s_status length])];
|
||||
[statusItem setAttributedTitle:s_status];
|
||||
[statusItem setImage:nil];
|
||||
[statusItem setAlternateImage:nil];
|
||||
[s_status release];
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
[paragraphStyle release];
|
||||
[s_status release];
|
||||
}
|
||||
|
||||
|
||||
@ -532,6 +554,23 @@ NSUserDefaults *defaults;
|
||||
}
|
||||
}
|
||||
|
||||
// Called when user clicks on smcFanControl status bar item
|
||||
// in the status area of the menubar. The fan speed
|
||||
// menu items are now only updated here in order to
|
||||
// reduce the energy impact of -readFanData.
|
||||
- (void)menuNeedsUpdate:(NSMenu*)menu {
|
||||
if (theMenu == menu) {
|
||||
if (s_sed == nil)
|
||||
return;
|
||||
|
||||
int i;
|
||||
for(i=0; i<g_numFans; ++i){
|
||||
NSString *fandesc=[[[s_sed objectForKey:@"Fans"] objectAtIndex:i] objectForKey:@"Description"];
|
||||
[[theMenu itemWithTag:(i+1)*10] setTitle:[NSString stringWithFormat:@"%@: %@ rpm",fandesc,[[NSNumber numberWithInt:[smcWrapper get_fan_rpm:i]] stringValue]]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#pragma mark **Helper-Methods**
|
||||
|
||||
@ -60,9 +60,7 @@ static NSDictionary *tsensors = nil;
|
||||
if (c_temp<=0) {
|
||||
NSArray *allTSensors = [tsensors allKeys];
|
||||
for (NSString *key in allTSensors) {
|
||||
bool bPtrEq = (key == foundKey);
|
||||
bool bCmpEq = ([key isEqualToString:foundKey]);
|
||||
if (false == bPtrEq && false == bCmpEq) {
|
||||
if (![key isEqualToString:foundKey]) {
|
||||
SMCReadKey2((char*)[[tsensors objectForKey:key] UTF8String], &val,conn);
|
||||
c_temp= ((val.bytes[0] * 256 + val.bytes[1]) >> 2)/64;
|
||||
if (c_temp>0) break;
|
||||
|
||||
@ -440,7 +440,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
kern_return_t result;
|
||||
int op = OP_NONE;
|
||||
UInt32Char_t key = { 0 }; //MAW
|
||||
UInt32Char_t key = { 0 };
|
||||
SMCVal_t val;
|
||||
|
||||
while ((c = getopt(argc, argv, "fhk:lrw:v")) != -1)
|
||||
|
||||
Reference in New Issue
Block a user