light.c 27KB

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