Browse Source

Save and restore last selected LED mode

Thomas Buck 9 years ago
parent
commit
2e1736a073
4 changed files with 130 additions and 111 deletions
  1. 121
    110
      CaseLights/AppDelegate.m
  2. 3
    0
      CaseLights/GPUStats.m
  3. 1
    1
      CaseLights/Info.plist
  4. 5
    0
      CaseLights/Serial.m

+ 121
- 110
CaseLights/AppDelegate.m View File

@@ -14,9 +14,19 @@
14 14
 
15 15
 #define PREF_SERIAL_PORT @"SerialPort"
16 16
 #define PREF_LIGHTS_STATE @"LightState"
17
+#define PREF_LED_MODE @"LEDMode"
18
+
19
+#define TEXT_CPU_USAGE @"CPU Usage"
20
+#define TEXT_RAM_USAGE @"RAM Usage"
21
+#define TEXT_GPU_USAGE @"GPU Usage"
22
+#define TEXT_VRAM_USAGE @"VRAM Usage"
23
+#define TEXT_CPU_TEMPERATURE @"CPU Temperature"
24
+#define TEXT_GPU_TEMPERATURE @"GPU Temperature"
17 25
 
18 26
 @interface AppDelegate ()
19 27
 
28
+@property (weak) NSMenuItem *lastLEDMode;
29
+
20 30
 @end
21 31
 
22 32
 @implementation AppDelegate
@@ -26,10 +36,11 @@
26 36
 @synthesize buttonOff, buttonLights;
27 37
 @synthesize statusItem, statusImage;
28 38
 @synthesize staticColors;
29
-@synthesize serial;
39
+@synthesize serial, lastLEDMode;
30 40
 
31 41
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
32 42
     serial = [[Serial alloc] init];
43
+    lastLEDMode = nil;
33 44
     
34 45
     // Prepare status bar menu
35 46
     statusImage = [NSImage imageNamed:@"MenuIcon"];
@@ -42,10 +53,16 @@
42 53
     NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
43 54
     NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:PREF_SERIAL_PORT];
44 55
     [appDefaults setObject:[NSNumber numberWithBool:NO] forKey:PREF_LIGHTS_STATE];
56
+    [appDefaults setObject:@"" forKey:PREF_LED_MODE];
45 57
     [store registerDefaults:appDefaults];
46 58
     [store synchronize];
47 59
     NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
48 60
     BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
61
+    NSString *lastMode = [store stringForKey:PREF_LED_MODE];
62
+    
63
+    if ([lastMode isEqualToString:@""]) {
64
+        [buttonOff setState:NSOnState];
65
+    }
49 66
     
50 67
     // Prepare static colors menu
51 68
     staticColors = [NSDictionary dictionaryWithObjectsAndKeys:
@@ -58,30 +75,15 @@
58 75
                     [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:1.0f alpha:0.0f], @"White",
59 76
                     nil];
60 77
     for (NSString *key in [staticColors allKeys]) {
61
-        NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedStaticColor:) keyEquivalent:@""];
78
+        NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedVisualization:) keyEquivalent:@""];
79
+        if ([key isEqualToString:lastMode]) {
80
+            [item setState:NSOnState];
81
+        }
62 82
         [menuColors addItem:item];
63
-        
64
-        // TODO Enable item if it was last used
65 83
     }
66 84
     
67 85
     // TODO Prepare animations menu
68 86
     
69
-    // Check if GPU Stats are available, add menu items if so
70
-    NSNumber *usage;
71
-    NSNumber *freeVRAM;
72
-    NSNumber *usedVRAM;
73
-    if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
74
-        NSLog(@"Error reading GPU information\n");
75
-    } else {
76
-        NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:@"GPU Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
77
-        [menuVisualizations addItem:itemUsage];
78
-        
79
-        NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:@"VRAM Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
80
-        [menuVisualizations addItem:itemVRAM];
81
-        
82
-        // TODO Enable item if it was last used
83
-    }
84
-    
85 87
     JSKSystemMonitor *systemMonitor = [JSKSystemMonitor systemMonitor];
86 88
     
87 89
 #ifdef DEBUG
@@ -93,16 +95,38 @@
93 95
 #endif
