Skip to content

Commit

Permalink
Moved Preference Strings into Constants
Browse files Browse the repository at this point in the history
  • Loading branch information
hholtmann committed Oct 16, 2016
1 parent b5103c8 commit 2d69dcc
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 62 deletions.
28 changes: 28 additions & 0 deletions Classes/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Constants.h
// smcFanControl
//
// Created by Hendrik Holtmann on 16/10/16.
//
//

#define PREF_TEMP_UNIT @"Unit"
#define PREF_SELECTION_DEFAULT @"SelDefault"
#define PREF_AUTOSTART_ENABLED @"AutoStart"
#define PREF_AUTOMATIC_CHANGE @"AutomaticChange"
#define PREF_BATTERY_SELECTION @"selbatt"
#define PREF_AC_SELECTION @"selac"
#define PREF_CHARGING_SELECTION @"selload"
#define PREF_MENU_DISPLAYMODE @"MenuBar"
#define PREF_TEMPERATURE_SENSOR @"TSensor"
#define PREF_NUMBEROF_LAUNCHES @"NumLaunches"
#define PREF_DONATIONMESSAGE_DISPLAY @"DonationMessageDisplay"
#define PREF_MENU_TEXTCOLOR @"MenuColor"
#define PREF_FAVORITES_ARRAY @"Favorites"

#define PREF_FAN_ARRAY @"FanData"
#define PREF_FAN_TITLE @"Title"
#define PREF_FAN_MINSPEED @"Minspeed"
#define PREF_FAN_SELSPEED @"selspeed"
#define PREF_FAN_SYNC @"sync"
#define PREF_FAN_SHOWMENU @"menu"
1 change: 1 addition & 0 deletions Classes/FanControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h>
#import "Constants.h"


#define kMenuBarHeight 22
Expand Down
114 changes: 57 additions & 57 deletions Classes/FanControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ -(void)upgradeFavorites
for (i=0;i<[rfavorites count];i++)
{
BOOL selected = NO;
NSArray *fans = rfavorites[i][@"FanData"];
NSArray *fans = rfavorites[i][PREF_FAN_ARRAY];
for (j=0;j<[fans count];j++) {
if ([fans[j][@"menu"] boolValue] == YES ) {
if ([fans[j][PREF_FAN_SHOWMENU] boolValue] == YES ) {
selected = YES;
}
}
if (selected==NO) {
rfavorites[i][@"FanData"][0][@"menu"] = @YES;
rfavorites[i][PREF_FAN_ARRAY][0][PREF_FAN_SHOWMENU] = @YES;
}
}

Expand All @@ -111,21 +111,21 @@ -(void) awakeFromNib {

NSMutableArray *favorites = [[NSMutableArray alloc] init];

NSMutableDictionary *defaultFav = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Default", @"Title",
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[[mdefaults get_machine_defaults] objectForKey:@"Fans"]]], @"FanData",nil];
NSMutableDictionary *defaultFav = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Default", PREF_FAN_TITLE,
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[[mdefaults get_machine_defaults] objectForKey:@"Fans"]]], PREF_FAN_ARRAY,nil];

[favorites addObject:defaultFav];


NSRange range=[[MachineDefaults computerModel] rangeOfString:@"MacBook"];
if (range.length>0) {
//for macbooks add a second default
NSMutableDictionary *higherFav=[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Higher RPM", @"Title",
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[[mdefaults get_machine_defaults] objectForKey:@"Fans"]]], @"FanData",nil];
NSMutableDictionary *higherFav=[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Higher RPM", PREF_FAN_TITLE,
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[[mdefaults get_machine_defaults] objectForKey:@"Fans"]]], PREF_FAN_ARRAY,nil];
for (NSUInteger i=0;i<[_machineDefaultsDict[@"Fans"] count];i++) {

int min_value=([[[[_machineDefaultsDict objectForKey:@"Fans"] objectAtIndex:i] objectForKey:@"Minspeed"] intValue])*2;
[[[higherFav objectForKey:@"FanData"] objectAtIndex:i] setObject:[NSNumber numberWithInt:min_value] forKey:@"selspeed"];
int min_value=([[[[_machineDefaultsDict objectForKey:@"Fans"] objectAtIndex:i] objectForKey:PREF_FAN_MINSPEED] intValue])*2;
[[[higherFav objectForKey:PREF_FAN_ARRAY] objectAtIndex:i] setObject:[NSNumber numberWithInt:min_value] forKey:PREF_FAN_SELSPEED];
}
[favorites addObject:higherFav];

