update for beta release
[framework/uifw/e17.git] / src / bin / e_backlight_main.c
1 #include "config.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <dirent.h>
10 #include <fcntl.h>
11
12 #include <Eeze.h>
13
14 /* local subsystem functions */
15 static int
16 _bl_write_file(const char *file, int val)
17 {
18    char buf[256];
19    int fd = open(file, O_WRONLY);
20    if (fd < 0)
21      {
22         perror("open");
23         return -1;
24      }
25    snprintf(buf, sizeof(buf), "%i", val);
26    if (write(fd, buf, strlen(buf)) != (int)strlen(buf))
27      {
28         perror("write");
29         close(fd);
30         return -1;
31      }
32    close(fd);
33    return 0;
34 }
35
36 /* local subsystem globals */
37 typedef struct _Bl_Entry
38 {
39    char type;
40    const char *base;
41    const char *max;
42    const char *set;
43 } Bl_Entry;
44
45 static const Bl_Entry search[] =
46 {
47    { 'F', "/sys/devices/virtual/backlight/acpi_video0", "max_brightness", "brightness" },
48    { 'D', "/sys/devices/virtual/backlight", "max_brightness", "brightness" },
49    { 'F', "/sys/class/leds/lcd-backlight", "max_brightness", "brightness" },
50    { 'F', "/sys/class/backlight/acpi_video0", "max_brightness", "brightness" },
51    { 'D', "/sys/class/backlight", "max_brightness", "brightness" }
52 };
53
54 /* externally accessible functions */
55 int
56 main(int argc, char **argv)
57 {
58    int i;
59    int level;
60    const char *f;
61    int maxlevel = 0, curlevel = -1;
62    Eina_List *devs;
63    char buf[4096] = "";
64    
65    for (i = 1; i < argc; i++)
66      {
67         if ((!strcmp(argv[i], "-h")) ||
68             (!strcmp(argv[i], "-help")) ||
69             (!strcmp(argv[i], "--help")))
70           {
71              printf("This is an internal tool for Enlightenment.\n"
72                     "do not use it.\n");
73              exit(0);
74           }
75      }
76    if (argc == 2)
77      level = atoi(argv[1]);
78    else
79      exit(1);
80
81    if (setuid(0) != 0)
82      {
83         printf("ERROR: UNABLE TO ASSUME ROOT PRIVILEGES\n");
84         exit(5);
85      }
86    if (setgid(0) != 0)
87      {
88         printf("ERROR: UNABLE TO ASSUME ROOT GROUP PRIVILEGES\n");
89         exit(7);
90      }
91
92    eeze_init();
93    devs = eeze_udev_find_by_filter("backlight", NULL, NULL);
94    if (!devs) return -1;
95    EINA_LIST_FREE(devs, f)
96      {
97         const char *str;
98
99         str = eeze_udev_syspath_get_sysattr(f, "max_brightness");
100         if (str)
101           {
102              maxlevel = atoi(str);
103              eina_stringshare_del(str);
104              str = eeze_udev_syspath_get_sysattr(f, "brightness");
105              if (str)
106                {
107                   curlevel = atoi(str);
108                   eina_stringshare_del(str);
109                }
110           }
111
112         if (maxlevel <= 0) maxlevel = 255;
113         if (curlevel >= 0)
114           {
115              curlevel = ((maxlevel * level) + (500 / maxlevel)) / 1000;
116      //        printf("SET: %i, %i/%i\n", level, curlevel, maxlevel);
117              snprintf(buf, sizeof(buf), "%s/brightness", f);
118              return _bl_write_file(buf, curlevel);
119           }
120         eina_stringshare_del(f);
121      }
122
123    return -1;
124 }