2011-06-15 07:18:23 -07:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2011-10-29 11:45:43 -07:00
|
|
|
#include <Eeze.h>
|
2011-06-15 07:18:23 -07:00
|
|
|
|
2011-10-29 11:45:43 -07:00
|
|
|
/* local subsystem functions */
|
2011-06-15 07:18:23 -07:00
|
|
|
static int
|
2011-10-28 21:36:32 -07:00
|
|
|
_bl_write_file(const char *file, int val)
|
2011-06-15 07:18:23 -07:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
int fd = open(file, O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
perror("open");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-10 13:08:58 -08:00
|
|
|
snprintf(buf, sizeof(buf), "%d", val);
|
2011-06-15 07:18:23 -07:00
|
|
|
if (write(fd, buf, strlen(buf)) != (int)strlen(buf))
|
|
|
|
{
|
|
|
|
perror("write");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* externally accessible functions */
|
|
|
|
int
|
2011-07-23 10:46:25 -07:00
|
|
|
main(int argc, char **argv)
|
2011-06-15 07:18:23 -07:00
|
|
|
{
|
2012-09-16 03:43:17 -07:00
|
|
|
int i, level, devok = 0;
|
|
|
|
const char *f, *dev = NULL, *str;
|
2011-10-28 21:36:32 -07:00
|
|
|
int maxlevel = 0, curlevel = -1;
|
2012-09-16 03:43:17 -07:00
|
|
|
Eina_List *devs, *l;
|
2011-10-28 21:36:32 -07:00
|
|
|
char buf[4096] = "";
|
2012-06-20 23:19:43 -07:00
|
|
|
|
2011-06-15 07:18:23 -07:00
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if ((!strcmp(argv[i], "-h")) ||
|
|
|
|
(!strcmp(argv[i], "-help")) ||
|
|
|
|
(!strcmp(argv[i], "--help")))
|
|
|
|
{
|
2011-07-23 10:46:25 -07:00
|
|
|
printf("This is an internal tool for Enlightenment.\n"
|
|
|
|
"do not use it.\n");
|
2011-06-15 07:18:23 -07:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2012-09-16 03:43:17 -07:00
|
|
|
if (argc == 3)
|
|
|
|
{
|
|
|
|
level = atoi(argv[1]);
|
|
|
|
dev = argv[2];
|
|
|
|
}
|
2011-06-15 07:18:23 -07:00
|
|
|
else
|
2011-07-23 10:46:25 -07:00
|
|
|
exit(1);
|
2013-01-11 02:34:29 -08:00
|
|
|
|
2012-09-16 03:43:17 -07:00
|
|
|
if (!dev) return -1;
|
2011-06-15 07:18:23 -07:00
|
|
|
|
|
|
|
if (setuid(0) != 0)
|
|
|
|
{
|
|
|
|
printf("ERROR: UNABLE TO ASSUME ROOT PRIVILEGES\n");
|
|
|
|
exit(5);
|
|
|
|
}
|
|
|
|
if (setgid(0) != 0)
|
|
|
|
{
|
|
|
|
printf("ERROR: UNABLE TO ASSUME ROOT GROUP PRIVILEGES\n");
|
|
|
|
exit(7);
|
|
|
|
}
|
2011-10-28 21:36:32 -07:00
|
|
|
|
2011-10-29 11:45:43 -07:00
|
|
|
eeze_init();
|
|
|
|
devs = eeze_udev_find_by_filter("backlight", NULL, NULL);
|
2011-12-23 23:29:21 -08:00
|
|
|
if (!devs)
|
|
|
|
{
|
|
|
|
devs = eeze_udev_find_by_filter("leds", NULL, NULL);
|
|
|
|
if (!devs) return -1;
|
|
|
|
}
|
2012-09-16 03:43:17 -07:00
|
|
|
if (devs)
|
2012-01-07 07:12:50 -08:00
|
|
|
{
|
|
|
|
EINA_LIST_FOREACH(devs, l, f)
|
|
|
|
{
|
2012-09-16 03:43:17 -07:00
|
|
|
if (!strcmp(f, dev))
|
|
|
|
{
|
|
|
|
dev = f;
|
|
|
|
devok = 1;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-07 07:12:50 -08:00
|
|
|
}
|
|
|
|
}
|
2013-01-11 02:34:29 -08:00
|
|
|
|
2012-09-16 03:43:17 -07:00
|
|
|
if (!devok) return -1;
|
2013-01-11 02:34:29 -08:00
|
|
|
|
2012-09-16 03:43:17 -07:00
|
|
|
str = eeze_udev_syspath_get_sysattr(dev, "max_brightness");
|
|
|
|
if (str)
|
2011-06-15 07:18:23 -07:00
|
|
|
{
|
2012-09-16 03:43:17 -07:00
|
|
|
maxlevel = atoi(str);
|
|
|
|
eina_stringshare_del(str);
|
|
|
|
str = eeze_udev_syspath_get_sysattr(dev, "brightness");
|
2011-10-29 11:45:43 -07:00
|
|
|
if (str)
|
2011-06-15 07:18:23 -07:00
|
|
|
{
|
2012-09-16 03:43:17 -07:00
|
|
|
curlevel = atoi(str);
|
2011-10-29 11:45:43 -07:00
|
|
|
eina_stringshare_del(str);
|
2011-06-15 07:18:23 -07:00
|
|
|
}
|
|
|
|
}
|
2013-01-11 02:34:29 -08:00
|
|
|
|
2012-09-16 03:43:17 -07:00
|
|
|
if (maxlevel <= 0) maxlevel = 255;
|
|
|
|
if (curlevel >= 0)
|
|
|
|
{
|
2012-12-08 22:27:42 -08:00
|
|
|
curlevel = ((maxlevel * level) + 500) / 1000;
|
|
|
|
if (curlevel > maxlevel) curlevel = maxlevel;
|
2013-01-11 02:34:29 -08:00
|
|
|
else if (curlevel < 0)
|
|
|
|
curlevel = 0;
|
2012-09-16 03:43:17 -07:00
|
|
|
snprintf(buf, sizeof(buf), "%s/brightness", f);
|
|
|
|
return _bl_write_file(buf, curlevel);
|
|
|
|
}
|
2013-01-11 02:34:29 -08:00
|
|
|
|
2012-09-16 03:43:17 -07:00
|
|
|
EINA_LIST_FREE(devs, f)
|
|
|
|
eina_stringshare_del(f);
|
2013-01-11 02:34:29 -08:00
|
|
|
|
2011-06-15 07:18:23 -07:00
|
|
|
return -1;
|
|
|
|
}
|
2013-01-11 02:34:29 -08:00
|
|
|
|