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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. @synthesize serial;
  23. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  24. serial = [[Serial alloc] init];
  25. // Prepare status bar menu
  26. statusImage = [NSImage imageNamed:@"MenuIcon"];
  27. [statusImage setTemplate:YES];
  28. statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
  29. [statusItem setImage:statusImage];
  30. [statusItem setMenu:statusMenu];
  31. // Set default configuration values, load existing ones
  32. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  33. NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:PREF_SERIAL_PORT];
  34. [appDefaults setObject:[NSNumber numberWithBool:NO] forKey:PREF_LIGHTS_STATE];
  35. [store registerDefaults:appDefaults];
  36. [store synchronize];
  37. NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
  38. BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
  39. // Prepare static colors menu
  40. staticColors = [NSDictionary dictionaryWithObjectsAndKeys:
  41. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:0.0f alpha:0.0f], @"Red",
  42. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:0.0f alpha:0.0f], @"Green",
  43. [NSColor colorWithCalibratedRed:0.0f green:0.0f blue:1.0f alpha:0.0f], @"Blue",
  44. [NSColor colorWithCalibratedRed:0.0f green:1.0f blue:1.0f alpha:0.0f], @"Cyan",
  45. [NSColor colorWithCalibratedRed:1.0f green:0.0f blue:1.0f alpha:0.0f], @"Magenta",
  46. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:0.0f alpha:0.0f], @"Yellow",
  47. [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:1.0f alpha:0.0f], @"White",
  48. nil];
  49. for (NSString *key in [staticColors allKeys]) {
  50. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:key action:@selector(selectedStaticColor:) keyEquivalent:@""];
  51. [menuColors addItem:item];
  52. // TODO Enable item if it was last used
  53. }
  54. // TODO Prepare animations menu
  55. // Check if GPU Stats are available, add menu items if so
  56. NSNumber *usage;
  57. NSNumber *freeVRAM;
  58. NSNumber *usedVRAM;
  59. if ([GPUStats getGPUUsage:&usage freeVRAM:&freeVRAM usedVRAM:&usedVRAM] != 0) {
  60. NSLog(@"Error reading GPU information\n");
  61. } else {
  62. NSMenuItem *itemUsage = [[NSMenuItem alloc] initWithTitle:@"GPU Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
  63. [menuVisualizations addItem:itemUsage];
  64. NSMenuItem *itemVRAM = [[NSMenuItem alloc] initWithTitle:@"VRAM Usage" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
  65. [menuVisualizations addItem:itemVRAM];
  66. // TODO Enable item if it was last used
  67. }
  68. JSKSystemMonitor *systemMonitor = [JSKSystemMonitor systemMonitor];
  69. #ifdef DEBUG
  70. JSKMCPUUsageInfo cpuUsageInfo = systemMonitor.cpuUsageInfo;
  71. NSLog(@"CPU Usage: %.3f%%\n", cpuUsageInfo.usage);
  72. JSKMMemoryUsageInfo memoryUsageInfo = systemMonitor.memoryUsageInfo;
  73. 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));
  74. #endif
  75. // Add CPU Usage menu item
  76. NSMenuItem *cpuUsageItem = [[NSMenuItem alloc] initWithTitle:@"CPU Usage" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
  77. [menuVisualizations addItem:cpuUsageItem];
  78. // TODO enable item if it was last used
  79. // Add Memory Usage item
  80. NSMenuItem *memoryUsageItem = [[NSMenuItem alloc] initWithTitle:@"RAM Usage" action:@selector(selectedMemoryVisualization:) keyEquivalent:@""];
  81. [menuVisualizations addItem:memoryUsageItem];
  82. // TODO enable item if it was last used
  83. // Check available temperatures and add menu items
  84. JSKSMC *smc = [JSKSMC smc];
  85. for (int i = 0; i < [[smc workingTempKeys] count]; i++) {
  86. NSString *key = [smc.workingTempKeys objectAtIndex:i];
  87. #ifdef DEBUG
  88. NSString *name = [smc humanReadableNameForKey:key];
  89. NSLog(@"Sensor \"%@\": \"%@\"\n", key, name);
  90. #endif
  91. if ([key isEqualToString:@"TC0D"]) {
  92. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"CPU Temperature" action:@selector(selectedCPUVisualization:) keyEquivalent:@""];
  93. [menuVisualizations addItem:item];
  94. // TODO enable item if it was last used
  95. }
  96. if ([key isEqualToString:@"TG0D"]) {
  97. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"GPU Temperature" action:@selector(selectedGPUVisualization:) keyEquivalent:@""];
  98. [menuVisualizations addItem:item];
  99. // TODO enable item if it was last used
  100. }
  101. }
  102. // Prepare serial port menu
  103. NSArray *ports = [Serial listSerialPorts];
  104. if ([ports count] > 0) {
  105. [menuPorts removeAllItems];
  106. for (int i = 0; i < [ports count]; i++) {
  107. // Add Menu Item for this port
  108. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
  109. [menuPorts addItem:item];
  110. // Set Enabled if it was used the last time
  111. if ((savedPort != nil) && [[ports objectAtIndex:i] isEqualToString:savedPort]) {
  112. [[menuPorts itemAtIndex:i] setState:NSOnState];
  113. // Try to open serial port
  114. [serial setPortName:savedPort];
  115. if ([serial openPort]) {
  116. // Unselect it when an error occured opening the port
  117. [[menuPorts itemAtIndex:i] setState:NSOffState];
  118. }
  119. }
  120. }
  121. }
  122. // Restore previously used lights configuration
  123. if (turnOnLights) {
  124. // TODO Turn on lights
  125. [buttonLights setState:NSOnState];
  126. } else {
  127. // TODO Turn off lights
  128. }
  129. // TODO Restore previously used LED configuration
  130. }
  131. - (void)applicationWillTerminate:(NSNotification *)aNotification {
  132. // Close serial port, if it was opened
  133. if ([serial isOpen]) {
  134. [serial closePort];
  135. }
  136. }
  137. - (IBAction)relistSerialPorts:(id)sender {
  138. // Refill port list
  139. NSArray *ports = [Serial listSerialPorts];
  140. [menuPorts removeAllItems];
  141. for (int i = 0; i < [ports count]; i++) {
  142. // Add Menu Item for this port
  143. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[ports objectAtIndex:i] action:@selector(selectedSerialPort:) keyEquivalent:@""];
  144. [menuPorts addItem:item];
  145. // Mark it if it is currently open
  146. if ([serial isOpen]) {
  147. if ([[ports objectAtIndex:i] isEqualToString:[serial portName]]) {
  148. [[menuPorts itemAtIndex:i] setState:NSOnState];
  149. }
  150. }
  151. }
  152. }
  153. - (IBAction)turnLEDsOff:(NSMenuItem *)sender {
  154. if ([sender state] == NSOffState) {
  155. // Turn off all other LED menu items
  156. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  157. [[menuColors itemAtIndex:i] setState:NSOffState];
  158. }
  159. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  160. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  161. }
  162. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  163. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  164. }
  165. // Turn on "off" menu item
  166. [sender setState:NSOnState];
  167. // TODO Send command to turn off LEDs
  168. } else {
  169. // TODO Try to restore last LED setting
  170. }
  171. }
  172. - (IBAction)toggleLights:(NSMenuItem *)sender {
  173. if ([sender state] == NSOffState) {
  174. // TODO Turn on lights
  175. [sender setState:NSOnState];
  176. } else {
  177. // TODO Turn off lights
  178. [sender setState:NSOffState];
  179. }
  180. // Store changed value in preferences
  181. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  182. [store setBool:([sender state] == NSOnState) forKey:PREF_LIGHTS_STATE];
  183. [store synchronize];
  184. }
  185. - (void)selectedStaticColor:(NSMenuItem *)source {
  186. // Turn off all other LED menu items
  187. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  188. [[menuColors itemAtIndex:i] setState:NSOffState];
  189. }
  190. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  191. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  192. }
  193. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  194. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  195. }
  196. [buttonOff setState:NSOffState];
  197. // Turn on "off" menu item
  198. [source setState:NSOnState];
  199. // TODO store new selection
  200. // TODO send command
  201. }
  202. - (void)selectedGPUVisualization:(NSMenuItem *)sender {
  203. // Turn off all other LED menu items
  204. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  205. [[menuColors itemAtIndex:i] setState:NSOffState];
  206. }
  207. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  208. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  209. }
  210. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  211. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  212. }
  213. [buttonOff setState:NSOffState];
  214. if ([sender.title isEqualToString:@"GPU Usage"]) {
  215. // Turn on "off" menu item
  216. [sender setState:NSOnState];
  217. // TODO store new selection
  218. // TODO send command
  219. } else if ([sender.title isEqualToString:@"VRAM 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:@"GPU 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 GPU Visualization selected!\n");
  231. }
  232. }
  233. - (void)selectedCPUVisualization:(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. if ([sender.title isEqualToString:@"CPU Usage"]) {
  246. // Turn on "off" menu item
  247. [sender setState:NSOnState];
  248. // TODO store new selection
  249. // TODO send command
  250. } else if ([sender.title isEqualToString:@"CPU Temperature"]) {
  251. // Turn on "off" menu item
  252. [sender setState:NSOnState];
  253. // TODO store new selection
  254. // TODO send command
  255. } else {
  256. NSLog(@"Unknown CPU Visualization selected!\n");
  257. }
  258. }
  259. - (void)selectedMemoryVisualization:(NSMenuItem *)sender {
  260. // Turn off all other LED menu items
  261. for (int i = 0; i < [menuColors numberOfItems]; i++) {
  262. [[menuColors itemAtIndex:i] setState:NSOffState];
  263. }
  264. for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
  265. [[menuAnimations itemAtIndex:i] setState:NSOffState];
  266. }
  267. for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
  268. [[menuVisualizations itemAtIndex:i] setState:NSOffState];
  269. }
  270. [buttonOff setState:NSOffState];
  271. // Turn on "off" menu item
  272. [sender setState:NSOnState];
  273. // TODO store new selection
  274. // TODO send command
  275. }
  276. - (void)selectedSerialPort:(NSMenuItem *)source {
  277. // Store selection for next start-up
  278. NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
  279. [store setObject:[source title] forKey:PREF_SERIAL_PORT];
  280. [store synchronize];
  281. // De-select all other ports
  282. for (int i = 0; i < [menuPorts numberOfItems]; i++) {
  283. [[menuPorts itemAtIndex:i] setState:NSOffState];
  284. }
  285. // Select only the current port
  286. [source setState:NSOnState];
  287. // Close previously opened port, if any
  288. if ([serial isOpen]) {
  289. [serial closePort];
  290. }
  291. // Try to open selected port
  292. [serial setPortName:[source title]];
  293. if ([serial openPort] != 0) {
  294. [source setState:NSOffState];
  295. } else {
  296. // TODO Restore the current configuration
  297. }
  298. }
  299. - (IBAction)showAbout:(id)sender {
  300. [NSApp activateIgnoringOtherApps:YES];
  301. [application orderFrontStandardAboutPanel:self];
  302. }
  303. @end