hardware/liblights
Revision | 1b56f57da510b43bd2c9c9f77e88e793b672186b (tree) |
---|---|
Time | 2013-02-28 02:06:49 |
Author | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
search brightness under /sys/class/backlight automatically
@@ -18,6 +18,8 @@ | ||
18 | 18 | |
19 | 19 | #include <cutils/log.h> |
20 | 20 | |
21 | +#include <sys/types.h> | |
22 | +#include <dirent.h> | |
21 | 23 | #include <stdint.h> |
22 | 24 | #include <string.h> |
23 | 25 | #include <unistd.h> |
@@ -39,9 +41,6 @@ char const*const LLP_BRIGHTNESS_FILE = "backlight.brightness_file"; | ||
39 | 41 | char const*const LLP_MAX_BRIGHTNESS_FILE = "backlight.max_brightness_file"; |
40 | 42 | char const*const LLP_MAX_BRIGHTNESS = "backlight.max_brightness"; |
41 | 43 | |
42 | -char const*const DEF_LLP_BRIGHTNESS_FILE = "/sys/class/backlight/acpi_video0/brightness"; | |
43 | -char const*const DEF_LLP_MAX_BRIGHTNESS_FILE = "/sys/class/backlight/acpi_video0/max_brightness"; | |
44 | - | |
45 | 44 | void init_globals(void) |
46 | 45 | { |
47 | 46 | pthread_mutex_init(&g_lock, NULL); |
@@ -110,6 +109,44 @@ static int close_lights(struct light_device_t *dev) | ||
110 | 109 | return 0; |
111 | 110 | } |
112 | 111 | |
112 | +static int check_backlight_file(char const* name, char* path) | |
113 | +{ | |
114 | + int ret; | |
115 | + if (access(name, F_OK)) { | |
116 | + ret = 0; | |
117 | + } else { | |
118 | + ALOGD("Detect %s", name); | |
119 | + strcpy(path, name); | |
120 | + ret = 1; // found | |
121 | + } | |
122 | + return ret; | |
123 | +} | |
124 | + | |
125 | +static int find_backlight_file(char const* file, char* path) | |
126 | +{ | |
127 | + int ret; | |
128 | + char name[PATH_MAX]; | |
129 | + const char* dirname = "/sys/class/backlight"; | |
130 | + | |
131 | + // Check acpi_video0 first. It seems to work in most cases. | |
132 | + snprintf(name, PATH_MAX, "%s/acpi_video0/%s", dirname, file); | |
133 | + if (!(ret = check_backlight_file(name, path))) { | |
134 | + DIR* dir = opendir(dirname); | |
135 | + if (dir != NULL) { | |
136 | + struct dirent* de; | |
137 | + while ((de = readdir(dir))) { | |
138 | + if (de->d_name[0] != '.') { | |
139 | + snprintf(name, PATH_MAX, "%s/%s/%s", dirname, de->d_name, file); | |
140 | + if ((ret = check_backlight_file(name, path))) { | |
141 | + break; | |
142 | + } | |
143 | + } | |
144 | + } | |
145 | + closedir(dir); | |
146 | + } | |
147 | + } | |
148 | + return ret; | |
149 | +} | |
113 | 150 | |
114 | 151 | static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device) |
115 | 152 | { |
@@ -125,10 +162,11 @@ static int open_lights(const struct hw_module_t* module, char const* name, struc | ||
125 | 162 | return -EINVAL; |
126 | 163 | } |
127 | 164 | } else { |
128 | - if (property_get(LLP_MAX_BRIGHTNESS_FILE, max_b_file, DEF_LLP_MAX_BRIGHTNESS_FILE)) { | |
165 | + if (property_get(LLP_MAX_BRIGHTNESS_FILE, max_b_file, NULL) || | |
166 | + find_backlight_file("max_brightness", max_b_file)) { | |
129 | 167 | max_brightness = read_int(max_b_file); |
130 | 168 | } else { |
131 | - ALOGE("%s system property not set", LLP_MAX_BRIGHTNESS_FILE); | |
169 | + ALOGE("Unable to detect max_brightness. Try to set %s", LLP_MAX_BRIGHTNESS_FILE); | |
132 | 170 | return -EINVAL; |
133 | 171 | } |
134 | 172 | } |
@@ -136,7 +174,8 @@ static int open_lights(const struct hw_module_t* module, char const* name, struc | ||
136 | 174 | if (max_brightness < 1) { |
137 | 175 | max_brightness = 255; |
138 | 176 | } |
139 | - if (!property_get(LLP_BRIGHTNESS_FILE, brightness_file, DEF_LLP_BRIGHTNESS_FILE)) { | |
177 | + if (!property_get(LLP_BRIGHTNESS_FILE, brightness_file, NULL) && | |
178 | + !find_backlight_file("brightness", brightness_file)) { | |
140 | 179 | ALOGE("%s system property not set", LLP_BRIGHTNESS_FILE); |
141 | 180 | return -EINVAL; |
142 | 181 | } |