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