light.c 28KB

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