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>
28 #include <hardware/lights.h>
29 #include <cutils/properties.h>
31 /******************************************************************************/
33 static pthread_once_t g_init = PTHREAD_ONCE_INIT;
34 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
35 static int max_brightness = 255;
36 static char brightness_file[PROPERTY_VALUE_MAX] = { '\0' };
38 char const*const LLP_BRIGHTNESS_FILE = "backlight.brightness_file";
39 char const*const LLP_MAX_BRIGHTNESS_FILE = "backlight.max_brightness_file";
40 char const*const LLP_MAX_BRIGHTNESS = "backlight.max_brightness";
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";
45 void init_globals(void)
47 pthread_mutex_init(&g_lock, NULL);
50 static int write_int(char* path, int value)
53 static int already_warned = 0;
55 fd = open(path, O_RDWR);
58 int bytes = sprintf(buffer, "%d\n", value);
59 int amt = write(fd, buffer, bytes);
61 return amt == -1 ? -errno : 0;
63 if (already_warned == 0) {
64 LOGE("write_int failed to open %s\n", path);
71 static int read_int(char* path)
75 fd = open(path, O_RDONLY);
78 int amt = read(fd, buffer, 3);
80 if (amt <= 0) return -errno;
82 amt = sscanf(buffer, "%d", &ret);
83 return amt == -1 ? -errno : ret;
88 static int rgb_to_brightness16(struct light_state_t const* state)
90 int color = state->color & 0x00ffffff;
91 return ((77*((color>>16)&0x00ff)) + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff)));
94 static int set_light_backlight(struct light_device_t* dev, struct light_state_t const* state)
97 int brightness = rgb_to_brightness16(state) / (65536 / (max_brightness + 1));
98 LOGV("Setting display brightness to %d", brightness);
100 pthread_mutex_lock(&g_lock);
101 err = write_int(brightness_file, (brightness));
102 pthread_mutex_unlock(&g_lock);
107 static int close_lights(struct light_device_t *dev)
114 static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device)
116 int (*set_light)(struct light_device_t* dev,
117 struct light_state_t const* state);
119 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
120 set_light = set_light_backlight;
121 char max_b_file[PROPERTY_VALUE_MAX] = { '\0' };
122 if (property_get(LLP_MAX_BRIGHTNESS, max_b_file, NULL)) {
123 if (!sscanf(max_b_file, "%d", &max_brightness)) {
124 LOGE("%s system property is set to '%s', this could not be parsed as an integer!", LLP_MAX_BRIGHTNESS, max_b_file);
128 if (property_get(LLP_MAX_BRIGHTNESS_FILE, max_b_file, DEF_LLP_MAX_BRIGHTNESS_FILE)) {
129 max_brightness = read_int(max_b_file);
131 LOGE("%s system property not set", LLP_MAX_BRIGHTNESS_FILE);
135 LOGV("Read max display brightness of %d", max_brightness);
136 if (max_brightness < 1) {
137 max_brightness = 255;
139 if (!property_get(LLP_BRIGHTNESS_FILE, brightness_file, DEF_LLP_BRIGHTNESS_FILE)) {
140 LOGE("%s system property not set", LLP_BRIGHTNESS_FILE);
147 pthread_once(&g_init, init_globals);
149 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
150 memset(dev, 0, sizeof(*dev));
152 dev->common.tag = HARDWARE_DEVICE_TAG;
153 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
154 dev->common.module = (struct hw_module_t*)module;
155 dev->common.version = 0;
156 dev->set_light = set_light;
158 *device = (struct hw_device_t*)dev;
163 static struct hw_module_methods_t lights_module_methods = {
167 const struct hw_module_t HAL_MODULE_INFO_SYM = {
168 .tag = HARDWARE_MODULE_TAG,
171 .id = LIGHTS_HARDWARE_MODULE_ID,
172 .name = "Generic sysfs liblights implementation",
173 .author = "Stefan Seidel",
174 .methods = &lights_module_methods,