summaryrefslogtreecommitdiff
path: root/src/bin/e_backlight.c
blob: 683a34007756d57e489fd1409f52d0b30b4cd985 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#include "e.h"
#ifdef HAVE_EEZE
# include <Eeze.h>
#endif

// FIXME: backlight should be tied per zone but this implementation is just
// a signleton right now as thats 99% of use cases. but api supports
// doing more. for now make it work in the singleton

#define MODE_NONE  -1
#define MODE_RANDR 0
#define MODE_SYS   1

static double bl_val = 1.0;
static double bl_animval = 1.0;
static int sysmode = MODE_NONE;
static Ecore_Animator *bl_anim = NULL;
static Eina_List *bl_devs = NULL;

static Ecore_Event_Handler *_e_backlight_handler_config_mode = NULL;
static Ecore_Event_Handler *_e_backlight_handler_border_fullscreen = NULL;
static Ecore_Event_Handler *_e_backlight_handler_border_unfullscreen = NULL;
static Ecore_Event_Handler *_e_backlight_handler_border_remove = NULL;
static Ecore_Event_Handler *_e_backlight_handler_border_iconify = NULL;
static Ecore_Event_Handler *_e_backlight_handler_border_uniconify = NULL;
static Ecore_Event_Handler *_e_backlight_handler_border_desk_set = NULL;
static Ecore_Event_Handler *_e_backlight_handler_desk_show = NULL;

static void      _e_backlight_update(E_Zone *zone);
static void      _e_backlight_set(E_Zone *zone, double val);
static Eina_Bool _bl_anim(void *data, double pos);
static Eina_Bool bl_avail = EINA_FALSE;
static Eina_Bool _e_backlight_handler(void *d, int type, void *ev);
#ifdef HAVE_EEZE
static double bl_delayval = 1.0;
static const char *bl_sysval = NULL;
static Ecore_Event_Handler *bl_sys_exit_handler = NULL;
static Ecore_Exe *bl_sys_set_exe = NULL;
static Eina_Bool bl_sys_pending_set = EINA_FALSE;
static Eina_Bool bl_sys_set_exe_ready = EINA_TRUE;

static void      _bl_sys_find(void);
static void      _bl_sys_level_get(void);
static Eina_Bool _e_bl_cb_exit(void *data __UNUSED__, int type __UNUSED__, void *event);
static void      _bl_sys_level_set(double val);
#endif

EAPI int E_EVENT_BACKLIGHT_CHANGE = -1;

EINTERN int
e_backlight_init(void)
{
#ifdef HAVE_EEZE
   eeze_init();
#endif
// why did someone do this? this makes it ONLY work if xrandr has bl support.
// WRONG!
//   bl_avail = ecore_x_randr_output_backlight_available();
   bl_avail = EINA_TRUE;

   _e_backlight_handler_config_mode = ecore_event_handler_add
       (E_EVENT_CONFIG_MODE_CHANGED, _e_backlight_handler, NULL);

   _e_backlight_handler_border_fullscreen = ecore_event_handler_add
       (E_EVENT_CLIENT_FULLSCREEN, _e_backlight_handler, NULL);

   _e_backlight_handler_border_unfullscreen = ecore_event_handler_add
       (E_EVENT_CLIENT_UNFULLSCREEN, _e_backlight_handler, NULL);

   _e_backlight_handler_border_remove = ecore_event_handler_add
       (E_EVENT_CLIENT_REMOVE, _e_backlight_handler, NULL);

   _e_backlight_handler_border_iconify = ecore_event_handler_add
       (E_EVENT_CLIENT_ICONIFY, _e_backlight_handler, NULL);

   _e_backlight_handler_border_uniconify = ecore_event_handler_add
       (E_EVENT_CLIENT_UNICONIFY, _e_backlight_handler, NULL);

   _e_backlight_handler_border_desk_set = ecore_event_handler_add
       (E_EVENT_CLIENT_DESK_SET, _e_backlight_handler, NULL);

   _e_backlight_handler_desk_show = ecore_event_handler_add
       (E_EVENT_DESK_SHOW, _e_backlight_handler, NULL);

   if (bl_avail)
     {
        e_backlight_update();
        if (!getenv("E_RESTART"))
          {
             e_backlight_level_set(NULL, 0.1, 0.0);
             e_backlight_level_set(NULL, e_config->backlight.normal, 0.0);
          }
     }

   E_EVENT_BACKLIGHT_CHANGE = ecore_event_type_new();

   return 1;
}

