OSDN Git Service

include the header of malloc/free prototypes
[android-x86/hardware-liblights.git] / lights.c
index f8940b3..09ab603 100644 (file)
--- a/lights.c
+++ b/lights.c
 
 #include <cutils/log.h>
 
+#include <sys/types.h>
+#include <dirent.h>
 #include <stdint.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
@@ -39,9 +42,6 @@ char const*const LLP_BRIGHTNESS_FILE     = "backlight.brightness_file";
 char const*const LLP_MAX_BRIGHTNESS_FILE = "backlight.max_brightness_file";
 char const*const LLP_MAX_BRIGHTNESS      = "backlight.max_brightness";
 
-char const*const DEF_LLP_BRIGHTNESS_FILE     = "/sys/class/backlight/acpi_video0/brightness";
-char const*const DEF_LLP_MAX_BRIGHTNESS_FILE = "/sys/class/backlight/acpi_video0/max_brightness";
-
 void init_globals(void)
 {
     pthread_mutex_init(&g_lock, NULL);
@@ -74,8 +74,8 @@ static int read_int(char* path)
 
     fd = open(path, O_RDONLY);
     if (fd >= 0) {
-        char buffer[3];
-        int amt = read(fd, buffer, 3);
+        char buffer[20];
+        int amt = read(fd, buffer, 20);
         close(fd);
         if (amt <= 0) return -errno;
         int ret = -1;
@@ -91,11 +91,15 @@ static int rgb_to_brightness16(struct light_state_t const* state)
     return ((77*((color>>16)&0x00ff)) + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff)));
 }
 
-static int set_light_backlight(struct light_device_t* dev, struct light_state_t const* state)
+static int set_light_backlight(struct light_device_t* dev __attribute__ ((__unused__)), struct light_state_t const* state)
 {
     int err = 0;
-    int brightness = rgb_to_brightness16(state) / (65536 / (max_brightness + 1));
-    ALOGV("Setting display brightness to %d", brightness);
+    int brightness = rgb_to_brightness16(state);
+
+    if (max_brightness < 65536)
+        brightness = brightness / (65536 / (max_brightness + 1));
+    else
+        brightness = brightness * (max_brightness / 65536);
 
     pthread_mutex_lock(&g_lock);
     err = write_int(brightness_file, (brightness));
@@ -110,6 +114,56 @@ static int close_lights(struct light_device_t *dev)
     return 0;
 }
 
+static int check_backlight_file(char const* name, char* path)
+{
+    int ret;
+    if (access(name, F_OK)) {
+        ret = 0;
+    } else {
+        ALOGD("Detect %s", name);
+        strcpy(path, name);
+        ret = 1; // found
+    }
+    return ret;
+}
+
+static int find_backlight_file(char const* file, char* path)
+{
+    int ret = 0;
+    size_t i;
+    DIR* dir;
+    char name[PATH_MAX];
+    const char* dirname = "/sys/class/backlight";
+    const char* dirs[] = {
+        "eeepc",
+        "intel_backlight",
+        "thinkpad_screen",
+        "acpi_video0",
+    };
+
+    // Check some known dirs
+    for (i = 0; i < sizeof(dirs) / sizeof(const char*); ++i) {
+        snprintf(name, PATH_MAX, "%s/%s/%s", dirname, dirs[i], file);
+        if (check_backlight_file(name, path)) {
+            ALOGV("Using device %s", name);
+            return 1;
+        }
+    }
+
+    if ((dir = opendir(dirname))) {
+        struct dirent* de;
+        while ((de = readdir(dir))) {
+            if (de->d_name[0] != '.') {
+                snprintf(name, PATH_MAX, "%s/%s/%s", dirname, de->d_name, file);
+                if ((ret = check_backlight_file(name, path))) {
+                    break;
+                }
+            }
+        }
+        closedir(dir);
+    }
+    return ret;
+}
 
 static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device)
 {
@@ -125,10 +179,11 @@ static int open_lights(const struct hw_module_t* module, char const* name, struc
                 return -EINVAL;
             }
         } else {
-            if (property_get(LLP_MAX_BRIGHTNESS_FILE, max_b_file, DEF_LLP_MAX_BRIGHTNESS_FILE)) {
+            if (property_get(LLP_MAX_BRIGHTNESS_FILE, max_b_file, NULL) ||
+                    find_backlight_file("max_brightness", max_b_file)) {
                 max_brightness = read_int(max_b_file);
             } else {
-                ALOGE("%s system property not set", LLP_MAX_BRIGHTNESS_FILE);
+                ALOGE("Unable to detect max_brightness. Try to set %s", LLP_MAX_BRIGHTNESS_FILE);
                 return -EINVAL;
             }
         }
@@ -136,7 +191,8 @@ static int open_lights(const struct hw_module_t* module, char const* name, struc
         if (max_brightness < 1) {
             max_brightness = 255;
         }
-        if (!property_get(LLP_BRIGHTNESS_FILE, brightness_file, DEF_LLP_BRIGHTNESS_FILE)) {
+        if (!property_get(LLP_BRIGHTNESS_FILE, brightness_file, NULL) &&
+                !find_backlight_file("brightness", brightness_file)) {
             ALOGE("%s system property not set", LLP_BRIGHTNESS_FILE);
             return -EINVAL;
         }