light.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. #include "light.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <getopt.h>
  7. #include <sys/stat.h>
  8. #include <errno.h>
  9. void light_defaultConfig()
  10. {
  11. light_Configuration.controllerMode = LIGHT_AUTO;
  12. memset(&light_Configuration.specifiedController, '\0', 256);
  13. light_Configuration.operationMode = LIGHT_GET;
  14. light_Configuration.valueMode = LIGHT_PERCENT;
  15. light_Configuration.specifiedValueRaw = 0;
  16. light_Configuration.specifiedValuePercent = 0.0;
  17. light_Configuration.target = LIGHT_BRIGHTNESS;
  18. light_verbosity = 0;
  19. }
  20. LIGHT_BOOL light_parseArguments(int argc, char** argv)
  21. {
  22. int currFlag;
  23. LIGHT_BOOL opSet = FALSE;
  24. LIGHT_BOOL targetSet = FALSE;
  25. LIGHT_BOOL ctrlSet = FALSE;
  26. LIGHT_BOOL valSet = FALSE;
  27. unsigned long specLen = 0;
  28. while((currFlag = getopt(argc, argv, "HhVGSAUbmcas:prv:")) != -1)
  29. {
  30. switch(currFlag)
  31. {
  32. /* -- Operations -- */
  33. case 'H':
  34. case 'h':
  35. ASSERT_OPSET();
  36. light_Configuration.operationMode = LIGHT_PRINT_HELP;
  37. break;
  38. case 'V':
  39. ASSERT_OPSET();
  40. light_Configuration.operationMode = LIGHT_PRINT_VERSION;
  41. break;
  42. case 'G':
  43. ASSERT_OPSET();
  44. light_Configuration.operationMode = LIGHT_GET;
  45. break;
  46. case 'S':
  47. ASSERT_OPSET();
  48. light_Configuration.operationMode = LIGHT_SET;
  49. break;
  50. case 'A':
  51. ASSERT_OPSET();
  52. light_Configuration.operationMode = LIGHT_ADD;
  53. break;
  54. case 'U':
  55. ASSERT_OPSET();
  56. light_Configuration.operationMode = LIGHT_SUB;
  57. break;
  58. /* -- Targets -- */
  59. case 'b':
  60. ASSERT_TARGETSET();
  61. light_Configuration.target = LIGHT_BRIGHTNESS;
  62. break;
  63. case 'm':
  64. ASSERT_TARGETSET();
  65. light_Configuration.target = LIGHT_MAX_BRIGHTNESS;
  66. break;
  67. case 'c':
  68. ASSERT_TARGETSET();
  69. light_Configuration.target = LIGHT_MIN_CAP;
  70. break;
  71. /* -- Controller selection -- */
  72. case 'a':
  73. ASSERT_CTRLSET();
  74. light_Configuration.controllerMode = LIGHT_AUTO;
  75. break;;
  76. case 's':
  77. ASSERT_CTRLSET();
  78. light_Configuration.controllerMode = LIGHT_SPECIFY;
  79. if(optarg == NULL)
  80. {
  81. printf("-s NEEDS an argument.\n\n");
  82. light_printHelp();
  83. }
  84. specLen = strlen(optarg);
  85. if(specLen > 255)
  86. {
  87. specLen = 255;
  88. }
  89. strncpy(light_Configuration.specifiedController, optarg, specLen);
  90. light_Configuration.specifiedController[255] = '\0';
  91. break;
  92. /* -- Value modes -- */
  93. case 'p':
  94. ASSERT_VALSET();
  95. light_Configuration.valueMode = LIGHT_PERCENT;
  96. break;
  97. case 'r':
  98. ASSERT_VALSET();
  99. light_Configuration.valueMode = LIGHT_RAW;
  100. break;
  101. /* -- Other -- */
  102. case 'v':
  103. if(optarg == NULL)
  104. {
  105. printf("-v NEEDS an argument.\n\n");
  106. light_printHelp();
  107. return FALSE;
  108. }
  109. if(sscanf(optarg, "%i", &light_verbosity) != 1)
  110. {
  111. printf("-v Verbosity is not specified in a recognizable format.\n\n");
  112. light_printHelp();
  113. return FALSE;
  114. }
  115. if(light_verbosity < 0 || light_verbosity > 3)
  116. {
  117. printf("-v Verbosity has to be between 0 and 3.\n\n");
  118. light_printHelp();
  119. return FALSE;
  120. }
  121. break;
  122. }
  123. }
  124. /* If we need a <value> (for writing), make sure we have it! */
  125. if(light_Configuration.operationMode == LIGHT_SET ||
  126. light_Configuration.operationMode == LIGHT_ADD ||
  127. light_Configuration.operationMode == LIGHT_SUB)
  128. {
  129. if(argc - optind != 1)
  130. {
  131. printf("Light needs an argument for <value>.\n\n");
  132. light_printHelp();
  133. return FALSE;
  134. }
  135. if(light_Configuration.valueMode == LIGHT_PERCENT)
  136. {
  137. if(sscanf(argv[optind], "%lf", &light_Configuration.specifiedValuePercent) != 1){
  138. printf("<value> is not specified in a recognizable format.\n\n");
  139. light_printHelp();
  140. return FALSE;
  141. }
  142. light_Configuration.specifiedValuePercent = LIGHT_CLAMP(light_Configuration.specifiedValuePercent, 0.00, 100.00);
  143. }else{
  144. if(sscanf(argv[optind], "%lu", &light_Configuration.specifiedValueRaw) != 1){
  145. printf("<value> is not specified in a recognizable format.\n\n");
  146. light_printHelp();
  147. return FALSE;
  148. }
  149. }
  150. }
  151. return TRUE;
  152. }
  153. void light_printVersion(){
  154. printf("Light %u.%u (%s)\n", LIGHT_VER_MAJOR, LIGHT_VER_MINOR, LIGHT_VER_TYPE);
  155. printf("Copyright (C) %u %s\n", LIGHT_YEAR, LIGHT_AUTHOR);
  156. printf("This is free software, see the source for copying conditions. There is NO\n");
  157. printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\n\n");
  158. }
  159. void light_printHelp(){
  160. printf("Usage: light <options> <value>\n");
  161. printf("<value> has to be either integral(raw mode) or decimal(percent mode) depending on the specified value mode.\n");
  162. printf("<options> can be any of the following:\n\n");
  163. printf("Operations (can not be used in conjunction):\n");
  164. printf(" -H -h:\tPrints this help and exits\n");
  165. printf(" -V:\t\tPrints version info and exits\n");
  166. printf(" -G:\t\tGet value (default)\n");
  167. printf(" -S:\t\tSet value\n");
  168. printf(" -A:\t\tAdd value\n");
  169. printf(" -U:\t\tSubtract value\n\n");
  170. printf("Targets (can not be used in conjunction):\n");
  171. printf(" -b:\t\tBrightness (default)\n \t\tUsed with [GSAU]\n\n");
  172. printf(" -m:\t\tMaximum brightness\n \t\tUsed with [G]\n\n");
  173. printf(" -c:\t\tMinimum cap\n \t\tUsed with [GS]\n");
  174. printf(" \t\tG returns null if no minimum cap is set.\n\n");
  175. printf("Controller selection (can not be used in conjunction):\n");
  176. printf(" -a:\t\tSelects controller automatically (default).\n");
  177. printf(" -s:\t\tSpecify controller to use. (needs argument)\n\n");
  178. printf("Value modes (can not be used in conjunction):\n");
  179. printf(" -p:\t\tInterpret <value> as, and output values in, percent. (default)\n");
  180. printf(" -r:\t\tInterpret <value> as, and output values in, raw mode.\n\n");
  181. printf("Other:\n");
  182. printf(" -v:\t\tSets the verbosity level, (needs argument).\n \t\t0: Only outputs read values.\n \t\t1: Read values, Errors.\n \t\t2: Read values, Errors, Warnings.\n \t\t3: Read values, Errors, Warnings, Notices.\n\n");
  183. }
  184. LIGHT_BOOL light_initialize(int argc, char** argv)
  185. {
  186. int mkdirVal;
  187. light_defaultConfig();
  188. if(!light_parseArguments(argc, argv))
  189. {
  190. LIGHT_ERR("could not parse arguments");
  191. return FALSE;
  192. }
  193. if(light_Configuration.operationMode == LIGHT_PRINT_HELP || light_Configuration.operationMode == LIGHT_PRINT_VERSION)
  194. {
  195. return TRUE;
  196. }
  197. /* Make sure we have a valid /etc/light directory, as well as /etc/light/mincap */
  198. mkdirVal = mkdir("/etc/light", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  199. if(mkdirVal != 0 && errno != EEXIST)
  200. {
  201. LIGHT_ERR("/etc/light does not exist and could not be created");
  202. return FALSE;
  203. }
  204. mkdirVal = mkdir("/etc/light/mincap", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  205. if(mkdirVal != 0 && errno != EEXIST)
  206. {
  207. LIGHT_ERR("/etc/light/mincap does not exist and could not be created");
  208. return FALSE;
  209. }
  210. /* Make sure we have a valid controller before we proceed */
  211. if(light_Configuration.controllerMode == LIGHT_AUTO)
  212. {
  213. LIGHT_NOTE("Automatic mode -- finding best controller");
  214. if(!light_getBestController(light_Configuration.specifiedController))
  215. {
  216. LIGHT_ERR("could not find suitable controller");
  217. return FALSE;
  218. }
  219. }
  220. if(!light_controllerAccessible(light_Configuration.specifiedController))
  221. {
  222. LIGHT_ERR("selected controller is not valid, make sure this application is run as root.");
  223. return FALSE;
  224. }
  225. return TRUE;
  226. }
  227. LIGHT_BOOL light_execute()
  228. {
  229. unsigned long rawCurr; /* The current brightness, in raw mode */
  230. double percentCurr; /* The current brightness, in percent */
  231. unsigned long rawMax; /* The max brightness, in percent */
  232. unsigned long rawSetP; /* The final value to be set, in raw mode, when setting with percent */
  233. unsigned long rawAddP; /* The final value to be set, in raw mode, when adding with percent */
  234. unsigned long rawSubP; /* The final value to be set, in raw mode, when subtracting with percent */
  235. unsigned long rawSetR; /* The final value to be set, in raw mode, when setting with raw values */
  236. unsigned long rawAddR; /* The final value to be set, in raw mode, when adding with raw values */
  237. unsigned long rawSubR; /* The final value to be set, in raw mode, when subtracting with raw values */
  238. unsigned long minCap; /* The minimum cap, in raw mode */
  239. double percentMinCap; /* The minimum cap, in percent */
  240. LIGHT_BOOL hasMinCap; /* If we have a minimum cap */
  241. unsigned long minCapP; /* The final value to be set, in raw mode, when setting with percent (This is uncapped and used for minimum cap setting) */
  242. unsigned long minCapR; /* The final value to be set, in raw mode, when setting with raw values (This is uncapped and used for minimum cap setting) */
  243. unsigned long *writeVal; /* The actual value used for writing */
  244. /* Print help and version info */
  245. if(light_Configuration.operationMode == LIGHT_PRINT_HELP)
  246. {
  247. light_printHelp();
  248. return TRUE;
  249. }
  250. if(light_Configuration.operationMode == LIGHT_PRINT_VERSION)
  251. {
  252. light_printVersion();
  253. return TRUE;
  254. }
  255. /* Prepare variables */
  256. /* -- First, get the current, min and max values directly from controller/configuration (raw values) */
  257. if(!light_getBrightness(light_Configuration.specifiedController, &rawCurr))
  258. {
  259. LIGHT_ERR("could not get brightness");
  260. return FALSE;
  261. }
  262. if(!light_getMaxBrightness(light_Configuration.specifiedController, &rawMax))
  263. {
  264. LIGHT_ERR("could not get max brightness");
  265. return FALSE;
  266. }
  267. if(!light_getMinCap(light_Configuration.specifiedController, &hasMinCap, &minCap))
  268. {
  269. LIGHT_ERR("could not get min brightness");
  270. return FALSE;
  271. }
  272. if( hasMinCap && ( minCap < 0 || minCap > rawMax ) )
  273. {
  274. LIGHT_WARN("invalid minimum cap for controller, ignoring and using 0");
  275. minCap = 0;
  276. }
  277. /* -- Secondly, calculate the rest of the values (Clamp them here as well!) */
  278. percentCurr = LIGHT_CLAMP( ((double)rawCurr) / ((double)rawMax) * 100 , 0.00, 100.00 );
  279. percentMinCap = LIGHT_CLAMP( ((double)minCap) / ((double)rawMax) * 100 , 0.00, 100.00 );
  280. rawSetP = LIGHT_CLAMP( ((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100) , minCap, rawMax );
  281. rawAddP = LIGHT_CLAMP( ((unsigned long) ( (percentCurr + light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
  282. if( light_Configuration.specifiedValuePercent > percentCurr)
  283. {
  284. rawSubP = LIGHT_CLAMP(0, minCap, rawMax);
  285. }else{
  286. rawSubP = LIGHT_CLAMP( ((unsigned long) ( (percentCurr - light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
  287. }
  288. rawSetR = LIGHT_CLAMP( light_Configuration.specifiedValueRaw , minCap, rawMax );
  289. rawAddR = LIGHT_CLAMP( rawCurr + light_Configuration.specifiedValueRaw , minCap, rawMax );
  290. if(light_Configuration.specifiedValueRaw > rawCurr){
  291. rawSubR = LIGHT_CLAMP(0, minCap, rawMax)
  292. }else{
  293. rawSubR = LIGHT_CLAMP( rawCurr - light_Configuration.specifiedValueRaw , minCap, rawMax );
  294. }
  295. minCapP = LIGHT_CLAMP(((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100), 0, rawMax);
  296. minCapR = LIGHT_CLAMP( light_Configuration.specifiedValueRaw, 0, rawMax );
  297. /* Handle get operations */
  298. if(light_Configuration.operationMode == LIGHT_GET)
  299. {
  300. switch(light_Configuration.target){
  301. case LIGHT_BRIGHTNESS:
  302. (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
  303. break;
  304. case LIGHT_MAX_BRIGHTNESS:
  305. (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", rawMax) : printf("100.00\n"); /* <- I know how stupid it is but it might just make someones life easier */
  306. break;
  307. case LIGHT_MIN_CAP:
  308. (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", minCap) : printf("%.2f\n", percentMinCap);
  309. break;
  310. }
  311. return TRUE;
  312. }
  313. /* Handle set/add/sub operations */
  314. if(light_Configuration.operationMode == LIGHT_SET ||
  315. light_Configuration.operationMode == LIGHT_ADD ||
  316. light_Configuration.operationMode == LIGHT_SUB)
  317. {
  318. /* Set the pointer we will use for write to a fallback (that shouldn't change anything) */
  319. writeVal = &rawCurr;
  320. if(light_Configuration.target == LIGHT_MIN_CAP)
  321. {
  322. /* Handle minimum cap files */
  323. /* If we are not attempting to set, fail! */
  324. if(light_Configuration.operationMode != LIGHT_SET)
  325. {
  326. printf("Minimum cap can only be used with get/set operations.\n");
  327. return FALSE;
  328. }
  329. /* Point our writevalue and attempt to write*/
  330. writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &minCapR : &minCapP;
  331. if(!light_setMinCap(light_Configuration.specifiedController, *writeVal))
  332. {
  333. LIGHT_ERR("could not set minimum cap");
  334. return FALSE;
  335. }
  336. /* All good? Return true. */
  337. return TRUE;
  338. }else if(light_Configuration.target == LIGHT_BRIGHTNESS){
  339. /* Handle brightness writing */
  340. /* Point our writevalue according to configuration */
  341. switch(light_Configuration.operationMode)
  342. {
  343. case LIGHT_SET:
  344. writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawSetR : &rawSetP;
  345. break;
  346. case LIGHT_ADD:
  347. writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawAddR : &rawAddP;
  348. break;
  349. case LIGHT_SUB:
  350. writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawSubR : &rawSubP;
  351. break;
  352. case LIGHT_GET:
  353. case LIGHT_PRINT_HELP:
  354. case LIGHT_PRINT_VERSION:
  355. break;
  356. }
  357. /* Attempt to write */
  358. if(!light_setBrightness(light_Configuration.specifiedController, *writeVal))
  359. {
  360. LIGHT_ERR("could not set brightness");
  361. return FALSE;
  362. }
  363. /* All good? return true. */
  364. return TRUE;
  365. }else{
  366. /* If we didn't provide a valid target for write operations, fail. */
  367. printf("set/add/subtract operations are only available for brightness and minimum cap files.\n");
  368. return FALSE;
  369. }
  370. }
  371. printf("Controller: %s\nValueRaw: %lu\nValuePercent: %.2f\nOpMode: %u\nValMode: %u\nTarget: %u\n\n", light_Configuration.specifiedController, light_Configuration.specifiedValueRaw, light_Configuration.specifiedValuePercent, light_Configuration.operationMode, light_Configuration.valueMode, light_Configuration.target);
  372. printf("You did not specify a valid combination of commandline arguments. Have some help: \n");
  373. light_printHelp();
  374. return FALSE;
  375. }
  376. void light_free()
  377. {
  378. }
  379. LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET type, char **buffer)
  380. {
  381. char* returner = (char*)malloc(256);
  382. int spfVal = -1;
  383. if(returner == NULL)
  384. {
  385. LIGHT_MEMERR();
  386. buffer = NULL;
  387. return FALSE;
  388. }
  389. memset(returner, '\0', 256);
  390. switch(type)
  391. {
  392. case LIGHT_BRIGHTNESS:
  393. spfVal = sprintf(returner, "/sys/class/backlight/%s/brightness", controller);
  394. break;
  395. case LIGHT_MAX_BRIGHTNESS:
  396. spfVal = sprintf(returner, "/sys/class/backlight/%s/max_brightness", controller);
  397. break;
  398. case LIGHT_MIN_CAP:
  399. spfVal = sprintf(returner, "/etc/light/mincap/%s", controller);
  400. break;
  401. }
  402. if(spfVal < 0)
  403. {
  404. LIGHT_ERR("sprintf failed");
  405. free(returner);
  406. buffer = NULL;
  407. return FALSE;
  408. }
  409. *buffer = returner;
  410. return TRUE;
  411. }
  412. LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v)
  413. {
  414. char *brightnessPath = NULL;
  415. LIGHT_BOOL readVal = FALSE;
  416. if(!light_genPath(controller, LIGHT_BRIGHTNESS, &brightnessPath))
  417. {
  418. LIGHT_ERR("could not generate path to brightness file");
  419. return FALSE;
  420. }
  421. readVal = light_readULong( brightnessPath , v);
  422. free(brightnessPath);
  423. if(!readVal)
  424. {
  425. LIGHT_ERR("could not read value from brightness file");
  426. return FALSE;
  427. }
  428. return TRUE;
  429. }
  430. LIGHT_BOOL light_getMaxBrightness(char const *controller, unsigned long *v)
  431. {
  432. char *maxPath;
  433. LIGHT_BOOL readVal = FALSE;
  434. if(!light_genPath(controller, LIGHT_MAX_BRIGHTNESS, &maxPath))
  435. {
  436. LIGHT_ERR("could not generate path to maximum brightness file");
  437. return FALSE;
  438. }
  439. readVal = light_readULong(maxPath , v);
  440. free(maxPath);
  441. if(!readVal)
  442. {
  443. LIGHT_ERR("could not read value from max brightness file");
  444. return FALSE;
  445. }
  446. if(*v == 0)
  447. {
  448. LIGHT_ERR("max brightness is 0, so controller is not valid");
  449. return FALSE;
  450. }
  451. return TRUE;
  452. }
  453. LIGHT_BOOL light_setBrightness(char const *controller, unsigned long v)
  454. {
  455. char *brightnessPath = NULL;
  456. LIGHT_BOOL writeVal = FALSE;
  457. if(!light_genPath(controller, LIGHT_BRIGHTNESS, &brightnessPath))
  458. {
  459. LIGHT_ERR("could not generate path to brightness file");
  460. return FALSE;
  461. }
  462. writeVal = light_writeULong(brightnessPath, v);
  463. if(!writeVal)
  464. {
  465. LIGHT_ERR("could not write value to brightness file");
  466. }
  467. free(brightnessPath);
  468. return writeVal;
  469. }
  470. LIGHT_BOOL light_controllerAccessible(char const *controller)
  471. {
  472. char *brightnessPath = NULL;
  473. unsigned long dummy;
  474. if(!light_getBrightness(controller, &dummy))
  475. {
  476. LIGHT_WARN("could not read controllers brightness file, so controller is not accessible")
  477. return FALSE;
  478. }
  479. if(!light_getMaxBrightness(controller, &dummy))
  480. {
  481. LIGHT_WARN("could not read controller max brightness file, so controller is not accessible")
  482. return FALSE;
  483. }
  484. if(!light_genPath(controller, LIGHT_BRIGHTNESS, &brightnessPath))
  485. {
  486. LIGHT_ERR("could not generate path to brightness file");
  487. return FALSE;
  488. }
  489. if(!light_isWritable(brightnessPath))
  490. {
  491. LIGHT_WARN("could not open controller brightness file for writing, so controller is not accessible");
  492. free(brightnessPath);
  493. return FALSE;
  494. }
  495. free(brightnessPath);
  496. return TRUE;
  497. }
  498. LIGHT_BOOL light_iterateControllers()
  499. {
  500. LIGHT_BOOL dotsKilled = FALSE;
  501. if(light_iteratorDir == NULL)
  502. {
  503. light_iteratorDir = opendir("/sys/class/backlight");
  504. if(light_iteratorDir == NULL)
  505. {
  506. LIGHT_ERR("could not open /sys/class/backlight directory");
  507. return FALSE;
  508. }
  509. }
  510. while(!dotsKilled)
  511. {
  512. light_iterator = readdir(light_iteratorDir);
  513. if(light_iterator == NULL)
  514. {
  515. if(light_iteratorDir != NULL)
  516. {
  517. closedir(light_iteratorDir);
  518. light_iteratorDir = NULL;
  519. }
  520. return FALSE;
  521. }
  522. if(light_iterator->d_name[0] != '.')
  523. {
  524. dotsKilled = TRUE;
  525. }
  526. }
  527. strcpy(light_currentController, light_iterator->d_name);
  528. return TRUE;
  529. }
  530. LIGHT_BOOL light_getBestController(char *controller)
  531. {
  532. char bestYet[256];
  533. unsigned long bestValYet = 0;
  534. LIGHT_BOOL foundOkController = FALSE;
  535. memset(bestYet, '\0', 256);
  536. while(light_iterateControllers())
  537. {
  538. unsigned long currVal = 0;
  539. if(light_getMaxBrightness(light_currentController, &currVal))
  540. {
  541. if(light_controllerAccessible(light_currentController))
  542. {
  543. if(currVal > bestValYet)
  544. {
  545. foundOkController = TRUE;
  546. bestValYet = currVal;
  547. memset(bestYet, '\0', 256);
  548. strcpy(bestYet, light_currentController);
  549. }else{
  550. LIGHT_NOTE("ignoring controller as better one already found");
  551. }
  552. }else{
  553. LIGHT_WARN("controller not accessible");
  554. }
  555. }else{
  556. LIGHT_WARN("could not read max brightness from file");
  557. }
  558. }
  559. if(!foundOkController)
  560. {
  561. LIGHT_ERR("could not find an accessible controller");
  562. return FALSE;
  563. }
  564. if(bestValYet == 0)
  565. {
  566. LIGHT_ERR("found accessible controller but it's useless/corrupt");
  567. return FALSE;
  568. }
  569. memset(controller, '\0', 256);
  570. strcpy(controller, bestYet);
  571. return TRUE;
  572. }
  573. LIGHT_BOOL light_getMinCap(char const * controller, LIGHT_BOOL * hasMinCap, unsigned long * minCap)
  574. {
  575. char * mincapPath = NULL;
  576. if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
  577. {
  578. LIGHT_ERR("could not generate path to minimum cap file");
  579. return FALSE;
  580. }
  581. if(!light_isReadable(mincapPath)){
  582. *hasMinCap = FALSE;
  583. *minCap = 0;
  584. free(mincapPath);
  585. return TRUE;
  586. }
  587. if(!light_readULong(mincapPath, minCap))
  588. {
  589. LIGHT_ERR("could not read minimum cap from file");
  590. free(mincapPath);
  591. return FALSE;
  592. }
  593. *hasMinCap = TRUE;
  594. free(mincapPath);
  595. return TRUE;
  596. }
  597. LIGHT_BOOL light_setMinCap(char const * controller, unsigned long v)
  598. {
  599. char * mincapPath = NULL;
  600. if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
  601. {
  602. LIGHT_ERR("could not generate path to minimum cap file");
  603. return FALSE;
  604. }
  605. if(!light_writeULong(mincapPath, v))
  606. {
  607. LIGHT_ERR("could not write to minimum cap file");
  608. free(mincapPath);
  609. return FALSE;
  610. }
  611. free(mincapPath);
  612. return TRUE;
  613. }