Expand All @@ -141,19 +141,19 @@ -(void) awakeFromNib {
defaults = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@0, @"Unit",
@0, @"SelDefault",
@NO, @"AutoStart",
@NO,@"AutomaticChange",
@0,@"selbatt",
@0,@"selac",
@0,@"selload",
@0,@"MenuBar",
@"TC0D",@"TSensor",
@0,@"NumLaunches",
@NO,@"DonationMessageShown",
[NSArchiver archivedDataWithRootObject:[NSColor blackColor]],@"MenuColor",
favorites,@"Favorites",
@0, PREF_TEMP_UNIT,
@0, PREF_SELECTION_DEFAULT,
@NO,PREF_AUTOSTART_ENABLED,
@NO,PREF_AUTOMATIC_CHANGE,
@0, PREF_BATTERY_SELECTION,
@0, PREF_AC_SELECTION,
@0, PREF_CHARGING_SELECTION,
@0, PREF_MENU_DISPLAYMODE,
@"TC0D",PREF_TEMPERATURE_SENSOR,
@0, PREF_NUMBEROF_LAUNCHES,
@NO,PREF_DONATIONMESSAGE_DISPLAY,
[NSArchiver archivedDataWithRootObject:[NSColor blackColor]],PREF_MENU_TEXTCOLOR,
favorites,PREF_FAVORITES_ARRAY,
nil]];


Expand All @@ -169,15 +169,15 @@ -(void) awakeFromNib {

[FavoritesController bind:@"content"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.Favorites"
withKeyPath:[@"values." stringByAppendingString:PREF_FAVORITES_ARRAY]
options:nil];
[FavoritesController setEditable:YES];

// set slider sync - only for MBP
for (i=0;i<[[FavoritesController arrangedObjects] count];i++) {
if([[FavoritesController arrangedObjects][i][@"sync"] boolValue]==YES) {
if([[FavoritesController arrangedObjects][i][PREF_FAN_SYNC] boolValue]==YES) {
[FavoritesController setSelectionIndex:i];
[self syncBinder:[[FavoritesController arrangedObjects][i][@"sync"] boolValue]];
[self syncBinder:[[FavoritesController arrangedObjects][i][PREF_FAN_SYNC] boolValue]];
}
}

Expand All @@ -198,8 +198,8 @@ -(void) awakeFromNib {
[autochange setEnabled:false];
}
[faqText replaceCharactersInRange:NSMakeRange(0,0) withRTF: [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"F.A.Q" ofType:@"rtf"]]];
[self apply_settings:nil controllerindex:[[defaults objectForKey:@"SelDefault"] intValue]];
[[[[theMenu itemWithTag:1] submenu] itemAtIndex:[[defaults objectForKey:@"SelDefault"] intValue]] setState:NSOnState];
[self apply_settings:nil controllerindex:[[defaults objectForKey:PREF_SELECTION_DEFAULT] intValue]];
[[[[theMenu itemWithTag:1] submenu] itemAtIndex:[[defaults objectForKey:PREF_SELECTION_DEFAULT] intValue]] setState:NSOnState];
[[sliderCell dataCell] setControlSize:NSSmallControlSize];
[self changeMenu:nil];

Expand All @@ -222,10 +222,10 @@ -(void) awakeFromNib {
[self upgradeFavorites];

//autostart
[[NSUserDefaults standardUserDefaults] setValue:@([self isInAutoStart]) forKey:@"AutoStart"];
NSUInteger numLaunches = [[[NSUserDefaults standardUserDefaults] objectForKey:@"NumLaunches"] integerValue];
[[NSUserDefaults standardUserDefaults] setObject:@(numLaunches+1) forKey:@"NumLaunches"];
if (numLaunches != 0 && (numLaunches % 5 == 0) && ![[[NSUserDefaults standardUserDefaults] objectForKey:@"DonationMessageShown"] boolValue]) {
[[NSUserDefaults standardUserDefaults] setValue:@([self isInAutoStart]) forKey:PREF_AUTOSTART_ENABLED];
NSUInteger numLaunches = [[[NSUserDefaults standardUserDefaults] objectForKey:PREF_NUMBEROF_LAUNCHES] integerValue];
[[NSUserDefaults standardUserDefaults] setObject:@(numLaunches+1) forKey:PREF_NUMBEROF_LAUNCHES];
if (numLaunches != 0 && (numLaunches % 3 == 0) && ![[[NSUserDefaults standardUserDefaults] objectForKey:PREF_DONATIONMESSAGE_DISPLAY] boolValue]) {
[self displayDonationMessage];
}

Expand All @@ -241,9 +241,9 @@ -(void)displayDonationMessage
NSModalResponse code=[alert runModal];
if (code == NSAlertDefaultReturn) {
[self paypal:nil];
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"DonationMessageShown"];
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:PREF_DONATIONMESSAGE_DISPLAY];
} else if (code == NSAlertAlternateReturn) {
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"DonationMessageShown"];
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:PREF_DONATIONMESSAGE_DISPLAY];
}
}

