Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Aug 8th, 2010 | Syntax: None | Size: 5.98 KB | Hits: 30 | Expires: Never
Copy text to clipboard
  1. /*
  2.  * Copyright (C) 2008 The Android Open Source 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.  
  18. #define LOG_TAG "lights"
  19.  
  20. #include <cutils/log.h>
  21.  
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <pthread.h>
  28.  
  29. #include <sys/ioctl.h>
  30. #include <sys/types.h>
  31.  
  32. #include <hardware/lights.h>
  33.  
  34. /******************************************************************************/
  35.  
  36. static pthread_once_t g_init = PTHREAD_ONCE_INIT;
  37. static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
  38. static struct light_state_t g_notification;
  39. static struct light_state_t g_battery;
  40. static int g_backlight = 255;
  41.  
  42. char const*const LCD_FILE
  43.         = "/sys/class/leds/lcd-backlight/brightness";
  44.  
  45. char const*const KEYBOARD_FILE
  46.         = "/sys/class/leds/keyboard-backlight/brightness";
  47.  
  48. char const*const BUTTON_FILE
  49.         = "/sys/class/leds/button-backlight/brightness";
  50.                
  51. char const*const MAIN_FILE
  52.                 = "/sys/class/leds/main/brightness";
  53.  
  54. /**
  55.  * device methods
  56.  */
  57.  
  58. void init_globals(void)
  59. {
  60.     // init the mutex
  61.     pthread_mutex_init(&g_lock, NULL);
  62. }
  63.  
  64. static int
  65. write_int(char const* path, int value)
  66. {
  67.     int fd;
  68.     static int already_warned = 0;
  69.  
  70.     fd = open(path, O_RDWR);
  71.     if (fd >= 0) {
  72.         char buffer[20];
  73.         int bytes = sprintf(buffer, "%d\n", value);
  74.         int amt = write(fd, buffer, bytes);
  75.         close(fd);
  76.         return amt == -1 ? -errno : 0;
  77.     } else {
  78.         if (already_warned == 0) {
  79.             LOGE("write_int failed to open %s\n", path);
  80.             already_warned = 1;
  81.         }
  82.         return -errno;
  83.     }
  84. }
  85.  
  86. static int
  87. is_lit(struct light_state_t const* state)
  88. {
  89.     return state->color & 0x00ffffff;
  90. }
  91.  
  92. static int
  93. rgb_to_brightness(struct light_state_t const* state)
  94. {
  95.     int color = state->color & 0x00ffffff;
  96.     return ((77*((color>>16)&0x00ff))
  97.             + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
  98. }
  99.  
  100. static int
  101. set_light_backlight(struct light_device_t* dev,
  102.         struct light_state_t const* state)
  103. {
  104.     int err = 0;
  105.     int brightness = rgb_to_brightness(state);
  106.     pthread_mutex_lock(&g_lock);
  107.     g_backlight = brightness;
  108.     err = write_int(LCD_FILE, brightness);
  109.         LOGV("set_light_backlight color=0x%08x", state->color);
  110.     pthread_mutex_unlock(&g_lock);
  111.     return err;
  112. }
  113.  
  114. static int
  115. set_light_keyboard(struct light_device_t* dev,
  116.         struct light_state_t const* state)
  117. {
  118.     pthread_mutex_lock(&g_lock);
  119.         LOGV("set_light_keyboard color=0x%08x", state->color);
  120.     pthread_mutex_unlock(&g_lock);
  121.     return err;
  122. }
  123.  
  124. static int
  125. set_light_buttons(struct light_device_t* dev,
  126.         struct light_state_t const* state)
  127. {
  128.     pthread_mutex_lock(&g_lock);
  129.         LOGV("set_light_buttons color=0x%08x", state->color);
  130.     pthread_mutex_unlock(&g_lock);
  131.     return err;
  132. }
  133.  
  134. static int
  135. set_light_battery(struct light_device_t* dev,
  136.         struct light_state_t const* state)
  137. {
  138.     pthread_mutex_lock(&g_lock);
  139.     g_battery = *state;
  140.     LOGV("set_light_battery color=0x%08x", state->color);
  141.     pthread_mutex_unlock(&g_lock);
  142.     return 0;
  143. }
  144.  
  145. static int
  146. set_light_notifications(struct light_device_t* dev,
  147.         struct light_state_t const* state)
  148. {
  149.     pthread_mutex_lock(&g_lock);
  150.     g_notification = *state;
  151.     LOGV("set_light_notifications color=0x%08x", state->color);
  152.     handle_speaker_battery_locked(dev);
  153.     pthread_mutex_unlock(&g_lock);
  154.     return 0;
  155. }
  156.  
  157.  
  158. /** Close the lights device */
  159. static int
  160. close_lights(struct light_device_t *dev)
  161. {
  162.     if (dev) {
  163.         free(dev);
  164.     }
  165.     return 0;
  166. }
  167.  
  168.  
  169. /******************************************************************************/
  170.  
  171. /**
  172.  * module methods
  173.  */
  174.  
  175. /** Open a new instance of a lights device using name */
  176. static int open_lights(const struct hw_module_t* module, char const* name,
  177.         struct hw_device_t** device)
  178. {
  179.     int (*set_light)(struct light_device_t* dev, struct light_state_t const* state);
  180.  
  181.     if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
  182.         set_light = set_light_backlight;
  183.     }
  184.     else if (0 == strcmp(LIGHT_ID_KEYBOARD, name)) {
  185.         set_light = set_light_keyboard;
  186.     }
  187.     else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
  188.         set_light = set_light_buttons;
  189.     }
  190.     else if (0 == strcmp(LIGHT_ID_BATTERY, name)) {
  191.         set_light = set_light_battery;
  192.     }
  193.     else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) {
  194.         set_light = set_light_notifications;
  195.     }
  196.     else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) {
  197.         set_light = set_light_attention;
  198.     }
  199.     else {
  200.         return -EINVAL;
  201.     }
  202.  
  203.     pthread_once(&g_init, init_globals);
  204.  
  205.     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
  206.     memset(dev, 0, sizeof(*dev));
  207.  
  208.     dev->common.tag = HARDWARE_DEVICE_TAG;
  209.     dev->common.version = 0;
  210.     dev->common.module = (struct hw_module_t*)module;
  211.     dev->common.close = (int (*)(struct hw_device_t*))close_lights;
  212.     dev->set_light = set_light;
  213.  
  214.     *device = (struct hw_device_t*)dev;
  215.     return 0;
  216. }
  217.  
  218.  
  219. static struct hw_module_methods_t lights_module_methods = {
  220.     .open =  open_lights,
  221. };
  222.  
  223. /*
  224.  * The lights Module
  225.  */
  226. const struct hw_module_t HAL_MODULE_INFO_SYM = {
  227.     .tag = HARDWARE_MODULE_TAG,
  228.     .version_major = 1,
  229.     .version_minor = 0,
  230.     .id = LIGHTS_HARDWARE_MODULE_ID,
  231.     .name = "QCT MSM7K lights Module",
  232.     .author = "Google, Inc.",
  233.     .methods = &lights_module_methods,
  234. };