2 * Copyright (C) 2012 Stefan Seidel
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define LOG_TAG "lights"
19 #include <cutils/log.h>
21 #include <sys/types.h>
30 #include <hardware/lights.h>
31 #include <cutils/properties.h>
33 /******************************************************************************/
35 static pthread_once_t g_init = PTHREAD_ONCE_INIT;
36 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
37 static int max_brightness = 255;
38 static char brightness_file[PROPERTY_VALUE_MAX] = { '\0' };
40 char const*const LLP_BRIGHTNESS_FILE = "backlight.brightness_file";
41 char const*const LLP_MAX_BRIGHTNESS_FILE = "backlight.max_brightness_file";
42 char const*const LLP_MAX_BRIGHTNESS = "backlight.max_brightness";
44 void init_globals(void)
46 pthread_mutex_init(&g_lock, NULL);
49 static int write_int(char* path, int value)
52 static int already_warned = 0;
54 fd = open(path, O_RDWR);
57 int bytes = sprintf(buffer, "%d\n", value);
58 int amt = write(fd, buffer, bytes);
60 return amt == -1 ? -errno : 0;
62 if (already_warned == 0) {
63 ALOGE("write_int failed to open %s\n", path);
70 static int read_int(char* path)
74 fd = open(path, O_RDONLY);
77 int amt = read(fd, buffer, 20);
79 if (amt <= 0) return -errno;
81 amt = sscanf(buffer, "%d", &ret);
82 return amt == -1 ? -errno : ret;
87 static int rgb_to_brightness16(struct light_state_t const* state)
89 int color = state->color & 0x00ffffff;
90 return ((77*((color>>16)&0x00ff)) + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff)));
93 static int set_light_backlight(struct light_device_t* dev, struct light_state_t const* state)
96 int brightness = rgb_to_brightness16(state) / (65536 / (max_brightness + 1));
97 ALOGV("Setting display brightness to %d", brightness);
99 pthread_mutex_lock(&g_lock);
100 err = write_int(brightness_file, (brightness));
101 pthread_mutex_unlock(&g_lock);
106 static int close_lights(struct light_device_t *dev)
112 static int check_backlight_file(char const* name, char* path)
115 if (access(name, F_OK)) {
118 ALOGD("Detect %s", name);
125 static int find_backlight_file(char const* file, char* path)
129 const char* dirname = "/sys/class/backlight";
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);
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))) {
151 static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device)
153 int (*set_light)(struct light_device_t* dev,
154 struct light_state_t const* state);
156 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
157 set_light = set_light_backlight;
158 char max_b_file[PROPERTY_VALUE_MAX] = { '\0' };
159 if (property_get(LLP_MAX_BRIGHTNESS, max_b_file, NULL)) {
160 if (!sscanf(max_b_file, "%d", &max_brightness)) {
161 ALOGE("%s system property is set to '%s', this could not be parsed as an integer!", LLP_MAX_BRIGHTNESS, max_b_file);
165 if (property_get(LLP_MAX_BRIGHTNESS_FILE, max_b_file, NULL) ||
166 find_backlight_file("max_brightness", max_b_file)) {
167 max_brightness = read_int(max_b_file);
169 ALOGE("Unable to detect max_brightness. Try to set %s", LLP_MAX_BRIGHTNESS_FILE);
173 ALOGV("Read max display brightness of %d", max_brightness);
174 if (max_brightness < 1) {
175 max_brightness = 255;
177 if (!property_get(LLP_BRIGHTNESS_FILE, brightness_file, NULL) &&
178 !find_backlight_file("brightness", brightness_file)) {
179 ALOGE("%s system property not set", LLP_BRIGHTNESS_FILE);
186 pthread_once(&g_init, init_globals);
188 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
189 memset(dev, 0, sizeof(*dev));
191 dev->common.tag = HARDWARE_DEVICE_TAG;
192 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
193 dev->common.module = (struct hw_module_t*)module;
194 dev->common.version = 0;
195 dev->set_light = set_light;
197 *device = (struct hw_device_t*)dev;
202 static struct hw_module_methods_t lights_module_methods = {
206 struct hw_module_t HAL_MODULE_INFO_SYM = {
207 .tag = HARDWARE_MODULE_TAG,
210 .id = LIGHTS_HARDWARE_MODULE_ID,
211 .name = "Generic sysfs liblights implementation",
212 .author = "Stefan Seidel",
213 .methods = &lights_module_methods,