backlight_class.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "backlight_class.h"
  15. #include "fileaccess.h"
  16. #include "log.h"
  17. #include "util.h"
  18. #include "conf.h"
  19. #include "main.h"
  20. #include <string.h>
  21. #include <limits.h>
  22. #include <errno.h>
  23. #define BASEPATH "/class/backlight"
  24. static int backlight_class_max_brightness(struct backlight *b)
  25. {
  26. struct backlight_class *bc = container_of(b, struct backlight_class, backlight);
  27. return bc->max_brightness;
  28. }
  29. static int backlight_class_current_brightness(struct backlight *b)
  30. {
  31. struct backlight_class *bc = container_of(b, struct backlight_class, backlight);
  32. return bc->brightness;
  33. }
  34. static int backlight_class_set_brightness(struct backlight *b, int value)
  35. {
  36. struct backlight_class *bc = container_of(b, struct backlight_class, backlight);
  37. int err;
  38. value = min(b->max_brightness(b), value);
  39. value = max(b->min_brightness(b), value);
  40. if (bc->brightness == value)
  41. return 0;
  42. err = file_write_int(bc->set_br_file, value, 10);
  43. if (err)
  44. return err;
  45. bc->brightness = value;
  46. backlight_notify_state_change(b);
  47. return 0;
  48. }
  49. static int backlight_class_read_file(struct backlight_class *bc)
  50. {
  51. int err, value;
  52. err = file_read_int(bc->actual_br_file, &value, 0);
  53. if (err) {
  54. logerr("WARNING: Failed to read actual backlight brightness\n");
  55. return -abs(err);
  56. }
  57. return value;
  58. }
  59. static int backlight_class_update(struct backlight *b)
  60. {
  61. struct backlight_class *bc = container_of(b, struct backlight_class, backlight);
  62. int expected_brightness, cur_brightness, err, res;
  63. expected_brightness = bc->brightness;
  64. res = backlight_class_read_file(bc);
  65. if (res < 0)
  66. return res;
  67. cur_brightness = res;
  68. if (cur_brightness != expected_brightness &&
  69. cur_brightness != b->min_brightness(b)) {
  70. logdebug("Amending backlight brightness from %d to %d\n",
  71. cur_brightness, expected_brightness);
  72. err = backlight_class_set_brightness(b, expected_brightness);
  73. if (err)
  74. return err;
  75. }
  76. return 0;
  77. }
  78. static void backlight_class_destroy(struct backlight *b)
  79. {
  80. struct backlight_class *bc = container_of(b, struct backlight_class, backlight);
  81. file_close(bc->actual_br_file);
  82. file_close(bc->set_br_file);
  83. free(bc);
  84. }
  85. static const char * backlight_select_sysfs_dir(struct list_head *dir_entries)
  86. {
  87. struct dir_entry *ent;
  88. const char *prefer_dir, *selection = NULL;
  89. /* Use the preferred device, if any. */
  90. prefer_dir = config_get(backend.config,
  91. "BACKLIGHT_CLASS",
  92. "prefer_device",
  93. NULL);
  94. if (prefer_dir) {
  95. list_for_each_entry(ent, dir_entries, list) {
  96. if ((ent->type & (DT_LNK | DT_DIR)) &&
  97. strcmp(ent->name, prefer_dir) == 0) {
  98. selection = ent->name;
  99. break;
  100. }
  101. }
  102. if (!selection) {
  103. logerr("class backlight: Failed to find preferred "
  104. "device '%s'\n", prefer_dir);
  105. }
  106. }
  107. if (!selection) {
  108. /* Return first entry, otherwise. */
  109. list_for_each_entry(ent, dir_entries, list) {
  110. if (ent->type & (DT_LNK | DT_DIR)) {
  111. selection = ent->name;
  112. break;
  113. }
  114. }
  115. }
  116. return selection;
  117. }
  118. static struct backlight * backlight_class_probe(void)
  119. {
  120. struct backlight_class *bc;
  121. struct fileaccess *file, *actual_br_file = NULL, *set_br_file = NULL;
  122. LIST_HEAD(dir_entries);
  123. int err, res, max_brightness;
  124. const char *dirname;
  125. err = list_sysfs_directory(&dir_entries, BASEPATH);
  126. if (err <= 0)
  127. return NULL;
  128. dirname = backlight_select_sysfs_dir(&dir_entries);
  129. if (!dirname)
  130. goto err_ent_free;
  131. logdebug("class backlight: Using '%s' for brightness adjustment\n",
  132. dirname);
  133. file = sysfs_file_open(O_RDONLY, "%s/%s/max_brightness",
  134. BASEPATH, dirname);
  135. if (!file)
  136. goto err_files_close;
  137. err = file_read_int(file, &max_brightness, 0);
  138. file_close(file);
  139. if (err)
  140. goto err_files_close;
  141. actual_br_file = sysfs_file_open(O_RDONLY, "%s/%s/actual_brightness",
  142. BASEPATH, dirname);
  143. if (!actual_br_file)
  144. goto err_files_close;
  145. set_br_file = sysfs_file_open(O_RDWR, "%s/%s/brightness",
  146. BASEPATH, dirname);
  147. if (!set_br_file)
  148. goto err_files_close;
  149. bc = zalloc(sizeof(*bc));
  150. if (!bc)
  151. goto err_files_close;
  152. bc->actual_br_file = actual_br_file;
  153. bc->set_br_file = set_br_file;
  154. bc->max_brightness = max_brightness;
  155. backlight_init(&bc->backlight, "class");
  156. bc->backlight.max_brightness = backlight_class_max_brightness;
  157. bc->backlight.current_brightness = backlight_class_current_brightness;
  158. bc->backlight.set_brightness = backlight_class_set_brightness;
  159. bc->backlight.destroy = backlight_class_destroy;
  160. bc->backlight.update = backlight_class_update;
  161. bc->backlight.poll_interval = 2000;
  162. res = backlight_class_read_file(bc);
  163. if (res < 0)
  164. goto err_free;
  165. bc->brightness = res;
  166. backlight_notify_state_change(&bc->backlight);
  167. dir_entries_free(&dir_entries);
  168. return &bc->backlight;
  169. err_free:
  170. free(bc);
  171. err_files_close:
  172. file_close(set_br_file);
  173. file_close(actual_br_file);
  174. err_ent_free:
  175. dir_entries_free(&dir_entries);
  176. return NULL;
  177. }
  178. BACKLIGHT_PROBE(class, backlight_class_probe);