light.c 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. #include "light.h"
  2. #include "helpers.h"
  3. // The different device implementations
  4. #include "impl/sysfs.h"
  5. #include "impl/util.h"
  6. #include "impl/razer.h"
  7. #include <stdlib.h> // malloc, free
  8. #include <string.h> // strstr
  9. #include <stdio.h> // snprintf
  10. #include <unistd.h> // geteuid
  11. #include <sys/types.h> // geteuid
  12. #include <errno.h>
  13. #include <inttypes.h> // PRIu64
  14. /* Static helper functions for this file only, prefix with _ */
  15. static void _light_add_enumerator_device(light_device_enumerator_t *enumerator, light_device_t *new_device)
  16. {
  17. // Create a new device array
  18. uint64_t new_num_devices = enumerator->num_devices + 1;
  19. light_device_t **new_devices = malloc(new_num_devices * sizeof(light_device_t*));
  20. // Copy old device array to new one
  21. for(uint64_t i = 0; i < enumerator->num_devices; i++)
  22. {
  23. new_devices[i] = enumerator->devices[i];
  24. }
  25. // Set the new device
  26. new_devices[enumerator->num_devices] = new_device;
  27. // Free the old devices array, if needed
  28. if(enumerator->devices != NULL)
  29. {
  30. free(enumerator->devices);
  31. }
  32. // Replace the devices array with the new one
  33. enumerator->devices = new_devices;
  34. enumerator->num_devices = new_num_devices;
  35. }
  36. static void _light_add_device_target(light_device_t *device, light_device_target_t *new_target)
  37. {
  38. // Create a new targets array
  39. uint64_t new_num_targets = device->num_targets + 1;
  40. light_device_target_t **new_targets = malloc(new_num_targets * sizeof(light_device_target_t*));
  41. // Copy old targets array to new one
  42. for(uint64_t i = 0; i < device->num_targets; i++)
  43. {
  44. new_targets[i] = device->targets[i];
  45. }
  46. // Set the new target
  47. new_targets[device->num_targets] = new_target;
  48. // Free the old targets array, if needed
  49. if(device->targets != NULL)
  50. {
  51. free(device->targets);
  52. }
  53. // Replace the targets array with the new one
  54. device->targets= new_targets;
  55. device->num_targets = new_num_targets;
  56. }
  57. static void _light_get_target_path(light_context_t* ctx, char* output_path, size_t output_size)
  58. {
  59. snprintf(output_path, output_size,
  60. "%s/targets/%s/%s/%s",
  61. ctx->sys_params.conf_dir,
  62. ctx->run_params.device_target->device->enumerator->name,
  63. ctx->run_params.device_target->device->name,
  64. ctx->run_params.device_target->name
  65. );
  66. }
  67. static void _light_get_target_file(light_context_t* ctx, char* output_path, size_t output_size, char const * file)
  68. {
  69. snprintf(output_path, output_size,
  70. "%s/targets/%s/%s/%s/%s",
  71. ctx->sys_params.conf_dir,
  72. ctx->run_params.device_target->device->enumerator->name,
  73. ctx->run_params.device_target->device->name,
  74. ctx->run_params.device_target->name,
  75. file
  76. );
  77. }
  78. static uint64_t _light_get_min_cap(light_context_t *ctx)
  79. {
  80. char target_path[NAME_MAX];
  81. _light_get_target_file(ctx, target_path, sizeof(target_path), "minimum");
  82. uint64_t minimum_value = 0;
  83. if(!light_file_read_uint64(target_path, &minimum_value))
  84. {
  85. return 0;
  86. }
  87. return minimum_value;
  88. }
  89. static light_device_enumerator_t* _light_find_enumerator(light_context_t *ctx, char const *comp)
  90. {
  91. for(uint64_t e = 0; e < ctx->num_enumerators; e++)
  92. {
  93. if(strncmp(comp, ctx->enumerators[e]->name, NAME_MAX) == 0)
  94. {
  95. return ctx->enumerators[e];
  96. }
  97. }
  98. return NULL;
  99. }
  100. static light_device_t* _light_find_device(light_device_enumerator_t *en, char const *comp)
  101. {
  102. for(uint64_t d = 0; d < en->num_devices; d++)
  103. {
  104. if(strncmp(comp, en->devices[d]->name, NAME_MAX) == 0)
  105. {
  106. return en->devices[d];
  107. }
  108. }
  109. return NULL;
  110. }
  111. static light_device_target_t* _light_find_target(light_device_t * dev, char const *comp)
  112. {
  113. for(uint64_t t = 0; t < dev->num_targets; t++)
  114. {
  115. if(strncmp(comp, dev->targets[t]->name, NAME_MAX) == 0)
  116. {
  117. return dev->targets[t];
  118. }
  119. }
  120. return NULL;
  121. }
  122. static bool _light_raw_to_percent(light_device_target_t *target, uint64_t inraw, double *outpercent)
  123. {
  124. double inraw_d = (double)inraw;
  125. uint64_t max_value = 0;
  126. if(!target->get_max_value(target, &max_value))
  127. {
  128. LIGHT_ERR("couldn't read from target");
  129. return false;
  130. }
  131. double max_value_d = (double)max_value;
  132. double percent = light_percent_clamp((inraw_d / max_value_d) * 100.0);
  133. *outpercent = percent;
  134. return true;
  135. }
  136. static bool _light_percent_to_raw(light_device_target_t *target, double inpercent, uint64_t *outraw)
  137. {
  138. uint64_t max_value = 0;
  139. if(!target->get_max_value(target, &max_value))
  140. {
  141. LIGHT_ERR("couldn't read from target");
  142. return false;
  143. }
  144. double max_value_d = (double)max_value;
  145. double target_value_d = max_value_d * (light_percent_clamp(inpercent) / 100.0);
  146. uint64_t target_value = LIGHT_CLAMP((uint64_t)target_value_d, 0, max_value);
  147. *outraw = target_value;
  148. return true;
  149. }
  150. static void _light_print_usage()
  151. {
  152. printf("Usage:\n"
  153. " light [OPTIONS] [VALUE]\n"
  154. "\n"
  155. "Commands:\n"
  156. " -H, -h Show this help and exit\n"
  157. " -V Show program version and exit\n"
  158. " -L List available devices\n"
  159. " -A Increase brightness by value\n"
  160. " -U Decrease brightness by value\n"
  161. " -S Set brightness to value\n"
  162. " -G Get brightness\n"
  163. " -N Set minimum brightness to value\n"
  164. " -P Get minimum brightness\n"
  165. " -I Save the current brightness\n"
  166. " -O Restore the previously saved brightness\n"
  167. "\n"
  168. "Options:\n"
  169. " -r Interpret input and output values in raw mode\n"
  170. " -s Specify device target path to use, use -L to list available\n"
  171. " -v Specify the verbosity level (default 0)\n"
  172. " 0: Values only\n"
  173. " 1: Values, Errors.\n"
  174. " 2: Values, Errors, Warnings.\n"
  175. " 3: Values, Errors, Warnings, Notices.\n"
  176. "\n");
  177. printf("Copyright (C) %s %s\n", LIGHT_YEAR, LIGHT_AUTHOR);
  178. printf("This is free software, see the source for copying conditions. There is NO\n"
  179. "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\n"
  180. "\n");
  181. }
  182. static bool _light_set_context_command(light_context_t *ctx, LFUNCCOMMAND new_cmd)
  183. {
  184. if(ctx->run_params.command != NULL)
  185. {
  186. LIGHT_WARN("a command was already set. ignoring.");
  187. return false;
  188. }
  189. ctx->run_params.command = new_cmd;
  190. return true;
  191. }
  192. static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
  193. {
  194. int32_t curr_arg = -1;
  195. int32_t log_level = 0;
  196. char ctrl_name[NAME_MAX];
  197. bool need_value = false;
  198. bool need_target = true; // default cmd is get brightness
  199. bool specified_target = false;
  200. snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto");
  201. while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUOIv:s:r")) != -1)
  202. {
  203. switch(curr_arg)
  204. {
  205. // Options
  206. case 'v':
  207. if (sscanf(optarg, "%i", &log_level) != 1)
  208. {
  209. fprintf(stderr, "-v argument is not an integer.\n\n");
  210. _light_print_usage();
  211. return false;
  212. }
  213. if (log_level < 0 || log_level > 3)
  214. {
  215. fprintf(stderr, "-v argument must be between 0 and 3.\n\n");
  216. _light_print_usage();
  217. return false;
  218. }
  219. light_loglevel = (light_loglevel_t)log_level;
  220. break;
  221. case 's':
  222. snprintf(ctrl_name, sizeof(ctrl_name), "%s", optarg);
  223. specified_target = true;
  224. break;
  225. case 'r':
  226. ctx->run_params.raw_mode = true;
  227. break;
  228. // Commands
  229. case 'H':
  230. case 'h':
  231. _light_set_context_command(ctx, light_cmd_print_help);
  232. break;
  233. case 'V':
  234. _light_set_context_command(ctx, light_cmd_print_version);
  235. break;
  236. case 'G':
  237. _light_set_context_command(ctx, light_cmd_get_brightness);
  238. need_target = true;
  239. break;
  240. case 'S':
  241. _light_set_context_command(ctx, light_cmd_set_brightness);
  242. need_value = true;
  243. need_target = true;
  244. break;
  245. case 'L':
  246. _light_set_context_command(ctx, light_cmd_list_devices);
  247. break;
  248. case 'M':
  249. _light_set_context_command(ctx, light_cmd_get_max_brightness);
  250. need_target = true;
  251. break;
  252. case 'N':
  253. _light_set_context_command(ctx, light_cmd_set_min_brightness);
  254. need_target = true;
  255. need_value = true;
  256. break;
  257. case 'P':
  258. _light_set_context_command(ctx, light_cmd_get_min_brightness);
  259. need_target = true;
  260. break;
  261. case 'A':
  262. _light_set_context_command(ctx, light_cmd_add_brightness);
  263. need_target = true;
  264. need_value = true;
  265. break;
  266. case 'U':
  267. _light_set_context_command(ctx, light_cmd_sub_brightness);
  268. need_target = true;
  269. need_value = true;
  270. break;
  271. case 'O':
  272. _light_set_context_command(ctx, light_cmd_save_brightness);
  273. need_target = true;
  274. break;
  275. case 'I':
  276. _light_set_context_command(ctx, light_cmd_restore_brightness);
  277. need_target = true;
  278. break;
  279. }
  280. }
  281. if(ctx->run_params.command == NULL)
  282. {
  283. _light_set_context_command(ctx, light_cmd_get_brightness);
  284. }
  285. if(need_target)
  286. {
  287. light_device_target_t *curr_target = light_find_device_target(ctx, ctrl_name);
  288. if(curr_target == NULL)
  289. {
  290. if(specified_target)
  291. {
  292. fprintf(stderr, "We couldn't find the specified device target at the path \"%s\". Use -L to find one.\n\n", ctrl_name);
  293. return false;
  294. }
  295. else
  296. {
  297. fprintf(stderr, "No backlight controller was found, so we could not decide an automatic target. The current command will have no effect. Please use -L to find a target and then specify it with -s.\n\n");
  298. curr_target = light_find_device_target(ctx, "util/test/dryrun");
  299. }
  300. }
  301. ctx->run_params.device_target = curr_target;
  302. }
  303. if(need_value)
  304. {
  305. if ( (argc - optind) != 1)
  306. {
  307. fprintf(stderr, "please specify a <value> for this command.\n\n");
  308. _light_print_usage();
  309. return false;
  310. }
  311. if (ctx->run_params.raw_mode)
  312. {
  313. if (sscanf(argv[optind], "%lu", &ctx->run_params.value) != 1)
  314. {
  315. fprintf(stderr, "<value> is not an integer.\n\n");
  316. _light_print_usage();
  317. return false;
  318. }
  319. }
  320. else
  321. {
  322. double percent_value = 0.0;
  323. if (sscanf(argv[optind], "%lf", &percent_value) != 1)
  324. {
  325. fprintf(stderr, "<value> is not a decimal.\n\n");
  326. _light_print_usage();
  327. return false;
  328. }
  329. percent_value = light_percent_clamp(percent_value);
  330. uint64_t raw_value = 0;
  331. if(!_light_percent_to_raw(ctx->run_params.device_target, percent_value, &raw_value))
  332. {
  333. LIGHT_ERR("failed to convert from percent to raw for device target");
  334. return false;
  335. }
  336. ctx->run_params.value = raw_value;
  337. }
  338. }
  339. return true;
  340. }
  341. /* API function definitions */
  342. light_context_t* light_initialize(int argc, char **argv)
  343. {
  344. light_context_t *new_ctx = malloc(sizeof(light_context_t));
  345. // Setup default values and runtime params
  346. new_ctx->enumerators = NULL;
  347. new_ctx->num_enumerators = 0;
  348. new_ctx->run_params.command = NULL;
  349. new_ctx->run_params.device_target = NULL;
  350. new_ctx->run_params.value = 0;
  351. new_ctx->run_params.raw_mode = false;
  352. // Setup the configuration folder
  353. // If we are root, use the system-wide configuration folder, otherwise try to find a user-specific folder, or fall back to ~/.config
  354. if (geteuid() == 0)
  355. {
  356. snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light");
  357. }
  358. else
  359. {
  360. char *xdg_conf = getenv("XDG_CONFIG_HOME");
  361. if (xdg_conf != NULL)
  362. {
  363. snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
  364. }
  365. else
  366. {
  367. snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME"));
  368. }
  369. }
  370. // Make sure the configuration folder exists, otherwise attempt to create it
  371. int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  372. if (rc && errno != EEXIST)
  373. {
  374. LIGHT_WARN("couldn't create configuration directory");
  375. return false;
  376. }
  377. // Create the built-in enumerators
  378. light_create_enumerator(new_ctx, "sysfs", &impl_sysfs_init, &impl_sysfs_free);
  379. light_create_enumerator(new_ctx, "util", &impl_util_init, &impl_util_free);
  380. light_create_enumerator(new_ctx, "razer", &impl_razer_init, &impl_razer_free);
  381. // This is where we would create enumerators from plugins as well
  382. // 1. Run the plugins get_name() function to get its name
  383. // 2. Point to the plugins init() and free() functions when creating the enumerator
  384. // initialize all enumerators, this will create all the devices and their targets
  385. if(!light_init_enumerators(new_ctx))
  386. {
  387. LIGHT_WARN("failed to initialize all enumerators");
  388. }
  389. // Parse arguments
  390. if(!_light_parse_arguments(new_ctx, argc, argv))
  391. {
  392. LIGHT_ERR("failed to parse arguments");
  393. return NULL;
  394. }
  395. return new_ctx;
  396. }
  397. bool light_execute(light_context_t *ctx)
  398. {
  399. if(ctx->run_params.command == NULL)
  400. {
  401. LIGHT_ERR("run parameters command was null, can't execute");
  402. return false;
  403. }
  404. return ctx->run_params.command(ctx);
  405. }
  406. void light_free(light_context_t *ctx)
  407. {
  408. if(!light_free_enumerators(ctx))
  409. {
  410. LIGHT_WARN("failed to free all enumerators");
  411. }
  412. free(ctx);
  413. }
  414. light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT init_func, LFUNCENUMFREE free_func)
  415. {
  416. // Create a new enumerator array
  417. uint64_t new_num_enumerators = ctx->num_enumerators + 1;
  418. light_device_enumerator_t **new_enumerators = malloc(new_num_enumerators * sizeof(light_device_enumerator_t*));
  419. // Copy old enumerator array to new one
  420. for(uint64_t i = 0; i < ctx->num_enumerators; i++)
  421. {
  422. new_enumerators[i] = ctx->enumerators[i];
  423. }
  424. // Allocate the new enumerator
  425. new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t));
  426. light_device_enumerator_t *returner = new_enumerators[ctx->num_enumerators];
  427. returner->devices = NULL;
  428. returner->num_devices = 0;
  429. returner->init = init_func;
  430. returner->free = free_func;
  431. snprintf(returner->name, sizeof(returner->name), "%s", name);
  432. // Free the old enumerator array, if needed
  433. if(ctx->enumerators != NULL)
  434. {
  435. free(ctx->enumerators);
  436. }
  437. // Replace the enumerator array with the new one
  438. ctx->enumerators = new_enumerators;
  439. ctx->num_enumerators = new_num_enumerators;
  440. // Return newly created device
  441. return returner;
  442. }
  443. bool light_init_enumerators(light_context_t *ctx)
  444. {
  445. bool success = true;
  446. for(uint64_t i = 0; i < ctx->num_enumerators; i++)
  447. {
  448. light_device_enumerator_t * curr_enumerator = ctx->enumerators[i];
  449. if(!curr_enumerator->init(curr_enumerator))
  450. {
  451. success = false;
  452. }
  453. }
  454. return success;
  455. }
  456. bool light_free_enumerators(light_context_t *ctx)
  457. {
  458. bool success = true;
  459. for(uint64_t i = 0; i < ctx->num_enumerators; i++)
  460. {
  461. light_device_enumerator_t * curr_enumerator = ctx->enumerators[i];
  462. if(!curr_enumerator->free(curr_enumerator))
  463. {
  464. success = false;
  465. }
  466. if(curr_enumerator->devices != NULL)
  467. {
  468. for(uint64_t d = 0; d < curr_enumerator->num_devices; d++)
  469. {
  470. light_delete_device(curr_enumerator->devices[d]);
  471. }
  472. free(curr_enumerator->devices);
  473. curr_enumerator->devices = NULL;
  474. }
  475. free(curr_enumerator);
  476. }
  477. free(ctx->enumerators);
  478. ctx->enumerators = NULL;
  479. ctx->num_enumerators = 0;
  480. return success;
  481. }
  482. bool light_split_target_path(char const *in_path, light_target_path_t *out_path)
  483. {
  484. char const * begin = in_path;
  485. char const * end = strstr(begin, "/");
  486. if(end == NULL)
  487. {
  488. LIGHT_WARN("invalid path passed to split_target_path");
  489. return false;
  490. }
  491. size_t size = end - begin;
  492. strncpy(out_path->enumerator, begin, size);
  493. out_path->enumerator[size] = '\0';
  494. begin = end + 1;
  495. end = strstr(begin, "/");
  496. if(end == NULL)
  497. {
  498. LIGHT_WARN("invalid path passed to split_target_path");
  499. return false;
  500. }
  501. size = end - begin;
  502. strncpy(out_path->device, begin, size);
  503. out_path->device[size] = '\0';
  504. strcpy(out_path->target, end + 1);
  505. return true;
  506. }
  507. light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name)
  508. {
  509. light_target_path_t new_path;
  510. if(!light_split_target_path(name, &new_path))
  511. {
  512. LIGHT_WARN("light_find_device_target needs a path in the format of \"enumerator/device/target\", the following format is not recognized: \"%s\"", name);
  513. return NULL;
  514. }
  515. /*
  516. Uncomment to debug the split function
  517. printf("enumerator: %s %u\ndevice: %s %u\ntarget: %s %u\n",
  518. new_path.enumerator, strlen(new_path.enumerator),
  519. new_path.device, strlen(new_path.device),
  520. new_path.target, strlen(new_path.target));
  521. */
  522. // find a matching enumerator
  523. light_device_enumerator_t *enumerator = _light_find_enumerator(ctx, new_path.enumerator);
  524. if(enumerator == NULL)
  525. {
  526. LIGHT_WARN("no such enumerator, \"%s\"", new_path.enumerator);
  527. return NULL;
  528. }
  529. light_device_t *device = _light_find_device(enumerator, new_path.device);
  530. if(device == NULL)
  531. {
  532. LIGHT_WARN("no such device, \"%s\"", new_path.device);
  533. return NULL;
  534. }
  535. light_device_target_t *target = _light_find_target(device, new_path.target);
  536. if(target == NULL)
  537. {
  538. LIGHT_WARN("no such target, \"%s\"", new_path.target);
  539. return NULL;
  540. }
  541. return target;
  542. }
  543. bool light_cmd_print_help(light_context_t *ctx)
  544. {
  545. _light_print_usage();
  546. return true;
  547. }
  548. bool light_cmd_print_version(light_context_t *ctx)
  549. {
  550. printf("v%s\n", VERSION);
  551. return true;
  552. }
  553. bool light_cmd_list_devices(light_context_t *ctx)
  554. {
  555. printf("Listing device targets:\n");
  556. for(uint64_t enumerator = 0; enumerator < ctx->num_enumerators; enumerator++)
  557. {
  558. light_device_enumerator_t *curr_enumerator = ctx->enumerators[enumerator];
  559. for(uint64_t device = 0; device < curr_enumerator->num_devices; device++)
  560. {
  561. light_device_t *curr_device = curr_enumerator->devices[device];
  562. for(uint64_t target = 0; target < curr_device->num_targets; target++)
  563. {
  564. light_device_target_t *curr_target = curr_device->targets[target];
  565. printf("\t%s/%s/%s\n", curr_enumerator->name, curr_device->name, curr_target->name);
  566. }
  567. }
  568. }
  569. return true;
  570. }
  571. bool light_cmd_set_brightness(light_context_t *ctx)
  572. {
  573. light_device_target_t *target = ctx->run_params.device_target;
  574. if(target == NULL)
  575. {
  576. LIGHT_ERR("didn't have a valid target, programmer mistake");
  577. return false;
  578. }
  579. uint64_t mincap = _light_get_min_cap(ctx);
  580. uint64_t value = ctx->run_params.value;
  581. if(mincap > value)
  582. {
  583. value = mincap;
  584. }
  585. if(!target->set_value(target, value))
  586. {
  587. LIGHT_ERR("failed to write to target");
  588. return false;
  589. }
  590. return true;
  591. }
  592. bool light_cmd_get_brightness(light_context_t *ctx)
  593. {
  594. light_device_target_t *target = ctx->run_params.device_target;
  595. if(target == NULL)
  596. {
  597. LIGHT_ERR("didn't have a valid target, programmer mistake");
  598. return false;
  599. }
  600. uint64_t value = 0;
  601. if(!target->get_value(target, &value))
  602. {
  603. LIGHT_ERR("failed to read from target");
  604. return false;
  605. }
  606. if(ctx->run_params.raw_mode)
  607. {
  608. printf("%" PRIu64 "\n", value);
  609. }
  610. else
  611. {
  612. double percent = 0.0;
  613. if(!_light_raw_to_percent(target, value, &percent))
  614. {
  615. LIGHT_ERR("failed to convert from raw to percent from device target");
  616. return false;
  617. }
  618. printf("%.2f\n", percent);
  619. }
  620. return true;
  621. }
  622. bool light_cmd_get_max_brightness(light_context_t *ctx)
  623. {
  624. light_device_target_t *target = ctx->run_params.device_target;
  625. if(target == NULL)
  626. {
  627. LIGHT_ERR("didn't have a valid target, programmer mistake");
  628. return false;
  629. }
  630. if(!ctx->run_params.raw_mode)
  631. {
  632. printf("100.0\n");
  633. return true;
  634. }
  635. uint64_t max_value = 0;
  636. if(!target->get_max_value(target, &max_value))
  637. {
  638. LIGHT_ERR("failed to read from device target");
  639. return false;
  640. }
  641. printf("%" PRIu64 "\n", max_value);
  642. return true;
  643. }
  644. bool light_cmd_set_min_brightness(light_context_t *ctx)
  645. {
  646. char target_path[NAME_MAX];
  647. _light_get_target_path(ctx, target_path, sizeof(target_path));
  648. // Make sure the target folder exists, otherwise attempt to create it
  649. int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  650. if (rc && errno != EEXIST)
  651. {
  652. LIGHT_ERR("couldn't create target directory for minimum brightness");
  653. return false;
  654. }
  655. char target_filepath[NAME_MAX];
  656. _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "minimum");
  657. if(!light_file_write_uint64(target_filepath, ctx->run_params.value))
  658. {
  659. LIGHT_ERR("couldn't write value to minimum file");
  660. return false;
  661. }
  662. return true;
  663. }
  664. bool light_cmd_get_min_brightness(light_context_t *ctx)
  665. {
  666. char target_path[NAME_MAX];
  667. _light_get_target_file(ctx, target_path, sizeof(target_path), "minimum");
  668. uint64_t minimum_value = 0;
  669. if(!light_file_read_uint64(target_path, &minimum_value))
  670. {
  671. if(ctx->run_params.raw_mode)
  672. {
  673. printf("0\n");
  674. }
  675. else
  676. {
  677. printf("0.00\n");
  678. }
  679. return true;
  680. }
  681. if(ctx->run_params.raw_mode)
  682. {
  683. printf("%" PRIu64 "\n", minimum_value);
  684. }
  685. else
  686. {
  687. double minimum_d = 0.0;
  688. if(!_light_raw_to_percent(ctx->run_params.device_target, minimum_value, &minimum_d))
  689. {
  690. LIGHT_ERR("failed to convert value from raw to percent for device target");
  691. return false;
  692. }
  693. printf("%.2f\n", minimum_d);
  694. }
  695. return true;
  696. }
  697. bool light_cmd_add_brightness(light_context_t *ctx)
  698. {
  699. light_device_target_t *target = ctx->run_params.device_target;
  700. if(target == NULL)
  701. {
  702. LIGHT_ERR("didn't have a valid target, programmer mistake");
  703. return false;
  704. }
  705. uint64_t value = 0;
  706. if(!target->get_value(target, &value))
  707. {
  708. LIGHT_ERR("failed to read from target");
  709. return false;
  710. }
  711. uint64_t max_value = 0;
  712. if(!target->get_max_value(target, &max_value))
  713. {
  714. LIGHT_ERR("failed to read from target");
  715. return false;
  716. }
  717. value += ctx->run_params.value;
  718. uint64_t mincap = _light_get_min_cap(ctx);
  719. if(mincap > value)
  720. {
  721. value = mincap;
  722. }
  723. if(value > max_value)
  724. {
  725. value = max_value;
  726. }
  727. if(!target->set_value(target, value))
  728. {
  729. LIGHT_ERR("failed to write to target");
  730. return false;
  731. }
  732. return true;
  733. }
  734. bool light_cmd_sub_brightness(light_context_t *ctx)
  735. {
  736. light_device_target_t *target = ctx->run_params.device_target;
  737. if(target == NULL)
  738. {
  739. LIGHT_ERR("didn't have a valid target, programmer mistake");
  740. return false;
  741. }
  742. uint64_t value = 0;
  743. if(!target->get_value(target, &value))
  744. {
  745. LIGHT_ERR("failed to read from target");
  746. return false;
  747. }
  748. if(value > ctx->run_params.value)
  749. {
  750. value -= ctx->run_params.value;
  751. }
  752. else
  753. {
  754. value = 0;
  755. }
  756. uint64_t mincap = _light_get_min_cap(ctx);
  757. if(mincap > value)
  758. {
  759. value = mincap;
  760. }
  761. if(!target->set_value(target, value))
  762. {
  763. LIGHT_ERR("failed to write to target");
  764. return false;
  765. }
  766. return true;
  767. }
  768. bool light_cmd_save_brightness(light_context_t *ctx)
  769. {
  770. char target_path[NAME_MAX];
  771. _light_get_target_path(ctx, target_path, sizeof(target_path));
  772. // Make sure the target folder exists, otherwise attempt to create it
  773. int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  774. if (rc && errno != EEXIST)
  775. {
  776. LIGHT_ERR("couldn't create target directory for save brightness");
  777. return false;
  778. }
  779. char target_filepath[NAME_MAX];
  780. _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "save");
  781. uint64_t curr_value = 0;
  782. if(!ctx->run_params.device_target->get_value(ctx->run_params.device_target, &curr_value))
  783. {
  784. LIGHT_ERR("couldn't read from device target");
  785. return false;
  786. }
  787. if(!light_file_write_uint64(target_filepath, curr_value))
  788. {
  789. LIGHT_ERR("couldn't write value to savefile");
  790. return false;
  791. }
  792. return true;
  793. }
  794. bool light_cmd_restore_brightness(light_context_t *ctx)
  795. {
  796. char target_path[NAME_MAX];
  797. _light_get_target_file(ctx, target_path, sizeof(target_path), "save");
  798. uint64_t saved_value = 0;
  799. if(!light_file_read_uint64(target_path, &saved_value))
  800. {
  801. LIGHT_ERR("couldn't read value from savefile");
  802. return false;
  803. }
  804. uint64_t mincap = _light_get_min_cap(ctx);
  805. if(mincap > saved_value)
  806. {
  807. saved_value = mincap;
  808. }
  809. if(!ctx->run_params.device_target->set_value(ctx->run_params.device_target, saved_value))
  810. {
  811. LIGHT_ERR("couldn't write saved value to device target");
  812. return false;
  813. }
  814. return true;
  815. }
  816. light_device_t *light_create_device(light_device_enumerator_t *enumerator, char const *name, void *device_data)
  817. {
  818. light_device_t *new_device = malloc(sizeof(light_device_t));
  819. new_device->enumerator = enumerator;
  820. new_device->targets = NULL;
  821. new_device->num_targets = 0;
  822. new_device->device_data = device_data;
  823. snprintf(new_device->name, sizeof(new_device->name), "%s", name);
  824. _light_add_enumerator_device(enumerator, new_device);
  825. return new_device;
  826. }
  827. void light_delete_device(light_device_t *device)
  828. {
  829. for(uint64_t i = 0; i < device->num_targets; i++)
  830. {
  831. light_delete_device_target(device->targets[i]);
  832. }
  833. if(device->targets != NULL)
  834. {
  835. free(device->targets);
  836. }
  837. if(device->device_data != NULL)
  838. {
  839. free(device->device_data);
  840. }
  841. free(device);
  842. }
  843. light_device_target_t *light_create_device_target(light_device_t *device, char const *name, LFUNCVALSET setfunc, LFUNCVALGET getfunc, LFUNCMAXVALGET getmaxfunc, LFUNCCUSTOMCMD cmdfunc, void *target_data)
  844. {
  845. light_device_target_t *new_target = malloc(sizeof(light_device_target_t));
  846. new_target->device = device;
  847. new_target->set_value = setfunc;
  848. new_target->get_value = getfunc;
  849. new_target->get_max_value = getmaxfunc;
  850. new_target->custom_command = cmdfunc;
  851. new_target->device_target_data = target_data;
  852. snprintf(new_target->name, sizeof(new_target->name), "%s", name);
  853. _light_add_device_target(device, new_target);
  854. return new_target;
  855. }
  856. void light_delete_device_target(light_device_target_t *device_target)
  857. {
  858. if(device_target->device_target_data != NULL)
  859. {
  860. free(device_target->device_target_data);
  861. }
  862. free(device_target);
  863. }