The repository formerly known as dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bri.c 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (c) 2017, Curtis McEnroe <programble@gmail.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <dirent.h>
  17. #include <err.h>
  18. #include <errno.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sysexits.h>
  23. #include <unistd.h>
  24. typedef unsigned uint;
  25. static const char *Class = "/sys/class/backlight";
  26. int main(int argc, char *argv[]) {
  27. int error;
  28. const char *input = (argc > 1) ? argv[1] : NULL;
  29. error = chdir(Class);
  30. if (error) err(EX_OSFILE, "%s", Class);
  31. DIR *dir = opendir(".");
  32. if (!dir) err(EX_OSFILE, "%s", Class);
  33. struct dirent *entry;
  34. while (NULL != (errno = 0, entry = readdir(dir))) {
  35. if (entry->d_name[0] == '.') continue;
  36. error = chdir(entry->d_name);
  37. if (error) err(EX_OSFILE, "%s/%s", Class, entry->d_name);
  38. break;
  39. }
  40. if (!entry) {
  41. if (errno) err(EX_IOERR, "%s", Class);
  42. errx(EX_CONFIG, "%s: empty", Class);
  43. }
  44. FILE *actual = fopen("actual_brightness", "r");
  45. if (!actual) err(EX_OSFILE, "actual_brightness");
  46. uint value;
  47. int match = fscanf(actual, "%u", &value);
  48. if (match == EOF) err(EX_IOERR, "actual_brightness");
  49. if (match < 1) errx(EX_DATAERR, "actual_brightness");
  50. if (!input) {
  51. printf("%u\n", value);
  52. return EX_OK;
  53. }
  54. if (input[0] == '+' || input[0] == '-') {
  55. size_t count = strnlen(input, 16);
  56. if (input[0] == '+') {
  57. value += 16 * count;
  58. } else {
  59. value -= 16 * count;
  60. }
  61. } else {
  62. value = strtoul(input, NULL, 0);
  63. }
  64. FILE *brightness = fopen("brightness", "w");
  65. if (!brightness) err(EX_OSFILE, "brightness");
  66. int size = fprintf(brightness, "%u", value);
  67. if (size < 0) err(EX_IOERR, "brightness");
  68. return EX_OK;
  69. }