light.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. LIGHT_BOOL light_execute()
  262. {
  263. unsigned long rawCurr; /* The current brightness, in raw mode */
  264. double percentCurr; /* The current brightness, in percent */
  265. unsigned long rawMax; /* The max brightness, in percent */
  266. unsigned long rawSetP; /* The final value to be set, in raw mode, when setting with percent */
  267. unsigned long rawAddP; /* The final value to be set, in raw mode, when adding with percent */
  268. unsigned long rawSubP; /* The final value to be set, in raw mode, when subtracting with percent */
  269. unsigned long rawSetR; /* The final value to be set, in raw mode, when setting with raw values */
  270. unsigned long rawAddR; /* The final value to be set, in raw mode, when adding with raw values */
  271. unsigned long rawSubR; /* The final value to be set, in raw mode, when subtracting with raw values */
  272. unsigned long minCap; /* The minimum cap, in raw mode */
  273. double percentMinCap; /* The minimum cap, in percent */
  274. LIGHT_BOOL hasMinCap; /* If we have a minimum cap */
  275. 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) */
  276. 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) */
  277. unsigned long *writeVal; /* The actual value used for writing */
  278. /* Print help and version info */
  279. if(light_Configuration.operationMode == LIGHT_PRINT_HELP)
  280. {
  281. light_printHelp();
  282. return TRUE;
  283. }
  284. if(light_Configuration.operationMode == LIGHT_PRINT_VERSION)
  285. {
  286. light_printVersion();
  287. return TRUE;
  288. }
  289. if(light_Configuration.operationMode == LIGHT_LIST_CTRL)
  290. {
  291. /* listControllers() can return FALSE, but only if it does not find any controllers. That is not enough for an unsuccessfull run. */
  292. light_listControllers();
  293. return TRUE;
  294. }
  295. /* Prepare variables */
  296. LIGHT_NOTE_FMT("Executing light on '%s' controller", light_Configuration.specifiedController);
  297. /* -- First, get the current, min and max values directly from controller/configuration (raw values) */
  298. if(!light_getBrightness(light_Configuration.specifiedController, &rawCurr))
  299. {
  300. LIGHT_ERR("could not get brightness");
  301. return FALSE;
  302. }
  303. if(light_Configuration.hasCachedMaxBrightness)
  304. {
  305. rawMax = light_Configuration.cachedMaxBrightness;
  306. }
  307. else if(!light_getMaxBrightness(light_Configuration.specifiedController, &rawMax))
  308. {
  309. LIGHT_ERR("could not get max brightness");
  310. return FALSE;
  311. }
  312. if(!light_getMinCap(light_Configuration.specifiedController, &hasMinCap, &minCap))
  313. {
  314. LIGHT_ERR("could not get min brightness");
  315. return FALSE;
  316. }
  317. if( hasMinCap && minCap > rawMax )
  318. {
  319. LIGHT_WARN_FMT("invalid minimum cap (raw) value of '%lu' for controller, ignoring and using 0", minCap);
  320. LIGHT_WARN_FMT("minimum cap must be inferior to '%lu'", rawMax);
  321. minCap = 0;
  322. }
  323. /* -- Secondly, calculate the rest of the values (Clamp them here as well!) */
  324. percentCurr = LIGHT_CLAMP( ((double)rawCurr) / ((double)rawMax) * 100 , 0.00, 100.00 );
  325. percentMinCap = LIGHT_CLAMP( ((double)minCap) / ((double)rawMax) * 100 , 0.00, 100.00 );
  326. rawSetP = LIGHT_CLAMP( ((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100) , minCap, rawMax );
  327. rawAddP = LIGHT_CLAMP( ((unsigned long) ( (percentCurr + light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
  328. if( light_Configuration.specifiedValuePercent > percentCurr)
  329. {
  330. rawSubP = LIGHT_CLAMP(0, minCap, rawMax);
  331. }else{
  332. rawSubP = LIGHT_CLAMP( ((unsigned long) ( (percentCurr - light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
  333. }
  334. rawSetR = LIGHT_CLAMP( light_Configuration.specifiedValueRaw , minCap, rawMax );
  335. rawAddR = LIGHT_CLAMP( rawCurr + light_Configuration.specifiedValueRaw , minCap, rawMax );
  336. if(light_Configuration.specifiedValueRaw > rawCurr){
  337. rawSubR = LIGHT_CLAMP(0, minCap, rawMax)
  338. }else{
  339. rawSubR = LIGHT_CLAMP( rawCurr - light_Configuration.specifiedValueRaw , minCap, rawMax );
  340. }
  341. minCapP = LIGHT_CLAMP(((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100), 0, rawMax);
  342. minCapR = LIGHT_CLAMP( light_Configuration.specifiedValueRaw, 0, rawMax );
  343. /* Handle get operations */
  344. if(light_Configuration.operationMode == LIGHT_GET)
  345. {
  346. switch(light_Configuration.target){
  347. case LIGHT_BRIGHTNESS:
  348. (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
  349. break;
  350. case LIGHT_MAX_BRIGHTNESS:
  351. (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 */
  352. break;
  353. case LIGHT_MIN_CAP:
  354. (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", minCap) : printf("%.2f\n", percentMinCap);
  355. break;
  356. case LIGHT_SAVERESTORE:
  357. break;
  358. case LIGHT_KEYBOARD:
  359. (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
  360. break;
  361. case LIGHT_KEYBOARD_MAX_BRIGHTNESS:
  362. (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 */
  363. break;
  364. }
  365. return TRUE;
  366. }
  367. /* Handle set/add/sub operations */
  368. if(light_Configuration.operationMode == LIGHT_SET ||
  369. light_Configuration.operationMode == LIGHT_ADD ||
  370. light_Configuration.operationMode == LIGHT_SUB)
  371. {
  372. /* Set the pointer we will use for write to a fallback (that shouldn't change anything) */
  373. writeVal = &rawCurr;
  374. if(light_Configuration.target == LIGHT_MIN_CAP)
  375. {
  376. /* Handle minimum cap files */
  377. /* If we are not attempting to set, fail! */
  378. if(light_Configuration.operationMode != LIGHT_SET)
  379. {
  380. fprintf(stderr, "Minimum cap can only be used with get/set operations.\n");
  381. return FALSE;
  382. }
  383. /* Point our writevalue and attempt to write*/
  384. writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &minCapR : &minCapP;
  385. if(!light_setMinCap(light_Configuration.specifiedController, *writeVal))
  386. {
  387. LIGHT_ERR("could not set minimum cap");
  388. return FALSE;
  389. }
  390. /* All good? Return true. */
  391. return TRUE;
  392. }else if(light_Configuration.target == LIGHT_BRIGHTNESS || light_Configuration.target == LIGHT_KEYBOARD){
  393. /* Handle brightness writing */
  394. /* Point our writevalue according to configuration */
  395. switch(light_Configuration.operationMode)
  396. {
  397. case LIGHT_SET:
  398. writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawSetR : &rawSetP;
  399. break;
  400. case LIGHT_ADD:
  401. if(rawCurr == rawAddP && rawAddP != rawMax)
  402. {
  403. rawAddP+=1;
  404. }
  405. writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawAddR : &rawAddP;
  406. break;
  407. case LIGHT_SUB:
  408. writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawSubR : &rawSubP;
  409. break;
  410. case LIGHT_GET:
  411. case LIGHT_PRINT_HELP:
  412. case LIGHT_PRINT_VERSION:
  413. case LIGHT_LIST_CTRL:
  414. case LIGHT_SAVE:
  415. case LIGHT_RESTORE:
  416. break;
  417. }
  418. /* Attempt to write */
  419. if(!light_setBrightness(light_Configuration.specifiedController, *writeVal))
  420. {
  421. LIGHT_ERR("could not set brightness");
  422. return FALSE;
  423. }
  424. /* All good? return true. */
  425. return TRUE;
  426. }else{
  427. /* If we didn't provide a valid target for write operations, fail. */
  428. fprintf(stderr, "set/add/subtract operations are only available for brightness and minimum cap files.\n");
  429. return FALSE;
  430. }
  431. }
  432. /* Handle saves and restores*/
  433. if(light_Configuration.operationMode == LIGHT_SAVE){
  434. if(!light_saveBrightness(light_Configuration.specifiedController, rawCurr))
  435. {
  436. LIGHT_ERR("could not save brightness");
  437. return FALSE;
  438. }
  439. return TRUE;
  440. }
  441. if(light_Configuration.operationMode == LIGHT_RESTORE){
  442. if(!light_restoreBrightness(light_Configuration.specifiedController)){
  443. LIGHT_ERR("could not restore brightness");
  444. return FALSE;
  445. }
  446. return TRUE;
  447. }
  448. 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);
  449. fprintf(stderr, "You did not specify a valid combination of commandline arguments. Have some help: \n");
  450. light_printHelp();
  451. return FALSE;
  452. }
  453. void light_free()
  454. {
  455. }
  456. LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET type, char **buffer)
  457. {
  458. char* returner = malloc(256);
  459. int spfVal = -1;
  460. if(returner == NULL)
  461. {
  462. LIGHT_MEMERR();
  463. buffer = NULL;
  464. return FALSE;
  465. }
  466. memset(returner, '\0', 256);
  467. switch(type)
  468. {
  469. case LIGHT_BRIGHTNESS:
  470. spfVal = sprintf(returner, "/sys/class/backlight/%s/brightness", controller);
  471. break;
  472. case LIGHT_MAX_BRIGHTNESS:
  473. spfVal = sprintf(returner, "/sys/class/backlight/%s/max_brightness", controller);
  474. break;
  475. case LIGHT_MIN_CAP:
  476. spfVal = sprintf(returner, "/etc/light/mincap/%s", controller);
  477. break;
  478. case LIGHT_SAVERESTORE:
  479. spfVal = sprintf(returner, "/etc/light/save/%s", controller);
  480. break;
  481. case LIGHT_KEYBOARD:
  482. spfVal = sprintf(returner, "/sys/class/leds/%s/brightness", controller);
  483. break;
  484. case LIGHT_KEYBOARD_MAX_BRIGHTNESS:
  485. spfVal = sprintf(returner, "/sys/class/leds/%s/max_brightness", controller);
  486. break;
  487. }
  488. if(spfVal < 0)
  489. {
  490. LIGHT_ERR("sprintf failed");
  491. free(returner);
  492. buffer = NULL;
  493. return FALSE;
  494. }
  495. *buffer = returner;
  496. return TRUE;
  497. }
  498. LIGHT_BOOL light_getBrightnessPath(char const *controller, char **path)
  499. {
  500. LIGHT_TARGET target;
  501. if(light_Configuration.target == LIGHT_MIN_CAP)
  502. {
  503. target = LIGHT_BRIGHTNESS;
  504. }
  505. else
  506. {
  507. target = light_Configuration.target;
  508. }
  509. if(!light_genPath(controller, target, path))
  510. {
  511. LIGHT_ERR("could not generate path to brightness file");
  512. return FALSE;
  513. }
  514. return TRUE;
  515. }
  516. LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v)
  517. {
  518. char *brightnessPath = NULL;
  519. LIGHT_BOOL readVal = FALSE;
  520. if(!light_getBrightnessPath(controller, &brightnessPath))
  521. {
  522. return FALSE;
  523. }
  524. readVal = light_readULong( brightnessPath , v);
  525. free(brightnessPath);
  526. if(!readVal)
  527. {
  528. LIGHT_ERR("could not read value from brightness file");
  529. return FALSE;
  530. }
  531. return TRUE;
  532. }
  533. LIGHT_BOOL light_getMaxBrightnessPath(char const *controller, char **path)
  534. {
  535. LIGHT_TARGET target;
  536. if(light_Configuration.target == LIGHT_KEYBOARD)
  537. {
  538. target = LIGHT_KEYBOARD_MAX_BRIGHTNESS;
  539. }
  540. else
  541. {
  542. target = LIGHT_MAX_BRIGHTNESS;
  543. }
  544. if(!light_genPath(controller, target, path))
  545. {
  546. LIGHT_ERR("could not generate path to maximum brightness file");
  547. return FALSE;
  548. }
  549. return TRUE;
  550. }
  551. LIGHT_BOOL light_getMaxBrightness(char const *controller, unsigned long *v)
  552. {
  553. char *maxPath = NULL;
  554. LIGHT_BOOL readVal = FALSE;
  555. if (!light_getMaxBrightnessPath(controller, &maxPath))
  556. {
  557. return FALSE;
  558. }
  559. readVal = light_readULong(maxPath , v);
  560. free(maxPath);
  561. if(!readVal)
  562. {
  563. LIGHT_ERR("could not read value from max brightness file");
  564. return FALSE;
  565. }
  566. if(*v == 0)
  567. {
  568. LIGHT_ERR("max brightness is 0, so controller is not valid");
  569. return FALSE;
  570. }
  571. return TRUE;
  572. }
  573. LIGHT_BOOL light_setBrightness(char const *controller, unsigned long v)
  574. {
  575. char *brightnessPath = NULL;
  576. LIGHT_BOOL writeVal = FALSE;
  577. if(!light_genPath(controller, light_Configuration.target, &brightnessPath))
  578. {
  579. LIGHT_ERR("could not generate path to brightness file");
  580. return FALSE;
  581. }
  582. writeVal = light_writeULong(brightnessPath, v);
  583. if(!writeVal)
  584. {
  585. LIGHT_ERR("could not write value to brightness file");
  586. }
  587. free(brightnessPath);
  588. return writeVal;
  589. }
  590. LIGHT_BOOL light_controllerAccessible(char const *controller)
  591. {
  592. char *brightnessPath = NULL;
  593. /* On auto mode, we need to check if we can read the max brightness value
  594. of the controller for later computation */
  595. if(light_Configuration.controllerMode == LIGHT_AUTO ||
  596. light_Configuration.target == LIGHT_MAX_BRIGHTNESS)
  597. {
  598. if(!light_getMaxBrightnessPath(controller, &brightnessPath))
  599. {
  600. return FALSE;
  601. }
  602. if(!light_isReadable(brightnessPath))
  603. {
  604. LIGHT_WARN("could not open controller max brightness file for reading, so controller is not accessible");
  605. free(brightnessPath);
  606. return FALSE;
  607. }
  608. free(brightnessPath);
  609. }
  610. if(!light_getBrightnessPath(controller, &brightnessPath))
  611. {
  612. return FALSE;
  613. }
  614. if(light_Configuration.operationMode != LIGHT_GET &&
  615. light_Configuration.target != LIGHT_MIN_CAP &&
  616. !light_isWritable(brightnessPath))
  617. {
  618. LIGHT_WARN("could not open controller brightness file for writing, so controller is not accessible");
  619. free(brightnessPath);
  620. return FALSE;
  621. }
  622. else if (!light_isReadable(brightnessPath))
  623. {
  624. LIGHT_WARN("could not open controller brightness file for reading, so controller is not accessible");
  625. free(brightnessPath);
  626. return FALSE;
  627. }
  628. free(brightnessPath);
  629. return TRUE;
  630. }
  631. LIGHT_BOOL light_iterateControllers()
  632. {
  633. LIGHT_BOOL dotsKilled = FALSE;
  634. if(light_iteratorDir == NULL)
  635. {
  636. if(light_Configuration.target == LIGHT_KEYBOARD)
  637. {
  638. light_iteratorDir = opendir("/sys/class/leds");
  639. }
  640. else
  641. {
  642. light_iteratorDir = opendir("/sys/class/backlight");
  643. }
  644. if(light_iteratorDir == NULL)
  645. {
  646. LIGHT_ERR("could not open backlight or leds directory in /sys/class");
  647. return FALSE;
  648. }
  649. }
  650. while(!dotsKilled)
  651. {
  652. light_iterator = readdir(light_iteratorDir);
  653. if(light_iterator == NULL)
  654. {
  655. if(light_iteratorDir != NULL)
  656. {
  657. closedir(light_iteratorDir);
  658. light_iteratorDir = NULL;
  659. }
  660. return FALSE;
  661. }
  662. if(light_iterator->d_name[0] != '.')
  663. {
  664. dotsKilled = TRUE;
  665. }
  666. }
  667. strcpy(light_currentController, light_iterator->d_name);
  668. return TRUE;
  669. }
  670. LIGHT_BOOL light_getBestController(char *controller)
  671. {
  672. char bestYet[256];
  673. unsigned long bestValYet = 0;
  674. LIGHT_BOOL foundOkController = FALSE;
  675. memset(bestYet, '\0', 256);
  676. while(light_iterateControllers())
  677. {
  678. unsigned long currVal = 0;
  679. LIGHT_NOTE_FMT("found '%s' controller", light_currentController);
  680. if(light_controllerAccessible(light_currentController))
  681. {
  682. if(light_getMaxBrightness(light_currentController, &currVal))
  683. {
  684. if(currVal > bestValYet)
  685. {
  686. foundOkController = TRUE;
  687. bestValYet = currVal;
  688. memset(bestYet, '\0', 256);
  689. strcpy(bestYet, light_currentController);
  690. light_Configuration.hasCachedMaxBrightness = TRUE;
  691. light_Configuration.cachedMaxBrightness = currVal;
  692. }else{
  693. LIGHT_NOTE("ignoring controller as better one already found");
  694. }
  695. }else{
  696. LIGHT_WARN("could not read max brightness from file");
  697. }
  698. }else{
  699. LIGHT_WARN("controller not accessible");
  700. }
  701. }
  702. if(!foundOkController)
  703. {
  704. LIGHT_ERR("could not find an accessible controller");
  705. return FALSE;
  706. }
  707. if(bestValYet == 0)
  708. {
  709. LIGHT_ERR("found accessible controller but it's useless/corrupt");
  710. return FALSE;
  711. }
  712. memset(controller, '\0', 256);
  713. strcpy(controller, bestYet);
  714. return TRUE;
  715. }
  716. LIGHT_BOOL light_getMinCap(char const * controller, LIGHT_BOOL * hasMinCap, unsigned long * minCap)
  717. {
  718. char * mincapPath = NULL;
  719. if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
  720. {
  721. LIGHT_ERR("could not generate path to minimum cap file");
  722. return FALSE;
  723. }
  724. if(!light_isReadable(mincapPath)){
  725. *hasMinCap = FALSE;
  726. *minCap = 0;
  727. free(mincapPath);
  728. return TRUE;
  729. }
  730. if(!light_readULong(mincapPath, minCap))
  731. {
  732. LIGHT_ERR("could not read minimum cap from file");
  733. free(mincapPath);
  734. return FALSE;
  735. }
  736. *hasMinCap = TRUE;
  737. free(mincapPath);
  738. return TRUE;
  739. }
  740. LIGHT_BOOL light_setMinCap(char const * controller, unsigned long v)
  741. {
  742. char * mincapPath = NULL;
  743. if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
  744. {
  745. LIGHT_ERR("could not generate path to minimum cap file");
  746. return FALSE;
  747. }
  748. if(!light_writeULong(mincapPath, v))
  749. {
  750. LIGHT_ERR("could not write to minimum cap file");
  751. free(mincapPath);
  752. return FALSE;
  753. }
  754. free(mincapPath);
  755. return TRUE;
  756. }
  757. LIGHT_BOOL light_listControllers()
  758. {
  759. LIGHT_BOOL foundController = FALSE;
  760. while(light_iterateControllers())
  761. {
  762. if(!foundController)
  763. {
  764. foundController = TRUE;
  765. }
  766. printf("%s\n", light_currentController);
  767. }
  768. if(!foundController)
  769. {
  770. LIGHT_WARN("no controllers found, either check your system or your permissions");
  771. return FALSE;
  772. }
  773. return TRUE;
  774. }
  775. LIGHT_BOOL light_saveBrightness(char const *controller, unsigned long v){
  776. char *savePath = NULL;
  777. if(!light_genPath(controller, LIGHT_SAVERESTORE, &savePath))
  778. {
  779. LIGHT_ERR("could not generate path to save/restore file");
  780. return FALSE;
  781. }
  782. if(!light_writeULong(savePath, v))
  783. {
  784. LIGHT_ERR("could not write to save/restore file");
  785. free(savePath);
  786. return FALSE;
  787. }
  788. free(savePath);
  789. return TRUE;
  790. }
  791. LIGHT_BOOL light_restoreBrightness(char const *controller){
  792. char *restorePath = NULL;
  793. unsigned long v = 0;
  794. if(!light_genPath(controller, LIGHT_SAVERESTORE, &restorePath))
  795. {
  796. LIGHT_ERR("could not generate path to save/restore file");
  797. return FALSE;
  798. }
  799. if(!light_readULong(restorePath, &v))
  800. {
  801. LIGHT_ERR("could not read saved value");
  802. free(restorePath);
  803. return FALSE;
  804. }
  805. if(!light_setBrightness(controller, v))
  806. {
  807. LIGHT_ERR("could not set restored brightness");
  808. free(restorePath);
  809. return FALSE;
  810. }
  811. free(restorePath);
  812. return TRUE;
  813. }