OSDN Git Service

update to new liblights by Stefan Seidel
[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 <stdint.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <pthread.h>
27
28 #include <hardware/lights.h>
29 #include <cutils/properties.h>
30
31 /******************************************************************************/
32
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' };
37
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";
41
42 void init_globals(void)
43 {
44     pthread_mutex_init(&g_lock, NULL);
45 }
46
47 static int write_int(char* path, int value)
48 {
49     int fd;
50     static int already_warned = 0;
51
52     fd = open(path, O_RDWR);
53     if (fd >= 0) {
54         char buffer[20];
55         int bytes = sprintf(buffer, "%d\n", value);
56         int amt = write(fd, buffer, bytes);
57         close(fd);
58         return amt == -1 ? -errno : 0;
59     } else {
60         if (already_warned == 0) {
61             LOGE("write_int failed to open %s\n", path);
62             already_warned = 1;
63         }
64         return -errno;
65     }
66 }
67
68 static int read_int(char* path)
69 {
70     int fd;
71
72     fd = open(path, O_RDONLY);
73     if (fd >= 0) {
74         char buffer[3];
75         int amt = read(fd, buffer, 3);
76         close(fd);
77         if (amt <= 0) return -errno;
78         int ret = -1;
79         amt = sscanf(buffer, "%d", &ret);
80         return amt == -1 ? -errno : ret;
81     }
82     return -errno;
83 }
84
85 static int rgb_to_brightness16(struct light_state_t const* state)
86 {
87     int color = state->color & 0x00ffffff;
88     return ((77*((color>>16)&0x00ff)) + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff)));
89 }
90
91 static int set_light_backlight(struct light_device_t* dev, struct light_state_t const* state)
92 {
93     int err = 0;
94     int brightness = rgb_to_brightness16(state) / (65536 / (max_brightness + 1));
95     LOGV("Setting display brightness to %d", brightness);
96
97     pthread_mutex_lock(&g_lock);
98     err = write_int(brightness_file, (brightness));
99     pthread_mutex_unlock(&g_lock);
100
101     return err;
102 }
103
104 static int close_lights(struct light_device_t *dev)
105 {
106     free(dev);
107     return 0;
108 }
109
110
111 static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device)
112 {
113     int (*set_light)(struct light_device_t* dev,
114             struct light_state_t const* state);
115
116     if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
117         set_light = set_light_backlight;
118         char max_b_file[PROPERTY_VALUE_MAX] = { '\0' };
119         if (property_get(LLP_MAX_BRIGHTNESS, max_b_file, NULL)) {
120             if (!sscanf(max_b_file, "%d", &max_brightness)) {
121                 LOGE("%s system property is set to '%s', this could not be parsed as an integer!", LLP_MAX_BRIGHTNESS, max_b_file);
122                 return -EINVAL;
123             }
124         } else {
125             if (property_get(LLP_MAX_BRIGHTNESS_FILE, max_b_file, NULL)) {
126                 max_brightness = read_int(max_b_file);
127             } else {
128                 LOGE("%s system property not set", LLP_MAX_BRIGHTNESS_FILE);
129                 return -EINVAL;
130             }
131         }
132         LOGV("Read max display brightness of %d", max_brightness);
133         if (max_brightness < 1) {
134             max_brightness = 255;
135         }
136         if (!property_get(LLP_BRIGHTNESS_FILE, brightness_file, NULL)) {
137             LOGE("%s system property not set", LLP_BRIGHTNESS_FILE);
138             return -EINVAL;
139         }
140     } else {
141         return -EINVAL;
142     }
143
144     pthread_once(&g_init, init_globals);
145
146     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
147     memset(dev, 0, sizeof(*dev));
148
149     dev->common.tag = HARDWARE_DEVICE_TAG;
150     dev->common.close = (int (*)(struct hw_device_t*))close_lights;
151     dev->common.module = (struct hw_module_t*)module;
152     dev->common.version = 0;
153     dev->set_light = set_light;
154
155     *device = (struct hw_device_t*)dev;
156     return 0;
157 }
158
159
160 static struct hw_module_methods_t lights_module_methods = {
161     .open = open_lights,
162 };
163
164 const struct hw_module_t HAL_MODULE_INFO_SYM = {
165     .tag = HARDWARE_MODULE_TAG,
166     .version_major = 1,
167     .version_minor = 0,
168     .id = LIGHTS_HARDWARE_MODULE_ID,
169     .name = "Generic sysfs liblights implementation",
170     .author = "Stefan Seidel",
171     .methods = &lights_module_methods,
172 };