light.c 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. " -O Save the current brightness\n"
  167. " -I 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. uid_t uid = getuid();
  372. uid_t euid = geteuid();
  373. gid_t egid = getegid();
  374. // If the real user ID is different from the effective user ID (SUID mode)
  375. // and if we have the effective user ID of root (0)
  376. // and if the effective group ID is different from root (0),
  377. // then make sure to set the effective group ID to root (0).
  378. if((uid != euid) && (euid == 0) && (egid != 0))
  379. {
  380. if(setegid(euid) < 0)
  381. {
  382. LIGHT_ERR("could not change egid from %u to %u (uid: %u, euid: %u)", egid, euid, uid, euid);
  383. return false;
  384. }
  385. }
  386. // Setup the configuration folder
  387. // If we are root, use the system-wide configuration folder, otherwise try to find a user-specific folder, or fall back to ~/.config
  388. if(euid == 0)
  389. {
  390. snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s", "/etc/light");
  391. }
  392. else
  393. {
  394. char *xdg_conf = getenv("XDG_CONFIG_HOME");
  395. if(xdg_conf != NULL)
  396. {
  397. snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
  398. }
  399. else
  400. {
  401. snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME"));
  402. }
  403. }
  404. // Make sure the configuration folder exists, otherwise attempt to create it
  405. int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  406. if(rc && errno != EEXIST)
  407. {
  408. LIGHT_WARN("couldn't create configuration directory");
  409. return false;
  410. }
  411. // Create the built-in enumerators
  412. light_create_enumerator(new_ctx, "sysfs", &impl_sysfs_init, &impl_sysfs_free);
  413. light_create_enumerator(new_ctx, "util", &impl_util_init, &impl_util_free);
  414. light_create_enumerator(new_ctx, "razer", &impl_razer_init, &impl_razer_free);
  415. // This is where we would create enumerators from plugins as well
  416. // 1. Run the plugins get_name() function to get its name
  417. // 2. Point to the plugins init() and free() functions when creating the enumerator
  418. // initialize all enumerators, this will create all the devices and their targets
  419. if(!light_init_enumerators(new_ctx))
  420. {
  421. LIGHT_WARN("failed to initialize all enumerators");
  422. }
  423. // Parse arguments
  424. if(!_light_parse_arguments(new_ctx, argc, argv))
  425. {
  426. LIGHT_ERR("failed to parse arguments");
  427. return NULL;
  428. }
  429. return new_ctx;
  430. }
  431. bool light_execute(light_context_t *ctx)
  432. {
  433. if(ctx->run_params.command == NULL)
  434. {
  435. LIGHT_ERR("run parameters command was null, can't execute");
  436. return false;
  437. }
  438. return ctx->run_params.command(ctx);
  439. }
  440. void light_free(light_context_t *ctx)
  441. {
  442. if(!light_free_enumerators(ctx))
  443. {
  444. LIGHT_WARN("failed to free all enumerators");
  445. }
  446. free(ctx);
  447. }
  448. light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT init_func, LFUNCENUMFREE free_func)
  449. {
  450. // Create a new enumerator array
  451. uint64_t new_num_enumerators = ctx->num_enumerators + 1;
  452. light_device_enumerator_t **new_enumerators = malloc(new_num_enumerators * sizeof(light_device_enumerator_t*));
  453. // Copy old enumerator array to new one
  454. for(uint64_t i = 0; i < ctx->num_enumerators; i++)
  455. {
  456. new_enumerators[i] = ctx->enumerators[i];
  457. }
  458. // Allocate the new enumerator
  459. new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t));
  460. light_device_enumerator_t *returner = new_enumerators[ctx->num_enumerators];
  461. returner->devices = NULL;
  462. returner->num_devices = 0;
  463. returner->init = init_func;
  464. returner->free = free_func;
  465. snprintf(returner->name, sizeof(returner->name), "%s", name);
  466. // Free the old enumerator array, if needed
  467. if(ctx->enumerators != NULL)
  468. {
  469. free(ctx->enumerators);
  470. }
  471. // Replace the enumerator array with the new one
  472. ctx->enumerators = new_enumerators;
  473. ctx->num_enumerators = new_num_enumerators;
  474. // Return newly created device
  475. return returner;
  476. }
  477. bool light_init_enumerators(light_context_t *ctx)
  478. {
  479. bool success = true;
  480. for(uint64_t i = 0; i < ctx->num_enumerators; i++)
  481. {
  482. light_device_enumerator_t * curr_enumerator = ctx->enumerators[i];
  483. if(!curr_enumerator->init(curr_enumerator))
  484. {
  485. success = false;
  486. }
  487. }
  488. return success;
  489. }
  490. bool light_free_enumerators(light_context_t *ctx)
  491. {
  492. bool success = true;
  493. for(uint64_t i = 0; i < ctx->num_enumerators; i++)
  494. {
  495. light_device_enumerator_t * curr_enumerator = ctx->enumerators[i];
  496. if(!curr_enumerator->free(curr_enumerator))
  497. {
  498. success = false;
  499. }
  500. if(curr_enumerator->devices != NULL)
  501. {
  502. for(uint64_t d = 0; d < curr_enumerator->num_devices; d++)
  503. {
  504. light_delete_device(curr_enumerator->devices[d]);
  505. }
  506. free(curr_enumerator->devices);
  507. curr_enumerator->devices = NULL;
  508. }
  509. free(curr_enumerator);
  510. }
  511. free(ctx->enumerators);
  512. ctx->enumerators = NULL;
  513. ctx->num_enumerators = 0;
  514. return success;
  515. }
  516. bool light_split_target_path(char const *in_path, light_target_path_t *out_path)
  517. {
  518. char const * begin = in_path;
  519. char const * end = strstr(begin, "/");
  520. if(end == NULL)
  521. {
  522. LIGHT_WARN("invalid path passed to split_target_path");
  523. return false;
  524. }
  525. size_t size = end - begin;
  526. strncpy(out_path->enumerator, begin, size);
  527. out_path->enumerator[size] = '\0';
  528. begin = end + 1;
  529. end = strstr(begin, "/");
  530. if(end == NULL)
  531. {
  532. LIGHT_WARN("invalid path passed to split_target_path");
  533. return false;
  534. }
  535. size = end - begin;
  536. strncpy(out_path->device, begin, size);
  537. out_path->device[size] = '\0';
  538. strcpy(out_path->target, end + 1);
  539. return true;
  540. }
  541. light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name)
  542. {
  543. light_target_path_t new_path;
  544. if(!light_split_target_path(name, &new_path))
  545. {
  546. LIGHT_WARN("light_find_device_target needs a path in the format of \"enumerator/device/target\", the following format is not recognized: \"%s\"", name);
  547. return NULL;
  548. }
  549. /*
  550. Uncomment to debug the split function
  551. printf("enumerator: %s %u\ndevice: %s %u\ntarget: %s %u\n",
  552. new_path.enumerator, strlen(new_path.enumerator),
  553. new_path.device, strlen(new_path.device),
  554. new_path.target, strlen(new_path.target));
  555. */
  556. // find a matching enumerator
  557. light_device_enumerator_t *enumerator = _light_find_enumerator(ctx, new_path.enumerator);
  558. if(enumerator == NULL)
  559. {
  560. LIGHT_WARN("no such enumerator, \"%s\"", new_path.enumerator);
  561. return NULL;
  562. }
  563. light_device_t *device = _light_find_device(enumerator, new_path.device);
  564. if(device == NULL)
  565. {
  566. LIGHT_WARN("no such device, \"%s\"", new_path.device);
  567. return NULL;
  568. }
  569. light_device_target_t *target = _light_find_target(device, new_path.target);
  570. if(target == NULL)
  571. {
  572. LIGHT_WARN("no such target, \"%s\"", new_path.target);
  573. return NULL;
  574. }
  575. return target;
  576. }
  577. bool light_cmd_print_help(light_context_t *ctx)
  578. {
  579. _light_print_usage();
  580. return true;
  581. }
  582. bool light_cmd_print_version(light_context_t *ctx)
  583. {
  584. printf("v%s\n", VERSION);
  585. return true;
  586. }
  587. bool light_cmd_list_devices(light_context_t *ctx)
  588. {
  589. printf("Listing device targets:\n");
  590. for(uint64_t enumerator = 0; enumerator < ctx->num_enumerators; enumerator++)
  591. {
  592. light_device_enumerator_t *curr_enumerator = ctx->enumerators[enumerator];
  593. for(uint64_t device = 0; device < curr_enumerator->num_devices; device++)
  594. {
  595. light_device_t *curr_device = curr_enumerator->devices[device];
  596. for(uint64_t target = 0; target < curr_device->num_targets; target++)
  597. {
  598. light_device_target_t *curr_target = curr_device->targets[target];
  599. printf("\t%s/%s/%s\n", curr_enumerator->name, curr_device->name, curr_target->name);
  600. }
  601. }
  602. }
  603. return true;
  604. }
  605. bool light_cmd_set_brightness(light_context_t *ctx)
  606. {
  607. light_device_target_t *target = ctx->run_params.device_target;
  608. if(target == NULL)
  609. {
  610. LIGHT_ERR("didn't have a valid target, programmer mistake");
  611. return false;
  612. }
  613. uint64_t mincap = _light_get_min_cap(ctx);
  614. uint64_t value = ctx->run_params.value;
  615. if(mincap > value)
  616. {
  617. value = mincap;
  618. }
  619. if(!target->set_value(target, value))
  620. {
  621. LIGHT_ERR("failed to write to target");
  622. return false;
  623. }
  624. return true;
  625. }
  626. bool light_cmd_get_brightness(light_context_t *ctx)
  627. {
  628. light_device_target_t *target = ctx->run_params.device_target;
  629. if(target == NULL)
  630. {
  631. LIGHT_ERR("didn't have a valid target, programmer mistake");
  632. return false;
  633. }
  634. uint64_t value = 0;
  635. if(!target->get_value(target, &value))
  636. {
  637. LIGHT_ERR("failed to read from target");
  638. return false;
  639. }
  640. if(ctx->run_params.raw_mode)
  641. {
  642. printf("%" PRIu64 "\n", value);
  643. }
  644. else
  645. {
  646. double percent = 0.0;
  647. if(!_light_raw_to_percent(target, value, &percent))
  648. {
  649. LIGHT_ERR("failed to convert from raw to percent from device target");
  650. return false;
  651. }
  652. printf("%.2f\n", percent);
  653. }
  654. return true;
  655. }
  656. bool light_cmd_get_max_brightness(light_context_t *ctx)
  657. {
  658. light_device_target_t *target = ctx->run_params.device_target;
  659. if(target == NULL)
  660. {
  661. LIGHT_ERR("didn't have a valid target, programmer mistake");
  662. return false;
  663. }
  664. if(!ctx->run_params.raw_mode)
  665. {
  666. printf("100.0\n");
  667. return true;
  668. }
  669. uint64_t max_value = 0;
  670. if(!target->get_max_value(target, &max_value))
  671. {
  672. LIGHT_ERR("failed to read from device target");
  673. return false;
  674. }
  675. printf("%" PRIu64 "\n", max_value);
  676. return true;
  677. }
  678. bool light_cmd_set_min_brightness(light_context_t *ctx)
  679. {
  680. char target_path[NAME_MAX];
  681. _light_get_target_path(ctx, target_path, sizeof(target_path));
  682. // Make sure the target folder exists, otherwise attempt to create it
  683. int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  684. if(rc && errno != EEXIST)
  685. {
  686. LIGHT_ERR("couldn't create target directory for minimum brightness");
  687. return false;
  688. }
  689. char target_filepath[NAME_MAX];
  690. _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "minimum");
  691. if(!light_file_write_uint64(target_filepath, ctx->run_params.value))
  692. {
  693. LIGHT_ERR("couldn't write value to minimum file");
  694. return false;
  695. }
  696. return true;
  697. }
  698. bool light_cmd_get_min_brightness(light_context_t *ctx)
  699. {
  700. char target_path[NAME_MAX];
  701. _light_get_target_file(ctx, target_path, sizeof(target_path), "minimum");
  702. uint64_t minimum_value = 0;
  703. if(!light_file_read_uint64(target_path, &minimum_value))
  704. {
  705. if(ctx->run_params.raw_mode)
  706. {
  707. printf("0\n");
  708. }
  709. else
  710. {
  711. printf("0.00\n");
  712. }
  713. return true;
  714. }
  715. if(ctx->run_params.raw_mode)
  716. {
  717. printf("%" PRIu64 "\n", minimum_value);
  718. }
  719. else
  720. {
  721. double minimum_d = 0.0;
  722. if(!_light_raw_to_percent(ctx->run_params.device_target, minimum_value, &minimum_d))
  723. {
  724. LIGHT_ERR("failed to convert value from raw to percent for device target");
  725. return false;
  726. }
  727. printf("%.2f\n", minimum_d);
  728. }
  729. return true;
  730. }
  731. bool light_cmd_add_brightness(light_context_t *ctx)
  732. {
  733. light_device_target_t *target = ctx->run_params.device_target;
  734. if(target == NULL)
  735. {
  736. LIGHT_ERR("didn't have a valid target, programmer mistake");
  737. return false;
  738. }
  739. uint64_t value = 0;
  740. if(!target->get_value(target, &value))
  741. {
  742. LIGHT_ERR("failed to read from target");
  743. return false;
  744. }
  745. uint64_t max_value = 0;
  746. if(!target->get_max_value(target, &max_value))
  747. {
  748. LIGHT_ERR("failed to read from target");
  749. return false;
  750. }
  751. value += ctx->run_params.value;
  752. uint64_t mincap = _light_get_min_cap(ctx);
  753. if(mincap > value)
  754. {
  755. value = mincap;
  756. }
  757. if(value > max_value)
  758. {
  759. value = max_value;
  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_sub_brightness(light_context_t *ctx)
  769. {
  770. light_device_target_t *target = ctx->run_params.device_target;
  771. if(target == NULL)
  772. {
  773. LIGHT_ERR("didn't have a valid target, programmer mistake");
  774. return false;
  775. }
  776. uint64_t value = 0;
  777. if(!target->get_value(target, &value))
  778. {
  779. LIGHT_ERR("failed to read from target");
  780. return false;
  781. }
  782. if(value > ctx->run_params.value)
  783. {
  784. value -= ctx->run_params.value;
  785. }
  786. else
  787. {
  788. value = 0;
  789. }
  790. uint64_t mincap = _light_get_min_cap(ctx);
  791. if(mincap > value)
  792. {
  793. value = mincap;
  794. }
  795. if(!target->set_value(target, value))
  796. {
  797. LIGHT_ERR("failed to write to target");
  798. return false;
  799. }
  800. return true;
  801. }
  802. bool light_cmd_mul_brightness(light_context_t *ctx)
  803. {
  804. light_device_target_t *target = ctx->run_params.device_target;
  805. if(target == NULL)
  806. {
  807. LIGHT_ERR("didn't have a valid target, programmer mistake");
  808. return false;
  809. }
  810. uint64_t value = 0;
  811. if(!target->get_value(target, &value))
  812. {
  813. LIGHT_ERR("failed to read from target");
  814. return false;
  815. }
  816. uint64_t max_value = 0;
  817. if(!target->get_max_value(target, &max_value))
  818. {
  819. LIGHT_ERR("failed to read from target");
  820. return false;
  821. }
  822. uint64_t old_value = value;
  823. value *= ctx->run_params.float_value;
  824. // Check that we actually de/increase value
  825. if(value == old_value)
  826. {
  827. if(ctx->run_params.float_value > 1)
  828. value++;
  829. if(ctx->run_params.float_value < 1 && value > 0)
  830. value--;
  831. }
  832. uint64_t mincap = _light_get_min_cap(ctx);
  833. if(mincap > value)
  834. {
  835. value = mincap;
  836. }
  837. if(value > max_value)
  838. {
  839. value = max_value;
  840. }
  841. if(!target->set_value(target, value))
  842. {
  843. LIGHT_ERR("failed to write to target");
  844. return false;
  845. }
  846. return true;
  847. }
  848. bool light_cmd_save_brightness(light_context_t *ctx)
  849. {
  850. char target_path[NAME_MAX];
  851. _light_get_target_path(ctx, target_path, sizeof(target_path));
  852. // Make sure the target folder exists, otherwise attempt to create it
  853. int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  854. if(rc && errno != EEXIST)
  855. {
  856. LIGHT_ERR("couldn't create target directory for save brightness");
  857. return false;
  858. }
  859. char target_filepath[NAME_MAX];
  860. _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "save");
  861. uint64_t curr_value = 0;
  862. if(!ctx->run_params.device_target->get_value(ctx->run_params.device_target, &curr_value))
  863. {
  864. LIGHT_ERR("couldn't read from device target");
  865. return false;
  866. }
  867. if(!light_file_write_uint64(target_filepath, curr_value))
  868. {
  869. LIGHT_ERR("couldn't write value to savefile");
  870. return false;
  871. }
  872. return true;
  873. }
  874. bool light_cmd_restore_brightness(light_context_t *ctx)
  875. {
  876. char target_path[NAME_MAX];
  877. _light_get_target_file(ctx, target_path, sizeof(target_path), "save");
  878. uint64_t saved_value = 0;
  879. if(!light_file_read_uint64(target_path, &saved_value))
  880. {
  881. LIGHT_ERR("couldn't read value from savefile");
  882. return false;
  883. }
  884. uint64_t mincap = _light_get_min_cap(ctx);
  885. if(mincap > saved_value)
  886. {
  887. saved_value = mincap;
  888. }
  889. if(!ctx->run_params.device_target->set_value(ctx->run_params.device_target, saved_value))
  890. {
  891. LIGHT_ERR("couldn't write saved value to device target");
  892. return false;
  893. }
  894. return true;
  895. }
  896. light_device_t *light_create_device(light_device_enumerator_t *enumerator, char const *name, void *device_data)
  897. {
  898. light_device_t *new_device = malloc(sizeof(light_device_t));
  899. new_device->enumerator = enumerator;
  900. new_device->targets = NULL;
  901. new_device->num_targets = 0;
  902. new_device->device_data = device_data;
  903. snprintf(new_device->name, sizeof(new_device->name), "%s", name);
  904. _light_add_enumerator_device(enumerator, new_device);
  905. return new_device;
  906. }
  907. void light_delete_device(light_device_t *device)
  908. {
  909. for(uint64_t i = 0; i < device->num_targets; i++)
  910. {
  911. light_delete_device_target(device->targets[i]);
  912. }
  913. if(device->targets != NULL)
  914. {
  915. free(device->targets);
  916. }
  917. if(device->device_data != NULL)
  918. {
  919. free(device->device_data);
  920. }
  921. free(device);
  922. }
  923. 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)
  924. {
  925. light_device_target_t *new_target = malloc(sizeof(light_device_target_t));
  926. new_target->device = device;
  927. new_target->set_value = setfunc;
  928. new_target->get_value = getfunc;
  929. new_target->get_max_value = getmaxfunc;
  930. new_target->custom_command = cmdfunc;
  931. new_target->device_target_data = target_data;
  932. snprintf(new_target->name, sizeof(new_target->name), "%s", name);
  933. _light_add_device_target(device, new_target);
  934. return new_target;
  935. }
  936. void light_delete_device_target(light_device_target_t *device_target)
  937. {
  938. if(device_target->device_target_data != NULL)
  939. {
  940. free(device_target->device_target_data);
  941. }
  942. free(device_target);
  943. }