liblights: properly working liblights

This commit is contained in:
Mister Oyster 2018-01-13 17:36:16 +01:00
parent c63c30b715
commit 9e61f1a611
1 changed files with 139 additions and 94 deletions

View File

@ -1,7 +1,7 @@
/*
* Copyright (C) 2008 The Android Open Source Project
* Copyright (C) 2017 The M.A.D. Team
* Fixed for M2 Mini by Dinolek
* Ported to M2 Note by Moyster
* Copyright (C) 2018 MoYsTeR
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,18 +17,18 @@
*/
#define LOG_TAG "Lights"
#define LOG_TAG "lights"
#include <cutils/log.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
@ -37,11 +37,21 @@
static pthread_once_t g_init = PTHREAD_ONCE_INIT;
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
static struct light_state_t g_attention;
static int g_attention;
static struct light_state_t g_battery;
static struct light_state_t g_notification;
static int g_backlight = 255;
/* MEIZU LED */
char const *const MEIZU_LED_FILE = "/sys/class/leds/button-backlight/brightness";
char const *const MEIZU_TRIGGER_FILE = "/sys/class/leds/button-backlight/trigger";
char const *const MEIZU_DELAY_ON_FILE = "/sys/class/leds/button-backlight/delay_on";
char const *const MEIZU_DELAY_OFF_FILE = "/sys/class/leds/button-backlight/delay_off";
/* BACKLIGHT */
char const *const LCD_FILE = "/sys/class/leds/lcd-backlight/brightness";
char const *const LEDS_FILE = "/sys/class/leds/button-backlight/trigger";
/* device methods */
void init_globals(void)
@ -53,7 +63,6 @@ void init_globals(void)
static int write_int(char const *path, int value)
{
int fd;
static int already_warned;
fd = open(path, O_RDWR);
if (fd >= 0) {
@ -64,10 +73,6 @@ static int write_int(char const *path, int value)
close(fd);
return amt == -1 ? -errno : 0;
} else {
if (already_warned == 0) {
ALOGE("Write_int failed to open %s\n", path);
already_warned = 1;
}
return -errno;
}
}
@ -94,6 +99,39 @@ static int is_lit(struct light_state_t const *state)
return state->color & 0x00ffffff;
}
static int meizu_blink(int level, int onMS, int offMS)
{
static int preStatus; /* 0: off, 1: blink, 2: no blink */
int nowStatus;
int i = 0;
if (level == 0)
nowStatus = 0;
else if (onMS && offMS)
nowStatus = 1;
else
nowStatus = 2;
if (preStatus == nowStatus)
return -1;
if (nowStatus == 0)
write_int(MEIZU_LED_FILE, 0);
else if (nowStatus == 1) {
write_str(MEIZU_TRIGGER_FILE, "timer");
while (((access(MEIZU_DELAY_OFF_FILE, F_OK) == -1) ||
(access(MEIZU_DELAY_OFF_FILE, R_OK|W_OK) == -1)) && i < 10) {
i++;
}
write_int(MEIZU_DELAY_OFF_FILE, offMS);
write_int(MEIZU_DELAY_ON_FILE, onMS);
} else {
write_str(MEIZU_TRIGGER_FILE, "none");
write_int(MEIZU_LED_FILE, 255); /* default full brightness */
}
preStatus = nowStatus;
return 0;
}
static int rgb_to_brightness(struct light_state_t const *state)
{
@ -103,89 +141,12 @@ static int rgb_to_brightness(struct light_state_t const *state)
+ (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
}
static int set_speaker_light_locked(struct light_device_t *dev,
struct light_state_t const *state)
{
if (!dev)
return -1;
if (state) {
write_str(LEDS_FILE, "timer");
}
return 0;
}
static int unset_speaker_light_locked(struct light_device_t *dev)
{
if (!dev)
return -1;
write_str(LEDS_FILE, "none");
return 0;
}
static void handle_speaker_light_locked(struct light_device_t *dev)
{
if (is_lit(&g_attention))
set_speaker_light_locked(dev, &g_attention);
else if (is_lit(&g_notification))
set_speaker_light_locked(dev, &g_notification);
else
unset_speaker_light_locked(dev);
}
static int set_light_notifications(struct light_device_t *dev,
struct light_state_t const *state)
{
pthread_mutex_lock(&g_lock);
unsigned int brightness;
unsigned int color;
unsigned int rgb[3];
g_notification = *state;
/* If a brightness has been applied by the user */
brightness = (g_notification.color & 0xFF000000) >> 24;
if (brightness > 0 && brightness < 0xFF) {
/* Retrieve each of the RGB colors */
color = g_notification.color & 0x00FFFFFF;
rgb[0] = (color >> 16) & 0xFF;
rgb[1] = (color >> 8) & 0xFF;
rgb[2] = color & 0xFF;
/* Apply the brightness level */
if (rgb[0] > 0)
rgb[0] = (rgb[0] * brightness) / 0xFF;
if (rgb[1] > 0)
rgb[1] = (rgb[1] * brightness) / 0xFF;
if (rgb[2] > 0)
rgb[2] = (rgb[2] * brightness) / 0xFF;
/* Update with the new color */
g_notification.color = (rgb[0] << 16) + (rgb[1] << 8) + rgb[2];
}
handle_speaker_light_locked(dev);
pthread_mutex_unlock(&g_lock);
return 0;
}
static int set_light_attention(struct light_device_t *dev,
struct light_state_t const *state)
{
pthread_mutex_lock(&g_lock);
g_attention = *state;
handle_speaker_light_locked(dev);
pthread_mutex_unlock(&g_lock);
return 0;
}
static int set_light_backlight(struct light_device_t *dev,
struct light_state_t const *state)
struct light_state_t const *state)
{
if (!dev) {
if (!dev)
return -1;
}
int err = 0;
int brightness = rgb_to_brightness(state);
@ -195,6 +156,90 @@ static int set_light_backlight(struct light_device_t *dev,
return err;
}
static int set_speaker_light_locked(struct light_device_t *dev,
struct light_state_t const *state)
{
int len;
int alpha, red, green;
int onMS, offMS;
unsigned int colorRGB;
switch (state->flashMode) {
case LIGHT_FLASH_TIMED:
onMS = state->flashOnMS;
offMS = state->flashOffMS;
break;
case LIGHT_FLASH_NONE:
default:
onMS = 0;
offMS = 0;
break;
}
colorRGB = state->color;
alpha = (colorRGB >> 24) & 0xFF;
if (alpha) {
red = (colorRGB >> 16) & 0xFF;
green = (colorRGB >> 8) & 0xFF;
} else { /* alpha = 0 means turn the LED off */
red = green = 0;
}
if (red)
meizu_blink(red, onMS, offMS);
else if (green)
meizu_blink(green, onMS, offMS);
else
meizu_blink(0, 0, 0);
return 0;
}
static void handle_speaker_battery_locked(struct light_device_t *dev)
{
if (is_lit(&g_battery)) {
set_speaker_light_locked(dev, &g_battery);
} else {
set_speaker_light_locked(dev, &g_battery); /* Turkey workaround: notification and Low battery case, IPO bootup, NLED cannot blink */
set_speaker_light_locked(dev, &g_notification);
}
}
static int set_light_battery(struct light_device_t *dev,
struct light_state_t const *state)
{
pthread_mutex_lock(&g_lock);
g_battery = *state;
handle_speaker_battery_locked(dev);
pthread_mutex_unlock(&g_lock);
return 0;
}
static int set_light_notifications(struct light_device_t *dev,
struct light_state_t const *state)
{
pthread_mutex_lock(&g_lock);
g_notification = *state;
handle_speaker_battery_locked(dev);
pthread_mutex_unlock(&g_lock);
return 0;
}
static int set_light_attention(struct light_device_t *dev,
struct light_state_t const *state)
{
pthread_mutex_lock(&g_lock);
if (state->flashMode == LIGHT_FLASH_HARDWARE)
g_attention = state->flashOnMS;
else if (state->flashMode == LIGHT_FLASH_NONE)
g_attention = 0;
pthread_mutex_unlock(&g_lock);
return 0;
}
/* Close the lights device */
static int close_lights(struct light_device_t *dev)
{
@ -251,6 +296,6 @@ struct hw_module_t HAL_MODULE_INFO_SYM = {
.version_minor = 0,
.id = LIGHTS_HARDWARE_MODULE_ID,
.name = "Meizu Lights Module",
.author = "Mediatek",
.author = "MediaTek",
.methods = &lights_module_methods,
};