light.c 31KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. // 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. {
  812. if(ctx->run_params.float_value > 1)
  813. value++;
  814. if(ctx->run_params.float_value < 1 && value > 0)
  815. value--;
  816. }
  817. uint64_t mincap = _light_get_min_cap(ctx);
  818. if(mincap > value)
  819. {
  820. value = mincap;
  821. }
  822. if(value > max_value)
  823. {
  824. value = max_value;
  825. }
  826. if(!target->set_value(target, value))
  827. {
  828. LIGHT_ERR("failed to write to target");
  829. return false;
  830. }
  831. return true;
  832. }
  833. bool light_cmd_save_brightness(light_context_t *ctx)
  834. {
  835. char target_path[NAME_MAX];
  836. _light_get_target_path(ctx, target_path, sizeof(target_path));
  837. // Make sure the target folder exists, otherwise attempt to create it
  838. int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  839. if(rc && errno != EEXIST)
  840. {
  841. LIGHT_ERR("couldn't create target directory for save brightness");
  842. return false;
  843. }
  844. char target_filepath[NAME_MAX];
  845. _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "save");
  846. uint64_t curr_value = 0;
  847. if(!ctx->run_params.device_target->get_value(ctx->run_params.device_target, &curr_value))
  848. {
  849. LIGHT_ERR("couldn't read from device target");
  850. return false;
  851. }
  852. if(!light_file_write_uint64(target_filepath, curr_value))
  853. {
  854. LIGHT_ERR("couldn't write value to savefile");
  855. return false;
  856. }
  857. return true;
  858. }
  859. bool light_cmd_restore_brightness(light_context_t *ctx)
  860. {
  861. char target_path[NAME_MAX];
  862. _light_get_target_file(ctx, target_path, sizeof(target_path), "save");
  863. uint64_t saved_value = 0;
  864. if(!light_file_read_uint64(target_path, &saved_value))
  865. {
  866. LIGHT_ERR("couldn't read value from savefile");
  867. return false;
  868. }
  869. uint64_t mincap = _light_get_min_cap(ctx);
  870. if(mincap > saved_value)
  871. {
  872. saved_value = mincap;
  873. }
  874. if(!ctx->run_params.device_target->set_value(ctx->run_params.device_target, saved_value))
  875. {
  876. LIGHT_ERR("couldn't write saved value to device target");
  877. return false;
  878. }
  879. return true;
  880. }
  881. light_device_t *light_create_device(light_device_enumerator_t *enumerator, char const *name, void *device_data)
  882. {
  883. light_device_t *new_device = malloc(sizeof(light_device_t));
  884. new_device->enumerator = enumerator;
  885. new_device->targets = NULL;
  886. new_device->num_targets = 0;
  887. new_device->device_data = device_data;
  888. snprintf(new_device->name, sizeof(new_device->name), "%s", name);
  889. _light_add_enumerator_device(enumerator, new_device);
  890. return new_device;
  891. }
  892. void light_delete_device(light_device_t *device)
  893. {
  894. for(uint64_t i = 0; i < device->num_targets; i++)
  895. {
  896. light_delete_device_target(device->targets[i]);
  897. }
  898. if(device->targets != NULL)
  899. {
  900. free(device->targets);
  901. }
  902. if(device->device_data != NULL)
  903. {
  904. free(device->device_data);
  905. }
  906. free(device);
  907. }
  908. 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)
  909. {
  910. light_device_target_t *new_target = malloc(sizeof(light_device_target_t));
  911. new_target->device = device;
  912. new_target->set_value = setfunc;
  913. new_target->get_value = getfunc;
  914. new_target->get_max_value = getmaxfunc;
  915. new_target->custom_command = cmdfunc;
  916. new_target->device_target_data = target_data;
  917. snprintf(new_target->name, sizeof(new_target->name), "%s", name);
  918. _light_add_device_target(device, new_target);
  919. return new_target;
  920. }
  921. void light_delete_device_target(light_device_target_t *device_target)
  922. {
  923. if(device_target->device_target_data != NULL)
  924. {
  925. free(device_target->device_target_data);
  926. }
  927. free(device_target);
  928. }