94 96
     
95 97
     // Add CPU Usage menu item
96
-    NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:@"CPU Usage" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
98
+    NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
99
+    if ([lastMode isEqualToString:TEXT_CPU_USAGE]) {
100
+        [cpuUsageItem setState:NSOnState];
101
+    }
97 102
     [menuVisualizations addItem:cpuUsageItem];
98 103
     
99
-    // TODO enable item if it was last used
100
-    
101 104
     // Add Memory Usage item
102
-    NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:@"RAM Usage" action:@selector(selectedMemoryVisualization:) keyEquivalent:@""];
105
+    NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:TEXT_RAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
106
+    if ([lastMode isEqualToString:TEXT_RAM_USAGE]) {
107
+        [memoryUsageItem setState:NSOnState];
108
+    }
103 109
     [menuVisualizations addItem:memoryUsageItem];
104 110
     
105
-    // TODO enable item if it was last used
111
+    // Check if GPU Stats are available, add menu items if so
112
+    NSNumber *usage;
113
+    NSNumber *freeVRAM;
114
+    NSNumber *usedVRAM;
115
+    if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
116
+        NSLog(@"Error reading GPU information\n");
117
+    } else {
118
+        NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
119
+        if ([lastMode isEqualToString:TEXT_GPU_USAGE]) {
120
+            [itemUsage setState:NSOnState];
121
+        }
122
+        [menuVisualizations addItem:itemUsage];
123
+        
124
+        NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:TEXT_VRAM_USAGE action:@selector(selectedVisualization:) keyEquivalent:@""];
125
+        if ([lastMode isEqualToString:TEXT_VRAM_USAGE]) {
126
+            [itemVRAM setState:NSOnState];
127
+        }
128
+        [menuVisualizations addItem:itemVRAM];
129
+    }
106 130
     
107 131
     // Check available temperatures and add menu items
108 132
     JSKSMC *smc = [JSKSMC smc];
@@ -115,17 +139,19 @@
115 139
 #endif
116 140
         
117 141
         if ([key isEqualToString:@"TC0D"]) {
118
-            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"CPU Temperature" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
142
+            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_CPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
143
+            if ([lastMode isEqualToString:TEXT_CPU_TEMPERATURE]) {
144
+                [item setState:NSOnState];
145
+            }
119 146
             [menuVisualizations addItem:item];
120
-            
121
-            // TODO enable item if it was last used
122 147
         }
123 148
         
124 149
         if ([key isEqualToString:@"TG0D"]) {
125
-            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"GPU Temperature" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
150
+            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:TEXT_GPU_TEMPERATURE action:@selector(selectedVisualization:) keyEquivalent:@""];
151
+            if ([lastMode isEqualToString:TEXT_GPU_TEMPERATURE]) {
152
+                [item setState:NSOnState];
153
+            }
126 154
             [menuVisualizations addItem:item];
127
-            
128
-            // TODO enable item if it was last used
129 155
         }
130 156
     }
131 157
     
@@ -191,23 +217,46 @@
191 217
 
192 218
 - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
193 219
     if ([sender state] == NSOffState) {
220
+        lastLEDMode = nil;
221
+
194 222
         // Turn off all other LED menu items
195 223
         for (int i = 0; i < [menuColors numberOfItems]; i++) {
224
+            if ([[menuColors itemAtIndex:i] state] == NSOnState) {
225
+                lastLEDMode = [menuColors itemAtIndex:i];
226
+            }
196 227
             [[menuColors itemAtIndex:i] setState:NSOffState];
197 228
         }
198 229
         for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
230
+            if ([[menuAnimations itemAtIndex:i] state] == NSOnState) {
231
+                lastLEDMode = [menuAnimations itemAtIndex:i];
232
+            }
199 233
             [[menuAnimations itemAtIndex:i] setState:NSOffState];
200 234
         }
201 235
         for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
236
+            if ([[menuVisualizations itemAtIndex:i] state] == NSOnState) {
237
+                lastLEDMode = [menuVisualizations itemAtIndex:i];
238
+            }
202 239
             [[menuVisualizations itemAtIndex:i] setState:NSOffState];
203 240
         }
