Arnab Chaudhuri | a958e7e | 2016-05-07 14:44:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The CyanogenMod Project |
| 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 | |
| 27 | #include <sys/ioctl.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #include <hardware/lights.h> |
| 31 | #include <hardware/hardware.h> |
| 32 | |
| 33 | static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; |
| 34 | |
| 35 | /** Paths to light files **/ |
| 36 | char const *const BACKLIGHT_FILE = "/sys/class/backlight/panel/brightness"; |
| 37 | char const *const BUTTON_FILE = "/sys/class/sec/sec_touchkey/brightness"; |
| 38 | char const *const NOTIFICATION_FILE = "/sys/class/misc/backlightnotification/notification_led"; |
| 39 | |
| 40 | /** Write integer to file **/ |
| 41 | static int write_int(char const *path, int value) |
| 42 | { |
| 43 | int fd; |
| 44 | static int already_warned = -1; |
| 45 | fd = open(path, O_RDWR); |
| 46 | if (fd >= 0) { |
| 47 | char buffer[20]; |
| 48 | int bytes = sprintf(buffer, "%d\n", value); |
| 49 | int amt = write(fd, buffer, bytes); |
| 50 | close(fd); |
| 51 | return amt == -1 ? -errno : 0; |
| 52 | } else { |
| 53 | if (already_warned == -1) { |
| 54 | ALOGE("write_int failed to open %s\n", path); |
| 55 | already_warned = 1; |
| 56 | } |
| 57 | return -errno; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** Convert RGB to 0-255 integer **/ |
| 62 | static int rgb_to_brightness(struct light_state_t const *state) |
| 63 | { |
| 64 | int color = state->color & 0x00ffffff; |
| 65 | return ((77 * ((color >> 16) & 0x00ff)) |
| 66 | + (150 * ((color >> 8) & 0x00ff)) + |
| 67 | (29 * (color & 0x00ff))) >> 8; |
| 68 | } |
| 69 | |
| 70 | /** Is light already enabled? **/ |
| 71 | static int is_lit(struct light_state_t const* state) |
| 72 | { |
| 73 | return state->color & 0x00ffffff; |
| 74 | } |
| 75 | |
| 76 | /** Set LCD backlight **/ |
| 77 | static int set_light_backlight(struct light_device_t *dev, struct light_state_t const *state) |
| 78 | { |
| 79 | pthread_mutex_lock(&g_lock); |
| 80 | int err = write_int(BACKLIGHT_FILE, rgb_to_brightness(state)); |
| 81 | pthread_mutex_unlock(&g_lock); |
| 82 | return err; |
| 83 | } |
| 84 | |
| 85 | /** Set buttons backlight **/ |
| 86 | static int set_light_buttons(struct light_device_t* dev, struct light_state_t const* state) |
| 87 | { |
| 88 | int err = 0; |
| 89 | pthread_mutex_lock (&g_lock); |
| 90 | if(is_lit(state)) |
| 91 | err = write_int(BUTTON_FILE, 1); |
| 92 | else |
| 93 | err = write_int(BUTTON_FILE, 0); |
| 94 | pthread_mutex_unlock (&g_lock); |
| 95 | return err; |
| 96 | } |
| 97 | |
| 98 | /** Set buttons backlight as BLN **/ |
| 99 | static int set_light_notifications(struct light_device_t* dev, struct light_state_t const* state) |
| 100 | { |
| 101 | int err = 0; |
| 102 | pthread_mutex_lock (&g_lock); |
| 103 | if(is_lit(state)) |
| 104 | err = write_int(NOTIFICATION_FILE, 1); |
| 105 | else |
| 106 | err = write_int(NOTIFICATION_FILE, 0); |
| 107 | pthread_mutex_unlock (&g_lock); |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | /** Close the lights device **/ |
| 112 | static int close_lights(struct light_device_t *dev) |
| 113 | { |
| 114 | if (dev) |
| 115 | free(dev); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /** Open a new instance of a lights device using name **/ |
| 120 | static int open_lights(const struct hw_module_t *module, |
| 121 | char const *name, struct hw_device_t **device) |
| 122 | { |
| 123 | pthread_t lighting_poll_thread; |
| 124 | |
| 125 | int (*set_light) (struct light_device_t *dev, |
| 126 | struct light_state_t const *state); |
| 127 | |
| 128 | if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) |
| 129 | set_light = set_light_backlight; |
| 130 | else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) |
| 131 | set_light = set_light_buttons; |
| 132 | else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) |
| 133 | set_light = set_light_notifications; |
| 134 | else |
| 135 | return -EINVAL; |
| 136 | |
| 137 | pthread_mutex_init(&g_lock, NULL); |
| 138 | |
| 139 | struct light_device_t *dev = malloc(sizeof(struct light_device_t)); |
| 140 | memset(dev, 0, sizeof(*dev)); |
| 141 | |
| 142 | dev->common.tag = HARDWARE_DEVICE_TAG; |
| 143 | dev->common.version = 0; |
| 144 | dev->common.module = (struct hw_module_t *)module; |
| 145 | dev->common.close = (int (*)(struct hw_device_t *))close_lights; |
| 146 | dev->set_light = set_light; |
| 147 | |
| 148 | *device = (struct hw_device_t *)dev; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /** Method list **/ |
| 154 | static struct hw_module_methods_t lights_methods = |
| 155 | { |
| 156 | .open = open_lights, |
| 157 | }; |
| 158 | |
| 159 | /** The backlight module **/ |
| 160 | |
| 161 | struct hw_module_t HAL_MODULE_INFO_SYM = { |
| 162 | .tag = HARDWARE_MODULE_TAG, |
| 163 | .version_major = 1, |
| 164 | .version_minor = 0, |
| 165 | .id = LIGHTS_HARDWARE_MODULE_ID, |
| 166 | .name = "Lights module", |
| 167 | .author = "The CyanogenMod Project", |
| 168 | .methods = &lights_methods, |
| 169 | }; |