EINTERN int
e_backlight_shutdown(void)
{
   const char *s;

   if (bl_anim) ecore_animator_del(bl_anim);
   bl_anim = NULL;

   if (e_config->backlight.mode != E_BACKLIGHT_MODE_NORMAL)
     e_backlight_level_set(NULL, e_config->backlight.normal, 0.0);

   EINA_LIST_FREE(bl_devs, s)
     eina_stringshare_del(s);
#ifdef HAVE_EEZE
   if (bl_sysval) eina_stringshare_del(bl_sysval);
   bl_sysval = NULL;
   if (bl_sys_exit_handler) ecore_event_handler_del(bl_sys_exit_handler);
   bl_sys_exit_handler = NULL;
   bl_sys_set_exe = NULL;
   bl_sys_pending_set = EINA_FALSE;
   eeze_shutdown();
#endif
   if (_e_backlight_handler_config_mode)
     {
        ecore_event_handler_del(_e_backlight_handler_config_mode);
        _e_backlight_handler_config_mode = NULL;
     }

   if (_e_backlight_handler_border_fullscreen)
     {
        ecore_event_handler_del(_e_backlight_handler_border_fullscreen);
        _e_backlight_handler_border_fullscreen = NULL;
     }

   if (_e_backlight_handler_border_unfullscreen)
     {
        ecore_event_handler_del(_e_backlight_handler_border_unfullscreen);
        _e_backlight_handler_border_unfullscreen = NULL;
     }

   if (_e_backlight_handler_border_remove)
     {
        ecore_event_handler_del(_e_backlight_handler_border_remove);
        _e_backlight_handler_border_remove = NULL;
     }

   if (_e_backlight_handler_border_iconify)
     {
        ecore_event_handler_del(_e_backlight_handler_border_iconify);
        _e_backlight_handler_border_iconify = NULL;
     }

   if (_e_backlight_handler_border_uniconify)
     {
        ecore_event_handler_del(_e_backlight_handler_border_uniconify);
        _e_backlight_handler_border_uniconify = NULL;
     }

   if (_e_backlight_handler_border_desk_set)
     {
        ecore_event_handler_del(_e_backlight_handler_border_desk_set);
        _e_backlight_handler_border_desk_set = NULL;
     }

   if (_e_backlight_handler_desk_show)
     {
        ecore_event_handler_del(_e_backlight_handler_desk_show);
        _e_backlight_handler_desk_show = NULL;
     }
   return 1;
}

EAPI Eina_Bool
e_backlight_exists(void)
{
   if (sysmode == MODE_NONE) return EINA_FALSE;
   return EINA_TRUE;
}

EAPI void
e_backlight_update(void)
{
   const Eina_List *l, *ll;
   E_Comp *c;
   E_Zone *zone;

   if (bl_avail == EINA_FALSE) return;

   EINA_LIST_FOREACH(e_comp_list(), l, c)
     EINA_LIST_FOREACH(c->zones, ll, zone)
       _e_backlight_update(zone);
}

