88#if HAS_MUTTER50
99[DBus (name = " io.elementary.gala.BrightnessManager" )]
1010public class Gala.BrightnessManager : GLib .Object {
11+ [DBus (visible = false )]
12+ public signal uint ? monitor_added (MonitorBrightness monitor_brightness );
13+ [DBus (visible = false )]
14+ public signal void monitor_removed (uint connection_id );
15+
16+ private int _brightness;
1117 public int brightness {
1218 get {
13- if (backlight == null ) {
19+ if (exposed_monitors . size () == 0 )
1420 return - 1 ;
15- }
16- int min, max;
17- int value ;
18- backlight. get_brightness_info (out min, out max);
19- value = backlight. get_brightness ();
20- return ((value - min) * 100 ) / (max - min);
21+
22+
23+ return _brightness;
2124 }
2225 set {
23- if (brightness == value || backlight == null ) {
26+ if (_brightness == value || exposed_monitors . size () == 0 )
2427 return ;
25- }
26- int min, max;
27- backlight. get_brightness_info (out min, out max);
28- backlight. set_brightness ((int ) (value * (max - min) / 100 ) + min);
28+
29+ foreach (var monitor_brightness in exposed_monitors. get_values ())
30+ monitor_brightness. brightness = value ;
31+
32+ _brightness = value ;
2933 }
3034 }
3135
3236 [DBus (visible = false )]
3337 public unowned Meta.Display display { private get ; construct; }
38+
39+ private GLib . HashTable<unowned Meta . Monitor , MonitorBrightness > exposed_monitors;
40+ private GLib . HashTable<unowned Meta . Monitor , uint > connection_ids;
3441 private unowned Meta . MonitorManager monitor_manager;
35- private unowned Meta . Backlight ? backlight;
3642
3743 public BrightnessManager (Meta .Display display ) {
3844 Object (
@@ -43,6 +49,11 @@ public class Gala.BrightnessManager : GLib.Object {
4349 construct {
4450 monitor_manager = display. get_context (). get_backend (). get_monitor_manager ();
4551 monitor_manager. monitors_changed. connect (update_available_backlights);
52+ exposed_monitors = new GLib .HashTable<unowned Meta . Monitor , MonitorBrightness > (null , null );
53+ connection_ids = new GLib .HashTable<unowned Meta . Monitor , uint> (null , null );
54+
55+ // TODO: bind to value from gschema
56+ _brightness = 100 ;
4657
4758 var keybinding_settings = new GLib .Settings (" io.elementary.desktop.wm.keybindings" );
4859 display. add_keybinding (" brightness-brighter" , keybinding_settings, Meta . KeyBindingFlags . NONE , () = > {
@@ -51,16 +62,39 @@ public class Gala.BrightnessManager : GLib.Object {
5162 display. add_keybinding (" brightness-dimmer" , keybinding_settings, Meta . KeyBindingFlags . NONE , () = > {
5263 brightness - = 5 ;
5364 });
54-
55- update_available_backlights ();
5665 }
5766
58- private void update_available_backlights () {
59- backlight = null ;
67+ [ DBus ( visible = false )]
68+ public void update_available_backlights () {
6069 unowned var monitors = monitor_manager. get_monitors ();
70+ var new_monitors = monitors. copy ();
71+ var old_monitors = exposed_monitors. get_keys ();
6172 foreach (unowned var monitor in monitors) {
62- if (monitor. is_primary () && monitor. is_active ()) {
63- backlight = monitor. get_backlight ();
73+ if (monitor. get_backlight () == null )
74+ continue ;
75+
76+ if (old_monitors. find (monitor) != null ) {
77+ old_monitors. remove (monitor);
78+ new_monitors. remove (monitor);
79+ }
80+ }
81+
82+ if (new_monitors. length () > 0 ) {
83+ foreach (var monitor in new_monitors) {
84+ var monitor_brightness = new MonitorBrightness (monitor);
85+ var connection_id = monitor_added (monitor_brightness);
86+ if (connection_id != null ) {
87+ exposed_monitors. insert (monitor, monitor_brightness);
88+ connection_ids. insert (monitor, connection_id);
89+ }
90+ }
91+ }
92+
93+ if (old_monitors. length () > 0 ) {
94+ foreach (var monitor in old_monitors) {
95+ monitor_removed (connection_ids. get (monitor));
96+ exposed_monitors. remove (monitor);
97+ connection_ids. remove (monitor);
6498 }
6599 }
66100 }
0 commit comments