forked from mirror/smcFanControl
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
|
#define kMenuBarHeight 22
|
||||||
|
|
||||||
// Max number of fans supported.
|
|
||||||
#define kMaxFanRpms 100
|
|
||||||
|
|
||||||
|
@interface FanControl : NSObject <NSMenuDelegate>
|
||||||
@interface FanControl : NSObject
|
|
||||||
|
|
||||||
{
|
{
|
||||||
IBOutlet id currentSpeed;
|
IBOutlet id currentSpeed;
|
||||||
@ -134,6 +131,7 @@
|
|||||||
- (void) syncBinder:(Boolean)bind;
|
- (void) syncBinder:(Boolean)bind;
|
||||||
- (IBAction) changeMenu:(id)sender;
|
- (IBAction) changeMenu:(id)sender;
|
||||||
- (IBAction)menuSelect:(id)sender;
|
- (IBAction)menuSelect:(id)sender;
|
||||||
|
- (void)menuNeedsUpdate:(NSMenu*)menu;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -236,6 +236,11 @@ NSUserDefaults *defaults;
|
|||||||
for(i=0;i<[s_menus count];i++) {
|
for(i=0;i<[s_menus count];i++) {
|
||||||
[theMenu insertItem:[s_menus objectAtIndex:i] atIndex: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,123 +308,140 @@ NSUserDefaults *defaults;
|
|||||||
-(void) readFanData:(NSTimer*)timer{
|
-(void) readFanData:(NSTimer*)timer{
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
NSString *temp;
|
|
||||||
NSString *fan;
|
|
||||||
|
|
||||||
|
|
||||||
//on init handling
|
//on init handling
|
||||||
if (s_sed==nil) {
|
if (s_sed==nil) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store fan speeds in simple array so that they only need to be read once.
|
// Determine what data is actually needed to keep the energy impact
|
||||||
int fanRpms[kMaxFanRpms] = { 0 };
|
// as low as possible.
|
||||||
if (g_numFans > kMaxFanRpms)
|
bool bNeedTemp = false;
|
||||||
return;
|
bool bNeedRpm = false;
|
||||||
|
const int menuBarSetting = [[defaults objectForKey:@"MenuBar"] intValue];
|
||||||
|
switch (menuBarSetting) {
|
||||||
// Determine which fan speed to show in the menubar.
|
default:
|
||||||
int selected = 0;
|
case 1:
|
||||||
NSArray *fans = [[[FavoritesController arrangedObjects] objectAtIndex:[FavoritesController selectionIndex]] objectForKey:@"FanData"];
|
bNeedTemp = true;
|
||||||
for (i=0;i<[fans count];i++)
|
bNeedRpm = true;
|
||||||
{
|
|
||||||
if ([[[fans objectAtIndex:i] objectForKey:@"menu"] boolValue]==YES) {
|
|
||||||
selected = i;
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
case 2:
|
||||||
if (selected < 0 || selected >= g_numFans)
|
bNeedTemp = true;
|
||||||
{
|
bNeedRpm = true;
|
||||||
// Bad selected fan index.
|
break;
|
||||||
return;
|
|
||||||
|
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<g_numFans && i<[fans count]; i++)
|
||||||
|
{
|
||||||
|
if ([[[fans objectAtIndex:i] objectForKey:@"menu"] boolValue]==YES) {
|
||||||
|
selectedRpm = [smcWrapper get_fan_rpm:i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
if (bNeedTemp == true) {
|
||||||
for(i=0; i<g_numFans; ++i){
|
// Read current temperature and format text for the menubar.
|
||||||
fanRpms[i] = [smcWrapper get_fan_rpm:i];
|
c_temp = [smcWrapper get_maintemp];
|
||||||
}
|
|
||||||
|
|
||||||
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 ([[defaults objectForKey:@"Unit"] intValue]==0) {
|
|
||||||
temp=[NSString stringWithFormat:@"%@%CC",[NSNumber numberWithFloat:c_temp],(unsigned short)0xb0];
|
|
||||||
} else {
|
|
||||||
NSNumberFormatter *ncf=[[[NSNumberFormatter alloc] init] autorelease];
|
|
||||||
[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]]];
|
|
||||||
|
|
||||||
const int menuBarSetting = [[defaults objectForKey:@"MenuBar"] intValue];
|
|
||||||
if (menuBarSetting <= 1) {
|
|
||||||
NSString *add;
|
|
||||||
int fsize;
|
|
||||||
if (menuBarSetting==0) {
|
|
||||||
add=@"\n";
|
|
||||||
fsize=9;
|
|
||||||
[statusItem setLength:53];
|
|
||||||
} else {
|
|
||||||
add=@" ";
|
|
||||||
fsize=11;
|
|
||||||
[statusItem setLength:96];
|
|
||||||
}
|
|
||||||
|
|
||||||
NSMutableAttributedString *s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",temp,add,fan]];
|
if ([[defaults objectForKey:@"Unit"] intValue]==0) {
|
||||||
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
temp = [NSString stringWithFormat:@"%@%CC",[NSNumber numberWithFloat:c_temp],(unsigned short)0xb0];
|
||||||
[paragraphStyle setAlignment:NSLeftTextAlignment];
|
} else {
|
||||||
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:fsize] range:NSMakeRange(0,[s_status length])];
|
NSNumberFormatter *ncf=[[[NSNumberFormatter alloc] init] autorelease];
|
||||||
[s_status addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[s_status length])];
|
[ncf setFormat:@"00;00;-00"];
|
||||||
[s_status addAttribute:NSForegroundColorAttributeName value:(NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]] range:NSMakeRange(0,[s_status length])];
|
temp = [NSString stringWithFormat:@"%@%CF",[ncf stringForObjectValue:[[NSNumber numberWithFloat:c_temp] celsius_fahrenheit]],(unsigned short)0xb0];
|
||||||
[statusItem setAttributedTitle:s_status];
|
}
|
||||||
[statusItem setImage:nil];
|
}
|
||||||
[statusItem setAlternateImage:nil];
|
|
||||||
[paragraphStyle release];
|
// Update the temp and/or fan speed text in the menubar.
|
||||||
[s_status release];
|
NSMutableAttributedString *s_status = nil;
|
||||||
}
|
NSMutableParagraphStyle *paragraphStyle = nil;
|
||||||
else if (menuBarSetting==2) {
|
|
||||||
[statusItem setLength:26];
|
switch (menuBarSetting) {
|
||||||
[statusItem setTitle:nil];
|
default:
|
||||||
[statusItem setToolTip:[NSString stringWithFormat:@"%@\n%@",temp,fan]];
|
case 1: {
|
||||||
[statusItem setImage:menu_image];
|
int fsize = 0;
|
||||||
[statusItem setAlternateImage:menu_image_alt];
|
NSString *add = nil;
|
||||||
|
if (menuBarSetting==0) {
|
||||||
}
|
add=@"\n";
|
||||||
else if (menuBarSetting==3) {
|
fsize=9;
|
||||||
[statusItem setLength:46];
|
[statusItem setLength:53];
|
||||||
NSMutableAttributedString *s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",temp]];
|
} else {
|
||||||
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:12] range:NSMakeRange(0,[s_status length])];
|
add=@" ";
|
||||||
[s_status addAttribute:NSForegroundColorAttributeName value:(NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]] range:NSMakeRange(0,[s_status length])];
|
fsize=11;
|
||||||
[statusItem setAttributedTitle:s_status];
|
[statusItem setLength:96];
|
||||||
[statusItem setImage:nil];
|
}
|
||||||
[statusItem setAlternateImage:nil];
|
|
||||||
[s_status release];
|
s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",temp,add,fan]];
|
||||||
|
paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
||||||
}
|
[paragraphStyle setAlignment:NSLeftTextAlignment];
|
||||||
else if (menuBarSetting==4) {
|
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:fsize] range:NSMakeRange(0,[s_status length])];
|
||||||
//TODO: Main temp not used in this case, don't bother reading the main temp in this case.
|
[s_status addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[s_status length])];
|
||||||
[statusItem setLength:65];
|
[s_status addAttribute:NSForegroundColorAttributeName value:(NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]] range:NSMakeRange(0,[s_status length])];
|
||||||
NSMutableAttributedString *s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",fan]];
|
[statusItem setAttributedTitle:s_status];
|
||||||
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:12] range:NSMakeRange(0,[s_status length])];
|
[statusItem setImage:nil];
|
||||||
[s_status addAttribute:NSForegroundColorAttributeName value:(NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]] range:NSMakeRange(0,[s_status length])];
|
[statusItem setAlternateImage:nil];
|
||||||
[statusItem setAttributedTitle:s_status];
|
break;
|
||||||
[statusItem setImage:nil];
|
}
|
||||||
[statusItem setAlternateImage:nil];
|
|
||||||
[s_status release];
|
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;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
[statusItem setLength:46];
|
||||||
|
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];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
[statusItem setLength:65];
|
||||||
|
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];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
[paragraphStyle release];
|
||||||
|
[s_status release];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -530,7 +552,24 @@ NSUserDefaults *defaults;
|
|||||||
[[[FanController arrangedObjects] objectAtIndex:i] setValue:[NSNumber numberWithBool:NO] forKey:@"menu"];
|
[[[FanController arrangedObjects] objectAtIndex:i] setValue:[NSNumber numberWithBool:NO] forKey:@"menu"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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]]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -60,9 +60,7 @@ static NSDictionary *tsensors = nil;
|
|||||||
if (c_temp<=0) {
|
if (c_temp<=0) {
|
||||||
NSArray *allTSensors = [tsensors allKeys];
|
NSArray *allTSensors = [tsensors allKeys];
|
||||||
for (NSString *key in allTSensors) {
|
for (NSString *key in allTSensors) {
|
||||||
bool bPtrEq = (key == foundKey);
|
if (![key isEqualToString:foundKey]) {
|
||||||
bool bCmpEq = ([key isEqualToString:foundKey]);
|
|
||||||
if (false == bPtrEq && false == bCmpEq) {
|
|
||||||
SMCReadKey2((char*)[[tsensors objectForKey:key] UTF8String], &val,conn);
|
SMCReadKey2((char*)[[tsensors objectForKey:key] UTF8String], &val,conn);
|
||||||
c_temp= ((val.bytes[0] * 256 + val.bytes[1]) >> 2)/64;
|
c_temp= ((val.bytes[0] * 256 + val.bytes[1]) >> 2)/64;
|
||||||
if (c_temp>0) break;
|
if (c_temp>0) break;
|
||||||
|
|||||||
@ -440,7 +440,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
kern_return_t result;
|
kern_return_t result;
|
||||||
int op = OP_NONE;
|
int op = OP_NONE;
|
||||||
UInt32Char_t key = { 0 }; //MAW
|
UInt32Char_t key = { 0 };
|
||||||
SMCVal_t val;
|
SMCVal_t val;
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "fhk:lrw:v")) != -1)
|
while ((c = getopt(argc, argv, "fhk:lrw:v")) != -1)
|
||||||
|
|||||||
Reference in New Issue
Block a user