Expand Down Expand Up @@ -296,7 +296,7 @@ - (IBAction)close_favorite:(id)sender{
- (IBAction)save_favorite:(id)sender{
MachineDefaults *msdefaults=[[MachineDefaults alloc] init:nil];
if ([[newfavorite_title stringValue] length]>0) {
NSMutableDictionary *toinsert=[[NSMutableDictionary alloc] initWithObjectsAndKeys:[newfavorite_title stringValue],@"Title",[msdefaults get_machine_defaults][@"Fans"],@"FanData",nil]; //default as template
NSMutableDictionary *toinsert=[[NSMutableDictionary alloc] initWithObjectsAndKeys:[newfavorite_title stringValue],@"Title",[msdefaults get_machine_defaults][@"Fans"],PREF_FAN_ARRAY,nil]; //default as template
[toinsert setValue:@0 forKey:@"Standard"];
[FavoritesController addObject:toinsert];
[newfavoritewindow close];
Expand All @@ -318,9 +318,9 @@ - (void) deleteAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode con
{
if (returnCode==0) {
//delete favorite, but resets presets before
[self check_deletion:@"selbatt"];
[self check_deletion:@"selac"];
[self check_deletion:@"selload"];
[self check_deletion:PREF_BATTERY_SELECTION];
[self check_deletion:PREF_AC_SELECTION];
[self check_deletion:PREF_CHARGING_SELECTION];
[FavoritesController removeObjects:[FavoritesController selectedObjects]];
}
}
Expand Down Expand Up @@ -349,7 +349,7 @@ -(void) readFanData:(id)caller{
// as low as possible.
bool bNeedTemp = false;
bool bNeedRpm = false;
const int menuBarSetting = [[defaults objectForKey:@"MenuBar"] intValue];
const int menuBarSetting = [[defaults objectForKey:PREF_MENU_DISPLAYMODE] intValue];
switch (menuBarSetting) {
default:
case 1:
Expand Down Expand Up @@ -380,10 +380,10 @@ -(void) readFanData:(id)caller{

if (bNeedRpm == true) {
// Read the current fan speed for the desired fan and format text for display in the menubar.
NSArray *fans = [FavoritesController arrangedObjects][[FavoritesController selectionIndex]][@"FanData"];
NSArray *fans = [FavoritesController arrangedObjects][[FavoritesController selectionIndex]][PREF_FAN_ARRAY];
for (i=0; i<g_numFans && i<[fans count]; i++)
{
if ([fans[i][@"menu"] boolValue]==YES) {
if ([fans[i][PREF_FAN_SHOWMENU] boolValue]==YES) {
selectedRpm = [smcWrapper get_fan_rpm:i];
break;
}
Expand All @@ -400,7 +400,7 @@ -(void) readFanData:(id)caller{
// Read current temperature and format text for the menubar.
c_temp = [smcWrapper get_maintemp];

if ([[defaults objectForKey:@"Unit"] intValue]==0) {
if ([[defaults objectForKey:PREF_TEMP_UNIT] intValue]==0) {
temp = [NSString stringWithFormat:@"%@%CC",@(c_temp),(unsigned short)0xb0];
} else {
NSNumberFormatter *ncf=[[NSNumberFormatter alloc] init];
Expand All @@ -413,7 +413,7 @@ -(void) readFanData:(id)caller{
NSMutableAttributedString *s_status = nil;
NSMutableParagraphStyle *paragraphStyle = nil;

NSColor *menuColor = (NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"MenuColor"]];
NSColor *menuColor = (NSColor*)[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:PREF_MENU_TEXTCOLOR]];
BOOL setColor = NO;
if (!([[menuColor colorUsingColorSpaceName:
NSCalibratedWhiteColorSpace] whiteComponent] == 0.0) || ![statusItem respondsToSelector:@selector(button)]) setColor = YES;
Expand Down Expand Up @@ -517,7 +517,7 @@ -(void) readFanData:(id)caller{

- (IBAction)savePreferences:(id)sender{
[(NSUserDefaultsController *)DefaultsController save:sender];
[defaults setValue:[FavoritesController content] forKey:@"Favorites"];
[defaults setValue:[FavoritesController content] forKey:PREF_FAVORITES_ARRAY];
[defaults synchronize];
[mainwindow close];
[self apply_settings:sender controllerindex:[FavoritesController selectionIndex]];
Expand All @@ -538,8 +538,8 @@ -(void)apply_settings:(id)sender controllerindex:(int)cIndex{
int i;
[FanControl setRights];
[FavoritesController setSelectionIndex:cIndex];
for (i=0;i<[[FavoritesController arrangedObjects][cIndex][@"FanData"] count];i++) {
[smcWrapper setKey_external:[NSString stringWithFormat:@"F%dMn",i] value:[[FanController arrangedObjects][i][@"selspeed"] tohex]];
for (i=0;i<[[FavoritesController arrangedObjects][cIndex][PREF_FAN_ARRAY] count];i++) {
[smcWrapper setKey_external:[NSString stringWithFormat:@"F%dMn",i] value:[[FanController arrangedObjects][i][PREF_FAN_SELSPEED] tohex]];
}
NSMenu *submenu = [[NSMenu alloc] init];

Expand All @@ -557,9 +557,9 @@ -(void)apply_settings:(id)sender controllerindex:(int)cIndex{
[[[[theMenu itemWithTag:1] submenu] itemAtIndex:i] setState:NSOffState];
}
[[[[theMenu itemWithTag:1] submenu] itemAtIndex:cIndex] setState:NSOnState];
[defaults setObject:@(cIndex) forKey:@"SelDefault"];
[defaults setObject:@(cIndex) forKey:PREF_SELECTION_DEFAULT];
//change active setting display
[[theMenu itemWithTag:1] setTitle:[NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"Active Setting",nil),[FavoritesController arrangedObjects][[FavoritesController selectionIndex]][@"Title"] ]];
[[theMenu itemWithTag:1] setTitle:[NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"Active Setting",nil),[FavoritesController arrangedObjects][[FavoritesController selectionIndex]][PREF_FAN_TITLE] ]];
}


Expand Down Expand Up @@ -599,7 +599,7 @@ - (IBAction)syncSliders:(id)sender{


- (IBAction) changeMenu:(id)sender{
if ([[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"MenuBar"] intValue]==2) {
if ([[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:PREF_MENU_DISPLAYMODE] intValue]==2) {
[colorSelector setEnabled:NO];
} else {
[colorSelector setEnabled:YES];
Expand All @@ -612,7 +612,7 @@ - (IBAction)menuSelect:(id)sender{
int i;
for (i=0;i<[[FanController arrangedObjects] count];i++) {
if (i!=[sender selectedRow]) {
[[FanController arrangedObjects][i] setValue:@NO forKey:@"menu"];
[[FanController arrangedObjects][i] setValue:@NO forKey:PREF_FAN_SHOWMENU];
}
}
}
Expand Down Expand Up @@ -700,11 +700,11 @@ -(void) syncBinder:(Boolean)bind{
//in case plist is corrupt, don't bind
if ([[FanController arrangedObjects] count]>1 ) {
if (bind==YES) {
[[FanController arrangedObjects][1] bind:@"selspeed" toObject:[FanController arrangedObjects][0] withKeyPath:@"selspeed" options:nil];
[[FanController arrangedObjects][0] bind:@"selspeed" toObject:[FanController arrangedObjects][1] withKeyPath:@"selspeed" options:nil];
[[FanController arrangedObjects][1] bind:PREF_FAN_SELSPEED toObject:[FanController arrangedObjects][0] withKeyPath:PREF_FAN_SELSPEED options:nil];
[[FanController arrangedObjects][0] bind:PREF_FAN_SELSPEED toObject:[FanController arrangedObjects][1] withKeyPath:PREF_FAN_SELSPEED options:nil];
} else {
[[FanController arrangedObjects][1] unbind:@"selspeed"];
[[FanController arrangedObjects][0] unbind:@"selspeed"];
[[FanController arrangedObjects][1] unbind:PREF_FAN_SELSPEED];
[[FanController arrangedObjects][0] unbind:PREF_FAN_SELSPEED];
}
}
}
Expand All @@ -713,27 +713,27 @@ -(void) syncBinder:(Boolean)bind{
#pragma mark **Power Watchdog-Methods**

- (void)systemDidWakeFromSleep:(id)sender{
[self apply_settings:nil controllerindex:[[defaults objectForKey:@"SelDefault"] intValue]];
[self apply_settings:nil controllerindex:[[defaults objectForKey:PREF_SELECTION_DEFAULT] intValue]];
}


- (void)powerChangeToBattery:(id)sender{

if ([[defaults objectForKey:@"AutomaticChange"] boolValue]==YES) {
[self apply_settings:nil controllerindex:[[defaults objectForKey:@"selbatt"] intValue]];
[self apply_settings:nil controllerindex:[[defaults objectForKey:PREF_BATTERY_SELECTION] intValue]];
}
}

- (void)powerChangeToAC:(id)sender{
if ([[defaults objectForKey:@"AutomaticChange"] boolValue]==YES) {
[self apply_settings:nil controllerindex:[[defaults objectForKey:@"selac"] intValue]];
[self apply_settings:nil controllerindex:[[defaults objectForKey:PREF_AC_SELECTION] intValue]];

}
}

- (void)powerChangeToACLoading:(id)sender{
if ([[defaults objectForKey:@"AutomaticChange"] boolValue]==YES) {
[self apply_settings:nil controllerindex:[[defaults objectForKey:@"selload"] intValue]];
[self apply_settings:nil controllerindex:[[defaults objectForKey:PREF_CHARGING_SELECTION] intValue]];

}
}
Expand Down
1 change: 1 addition & 0 deletions Classes/smcWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import <Cocoa/Cocoa.h>
#import <smc.h>
#import <MachineDefaults.h>
#import "Constants.h"

@interface smcWrapper : NSObject {
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/smcWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ +(float)readTempSensors
{
float retValue;
SMCVal_t val;
NSString *sensor = [[NSUserDefaults standardUserDefaults] objectForKey:@"TSensor"];
NSString *sensor = [[NSUserDefaults standardUserDefaults] objectForKey:PREF_TEMPERATURE_SENSOR];
SMCReadKey2((char*)[sensor UTF8String], &val,conn);
retValue= ((val.bytes[0] * 256 + val.bytes[1]) >> 2)/64;
allSensors = [NSArray arrayWithObjects:@"TC0D",@"TC0P",@"TCAD",@"TC0H",@"TC0F",@"TCAH",@"TCBH",nil];
Expand All @@ -50,7 +50,7 @@ +(float)readTempSensors
SMCReadKey2((char*)[sensor UTF8String], &val,conn);
retValue= ((val.bytes[0] * 256 + val.bytes[1]) >> 2)/64;
if (retValue>0 && floor(retValue) != 129 ) {
[[NSUserDefaults standardUserDefaults] setObject:sensor forKey:@"TSensor"];
[[NSUserDefaults standardUserDefaults] setObject:sensor forKey:PREF_TEMPERATURE_SENSOR];
[[NSUserDefaults standardUserDefaults] synchronize];
break;
}
Expand Down
Loading

0 comments on commit 2d69dcc

Please sign in to comment.