204 241
         
205 242
         // Turn on "off" menu item
206 243
         [sender setState:NSOnState];
207 244
         
245
+        // Store changed value in preferences
246
+        NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
247
+        [store setObject:@"" forKey:PREF_LED_MODE];
248
+        [store synchronize];
249
+        
250
+#ifdef DEBUG
251
+        NSLog(@"Stored new mode: \"\"!\n");
252
+#endif
253
+        
208 254
         // TODO Send command to turn off LEDs
209 255
     } else {
210
-        // TODO Try to restore last LED setting
256
+        // Try to restore last LED setting
257
+        if (lastLEDMode != nil) {
258
+            [self selectedVisualization:lastLEDMode];
259
+        }
211 260
     }
212 261
 }
213 262
 
@@ -228,28 +277,7 @@
228 277
     [store synchronize];
229 278
 }
230 279
 
231
-- (void)selectedStaticColor:(NSMenuItem *)source {
232
-    // Turn off all other LED menu items
233
-    for (int i = 0; i < [menuColors numberOfItems]; i++) {
234
-        [[menuColors itemAtIndex:i] setState:NSOffState];
235
-    }
236
-    for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
237
-        [[menuAnimations itemAtIndex:i] setState:NSOffState];
238
-    }
239
-    for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
240
-        [[menuVisualizations itemAtIndex:i] setState:NSOffState];
241
-    }
242
-    [buttonOff setState:NSOffState];
243
-    
244
-    // Turn on "off" menu item
245
-    [source setState:NSOnState];
246
-    
247
-    // TODO store new selection
248
-    
249
-    // TODO send command
250
-}
251
-
252
-- (void)selectedGPUVisualization:(NSMenuItem *)sender {
280
+- (void)selectedVisualization:(NSMenuItem *)sender {
253 281
     // Turn off all other LED menu items
254 282
     for (int i = 0; i < [menuColors numberOfItems]; i++) {
255 283
         [[menuColors itemAtIndex:i] setState:NSOffState];
@@ -261,84 +289,67 @@
261 289
         [[menuVisualizations itemAtIndex:i] setState:NSOffState];
262 290
     }
263 291
     [buttonOff setState:NSOffState];
292
+    [sender setState:NSOnState];
264 293
     
265
-    if ([sender.title isEqualToString:@"GPU Usage"]) {
266
-        // Turn on "off" menu item
267
-        [sender setState:NSOnState];
268
-        
294
+    if ([sender.title isEqualToString:TEXT_GPU_USAGE]) {
269 295
         // TODO store new selection
270 296
         
271 297
         // TODO send command
272
-    } else if ([sender.title isEqualToString:@"VRAM Usage"]) {
273
-        // Turn on "off" menu item
274
-        [sender setState:NSOnState];
275
-        
298
+    } else if ([sender.title isEqualToString:TEXT_VRAM_USAGE]) {
276 299
         // TODO store new selection
277 300
         
278 301
         // TODO send command
279
-    } else if ([sender.title isEqualToString:@"GPU Temperature"]) {
280
-        // Turn on "off" menu item
281
-        [sender setState:NSOnState];
282
-        
302
+    } else if ([sender.title isEqualToString:TEXT_GPU_TEMPERATURE]) {
283 303
         // TODO store new selection
284 304
         
285 305
         // TODO send command
286
-    } else {
287
-        NSLog(@"Unknown GPU Visualization selected!\n");
288
-    }
289
-}
290
-
291
-- (void)selectedCPUVisualization:(NSMenuItem *)sender {
292
-    // Turn off all other LED menu items
293
-    for (int i = 0; i < [menuColors numberOfItems]; i++) {
294
-        [[menuColors itemAtIndex:i] setState:NSOffState];
295
-    }
296
-    for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
297
-        [[menuAnimations itemAtIndex:i] setState:NSOffState];
298
-    }
299
-    for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
300
-        [[menuVisualizations itemAtIndex:i] setState:NSOffState];
301
-    }
302
-    [buttonOff setState:NSOffState];
303
-    
304
-    if ([sender.title isEqualToString:@"CPU Usage"]) {
305
-        // Turn on "off" menu item
306
-        [sender setState:NSOnState];
306
+    } else if ([sender.title isEqualToString:TEXT_CPU_USAGE]) {
307
+        // TODO store new selection
307 308
         
309
+        // TODO send command
310
+    } else if ([sender.title isEqualToString:TEXT_CPU_TEMPERATURE]) {
308 311
         // TODO store new selection
309 312
         
310 313
         // TODO send command
311
-    } else if ([sender.title isEqualToString:@"CPU Temperature"]) {
312
-        // Turn on "off" menu item
313
-        [sender setState:NSOnState];
314 314
         
315
+    } else if ([sender.title isEqualToString:TEXT_RAM_USAGE]) {
315 316
         // TODO store new selection
316 317
         
317 318
         // TODO send command
319
+        
318 320
     } else {
319
-        NSLog(@"Unknown CPU Visualization selected!\n");
321
+        BOOL found = NO;
322
+        
323
+        // Check if a static color was selected
324
+        for (NSString *key in [staticColors allKeys]) {
325
+            if ([sender.title isEqualToString:key]) {
326
+                found = YES;
327
+                
328
+                // TODO store new selection
329
+                
330
+                // TODO send command
331
+                
332
+            }
333
+        }
334
+        
335
+        if (found) goto end_found;
336
+        
337
+        // TODO Check if an animation was selected
338
+        
339
+        NSLog(@"Unknown LED Visualization selected!\n");
340
+        return;
320 341
     }
321
-}
322 342
 
