Simple RGB LED controller for Mac OS X
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppDelegate.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // AppDelegate.m
  3. // CaseLights
  4. //
  5. // Created by Thomas Buck on 21.12.15.
  6. // Copyright © 2015 xythobuz. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "Serial.h"
  10. #import "GPUStats.h"
  11. #import <SystemInfoKit/SystemInfoKit.h>
  12. #define PREF_SERIAL_PORT @"SerialPort"
  13. #define PREF_LIGHTS_STATE @"LightState"
  14. @interface AppDelegate ()
  15. @end
  16. @implementation AppDelegate
  17. @synthesize statusMenu, application;
  18. @synthesize menuColors, menuAnimations, menuVisualizations, menuPorts;
  19. @synthesize buttonOff, buttonLights;
  20. @synthesize statusItem, statusImage;
  21. @synthesize staticColors;
  22. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  23. // Prepare status bar menu
  24. statusImage = [NSImage imageNamed:@"MenuIcon"];
  25. [statusImage setTemplate:YES];
  26. statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
  27. [statusItem setImage:statusImage];
  28. [statusItem setMenu:statusMenu];
  29. // Set default configuration values, load existing ones
  30. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  31. NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:PREF_SERIAL_PORT];
  32. [appDefaults setObject:[NSNumber numberWithBool:NO] forKey:PREF_LIGHTS_STATE];
  33. [store registerDefaults:appDefaults];
  34. [store synchronize];
  35. NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
  36. BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
  37. // Prepare static colors menu
  38. staticColors = [NSDictionary dictionaryWithObjectsAndKeys:
  39. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:0.0f alpha:0.0f], @"Red",
  40. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:0.0f alpha:0.0f], @"Green",
  41. [NSColor colorWithCalibratedRed:0.0f green:0.0f blue:1.0f alpha:0.0f], @"Blue",
  42. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:1.0f alpha:0.0f], @"Cyan",
  43. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:1.0f alpha:0.0f], @"Magenta",
  44. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:0.0f alpha:0.0f], @"Yellow",
  45. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:1.0f alpha:0.0f], @"White",
  46. nil];
  47. for (NSString *key in [staticColors allKeys]) {
  48. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedStaticColor:) keyEquivalent:@""];
  49. [menuColors addItem:item];
  50. // TODO Enable item if it was last used
  51. }
  52. // TODO Prepare animations menu
  53. // Check if GPU Stats are available, add menu items if so
  54. NSNumber *usage;
  55. NSNumber *freeVRAM;
  56. NSNumber *usedVRAM;
  57. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  58. NSLog(@"Error reading GPU information\n");
  59. } else {
  60. NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:@"GPU Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
  61. [menuVisualizations addItem:itemUsage];
  62. NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:@"VRAM Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
  63. [menuVisualizations addItem:itemVRAM];
  64. // TODO Enable item if it was last used
  65. }
  66. JSKSystemMonitor *systemMonitor = [JSKSystemMonitor systemMonitor];
  67. #ifdef DEBUG
  68. JSKMCPUUsageInfo cpuUsageInfo = systemMonitor.cpuUsageInfo;
  69. NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
  70. JSKMMemoryUsageInfo memoryUsageInfo = systemMonitor.memoryUsageInfo;
  71. NSLog(@"Memory Usage: %.2fGB Free, %.2fGB Used, %.2fGB Active, %.2fGB Inactive, %.2fGB Compressed, %.2fGB Wired\n", memoryUsageInfo.freeMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.usedMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.activeMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.inactiveMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.compressedMemory / (1024.0 * 1024.0 * 1024.0), memoryUsageInfo.wiredMemory / (1024.0 * 1024.0 * 1024.0));
  72. #endif
  73. // Add CPU Usage menu item
  74. NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:@"CPU Usage" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
  75. [menuVisualizations addItem:cpuUsageItem];
  76. // TODO enable item if it was last used
  77. // Add Memory Usage item
  78. NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:@"RAM Usage" action:@selector(selectedMemoryVisualization:) keyEquivalent:@""];
  79. [menuVisualizations addItem:memoryUsageItem];
  80. // TODO enable item if it was last used
  81. // Check available temperatures and add menu items
  82. JSKSMC *smc = [JSKSMC smc];
  83. for (int i = 0; i < [[smc workingTempKeys] count]; i++) {
  84. NSString *key = [smc.workingTempKeys objectAtIndex:i];
  85. #ifdef DEBUG
  86. NSString *name = [smc humanReadableNameForKey:key];
  87. NSLog(@"Sensor \"%@\": \"%@\"\n", key, name);
  88. #endif
  89. if ([key isEqualToString:@"TC0D"]) {
  90. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"CPU Temperature" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
  91. [menuVisualizations addItem:item];
  92. // TODO enable item if it was last used
  93. }
  94. if ([key isEqualToString:@"TG0D"]) {
  95. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"GPU Temperature" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
  96. [menuVisualizations addItem:item];
  97. // TODO enable item if it was last used
  98. }
  99. }
  100. // Prepare serial port menu
  101. NSArray *ports = [Serial listSerialPorts];
  102. if ([ports count] > 0) {
  103. [menuPorts removeAllItems];
  104. for (int i = 0; i < [ports count]; i++) {
  105. // Add Menu Item for this port
  106. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
  107. [menuPorts addItem:item];
  108. // Set Enabled if it was used the last time
  109. if ((savedPort != nil) && [[ports objectAtIndex:i] isEqualToString:savedPort]) {
  110. [[menuPorts itemAtIndex:i] setState:NSOnState];
  111. }
  112. }
  113. }
  114. // TODO Open serial port, if it was already specified and found
  115. // Restore previously used lights configuration
  116. if (turnOnLights) {
  117. // TODO Turn on lights
  118. [buttonLights setState:NSOnState];
  119. } else {
  120. // TODO Turn off lights
  121. }
  122. // TODO Restore previously used LED configuration
  123. }
  124. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  125. // TODO Close serial port, if it was specified and opened
  126. }
  127. - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
  128. if ([sender state] == NSOffState) {
  129. // Turn off all other LED menu items
  130. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  131. [[menuColors itemAtIndex:i] setState:NSOffState];
  132. }
  133. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  134. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  135. }
  136. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  137. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  138. }
  139. // Turn on "off" menu item
  140. [sender setState:NSOnState];
  141. // TODO Send command to turn off LEDs
  142. } else {
  143. // TODO Try to restore last LED setting
  144. }
  145. }
  146. - (IBAction)toggleLights:(NSMenuItem *)sender {
  147. if ([sender state] == NSOffState) {
  148. // TODO Turn on lights
  149. [sender setState:NSOnState];
  150. } else {
  151. // TODO Turn off lights
  152. [sender setState:NSOffState];
  153. }
  154. // Store changed value in preferences
  155. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  156. [store setBool:([sender state] == NSOnState) forKey:PREF_LIGHTS_STATE];
  157. [store synchronize];
  158. }
  159. - (void)selectedStaticColor:(NSMenuItem *)source {
  160. // Turn off all other LED menu items
  161. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  162. [[menuColors itemAtIndex:i] setState:NSOffState];
  163. }
  164. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  165. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  166. }
  167. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  168. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  169. }
  170. [buttonOff setState:NSOffState];
  171. // Turn on "off" menu item
  172. [source setState:NSOnState];
  173. // TODO store new selection
  174. // TODO send command
  175. }
  176. - (void)selectedGPUVisualization:(NSMenuItem *)sender {
  177. // Turn off all other LED menu items
  178. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  179. [[menuColors itemAtIndex:i] setState:NSOffState];
  180. }
  181. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  182. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  183. }
  184. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  185. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  186. }
  187. [buttonOff setState:NSOffState];
  188. if ([sender.title isEqualToString:@"GPU Usage"]) {
  189. // Turn on "off" menu item
  190. [sender setState:NSOnState];
  191. // TODO store new selection
  192. // TODO send command
  193. } else if ([sender.title isEqualToString:@"VRAM Usage"]) {
  194. // Turn on "off" menu item
  195. [sender setState:NSOnState];
  196. // TODO store new selection
  197. // TODO send command
  198. } else if ([sender.title isEqualToString:@"GPU Temperature"]) {
  199. // Turn on "off" menu item
  200. [sender setState:NSOnState];
  201. // TODO store new selection
  202. // TODO send command
  203. } else {
  204. NSLog(@"Unknown GPU Visualization selected!\n");
  205. }
  206. }
  207. - (void)selectedCPUVisualization:(NSMenuItem *)sender {
  208. // Turn off all other LED menu items
  209. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  210. [[menuColors itemAtIndex:i] setState:NSOffState];
  211. }
  212. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  213. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  214. }
  215. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  216. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  217. }
  218. [buttonOff setState:NSOffState];
  219. if ([sender.title isEqualToString:@"CPU Usage"]) {
  220. // Turn on "off" menu item
  221. [sender setState:NSOnState];
  222. // TODO store new selection
  223. // TODO send command
  224. } else if ([sender.title isEqualToString:@"CPU Temperature"]) {
  225. // Turn on "off" menu item
  226. [sender setState:NSOnState];
  227. // TODO store new selection
  228. // TODO send command
  229. } else {
  230. NSLog(@"Unknown CPU Visualization selected!\n");
  231. }
  232. }
  233. - (void)selectedMemoryVisualization:(NSMenuItem *)sender {
  234. // Turn off all other LED menu items
  235. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  236. [[menuColors itemAtIndex:i] setState:NSOffState];
  237. }
  238. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  239. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  240. }
  241. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  242. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  243. }
  244. [buttonOff setState:NSOffState];
  245. // Turn on "off" menu item
  246. [sender setState:NSOnState];
  247. // TODO store new selection
  248. // TODO send command
  249. }
  250. - (void)selectedSerialPort:(NSMenuItem *)source {
  251. // Store selection for next start-up
  252. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  253. [store setObject:[source title] forKey:PREF_SERIAL_PORT];
  254. [store synchronize];
  255. // De-select all other ports
  256. for (int i = 0; i < [menuPorts numberOfItems]; i++) {
  257. [[menuPorts itemAtIndex:i] setState:NSOffState];
  258. }
  259. // Select only the current port
  260. [source setState:NSOnState];
  261. // TODO Close previously opened port, if any
  262. // TODO Try to open selected port
  263. }
  264. - (IBAction)showAbout:(id)sender {
  265. [NSApp activateIgnoringOtherApps:YES];
  266. [application orderFrontStandardAboutPanel:self];
  267. }
  268. @end