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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. // Check available temperatures and add menu items
  67. JSKSMC *smc = [JSKSMC smc];
  68. for (int i = 0; i < [[smc workingTempKeys] count]; i++) {
  69. NSString *key = [smc.workingTempKeys objectAtIndex:i];
  70. //NSString *name = [smc humanReadableNameForKey:key];
  71. //NSLog(@"%@: %@\n", key, name);
  72. if ([key isEqualToString:@"TC0D"]) {
  73. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"CPU Temperature" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
  74. [menuVisualizations addItem:item];
  75. // TODO enable item if it was last used
  76. }
  77. // TODO add GPU temperature item
  78. }
  79. // Add CPU Usage menu item
  80. NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:@"CPU Usage" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
  81. [menuVisualizations addItem:cpuUsageItem];
  82. // TODO enable item if it was last used
  83. // Add Memory Usage item
  84. NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:@"RAM Usage" action:@selector(selectedMemoryVisualization:) keyEquivalent:@""];
  85. [menuVisualizations addItem:memoryUsageItem];
  86. // TODO enable item if it was last used
  87. JSKSystemMonitor *systemMonitor = [JSKSystemMonitor systemMonitor];
  88. //JSKMCPUUsageInfo cpuUsageInfo = systemMonitor.cpuUsageInfo;
  89. JSKMMemoryUsageInfo memoryUsageInfo = systemMonitor.memoryUsageInfo;
  90. NSLog(@"Memory Usage: %lld Free, %lld Used, %lld Active, %lld Inactive, %lld Compressed, %lld Wired\n", memoryUsageInfo.freeMemory / 1000000, memoryUsageInfo.usedMemory / 1000000, memoryUsageInfo.activeMemory / 1000000, memoryUsageInfo.inactiveMemory / 1000000, memoryUsageInfo.compressedMemory / 1000000, memoryUsageInfo.wiredMemory / 1000000);
  91. // Prepare serial port menu
  92. NSArray *ports = [Serial listSerialPorts];
  93. if ([ports count] > 0) {
  94. [menuPorts removeAllItems];
  95. for (int i = 0; i < [ports count]; i++) {
  96. // Add Menu Item for this port
  97. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
  98. [menuPorts addItem:item];
  99. // Set Enabled if it was used the last time
  100. if ((savedPort != nil) && [[ports objectAtIndex:i] isEqualToString:savedPort]) {
  101. [[menuPorts itemAtIndex:i] setState:NSOnState];
  102. }
  103. }
  104. }
  105. // TODO Open serial port, if it was already specified and found
  106. // Restore previously used lights configuration
  107. if (turnOnLights) {
  108. // TODO Turn on lights
  109. [buttonLights setState:NSOnState];
  110. } else {
  111. // TODO Turn off lights
  112. }
  113. // TODO Restore previously used LED configuration
  114. }
  115. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  116. // TODO Close serial port, if it was specified and opened
  117. }
  118. - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
  119. if ([sender state] == NSOffState) {
  120. // Turn off all other LED menu items
  121. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  122. [[menuColors itemAtIndex:i] setState:NSOffState];
  123. }
  124. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  125. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  126. }
  127. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  128. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  129. }
  130. // Turn on "off" menu item
  131. [sender setState:NSOnState];
  132. // TODO Send command to turn off LEDs
  133. } else {
  134. // TODO Try to restore last LED setting
  135. }
  136. }
  137. - (IBAction)toggleLights:(NSMenuItem *)sender {
  138. if ([sender state] == NSOffState) {
  139. // TODO Turn on lights
  140. [sender setState:NSOnState];
  141. } else {
  142. // TODO Turn off lights
  143. [sender setState:NSOffState];
  144. }
  145. // Store changed value in preferences
  146. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  147. [store setBool:([sender state] == NSOnState) forKey:PREF_LIGHTS_STATE];
  148. [store synchronize];
  149. }
  150. - (void)selectedStaticColor:(NSMenuItem *)source {
  151. // Turn off all other LED menu items
  152. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  153. [[menuColors itemAtIndex:i] setState:NSOffState];
  154. }
  155. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  156. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  157. }
  158. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  159. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  160. }
  161. [buttonOff setState:NSOffState];
  162. // Turn on "off" menu item
  163. [source setState:NSOnState];
  164. // TODO store new selection
  165. // TODO send command
  166. }
  167. - (void)selectedGPUVisualization:(NSMenuItem *)sender {
  168. // Turn off all other LED menu items
  169. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  170. [[menuColors itemAtIndex:i] setState:NSOffState];
  171. }
  172. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  173. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  174. }
  175. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  176. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  177. }
  178. [buttonOff setState:NSOffState];
  179. if ([sender.title isEqualToString:@"GPU Usage"]) {
  180. // Turn on "off" menu item
  181. [sender setState:NSOnState];
  182. // TODO store new selection
  183. // TODO send command
  184. } else if ([sender.title isEqualToString:@"VRAM Usage"]) {
  185. // Turn on "off" menu item
  186. [sender setState:NSOnState];
  187. // TODO store new selection
  188. // TODO send command
  189. } else {
  190. NSLog(@"Unknown GPU Visualization selected!\n");
  191. }
  192. }
  193. - (void)selectedCPUVisualization:(NSMenuItem *)sender {
  194. // Turn off all other LED menu items
  195. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  196. [[menuColors itemAtIndex:i] setState:NSOffState];
  197. }
  198. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  199. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  200. }
  201. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  202. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  203. }
  204. [buttonOff setState:NSOffState];
  205. if ([sender.title isEqualToString:@"CPU Usage"]) {
  206. // Turn on "off" menu item
  207. [sender setState:NSOnState];
  208. // TODO store new selection
  209. // TODO send command
  210. } else if ([sender.title isEqualToString:@"CPU Temperature"]) {
  211. // Turn on "off" menu item
  212. [sender setState:NSOnState];
  213. // TODO store new selection
  214. // TODO send command
  215. } else {
  216. NSLog(@"Unknown CPU Visualization selected!\n");
  217. }
  218. }
  219. - (void)selectedMemoryVisualization:(NSMenuItem *)sender {
  220. // Turn off all other LED menu items
  221. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  222. [[menuColors itemAtIndex:i] setState:NSOffState];
  223. }
  224. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  225. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  226. }
  227. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  228. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  229. }
  230. [buttonOff setState:NSOffState];
  231. // Turn on "off" menu item
  232. [sender setState:NSOnState];
  233. // TODO store new selection
  234. // TODO send command
  235. }
  236. - (void)selectedSerialPort:(NSMenuItem *)source {
  237. // Store selection for next start-up
  238. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  239. [store setObject:[source title] forKey:PREF_SERIAL_PORT];
  240. [store synchronize];
  241. // De-select all other ports
  242. for (int i = 0; i < [menuPorts numberOfItems]; i++) {
  243. [[menuPorts itemAtIndex:i] setState:NSOffState];
  244. }
  245. // Select only the current port
  246. [source setState:NSOnState];
  247. // TODO Close previously opened port, if any
  248. // TODO Try to open selected port
  249. }
  250. - (IBAction)showAbout:(id)sender {
  251. [NSApp activateIgnoringOtherApps:YES];
  252. [application orderFrontStandardAboutPanel:self];
  253. }
  254. @end