EAPI void
e_backlight_level_set(E_Zone *zone, double val, double tim)
{
   double bl_now;
   // zone == NULL == everything
   // set backlight associated with zone to val over period of tim
   // if tim == 0.0 - then do it instantnly, if time == -1 use some default
   // transition time
   if (val < 0.0) val = 0.0;
   else if (val > 1.0)
     val = 1.0;
   if ((val == bl_val) && (!bl_anim)) return;
   if (!zone) zone = e_util_zone_current_get(e_manager_current_get());
   ecore_event_add(E_EVENT_BACKLIGHT_CHANGE, NULL, NULL, NULL);
   bl_now = bl_val;
   bl_val = val;
   if (fabs(tim) < DBL_EPSILON)
     {
        _e_backlight_set(zone, val);
        return;
     }
//   if (e_config->backlight.mode != E_BACKLIGHT_MODE_NORMAL) return;
   if (e_config->backlight.mode == E_BACKLIGHT_MODE_NORMAL)
     tim = 0.5;
   else
   if (tim < 0.0)
     tim = e_config->backlight.transition;

   E_FREE_FUNC(bl_anim, ecore_animator_del);
   bl_anim = ecore_animator_timeline_add(tim, _bl_anim, zone);
   bl_animval = bl_now;
}

EAPI double
e_backlight_level_get(E_Zone *zone __UNUSED__)
{
   // zone == NULL == everything
   return bl_val;
}

EAPI void
e_backlight_mode_set(E_Zone *zone, E_Backlight_Mode mode)
{
   E_Backlight_Mode pmode;
   
   // zone == NULL == everything
   if (e_config->backlight.mode == mode) return;
   pmode = e_config->backlight.mode;
   e_config->backlight.mode = mode;
   if (e_config->backlight.mode == E_BACKLIGHT_MODE_NORMAL)
     {
        e_backlight_level_set(zone, e_config->backlight.normal, -1.0);
     }
   else if (e_config->backlight.mode == E_BACKLIGHT_MODE_OFF)
     {
        e_backlight_level_set(zone, 0.0, -1.0);
     }
   else if (e_config->backlight.mode == E_BACKLIGHT_MODE_DIM)
     {
        if ((pmode != E_BACKLIGHT_MODE_NORMAL) ||
            ((pmode == E_BACKLIGHT_MODE_NORMAL) &&
             (e_config->backlight.normal > e_config->backlight.dim)))
          e_backlight_level_set(zone, e_config->backlight.dim, -1.0);
     }
   else if (e_config->backlight.mode == E_BACKLIGHT_MODE_MAX)
     e_backlight_level_set(zone, 1.0, -1.0);
}

EAPI E_Backlight_Mode
e_backlight_mode_get(E_Zone *zone __UNUSED__)
{
   // zone == NULL == everything
   return e_config->backlight.mode;
}

EAPI const Eina_List *
e_backlight_devices_get(void)
{
   return bl_devs;
}

/* local subsystem functions */

static Eina_Bool
_e_backlight_handler(void *d __UNUSED__, int type __UNUSED__, void *ev __UNUSED__)
{
   e_backlight_update();
   return ECORE_CALLBACK_PASS_ON;
}

static void
_e_backlight_update(E_Zone *zone)
{
   double x_bl = -1.0;
   Ecore_X_Window root;
   Ecore_X_Randr_Output *out;
   int i, num = 0;

   root = zone->comp->man->root;
   // try randr
   if (ecore_x_randr_output_backlight_available())
     {
        out = ecore_x_randr_window_outputs_get(root, &num);
        if ((out) && (num > 0))
          {
             char *name;
             const char *s;
             Eina_Bool gotten = EINA_FALSE;

             EINA_LIST_FREE(bl_devs, s)
               eina_stringshare_del(s);
             for (i = 0; i < num; i++)
               {
                  name = ecore_x_randr_output_name_get(root, out[i], NULL);
                  bl_devs = eina_list_append(bl_devs, eina_stringshare_add(name));
                  if ((name) && (e_config->backlight.sysdev) &&
                      (!strcmp(name, e_config->backlight.sysdev)))
                    {
                       x_bl = ecore_x_randr_output_backlight_level_get(root, out[i]);
                       gotten = EINA_TRUE;
                    }
                  free(name);
               }
             if (!gotten)
               x_bl = ecore_x_randr_output_backlight_level_get(root, out[0]);
          }
        free(out);
     }
   if (x_bl >= 0.0)
     {
        bl_val = x_bl;
        sysmode = MODE_RANDR;
        return;
     }
#ifdef HAVE_EEZE
   _bl_sys_find();
   if (bl_sysval)
     {
        sysmode = MODE_SYS;
        _bl_sys_level_get();
        return;
     }
#endif
}

