summaryrefslogtreecommitdiff
path: root/src/bin/e_backlight_main.c
blob: 4aadbab27fb0efe1f089a2e9965709eee00b26ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#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>

#include <Eeze.h>

/* local subsystem functions */
static int
_bl_write_file(const char *file, int val)
{
   char buf[256];
   int fd = open(file, O_WRONLY);
   if (fd < 0)
     {
        perror("open");
        return -1;
     }
   snprintf(buf, sizeof(buf), "%d", val);
   if (write(fd, buf, strlen(buf)) != (int)strlen(buf))
     {
        perror("write");
        close(fd);
        return -1;
     }
   close(fd);
   return 0;
}

/* externally accessible functions */
int
main(int argc, char **argv)
{
   int i, level, devok = 0;
   const char *f, *dev = NULL, *str;
   int maxlevel = 0, curlevel = -1;
   Eina_List *devs, *l;
   char buf[4096] = "";

   for (i = 1; i < argc; i++)
     {
        if ((!strcmp(argv[i], "-h")) ||
            (!strcmp(argv[i], "-help")) ||
            (!strcmp(argv[i], "--help")))
          {
             printf("This is an internal tool for Enlightenment.\n"
                    "do not use it.\n");
             exit(0);
          }
     }
   if (argc == 3)
     {
        level = atoi(argv[1]);
        dev = argv[2];
     }
   else
     exit(1);

   if (!dev) return -1;

   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);
     }

   eeze_init();
   devs = eeze_udev_find_by_filter("backlight", NULL, NULL);
   if (!devs)
     {
        devs = eeze_udev_find_by_filter("leds", NULL, NULL);
        if (!devs) return -1;
     }
   if (devs)
     {
        EINA_LIST_FOREACH(devs, l, f)
          {
             if (!strcmp(f, dev))
               {
                  dev = f;
                  devok = 1;
                  break;
               }
          }
     }

   if (!devok) return -1;

   str = eeze_udev_syspath_get_sysattr(dev, "max_brightness");
   if (str)
     {
        maxlevel = atoi(str);
        eina_stringshare_del(str);
        str = eeze_udev_syspath_get_sysattr(dev, "brightness");
        if (str)
          {
             curlevel = atoi(str);
             eina_stringshare_del(str);
          }
     }

   if (maxlevel <= 0) maxlevel = 255;
   if (curlevel >= 0)
     {
        curlevel = ((maxlevel * level) + 500) / 1000;
        if (curlevel > maxlevel) curlevel = maxlevel;
        else if (curlevel < 0)
          curlevel = 0;
        snprintf(buf, sizeof(buf), "%s/brightness", f);
        return _bl_write_file(buf, curlevel);
     }

   EINA_LIST_FREE(devs, f)
     eina_stringshare_del(f);

   return -1;
}