123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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_Configuration.hasCachedMaxBrightness = FALSE;
  19. light_Configuration.cachedMaxBrightness = 0;
  20. light_verbosity = 0;
  21. }
  22. LIGHT_BOOL light_parseArguments(int argc, char** argv)
  23. {
  24. int currFlag;
  25. int verbosity;
  26. LIGHT_BOOL opSet = FALSE;
  27. LIGHT_BOOL targetSet = FALSE;
  28. LIGHT_BOOL ctrlSet = FALSE;
  29. LIGHT_BOOL valSet = FALSE;
  30. unsigned long specLen = 0;
  31. while((currFlag = getopt(argc, argv, "HhVGSAULIObmckas:prv:")) != -1)
  32. {
  33. switch(currFlag)
  34. {
  35. /* -- Operations -- */
  36. case 'H':
  37. case 'h':
  38. ASSERT_OPSET();
  39. light_Configuration.operationMode = LIGHT_PRINT_HELP;
  40. break;
  41. case 'V':
  42. ASSERT_OPSET();
  43. light_Configuration.operationMode = LIGHT_PRINT_VERSION;
  44. break;
  45. case 'G':
  46. ASSERT_OPSET();
  47. light_Configuration.operationMode = LIGHT_GET;
  48. break;
  49. case 'S':
  50. ASSERT_OPSET();
  51. light_Configuration.operationMode = LIGHT_SET;
  52. break;
  53. case 'A':
  54. ASSERT_OPSET();
  55. light_Configuration.operationMode = LIGHT_ADD;
  56. break;
  57. case 'U':
  58. ASSERT_OPSET();
  59. light_Configuration.operationMode = LIGHT_SUB;
  60. break;
  61. case 'L':
  62. ASSERT_OPSET();
  63. light_Configuration.operationMode = LIGHT_LIST_CTRL;
  64. break;
  65. case 'I':
  66. ASSERT_OPSET();
  67. light_Configuration.operationMode = LIGHT_RESTORE;
  68. break;
  69. case 'O':
  70. ASSERT_OPSET();
  71. light_Configuration.operationMode = LIGHT_SAVE;
  72. break;
  73. /* -- Targets -- */
  74. case 'b':
  75. ASSERT_TARGETSET();
  76. light_Configuration.target = LIGHT_BRIGHTNESS;
  77. break;
  78. case 'm':
  79. ASSERT_TARGETSET();
  80. light_Configuration.target = LIGHT_MAX_BRIGHTNESS;
  81. break;
  82. case 'c':
  83. ASSERT_TARGETSET();
  84. light_Configuration.target = LIGHT_MIN_CAP;
  85. break;
  86. case 'k':
  87. ASSERT_TARGETSET();
  88. light_Configuration.target = LIGHT_KEYBOARD;
  89. /* -- Controller selection -- */
  90. case 'a':
  91. ASSERT_CTRLSET();
  92. light_Configuration.controllerMode = LIGHT_AUTO;
  93. break;;
  94. case 's':
  95. ASSERT_CTRLSET();
  96. light_Configuration.controllerMode = LIGHT_SPECIFY;
  97. if(optarg == NULL)
  98. {
  99. printf("-s NEEDS an argument.\n\n");
  100. light_printHelp();
  101. }
  102. specLen = strlen(optarg);
  103. if(specLen > 255)
  104. {
  105. specLen = 255;
  106. }
  107. strncpy(light_Configuration.specifiedController, optarg, specLen);
  108. light_Configuration.specifiedController[255] = '\0';
  109. break;
  110. /* -- Value modes -- */
  111. case 'p':
  112. ASSERT_VALSET();
  113. light_Configuration.valueMode = LIGHT_PERCENT;
  114. break;
  115. case 'r':
  116. ASSERT_VALSET();
  117. light_Configuration.valueMode = LIGHT_RAW;
  118. break;
  119. /* -- Other -- */
  120. case 'v':
  121. if(optarg == NULL)
  122. {
  123. printf("-v NEEDS an argument.\n\n");
  124. light_printHelp();
  125. return FALSE;
  126. }
  127. if(sscanf(optarg, "%i", &verbosity) != 1)
  128. {
  129. printf("-v Verbosity is not specified in a recognizable format.\n\n");
  130. light_printHelp();
  131. return FALSE;
  132. }
  133. if(verbosity < 0 || verbosity > 3)
  134. {
  135. printf("-v Verbosity has to be between 0 and 3.\n\n");
  136. light_printHelp();
  137. return FALSE;
  138. }
  139. light_verbosity = (LIGHT_LOG_LEVEL)verbosity;
  140. break;
  141. }
  142. }
  143. /* If we need a <value> (for writing), make sure we have it! */
  144. if(light_Configuration.operationMode == LIGHT_SET ||
  145. light_Configuration.operationMode == LIGHT_ADD ||
  146. light_Configuration.operationMode == LIGHT_SUB)
  147. {
  148. if(argc - optind != 1)
  149. {
  150. printf("Light needs an argument for <value>.\n\n");
  151. light_printHelp();
  152. return FALSE;
  153. }
  154. if(light_Configuration.valueMode == LIGHT_PERCENT)
  155. {
  156. if(sscanf(argv[optind], "%lf", &light_Configuration.specifiedValuePercent) != 1){
  157. printf("<value> is not specified in a recognizable format.\n\n");
  158. light_printHelp();
  159. return FALSE;
  160. }
  161. light_Configuration.specifiedValuePercent = LIGHT_CLAMP(light_Configuration.specifiedValuePercent, 0.00, 100.00);
  162. }else{
  163. if(sscanf(argv[optind], "%lu", &light_Configuration.specifiedValueRaw) != 1){
  164. printf("<value> is not specified in a recognizable format.\n\n");
  165. light_printHelp();
  166. return FALSE;
  167. }
  168. }
  169. }
  170. return TRUE;
  171. }
  172. void light_printVersion(){
  173. printf("Light %u.%u (%s)\n", LIGHT_VER_MAJOR, LIGHT_VER_MINOR, LIGHT_VER_TYPE);
  174. printf("Copyright (C) %u %s\n", LIGHT_YEAR, LIGHT_AUTHOR);
  175. printf("This is free software, see the source for copying conditions. There is NO\n");
  176. printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\n\n");
  177. }
  178. void light_printHelp(){
  179. printf("Usage: light <options> <value>\n");
  180. printf("<value> has to be either integral(raw mode) or decimal(percent mode) depending on the specified value mode.\n");
  181. printf("<options> can be any of the following:\n\n");
  182. printf("Operations (can not be used in conjunction):\n");
  183. printf(" -H -h:\tPrints this help and exits\n");
  184. printf(" -V:\t\tPrints version info and exits\n");
  185. printf(" -G:\t\tGet value (default)\n");
  186. printf(" -S:\t\tSet value\n");
  187. printf(" -A:\t\tAdd value\n");
  188. printf(" -U:\t\tSubtract value\n");
  189. printf(" -L:\t\tList controllers\n");
  190. printf(" -I:\t\tRestore brightness\n");
  191. printf(" -O:\t\tSave brightness\n\n");
  192. printf("Targets (can not be used in conjunction):\n");
  193. printf(" -b:\t\tBrightness (default)\n \t\tUsed with [GSAU]\n\n");
  194. printf(" -m:\t\tMaximum brightness\n \t\tUsed with [G]\n\n");
  195. printf(" -c:\t\tMinimum cap\n \t\tUsed with [GS]\n");
  196. printf(" -k:\t\tSet keyboard brightness instead of display brightness \n \t\tUsed with [GSAU]");
  197. printf(" \t\tG returns null if no minimum cap is set.\n\n");
  198. printf("Controller selection (can not be used in conjunction):\n");
  199. printf(" -a:\t\tSelects controller automatically (default).\n");
  200. printf(" -s:\t\tSpecify controller to use. (needs argument)\n\n");
  201. printf("Value modes (can not be used in conjunction):\n");
  202. printf(" -p:\t\tInterpret <value> as, and output values in, percent. (default)\n");
  203. printf(" -r:\t\tInterpret <value> as, and output values in, raw mode.\n\n");
  204. printf("Other:\n");
  205. 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");
  206. }
  207. LIGHT_BOOL light_initialize(int argc, char** argv)
  208. {
  209. int mkdirVal;
  210. LIGHT_OP_MODE mode;
  211. light_defaultConfig();
  212. if(!light_parseArguments(argc, argv))
  213. {
  214. LIGHT_ERR("could not parse arguments");
  215. return FALSE;
  216. }
  217. mode = light_Configuration.operationMode;
  218. /* Just return true for operation modes that do not need initialization */
  219. if(mode == LIGHT_PRINT_HELP ||
  220. mode == LIGHT_PRINT_VERSION ||
  221. mode == LIGHT_LIST_CTRL)
  222. {
  223. return TRUE;
  224. }
  225. if(mode == LIGHT_SAVE ||
  226. (mode == LIGHT_SET && light_Configuration.target == LIGHT_MIN_CAP))
  227. {
  228. /* Make sure we have a valid /etc/light directory, as well as mincap and save */
  229. char const * const dirs[3] = {"/etc/light", "/etc/light/mincap", "/etc/light/save"};
  230. char const * const *dir = dirs;
  231. char const * const direrr = "'%s' does not exist and could not be created, make sure this application is run as root.";
  232. while (dir < dirs + 3)
  233. {
  234. mkdirVal = mkdir(*dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  235. if(mkdirVal != 0 && errno != EEXIST)
  236. {
  237. LIGHT_ERR_FMT(direrr, *dir);
  238. return FALSE;
  239. }
  240. ++dir;
  241. }
  242. }
  243. /* Make sure we have a valid controller before we proceed */
  244. if(light_Configuration.controllerMode == LIGHT_AUTO)
  245. {
  246. LIGHT_NOTE("Automatic mode -- finding best controller");
  247. if(!light_getBestController(light_Configuration.specifiedController))
  248. {
  249. LIGHT_ERR("could not find suitable controller");
  250. return FALSE;
  251. }
  252. }
  253. else if(!light_controllerAccessible(light_Configuration.specifiedController))
  254. {
  255. LIGHT_ERR_FMT("selected controller '%s' is not valid",
  256. light_Configuration.specifiedController);
  257. return FALSE;
  258. }
  259. return TRUE;
  260. }
  261. /* Print help and version info */
  262. LIGHT_BOOL light_handleInfo()
  263. {
  264. if(light_Configuration.operationMode == LIGHT_PRINT_HELP)
  265. {
  266. light_printHelp();
  267. return TRUE;
  268. }
  269. if(light_Configuration.operationMode == LIGHT_PRINT_VERSION)
  270. {
  271. light_printVersion();
  272. return TRUE;
  273. }
  274. if(light_Configuration.operationMode == LIGHT_LIST_CTRL)
  275. {
  276. /* listControllers() can return FALSE, but only if it does not find any controllers. That is not enough for an unsuccessfull run. */
  277. light_listControllers();
  278. return TRUE;
  279. }
  280. return FALSE;
  281. }
  282. LIGHT_BOOL light_initExecution(unsigned long *rawCurr, unsigned long *rawMax, LIGHT_BOOL *hasMinCap, unsigned long *minCap)
  283. {
  284. if(light_Configuration.hasCachedMaxBrightness)
  285. {
  286. *rawMax = light_Configuration.cachedMaxBrightness;
  287. }
  288. else if(!light_getMaxBrightness(light_Configuration.specifiedController, rawMax))
  289. {
  290. LIGHT_ERR("could not get max brightness");
  291. return FALSE;
  292. }
  293. /* No need to go further if targetting mincap */
  294. if(light_Configuration.target == LIGHT_MIN_CAP)
  295. {
  296. return TRUE;
  297. }
  298. if(!light_getBrightness(light_Configuration.specifiedController, rawCurr))
  299. {
  300. LIGHT_ERR("could not get brightness");
  301. return FALSE;
  302. }
  303. if(!light_getMinCap(light_Configuration.specifiedController, hasMinCap, minCap))
  304. {
  305. LIGHT_ERR("could not get min brightness");
  306. return FALSE;
  307. }
  308. if( *hasMinCap && *minCap > *rawMax )
  309. {
  310. LIGHT_WARN_FMT("invalid minimum cap (raw) value of '%lu' for controller, ignoring and using 0", *minCap);
  311. LIGHT_WARN_FMT("minimum cap must be inferior to '%lu'", *rawMax);
  312. minCap = 0;
  313. }
  314. return TRUE;
  315. }
  316. LIGHT_BOOL light_execute()
  317. {
  318. unsigned long rawCurr; /* The current brightness, in raw units */
  319. double percentCurr; /* The current brightness, in percent */
  320. unsigned long rawMax; /* The max brightness, in percent */
  321. unsigned long minCap; /* The minimum cap, in raw units */
  322. double percentMinCap; /* The minimum cap, in percent */
  323. LIGHT_BOOL hasMinCap; /* If we have a minimum cap */
  324. unsigned long writeVal;
  325. LIGHT_VAL_MODE valueMode;
  326. if(light_handleInfo())
  327. {
  328. return TRUE;
  329. }
  330. /* -- First, get the current, min and max values directly from controller/configuration (raw values) */
  331. if(!light_initExecution(&rawCurr, &rawMax, &hasMinCap, &minCap))
  332. {
  333. return FALSE;
  334. }
  335. /* -- Secondly, calculate the rest of the values (Clamp them here as well!) */
  336. valueMode = light_Configuration.valueMode;
  337. percentCurr = LIGHT_CLAMP( ((double)rawCurr) / ((double)rawMax) * 100 , 0.00, 100.00 );
  338. percentMinCap = LIGHT_CLAMP( ((double)minCap) / ((double)rawMax) * 100 , 0.00, 100.00 );
  339. LIGHT_NOTE_FMT("executing light on '%s' controller", light_Configuration.specifiedController);
  340. /* Handle get operations */
  341. if(light_Configuration.operationMode == LIGHT_GET)
  342. {
  343. switch(light_Configuration.target){
  344. case LIGHT_BRIGHTNESS:
  345. (valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
  346. break;
  347. case LIGHT_MAX_BRIGHTNESS:
  348. (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 */
  349. break;
  350. case LIGHT_MIN_CAP:
  351. (valueMode == LIGHT_RAW) ? printf("%lu\n", minCap) : printf("%.2f\n", percentMinCap);
  352. break;
  353. case LIGHT_SAVERESTORE:
  354. break;
  355. }
  356. return TRUE;
  357. }
  358. /* Handle saves and restores*/
  359. if(light_Configuration.operationMode == LIGHT_SAVE){
  360. if(!light_saveBrightness(light_Configuration.specifiedController, rawCurr))
  361. {
  362. LIGHT_ERR("could not save brightness");
  363. return FALSE;
  364. }
  365. return TRUE;
  366. }
  367. if(light_Configuration.operationMode == LIGHT_RESTORE){
  368. if(!light_restoreBrightness(light_Configuration.specifiedController)){
  369. LIGHT_ERR("could not restore brightness");
  370. return FALSE;
  371. }
  372. return TRUE;
  373. }
  374. /* Handle set/add/sub operations */
  375. if(light_Configuration.operationMode == LIGHT_SET ||
  376. light_Configuration.operationMode == LIGHT_ADD ||
  377. light_Configuration.operationMode == LIGHT_SUB)
  378. {
  379. if(light_Configuration.target == LIGHT_MIN_CAP)
  380. {
  381. /* Handle minimum cap files */
  382. writeVal = valueMode == LIGHT_RAW ?
  383. LIGHT_CLAMP( light_Configuration.specifiedValueRaw, 0, rawMax ) :
  384. LIGHT_CLAMP(((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100), 0, rawMax);
  385. /* If we are not attempting to set, fail! */
  386. if(light_Configuration.operationMode != LIGHT_SET)
  387. {
  388. fprintf(stderr, "Minimum cap can only be used with get/set operations.\n");
  389. return FALSE;
  390. }
  391. if(!light_setMinCap(light_Configuration.specifiedController, writeVal))
  392. {
  393. LIGHT_ERR("could not set minimum cap");
  394. return FALSE;
  395. }
  396. /* All good? Return true. */
  397. return TRUE;
  398. }else if(light_Configuration.target == LIGHT_BRIGHTNESS || light_Configuration.target == LIGHT_KEYBOARD){
  399. /* Handle brightness writing */
  400. switch(light_Configuration.operationMode)
  401. {
  402. case LIGHT_SET:
  403. writeVal = valueMode == LIGHT_RAW ?
  404. LIGHT_CLAMP( light_Configuration.specifiedValueRaw , minCap, rawMax ) :
  405. LIGHT_CLAMP( ((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100) , minCap, rawMax );
  406. break;
  407. case LIGHT_ADD:
  408. writeVal = valueMode == LIGHT_RAW ?
  409. LIGHT_CLAMP( rawCurr + light_Configuration.specifiedValueRaw , minCap, rawMax ) :
  410. LIGHT_CLAMP( ((unsigned long) ( (percentCurr + light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
  411. break;
  412. case LIGHT_SUB:
  413. if(light_Configuration.specifiedValueRaw > rawCurr){
  414. writeVal = LIGHT_CLAMP(0, minCap, rawMax);
  415. }else{
  416. writeVal = valueMode == LIGHT_RAW ?
  417. LIGHT_CLAMP( rawCurr - light_Configuration.specifiedValueRaw , minCap, rawMax ):
  418. LIGHT_CLAMP( ((unsigned long) ( (percentCurr - light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
  419. }
  420. break;
  421. /* we have already taken other possibilities, so we shouldn't get here */
  422. default:
  423. return FALSE;
  424. }
  425. /* Attempt to write */
  426. if(!light_setBrightness(light_Configuration.specifiedController, writeVal))
  427. {
  428. LIGHT_ERR("could not set brightness");
  429. return FALSE;
  430. }
  431. /* All good? return true. */
  432. return TRUE;
  433. }else{
  434. /* If we didn't provide a valid target for write operations, fail. */
  435. fprintf(stderr, "set/add/subtract operations are only available for brightness and minimum cap files.\n");
  436. return FALSE;
  437. }
  438. }
  439. /* Handle saves and restores*/
  440. if(light_Configuration.operationMode == LIGHT_SAVE){
  441. if(!light_saveBrightness(light_Configuration.specifiedController, rawCurr))
  442. {
  443. LIGHT_ERR("could not save brightness");
  444. return FALSE;
  445. }
  446. return TRUE;
  447. }
  448. if(light_Configuration.operationMode == LIGHT_RESTORE){
  449. if(!light_restoreBrightness(light_Configuration.specifiedController)){
  450. LIGHT_ERR("could not restore brightness");
  451. return FALSE;
  452. }
  453. return TRUE;
  454. }
  455. fprintf(stderr, "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);
  456. fprintf(stderr, "You did not specify a valid combination of commandline arguments. Have some help: \n");
  457. light_printHelp();
  458. return FALSE;
  459. }
  460. void light_free()
  461. {
  462. }
  463. LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET type, char **buffer)
  464. {
  465. char* returner = malloc(256);
  466. int spfVal = -1;
  467. if(returner == NULL)
  468. {
  469. LIGHT_MEMERR();
  470. buffer = NULL;
  471. return FALSE;
  472. }
  473. memset(returner, '\0', 256);
  474. switch(type)
  475. {
  476. case LIGHT_BRIGHTNESS:
  477. spfVal = sprintf(returner, "/sys/class/backlight/%s/brightness", controller);
  478. break;
  479. case LIGHT_MAX_BRIGHTNESS:
  480. spfVal = sprintf(returner, "/sys/class/backlight/%s/max_brightness", controller);
  481. break;
  482. case LIGHT_MIN_CAP:
  483. spfVal = sprintf(returner, "/etc/light/mincap/%s", controller);
  484. break;
  485. case LIGHT_SAVERESTORE:
  486. spfVal = sprintf(returner, "/etc/light/save/%s", controller);
  487. break;
  488. case LIGHT_KEYBOARD:
  489. spfVal = sprintf(returner, "/sys/class/leds/%s/brightness", controller);
  490. break;
  491. case LIGHT_KEYBOARD_MAX_BRIGHTNESS:
  492. spfVal = sprintf(returner, "/sys/class/leds/%s/max_brightness", controller);
  493. break;
  494. }
  495. if(spfVal < 0)
  496. {
  497. LIGHT_ERR("sprintf failed");
  498. free(returner);
  499. buffer = NULL;
  500. return FALSE;
  501. }
  502. *buffer = returner;
  503. return TRUE;
  504. }
  505. LIGHT_BOOL light_getBrightnessPath(char const *controller, char **path)
  506. {
  507. LIGHT_TARGET target;
  508. if(light_Configuration.target == LIGHT_MIN_CAP)
  509. {
  510. target = LIGHT_BRIGHTNESS;
  511. }
  512. else
  513. {
  514. target = light_Configuration.target;
  515. }
  516. if(!light_genPath(controller, target, path))
  517. {
  518. LIGHT_ERR("could not generate path to brightness file");
  519. return FALSE;
  520. }
  521. return TRUE;
  522. }
  523. LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v)
  524. {
  525. char *brightnessPath = NULL;
  526. LIGHT_BOOL readVal = FALSE;
  527. if(!light_getBrightnessPath(controller, &brightnessPath))
  528. {
  529. return FALSE;
  530. }
  531. readVal = light_readULong( brightnessPath , v);
  532. free(brightnessPath);
  533. if(!readVal)
  534. {
  535. LIGHT_ERR("could not read value from brightness file");
  536. return FALSE;
  537. }
  538. return TRUE;
  539. }
  540. LIGHT_BOOL light_getMaxBrightnessPath(char const *controller, char **path)
  541. {
  542. LIGHT_TARGET target;
  543. if(light_Configuration.target == LIGHT_KEYBOARD)
  544. {
  545. target = LIGHT_KEYBOARD_MAX_BRIGHTNESS;
  546. }
  547. else
  548. {
  549. target = LIGHT_MAX_BRIGHTNESS;
  550. }
  551. if(!light_genPath(controller, target, path))
  552. {
  553. LIGHT_ERR("could not generate path to maximum brightness file");
  554. return FALSE;
  555. }
  556. return TRUE;
  557. }
  558. LIGHT_BOOL light_getMaxBrightness(char const *controller, unsigned long *v)
  559. {
  560. char *maxPath = NULL;
  561. LIGHT_BOOL readVal = FALSE;
  562. if (!light_getMaxBrightnessPath(controller, &maxPath))
  563. {
  564. return FALSE;
  565. }
  566. readVal = light_readULong(maxPath , v);
  567. free(maxPath);
  568. if(!readVal)
  569. {
  570. LIGHT_ERR("could not read value from max brightness file");
  571. return FALSE;
  572. }
  573. if(*v == 0)
  574. {
  575. LIGHT_ERR("max brightness is 0, so controller is not valid");
  576. return FALSE;
  577. }
  578. return TRUE;
  579. }
  580. LIGHT_BOOL light_setBrightness(char const *controller, unsigned long v)
  581. {
  582. char *brightnessPath = NULL;
  583. LIGHT_BOOL writeVal = FALSE;
  584. if(!light_genPath(controller, light_Configuration.target, &brightnessPath))
  585. {
  586. LIGHT_ERR("could not generate path to brightness file");
  587. return FALSE;
  588. }
  589. writeVal = light_writeULong(brightnessPath, v);
  590. if(!writeVal)
  591. {
  592. LIGHT_ERR("could not write value to brightness file");
  593. }
  594. free(brightnessPath);
  595. return writeVal;
  596. }
  597. LIGHT_BOOL light_controllerAccessible(char const *controller)
  598. {
  599. char *brightnessPath = NULL;
  600. /* On auto mode, we need to check if we can read the max brightness value
  601. of the controller for later computation */
  602. if(light_Configuration.controllerMode == LIGHT_AUTO ||
  603. light_Configuration.target == LIGHT_MAX_BRIGHTNESS)
  604. {
  605. if(!light_getMaxBrightnessPath(controller, &brightnessPath))
  606. {
  607. return FALSE;
  608. }
  609. if(!light_isReadable(brightnessPath))
  610. {
  611. LIGHT_WARN("could not open controller max brightness file for reading, so controller is not accessible");
  612. free(brightnessPath);
  613. return FALSE;
  614. }
  615. free(brightnessPath);
  616. }
  617. if(!light_getBrightnessPath(controller, &brightnessPath))
  618. {
  619. return FALSE;
  620. }
  621. if(light_Configuration.operationMode != LIGHT_GET &&
  622. light_Configuration.target != LIGHT_MIN_CAP &&
  623. !light_isWritable(brightnessPath))
  624. {
  625. LIGHT_WARN("could not open controller brightness file for writing, so controller is not accessible");
  626. free(brightnessPath);
  627. return FALSE;
  628. }
  629. else if (!light_isReadable(brightnessPath))
  630. {
  631. LIGHT_WARN("could not open controller brightness file for reading, so controller is not accessible");
  632. free(brightnessPath);
  633. return FALSE;
  634. }
  635. free(brightnessPath);
  636. return TRUE;
  637. }
  638. LIGHT_BOOL light_iterateControllers()
  639. {
  640. LIGHT_BOOL dotsKilled = FALSE;
  641. if(light_iteratorDir == NULL)
  642. {
  643. if(light_Configuration.target == LIGHT_KEYBOARD)
  644. {
  645. light_iteratorDir = opendir("/sys/class/leds");
  646. }
  647. else
  648. {
  649. light_iteratorDir = opendir("/sys/class/backlight");
  650. }
  651. if(light_iteratorDir == NULL)
  652. {
  653. LIGHT_ERR("could not open backlight or leds directory in /sys/class");
  654. return FALSE;
  655. }
  656. }
  657. while(!dotsKilled)
  658. {
  659. light_iterator = readdir(light_iteratorDir);
  660. if(light_iterator == NULL)
  661. {
  662. if(light_iteratorDir != NULL)
  663. {
  664. closedir(light_iteratorDir);
  665. light_iteratorDir = NULL;
  666. }
  667. return FALSE;
  668. }
  669. if(light_iterator->d_name[0] != '.')
  670. {
  671. dotsKilled = TRUE;
  672. }
  673. }
  674. strcpy(light_currentController, light_iterator->d_name);
  675. return TRUE;
  676. }
  677. LIGHT_BOOL light_getBestController(char *controller)
  678. {
  679. char bestYet[256];
  680. unsigned long bestValYet = 0;
  681. LIGHT_BOOL foundOkController = FALSE;
  682. memset(bestYet, '\0', 256);
  683. while(light_iterateControllers())
  684. {
  685. unsigned long currVal = 0;
  686. LIGHT_NOTE_FMT("found '%s' controller", light_currentController);
  687. if(light_controllerAccessible(light_currentController))
  688. {
  689. if(light_getMaxBrightness(light_currentController, &currVal))
  690. {
  691. if(currVal > bestValYet)
  692. {
  693. foundOkController = TRUE;
  694. bestValYet = currVal;
  695. memset(bestYet, '\0', 256);
  696. strcpy(bestYet, light_currentController);
  697. light_Configuration.hasCachedMaxBrightness = TRUE;
  698. light_Configuration.cachedMaxBrightness = currVal;
  699. }else{
  700. LIGHT_NOTE("ignoring controller as better one already found");
  701. }
  702. }else{
  703. LIGHT_WARN("could not read max brightness from file");
  704. }
  705. }else{
  706. LIGHT_WARN("controller not accessible");
  707. }
  708. }
  709. if(!foundOkController)
  710. {
  711. LIGHT_ERR("could not find an accessible controller");
  712. return FALSE;
  713. }
  714. if(bestValYet == 0)
  715. {
  716. LIGHT_ERR("found accessible controller but it's useless/corrupt");
  717. return FALSE;
  718. }
  719. memset(controller, '\0', 256);
  720. strcpy(controller, bestYet);
  721. return TRUE;
  722. }
  723. LIGHT_BOOL light_getMinCap(char const * controller, LIGHT_BOOL * hasMinCap, unsigned long * minCap)
  724. {
  725. char * mincapPath = NULL;
  726. if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
  727. {
  728. LIGHT_ERR("could not generate path to minimum cap file");
  729. return FALSE;
  730. }
  731. if(!light_isReadable(mincapPath)){
  732. *hasMinCap = FALSE;
  733. *minCap = 0;
  734. free(mincapPath);
  735. return TRUE;
  736. }
  737. if(!light_readULong(mincapPath, minCap))
  738. {
  739. LIGHT_ERR("could not read minimum cap from file");
  740. free(mincapPath);
  741. return FALSE;
  742. }
  743. *hasMinCap = TRUE;
  744. free(mincapPath);
  745. return TRUE;
  746. }
  747. LIGHT_BOOL light_setMinCap(char const * controller, unsigned long v)
  748. {
  749. char * mincapPath = NULL;
  750. if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
  751. {
  752. LIGHT_ERR("could not generate path to minimum cap file");
  753. return FALSE;
  754. }
  755. if(!light_writeULong(mincapPath, v))
  756. {
  757. LIGHT_ERR("could not write to minimum cap file");
  758. free(mincapPath);
  759. return FALSE;
  760. }
  761. free(mincapPath);
  762. return TRUE;
  763. }
  764. LIGHT_BOOL light_listControllers()
  765. {
  766. LIGHT_BOOL foundController = FALSE;
  767. while(light_iterateControllers())
  768. {
  769. if(!foundController)
  770. {
  771. foundController = TRUE;
  772. }
  773. printf("%s\n", light_currentController);
  774. }
  775. if(!foundController)
  776. {
  777. LIGHT_WARN("no controllers found, either check your system or your permissions");
  778. return FALSE;
  779. }
  780. return TRUE;
  781. }
  782. LIGHT_BOOL light_saveBrightness(char const *controller, unsigned long v){
  783. char *savePath = NULL;
  784. if(!light_genPath(controller, LIGHT_SAVERESTORE, &savePath))
  785. {
  786. LIGHT_ERR("could not generate path to save/restore file");
  787. return FALSE;
  788. }
  789. if(!light_writeULong(savePath, v))
  790. {
  791. LIGHT_ERR("could not write to save/restore file");
  792. free(savePath);
  793. return FALSE;
  794. }
  795. free(savePath);
  796. return TRUE;
  797. }
  798. LIGHT_BOOL light_restoreBrightness(char const *controller){
  799. char *restorePath = NULL;
  800. unsigned long v = 0;
  801. if(!light_genPath(controller, LIGHT_SAVERESTORE, &restorePath))
  802. {
  803. LIGHT_ERR("could not generate path to save/restore file");
  804. return FALSE;
  805. }
  806. if(!light_readULong(restorePath, &v))
  807. {
  808. LIGHT_ERR("could not read saved value");
  809. free(restorePath);
  810. return FALSE;
  811. }
  812. if(!light_setBrightness(controller, v))
  813. {
  814. LIGHT_ERR("could not set restored brightness");
  815. free(restorePath);
  816. return FALSE;
  817. }
  818. free(restorePath);
  819. return TRUE;
  820. }