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;
|
||||||
{
|
break;
|
||||||
if ([[[fans objectAtIndex:i] objectForKey:@"menu"] boolValue]==YES) {
|
|
||||||
selected = i;
|
case 2:
|
||||||
|
bNeedTemp = true;
|
||||||
|
bNeedRpm = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
bNeedTemp = true;
|
||||||
|
bNeedRpm = false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
bNeedTemp = false;
|
||||||
|
bNeedRpm = true;
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
|
||||||
if (selected < 0 || selected >= g_numFans)
|
|
||||||
{
|
|
||||||
// Bad selected fan index.
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//read the current fan speeds
|
NSString *temp = nil;
|
||||||
for(i=0; i<g_numFans; ++i){
|
NSString *fan = nil;
|
||||||
fanRpms[i] = [smcWrapper get_fan_rpm:i];
|
float c_temp = 0.0f;
|
||||||
}
|
int selectedRpm = 0;
|
||||||
|
|
||||||
float c_temp=[smcWrapper get_maintemp];
|
if (bNeedRpm == true) {
|
||||||
const int selectedRpm = fanRpms[selected];
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//populate Menu Items with recent Data
|
NSNumberFormatter *nc=[[[NSNumberFormatter alloc] init] autorelease];
|
||||||
|
//avoid jumping in menu bar
|
||||||
|
[nc setFormat:@"000;000;-000"];
|
||||||
|
|
||||||
// Proceed with building the strings, etc. to update the text in the menubar.
|
fan = [NSString stringWithFormat:@"%@rpm",[nc stringForObjectValue:[NSNumber numberWithFloat:selectedRpm]]];
|
||||||
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) {
|
if (bNeedTemp == true) {
|
||||||
temp=[NSString stringWithFormat:@"%@%CC",[NSNumber numberWithFloat:c_temp],(unsigned short)0xb0];
|
// Read current temperature and format text for the menubar.
|
||||||
} else {
|
c_temp = [smcWrapper get_maintemp];
|
||||||
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]]];
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const int menuBarSetting = [[defaults objectForKey:@"MenuBar"] intValue];
|
// Update the temp and/or fan speed text in the menubar.
|
||||||
if (menuBarSetting <= 1) {
|
NSMutableAttributedString *s_status = nil;
|
||||||
NSString *add;
|
NSMutableParagraphStyle *paragraphStyle = nil;
|
||||||
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]];
|
switch (menuBarSetting) {
|
||||||
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
default:
|
||||||
[paragraphStyle setAlignment:NSLeftTextAlignment];
|
case 1: {
|
||||||
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:fsize] range:NSMakeRange(0,[s_status length])];
|
int fsize = 0;
|
||||||
[s_status addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[s_status length])];
|
NSString *add = nil;
|
||||||
[s_status addAttribute:NSForegroundColorAttributeName value:(NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]] range:NSMakeRange(0,[s_status length])];
|
if (menuBarSetting==0) {
|
||||||
[statusItem setAttributedTitle:s_status];
|
add=@"\n";
|
||||||
[statusItem setImage:nil];
|
fsize=9;
|
||||||
[statusItem setAlternateImage:nil];
|
[statusItem setLength:53];
|
||||||
[paragraphStyle release];
|
} else {
|
||||||
[s_status release];
|
add=@" ";
|
||||||
}
|
fsize=11;
|
||||||
else if (menuBarSetting==2) {
|
[statusItem setLength:96];
|
||||||
[statusItem setLength:26];
|
}
|
||||||
[statusItem setTitle:nil];
|
|
||||||
[statusItem setToolTip:[NSString stringWithFormat:@"%@\n%@",temp,fan]];
|
|
||||||
[statusItem setImage:menu_image];
|
|
||||||
[statusItem setAlternateImage:menu_image_alt];
|
|
||||||
|
|
||||||
}
|
s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",temp,add,fan]];
|
||||||
else if (menuBarSetting==3) {
|
paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
||||||
[statusItem setLength:46];
|
[paragraphStyle setAlignment:NSLeftTextAlignment];
|
||||||
NSMutableAttributedString *s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",temp]];
|
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:fsize] range:NSMakeRange(0,[s_status length])];
|
||||||
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:12] range:NSMakeRange(0,[s_status length])];
|
[s_status addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[s_status length])];
|
||||||
[s_status addAttribute:NSForegroundColorAttributeName value:(NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]] 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 setAttributedTitle:s_status];
|
||||||
[statusItem setImage:nil];
|
[statusItem setImage:nil];
|
||||||
[statusItem setAlternateImage:nil];
|
[statusItem setAlternateImage:nil];
|
||||||
[s_status release];
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
case 2:
|
||||||
else if (menuBarSetting==4) {
|
// TODO: Big waste of energy to update this tooltip every X seconds when the user
|
||||||
//TODO: Main temp not used in this case, don't bother reading the main temp in this case.
|
// is unlikely to hover the smcFanControl icon over and over again.
|
||||||
[statusItem setLength:65];
|
[statusItem setLength:26];
|
||||||
NSMutableAttributedString *s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",fan]];
|
[statusItem setTitle:nil];
|
||||||
[s_status addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:12] range:NSMakeRange(0,[s_status length])];
|
[statusItem setToolTip:[NSString stringWithFormat:@"%@\n%@",temp,fan]];
|
||||||
[s_status addAttribute:NSForegroundColorAttributeName value:(NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]] range:NSMakeRange(0,[s_status length])];
|
[statusItem setImage:menu_image];
|
||||||
[statusItem setAttributedTitle:s_status];
|
[statusItem setAlternateImage:menu_image_alt];
|
||||||
[statusItem setImage:nil];
|
break;
|
||||||
[statusItem setAlternateImage:nil];
|
|
||||||
[s_status release];
|
|
||||||
|
|
||||||
}
|
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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -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**
|
#pragma mark **Helper-Methods**
|
||||||
|
|||||||
@ -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