|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | #if HAS_MUTTER50 |
| 9 | +public struct Gala.Monitor { |
| 10 | + unowned Meta.Monitor meta_mointor; |
| 11 | + unowned Meta.Backlight backlight; |
| 12 | + ulong connection_id; |
| 13 | +} |
| 14 | + |
9 | 15 | [DBus (name = "io.elementary.gala.BrightnessManager")] |
10 | 16 | public 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; |
17 | | - public int brightness { |
18 | | - get { |
19 | | - if (exposed_monitors.size () == 0) |
20 | | - return -1; |
21 | | - |
22 | | - |
23 | | - return _brightness; |
24 | | - } |
25 | | - set { |
26 | | - if (_brightness == value || exposed_monitors.size () == 0) |
27 | | - return; |
28 | | - |
29 | | - foreach (var monitor_brightness in exposed_monitors.get_values ()) |
30 | | - monitor_brightness.brightness = value; |
31 | | - |
32 | | - _brightness = value; |
33 | | - } |
34 | | - } |
| 17 | + public signal void monitors_changed (); |
| 18 | + public signal void monitor_brightness_changed (uint index, uint value); |
35 | 19 |
|
36 | 20 | [DBus (visible = false)] |
37 | 21 | public unowned Meta.Display display { private get; construct; } |
38 | 22 |
|
39 | | - private GLib.HashTable<unowned Meta.Monitor, MonitorBrightness> exposed_monitors; |
40 | | - private GLib.HashTable<unowned Meta.Monitor, uint> connection_ids; |
41 | 23 | private unowned Meta.MonitorManager monitor_manager; |
| 24 | + private GLib.List<Gala.Monitor?> monitors; |
42 | 25 |
|
43 | 26 | public BrightnessManager (Meta.Display display) { |
44 | 27 | Object ( |
45 | | - display: display |
| 28 | + display: display |
46 | 29 | ); |
47 | 30 | } |
48 | 31 |
|
49 | 32 | construct { |
50 | 33 | monitor_manager = display.get_context ().get_backend ().get_monitor_manager (); |
51 | | - 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; |
| 34 | + monitor_manager.monitors_changed.connect (monitors_changed_cb); |
| 35 | + monitors = new GLib.List<Gala.Monitor?> (); |
| 36 | + monitors_changed_cb (); |
57 | 37 |
|
58 | 38 | var keybinding_settings = new GLib.Settings ("io.elementary.desktop.wm.keybindings"); |
59 | 39 | display.add_keybinding ("brightness-brighter", keybinding_settings, Meta.KeyBindingFlags.NONE, () => { |
60 | | - brightness += 5; |
| 40 | + try { |
| 41 | + set_monitor_brightness (0, get_monitor_brightness (0) + 5); |
| 42 | + } catch {} |
61 | 43 | }); |
62 | 44 | display.add_keybinding ("brightness-dimmer", keybinding_settings, Meta.KeyBindingFlags.NONE, () => { |
63 | | - brightness -= 5; |
| 45 | + try { |
| 46 | + int delta = get_monitor_brightness (0) - 5; |
| 47 | + if (delta < 0) |
| 48 | + delta = 0; |
| 49 | + |
| 50 | + set_monitor_brightness (0, delta); |
| 51 | + } catch {} |
64 | 52 | }); |
65 | 53 | } |
66 | 54 |
|
67 | | - [DBus (visible = false)] |
68 | | - public void update_available_backlights () { |
69 | | - unowned var monitors = monitor_manager.get_monitors (); |
70 | | - var new_monitors = monitors.copy (); |
71 | | - var old_monitors = exposed_monitors.get_keys (); |
72 | | - foreach (unowned var monitor in monitors) { |
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 | | - } |
| 55 | + public void set_monitor_brightness (uint index, uint value) throws GLib.Error { |
| 56 | + if (index < monitors.length ()) { |
| 57 | + var backlight = monitors.nth_data (index).backlight; |
| 58 | + int min, max; |
| 59 | + backlight.get_brightness_info (out min, out max); |
| 60 | + backlight.set_brightness ((int) (value * (max - min) / 100) + min); |
80 | 61 | } |
| 62 | + } |
| 63 | + |
| 64 | + public int get_monitor_brightness (uint index) throws GLib.Error { |
| 65 | + if (index < monitors.length ()) { |
| 66 | + var backlight = monitors.nth_data (index).backlight; |
| 67 | + int min, max; |
| 68 | + int value; |
| 69 | + backlight.get_brightness_info (out min, out max); |
| 70 | + value = backlight.get_brightness (); |
| 71 | + return (((value - min) * 100) / (max - min)).clamp (0, 100); |
| 72 | + } else |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + public string get_monitor_data (uint index) throws GLib.Error { |
| 77 | + if (index < monitors.length ()) |
| 78 | + return monitors.nth_data (index).meta_mointor.get_display_name (); |
| 79 | + else |
| 80 | + return ""; |
| 81 | + } |
| 82 | + |
| 83 | + public uint get_monitor_count () throws GLib.Error { |
| 84 | + return monitors.length (); |
| 85 | + } |
81 | 86 |
|
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 | | - } |
| 87 | + private void monitors_changed_cb () { |
| 88 | + foreach (var data in monitors) { |
| 89 | + data.backlight.disconnect (data.connection_id); |
| 90 | + monitors.remove (data); |
91 | 91 | } |
92 | 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); |
98 | | - } |
| 93 | + var meta_monitors = monitor_manager.get_monitors ().copy (); |
| 94 | + foreach (var meta_monitor in meta_monitors) { |
| 95 | + var backlight = meta_monitor.get_backlight (); |
| 96 | + var connection_id = backlight.notify["brightness"].connect (monitor_brightness_changed_cb); |
| 97 | + if (meta_monitor.is_primary ()) |
| 98 | + monitors.insert ({ meta_monitor, backlight, connection_id }, 0); |
| 99 | + else |
| 100 | + monitors.append ({ meta_monitor, backlight, connection_id }); |
99 | 101 | } |
| 102 | + |
| 103 | + monitors_changed (); |
| 104 | + } |
| 105 | + |
| 106 | + private void monitor_brightness_changed_cb (GLib.Object sender, GLib.ParamSpec prop) { |
| 107 | + var backlight = (Meta.Backlight) sender; |
| 108 | + var list = monitors.search<unowned Meta.Backlight> (backlight, (monitor, backlight) => { |
| 109 | + if (monitor.backlight == backlight) |
| 110 | + return 0; |
| 111 | + else |
| 112 | + return -1; |
| 113 | + }).copy (); |
| 114 | + |
| 115 | + if (list == null || list.is_empty ()) |
| 116 | + return; |
| 117 | + |
| 118 | + var index = monitors.index (list.data); |
| 119 | + monitor_brightness_changed (index, get_monitor_brightness (index)); |
100 | 120 | } |
101 | 121 | } |
102 | 122 | #endif |
0 commit comments