light.c 24KB

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