light.c 25KB

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