323
-- (void)selectedMemoryVisualization:(NSMenuItem *)sender {
324
-    // Turn off all other LED menu items
325
-    for (int i = 0; i < [menuColors numberOfItems]; i++) {
326
-        [[menuColors itemAtIndex:i] setState:NSOffState];
327
-    }
328
-    for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
329
-        [[menuAnimations itemAtIndex:i] setState:NSOffState];
330
-    }
331
-    for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
332
-        [[menuVisualizations itemAtIndex:i] setState:NSOffState];
343
+    end_found: {
344
+        // Store changed value in preferences
345
+        NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
346
+        [store setObject:[sender title] forKey:PREF_LED_MODE];
347
+        [store synchronize];
348
+        
349
+#ifdef DEBUG
350
+        NSLog(@"Stored new mode: \"%@\"!\n", [sender title]);
351
+#endif
333 352
     }
334
-    [buttonOff setState:NSOffState];
335
-    
336
-    // Turn on "off" menu item
337
-    [sender setState:NSOnState];
338
-    
339
-    // TODO store new selection
340
-    
341
-    // TODO send command
342 353
 }
343 354
 
344 355
 - (void)selectedSerialPort:(NSMenuItem *)source {

+ 3
- 0
CaseLights/GPUStats.m View File

@@ -2,6 +2,9 @@
2 2
 //  GPUStats.m
3 3
 //  CaseLights
4 4
 //
5
+//  For more informations refer to this StackOverflow answer:
6
+//  http://stackoverflow.com/a/22440235
7
+//
5 8
 //  Created by Thomas Buck on 23.12.15.
6 9
 //  Copyright © 2015 xythobuz. All rights reserved.
7 10
 //

+ 1
- 1
CaseLights/Info.plist View File

@@ -21,7 +21,7 @@
21 21
 	<key>CFBundleSignature</key>
22 22
 	<string>????</string>
23 23
 	<key>CFBundleVersion</key>
24
-	<string>66</string>
24
+	<string>75</string>
25 25
 	<key>LSApplicationCategoryType</key>
26 26
 	<string>public.app-category.utilities</string>
27 27
 	<key>LSMinimumSystemVersion</key>

+ 5
- 0
CaseLights/Serial.m View File

@@ -152,6 +152,11 @@
152 152
     
153 153
     const char *data = [string UTF8String];
154 154
     size_t length = strlen(data);
155
+    
156
+#ifdef DEBUG
157
+    NSLog(@"Sending string \"%s\"...\n", data);
158
+#endif
159
+    
155 160
     ssize_t sent = 0;
156 161
     while (sent < length) {
157 162
         ssize_t ret = write(fd, data + sent, length - sent);

Loading…
Cancel
Save