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
|
#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,70 +308,85 @@ 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) {
|
||||||
|
default:
|
||||||
|
case 1:
|
||||||
|
bNeedTemp = true;
|
||||||
|
bNeedRpm = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
bNeedTemp = true;
|
||||||
|
bNeedRpm = true;
|
||||||
|
break;
|
||||||
|
|
||||||
// Determine which fan speed to show in the menubar.
|
case 3:
|
||||||
int selected = 0;
|
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"];
|
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) {
|
if ([[[fans objectAtIndex:i] objectForKey:@"menu"] boolValue]==YES) {
|
||||||
selected = i;
|
selectedRpm = [smcWrapper get_fan_rpm:i];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (selected < 0 || selected >= g_numFans)
|
|
||||||
{
|
|
||||||
// Bad selected fan index.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//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 ([[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];
|
NSNumberFormatter *nc=[[[NSNumberFormatter alloc] init] autorelease];
|
||||||
//avoid jumping in menu bar
|
//avoid jumping in menu bar
|
||||||
[nc setFormat:@"000;000;-000"];
|
[nc setFormat:@"000;000;-000"];
|
||||||
|
|
||||||
fan=[NSString stringWithFormat:@"%@rpm",[nc stringForObjectValue:[NSNumber numberWithFloat:selectedRpm]]];
|
fan = [NSString stringWithFormat:@"%@rpm",[nc stringForObjectValue:[NSNumber numberWithFloat:selectedRpm]]];
|
||||||
|
}
|
||||||
|
|
||||||
const int menuBarSetting = [[defaults objectForKey:@"MenuBar"] intValue];
|
if (bNeedTemp == true) {
|
||||||
if (menuBarSetting <= 1) {
|
// Read current temperature and format text for the menubar.
|
||||||
NSString *add;
|
c_temp = [smcWrapper get_maintemp];
|
||||||
int fsize;
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the temp and/or fan speed text in the menubar.
|
||||||
|
NSMutableAttributedString *s_status = nil;
|
||||||
|
NSMutableParagraphStyle *paragraphStyle = nil;
|
||||||
|
|
||||||
|
switch (menuBarSetting) {
|
||||||
|
default:
|
||||||
|
case 1: {
|
||||||
|
int fsize = 0;
|
||||||
|
NSString *add = nil;
|
||||||
if (menuBarSetting==0) {
|
if (menuBarSetting==0) {
|
||||||
add=@"\n";
|
add=@"\n";
|
||||||
fsize=9;
|
fsize=9;
|
||||||
@ -377,8 +397,8 @@ NSUserDefaults *defaults;
|
|||||||
[statusItem setLength:96];
|
[statusItem setLength:96];
|
||||||
}
|
}
|
||||||
|
|
||||||
NSMutableAttributedString *s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",temp,add,fan]];
|
s_status=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",temp,add,fan]];
|
||||||
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
||||||
[paragraphStyle setAlignment:NSLeftTextAlignment];
|
[paragraphStyle setAlignment:NSLeftTextAlignment];
|
||||||
[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:fsize] range:NSMakeRange(0,[s_status length])];
|
||||||
[s_status addAttribute:NSParagraphStyleAttributeName value:paragraphStyle 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 setAttributedTitle:s_status];
|
||||||
[statusItem setImage:nil];
|
[statusItem setImage:nil];
|
||||||
[statusItem setAlternateImage:nil];
|
[statusItem setAlternateImage:nil];
|
||||||
[paragraphStyle release];
|
break;
|
||||||
[s_status release];
|
|
||||||
}
|
}
|
||||||
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 setLength:26];
|
||||||
[statusItem setTitle:nil];
|
[statusItem setTitle:nil];
|
||||||
[statusItem setToolTip:[NSString stringWithFormat:@"%@\n%@",temp,fan]];
|
[statusItem setToolTip:[NSString stringWithFormat:@"%@\n%@",temp,fan]];
|
||||||
[statusItem setImage:menu_image];
|
[statusItem setImage:menu_image];
|
||||||
[statusItem setAlternateImage:menu_image_alt];
|
[statusItem setAlternateImage:menu_image_alt];
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
case 3:
|
||||||
else if (menuBarSetting==3) {
|
|
||||||
[statusItem setLength:46];
|
[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: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])];
|
[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 4:
|
||||||
else if (menuBarSetting==4) {
|
|
||||||
//TODO: Main temp not used in this case, don't bother reading the main temp in this case.
|
|
||||||
[statusItem setLength:65];
|
[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: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])];
|
[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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[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