liblight: Add brightness level extension support

Add brightness level extension API to set backlight
through display API. The feature is enabled by property.

Change-Id: Ia6911433705cc946ed73ffc820396c350e3c8b88
diff --git a/liblight/Android.mk b/liblight/Android.mk
index ff4825b..aa09187 100644
--- a/liblight/Android.mk
+++ b/liblight/Android.mk
@@ -17,9 +17,12 @@
 # hw/<COPYPIX_HARDWARE_MODULE_ID>.<ro.board.platform>.so
 include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES := lights.c
+LOCAL_C_INCLUDES += $(TARGET_OUT_HEADERS)/common/inc
+LOCAL_C_INCLUDES += $(TARGET_OUT_HEADERS)/qdcm/inc
+
+LOCAL_SRC_FILES := lights.c lights_prv.cpp
 LOCAL_MODULE_RELATIVE_PATH := hw
-LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_SHARED_LIBRARIES := liblog libcutils libsdm-disp-apis
 LOCAL_CFLAGS := -DLOG_TAG=\"qdlights\"
 LOCAL_CLANG  := true
 LOCAL_MODULE := lights.$(TARGET_BOARD_PLATFORM)
diff --git a/liblight/lights.c b/liblight/lights.c
index 89c27cd..d63cc67 100644
--- a/liblight/lights.c
+++ b/liblight/lights.c
@@ -20,7 +20,7 @@
 // #define LOG_NDEBUG 0
 
 #include <cutils/log.h>
-
+#include <cutils/properties.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
@@ -33,6 +33,7 @@
 #include <sys/types.h>
 
 #include <hardware/lights.h>
+#include "lights_prv.h"
 
 #ifndef DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS
 #define DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS 0x80
@@ -46,6 +47,7 @@
 static struct light_state_t g_battery;
 static int g_last_backlight_mode = BRIGHTNESS_MODE_USER;
 static int g_attention = 0;
+static int g_brightness_max = 0;
 
 char const*const RED_LED_FILE
         = "/sys/class/leds/red/brightness";
@@ -157,6 +159,28 @@
 }
 
 static int
+set_light_backlight_ext(struct light_device_t* dev,
+        struct light_state_t const* state)
+{
+    int err = 0;
+
+    if(!dev) {
+        return -1;
+    }
+
+    int brightness = state->color & 0x00ffffff;
+    pthread_mutex_lock(&g_lock);
+
+    if (brightness >= 0 && brightness <= g_brightness_max) {
+        set_brightness_ext_level(brightness);
+    }
+
+    pthread_mutex_unlock(&g_lock);
+
+    return err;
+}
+
+static int
 set_speaker_light_locked(struct light_device_t* dev,
         struct light_state_t const* state)
 {
@@ -211,15 +235,15 @@
         if (red) {
             if (write_int(RED_BLINK_FILE, blink))
                 write_int(RED_LED_FILE, 0);
-	}
+    }
         if (green) {
             if (write_int(GREEN_BLINK_FILE, blink))
                 write_int(GREEN_LED_FILE, 0);
-	}
+    }
         if (blue) {
             if (write_int(BLUE_BLINK_FILE, blink))
                 write_int(BLUE_LED_FILE, 0);
-	}
+    }
     } else {
         write_int(RED_LED_FILE, red);
         write_int(GREEN_LED_FILE, green);
@@ -314,9 +338,19 @@
     int (*set_light)(struct light_device_t* dev,
             struct light_state_t const* state);
 
-    if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
-        set_light = set_light_backlight;
-    else if (0 == strcmp(LIGHT_ID_BATTERY, name))
+    if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
+        char property[PROPERTY_VALUE_MAX];
+        property_get("persist.extend.brightness", property, "0");
+
+        if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
+           !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
+            property_get("persist.display.max_brightness", property, "255");
+            g_brightness_max = atoi(property);
+            set_brightness_ext_init();
+            set_light = set_light_backlight_ext;
+        } else
+            set_light = set_light_backlight;
+    } else if (0 == strcmp(LIGHT_ID_BATTERY, name))
         set_light = set_light_battery;
     else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
         set_light = set_light_notifications;
diff --git a/liblight/lights_prv.cpp b/liblight/lights_prv.cpp
new file mode 100644
index 0000000..f916ed2
--- /dev/null
+++ b/liblight/lights_prv.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2017, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+
+#include <hardware/hwcomposer_defs.h>
+#include "disp_color_apis.h"
+#include "lights_prv.h"
+
+/******************************************************************************/
+static DISPAPI_HANDLE g_ctx;
+
+/**
+ * device methods
+ */
+
+void set_brightness_ext_init(void)
+{
+   disp_api_init((DISPAPI_HANDLE*) &g_ctx, 0);
+}
+
+int set_brightness_ext_level(int level)
+{
+    int err = disp_api_set_panel_brightness_level_ext(g_ctx, HWC_DISPLAY_PRIMARY,
+                                                 level, 0);
+
+    return err;
+}
diff --git a/liblight/lights_prv.h b/liblight/lights_prv.h
new file mode 100644
index 0000000..e0ec736
--- /dev/null
+++ b/liblight/lights_prv.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2017, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef LIGHTS_PRV_H
+#define LIGHTS_PRV_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern void set_brightness_ext_init(void);
+extern int set_brightness_ext_level(int level);
+
+#ifdef __cplusplus
+}
+#endif
+#endif