light.c 22KB

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