summaryrefslogtreecommitdiff
path: root/src/bin/system/e_system_backlight.c
blob: 1c6c11765d384eab5d77b3fdfa84ace3a0d7190f (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "e_system.h"

typedef struct
{
   char *dev;
   int val, max, val_set, val_get_count;
   Eina_Bool prefer : 1;
   Eina_Bool set : 1;
} Light;

static Eina_Lock _devices_lock;
static Eina_List *_devices = NULL;
static Eina_Semaphore _worker_sem;

static void
_light_set(Light *lig, int val)
{ // val == 0->1000
   int nval = ((lig->max * val) + 500) / 1000;
   if (nval < 0) nval = 0;
   else if (nval > lig->max) nval = lig->max;
   lig->val = nval;
#ifdef HAVE_EEZE
   char buf[PATH_MAX];
   snprintf(buf, sizeof(buf), "%s/brightness", lig->dev);
   int fd = open(buf, O_WRONLY);
   if (fd >= 0)
     {
        char buf2[32];
        snprintf(buf2, sizeof(buf2), "%i", lig->val);
        if (write(fd, buf2, strlen(buf2)) <= 0)
          ERR("Write failed of [%s] to [%s]\n", buf2, buf);
        close(fd);
     }
#elif defined(__FreeBSD_kernel__)
   sysctlbyname(lig->dev, NULL, NULL, &(lig->val), sizeof(lig->val));
#endif
}

static void
_light_get(Light *lig)
{
#ifdef HAVE_EEZE
   const char *s;
   s = eeze_udev_syspath_get_sysattr(lig->dev, "brightness");
   if (s)
     {
        lig->val = atoi(s);
        eina_stringshare_del(s);
     }
#elif defined(__FreeBSD_kernel__)
   size_t plen = sizeof(lig->val);
   sysctlbyname(lig->dev, &(lig->val), &plen, NULL, 0);
#endif
}

static void
_cb_worker(void *data EINA_UNUSED, Ecore_Thread *th)
{
   for (;;)
     {
        Eina_List *l;
        Light *lig;

        eina_semaphore_lock(&_worker_sem);

        eina_lock_take(&_devices_lock);
        EINA_LIST_FOREACH(_devices, l, lig)
          {
             if (lig->val_get_count > 0)
               {
                  _light_get(lig);
                  while (lig->val_get_count > 0)
                    {
                       Light *lig2 = calloc(1, sizeof(Light));
                       lig->val_get_count--;
                       if (lig2)
                         {
                            lig2->dev = strdup(lig->dev);
                            lig2->max = lig->max;
                            if (lig2->dev)
                              {
                                 lig2->val = lig->val;
                                 ecore_thread_feedback(th, lig2);
                              }
                            else free(lig2);
                         }
                    }
               }
             if (lig->set)
               {
                  lig->set = EINA_FALSE;
                  lig->val = lig->val_set;
                  _light_set(lig, lig->val);
               }
          }
        eina_lock_release(&_devices_lock);
     }
}

static void
_cb_worker_message(void *data EINA_UNUSED, Ecore_Thread *th EINA_UNUSED, void *msg_data)
{
   Light *lig = msg_data;
   int val = 0;

   if (lig->max > 0)
     {
        val = ((1000 * lig->val) + 500) / lig->max;
        if (val < 0) val = 0;
        else if (val > 1000) val = 1000;
     }
   e_system_inout_command_send("bklight-val", "%s %i", lig->dev, val);
   free(lig->dev);
   free(lig);
}

static void
_cb_worker_end(void *data EINA_UNUSED, Ecore_Thread *th EINA_UNUSED)
{
}

static void
_cb_worker_cancel(void *data EINA_UNUSED, Ecore_Thread *th EINA_UNUSED)
{
}

static void
_light_add(const char *dev)
{
   Light *lig = calloc(1, sizeof(Light));

   if (!lig) abort();
   lig->dev = strdup(dev);
   if (!lig->dev) abort();
   lig->val = -1; // unknown
#ifdef HAVE_EEZE
   const char *s;

   s = eeze_udev_syspath_get_sysattr(lig->dev, "brightness");
   if (!s)
     {
        free(lig->dev);
        free(lig);
        return;
     }
   lig->val = atoi(s);
   eina_stringshare_del(s);

   s = eeze_udev_syspath_get_sysattr(lig->dev, "max_brightness");
   if (s)
     {
        lig->max = atoi(s);
        eina_stringshare_del(s);
     }
   if (lig->max <= 0) lig->max = 255;
   lig->prefer = eeze_udev_syspath_check_sysattr(lig->dev, "type", "firmware");
#elif defined(__FreeBSD_kernel__)
   size_t plen = sizeof(lig->val);
   sysctlbyname(lig->dev, &(lig->val), &plen, NULL, 0);
   lig->max = 100;
#endif
   _devices = eina_list_append(_devices, lig);
   if (alert_backlight_reset)
     {  // set brightness to max if alert mode is on
        eina_lock_take(&_devices_lock);
        lig->val_set = lig->max;
        lig->set = EINA_TRUE;
        eina_semaphore_release(&_worker_sem, 1);
        eina_lock_release(&_devices_lock);
     }
}

#ifdef HAVE_EEZE
static Eina_Bool
_light_device_include(const char *dev)
{ // filter out known undesirable devices
   if (strstr(dev, "::capslock")) return EINA_FALSE;
   if (strstr(dev, "::numlock")) return EINA_FALSE;
   if (strstr(dev, "::scrolllock")) return EINA_FALSE;
   if (strstr(dev, "::compose")) return EINA_FALSE;
   if (strstr(dev, "::kana")) return EINA_FALSE;
   return EINA_TRUE;
}
#endif

static void
_light_refresh_devices()
{
   Light *lig;

   EINA_LIST_FREE(_devices, lig)
     {
        free(lig->dev);
        free(lig);
     }
#ifdef HAVE_EEZE
   Eina_List *devs;
   const char *s;

   devs = eeze_udev_find_by_filter("backlight", NULL, NULL);
   EINA_LIST_FREE(devs, s)
     {
        if (_light_device_include(s)) _light_add(s);
        eina_stringshare_del(s);
     }
   devs = eeze_udev_find_by_filter("leds", NULL, NULL);
   EINA_LIST_FREE(devs, s)
     {
        if (_light_device_include(s)) _light_add(s);
        eina_stringshare_del(s);
     }
#elif defined(__FreeBSD_kernel__)
   // XXX; shouldn't we scan for devices?
   _light_add("hw.acpi.video.lcd0.brightness");
#endif
}

static Light *
_light_find(const char *dev)
{
   Eina_List *l;
   Light *lig;

   if (!dev) return NULL;
   EINA_LIST_FOREACH(_devices, l, lig)
     {
        if (!strcmp(lig->dev, dev)) return lig;
     }
   return NULL;
}

static void
_cb_bklight_list(void *data EINA_UNUSED, const char *params EINA_UNUSED)
{ // reply = "dev1 -|p dev2 -|p ..."
   Eina_List *l;
   Light *lig;
   char *rep = NULL, *p = NULL;
   size_t repsize = 0;

   eina_lock_take(&_devices_lock);
   EINA_LIST_FOREACH(_devices, l, lig)
     {
        repsize += strlen(lig->dev) + 2 + 1;
     }
   if (repsize > 0)
     {
        rep = malloc(repsize);
        if (!rep) abort();
        p = rep;
        EINA_LIST_FOREACH(_devices, l, lig)
          {
             size_t len = strlen(lig->dev);
             memcpy(p, lig->dev, len + 1);
             p[len] = ' '; len++;
             if (lig->prefer) p[len] = 'p';
             else p[len] = '-';
             len++;
             if (l->next) p[len] = ' ';
             else p[len] = '\0';
             p += len + 1;
          }
     }
   eina_lock_release(&_devices_lock);
   e_system_inout_command_send("bklight-list", "%s", rep);
   free(rep);
}

static void
_cb_bklight_refresh(void *data EINA_UNUSED, const char *params EINA_UNUSED)
{
   Eina_Bool tmp = alert_backlight_reset;
   alert_backlight_reset = EINA_FALSE;
   eina_lock_take(&_devices_lock);
   _light_refresh_devices();
   eina_lock_release(&_devices_lock);
   alert_backlight_reset = tmp;
}

static void
_cb_bklight_get(void *data EINA_UNUSED, const char *params)
{
   Light *lig;

   eina_lock_take(&_devices_lock);
   lig = _light_find(params);
   if (!lig) goto done;
   lig->val_get_count++;
   eina_semaphore_release(&_worker_sem, 1);
   _light_get(lig);
done:
   eina_lock_release(&_devices_lock);
}

static void
_cb_bklight_set(void *data EINA_UNUSED, const char *params)
{
   Light *lig;
   char dev[1024] = "";
   int val = 0;

   if (!params) return;
   if (sscanf(params, "%1023s %i", dev, &val) != 2) return;
   eina_lock_take(&_devices_lock);
   lig = _light_find(dev);
   if (!lig) goto done;
   lig->val_set = val;
   lig->set = EINA_TRUE;
   eina_semaphore_release(&_worker_sem, 1);
done:
   eina_lock_release(&_devices_lock);
}

void
e_system_backlight_init(void)
{
   eina_lock_new(&_devices_lock);
   eina_semaphore_new(&_worker_sem, 0);
   _light_refresh_devices();
   ecore_thread_feedback_run(_cb_worker, _cb_worker_message,
                             _cb_worker_end, _cb_worker_cancel,
                             NULL, EINA_TRUE);
   e_system_inout_command_register("bklight-list",    _cb_bklight_list, NULL);
   e_system_inout_command_register("bklight-refresh", _cb_bklight_refresh, NULL);
   e_system_inout_command_register("bklight-get",     _cb_bklight_get, NULL);
   e_system_inout_command_register("bklight-set",     _cb_bklight_set, NULL);
}

void
e_system_backlight_shutdown(void)
{
   // only shutdown things we really have to - no need to free mem etc.
}