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

Brightness fixes #403

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions data/org.mate.power-manager.gschema.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
<summary>Reduce the backlight brightness when on battery power</summary>
<description>If the screen should be reduced in brightness when the computer is on battery power.</description>
</key>
<key name="kbd-backlight-enable" type="b">
<default>true</default>
<summary>Allow keyboard backlight brightness adjustment</summary>
<description>If the keyboard backlight brightness should be switched automatically.</description>
</key>
<key name="kbd-backlight-battery-reduce" type="b">
<default>true</default>
<summary>Reduce the keyboard backlight when on battery power</summary>
Expand Down
60 changes: 43 additions & 17 deletions src/gpm-backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,47 @@ gpm_backlight_client_changed_cb (UpClient *client, GParamSpec *pspec, GpmBacklig
gpm_backlight_brightness_evaluate_and_set (backlight, FALSE, TRUE);
}

/**
* 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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/gpm-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ G_BEGIN_DECLS
#define GPM_SETTINGS_BRIGHTNESS_DIM_BATT "brightness-dim-battery"

/* keyboard backlight */
#define GPM_SETTINGS_KBD_BACKLIGHT_ENABLE "kbd-backlight-enable"
#define GPM_SETTINGS_KBD_BACKLIGHT_BATT_REDUCE "kbd-backlight-battery-reduce"
#define GPM_SETTINGS_KBD_BRIGHTNESS_ON_AC "kbd-brightness-on-ac"
#define GPM_SETTINGS_KBD_BRIGHTNESS_DIM_BY_ON_BATT "kbd-brightness-dim-by-on-battery"
Expand Down
34 changes: 16 additions & 18 deletions src/gpm-kbd-backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,16 @@ gpm_kbd_backlight_evaluate_power_source_and_set (GpmKbdBacklight *backlight)
gfloat scale;
gboolean on_battery;
gboolean battery_reduce;
gboolean do_kbd_backlight;
guint value;
gboolean ret;

do_kbd_backlight = g_settings_get_boolean (backlight->priv->settings, GPM_SETTINGS_KBD_BACKLIGHT_ENABLE);
if (do_kbd_backlight == FALSE) {
g_warning ("policy is no dimming");
return FALSE;
}

brightness = backlight->priv->master_percentage;

g_object_get (backlight->priv->client,
Expand All @@ -344,25 +351,18 @@ gpm_kbd_backlight_evaluate_power_source_and_set (GpmKbdBacklight *backlight)

battery_reduce = g_settings_get_boolean (backlight->priv->settings, GPM_SETTINGS_KBD_BACKLIGHT_BATT_REDUCE);

if (on_battery) {
if (battery_reduce) {
value = g_settings_get_int (backlight->priv->settings, GPM_SETTINGS_KBD_BRIGHTNESS_DIM_BY_ON_BATT);

if (value > 100) {
g_warning ("Cannot scale brightness down by more than 100%%. Scaling by 50%%");
value = 50;
}

scale = (100 - value) / 100.0f;
brightness *= scale;
if (on_battery && battery_reduce) {
value = g_settings_get_int (backlight->priv->settings, GPM_SETTINGS_KBD_BRIGHTNESS_DIM_BY_ON_BATT);

value = (guint) brightness;

} else {
// do not change keyboard backlight
return TRUE;
if (value > 100) {
g_warning ("Cannot scale brightness down by more than 100%%. Scaling by 50%%");
value = 50;
}

scale = (100 - value) / 100.0f;
brightness *= scale;

value = (guint) brightness;
} else {
value = g_settings_get_int (backlight->priv->settings, GPM_SETTINGS_KBD_BRIGHTNESS_ON_AC);
}
Expand Down Expand Up @@ -419,8 +419,6 @@ gpm_kbd_backlight_button_pressed_cb (GpmButton *button,
static guint saved_brightness;
gboolean ret;

saved_brightness = backlight->priv->master_percentage;

if (g_strcmp0 (type, GPM_BUTTON_KBD_BRIGHT_UP) == 0) {
ret = gpm_kbd_backlight_brightness_up (backlight);

Expand Down