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.

112 lines
3.1 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <stdbool.h>
  6. static char *argv0 = "./backlight";
  7. static char *backlight_file = "/sys/devices/platform/backlight/backlight/backlight/brightness";
  8. static char *max_backlight_file = "/sys/devices/platform/backlight/backlight/backlight/max_brightness";
  9. int max_brightness = 10;
  10. void print_usage()
  11. {
  12. printf("usage: %s COMMAND [VALUE]\n", argv0);
  13. printf(" COMMAND = {get, set}\n");
  14. printf(" get ... prints current raw backlight level\n");
  15. printf(" get_percent ... prints current percentage backlight level\n");
  16. printf(" set ... sets backlight level to raw VALUE_RAW\n");
  17. printf(" set_percent ... sets backlight level to percentage VALUE_PCT\n");
  18. printf(" VALUE_RAW = [0..%d]\n", max_brightness);
  19. printf(" VALUE_PCT = [0..100]\n");
  20. }
  21. void print_usage_and_fail()
  22. {
  23. print_usage();
  24. exit(EXIT_FAILURE);
  25. }
  26. void read_max_brightness()
  27. {
  28. FILE *fp = fopen(max_backlight_file, "r");
  29. if (fp == NULL) {
  30. fprintf(stderr, "Cannot open file '%s' for reading. Using the default max brightness '%d'.\n",
  31. max_backlight_file, max_brightness);
  32. return;
  33. }
  34. fscanf(fp, "%d", &max_brightness);
  35. fclose(fp);
  36. }
  37. void command_get(bool percent)
  38. {
  39. FILE *fp = fopen(backlight_file, "r");
  40. if (fp == NULL) {
  41. fprintf(stderr, "Cannot open file '%s' for reading.\n", backlight_file);
  42. exit(EXIT_FAILURE);
  43. }
  44. int buffer_length = 255;
  45. char buffer[buffer_length];
  46. fgets(buffer, buffer_length, fp);
  47. if (percent) {
  48. int value = atoi(buffer);
  49. printf("%d\n", value * 100 / max_brightness);
  50. } else {
  51. printf("%s", buffer);
  52. }
  53. fclose(fp);
  54. }
  55. void command_set(int value, bool percent)
  56. {
  57. if (setuid(0)) {
  58. fprintf(stderr, "Cannot set root permissions via setuid(0).\n");
  59. exit(EXIT_FAILURE);
  60. }
  61. FILE *fp = fopen(backlight_file, "w+");
  62. if (fp == NULL) {
  63. fprintf(stderr, "Cannot open file '%s' for writing.\n", backlight_file);
  64. exit(EXIT_FAILURE);
  65. }
  66. int raw = percent ? (value * max_brightness / 100) : value;
  67. fprintf(fp, "%d\n", raw);
  68. fclose(fp);
  69. }
  70. int main(int argc, char *argv[])
  71. {
  72. read_max_brightness();
  73. if (argc < 1) {
  74. print_usage_and_fail();
  75. }
  76. argv0 = argv[0];
  77. if (argc < 2) {
  78. print_usage_and_fail();
  79. }
  80. char *command = argv[1];
  81. if ((strcmp(command, "get") == 0) && (argc == 2)) {
  82. command_get(false);
  83. } else if ((strcmp(command, "get_percent") == 0) && (argc == 2)) {
  84. command_get(true);
  85. } else if ((strcmp(command, "set") == 0) && (argc == 3)) {
  86. int value = atoi(argv[2]);
  87. if (value < 0 || value > max_brightness) {
  88. print_usage_and_fail();
  89. }
  90. command_set(value, false);
  91. } else if ((strcmp(command, "set_percent") == 0) && (argc == 3)) {
  92. int value = atoi(argv[2]);
  93. if (value < 0 || value > 100) {
  94. print_usage_and_fail();
  95. }
  96. command_set(value, true);
  97. } else {
  98. print_usage_and_fail();
  99. }
  100. exit(EXIT_SUCCESS);
  101. }