diff options
author | Arwed Meyer <[email protected]> | 2024-10-04 18:04:04 +0200 |
---|---|---|
committer | Victor Kareh <[email protected]> | 2025-09-26 15:50:40 +0000 |
commit | 89007db745154848beba3324edf64402fd94c46a (patch) | |
tree | ba00be879fa42b1b22959d0b4e0d242b5a36ba03 | |
parent | 3bde0327576a0a9e694816a1968bd5d79009d62b (diff) | |
download | mate-power-manager-89007db745154848beba3324edf64402fd94c46a.tar.bz2 mate-power-manager-89007db745154848beba3324edf64402fd94c46a.tar.xz |
backlight: Save brightness changes running on battery
Also save brightness config changes when running on battery.
To do that, calculate backlight dim factor and ac brightness factor from
master percentage value so that ac factor is only changed if brightness
is further increased when battery dim factor is already zero.
This behaviour seems less random to me than only saving changes when
running on AC.
-rw-r--r-- | src/gpm-backlight.c | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/src/gpm-backlight.c b/src/gpm-backlight.c index c4e95ab..3df80ef 100644 --- a/src/gpm-backlight.c +++ b/src/gpm-backlight.c @@ -405,6 +405,47 @@ gpm_backlight_client_changed_cb (UpClient *client, GParamSpec *pspec, GpmBacklig } /** + * gpm_backlight_save_settings: + * @backlight: This class instance + * @percentage: Current brightess in percent + **/ +static void +gpm_backlight_save_settings (GpmBacklight *backlight, guint percentage) +{ + gint battery_reduce; + gfloat brightness_ac; + gboolean on_battery; + + backlight->priv->master_percentage = percentage; + g_object_get (backlight->priv->client, "on-battery", &on_battery, NULL); + if (on_battery) { + /* If using battery, saving settings needs a bit of trickery. + * IMHO this shows that using two factors for setting + * brightness level is a crooked idea to begin with, but + * this is still less random than only saving changes when + * running on AC. + */ + brightness_ac = g_settings_get_double (backlight->priv->settings, GPM_SETTINGS_BRIGHTNESS_AC); + battery_reduce = 100 - (gint) (percentage * 100.0f / brightness_ac); + if (battery_reduce < 0) { + /* Brightness set higher than brightness-ac - we have to adjust that value. */ + g_settings_set_double (backlight->priv->settings, GPM_SETTINGS_BRIGHTNESS_AC, + percentage * 1.0); + battery_reduce = 0; + g_debug ("saving brightness for ac supply: %u", percentage); + } + g_debug ("saving brightness for battery - master percent: %u; ac supply: %f; dim battery: %u", + percentage, brightness_ac, battery_reduce); + g_settings_set_uint (backlight->priv->settings, GPM_SETTINGS_BRIGHTNESS_DIM_BATT, (guint) battery_reduce); + } else { + /* if using AC power supply, save the new brightness settings */ + g_debug ("saving brightness for ac supply: %u", percentage); + g_settings_set_double (backlight->priv->settings, GPM_SETTINGS_BRIGHTNESS_AC, + percentage * 1.0); + } +} + +/** * gpm_backlight_button_pressed_cb: * @power: The power class instance * @type: The button type, e.g. "power" @@ -418,7 +459,6 @@ gpm_backlight_button_pressed_cb (GpmButton *button, const gchar *type, GpmBackli GError *error = NULL; guint percentage; gboolean hw_changed; - gboolean on_battery; g_debug ("Button press event type=%s", type); if (g_strcmp0 (type, GPM_BUTTON_BRIGHT_UP) == 0) { @@ -433,14 +473,7 @@ gpm_backlight_button_pressed_cb (GpmButton *button, const gchar *type, GpmBackli percentage); gpm_backlight_dialog_show (backlight); /* save the new percentage */ - backlight->priv->master_percentage = percentage; - /* if using AC power supply, save the new brightness settings */ - g_object_get (backlight->priv->client, "on-battery", &on_battery, NULL); - if (!on_battery) { - g_debug ("saving brightness for ac supply: %u", percentage); - g_settings_set_double (backlight->priv->settings, GPM_SETTINGS_BRIGHTNESS_AC, - percentage*1.0); - } + gpm_backlight_save_settings (backlight, percentage); } /* we emit a signal for the brightness applet */ if (ret && hw_changed) { @@ -459,14 +492,7 @@ gpm_backlight_button_pressed_cb (GpmButton *button, const gchar *type, GpmBackli percentage); gpm_backlight_dialog_show (backlight); /* save the new percentage */ - backlight->priv->master_percentage = percentage; - /* if using AC power supply, save the new brightness settings */ - g_object_get (backlight->priv->client, "on-battery", &on_battery, NULL); - if (!on_battery) { - g_debug ("saving brightness for ac supply: %u", percentage); - g_settings_set_double (backlight->priv->settings, GPM_SETTINGS_BRIGHTNESS_AC, - percentage*1.0); - } + gpm_backlight_save_settings (backlight, percentage); } /* we emit a signal for the brightness applet */ if (ret && hw_changed) { |