static void
_e_backlight_set(E_Zone *zone, double val)
{
   if (sysmode == MODE_RANDR)
     {
        Ecore_X_Window root;
        Ecore_X_Randr_Output *out;
        int num = 0, i;
        char *name;

        root = zone->comp->man->root;
        out = ecore_x_randr_window_outputs_get(root, &num);
        if ((out) && (num > 0))
          {
             Eina_Bool gotten = EINA_FALSE;
             for (i = 0; i < num; i++)
               {
                  name = ecore_x_randr_output_name_get(root, out[i], NULL);
                  if (name)
                    {
                       if ((e_config->backlight.sysdev) &&
                           (!strcmp(name, e_config->backlight.sysdev)))
                         {
                            ecore_x_randr_output_backlight_level_set(root, out[i], val);
                            gotten = EINA_TRUE;
                         }
                       free(name);
                    }
               }
             if (!gotten)
               {
                  for (i = 0; i < num; i++)
                    ecore_x_randr_output_backlight_level_set(root, out[i], val);
               }
          }
        free(out);
     }
#ifdef HAVE_EEZE
   else if (sysmode == MODE_SYS)
     {
        if (bl_sysval)
          {
             _bl_sys_level_set(val);
          }
     }
#endif
}

static Eina_Bool
_bl_anim(void *data, double pos)
{
   E_Zone *zone = data;
   double v;

   // FIXME: if zone is deleted while anim going... bad things.
   pos = ecore_animator_pos_map(pos, ECORE_POS_MAP_DECELERATE, 0.0, 0.0);
   v = (bl_animval * (1.0 - pos)) + (bl_val * pos);
   _e_backlight_set(zone, v);
   if (pos >= 1.0)
     {
        bl_anim = NULL;
        return EINA_FALSE;
     }
   return EINA_TRUE;
}

#ifdef HAVE_EEZE
static void
_bl_sys_find(void)
{
   Eina_List *l, *devs, *pdevs = NULL;
   Eina_Bool use;
   const char *f, *s;
   int v;

   devs = eeze_udev_find_by_filter("backlight", NULL, NULL);
   if (!devs)
     {
        /* FIXME: need to make this more precise so we don't set keyboard LEDs or something */
        devs = eeze_udev_find_by_filter("leds", NULL, NULL);
        if (!devs) return;
     }
   if (eina_list_count(devs) > 1)
     {
        /* prefer backlights of type "firmware" where available */
        EINA_LIST_FOREACH(devs, l, f)
          {
             s = eeze_udev_syspath_get_sysattr(f, "type");
             use = (s && (!strcmp(s, "firmware")));
             eina_stringshare_del(s);
             if (!use) continue;
             s = eeze_udev_syspath_get_sysattr(f, "brightness");
             if (!s) continue;
             v = atoi(s);
             eina_stringshare_del(s);
             if (v < 0) continue;
             pdevs = eina_list_append(pdevs, eina_stringshare_add(f));
             eina_stringshare_del(f);
             l->data = NULL;
          }
        EINA_LIST_FOREACH(devs, l, f)
          {
             if (!l->data) continue;
             s = eeze_udev_syspath_get_sysattr(f, "brightness");
             if (!s) continue;
             v = atoi(s);
             eina_stringshare_del(s);
             if (v < 0) continue;
             pdevs = eina_list_append(pdevs, eina_stringshare_add(f));
          }
     }
   if (!pdevs)
     {
        /* add the other backlight or led's if none found */
        EINA_LIST_FOREACH(devs, l, f)
          {
             use = EINA_FALSE;
             s = eeze_udev_syspath_get_sysattr(f, "brightness");
             if (!s) continue;
             v = atoi(s);
             eina_stringshare_del(s);
             if (v < 0) continue;
             pdevs = eina_list_append(pdevs, eina_stringshare_add(f));
          }
     }
   /* clear out original devs list now we've filtered */
   EINA_LIST_FREE(devs, f)
     {
        if (f) eina_stringshare_del(f);
     }
   /* clear out old configured bl sysval */
   if (bl_sysval)
     {
        eina_stringshare_del(bl_sysval);
        bl_sysval = NULL;
     }
   EINA_LIST_FREE(bl_devs, s)
     eina_stringshare_del(s);
   /* if configured backlight is there - use it, or if not use first */
   EINA_LIST_FOREACH(pdevs, l, f)
     {
        bl_devs = eina_list_append(bl_devs, eina_stringshare_add(f));
        if (!bl_sysval)
          {
             if ((e_config->backlight.sysdev) &&
                 (!strcmp(e_config->backlight.sysdev, f)))
               bl_sysval = eina_stringshare_add(f);
          }
     }
   if (!bl_sysval)
     {
        EINA_LIST_FOREACH(pdevs, l, f)
          {
             if (!bl_sysval)
               bl_sysval = eina_stringshare_add(f);
          }
     }
   /* clear out preferred devs list */
   EINA_LIST_FREE(pdevs, f)
     {
        eina_stringshare_del(f);
     }
}

