From f1a9ebd2678c04de88de11533693b6d818244b38 Mon Sep 17 00:00:00 2001 From: Sepherosa Ziehau Date: Sun, 16 Aug 2015 21:22:35 +0800 Subject: [PATCH] powerd: Set backlight to the specified value, if AC line is unplugged. --- usr.sbin/powerd/powerd.8 | 6 ++++- usr.sbin/powerd/powerd.c | 57 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/usr.sbin/powerd/powerd.8 b/usr.sbin/powerd/powerd.8 index 6bb340dd7a..ad7d315ce1 100644 --- a/usr.sbin/powerd/powerd.8 +++ b/usr.sbin/powerd/powerd.8 @@ -1,7 +1,7 @@ .\" (c) Copyright 2010 by Matthew Dillon and Dima Ruban. Permission to .\" use and distribute based on the DragonFly copyright. .\" -.Dd July 17, 2015 +.Dd August 16, 2015 .Dt POWERD 8 .Os .Sh NAME @@ -10,6 +10,7 @@ .Sh SYNOPSIS .Nm .Op Fl cdeftQ +.Op Fl b Ar backlight_percentage .Op Fl h Ar highest_freq .Op Fl l Ar lowest_freq .Op Fl p Ar hysteresis @@ -37,6 +38,9 @@ and shutdown the machine after lingering for a little while .Pp The options are as follows: .Bl -tag -width ".Fl p Ar hysteresis" +.It Fl b Ar backlight_percentage +Set backlight to the specified percentage, +if AC line is not plugged in. .It Fl c Enable mwait C-state hint adjustment, if it is available. diff --git a/usr.sbin/powerd/powerd.c b/usr.sbin/powerd/powerd.c index 436bd7653d..91d5580121 100644 --- a/usr.sbin/powerd/powerd.c +++ b/usr.sbin/powerd/powerd.c @@ -159,6 +159,9 @@ static int BatShutdownLinger = -1; static int BatShutdownLingerSet = 60; /* unit: sec */ static int BatShutdownLingerCnt; static int BatShutdownAudioAlert = 1; +static int BackLightPct = 100; +static int OldBackLightLevel; +static int BackLightDown; static void sigintr(int signo); @@ -174,8 +177,11 @@ main(int ac, char **av) srt = 8.0; /* time for samples - 8 seconds */ pollrate = 1.0; /* polling rate in seconds */ - while ((ch = getopt(ac, av, "cdefh:l:p:r:tu:B:L:P:QT:")) != -1) { + while ((ch = getopt(ac, av, "b:cdefh:l:p:r:tu:B:L:P:QT:")) != -1) { switch(ch) { + case 'b': + BackLightPct = strtol(optarg, NULL, 10); + break; case 'c': AdjustCstate = 1; break; @@ -246,6 +252,11 @@ main(int ac, char **av) exit(1); } + if (BackLightPct > 100 || BackLightPct <= 0) { + fprintf(stderr, "Invalid backlight setting, ignore\n"); + BackLightPct = 100; + } + TriggerDown = TriggerUp - (TriggerUp * (double) Hysteresis / 100); /* @@ -589,7 +600,8 @@ usage(void) "[-h highest_freq] [-l lowest_freq] " "[-r poll_interval] [-u trigger_up] " "[-B min_battery_life] [-L low_battery_linger] " - "[-P battery_poll_interval] [-T sample_interval]\n"); + "[-P battery_poll_interval] [-T sample_interval] " + "[-b backlight]\n"); exit(1); } @@ -704,9 +716,50 @@ mon_battery(void) if (acline) { BatShutdownLinger = -1; BatShutdownLingerCnt = 0; + if (BackLightDown) { + BackLightDown = 0; + sysctlbyname("hw.backlight_level", NULL, NULL, + &OldBackLightLevel, sizeof(OldBackLightLevel)); + } return 1; } + if (!BackLightDown && BackLightPct != 100) { + int backlight_max, backlight; + + len = sizeof(backlight_max); + if (sysctlbyname("hw.backlight_max", &backlight_max, &len, + NULL, 0) < 0) { + /* No more backlight adjustment */ + BackLightPct = 100; + goto after_backlight; + } + + len = sizeof(OldBackLightLevel); + if (sysctlbyname("hw.backlight_level", &OldBackLightLevel, &len, + NULL, 0) < 0) { + /* No more backlight adjustment */ + BackLightPct = 100; + goto after_backlight; + } + + backlight = (backlight_max * BackLightPct) / 100; + if (backlight >= OldBackLightLevel) { + /* No more backlight adjustment */ + BackLightPct = 100; + goto after_backlight; + } + + if (sysctlbyname("hw.backlight_level", NULL, NULL, + &backlight, sizeof(backlight)) < 0) { + /* No more backlight adjustment */ + BackLightPct = 100; + goto after_backlight; + } + BackLightDown = 1; + } +after_backlight: + len = sizeof(life); if (sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0) < 0) return 1; -- 2.41.0