-
-
Notifications
You must be signed in to change notification settings - Fork 83
Inrtoduce BrightnessManager dbus interface #2831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+142
−3
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Copyright 2026 elementary, Inc. (https://elementary.io) | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| * | ||
| * Authored by: Denis Garaev <garaevdi@outlook.com> | ||
| */ | ||
|
|
||
| #if HAS_MUTTER50 | ||
| public struct Gala.Monitor { | ||
| unowned Meta.Monitor meta_mointor; | ||
| unowned Meta.Backlight backlight; | ||
| ulong connection_id; | ||
| } | ||
|
|
||
| [DBus (name = "io.elementary.gala.BrightnessManager")] | ||
| public class Gala.BrightnessManager : GLib.Object { | ||
| public signal void monitors_changed (); | ||
| public signal void monitor_brightness_changed (uint index, uint value); | ||
|
|
||
| [DBus (visible = false)] | ||
| public unowned Meta.Display display { private get; construct; } | ||
|
|
||
| private unowned Meta.MonitorManager monitor_manager; | ||
| private GLib.List<Gala.Monitor?> monitors; | ||
|
|
||
| public BrightnessManager (Meta.Display display) { | ||
| Object ( | ||
| display: display | ||
| ); | ||
| } | ||
|
|
||
| construct { | ||
| monitor_manager = display.get_context ().get_backend ().get_monitor_manager (); | ||
| monitor_manager.monitors_changed.connect (monitors_changed_cb); | ||
| monitors = new GLib.List<Gala.Monitor?> (); | ||
| monitors_changed_cb (); | ||
|
|
||
| var keybinding_settings = new GLib.Settings ("io.elementary.desktop.wm.keybindings"); | ||
| display.add_keybinding ("brightness-brighter", keybinding_settings, Meta.KeyBindingFlags.NONE, () => { | ||
| try { | ||
| set_monitor_brightness (0, get_monitor_brightness (0) + 5); | ||
| } catch {} | ||
| }); | ||
| display.add_keybinding ("brightness-dimmer", keybinding_settings, Meta.KeyBindingFlags.NONE, () => { | ||
| try { | ||
| int delta = get_monitor_brightness (0) - 5; | ||
| if (delta < 0) | ||
| delta = 0; | ||
|
|
||
| set_monitor_brightness (0, delta); | ||
| } catch {} | ||
| }); | ||
| } | ||
|
|
||
| public void set_monitor_brightness (uint index, uint value) throws GLib.Error { | ||
| if (index < monitors.length ()) { | ||
| var backlight = monitors.nth_data (index).backlight; | ||
| int min, max; | ||
| backlight.get_brightness_info (out min, out max); | ||
| backlight.set_brightness ((int) (value * (max - min) / 100) + min); | ||
| } | ||
| } | ||
|
|
||
| public int get_monitor_brightness (uint index) throws GLib.Error { | ||
| if (index < monitors.length ()) { | ||
| var backlight = monitors.nth_data (index).backlight; | ||
| int min, max; | ||
| int value; | ||
| backlight.get_brightness_info (out min, out max); | ||
| value = backlight.get_brightness (); | ||
| return (((value - min) * 100) / (max - min)).clamp (0, 100); | ||
| } else | ||
| return -1; | ||
| } | ||
|
|
||
| public string get_monitor_data (uint index) throws GLib.Error { | ||
| if (index < monitors.length ()) | ||
| return monitors.nth_data (index).meta_mointor.get_display_name (); | ||
| else | ||
| return ""; | ||
| } | ||
|
|
||
| public uint get_monitor_count () throws GLib.Error { | ||
| return monitors.length (); | ||
| } | ||
|
|
||
| private void monitors_changed_cb () { | ||
| foreach (var data in monitors) { | ||
| data.backlight.disconnect (data.connection_id); | ||
| monitors.remove (data); | ||
| } | ||
|
|
||
| var meta_monitors = monitor_manager.get_monitors ().copy (); | ||
| foreach (var meta_monitor in meta_monitors) { | ||
| var backlight = meta_monitor.get_backlight (); | ||
| var connection_id = backlight.notify["brightness"].connect (monitor_brightness_changed_cb); | ||
| if (meta_monitor.is_primary ()) | ||
| monitors.insert ({ meta_monitor, backlight, connection_id }, 0); | ||
| else | ||
| monitors.append ({ meta_monitor, backlight, connection_id }); | ||
| } | ||
|
|
||
| monitors_changed (); | ||
| } | ||
|
|
||
| private void monitor_brightness_changed_cb (GLib.Object sender, GLib.ParamSpec prop) { | ||
| var backlight = (Meta.Backlight) sender; | ||
| var list = monitors.search<unowned Meta.Backlight> (backlight, (monitor, backlight) => { | ||
| if (monitor.backlight == backlight) | ||
| return 0; | ||
| else | ||
| return -1; | ||
| }).copy (); | ||
|
|
||
| if (list == null || list.is_empty ()) | ||
| return; | ||
|
|
||
| var index = monitors.index (list.data); | ||
| monitor_brightness_changed (index, get_monitor_brightness (index)); | ||
| } | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.