OSDN Git Service

include the header of malloc/free prototypes
[android-x86/hardware-liblights.git] / lights.c
1 /*
2  * Copyright (C) 2012 Stefan Seidel
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #define LOG_TAG "lights"
18
19 #include <cutils/log.h>
20
21 #include <sys/types.h>
22 #include <dirent.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <pthread.h>
30
31 #include <hardware/lights.h>
32 #include <cutils/properties.h>
33
34 /******************************************************************************/
35
36 static pthread_once_t g_init = PTHREAD_ONCE_INIT;
37 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
38 static int max_brightness = 255;
39 static char brightness_file[PROPERTY_VALUE_MAX] = { '\0' };
40
41 char const*const LLP_BRIGHTNESS_FILE     = "backlight.brightness_file";
42 char const*const LLP_MAX_BRIGHTNESS_FILE = "backlight.max_brightness_file";
43 char const*const LLP_MAX_BRIGHTNESS      = "backlight.max_brightness";
44
45 void init_globals(void)
46 {
47     pthread_mutex_init(&g_lock, NULL);
48 }
49
50 static int write_int(char* path, int value)
51 {
52     int fd;
53     static int already_warned = 0;
54
55     fd = open(path, O_RDWR);
56     if (fd >= 0) {
57         char buffer[20];
58         int bytes = sprintf(buffer, "%d\n", value);
59         int amt = write(fd, buffer, bytes);
60         close(fd);
61         return amt == -1 ? -errno : 0;
62     } else {
63         if (already_warned == 0) {
64             ALOGE("write_int failed to open %s\n", path);
65             already_warned = 1;
66         }
67         return -errno;
68     }
69 }
70
71 static int read_int(char* path)
72 {
73     int fd;
74
75     fd = open(path, O_RDONLY);
76     if (fd >= 0) {
77         char buffer[20];
78         int amt = read(fd, buffer, 20);
79         close(fd);
80         if (amt <= 0) return -errno;
81         int ret = -1;
82         amt = sscanf(buffer, "%d", &ret);
83         return amt == -1 ? -errno : ret;
84     }
85     return -errno;
86 }
87
88 static int rgb_to_brightness16(struct light_state_t const* state)
89 {
90     int color = state->color & 0x00ffffff;
91     return ((77*((color>>16)&0x00ff)) + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff)));
92 }
93
94 static int set_light_backlight(struct light_device_t* dev __attribute__ ((__unused__)), struct light_state_t const* state)
95 {
96     int err = 0;
97     int brightness = rgb_to_brightness16(state);
98
99     if (max_brightness < 65536)
100         brightness = brightness / (65536 / (max_brightness + 1));
101     else
102         brightness = brightness * (max_brightness / 65536);
103
104     pthread_mutex_lock(&g_lock);
105     err = write_int(brightness_file, (brightness));
106     pthread_mutex_unlock(&g_lock);
107
108     return err;
109 }
110
111 static int close_lights(struct light_device_t *dev)
112 {
113     free(dev);
114     return 0;
115 }
116
117 static int check_backlight_file(char const* name, char* path)
118 {
119     int ret;
120     if (access(name, F_OK)) {
121         ret = 0;
122     } else {
123         ALOGD("Detect %s", name);
124         strcpy(path, name);
125         ret = 1; // found
126     }
127     return ret;
128 }
129
130 static int find_backlight_file(char const* file, char* path)
131 {
132     int ret = 0;
133     size_t i;
134     DIR* dir;
135     char name[PATH_MAX];
136     const char* dirname = "/sys/class/backlight";
137     const char* dirs[] = {
138         "eeepc",
139         "intel_backlight",
140         "thinkpad_screen",
141         "acpi_video0",
142     };
143
144     // Check some known dirs
145     for (i = 0; i < sizeof(dirs) / sizeof(const char*); ++i) {
146         snprintf(name, PATH_MAX, "%s/%s/%s", dirname, dirs[i], file);
147         if (check_backlight_file(name, path)) {
148             ALOGV("Using device %s", name);
149             return 1;
150         }
151     }
152
153     if ((dir = opendir(dirname))) {
154         struct dirent* de;
155         while ((de = readdir(dir))) {
156             if (de->d_name[0] != '.') {
157                 snprintf(name, PATH_MAX, "%s/%s/%s", dirname, de->d_name, file);
158                 if ((ret = check_backlight_file(name, path))) {
159                     break;
160                 }
161             }
162         }
163         closedir(dir);
164     }
165     return ret;
166 }
167
168 static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device)
169 {
170     int (*set_light)(struct light_device_t* dev,
171             struct light_state_t const* state);
172
173     if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
174         set_light = set_light_backlight;
175         char max_b_file[PROPERTY_VALUE_MAX] = { '\0' };
176         if (property_get(LLP_MAX_BRIGHTNESS, max_b_file, NULL)) {
177             if (!sscanf(max_b_file, "%d", &max_brightness)) {
178                 ALOGE("%s system property is set to '%s', this could not be parsed as an integer!", LLP_MAX_BRIGHTNESS, max_b_file);
179                 return -EINVAL;
180             }
181         } else {
182             if (property_get(LLP_MAX_BRIGHTNESS_FILE, max_b_file, NULL) ||
183                     find_backlight_file("max_brightness", max_b_file)) {
184                 max_brightness = read_int(max_b_file);
185             } else {
186                 ALOGE("Unable to detect max_brightness. Try to set %s", LLP_MAX_BRIGHTNESS_FILE);
187                 return -EINVAL;
188             }
189         }
190         ALOGV("Read max display brightness of %d", max_brightness);
191         if (max_brightness < 1) {
192             max_brightness = 255;
193         }
194         if (!property_get(LLP_BRIGHTNESS_FILE, brightness_file, NULL) &&
195                 !find_backlight_file("brightness", brightness_file)) {
196             ALOGE("%s system property not set", LLP_BRIGHTNESS_FILE);
197             return -EINVAL;
198         }
199     } else {
200         return -EINVAL;
201     }
202
203     pthread_once(&g_init, init_globals);
204
205     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
206     memset(dev, 0, sizeof(*dev));
207
208     dev->common.tag = HARDWARE_DEVICE_TAG;
209     dev->common.close = (int (*)(struct hw_device_t*))close_lights;
210     dev->common.module = (struct hw_module_t*)module;
211     dev->common.version = 0;
212     dev->set_light = set_light;
213
214     *device = (struct hw_device_t*)dev;
215     return 0;
216 }
217
218
219 static struct hw_module_methods_t lights_module_methods = {
220     .open = open_lights,
221 };
222
223 struct hw_module_t HAL_MODULE_INFO_SYM = {
224     .tag = HARDWARE_MODULE_TAG,
225     .version_major = 1,
226     .version_minor = 0,
227     .id = LIGHTS_HARDWARE_MODULE_ID,
228     .name = "Generic sysfs liblights implementation",
229     .author = "Stefan Seidel",
230     .methods = &lights_module_methods,
231 };