#define LOG_TAG "brightnessbtn"
#include <sys/stat.h>
#include <poll.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <cutils/log.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <cutils/properties.h>
const int MAX_POWERBTNS = 3; // убрал бы нафиг но дальше зачем то нужна)
int openfds(struct pollfd pfds[]) // функция открытия девайся? о_О
{
int cnt = 0;
const char *dirname = "/dev/input";
DIR *dir;
if ((dir = opendir(dirname))) {
int fd;
struct dirent *de;
while ((de = readdir(dir))) {
if (de->d_name[0] != 'e') // eventX
continue;
char name[PATH_MAX];
snprintf(name, PATH_MAX, "%s/%s", dirname, de->d_name);
fd = open(name, O_RDWR | O_NONBLOCK);
if (fd < 0) {
LOGE("could not open %s, %s", name, strerror(errno));
continue;
}
name[sizeof(name) - 1] = '\0';
if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
LOGE("could not get device name for %s, %s\n", name, strerror(errno));
name[0] = '\0';
}
// TODO: parse /etc/excluded-input-devices.xml
if (!strcmp(name, "nvec keyboard")) { //я так понял что он сам по имени выбирает путь девайса =)
LOGI("open %s(%s) ok", de->d_name, name);
pfds[cnt].events = POLLIN;
pfds[cnt++].fd = fd;
if (cnt < MAX_POWERBTNS)
continue;
else
break;
}
close(fd);
}
closedir(dir);
}
return cnt;
}
void change_brightness(int up_down){
int brightness
if (up_down){
brightness = 100;
write_int("/sys/class/backlight/pwm-backlight/brightness", brightness);
} else {
brightness = 30;
write_int("/sys/class/backlight/pwm-backlight/brightness", brightness);
}
}
int main()
{
struct pollfd pfds[MAX_POWERBTNS];
int cnt = openfds(pfds);
int timeout = -1;
char prop[PROPERTY_VALUE_MAX];
int ufd = open("/dev/uinput", O_WRONLY | O_NDELAY);
if (ufd >= 0) {
struct uinput_user_dev ud;
memset(&ud, 0, sizeof(ud));
strcpy(ud.name, "Android Power Button");
write(ufd, &ud, sizeof(ud));
ioctl(ufd, UI_SET_EVBIT, EV_KEY);
ioctl(ufd, UI_SET_KEYBIT, KEY_POWER);
ioctl(ufd, UI_DEV_CREATE, 0);
} else {
LOGE("could not open uinput device: %s", strerror(errno));
return -1;
}
for (;;) {
int i;
int pollres = poll(pfds, cnt) ;
LOGV("pollres=%d %d\n", pollres);
if (pollres < 0) {
LOGE("poll error: %s", strerror(errno));
break;
}
for (i = 0; i < cnt; ++i) {
if (pfds[i].revents & POLLIN) {
struct input_event iev;
size_t res = read(pfds[i].fd, &iev, sizeof(iev));
if (res < sizeof(iev)) {
LOGW("insufficient input data(%d)? fd=%d", res, pfds[i].fd);
continue;
}
LOGV("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, pfds[i].fd);
if (iev.type == EV_KEY && (iev.code == KEY_BRIGHTNESS_DOWN || iev.code == KEY_BRIGHTNESS_UP) && !iev.value) {
if (iev.code == KEY_BRIGHTNESS_DOWN) {
change_brightness(1);
} else {
change_brightness(0)
}
}
}
}
}
return 0;
}