Skip to content
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

Screen Dimming #651

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions src/daemon/screenlock.vala
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@
namespace Budgie {
const string DBUS_SCREENLOCK = "org.buddiesofbudgie.BudgieScreenlock";
const string DBUS_SCREENLOCK_PATH = "/org/buddiesofbudgie/Screenlock";
const string POWERSCREEN_DBUS_NAME = "org.gnome.SettingsDaemon.Power";
const string POWERSCREEN_DBUS_OBJECT_PATH = "/org/gnome/SettingsDaemon/Power";

[DBus (name="org.gnome.SettingsDaemon.Power.Screen")]
public interface PowerScreenRemote : GLib.Object {
public abstract int32 Brightness { get; set; }
}

[DBus (name="org.buddiesofbudgie.BudgieScreenlock")]
public class Screenlock {
static Screenlock? instance;

// Screen Dim capability
PowerScreenRemote? powerscreen_proxy = null;
bool isdimmable = false;
int32 current_brightness = 0;

// connections to various schemas used in screenlocking
private GLib.Settings power;
private GLib.Settings session;
Expand All @@ -34,6 +46,16 @@ namespace Budgie {
private GLib.GenericArray<Up.Device> devices;
private bool battery_mode;

private string calculate_dim() {
isdimmable = this.power.get_boolean("idle-dim");

if (!isdimmable) return "";

var dbus_call = "timeout 30 'dbus-send --type=method_call --dest=org.buddiesofbudgie.BudgieScreenlock /org/buddiesofbudgie/Screenlock org.buddiesofbudgie.BudgieScreenlock.Dim' ";
dbus_call += "resume 'dbus-send --type=method_call --dest=org.buddiesofbudgie.BudgieScreenlock /org/buddiesofbudgie/Screenlock org.buddiesofbudgie.BudgieScreenlock.Undim' ";
return dbus_call;
}

private string calculate_lockcommand() {
string output = "";

Expand Down Expand Up @@ -155,6 +177,8 @@ namespace Budgie {

new_idle = "swayidle -w ";

new_idle += calculate_dim();

if (idle_delay !=0 ) {
new_idle += "timeout " + idle_delay.to_string() + " 'wlopm --off \\*' resume 'wlopm --on \\*' ";
}
Expand Down Expand Up @@ -196,6 +220,15 @@ namespace Budgie {
}
}

/* Hold onto our PowerScreen proxy ref */
void on_powerscreen_get(Object? o, AsyncResult? res) {
try {
powerscreen_proxy = Bus.get_proxy.end(res);
} catch (Error e) {
warning("Failed to get PowerScreen proxy: %s", e.message);
}
}

[DBus (visible = false)]
public void setup_dbus() {
/* Hook up screenlock dbus */
Expand All @@ -205,6 +238,27 @@ namespace Budgie {
() => {} );
}

public async void dim() throws GLib.DBusError, GLib.IOError {
if (powerscreen_proxy == null) {
return;
}

current_brightness = powerscreen_proxy.Brightness;
int32 idle_brightness = power.get_int("idle-brightness");

if (current_brightness > idle_brightness) {
powerscreen_proxy.Brightness = idle_brightness;
}
}

public async void undim() throws GLib.DBusError, GLib.IOError {
if (powerscreen_proxy == null) {
return;
}

powerscreen_proxy.Brightness = current_brightness;
}

void on_bus_acquired(DBusConnection conn) {
try {
conn.register_object(DBUS_SCREENLOCK_PATH, this);
Expand All @@ -215,7 +269,6 @@ namespace Budgie {

public async void lock() throws GLib.DBusError, GLib.IOError {
if (!all_apps) {
yield;
return;
}

Expand All @@ -224,8 +277,6 @@ namespace Budgie {
} catch (SpawnError e) {
print("Error: %s\n", e.message);
}

yield;
}

[DBus (visible = false)]
Expand Down Expand Up @@ -272,6 +323,8 @@ namespace Budgie {
return;
}

Bus.get_proxy.begin<PowerScreenRemote>(BusType.SESSION, POWERSCREEN_DBUS_NAME, POWERSCREEN_DBUS_OBJECT_PATH, 0, null, on_powerscreen_get);

// Connect to upower and get notifications for all batteries and power-supplies to see if on battery mode
client = new Up.Client();
client.notify.connect(this.client_daemon);
Expand All @@ -289,7 +342,8 @@ namespace Budgie {
string[] search = { "sleep-inactive-ac-timeout",
"sleep-inactive-ac-type",
"sleep-inactive-battery-timeout",
"sleep-inactive-battery-type"};
"sleep-inactive-battery-type",
"idle-dim"};

if (key in search) {
calculate_idle();
Expand Down
Loading