[E_BORDER] Add missing code to delete timer when the window gets DEICONIFY_APPROVE...
[platform/core/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), "%d", 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 /* externally accessible functions */
37 int
38 main(int argc, char **argv)
39 {
40    int i, level, devok = 0;
41    const char *f, *dev = NULL, *str;
42    int maxlevel = 0, curlevel = -1;
43    Eina_List *devs, *l;
44    char buf[4096] = "";
45
46    for (i = 1; i < argc; i++)
47      {
48         if ((!strcmp(argv[i], "-h")) ||
49             (!strcmp(argv[i], "-help")) ||
50             (!strcmp(argv[i], "--help")))
51           {
52              printf("This is an internal tool for Enlightenment.\n"
53                     "do not use it.\n");
54              exit(0);
55           }
56      }
57    if (argc == 3)
58      {
59         level = atoi(argv[1]);
60         dev = argv[2];
61      }
62    else
63      exit(1);
64    
65    if (!dev) return -1;
66
67    if (setuid(0) != 0)
68      {
69         printf("ERROR: UNABLE TO ASSUME ROOT PRIVILEGES\n");
70         exit(5);
71      }
72    if (setgid(0) != 0)
73      {
74         printf("ERROR: UNABLE TO ASSUME ROOT GROUP PRIVILEGES\n");
75         exit(7);
76      }
77
78    eeze_init();
79    devs = eeze_udev_find_by_filter("backlight", NULL, NULL);
80    if (!devs)
81      {
82         devs = eeze_udev_find_by_filter("leds", NULL, NULL);
83         if (!devs) return -1;
84      }
85    if (devs)
86      {
87         EINA_LIST_FOREACH(devs, l, f)
88           {
89              if (!strcmp(f, dev))
90                {
91                   dev = f;
92                   devok = 1;
93                   break;
94                }
95           }
96      }
97    
98    if (!devok) return -1;
99    
100    str = eeze_udev_syspath_get_sysattr(dev, "max_brightness");
101    if (str)
102      {
103         maxlevel = atoi(str);
104         eina_stringshare_del(str);
105         str = eeze_udev_syspath_get_sysattr(dev, "brightness");
106         if (str)
107           {
108              curlevel = atoi(str);
109              eina_stringshare_del(str);
110           }
111      }
112    
113    if (maxlevel <= 0) maxlevel = 255;
114    if (curlevel >= 0)
115      {
116         curlevel = ((maxlevel * level) + (500 / maxlevel)) / 1000;
117         snprintf(buf, sizeof(buf), "%s/brightness", f);
118         return _bl_write_file(buf, curlevel);
119      }
120    
121    EINA_LIST_FREE(devs, f)
122      eina_stringshare_del(f);
123    
124    return -1;
125 }