Guest User

Untitled

a guest
Aug 19th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. #define LOG_TAG "brightnessbtn"
  2.  
  3. #include <sys/stat.h>
  4. #include <poll.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <dirent.h>
  8. #include <cutils/log.h>
  9. #include <linux/input.h>
  10. #include <linux/uinput.h>
  11. #include <cutils/properties.h>
  12.  
  13. const int MAX_POWERBTNS = 3;  // убрал бы нафиг но дальше зачем то нужна)
  14.  
  15. int openfds(struct pollfd pfds[])  //  функция открытия девайся? о_О
  16. {
  17.     int cnt = 0;
  18.     const char *dirname = "/dev/input";
  19.     DIR *dir;
  20.     if ((dir = opendir(dirname))) {
  21.         int fd;
  22.         struct dirent *de;
  23.         while ((de = readdir(dir))) {
  24.             if (de->d_name[0] != 'e') // eventX
  25.                 continue;
  26.             char name[PATH_MAX];
  27.             snprintf(name, PATH_MAX, "%s/%s", dirname, de->d_name);
  28.             fd = open(name, O_RDWR | O_NONBLOCK);
  29.             if (fd < 0) {
  30.                 LOGE("could not open %s, %s", name, strerror(errno));
  31.                 continue;
  32.             }
  33.             name[sizeof(name) - 1] = '\0';
  34.             if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
  35.                 LOGE("could not get device name for %s, %s\n", name, strerror(errno));
  36.                 name[0] = '\0';
  37.             }
  38.  
  39.             // TODO: parse /etc/excluded-input-devices.xml
  40.             if (!strcmp(name, "nvec keyboard")) {    //я так понял что он сам по имени выбирает путь девайса =)
  41.                 LOGI("open %s(%s) ok", de->d_name, name);
  42.                 pfds[cnt].events = POLLIN;
  43.                 pfds[cnt++].fd = fd;
  44.                 if (cnt < MAX_POWERBTNS)
  45.                     continue;
  46.                 else
  47.                     break;
  48.             }
  49.             close(fd);
  50.         }
  51.         closedir(dir);
  52.     }
  53.  
  54.     return cnt;
  55. }
  56.  
  57. void change_brightness(int up_down){
  58.     int brightness
  59.  
  60.     if (up_down){
  61.         brightness = 100;
  62.         write_int("/sys/class/backlight/pwm-backlight/brightness", brightness);
  63.     } else {
  64.         brightness = 30;
  65.         write_int("/sys/class/backlight/pwm-backlight/brightness", brightness);
  66.     }
  67. }
  68.  
  69. int main()
  70. {
  71.     struct pollfd pfds[MAX_POWERBTNS];
  72.     int cnt = openfds(pfds);
  73.     int timeout = -1;
  74.     char prop[PROPERTY_VALUE_MAX];
  75.  
  76.     int ufd = open("/dev/uinput", O_WRONLY | O_NDELAY);
  77.     if (ufd >= 0) {
  78.         struct uinput_user_dev ud;
  79.         memset(&ud, 0, sizeof(ud));
  80.         strcpy(ud.name, "Android Power Button");
  81.         write(ufd, &ud, sizeof(ud));
  82.         ioctl(ufd, UI_SET_EVBIT, EV_KEY);
  83.         ioctl(ufd, UI_SET_KEYBIT, KEY_POWER);
  84.         ioctl(ufd, UI_DEV_CREATE, 0);
  85.     } else {
  86.         LOGE("could not open uinput device: %s", strerror(errno));
  87.         return -1;
  88.     }
  89.    
  90.     for (;;) {
  91.         int i;
  92.         int pollres = poll(pfds, cnt) ;
  93.         LOGV("pollres=%d %d\n", pollres);
  94.         if (pollres < 0) {
  95.             LOGE("poll error: %s", strerror(errno));
  96.             break;
  97.         }
  98.    
  99.         for (i = 0; i < cnt; ++i) {
  100.             if (pfds[i].revents & POLLIN) {
  101.                 struct input_event iev;
  102.                 size_t res = read(pfds[i].fd, &iev, sizeof(iev));
  103.                 if (res < sizeof(iev)) {
  104.                     LOGW("insufficient input data(%d)? fd=%d", res, pfds[i].fd);
  105.                     continue;
  106.                 }
  107.                 LOGV("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, pfds[i].fd);
  108.                 if (iev.type == EV_KEY && (iev.code == KEY_BRIGHTNESS_DOWN || iev.code == KEY_BRIGHTNESS_UP) && !iev.value) {
  109.                     if (iev.code == KEY_BRIGHTNESS_DOWN) {
  110.                         change_brightness(1);
  111.                     } else {
  112.                         change_brightness(0)
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment