mybacklight.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. * Author: g0tsu
  12. * Email: g0tsu at dnmx.0rg
  13. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. char *brightness_path;
  19. char *max_brightness_path;
  20. int read_sysfs_int(const char* path) {
  21. FILE *f = fopen(path, "r");
  22. if (!f) {
  23. return -1;
  24. }
  25. int value;
  26. if (fscanf(f, "%d\n", &value) != 1) {
  27. value = -1;
  28. }
  29. fclose(f);
  30. return value;
  31. }
  32. int write_sysfs_int(const char* path, int value) {
  33. FILE *f = fopen(path, "w");
  34. if (!f) {
  35. return -1;
  36. }
  37. int ret = 0;
  38. if(fprintf(f, "%d\n", value) < 0) {
  39. ret = -1;
  40. }
  41. fclose(f);
  42. return ret;
  43. }
  44. int max_brightness() {
  45. return read_sysfs_int(max_brightness_path);
  46. }
  47. int brightness() {
  48. return read_sysfs_int(brightness_path);
  49. }
  50. int set_brightness(int value) {
  51. int max = max_brightness();
  52. if (value > max)
  53. value = max;
  54. return write_sysfs_int(brightness_path, value);
  55. }
  56. int inc_brightness(int value) {
  57. int max = max_brightness();
  58. value = brightness() + value;
  59. if (value > max)
  60. value = max;
  61. return set_brightness(value);
  62. }
  63. int dec_brightness(int value) {
  64. value = brightness() - value;
  65. if (value < 0) {
  66. value = 0;
  67. }
  68. return set_brightness(value);
  69. }
  70. int main(int argc, char *argv[]) {
  71. char *path, *command;
  72. int value;
  73. if (argc < 3) {
  74. goto usage;
  75. }
  76. path = argv[1];
  77. brightness_path = malloc(BUFSIZ + 2);
  78. max_brightness_path = malloc(BUFSIZ + 2);
  79. snprintf(brightness_path, BUFSIZ, "%s/brightness", path);
  80. snprintf(max_brightness_path, BUFSIZ, "%s/max_brightness", path);
  81. command = argv[2];
  82. if (strcmp(command, "-get") == 0) {
  83. printf("%d\n", brightness());
  84. return 0;
  85. }
  86. if (argc < 4) {
  87. goto usage;
  88. }
  89. value = strtol(argv[3], NULL, 10);
  90. if (strcmp(command, "-set") == 0) {
  91. if(set_brightness(value)) {
  92. goto fail;
  93. }
  94. return 0;
  95. }
  96. if (strcmp(command, "-inc") == 0) {
  97. if(inc_brightness(value)) {
  98. goto fail;
  99. }
  100. return 0;
  101. }
  102. if (strcmp(command, "-dec") == 0) {
  103. if(dec_brightness(value)) {
  104. goto fail;
  105. }
  106. return 0;
  107. }
  108. fail:
  109. perror("failed to set brightness");
  110. return 2;
  111. usage:
  112. fprintf(stderr, "Usage: mybacklight /sys/class/leds/smc... [-get|-set|-inc|-dec] [amount]\n");
  113. return 1;
  114. }