123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. " -T Multiply brightness by value (can be a non-whole number, ignores raw mode)\n"
  162. " -S Set brightness to value\n"
  163. " -G Get brightness\n"
  164. " -N Set minimum brightness to value\n"
  165. " -P Get minimum brightness\n"
  166. " -I Save the current brightness\n"
  167. " -O Restore the previously saved brightness\n"
  168. "\n"
  169. "Options:\n"
  170. " -r Interpret input and output values in raw mode (ignored for -T)\n"
  171. " -s Specify device target path to use, use -L to list available\n"
  172. " -v Specify the verbosity level (default 0)\n"
  173. " 0: Values only\n"
  174. " 1: Values, Errors.\n"
  175. " 2: Values, Errors, Warnings.\n"
  176. " 3: Values, Errors, Warnings, Notices.\n"
  177. "\n");
  178. printf("Copyright (C) %s %s\n", LIGHT_YEAR, LIGHT_AUTHOR);
  179. printf("This is free software, see the source for copying conditions. There is NO\n"
  180. "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\n"
  181. "\n");
  182. }
  183. static bool _light_set_context_command(light_context_t *ctx, LFUNCCOMMAND new_cmd)
  184. {
  185. if(ctx->run_params.command != NULL)
  186. {
  187. LIGHT_WARN("a command was already set. ignoring.");
  188. return false;
  189. }
  190. ctx->run_params.command = new_cmd;
  191. return true;
  192. }
  193. static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
  194. {
  195. int32_t curr_arg = -1;
  196. int32_t log_level = 0;
  197. char ctrl_name[NAME_MAX];
  198. bool need_value = false;
  199. bool need_float_value = false;
  200. bool need_target = true; // default cmd is get brightness
  201. bool specified_target = false;
  202. snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto");
  203. while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUTOIv:s:r")) != -1)
  204. {
  205. switch(curr_arg)
  206. {
  207. // Options
  208. case 'v':
  209. if (sscanf(optarg, "%i", &log_level) != 1)
  210. {
  211. fprintf(stderr, "-v argument is not an integer.\n\n");
  212. _light_print_usage();
  213. return false;
  214. }
  215. if (log_level < 0 || log_level > 3)
  216. {
  217. fprintf(stderr, "-v argument must be between 0 and 3.\n\n");
  218. _light_print_usage();
  219. return false;
  220. }
  221. light_loglevel = (light_loglevel_t)log_level;
  222. break;
  223. case 's':
  224. snprintf(ctrl_name, sizeof(ctrl_name), "%s", optarg);
  225. specified_target = true;
  226. break;
  227. case 'r':
  228. ctx->run_params.raw_mode = true;
  229. break;
  230. // Commands
  231. case 'H':
  232. case 'h':
  233. _light_set_context_command(ctx, light_cmd_print_help);
  234. break;
  235. case 'V':
  236. _light_set_context_command(ctx, light_cmd_print_version);
  237. break;
  238. case 'G':
  239. _light_set_context_command(ctx, light_cmd_get_brightness);
  240. need_target = true;
  241. break;
  242. case 'S':
  243. _light_set_context_command(ctx, light_cmd_set_brightness);
  244. need_value = true;
  245. need_target = true;
  246. break;
  247. case 'L':
  248. _light_set_context_command(ctx, light_cmd_list_devices);
  249. break;
  250. case 'M':
  251. _light_set_context_command(ctx, light_cmd_get_max_brightness);
  252. need_target = true;
  253. break;
  254. case 'N':
  255. _light_set_context_command(ctx, light_cmd_set_min_brightness);
  256. need_target = true;
  257. need_value = true;
  258. break;
  259. case 'P':
  260. _light_set_context_command(ctx, light_cmd_get_min_brightness);
  261. need_target = true;
  262. break;
  263. case 'A':
  264. _light_set_context_command(ctx, light_cmd_add_brightness);
  265. need_target = true;
  266. need_value = true;
  267. break;
  268. case 'U':
  269. _light_set_context_command(ctx, light_cmd_sub_brightness);
  270. need_target = true;
  271. need_value = true;
  272. break;
  273. case 'T':
  274. _light_set_context_command(ctx, light_cmd_mul_brightness);
  275. need_target = true;
  276. need_float_value = true;
  277. break;
  278. case 'O':
  279. _light_set_context_command(ctx, light_cmd_save_brightness);
  280. need_target = true;
  281. break;
  282. case 'I':
  283. _light_set_context_command(ctx, light_cmd_restore_brightness);
  284. need_target = true;
  285. break;
  286. }
  287. }
  288. if(ctx->run_params.command == NULL)
  289. {
  290. _light_set_context_command(ctx, light_cmd_get_brightness);
  291. }
  292. if(need_target)
  293. {
  294. light_device_target_t *curr_target = light_find_device_target(ctx, ctrl_name);
  295. if(curr_target == NULL)
  296. {
  297. if(specified_target)
  298. {
  299. fprintf(stderr, "We couldn't find the specified device target at the path \"%s\". Use -L to find one.\n\n", ctrl_name);
  300. return false;
  301. }
  302. else
  303. {
  304. 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");
  305. curr_target = light_find_device_target(ctx, "util/test/dryrun");
  306. }
  307. }
  308. ctx->run_params.device_target = curr_target;
  309. }
  310. if(need_value || need_float_value)
  311. {
  312. if ( (argc - optind) != 1)
  313. {
  314. fprintf(stderr, "please specify a <value> for this command.\n\n");
  315. _light_print_usage();
  316. return false;
  317. }
  318. }
  319. if (need_value)
  320. {
  321. if (ctx->run_params.raw_mode)
  322. {
  323. if (sscanf(argv[optind], "%lu", &ctx->run_params.value) != 1)
  324. {
  325. fprintf(stderr, "<value> is not an integer.\n\n");
  326. _light_print_usage();
  327. return false;
  328. }
  329. }
  330. else
  331. {
  332. double percent_value = 0.0;
  333. if (sscanf(argv[optind], "%lf", &percent_value) != 1)
  334. {
  335. fprintf(stderr, "<value> is not a decimal.\n\n");
  336. _light_print_usage();
  337. return false;
  338. }
  339. percent_value = light_percent_clamp(percent_value);
  340. uint64_t raw_value = 0;
  341. if(!_light_percent_to_raw(ctx->run_params.device_target, percent_value, &raw_value))
  342. {
  343. LIGHT_ERR("failed to convert from percent to raw for device target");
  344. return false;
  345. }
  346. ctx->run_params.value = raw_value;
  347. }
  348. }
  349. if (need_float_value)
  350. {
  351. if (sscanf(argv[optind], "%f", &ctx->run_params.float_value) != 1)
  352. {
  353. fprintf(stderr, "<value> is not a float.\n\n");
  354. _light_print_usage();
  355. return false;
  356. }
  357. }
  358. return true;
  359. }
  360. /* API function definitions */
  361. light_context_t* light_initialize(int argc, char **argv)
  362. {
  363. light_context_t *new_ctx = malloc(sizeof(light_context_t));
  364. // Setup default values and runtime params
  365. new_ctx->enumerators = NULL;
  366. new_ctx->num_enumerators = 0;
  367. new_ctx->run_params.command = NULL;
  368. new_ctx->run_params.device_target = NULL;
  369. new_ctx->run_params.value = 0;
  370. new_ctx->run_params.raw_mode = false;
  371. // Setup the configuration folder
  372. // If we are root, use the system-wide configuration folder, otherwise try to find a user-specific folder, or fall back to ~/.config
  373. if (geteuid() == 0)
  374. {
  375. snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light");
  376. }
  377. else
  378. {
  379. char *xdg_conf = getenv("XDG_CONFIG_HOME");
  380. if (xdg_conf != NULL)
  381. {
  382. snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
  383. }
  384. else
  385. {
  386. snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME"));
  387. }
  388. }
  389. // Make sure the configuration folder exists, otherwise attempt to create it
  390. int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  391. if (rc && errno != EEXIST)
  392. {
  393. LIGHT_WARN("couldn't create configuration directory");
  394. return false;
  395. }
  396. // Create the built-in enumerators
  397. light_create_enumerator(new_ctx, "sysfs", &impl_sysfs_init, &impl_sysfs_free);
  398. light_create_enumerator(new_ctx, "util", &impl_util_init, &impl_util_free);
  399. light_create_enumerator(new_ctx, "razer", &impl_razer_init, &impl_razer_free);
  400. // This is where we would create enumerators from plugins as well
  401. // 1. Run the plugins get_name() function to get its name
  402. // 2. Point to the plugins init() and free() functions when creating the enumerator
  403. // initialize all enumerators, this will create all the devices and their targets
  404. if(!light_init_enumerators(new_ctx))
  405. {
  406. LIGHT_WARN("failed to initialize all enumerators");
  407. }
  408. // Parse arguments
  409. if(!_light_parse_arguments(new_ctx, argc, argv))
  410. {
  411. LIGHT_ERR("failed to parse arguments");
  412. return NULL;
  413. }
  414. return new_ctx;
  415. }
  416. bool light_execute(light_context_t *ctx)
  417. {
  418. if(ctx->run_params.command == NULL)
  419. {
  420. LIGHT_ERR("run parameters command was null, can't execute");
  421. return false;
  422. }
  423. return ctx->run_params.command(ctx);
  424. }
  425. void light_free(light_context_t *ctx)
  426. {
  427. if(!light_free_enumerators(ctx))
  428. {
  429. LIGHT_WARN("failed to free all enumerators");
  430. }
  431. free(ctx);
  432. }
  433. light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT init_func, LFUNCENUMFREE free_func)
  434. {
  435. // Create a new enumerator array
  436. uint64_t new_num_enumerators = ctx->num_enumerators + 1;
  437. light_device_enumerator_t **new_enumerators = malloc(new_num_enumerators * sizeof(light_device_enumerator_t*));
  438. // Copy old enumerator array to new one
  439. for(uint64_t i = 0; i < ctx->num_enumerators; i++)
  440. {
  441. new_enumerators[i] = ctx->enumerators[i];
  442. }
  443. // Allocate the new enumerator
  444. new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t));
  445. light_device_enumerator_t *returner = new_enumerators[ctx->num_enumerators];
  446. returner->devices = NULL;
  447. returner->num_devices = 0;
  448. returner->init = init_func;
  449. returner->free = free_func;
  450. snprintf(returner->name, sizeof(returner->name), "%s", name);
  451. // Free the old enumerator array, if needed
  452. if(ctx->enumerators != NULL)
  453. {
  454. free(ctx->enumerators);
  455. }
  456. // Replace the enumerator array with the new one
  457. ctx->enumerators = new_enumerators;
  458. ctx->num_enumerators = new_num_enumerators;
  459. // Return newly created device
  460. return returner;
  461. }
  462. bool light_init_enumerators(light_context_t *ctx)
  463. {
  464. bool success = true;
  465. for(uint64_t i = 0; i < ctx->num_enumerators; i++)
  466. {
  467. light_device_enumerator_t * curr_enumerator = ctx->enumerators[i];
  468. if(!curr_enumerator->init(curr_enumerator))
  469. {
  470. success = false;
  471. }
  472. }
  473. return success;
  474. }
  475. bool light_free_enumerators(light_context_t *ctx)
  476. {
  477. bool success = true;
  478. for(uint64_t i = 0; i < ctx->num_enumerators; i++)
  479. {
  480. light_device_enumerator_t * curr_enumerator = ctx->enumerators[i];
  481. if(!curr_enumerator->free(curr_enumerator))
  482. {
  483. success = false;
  484. }
  485. if(curr_enumerator->devices != NULL)
  486. {
  487. for(uint64_t d = 0; d < curr_enumerator->num_devices; d++)
  488. {
  489. light_delete_device(curr_enumerator->devices[d]);
  490. }
  491. free(curr_enumerator->devices);
  492. curr_enumerator->devices = NULL;
  493. }
  494. free(curr_enumerator);
  495. }
  496. free(ctx->enumerators);
  497. ctx->enumerators = NULL;
  498. ctx->num_enumerators = 0;
  499. return success;
  500. }
  501. bool light_split_target_path(char const *in_path, light_target_path_t *out_path)
  502. {
  503. char const * begin = in_path;
  504. char const * end = strstr(begin, "/");
  505. if(end == NULL)
  506. {
  507. LIGHT_WARN("invalid path passed to split_target_path");
  508. return false;
  509. }
  510. size_t size = end - begin;
  511. strncpy(out_path->enumerator, begin, size);
  512. out_path->enumerator[size] = '\0';
  513. begin = end + 1;
  514. end = strstr(begin, "/");
  515. if(end == NULL)
  516. {
  517. LIGHT_WARN("invalid path passed to split_target_path");
  518. return false;
  519. }
  520. size = end - begin;
  521. strncpy(out_path->device, begin, size);
  522. out_path->device[size] = '\0';
  523. strcpy(out_path->target, end + 1);
  524. return true;
  525. }
  526. light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name)
  527. {
  528. light_target_path_t new_path;
  529. if(!light_split_target_path(name, &new_path))
  530. {
  531. LIGHT_WARN("light_find_device_target needs a path in the format of \"enumerator/device/target\", the following format is not recognized: \"%s\"", name);
  532. return NULL;
  533. }
  534. /*
  535. Uncomment to debug the split function
  536. printf("enumerator: %s %u\ndevice: %s %u\ntarget: %s %u\n",
  537. new_path.enumerator, strlen(new_path.enumerator),
  538. new_path.device, strlen(new_path.device),
  539. new_path.target, strlen(new_path.target));
  540. */
  541. // find a matching enumerator
  542. light_device_enumerator_t *enumerator = _light_find_enumerator(ctx, new_path.enumerator);
  543. if(enumerator == NULL)
  544. {
  545. LIGHT_WARN("no such enumerator, \"%s\"", new_path.enumerator);
  546. return NULL;
  547. }
  548. light_device_t *device = _light_find_device(enumerator, new_path.device);
  549. if(device == NULL)
  550. {
  551. LIGHT_WARN("no such device, \"%s\"", new_path.device);
  552. return NULL;
  553. }
  554. light_device_target_t *target = _light_find_target(device, new_path.target);
  555. if(target == NULL)
  556. {
  557. LIGHT_WARN("no such target, \"%s\"", new_path.target);
  558. return NULL;
  559. }
  560. return target;
  561. }
  562. bool light_cmd_print_help(light_context_t *ctx)
  563. {
  564. _light_print_usage();
  565. return true;
  566. }
  567. bool light_cmd_print_version(light_context_t *ctx)
  568. {
  569. printf("v%s\n", VERSION);
  570. return true;
  571. }
  572. bool light_cmd_list_devices(light_context_t *ctx)
  573. {
  574. printf("Listing device targets:\n");
  575. for(uint64_t enumerator = 0; enumerator < ctx->num_enumerators; enumerator++)
  576. {
  577. light_device_enumerator_t *curr_enumerator = ctx->enumerators[enumerator];
  578. for(uint64_t device = 0; device < curr_enumerator->num_devices; device++)
  579. {
  580. light_device_t *curr_device = curr_enumerator->devices[device];
  581. for(uint64_t target = 0; target < curr_device->num_targets; target++)
  582. {
  583. light_device_target_t *curr_target = curr_device->targets[target];
  584. printf("\t%s/%s/%s\n", curr_enumerator->name, curr_device->name, curr_target->name);
  585. }
  586. }
  587. }
  588. return true;
  589. }
  590. bool light_cmd_set_brightness(light_context_t *ctx)
  591. {
  592. light_device_target_t *target = ctx->run_params.device_target;
  593. if(target == NULL)
  594. {
  595. LIGHT_ERR("didn't have a valid target, programmer mistake");
  596. return false;
  597. }
  598. uint64_t mincap = _light_get_min_cap(ctx);
  599. uint64_t value = ctx->run_params.value;
  600. if(mincap > value)
  601. {
  602. value = mincap;
  603. }
  604. if(!target->set_value(target, value))
  605. {
  606. LIGHT_ERR("failed to write to target");
  607. return false;
  608. }
  609. return true;
  610. }
  611. bool light_cmd_get_brightness(light_context_t *ctx)
  612. {
  613. light_device_target_t *target = ctx->run_params.device_target;
  614. if(target == NULL)
  615. {
  616. LIGHT_ERR("didn't have a valid target, programmer mistake");
  617. return false;
  618. }
  619. uint64_t value = 0;
  620. if(!target->get_value(target, &value))
  621. {
  622. LIGHT_ERR("failed to read from target");
  623. return false;
  624. }
  625. if(ctx->run_params.raw_mode)
  626. {
  627. printf("%" PRIu64 "\n", value);
  628. }
  629. else
  630. {
  631. double percent = 0.0;
  632. if(!_light_raw_to_percent(target, value, &percent))
  633. {
  634. LIGHT_ERR("failed to convert from raw to percent from device target");
  635. return false;
  636. }
  637. printf("%.2f\n", percent);
  638. }
  639. return true;
  640. }
  641. bool light_cmd_get_max_brightness(light_context_t *ctx)
  642. {
  643. light_device_target_t *target = ctx->run_params.device_target;
  644. if(target == NULL)
  645. {
  646. LIGHT_ERR("didn't have a valid target, programmer mistake");
  647. return false;
  648. }
  649. if(!ctx->run_params.raw_mode)
  650. {
  651. printf("100.0\n");
  652. return true;
  653. }
  654. uint64_t max_value = 0;
  655. if(!target->get_max_value(target, &max_value))
  656. {
  657. LIGHT_ERR("failed to read from device target");
  658. return false;
  659. }
  660. printf("%" PRIu64 "\n", max_value);
  661. return true;
  662. }
  663. bool light_cmd_set_min_brightness(light_context_t *ctx)
  664. {
  665. char target_path[NAME_MAX];
  666. _light_get_target_path(ctx, target_path, sizeof(target_path));
  667. // Make sure the target folder exists, otherwise attempt to create it
  668. int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  669. if (rc && errno != EEXIST)
  670. {
  671. LIGHT_ERR("couldn't create target directory for minimum brightness");
  672. return false;
  673. }
  674. char target_filepath[NAME_MAX];
  675. _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "minimum");
  676. if(!light_file_write_uint64(target_filepath, ctx->run_params.value))
  677. {
  678. LIGHT_ERR("couldn't write value to minimum file");
  679. return false;
  680. }
  681. return true;
  682. }
  683. bool light_cmd_get_min_brightness(light_context_t *ctx)
  684. {
  685. char target_path[NAME_MAX];
  686. _light_get_target_file(ctx, target_path, sizeof(target_path), "minimum");
  687. uint64_t minimum_value = 0;
  688. if(!light_file_read_uint64(target_path, &minimum_value))
  689. {
  690. if(ctx->run_params.raw_mode)
  691. {
  692. printf("0\n");
  693. }
  694. else
  695. {
  696. printf("0.00\n");
  697. }
  698. return true;
  699. }
  700. if(ctx->run_params.raw_mode)
  701. {
  702. printf("%" PRIu64 "\n", minimum_value);
  703. }
  704. else
  705. {
  706. double minimum_d = 0.0;
  707. if(!_light_raw_to_percent(ctx->run_params.device_target, minimum_value, &minimum_d))
  708. {
  709. LIGHT_ERR("failed to convert value from raw to percent for device target");
  710. return false;
  711. }
  712. printf("%.2f\n", minimum_d);
  713. }
  714. return true;
  715. }
  716. bool light_cmd_add_brightness(light_context_t *ctx)
  717. {
  718. light_device_target_t *target = ctx->run_params.device_target;
  719. if(target == NULL)
  720. {
  721. LIGHT_ERR("didn't have a valid target, programmer mistake");
  722. return false;
  723. }
  724. uint64_t value = 0;
  725. if(!target->get_value(target, &value))
  726. {
  727. LIGHT_ERR("failed to read from target");
  728. return false;
  729. }
  730. uint64_t max_value = 0;
  731. if(!target->get_max_value(target, &max_value))
  732. {
  733. LIGHT_ERR("failed to read from target");
  734. return false;
  735. }
  736. value += ctx->run_params.value;
  737. uint64_t mincap = _light_get_min_cap(ctx);
  738. if(mincap > value)
  739. {
  740. value = mincap;
  741. }
  742. if(value > max_value)
  743. {
  744. value = max_value;
  745. }
  746. if(!target->set_value(target, value))
  747. {
  748. LIGHT_ERR("failed to write to target");
  749. return false;
  750. }
  751. return true;
  752. }
  753. bool light_cmd_sub_brightness(light_context_t *ctx)
  754. {
  755. light_device_target_t *target = ctx->run_params.device_target;
  756. if(target == NULL)
  757. {
  758. LIGHT_ERR("didn't have a valid target, programmer mistake");
  759. return false;
  760. }
  761. uint64_t value = 0;
  762. if(!target->get_value(target, &value))
  763. {
  764. LIGHT_ERR("failed to read from target");
  765. return false;
  766. }
  767. if(value > ctx->run_params.value)
  768. {
  769. value -= ctx->run_params.value;
  770. }
  771. else
  772. {
  773. value = 0;
  774. }
  775. uint64_t mincap = _light_get_min_cap(ctx);
  776. if(mincap > value)
  777. {
  778. value = mincap;
  779. }
  780. if(!target->set_value(target, value))
  781. {
  782. LIGHT_ERR("failed to write to target");
  783. return false;
  784. }
  785. return true;
  786. }
  787. bool light_cmd_mul_brightness(light_context_t *ctx)
  788. {
  789. light_device_target_t *target = ctx->run_params.device_target;
  790. if(target == NULL)
  791. {
  792. LIGHT_ERR("didn't have a valid target, programmer mistake");
  793. return false;
  794. }
  795. uint64_t value = 0;
  796. if(!target->get_value(target, &value))
  797. {
  798. LIGHT_ERR("failed to read from target");
  799. return false;
  800. }
  801. uint64_t max_value = 0;
  802. if(!target->get_max_value(target, &max_value))
  803. {
  804. LIGHT_ERR("failed to read from target");
  805. return false;
  806. }
  807. uint64_t old_value = value;
  808. value *= ctx->run_params.float_value;
  809. // Check that we actually de/increase value
  810. if( value == old_value ) {
  811. if( ctx->runs_params.float_value > 1 ) {
  812. value++;
  813. }
  814. if( ctx->runs_params.float_value < 1 && value > 0 ) {
  815. value--;
  816. }
  817. }
  818. uint64_t mincap = _light_get_min_cap(ctx);
  819. if(mincap > value)
  820. {
  821. value = mincap;
  822. }
  823. if(value > max_value)
  824. {
  825. value = max_value;
  826. }
  827. if(!target->set_value(target, value))
  828. {
  829. LIGHT_ERR("failed to write to target");
  830. return false;
  831. }
  832. return true;
  833. }
  834. bool light_cmd_save_brightness(light_context_t *ctx)
  835. {
  836. char target_path[NAME_MAX];
  837. _light_get_target_path(ctx, target_path, sizeof(target_path));
  838. // Make sure the target folder exists, otherwise attempt to create it
  839. int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  840. if (rc && errno != EEXIST)
  841. {
  842. LIGHT_ERR("couldn't create target directory for save brightness");
  843. return false;
  844. }
  845. char target_filepath[NAME_MAX];
  846. _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "save");
  847. uint64_t curr_value = 0;
  848. if(!ctx->run_params.device_target->get_value(ctx->run_params.device_target, &curr_value))
  849. {
  850. LIGHT_ERR("couldn't read from device target");
  851. return false;
  852. }
  853. if(!light_file_write_uint64(target_filepath, curr_value))
  854. {
  855. LIGHT_ERR("couldn't write value to savefile");
  856. return false;
  857. }
  858. return true;
  859. }
  860. bool light_cmd_restore_brightness(light_context_t *ctx)
  861. {
  862. char target_path[NAME_MAX];
  863. _light_get_target_file(ctx, target_path, sizeof(target_path), "save");
  864. uint64_t saved_value = 0;
  865. if(!light_file_read_uint64(target_path, &saved_value))
  866. {
  867. LIGHT_ERR("couldn't read value from savefile");
  868. return false;
  869. }
  870. uint64_t mincap = _light_get_min_cap(ctx);
  871. if(mincap > saved_value)
  872. {
  873. saved_value = mincap;
  874. }
  875. if(!ctx->run_params.device_target->set_value(ctx->run_params.device_target, saved_value))
  876. {
  877. LIGHT_ERR("couldn't write saved value to device target");
  878. return false;
  879. }
  880. return true;
  881. }
  882. light_device_t *light_create_device(light_device_enumerator_t *enumerator, char const *name, void *device_data)
  883. {
  884. light_device_t *new_device = malloc(sizeof(light_device_t));
  885. new_device->enumerator = enumerator;
  886. new_device->targets = NULL;
  887. new_device->num_targets = 0;
  888. new_device->device_data = device_data;
  889. snprintf(new_device->name, sizeof(new_device->name), "%s", name);
  890. _light_add_enumerator_device(enumerator, new_device);
  891. return new_device;
  892. }
  893. void light_delete_device(light_device_t *device)
  894. {
  895. for(uint64_t i = 0; i < device->num_targets; i++)
  896. {
  897. light_delete_device_target(device->targets[i]);
  898. }
  899. if(device->targets != NULL)
  900. {
  901. free(device->targets);
  902. }
  903. if(device->device_data != NULL)
  904. {
  905. free(device->device_data);
  906. }
  907. free(device);
  908. }
  909. 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)
  910. {
  911. light_device_target_t *new_target = malloc(sizeof(light_device_target_t));
  912. new_target->device = device;
  913. new_target->set_value = setfunc;
  914. new_target->get_value = getfunc;
  915. new_target->get_max_value = getmaxfunc;
  916. new_target->custom_command = cmdfunc;
  917. new_target->device_target_data = target_data;
  918. snprintf(new_target->name, sizeof(new_target->name), "%s", name);
  919. _light_add_device_target(device, new_target);
  920. return new_target;
  921. }
  922. void light_delete_device_target(light_device_target_t *device_target)
  923. {
  924. if(device_target->device_target_data != NULL)
  925. {
  926. free(device_target->device_target_data);
  927. }
  928. free(device_target);
  929. }