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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2008-2010 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2012-2021 MATE Developers
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include "egg-discrete.h"
#include "gpm-brightness.h"
#include "gpm-common.h"
#include "gpm-marshal.h"
#define GPM_SOLE_SETTER_USE_CACHE TRUE /* this may be insanity */
struct GpmBrightnessPrivate
{
gboolean has_changed_events;
gboolean cache_trusted;
guint cache_percentage;
guint last_set_hw;
Atom backlight;
Display *dpy;
GdkWindow *root_window;
guint shared_value;
gboolean has_extension;
gboolean hw_changed;
/* A cache of XRRScreenResources is used as XRRGetScreenResources is expensive */
GPtrArray *resources;
gint extension_levels;
gint extension_current;
};
enum {
BRIGHTNESS_CHANGED,
LAST_SIGNAL
};
typedef enum {
ACTION_BACKLIGHT_GET,
ACTION_BACKLIGHT_SET,
ACTION_BACKLIGHT_INC,
ACTION_BACKLIGHT_DEC
} GpmXRandROp;
G_DEFINE_TYPE_WITH_PRIVATE (GpmBrightness, gpm_brightness, G_TYPE_OBJECT)
static guint signals [LAST_SIGNAL] = { 0 };
static gpointer gpm_brightness_object = NULL;
/**
* gpm_brightness_helper_strtoint:
* @text: The text to be converted
* @value: The return numeric return value
*
* Convert a string to a signed integer value in a safe way.
*
* Return value: %TRUE if the string was converted correctly
**/
static gboolean
gpm_brightness_helper_strtoint (const gchar *text, gint *value)
{
gchar *endptr = NULL;
gint64 value_raw;
if (text == NULL)
return FALSE;
value_raw = g_ascii_strtoll (text, &endptr, 10);
if (endptr == text)
return FALSE;
if (errno == ERANGE || value_raw > G_MAXINT || value_raw < G_MININT)
return FALSE;
*value = (gint) value_raw;
return TRUE;
}
/**
* gpm_brightness_helper_get_value:
**/
static gint
gpm_brightness_helper_get_value (const gchar *argument)
{
gboolean ret;
GError *error = NULL;
gchar *stdout_data = NULL;
gint exit_status = 0;
gint value = -1;
gchar *command = NULL;
/* get the data */
command = g_strdup_printf (SBINDIR "/mate-power-backlight-helper --%s", argument);
ret = g_spawn_command_line_sync (command,
&stdout_data, NULL, &exit_status, &error);
if (!ret) {
g_error ("failed to get value: %s", error->message);
g_error_free (error);
goto out;
}
g_debug ("executing %s retval: %i", command, exit_status);
/* parse for a number */
ret = gpm_brightness_helper_strtoint (stdout_data, &value);
if (!ret)
goto out;
out:
g_free (command);
g_free (stdout_data);
return value;
}
/**
* gpm_brightness_helper_set_value:
**/
static gboolean
gpm_brightness_helper_set_value (const gchar *argument, gint value)
{
gboolean ret;
GError *error = NULL;
gint exit_status = 0;
gchar *command = NULL;
/* get the data */
command = g_strdup_printf ("pkexec " SBINDIR "/mate-power-backlight-helper --%s %i", argument, value);
ret = g_spawn_command_line_sync (command, NULL, NULL, &exit_status, &error);
if (!ret) {
g_error ("failed to get value: %s", error->message);
g_error_free (error);
goto out;
}
g_debug ("executing %s retval: %i", command, exit_status);
out:
g_free (command);
return ret;
}
/**
* gpm_brightness_get_step:
* @levels: The number of levels supported
* Return value: the amount of hardware steps to do on each increment or decrement
**/
static guint
gpm_brightness_get_step (guint levels)
{
/* macbook pro has a bazzillion brightness levels, do in 5% steps */
if (levels > 20)
return levels / 20;
return 1;
}
/**
* gpm_brightness_output_get_internal:
**/
static gboolean
gpm_brightness_output_get_internal (GpmBrightness *brightness, RROutput output, guint *cur)
{
unsigned long nitems;
unsigned long bytes_after;
guint *prop;
Atom actual_type;
int actual_format;
gboolean ret = FALSE;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
if (brightness->priv->backlight == None)
return FALSE;
if (XRRGetOutputProperty (brightness->priv->dpy, output, brightness->priv->backlight,
0, 4, False, False, None,
&actual_type, &actual_format,
&nitems, &bytes_after, ((unsigned char **)&prop)) != Success) {
g_debug ("failed to get property");
return FALSE;
}
if (actual_type == XA_INTEGER && nitems == 1 && actual_format == 32) {
memcpy (cur, prop, sizeof (guint));
ret = TRUE;
}
XFree (prop);
return ret;
}
/**
* gpm_brightness_output_set_internal:
**/
static gboolean
gpm_brightness_output_set_internal (GpmBrightness *brightness, RROutput output, guint value)
{
GdkDisplay *display;
gboolean ret = TRUE;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
/* don't abort on error */
display = gdk_display_get_default ();
gdk_x11_display_error_trap_push (display);
XRRChangeOutputProperty (brightness->priv->dpy, output, brightness->priv->backlight, XA_INTEGER, 32,
PropModeReplace, (unsigned char *) &value, 1);
XFlush (brightness->priv->dpy);
gdk_display_flush (display);
if (gdk_x11_display_error_trap_pop (display)) {
g_warning ("failed to XRRChangeOutputProperty for brightness %i", value);
ret = FALSE;
}
/* we changed the hardware */
if (ret)
brightness->priv->hw_changed = TRUE;
return ret;
}
/**
* gpm_brightness_setup_display:
**/
static gboolean
gpm_brightness_setup_display (GpmBrightness *brightness)
{
gint major, minor;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
/* get the display */
brightness->priv->dpy = GDK_DISPLAY_XDISPLAY (gdk_display_get_default());
if (!brightness->priv->dpy) {
g_error ("Cannot open display");
return FALSE;
}
/* is XRandR new enough? */
if (!XRRQueryVersion (brightness->priv->dpy, &major, &minor)) {
g_debug ("RandR extension missing");
return FALSE;
}
if (major < 1 || (major == 1 && minor < 3)) {
g_debug ("RandR version %d.%d too old", major, minor);
return FALSE;
}
/* Can we support "Backlight" */
brightness->priv->backlight = XInternAtom (brightness->priv->dpy, "Backlight", True);
if (brightness->priv->backlight == None) {
/* Do we support "BACKLIGHT" (legacy) */
brightness->priv->backlight = XInternAtom (brightness->priv->dpy, "BACKLIGHT", True);
if (brightness->priv->backlight == None) {
g_debug ("No outputs have backlight property");
return FALSE;
}
}
return TRUE;
}
/**
* gpm_brightness_output_get_limits:
**/
static gboolean
gpm_brightness_output_get_limits (GpmBrightness *brightness, RROutput output,
guint *min, guint *max)
{
XRRPropertyInfo *info;
gboolean ret = TRUE;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
info = XRRQueryOutputProperty (brightness->priv->dpy, output, brightness->priv->backlight);
if (info == NULL) {
g_debug ("could not get output property");
return FALSE;
}
if (!info->range || info->num_values != 2) {
g_debug ("was not range");
ret = FALSE;
goto out;
}
*min = info->values[0];
*max = info->values[1];
out:
XFree (info);
return ret;
}
/**
* gpm_brightness_output_get_percentage:
**/
static gboolean
gpm_brightness_output_get_percentage (GpmBrightness *brightness, RROutput output)
{
guint cur;
gboolean ret;
guint min, max;
guint percentage;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
ret = gpm_brightness_output_get_internal (brightness, output, &cur);
if (!ret)
return FALSE;
ret = gpm_brightness_output_get_limits (brightness, output, &min, &max);
if (!ret || min == max)
return FALSE;
g_debug ("hard value=%i, min=%i, max=%i", cur, min, max);
percentage = egg_discrete_to_percent (cur, (max-min)+1);
g_debug ("percentage %i", percentage);
brightness->priv->shared_value = percentage;
return TRUE;
}
/**
* gpm_brightness_output_down:
**/
static gboolean
gpm_brightness_output_down (GpmBrightness *brightness, RROutput output)
{
guint cur;
guint step;
gboolean ret;
guint min, max;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
ret = gpm_brightness_output_get_internal (brightness, output, &cur);
if (!ret)
return FALSE;
ret = gpm_brightness_output_get_limits (brightness, output, &min, &max);
if (!ret || min == max)
return FALSE;
g_debug ("hard value=%i, min=%i, max=%i", cur, min, max);
if (cur == min) {
g_debug ("already min");
return TRUE;
}
step = gpm_brightness_get_step ((max-min)+1);
if (cur < step) {
g_debug ("truncating to %i", min);
cur = min;
} else {
cur -= step;
}
ret = gpm_brightness_output_set_internal (brightness, output, cur);
return ret;
}
/**
* gpm_brightness_output_up:
**/
static gboolean
gpm_brightness_output_up (GpmBrightness *brightness, RROutput output)
{
guint cur;
gboolean ret;
guint min, max;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
ret = gpm_brightness_output_get_internal (brightness, output, &cur);
if (!ret)
return FALSE;
ret = gpm_brightness_output_get_limits (brightness, output, &min, &max);
if (!ret || min == max)
return FALSE;
g_debug ("hard value=%i, min=%i, max=%i", cur, min, max);
if (cur == max) {
g_debug ("already max");
return TRUE;
}
cur += gpm_brightness_get_step ((max-min)+1);
if (cur > max) {
g_debug ("truncating to %i", max);
cur = max;
}
ret = gpm_brightness_output_set_internal (brightness, output, cur);
return ret;
}
/**
* gpm_brightness_output_set:
**/
static gboolean
gpm_brightness_output_set (GpmBrightness *brightness, RROutput output)
{
guint cur;
gboolean ret;
guint min, max;
gint i;
gint shared_value_abs;
guint step;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
ret = gpm_brightness_output_get_internal (brightness, output, &cur);
if (!ret)
return FALSE;
ret = gpm_brightness_output_get_limits (brightness, output, &min, &max);
if (!ret || min == max)
return FALSE;
shared_value_abs = egg_discrete_from_percent (brightness->priv->shared_value, (max-min)+1);
g_debug ("percent=%i, absolute=%i", brightness->priv->shared_value, shared_value_abs);
g_debug ("hard value=%i, min=%i, max=%i", cur, min, max);
if (shared_value_abs > (gint) max)
shared_value_abs = max;
if (shared_value_abs < (gint) min)
shared_value_abs = min;
if ((gint) cur == shared_value_abs) {
g_debug ("already set %i", cur);
return TRUE;
}
/* step the correct way */
if ((gint) cur < shared_value_abs) {
/* some adaptors have a large number of steps */
step = gpm_brightness_get_step (shared_value_abs - cur);
g_debug ("using step of %i", step);
/* going up */
for (i=cur; i<=shared_value_abs; i+=step) {
ret = gpm_brightness_output_set_internal (brightness, output, i);
if (!ret)
break;
if ((gint) cur != shared_value_abs)
g_usleep (1000 * GPM_BRIGHTNESS_DIM_INTERVAL);
}
} else {
/* some adaptors have a large number of steps */
step = gpm_brightness_get_step (cur - shared_value_abs);
g_debug ("using step of %i", step);
/* going down */
for (i=cur; i>=shared_value_abs; i-=step) {
ret = gpm_brightness_output_set_internal (brightness, output, i);
if (!ret)
break;
if ((gint) cur != shared_value_abs)
g_usleep (1000 * GPM_BRIGHTNESS_DIM_INTERVAL);
}
}
return TRUE;
}
/**
* gpm_brightness_foreach_resource:
**/
static gboolean
gpm_brightness_foreach_resource (GpmBrightness *brightness, GpmXRandROp op, XRRScreenResources *resources)
{
gint i;
gboolean ret;
gboolean success_any = FALSE;
RROutput output;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
/* do for each output */
for (i=0; i<resources->noutput; i++) {
output = resources->outputs[i];
g_debug ("resource %i of %i", i+1, resources->noutput);
if (op==ACTION_BACKLIGHT_GET) {
ret = gpm_brightness_output_get_percentage (brightness, output);
} else if (op==ACTION_BACKLIGHT_INC) {
ret = gpm_brightness_output_up (brightness, output);
} else if (op==ACTION_BACKLIGHT_DEC) {
ret = gpm_brightness_output_down (brightness, output);
} else if (op==ACTION_BACKLIGHT_SET) {
ret = gpm_brightness_output_set (brightness, output);
} else {
ret = FALSE;
g_warning ("op not known");
}
if (ret) {
success_any = TRUE;
}
}
return success_any;
}
/**
* gpm_brightness_foreach_screen:
**/
static gboolean
gpm_brightness_foreach_screen (GpmBrightness *brightness, GpmXRandROp op)
{
guint i;
guint length;
XRRScreenResources *resource;
gboolean ret;
gboolean success_any = FALSE;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
/* Return immediately if we can't use XRandR */
if (!brightness->priv->has_extension)
return FALSE;
/* do for each screen */
length = brightness->priv->resources->len;
for (i=0; i<length; i++) {
resource = (XRRScreenResources *) g_ptr_array_index (brightness->priv->resources, i);
g_debug ("using resource %p", resource);
ret = gpm_brightness_foreach_resource (brightness, op, resource);
if (ret)
success_any = TRUE;
}
XSync (brightness->priv->dpy, False);
return success_any;
}
/**
* gpm_brightness_trust_cache:
* @brightness: This brightness class instance
* Return value: if we can trust the cache
**/
static gboolean
gpm_brightness_trust_cache (GpmBrightness *brightness)
{
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
/* only return the cached value if the cache is trusted and we have change events */
if (brightness->priv->cache_trusted && brightness->priv->has_changed_events) {
g_debug ("using cache for value %u (okay)", brightness->priv->cache_percentage);
return TRUE;
}
/* can we trust that if we set a value 5 minutes ago, will it still be valid now?
* if we have multiple things setting policy on the workstation, e.g. fast user switching
* or kpowersave, then this will be invalid -- this logic may be insane */
if (GPM_SOLE_SETTER_USE_CACHE && brightness->priv->cache_trusted) {
g_debug ("using cache for value %u (probably okay)", brightness->priv->cache_percentage);
return TRUE;
}
return FALSE;
}
/**
* gpm_brightness_set:
* @brightness: This brightness class instance
* @percentage: The percentage brightness
* @hw_changed: If the hardware was changed, i.e. the brightness changed
* Return value: %TRUE if success, %FALSE if there was an error
**/
gboolean
gpm_brightness_set (GpmBrightness *brightness, guint percentage, gboolean *hw_changed)
{
gboolean ret = FALSE;
gboolean trust_cache;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
/* can we check the new value with the cache? */
trust_cache = gpm_brightness_trust_cache (brightness);
if (trust_cache && percentage == brightness->priv->cache_percentage) {
g_debug ("not setting the same value %i", percentage);
return TRUE;
}
/* set the value we want */
brightness->priv->shared_value = percentage;
/* reset to not-changed */
brightness->priv->hw_changed = FALSE;
ret = gpm_brightness_foreach_screen (brightness, ACTION_BACKLIGHT_SET);
/* legacy fallback */
if (!ret) {
if (brightness->priv->extension_levels < 0)
brightness->priv->extension_levels = gpm_brightness_helper_get_value ("get-max-brightness");
brightness->priv->extension_current = egg_discrete_from_percent (percentage, brightness->priv->extension_levels+1);
ret = gpm_brightness_helper_set_value ("set-brightness", brightness->priv->extension_current);
}
/* did the hardware have to be modified? */
if (ret && hw_changed != NULL)
*hw_changed = brightness->priv->hw_changed;
/* we did something to the hardware, so untrusted */
if (ret)
brightness->priv->cache_trusted = FALSE;
return ret;
}
/**
* gpm_brightness_get:
* @brightness: This brightness class instance
* Return value: The percentage brightness, or -1 for no hardware or error
*
* Gets the current (or at least what this class thinks is current) percentage
* brightness. This is quick as no HAL inquiry is done.
**/
gboolean
gpm_brightness_get (GpmBrightness *brightness, guint *percentage)
{
gboolean ret = FALSE;
gboolean trust_cache;
guint percentage_local;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
g_return_val_if_fail (percentage != NULL, FALSE);
/* can we use the cache? */
trust_cache = gpm_brightness_trust_cache (brightness);
if (trust_cache) {
*percentage = brightness->priv->cache_percentage;
return TRUE;
}
/* get the brightness from hardware -- slow */
ret = gpm_brightness_foreach_screen (brightness, ACTION_BACKLIGHT_GET);
percentage_local = brightness->priv->shared_value;
/* legacy fallback */
if (!ret) {
if (brightness->priv->extension_levels < 0)
brightness->priv->extension_levels = gpm_brightness_helper_get_value ("get-max-brightness");
brightness->priv->extension_current = gpm_brightness_helper_get_value ("get-brightness");
percentage_local = egg_discrete_to_percent (brightness->priv->extension_current, brightness->priv->extension_levels+1);
ret = TRUE;
}
/* valid? */
if (percentage_local > 100) {
g_warning ("percentage value of %i will be truncated", percentage_local);
percentage_local = 100;
}
/* a new value is always trusted if the method and checks succeed */
if (ret) {
brightness->priv->cache_percentage = percentage_local;
brightness->priv->cache_trusted = TRUE;
*percentage = percentage_local;
} else {
brightness->priv->cache_trusted = FALSE;
}
return ret;
}
/**
* gpm_brightness_up:
* @brightness: This brightness class instance
* @hw_changed: If the hardware was changed, i.e. the brightness changed
* Return value: %TRUE if success, %FALSE if there was an error
*
* If possible, put the brightness of the LCD up one unit.
**/
gboolean
gpm_brightness_up (GpmBrightness *brightness, gboolean *hw_changed)
{
gboolean ret = FALSE;
guint step;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
/* reset to not-changed */
brightness->priv->hw_changed = FALSE;
ret = gpm_brightness_foreach_screen (brightness, ACTION_BACKLIGHT_INC);
/* did the hardware have to be modified? */
if (ret && hw_changed != NULL)
*hw_changed = brightness->priv->hw_changed;
/* we did something to the hardware, so untrusted */
if (ret)
brightness->priv->cache_trusted = FALSE;
/* legacy fallback */
if (!ret) {
if (brightness->priv->extension_levels < 0)
brightness->priv->extension_levels = gpm_brightness_helper_get_value ("get-max-brightness");
brightness->priv->extension_current = gpm_brightness_helper_get_value ("get-brightness");
/* increase by the step, limiting to the maximum possible levels */
if (brightness->priv->extension_current < brightness->priv->extension_levels) {
step = gpm_brightness_get_step (brightness->priv->extension_levels);
brightness->priv->extension_current += step;
if (brightness->priv->extension_current > brightness->priv->extension_levels)
brightness->priv->extension_current = brightness->priv->extension_levels;
ret = gpm_brightness_helper_set_value ("set-brightness", brightness->priv->extension_current);
}
if (hw_changed != NULL)
*hw_changed = ret;
brightness->priv->cache_trusted = FALSE;
goto out;
}
out:
return ret;
}
/**
* gpm_brightness_down:
* @brightness: This brightness class instance
* @hw_changed: If the hardware was changed, i.e. the brightness changed
* Return value: %TRUE if success, %FALSE if there was an error
*
* If possible, put the brightness of the LCD down one unit.
**/
gboolean
gpm_brightness_down (GpmBrightness *brightness, gboolean *hw_changed)
{
gboolean ret = FALSE;
guint step;
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
/* reset to not-changed */
brightness->priv->hw_changed = FALSE;
ret = gpm_brightness_foreach_screen (brightness, ACTION_BACKLIGHT_DEC);
/* did the hardware have to be modified? */
if (ret && hw_changed != NULL)
*hw_changed = brightness->priv->hw_changed;
/* we did something to the hardware, so untrusted */
if (ret)
brightness->priv->cache_trusted = FALSE;
/* legacy fallback */
if (!ret) {
if (brightness->priv->extension_levels < 0)
brightness->priv->extension_levels = gpm_brightness_helper_get_value ("get-max-brightness");
brightness->priv->extension_current = gpm_brightness_helper_get_value ("get-brightness");
/* decrease by the step, limiting to zero */
if (brightness->priv->extension_current > 0) {
step = gpm_brightness_get_step (brightness->priv->extension_levels);
brightness->priv->extension_current -= step;
if (brightness->priv->extension_current < 0)
brightness->priv->extension_current = 0;
ret = gpm_brightness_helper_set_value ("set-brightness", brightness->priv->extension_current);
}
if (hw_changed != NULL)
*hw_changed = ret;
brightness->priv->cache_trusted = FALSE;
goto out;
}
out:
return ret;
}
/**
* gpm_brightness_may_have_changed:
**/
static void
gpm_brightness_may_have_changed (GpmBrightness *brightness)
{
gboolean ret;
guint percentage;
ret = gpm_brightness_get (brightness, &percentage);
if (!ret) {
g_warning ("failed to get output");
return;
}
g_debug ("emitting brightness-changed (%i)", percentage);
g_signal_emit (brightness, signals [BRIGHTNESS_CHANGED], 0, percentage);
}
/**
* gpm_brightness_filter_xevents:
**/
static GdkFilterReturn
gpm_brightness_filter_xevents (GdkXEvent *xevent, GdkEvent *event, gpointer data)
{
GpmBrightness *brightness = GPM_BRIGHTNESS (data);
if (event->type == GDK_NOTHING)
return GDK_FILTER_CONTINUE;
gpm_brightness_may_have_changed (brightness);
return GDK_FILTER_CONTINUE;
}
static void gpm_brightness_update_cache (GpmBrightness *brightness);
/**
* gpm_brightness_monitors_changed:
**/
static void
gpm_brightness_monitors_changed (GdkScreen *screen, GpmBrightness *brightness)
{
g_return_if_fail (GPM_IS_BRIGHTNESS (brightness));
gpm_brightness_update_cache (brightness);
}
/**
* gpm_brightness_update_cache:
**/
static void
gpm_brightness_update_cache (GpmBrightness *brightness)
{
guint length;
Window root;
GdkScreen *gscreen;
GdkDisplay *display;
XRRScreenResources *resource;
g_return_if_fail (GPM_IS_BRIGHTNESS (brightness));
/* invalidate and remove all the previous entries */
length = brightness->priv->resources->len;
if (length > 0)
g_ptr_array_set_size (brightness->priv->resources, 0);
display = gdk_display_get_default ();
gscreen = gdk_display_get_default_screen (display);
/* if we have not setup the changed on the monitor, set it here */
if (g_object_get_data (G_OBJECT (gscreen), "gpk-set-monitors-changed") == NULL) {
g_debug ("watching ::monitors_changed on %p", gscreen);
g_object_set_data (G_OBJECT (gscreen), "gpk-set-monitors-changed", (gpointer) "true");
g_signal_connect (G_OBJECT (gscreen), "monitors_changed",
G_CALLBACK (gpm_brightness_monitors_changed), brightness);
}
root = RootWindow (brightness->priv->dpy, 0);
gdk_x11_display_error_trap_push (display);
resource = XRRGetScreenResourcesCurrent (brightness->priv->dpy, root);
if (gdk_x11_display_error_trap_pop (display) || resource == NULL) {
g_warning ("failed to XRRGetScreenResourcesCurrent");
}
if (resource != NULL) {
g_debug ("adding resource %p", resource);
g_ptr_array_add (brightness->priv->resources, resource);
}
}
/**
* gpm_brightness_has_hw:
**/
gboolean
gpm_brightness_has_hw (GpmBrightness *brightness)
{
g_return_val_if_fail (GPM_IS_BRIGHTNESS (brightness), FALSE);
/* use XRandR first */
if (brightness->priv->has_extension)
return TRUE;
/* fallback to legacy access */
if (brightness->priv->extension_levels < 0)
brightness->priv->extension_levels = gpm_brightness_helper_get_value ("get-max-brightness");
if (brightness->priv->extension_levels > 0)
return TRUE;
return FALSE;
}
/**
* gpm_brightness_finalize:
**/
static void
gpm_brightness_finalize (GObject *object)
{
GpmBrightness *brightness;
g_return_if_fail (object != NULL);
g_return_if_fail (GPM_IS_BRIGHTNESS (object));
brightness = GPM_BRIGHTNESS (object);
g_ptr_array_unref (brightness->priv->resources);
gdk_window_remove_filter (brightness->priv->root_window,
gpm_brightness_filter_xevents, brightness);
G_OBJECT_CLASS (gpm_brightness_parent_class)->finalize (object);
}
/**
* gpm_brightness_class_init:
**/
static void
gpm_brightness_class_init (GpmBrightnessClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gpm_brightness_finalize;
signals [BRIGHTNESS_CHANGED] =
g_signal_new ("brightness-changed",
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GpmBrightnessClass, brightness_changed),
NULL, NULL, g_cclosure_marshal_VOID__UINT,
G_TYPE_NONE, 1, G_TYPE_UINT);
}
/**
* gpm_brightness_init:
* @brightness: This brightness class instance
**/
static void
gpm_brightness_init (GpmBrightness *brightness)
{
GdkScreen *screen;
GdkDisplay *display;
int event_base;
int ignore;
brightness->priv = gpm_brightness_get_instance_private (brightness);
brightness->priv->cache_trusted = FALSE;
brightness->priv->has_changed_events = FALSE;
brightness->priv->cache_percentage = 0;
brightness->priv->hw_changed = FALSE;
brightness->priv->extension_levels = -1;
brightness->priv->resources = g_ptr_array_new_with_free_func ((GDestroyNotify) XRRFreeScreenResources);
/* can we do this */
brightness->priv->has_extension = gpm_brightness_setup_display (brightness);
if (brightness->priv->has_extension == FALSE)
g_debug ("no XRANDR extension");
screen = gdk_screen_get_default ();
brightness->priv->root_window = gdk_screen_get_root_window (screen);
display = gdk_display_get_default ();
/* as we a filtering by a window, we have to add an event type */
if (!XRRQueryExtension (GDK_DISPLAY_XDISPLAY (gdk_display_get_default()), &event_base, &ignore)) {
g_warning ("can't get event_base for XRR");
}
gdk_x11_register_standard_event_type (display, event_base, RRNotify + 1);
gdk_window_add_filter (brightness->priv->root_window,
gpm_brightness_filter_xevents, brightness);
/* don't abort on error */
gdk_x11_display_error_trap_push (display);
XRRSelectInput (GDK_DISPLAY_XDISPLAY (gdk_display_get_default()),
GDK_WINDOW_XID (brightness->priv->root_window),
RRScreenChangeNotifyMask |
RROutputPropertyNotifyMask); /* <--- the only one we need, but see rh:345551 */
gdk_display_flush (display);
if (gdk_x11_display_error_trap_pop (display))
g_warning ("failed to select XRRSelectInput");
/* create cache of XRRScreenResources as XRRGetScreenResources() is slow */
gpm_brightness_update_cache (brightness);
}
/**
* gpm_brightness_new:
* Return value: A new brightness class instance.
* Can return NULL if no suitable hardware is found.
**/
GpmBrightness *
gpm_brightness_new (void)
{
if (gpm_brightness_object != NULL) {
g_object_ref (gpm_brightness_object);
} else {
gpm_brightness_object = g_object_new (GPM_TYPE_BRIGHTNESS, NULL);
g_object_add_weak_pointer (gpm_brightness_object, &gpm_brightness_object);
}
return GPM_BRIGHTNESS (gpm_brightness_object);
}
|