static void
_bl_sys_level_get(void)
{
   int maxval, val;
   const char *str;

   if (bl_anim) return;

   str = eeze_udev_syspath_get_sysattr(bl_sysval, "max_brightness");
   if (!str) return;

   maxval = atoi(str);
   eina_stringshare_del(str);
   if (maxval < 0) maxval = 255;
   str = eeze_udev_syspath_get_sysattr(bl_sysval, "brightness");
   if (!str) return;

   val = atoi(str);
   eina_stringshare_del(str);
   if ((!maxval) && (!val))
     {
        bl_val = 0;
        sysmode = MODE_NONE;
        return;
     }
   if (!maxval) maxval = 255;
   if ((val >= 0) && (val <= maxval))
     bl_val = (double)val / (double)maxval;
//   fprintf(stderr, "GET: %i/%i (%1.3f)\n", val, maxval, bl_val);
}

static Eina_Bool
_e_bl_cb_ext_delay(void *data __UNUSED__)
{
   bl_sys_set_exe_ready = EINA_TRUE;
   if (bl_sys_pending_set)
     {
        bl_sys_pending_set = EINA_FALSE;

        _bl_sys_level_set(bl_delayval);
     }
   return EINA_FALSE;
}

static Eina_Bool
_e_bl_cb_exit(void *data __UNUSED__, int type __UNUSED__, void *event)
{
   Ecore_Exe_Event_Del *ev;

   ev = event;
   if (ev->exe == bl_sys_set_exe)
     {
        bl_sys_set_exe_ready = EINA_FALSE;
        bl_sys_set_exe = NULL;
        ecore_timer_add(0.05, _e_bl_cb_ext_delay, NULL);
     }
   return ECORE_CALLBACK_RENEW;
}

static void
_bl_sys_level_set(double val)
{
   char buf[PATH_MAX];

   if (!bl_sys_exit_handler)
     bl_sys_exit_handler = ecore_event_handler_add(ECORE_EXE_EVENT_DEL,
                                                   _e_bl_cb_exit, NULL);
   bl_delayval = val;
   if ((bl_sys_set_exe) || (!bl_sys_set_exe_ready))
     {
        bl_sys_pending_set = EINA_TRUE;
        return;
     }
//   fprintf(stderr, "SET: %1.3f\n", val);
   snprintf(buf, sizeof(buf),
            "%s/enlightenment/utils/enlightenment_backlight %i %s",
            e_prefix_lib_get(), (int)(val * 1000.0), bl_sysval);
   bl_sys_set_exe = ecore_exe_run(buf, NULL);
}

#endif